zcodeAgentService.offPeakToolPolicy.test.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import { describe, expect, it, vi } from "vitest";
  2. import {
  3. zcodeProtocolMethods,
  4. OFF_PEAK_PROVIDER_IDS,
  5. buildRemoteWorkspaceIdentity,
  6. } from "@zcode/shared";
  7. import type { OffPeakClientConfig } from "../src/coding-plan-subscription/codingPlanSubscription.js";
  8. import { offPeakSelectionView } from "./fixtures/offPeakSelection.js";
  9. // D49:Off-Peak 本地支持能力作为 workspace 策略在客户端就绪时同步给 CLI。
  10. // 不支持的 workspace 不发策略请求;支持时发送 enabled:true,绝不读取远端灰度。
  11. // 旧 CLI method-not-found 降级忽略,不阻塞客户端就绪。
  12. class MockProcessManagerBase {
  13. onRuntimeLifecycle() {
  14. return { dispose() {} };
  15. }
  16. onRuntimeRestarted() {
  17. return { dispose() {} };
  18. }
  19. markReady() {}
  20. }
  21. async function setup(params: {
  22. offPeakEnabled: boolean | undefined;
  23. configResolver?: () => Promise<OffPeakClientConfig>;
  24. policyResponse?: (request: unknown) => unknown;
  25. }) {
  26. const resolveConfig = vi.fn(
  27. params.configResolver ??
  28. (async () => ({
  29. enabled: params.offPeakEnabled!,
  30. modelSelectionView: offPeakSelectionView(),
  31. })),
  32. );
  33. const request = vi.fn(async (method: string, requestParams?: unknown) => {
  34. if (method === zcodeProtocolMethods.workspaceUpdateOffPeakToolPolicy) {
  35. if (params.policyResponse) return params.policyResponse(requestParams);
  36. return requestParams;
  37. }
  38. throw new Error(`Unexpected method ${method}`);
  39. });
  40. const getClient = vi.fn(async () => ({
  41. request,
  42. transportKind: "stdio" as const,
  43. onNotification: () => ({ dispose() {} }),
  44. onRequest: () => ({ dispose() {} }),
  45. onClose: () => ({ dispose() {} }),
  46. }));
  47. vi.resetModules();
  48. vi.doMock("../src/zcode-agent/zcodeAgentProcessManager.js", () => ({
  49. ZCodeAgentProcessManager: class extends MockProcessManagerBase {
  50. getClient = getClient;
  51. disposeAll() {}
  52. },
  53. }));
  54. const { createZCodeAgentService } = await import("../src/zcode-agent/zcodeAgentService.js");
  55. const service = createZCodeAgentService({
  56. modelSelectionReadinessSource: { getView: async () => offPeakSelectionView() },
  57. ...(params.offPeakEnabled === undefined
  58. ? {}
  59. : {
  60. resolveOffPeakClientConfig: resolveConfig,
  61. resolveOffPeakTaskService: () => ({
  62. getCodingPlanSupport: async () => ({
  63. supported: true as const,
  64. kind: "zai-personal" as const,
  65. providerFamily: "zai" as const,
  66. providerId: OFF_PEAK_PROVIDER_IDS.zai,
  67. }),
  68. createTask: vi.fn(),
  69. list: vi.fn(),
  70. }),
  71. }),
  72. });
  73. return { service, request, getClient, resolveConfig };
  74. }
  75. function policyCalls(request: ReturnType<typeof vi.fn>) {
  76. return request.mock.calls.filter(
  77. ([method]) => method === zcodeProtocolMethods.workspaceUpdateOffPeakToolPolicy,
  78. );
  79. }
  80. describe("workspace/updateOffPeakToolPolicy 同步(D49-8)", () => {
  81. it("未装配 Off-Peak 依赖时不发策略请求", async () => {
  82. const { service, request, getClient } = await setup({ offPeakEnabled: undefined });
  83. await expect(service.initialize({ workspacePath: "/workspace/app" })).resolves.toEqual(
  84. expect.objectContaining({ available: true }),
  85. );
  86. expect(getClient).toHaveBeenCalled();
  87. expect(policyCalls(request)).toHaveLength(0);
  88. service.disposeAll();
  89. });
  90. it("灰度开启时在客户端就绪阶段下发 enabled:true", async () => {
  91. const { service, request } = await setup({ offPeakEnabled: true });
  92. await service.initialize({ workspacePath: "/workspace/app" });
  93. const calls = policyCalls(request);
  94. expect(calls).toHaveLength(1);
  95. expect(calls[0]?.[1]).toEqual({
  96. workspace: expect.objectContaining({ workspacePath: "/workspace/app" }),
  97. enabled: true,
  98. });
  99. service.disposeAll();
  100. });
  101. it("远端配置保持 pending 时本地 initialize 仍完成", async () => {
  102. const { service, resolveConfig } = await setup({
  103. offPeakEnabled: true,
  104. configResolver: () => new Promise<OffPeakClientConfig>(() => {}),
  105. });
  106. try {
  107. let ready = false;
  108. const operation = service.initialize({ workspacePath: "/workspace/app" }).then(() => {
  109. ready = true;
  110. });
  111. await vi.waitFor(() => expect(ready).toBe(true), { timeout: 500 });
  112. await operation;
  113. expect(resolveConfig).not.toHaveBeenCalled();
  114. } finally {
  115. service.disposeAll();
  116. }
  117. });
  118. it.each([
  119. { remoteSessionId: "remote-session" },
  120. {
  121. workspaceIdentity: buildRemoteWorkspaceIdentity("/workspace/app", {
  122. kind: "ssh",
  123. host: "test",
  124. username: "dev",
  125. }),
  126. },
  127. ])("远程工作区不注册也不读取灰度:%j", async (target) => {
  128. const { service, request, resolveConfig } = await setup({ offPeakEnabled: true });
  129. try {
  130. await service.initialize({ workspacePath: "/workspace/app", ...target });
  131. expect(policyCalls(request)).toHaveLength(0);
  132. expect(resolveConfig).not.toHaveBeenCalled();
  133. } finally {
  134. service.disposeAll();
  135. }
  136. });
  137. it("旧 CLI method-not-found(-32601)降级忽略,不阻塞客户端就绪", async () => {
  138. const { service, request } = await setup({
  139. offPeakEnabled: true,
  140. policyResponse: () => {
  141. throw Object.assign(new Error("Method not found"), { code: -32601 });
  142. },
  143. });
  144. await expect(service.initialize({ workspacePath: "/workspace/app" })).resolves.toEqual(
  145. expect.objectContaining({ available: true }),
  146. );
  147. expect(policyCalls(request)).toHaveLength(1);
  148. service.disposeAll();
  149. });
  150. it("策略同步超时等非 -32601 错误只记 warn,不阻断客户端就绪(机审 SG-02)", async () => {
  151. const { service, request } = await setup({
  152. offPeakEnabled: true,
  153. policyResponse: () => {
  154. throw new Error("request timed out");
  155. },
  156. });
  157. await expect(service.initialize({ workspacePath: "/workspace/app" })).resolves.toEqual(
  158. expect.objectContaining({ available: true }),
  159. );
  160. expect(policyCalls(request)).toHaveLength(1);
  161. service.disposeAll();
  162. });
  163. });