zcodeAgentService.offPeakToolPolicy.test.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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("灰度关闭仍注册本地工具,不读取远端灰度", async () => {
  91. const { service, request, resolveConfig } = await setup({ offPeakEnabled: false });
  92. await service.initialize({ workspacePath: "/workspace/app" });
  93. expect(policyCalls(request)).toHaveLength(1);
  94. expect(resolveConfig).not.toHaveBeenCalled();
  95. service.disposeAll();
  96. });
  97. it("灰度开启时在客户端就绪阶段下发 enabled:true", async () => {
  98. const { service, request } = await setup({ offPeakEnabled: true });
  99. await service.initialize({ workspacePath: "/workspace/app" });
  100. const calls = policyCalls(request);
  101. expect(calls).toHaveLength(1);
  102. expect(calls[0]?.[1]).toEqual({
  103. workspace: expect.objectContaining({ workspacePath: "/workspace/app" }),
  104. enabled: true,
  105. });
  106. service.disposeAll();
  107. });
  108. it("远端配置保持 pending 时本地 initialize 仍完成", async () => {
  109. const { service, resolveConfig } = await setup({
  110. offPeakEnabled: true,
  111. configResolver: () => new Promise<OffPeakClientConfig>(() => {}),
  112. });
  113. try {
  114. let ready = false;
  115. const operation = service.initialize({ workspacePath: "/workspace/app" }).then(() => {
  116. ready = true;
  117. });
  118. await vi.waitFor(() => expect(ready).toBe(true), { timeout: 500 });
  119. await operation;
  120. expect(resolveConfig).not.toHaveBeenCalled();
  121. } finally {
  122. service.disposeAll();
  123. }
  124. });
  125. it.each([
  126. { remoteSessionId: "remote-session" },
  127. {
  128. workspaceIdentity: buildRemoteWorkspaceIdentity("/workspace/app", {
  129. kind: "ssh",
  130. host: "test",
  131. username: "dev",
  132. }),
  133. },
  134. ])("远程工作区不注册也不读取灰度:%j", async (target) => {
  135. const { service, request, resolveConfig } = await setup({ offPeakEnabled: true });
  136. try {
  137. await service.initialize({ workspacePath: "/workspace/app", ...target });
  138. expect(policyCalls(request)).toHaveLength(0);
  139. expect(resolveConfig).not.toHaveBeenCalled();
  140. } finally {
  141. service.disposeAll();
  142. }
  143. });
  144. it("旧 CLI method-not-found(-32601)降级忽略,不阻塞客户端就绪", async () => {
  145. const { service, request } = await setup({
  146. offPeakEnabled: true,
  147. policyResponse: () => {
  148. throw Object.assign(new Error("Method not found"), { code: -32601 });
  149. },
  150. });
  151. await expect(service.initialize({ workspacePath: "/workspace/app" })).resolves.toEqual(
  152. expect.objectContaining({ available: true }),
  153. );
  154. expect(policyCalls(request)).toHaveLength(1);
  155. service.disposeAll();
  156. });
  157. it("策略同步超时等非 -32601 错误只记 warn,不阻断客户端就绪(机审 SG-02)", async () => {
  158. const { service, request } = await setup({
  159. offPeakEnabled: true,
  160. policyResponse: () => {
  161. throw new Error("request timed out");
  162. },
  163. });
  164. await expect(service.initialize({ workspacePath: "/workspace/app" })).resolves.toEqual(
  165. expect.objectContaining({ available: true }),
  166. );
  167. expect(policyCalls(request)).toHaveLength(1);
  168. service.disposeAll();
  169. });
  170. });