bundledZCodeBuiltinProviderConfig.test.ts 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
  2. import { tmpdir } from "node:os";
  3. import { join } from "node:path";
  4. import { afterEach, describe, expect, it } from "vitest";
  5. import { materializeBundledZCodeBuiltinProviderConfig } from "../src/bundledZCodeBuiltinProviderConfig.js";
  6. describe("materializeBundledZCodeBuiltinProviderConfig", () => {
  7. const tempDirs: string[] = [];
  8. afterEach(async () => {
  9. await Promise.all(tempDirs.splice(0).map((dir) => rm(dir, { recursive: true, force: true })));
  10. });
  11. it("在环境固定路径物化 ZCode Built-in Provider Config,并复用相同内容", async () => {
  12. const runtimeRoot = await mkdtemp(join(tmpdir(), "zcode-remote-provider-config-"));
  13. tempDirs.push(runtimeRoot);
  14. const content = emptyRelease(1);
  15. const firstPath = await materializeBundledZCodeBuiltinProviderConfig({
  16. environmentConfigRoot: runtimeRoot,
  17. content,
  18. });
  19. const secondPath = await materializeBundledZCodeBuiltinProviderConfig({
  20. environmentConfigRoot: runtimeRoot,
  21. content,
  22. });
  23. expect(secondPath).toBe(firstPath);
  24. expect(firstPath).toBe(
  25. join(runtimeRoot, "runtime", "provider", "bundled", "zcode-builtin.json"),
  26. );
  27. await expect(readFile(firstPath, "utf8")).resolves.toBe(`${content}\n`);
  28. });
  29. it("内容变化时在固定路径替换资源副本", async () => {
  30. const runtimeRoot = await mkdtemp(join(tmpdir(), "zcode-remote-provider-config-"));
  31. tempDirs.push(runtimeRoot);
  32. const firstContent = emptyRelease(1);
  33. const secondContent = emptyRelease(2);
  34. const firstPath = await materializeBundledZCodeBuiltinProviderConfig({
  35. environmentConfigRoot: runtimeRoot,
  36. content: firstContent,
  37. });
  38. const secondPath = await materializeBundledZCodeBuiltinProviderConfig({
  39. environmentConfigRoot: runtimeRoot,
  40. content: secondContent,
  41. });
  42. expect(secondPath).toBe(firstPath);
  43. await expect(readFile(secondPath, "utf8")).resolves.toBe(`${secondContent}\n`);
  44. });
  45. it("发现损坏资源副本时从有效随包资源重建", async () => {
  46. const runtimeRoot = await mkdtemp(join(tmpdir(), "zcode-remote-provider-config-"));
  47. tempDirs.push(runtimeRoot);
  48. const content = emptyRelease(1);
  49. const filePath = await materializeBundledZCodeBuiltinProviderConfig({
  50. environmentConfigRoot: runtimeRoot,
  51. content,
  52. });
  53. await writeFile(filePath, "corrupted", "utf8");
  54. await expect(
  55. materializeBundledZCodeBuiltinProviderConfig({ environmentConfigRoot: runtimeRoot, content }),
  56. ).resolves.toBe(filePath);
  57. await expect(readFile(filePath, "utf8")).resolves.toBe(`${content}\n`);
  58. });
  59. });
  60. function emptyRelease(revision: number): string {
  61. return JSON.stringify({
  62. schemaVersion: 1,
  63. revision,
  64. config: {
  65. providerConfigRules: { templateRules: [], providerRules: [] },
  66. modelConfigRules: {
  67. modelRules: [],
  68. modelApiRules: [],
  69. providerSiteRules: [],
  70. templateModelRules: [],
  71. builtinProviderModelRules: [],
  72. },
  73. },
  74. });
  75. }