v4-gateway-workspace-config.test.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. // ConversationV4Gateway 的 workspace-config 集成测试(M5 删波次 2):
  2. // 1. subscribeWorkspaceConfig:宿主钩子种子 → snapshot 帧(配置目录 + slash 目录)
  3. // 2. publishWorkspaceConfig → 订阅者收到 config.updated 增量帧;同值发布 conflated 不产帧
  4. // 3. 订阅先于任何发布且宿主无钩子 → 空目录 snapshot(不炸)
  5. // 4. unsubscribeWorkspaceConfig 后不再收帧
  6. // 5. base 水位续传:同代际 seq 对齐 → resume 且无初始帧
  7. import { describe, expect, it } from "vitest";
  8. import type {
  9. WorkspaceConfigState,
  10. WorkspaceConfigTopicFrame,
  11. } from "@zcode/shared/zcode-protocol-v4";
  12. import { workspaceConfigTopic } from "@zcode/shared/zcode-protocol-v4";
  13. import { ConversationV4Gateway } from "../src/zcode-protocol-v4/index.js";
  14. const configA: WorkspaceConfigState = {
  15. configOptions: [
  16. {
  17. id: "model",
  18. name: "Model",
  19. category: "model",
  20. type: "select",
  21. currentValue: "anthropic/haiku-4.5",
  22. options: [
  23. {
  24. value: "anthropic/haiku-4.5",
  25. name: "Haiku 4.5",
  26. modelProviderId: "anthropic",
  27. modelProviderName: "Anthropic",
  28. },
  29. ],
  30. },
  31. ],
  32. slashCommands: [{ name: "compact", description: "Compact", source: "builtin" }],
  33. };
  34. const configB: WorkspaceConfigState = {
  35. ...configA,
  36. configOptions: [
  37. {
  38. ...configA.configOptions[0]!,
  39. currentValue: "anthropic/opus-4.6",
  40. },
  41. ],
  42. };
  43. interface Stub {
  44. frames: WorkspaceConfigTopicFrame[];
  45. hostConfig: WorkspaceConfigState | null;
  46. }
  47. function makeGateway(stub: Stub, opts: { withConfigHook: boolean } = { withConfigHook: true }) {
  48. return new ConversationV4Gateway(
  49. {
  50. sessionExists: () => false,
  51. emitWireFrame: (wire) => {
  52. if (wire.kind === "complete" && wire.topic.startsWith("workspace-config/")) {
  53. stub.frames.push(wire.frame as WorkspaceConfigTopicFrame);
  54. }
  55. },
  56. executeCommand: async () => undefined,
  57. ...(opts.withConfigHook ? { getWorkspaceConfig: async () => stub.hostConfig } : {}),
  58. },
  59. { now: () => 1_700_000_999_000, createLogEpoch: () => "epoch-wc" },
  60. );
  61. }
  62. async function subscribe(gateway: ConversationV4Gateway, base?: { logEpoch: string; seq: number }) {
  63. return gateway.subscribeWorkspaceConfig({
  64. topic: workspaceConfigTopic("ws-1"),
  65. connectionId: "conn-1",
  66. clientMode: "desktop-continuous",
  67. ...(base ? { base } : {}),
  68. });
  69. }
  70. describe("v4 gateway workspace-config topic", () => {
  71. it("saturated mobile config subscriber does not block desktop and drains only itself", async () => {
  72. const stub: Stub = { frames: [], hostConfig: configA };
  73. const gateway = makeGateway(stub);
  74. const desktop = await gateway.subscribeWorkspaceConfig({
  75. topic: workspaceConfigTopic("ws-1"),
  76. connectionId: "desktop-config",
  77. clientMode: "desktop-continuous",
  78. });
  79. const mobile = await gateway.subscribeWorkspaceConfig({
  80. topic: workspaceConfigTopic("ws-1"),
  81. connectionId: "mobile-config",
  82. clientMode: "web-remote-replayable",
  83. });
  84. gateway.setConnectionFlowState({ connectionId: "mobile-config", state: "saturated" });
  85. gateway.publishWorkspaceConfig("ws-1", configB);
  86. expect(stub.frames.map((frame) => frame.subscriptionId)).toEqual([
  87. desktop.ack.subscriptionId,
  88. ]);
  89. gateway.setConnectionFlowState({ connectionId: "mobile-config", state: "drained" });
  90. expect(stub.frames.map((frame) => frame.subscriptionId)).toEqual([
  91. desktop.ack.subscriptionId,
  92. mobile.ack.subscriptionId,
  93. ]);
  94. });
  95. it("workspace-config recovery commit 后无新 publish 也会 flush 在途期间最新态", async () => {
  96. const stub: Stub = { frames: [], hostConfig: configA };
  97. const gateway = makeGateway(stub);
  98. const subscribed = await subscribe(gateway);
  99. const recovery = gateway.resyncReserved({
  100. topic: workspaceConfigTopic("ws-1"),
  101. connectionId: "conn-1",
  102. subscriptionId: subscribed.ack.subscriptionId,
  103. base: {
  104. logEpoch: subscribed.ack.logEpoch,
  105. seq: subscribed.initialFrame?.toSeq ?? 0,
  106. },
  107. });
  108. gateway.publishWorkspaceConfig("ws-1", configB);
  109. expect(stub.frames).toEqual([]);
  110. expect(recovery.commit()).toBe(true);
  111. expect(stub.frames).toHaveLength(1);
  112. expect(stub.frames[0]).toMatchObject({
  113. payload: { kind: "deltas", deltas: [{ op: "config.updated", config: configB }] },
  114. });
  115. });
  116. it("initial physical encode 失败会回滚 workspace-config subscription", async () => {
  117. const stub: Stub = {
  118. frames: [],
  119. hostConfig: {
  120. configOptions: [],
  121. slashCommands: [
  122. { name: "oversized", description: "x".repeat(17 * 1024 * 1024), source: "builtin" },
  123. ],
  124. },
  125. };
  126. const gateway = makeGateway(stub);
  127. await expect(subscribe(gateway)).rejects.toThrow("proto.frameAssemblyTooLarge");
  128. const publishers = (
  129. gateway as unknown as {
  130. configPublishers: Map<string, { hasSubscribers(): boolean }>;
  131. }
  132. ).configPublishers;
  133. expect(publishers.get("ws-1")?.hasSubscribers()).toBe(false);
  134. });
  135. it("订阅时经宿主钩子拉种子并返回待入 outbox 的 snapshot 帧", async () => {
  136. const stub: Stub = { frames: [], hostConfig: configA };
  137. const gateway = makeGateway(stub);
  138. const result = await subscribe(gateway);
  139. expect(result.ack.mode).toBe("snapshot");
  140. expect(result.ack.logEpoch).toBe("epoch-wc");
  141. expect(result.initialFrame?.payload.kind).toBe("snapshot");
  142. if (result.initialFrame?.payload.kind === "snapshot") {
  143. expect(result.initialFrame.payload.snapshot.config).toEqual(configA);
  144. expect(result.initialFrame.payload.snapshot.workspaceId).toBe("ws-1");
  145. }
  146. });
  147. it("publishWorkspaceConfig 推 config.updated 增量帧;同值发布 conflated 不产帧", async () => {
  148. const stub: Stub = { frames: [], hostConfig: configA };
  149. const gateway = makeGateway(stub);
  150. await subscribe(gateway);
  151. gateway.publishWorkspaceConfig("ws-1", configB);
  152. expect(stub.frames).toHaveLength(1);
  153. const frame = stub.frames[0]!;
  154. expect(frame.payload.kind).toBe("deltas");
  155. if (frame.payload.kind === "deltas") {
  156. expect(frame.payload.deltas).toEqual([{ op: "config.updated", config: configB }]);
  157. }
  158. // 同值再发布:conflated 去抖,不产新帧。
  159. gateway.publishWorkspaceConfig("ws-1", configB);
  160. expect(stub.frames).toHaveLength(1);
  161. });
  162. it("宿主无钩子时订阅得到空目录 snapshot(旧宿主兼容)", async () => {
  163. const stub: Stub = { frames: [], hostConfig: null };
  164. const gateway = makeGateway(stub, { withConfigHook: false });
  165. const result = await subscribe(gateway);
  166. expect(result.initialFrame?.payload.kind).toBe("snapshot");
  167. if (result.initialFrame?.payload.kind === "snapshot") {
  168. expect(result.initialFrame.payload.snapshot.config).toEqual({
  169. configOptions: [],
  170. slashCommands: [],
  171. });
  172. }
  173. });
  174. it("unsubscribeWorkspaceConfig 后不再收帧", async () => {
  175. const stub: Stub = { frames: [], hostConfig: configA };
  176. const gateway = makeGateway(stub);
  177. const result = await subscribe(gateway);
  178. gateway.unsubscribe({
  179. topic: workspaceConfigTopic("ws-1"),
  180. subscriptionId: result.ack.subscriptionId,
  181. connectionId: "conn-1",
  182. });
  183. gateway.publishWorkspaceConfig("ws-1", configB);
  184. expect(stub.frames).toHaveLength(0);
  185. });
  186. it("base 水位对齐时 resume 且无初始帧", async () => {
  187. const stub: Stub = { frames: [], hostConfig: configA };
  188. const gateway = makeGateway(stub);
  189. const first = await subscribe(gateway);
  190. const alignedSeq = first.initialFrame?.toSeq ?? 0;
  191. const resumed = await subscribe(gateway, {
  192. logEpoch: first.ack.logEpoch,
  193. seq: alignedSeq,
  194. });
  195. expect(resumed.ack.mode).toBe("resume");
  196. expect(resumed.initialFrame).toBeNull();
  197. });
  198. });