zcodeProtocolV4WorkspaceConfig.test.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // workspace-config topic 黄金测试(M5 删波次 2 additive 演进纪律:新增 topic 必须带黄金测试)。
  2. // 覆盖:帧信封 round-trip(snapshot / deltas)、topic key 对偶、载荷与 host 侧
  3. // ZCodeConfigOption / ZCodeSlashCommand 的结构对齐(syncer 零映射直通的前提)。
  4. import { describe, expect, it } from "vitest";
  5. import { zcodeSessionSettingsToZCodeConfigOptions } from "../src/zcode-agent-model-state.js";
  6. import type { ZCodeSessionSettingsState } from "../src/zcode-protocol/index.js";
  7. import type { ZCodeConfigOption, ZCodeSlashCommand } from "../src/zcode-task-types-core.js";
  8. import {
  9. parseWorkspaceConfigTopic,
  10. workspaceConfigSnapshotSchema,
  11. workspaceConfigStateSchema,
  12. workspaceConfigTopic,
  13. workspaceConfigTopicFrameSchema,
  14. type WorkspaceConfigState,
  15. } from "../src/zcode-protocol-v4/index.js";
  16. const goldenState = {
  17. configOptions: [
  18. {
  19. id: "model",
  20. name: "Model",
  21. category: "model",
  22. type: "select" as const,
  23. currentValue: "anthropic/haiku-4.5",
  24. options: [
  25. {
  26. value: "anthropic/haiku-4.5",
  27. name: "Haiku 4.5",
  28. description: "fast",
  29. modelProviderId: "anthropic",
  30. modelProviderName: "Anthropic",
  31. modelThoughtLevels: ["low", "medium", "high", "xhigh"],
  32. modelDefaultThoughtLevel: "medium",
  33. },
  34. ],
  35. },
  36. {
  37. id: "mode",
  38. name: "Mode",
  39. category: "mode",
  40. type: "select" as const,
  41. currentValue: "build",
  42. options: [{ value: "build", name: "Ask before changes" }],
  43. },
  44. ],
  45. slashCommands: [
  46. { name: "compact", description: "Compact the session", source: "builtin" as const },
  47. { name: "goal", description: "Run toward a goal", inputHint: "<objective>" },
  48. ],
  49. };
  50. describe("zcode-protocol-v4 workspace-config topic", () => {
  51. it("topic key 构造与解析对偶", () => {
  52. expect(workspaceConfigTopic("/repo")).toBe("workspace-config//repo");
  53. expect(parseWorkspaceConfigTopic(workspaceConfigTopic("/repo"))).toBe("/repo");
  54. expect(parseWorkspaceConfigTopic("workspace-config/")).toBeNull();
  55. expect(parseWorkspaceConfigTopic("sessions-index//repo")).toBeNull();
  56. });
  57. it("snapshot 帧 round-trip(fromSeq 固定 0)", () => {
  58. const frame = {
  59. topic: workspaceConfigTopic("/repo"),
  60. subscriptionId: "wcs-epoch-1",
  61. fromSeq: 0,
  62. toSeq: 3,
  63. sentAt: 1000,
  64. payload: {
  65. kind: "snapshot" as const,
  66. snapshot: {
  67. protocolVersion: 1 as const,
  68. workspaceId: "/repo",
  69. logEpoch: "epoch-a",
  70. config: goldenState,
  71. },
  72. },
  73. };
  74. const parsed = workspaceConfigTopicFrameSchema.parse(frame);
  75. expect(parsed).toEqual(frame);
  76. });
  77. it("deltas 帧 round-trip(config.updated 整体替换)", () => {
  78. const frame = {
  79. topic: workspaceConfigTopic("/repo"),
  80. subscriptionId: "wcs-epoch-2",
  81. fromSeq: 3,
  82. toSeq: 4,
  83. sentAt: 2000,
  84. payload: {
  85. kind: "deltas" as const,
  86. deltas: [{ op: "config.updated" as const, config: goldenState }],
  87. },
  88. };
  89. const parsed = workspaceConfigTopicFrameSchema.parse(frame);
  90. expect(parsed).toEqual(frame);
  91. });
  92. it("拒绝未知 delta op / 非法 snapshot", () => {
  93. expect(
  94. workspaceConfigTopicFrameSchema.safeParse({
  95. topic: "workspace-config//repo",
  96. subscriptionId: "s",
  97. fromSeq: 0,
  98. toSeq: 1,
  99. sentAt: 1,
  100. payload: { kind: "deltas", deltas: [{ op: "config.patched", config: goldenState }] },
  101. }).success,
  102. ).toBe(false);
  103. expect(
  104. workspaceConfigSnapshotSchema.safeParse({
  105. protocolVersion: 3,
  106. workspaceId: "/repo",
  107. logEpoch: "e",
  108. config: goldenState,
  109. }).success,
  110. ).toBe(false);
  111. });
  112. it("载荷与 host 侧 ZCodeConfigOption / ZCodeSlashCommand 结构对齐(零映射直通)", () => {
  113. const state: WorkspaceConfigState = workspaceConfigStateSchema.parse(goldenState);
  114. // 类型层面的对齐验证:v4 载荷可以直接赋给 host 侧下游消费类型。
  115. const configOptions: ZCodeConfigOption[] = state.configOptions;
  116. const slashCommands: ZCodeSlashCommand[] = state.slashCommands;
  117. expect(configOptions[0]?.id).toBe("model");
  118. expect(slashCommands[0]?.name).toBe("compact");
  119. });
  120. it("把每个模型的 reasoning 目录投影到 model select value", () => {
  121. const settings: ZCodeSessionSettingsState = {
  122. model: {
  123. current: { providerId: "anthropic", modelId: "claude-opus-4-8" },
  124. available: [
  125. {
  126. ref: { providerId: "anthropic", modelId: "claude-opus-4-8" },
  127. label: "Claude Opus 4.8",
  128. reasoning: {
  129. defaultLevel: "medium",
  130. levels: [
  131. { value: "low", label: "low" },
  132. { value: "medium", label: "medium" },
  133. { value: "high", label: "high" },
  134. { value: "xhigh", label: "xhigh" },
  135. ],
  136. },
  137. },
  138. {
  139. ref: { providerId: "anthropic", modelId: "plain-chat" },
  140. label: "Plain Chat",
  141. reasoning: { levels: [] },
  142. },
  143. {
  144. ref: { providerId: "anthropic", modelId: "legacy-model" },
  145. label: "Legacy Model",
  146. },
  147. ],
  148. },
  149. thoughtLevel: { enabled: true, available: [], current: undefined },
  150. mode: { current: "build" },
  151. };
  152. const modelOption = zcodeSessionSettingsToZCodeConfigOptions(settings).find(
  153. (option) => option.id === "model",
  154. );
  155. expect(modelOption?.options).toEqual([
  156. expect.objectContaining({
  157. value: "anthropic/claude-opus-4-8",
  158. modelThoughtLevels: ["low", "medium", "high", "xhigh"],
  159. modelDefaultThoughtLevel: "medium",
  160. }),
  161. expect.objectContaining({
  162. value: "anthropic/plain-chat",
  163. modelThoughtLevels: [],
  164. }),
  165. expect.not.objectContaining({ modelThoughtLevels: expect.anything() }),
  166. ]);
  167. });
  168. });