account-config-delivery.test.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import { mkdtemp, rm } from "node:fs/promises";
  2. import { tmpdir } from "node:os";
  3. import { join } from "node:path";
  4. import { fileURLToPath } from "node:url";
  5. import { afterEach, describe, expect, it, vi } from "vitest";
  6. import { ProviderConfigMap, createAccountProviderConfigSnapshot } from "@zcode/provider";
  7. import {
  8. ZCODE_BUILTIN_PROVIDER_CONFIG_FILE_ENV,
  9. ZCODE_PERSONAL_PROVIDER_CONFIG_FILE_ENV,
  10. } from "@zcode/provider-node";
  11. import { zcodeProtocolMethods } from "@zcode/shared";
  12. import {
  13. startProcessProviderRegistryRuntime,
  14. parseProcessAccountProviderConfigSnapshot,
  15. } from "../src/app/process-provider-registry-runtime.js";
  16. import { ZCodeProtocolAgentServer } from "../src/zcode-protocol/server.js";
  17. const cleanups: (() => Promise<void>)[] = [];
  18. afterEach(async () => {
  19. vi.restoreAllMocks();
  20. for (const cleanup of cleanups.splice(0)) await cleanup();
  21. });
  22. async function startRuntime() {
  23. const directory = await mkdtemp(join(tmpdir(), "account-delivery-"));
  24. cleanups.push(() => rm(directory, { recursive: true, force: true }));
  25. const started = await startProcessProviderRegistryRuntime({
  26. [ZCODE_BUILTIN_PROVIDER_CONFIG_FILE_ENV]: fileURLToPath(
  27. new URL("../../../../../config/provider/zcode-builtin.json", import.meta.url),
  28. ),
  29. [ZCODE_PERSONAL_PROVIDER_CONFIG_FILE_ENV]: join(directory, "personal.json"),
  30. });
  31. cleanups.unshift(async () => started.dispose());
  32. return started;
  33. }
  34. describe("托管 Worker 账号交付", () => {
  35. it("Account B 先到时接收 B、仍使用 A;配套配置到后才整体应用 B", async () => {
  36. const started = await startRuntime();
  37. const registry = started.runtime.registryService;
  38. const previous = registry.getSnapshot();
  39. const next = createAccountProviderConfigSnapshot("builtin-B", ProviderConfigMap.empty(), {});
  40. const server = new ZCodeProtocolAgentServer({
  41. createZCodeApp() {
  42. throw new Error("账号交付不得创建会话");
  43. },
  44. syncAccountProviderConfig: started.syncAccountProviderConfig,
  45. });
  46. expect(
  47. await server.handleMessage({
  48. id: 1,
  49. method: zcodeProtocolMethods.providerUpdateAccountConfig,
  50. params: {
  51. ...next,
  52. providers: Object.fromEntries(
  53. [...next.providers.entries()].map(([id, config]) => [id, config.toJSON()]),
  54. ),
  55. },
  56. }),
  57. ).toMatchObject({
  58. result: { receivedRevision: next.revision, providerCount: 0, status: "received" },
  59. });
  60. expect((await started.accountSource.read()).revision).toBe(next.revision);
  61. expect(registry.getSnapshot()).toBe(previous);
  62. const config = await started.runtime.configService.read();
  63. vi.spyOn(started.runtime.configService, "read").mockResolvedValue({
  64. ...config,
  65. revision: "config-B",
  66. zcodeBuiltinRevision: "builtin-B",
  67. });
  68. await registry.refresh("配套配置到达");
  69. expect(registry.getSnapshot()?.account.revision).toBe(next.revision);
  70. expect(registry.getSnapshot()?.config.zcodeBuiltinRevision).toBe("builtin-B");
  71. });
  72. it("刷新失败后同一份快照重交仍刷新,不能因 Source 已收过而跳过", async () => {
  73. const started = await startRuntime();
  74. const next = createAccountProviderConfigSnapshot(
  75. started.snapshot.config.zcodeBuiltinRevision,
  76. ProviderConfigMap.empty(),
  77. {},
  78. );
  79. const refresh = vi
  80. .spyOn(started.runtime.registryService, "refresh")
  81. .mockRejectedValueOnce(new Error("refresh failed"));
  82. await expect(started.syncAccountProviderConfig(next)).rejects.toThrow("refresh failed");
  83. await expect(started.syncAccountProviderConfig(next)).resolves.toBe(false);
  84. expect(refresh).toHaveBeenCalledTimes(2);
  85. expect(started.runtime.registryService.getSnapshot()?.account.revision).toBe(next.revision);
  86. });
  87. it("普通账号有权益却缺 current 时拒绝交付,不能宽松放行", () => {
  88. expect(() =>
  89. parseProcessAccountProviderConfigSnapshot({
  90. revision: "incomplete",
  91. basedOnZCodeBuiltinRevision: "builtin-A",
  92. providers: {
  93. "account:bigmodel-individual-coding-plan": {
  94. access: { type: "zhipu-account", entitled: true },
  95. },
  96. },
  97. states: {},
  98. }),
  99. ).toThrow("current");
  100. });
  101. it.each([
  102. { providerId: "account:bigmodel-individual-coding-plan", entitled: false },
  103. { providerId: "account:bigmodel-offpeak-idle-plan", entitled: true },
  104. ])("初始 fail-closed 和闲时不被强加 current:$providerId", ({ providerId, entitled }) => {
  105. expect(() =>
  106. parseProcessAccountProviderConfigSnapshot({
  107. revision: "allowed",
  108. basedOnZCodeBuiltinRevision: "builtin-A",
  109. states: {},
  110. providers: { [providerId]: { access: { type: "zhipu-account", entitled } } },
  111. }),
  112. ).not.toThrow();
  113. });
  114. });