zcode-builtin-process.test.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import { fork, type ChildProcess } from "node:child_process";
  2. import { once } from "node:events";
  3. import { mkdtemp, writeFile, rm } from "node:fs/promises";
  4. import { join } from "node:path";
  5. import { tmpdir } from "node:os";
  6. import { fileURLToPath } from "node:url";
  7. import { expect, it, vi } from "vitest";
  8. import { NodeZCodeBuiltinProviderConfigSource, decodeZCodeBuiltinRelease } from "../src/index.js";
  9. it("两个真实消费进程观察同一 Active 原子替换,各自发布同版 Account / Registry", async () => {
  10. const root = await mkdtemp(join(tmpdir(), "builtin-process-"));
  11. const release = {
  12. schemaVersion: 1,
  13. revision: 1,
  14. config: {
  15. providerConfigRules: { templateRules: [], providerRules: [] },
  16. modelConfigRules: {
  17. modelRules: [],
  18. modelApiRules: [],
  19. providerSiteRules: [],
  20. templateModelRules: [],
  21. builtinProviderModelRules: [],
  22. },
  23. },
  24. };
  25. const bundled = join(root, "bundled.json");
  26. const active = join(root, "active.json");
  27. await writeFile(bundled, JSON.stringify(release));
  28. const source = new NodeZCodeBuiltinProviderConfigSource({
  29. bundledFilePath: bundled,
  30. activeFilePath: active,
  31. watch: false,
  32. });
  33. const children: ChildProcess[] = [];
  34. const messages: { pid: number; builtin: string; account: string }[] = [];
  35. try {
  36. await source.read();
  37. for (let index = 0; index < 2; index++) {
  38. const child = fork(
  39. fileURLToPath(new URL("./fixtures/builtin-registry-observer.ts", import.meta.url)),
  40. [active, join(root, `personal-${index}.json`)],
  41. {
  42. execArgv: ["--import", import.meta.resolve("tsx")],
  43. stdio: ["ignore", "ignore", "inherit", "ipc"],
  44. },
  45. );
  46. children.push(child);
  47. child.on("message", (message) => messages.push(message as (typeof messages)[number]));
  48. }
  49. await vi.waitFor(() => expect(new Set(messages.map((message) => message.pid)).size).toBe(2), {
  50. timeout: 10_000,
  51. });
  52. await source.applyRemoteRelease(decodeZCodeBuiltinRelease({ ...release, revision: 2 }));
  53. await vi.waitFor(
  54. () =>
  55. expect(
  56. new Set(
  57. messages
  58. .filter((message) => message.builtin.startsWith("zcode-builtin:2:"))
  59. .map((message) => message.pid),
  60. ).size,
  61. ).toBe(2),
  62. { timeout: 10_000 },
  63. );
  64. expect(messages.every((message) => message.builtin === message.account)).toBe(true);
  65. } finally {
  66. source.dispose();
  67. await Promise.all(
  68. children.map(async (child) => {
  69. if (child.exitCode !== null || child.signalCode !== null) return;
  70. const exit = once(child, "exit");
  71. child.kill();
  72. await exit;
  73. }),
  74. );
  75. await rm(root, { recursive: true, force: true });
  76. }
  77. }, 25_000);