windowHostControllerProtocol.test.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. CONTROLLER_TASKS_INDEX_TOPIC,
  4. CONTROLLER_WORKSPACES_TOPIC,
  5. V4_METHODS,
  6. controllerSubscribeParamsSchema,
  7. isWindowHostControllerFrameGap,
  8. windowHostControllerTaskFrameSchema,
  9. windowHostControllerWorkspaceFrameSchema,
  10. windowHostTaskAddressSchema,
  11. } from "../src/zcode-protocol-v4/index.js";
  12. const remoteAddress = {
  13. remoteSessionId: "remote-session-1",
  14. workspacePath: "/work/demo",
  15. workspaceIdentity: "remote:ssh:dev:/work/demo",
  16. taskId: "task-1",
  17. };
  18. const remoteTask = {
  19. address: remoteAddress,
  20. meta: {
  21. taskId: "task-1",
  22. traceId: "trace-1",
  23. title: "Remote task",
  24. workspacePath: "/work/demo",
  25. workspaceIdentity: "remote:ssh:dev:/work/demo",
  26. createdAt: 1,
  27. updatedAt: 2,
  28. mode: "default" as const,
  29. },
  30. membership: {
  31. pinned: false,
  32. archived: false,
  33. active: true,
  34. },
  35. sourceAvailability: "online" as const,
  36. liveStatus: "running" as const,
  37. activity: {
  38. phase: "running" as const,
  39. lastActivityAt: 2,
  40. hasBackgroundWork: true,
  41. pendingInteractions: { permissionCount: 1, userInputCount: 0 },
  42. },
  43. };
  44. describe("R1 window Host Controller V4 protocol", () => {
  45. it("固定暴露两个窗口级 topic 和独立的 subscribe/resync/unsubscribe 方法", () => {
  46. expect(CONTROLLER_WORKSPACES_TOPIC).toBe("controller/workspaces");
  47. expect(CONTROLLER_TASKS_INDEX_TOPIC).toBe("controller/tasks-index");
  48. expect(V4_METHODS.controllerSubscribe).toBe("v4/controller/subscribe");
  49. expect(V4_METHODS.controllerResync).toBe("v4/controller/resync");
  50. expect(V4_METHODS.controllerUnsubscribe).toBe("v4/controller/unsubscribe");
  51. });
  52. it("subscribe 只接受 Controller topic,且调用方不能声明 clientMode", () => {
  53. expect(
  54. controllerSubscribeParamsSchema.safeParse({ topic: CONTROLLER_TASKS_INDEX_TOPIC }).success,
  55. ).toBe(true);
  56. expect(
  57. controllerSubscribeParamsSchema.safeParse({ topic: CONTROLLER_WORKSPACES_TOPIC }).success,
  58. ).toBe(true);
  59. expect(
  60. controllerSubscribeParamsSchema.safeParse({ topic: "sessions-index/workspace-1" }).success,
  61. ).toBe(false);
  62. expect(
  63. controllerSubscribeParamsSchema.safeParse({
  64. topic: CONTROLLER_TASKS_INDEX_TOPIC,
  65. clientMode: "web-remote-replayable",
  66. }).success,
  67. ).toBe(false);
  68. });
  69. it("remote task address 缺少 identity 时 fail-closed", () => {
  70. expect(windowHostTaskAddressSchema.safeParse(remoteAddress).success).toBe(true);
  71. expect(
  72. windowHostTaskAddressSchema.safeParse({
  73. remoteSessionId: "remote-session-1",
  74. workspacePath: "/work/demo",
  75. taskId: "task-1",
  76. }).success,
  77. ).toBe(false);
  78. });
  79. it("tasks-index snapshot 同时携带 seq 与 logEpoch,并拒绝 address/meta 串线", () => {
  80. const frame = {
  81. topic: CONTROLLER_TASKS_INDEX_TOPIC,
  82. subscriptionId: "subscription-1",
  83. logEpoch: "epoch-1",
  84. fromSeq: 0,
  85. toSeq: 7,
  86. sentAt: 10,
  87. payload: {
  88. kind: "snapshot" as const,
  89. snapshot: {
  90. protocolVersion: 1 as const,
  91. logEpoch: "epoch-1",
  92. tasks: [remoteTask],
  93. },
  94. },
  95. };
  96. expect(windowHostControllerTaskFrameSchema.safeParse(frame).success).toBe(true);
  97. expect(
  98. windowHostControllerTaskFrameSchema.safeParse({
  99. ...frame,
  100. payload: {
  101. kind: "snapshot",
  102. snapshot: {
  103. ...frame.payload.snapshot,
  104. tasks: [
  105. {
  106. ...remoteTask,
  107. activity: { ...remoteTask.activity, phase: "unknown" },
  108. },
  109. ],
  110. },
  111. },
  112. }).success,
  113. ).toBe(false);
  114. expect(
  115. windowHostControllerTaskFrameSchema.safeParse({
  116. ...frame,
  117. payload: {
  118. kind: "snapshot",
  119. snapshot: {
  120. ...frame.payload.snapshot,
  121. tasks: [
  122. {
  123. ...remoteTask,
  124. meta: { ...remoteTask.meta, taskId: "different-task" },
  125. },
  126. ],
  127. },
  128. },
  129. }).success,
  130. ).toBe(false);
  131. });
  132. it("workspaces snapshot 表达离线 source,但不携带 conversation/runtime payload", () => {
  133. const frame = {
  134. topic: CONTROLLER_WORKSPACES_TOPIC,
  135. subscriptionId: "subscription-workspaces-1",
  136. logEpoch: "workspace-epoch-1",
  137. fromSeq: 0,
  138. toSeq: 3,
  139. sentAt: 20,
  140. payload: {
  141. kind: "snapshot" as const,
  142. snapshot: {
  143. protocolVersion: 1 as const,
  144. logEpoch: "workspace-epoch-1",
  145. workspaces: [
  146. {
  147. remoteSessionId: "remote-session-1",
  148. workspacePath: "/work/demo",
  149. workspaceIdentity: "remote:ssh:dev:/work/demo",
  150. sourceAvailability: "offline" as const,
  151. connectionState: "disconnected" as const,
  152. },
  153. ],
  154. },
  155. },
  156. };
  157. expect(windowHostControllerWorkspaceFrameSchema.safeParse(frame).success).toBe(true);
  158. expect(
  159. windowHostControllerWorkspaceFrameSchema.safeParse({
  160. ...frame,
  161. payload: {
  162. kind: "snapshot",
  163. snapshot: {
  164. ...frame.payload.snapshot,
  165. runtimeSnapshot: { rows: [] },
  166. },
  167. },
  168. }).success,
  169. ).toBe(false);
  170. });
  171. it("按 subscription、logEpoch 和 fromSeq 检测 gap 并触发 resync", () => {
  172. const cursor = {
  173. subscriptionId: "subscription-1",
  174. logEpoch: "epoch-1",
  175. seq: 7,
  176. };
  177. expect(
  178. isWindowHostControllerFrameGap(cursor, {
  179. subscriptionId: "subscription-1",
  180. logEpoch: "epoch-1",
  181. fromSeq: 7,
  182. }),
  183. ).toBe(false);
  184. expect(
  185. isWindowHostControllerFrameGap(cursor, {
  186. subscriptionId: "subscription-1",
  187. logEpoch: "epoch-1",
  188. fromSeq: 9,
  189. }),
  190. ).toBe(true);
  191. expect(
  192. isWindowHostControllerFrameGap(cursor, {
  193. subscriptionId: "subscription-1",
  194. logEpoch: "epoch-2",
  195. fromSeq: 7,
  196. }),
  197. ).toBe(true);
  198. expect(
  199. isWindowHostControllerFrameGap(cursor, {
  200. subscriptionId: "subscription-2",
  201. logEpoch: "epoch-1",
  202. fromSeq: 7,
  203. }),
  204. ).toBe(true);
  205. });
  206. });