customOpenAIProviderStore.test.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import { mkdtemp, readFile, stat, writeFile } from "node:fs/promises";
  2. import { join } from "node:path";
  3. import { tmpdir } from "node:os";
  4. import { afterEach, describe, expect, it, vi } from "vitest";
  5. import {
  6. ModelConfigRules,
  7. ProviderConfigMap,
  8. ProviderConfigResolver,
  9. ProviderRegistry,
  10. } from "@zcode/provider";
  11. import { decodeProviderConfigFile } from "@zcode/provider-node";
  12. describe("custom OpenAI Provider E2E seed", () => {
  13. afterEach(() => {
  14. vi.unstubAllEnvs();
  15. vi.resetModules();
  16. });
  17. it("只写版本化 Personal Config,不再制造旧 Provider 事实来源", async () => {
  18. const homeDir = await mkdtemp(join(tmpdir(), "zcode-provider-seed-"));
  19. const cliConfigFile = join(homeDir, ".zcode", "cli", "config.json");
  20. await writeFileWithParents(
  21. cliConfigFile,
  22. JSON.stringify({ mcp: { example: { command: "example" } } }),
  23. );
  24. vi.stubEnv("ZCODE_E2E_HOME_DIR", homeDir);
  25. vi.stubEnv("E2E_PROVIDER_RUNTIME_BASE_URL", "http://127.0.0.1:43123");
  26. const { seedCustomOpenAIChatCompletionsProvider } =
  27. await import("./e2e/helpers/custom-openai-provider-store.js");
  28. await seedCustomOpenAIChatCompletionsProvider({
  29. providerId: "e2e-openai",
  30. providerName: "E2E OpenAI",
  31. modelId: "e2e-model",
  32. inputModalities: ["text", "image"],
  33. });
  34. const personalConfig = JSON.parse(
  35. await readFile(join(homeDir, ".zcode", "v2", "provider_config.json"), "utf-8"),
  36. ) as { config: { modelConfigRules: unknown } };
  37. const personal = decodeProviderConfigFile(personalConfig);
  38. const resolved = new ProviderConfigResolver().resolve({
  39. zcodeBuiltinProviders: new ProviderConfigMap(),
  40. accountProviders: new ProviderConfigMap(),
  41. zcodeBuiltinModelRules: new ModelConfigRules(),
  42. personalProviders: personal.providers,
  43. personalModels: personal.models,
  44. });
  45. expect(resolved.issues).toEqual([]);
  46. expect(
  47. new ProviderRegistry(resolved.registryProviders).validateSelection({
  48. providerId: "e2e-openai",
  49. modelId: "e2e-model",
  50. options: { reasoningLevel: "disabled" },
  51. }),
  52. ).toEqual({ ok: true });
  53. expect(personalConfig).toMatchObject({
  54. schemaVersion: 1,
  55. config: {
  56. providerConfigRules: {
  57. providerRules: [
  58. {
  59. providerId: "e2e-openai",
  60. providerName: "E2E OpenAI",
  61. config: {
  62. access: { type: "api-key", apiKey: "e2e-fixture-key" },
  63. api: {
  64. type: "openai-chat-completions",
  65. baseUrl: "http://127.0.0.1:43123",
  66. },
  67. personalModelIds: ["e2e-model"],
  68. },
  69. },
  70. ],
  71. },
  72. },
  73. });
  74. expect(personalConfig.config.modelConfigRules).toEqual(
  75. expect.objectContaining({
  76. providerModelRules: expect.arrayContaining([
  77. expect.objectContaining({
  78. providerId: "e2e-openai",
  79. modelId: "e2e-model",
  80. config: expect.objectContaining({
  81. properties: expect.objectContaining({
  82. inputFormat: expect.objectContaining({ supportsImage: true }),
  83. }),
  84. }),
  85. }),
  86. ]),
  87. }),
  88. );
  89. await expect(stat(join(homeDir, ".zcode", "v2", "model-providers.json"))).rejects.toMatchObject(
  90. { code: "ENOENT" },
  91. );
  92. expect(JSON.parse(await readFile(cliConfigFile, "utf-8"))).toEqual({
  93. mcp: { example: { command: "example" } },
  94. });
  95. const { readModelProviders } = await import("./e2e/helpers/desktop-app.js");
  96. expect(await readModelProviders()).toEqual([
  97. expect.objectContaining({
  98. id: "e2e-openai",
  99. name: "E2E OpenAI",
  100. apiKey: "e2e-fixture-key",
  101. endpoints: expect.objectContaining({ baseURL: "http://127.0.0.1:43123" }),
  102. models: [
  103. expect.objectContaining({
  104. id: "e2e-model",
  105. contextWindow: 200_000,
  106. maxOutputTokens: 32_000,
  107. modalities: expect.objectContaining({ input: ["text", "image"] }),
  108. }),
  109. ],
  110. }),
  111. ]);
  112. });
  113. it("E2E 观察保留 Coding Plan API Key,而不把已保存凭据投影为空", async () => {
  114. const homeDir = await mkdtemp(join(tmpdir(), "zcode-provider-key-read-"));
  115. vi.stubEnv("ZCODE_E2E_HOME_DIR", homeDir);
  116. await writeFileWithParents(
  117. join(homeDir, ".zcode", "v2", "provider_config.json"),
  118. JSON.stringify({
  119. schemaVersion: 1,
  120. config: {
  121. providerConfigRules: {
  122. providerRules: [
  123. {
  124. providerId: "example-plan",
  125. templateId: "example-template",
  126. providerName: "Example Plan",
  127. config: {
  128. group: "standard-personal",
  129. access: { type: "zhipu-coding-plan-api-key", apiKey: "e2e-plan-key" },
  130. personalModelIds: [],
  131. },
  132. },
  133. ],
  134. },
  135. modelConfigRules: { providerModelRules: [], manualProviderModelRules: [] },
  136. },
  137. }),
  138. );
  139. const { readModelProvider } = await import("./e2e/helpers/desktop-app.js");
  140. expect(await readModelProvider("example-plan")).toMatchObject({
  141. id: "example-plan",
  142. apiKey: "e2e-plan-key",
  143. });
  144. });
  145. it("E2E 诊断不再读取旧 Provider Store", async () => {
  146. const homeDir = await mkdtemp(join(tmpdir(), "zcode-provider-read-"));
  147. await writeFileWithParents(
  148. join(homeDir, ".zcode", "v2", "model-providers.json"),
  149. JSON.stringify([
  150. {
  151. id: "legacy-provider",
  152. name: "Legacy Provider",
  153. models: [],
  154. },
  155. ]),
  156. );
  157. vi.stubEnv("ZCODE_E2E_HOME_DIR", homeDir);
  158. const { readModelProviders } = await import("./e2e/helpers/desktop-app.js");
  159. expect(await readModelProviders()).toEqual([]);
  160. });
  161. });
  162. async function writeFileWithParents(file: string, content: string) {
  163. const { mkdir } = await import("node:fs/promises");
  164. const { dirname } = await import("node:path");
  165. await mkdir(dirname(file), { recursive: true });
  166. await writeFile(file, content, "utf-8");
  167. }