providerApiKeyPersistence.test.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import { describe, expect, it, vi } from "vitest";
  2. import type { ProviderSettingsView } from "@zcode/services";
  3. import {
  4. loadProviderApiKey,
  5. persistProviderApiKey,
  6. resolveSettingsProviderApiKey,
  7. resolveSettingsProviderApiKeyManagementUrl,
  8. } from "@/lib/providerApiKeyPersistence.js";
  9. function createSettingsView(): ProviderSettingsView {
  10. return {
  11. revision: 1,
  12. addableProviders: [],
  13. providerOrder: [],
  14. providers: [
  15. {
  16. providerId: "zai-api",
  17. enabled: true,
  18. executable: true,
  19. accessAllowed: true,
  20. personalConfig: { label: "My Z.ai" },
  21. effectiveConfig: {
  22. label: "My Z.ai",
  23. access: {
  24. type: "api-key",
  25. apiKey: "old-key",
  26. apiKeyManagementUrl: "https://api.z.ai/keys",
  27. },
  28. api: { baseUrl: "https://api.z.ai" },
  29. builtinModelIds: ["glm-5"],
  30. },
  31. issues: [],
  32. models: [],
  33. },
  34. ],
  35. };
  36. }
  37. describe("Provider API Key Personal persistence", () => {
  38. it("恢复手动套餐 Key 保留类型,不要求账号连接", async () => {
  39. const view = createSettingsView();
  40. view.providers[0]!.effectiveConfig.access = {
  41. type: "zhipu-coding-plan-api-key",
  42. apiKey: "old",
  43. };
  44. const save = vi.fn();
  45. expect(resolveSettingsProviderApiKey(view, "zai-api")).toBe("old");
  46. await persistProviderApiKey({
  47. providerId: "zai-api",
  48. apiKey: "new",
  49. providerSettingsService: { getView: async () => view, savePersonalProviderOverlay: save },
  50. });
  51. expect(save).toHaveBeenCalledWith("zai-api", {
  52. label: "My Z.ai",
  53. access: { type: "zhipu-coding-plan-api-key", apiKey: "new" },
  54. });
  55. });
  56. it("只覆盖 Personal API Key,不复制 Official Effective 字段", async () => {
  57. const calls: string[] = [];
  58. const savePersonalProviderOverlay = vi.fn(async () => {
  59. calls.push("personal");
  60. return createSettingsView();
  61. });
  62. await persistProviderApiKey({
  63. providerId: "zai-api",
  64. apiKey: "new-key",
  65. providerSettingsService: {
  66. getView: async () => createSettingsView(),
  67. savePersonalProviderOverlay,
  68. },
  69. });
  70. expect(calls).toEqual(["personal"]);
  71. expect(savePersonalProviderOverlay).toHaveBeenCalledWith("zai-api", {
  72. label: "My Z.ai",
  73. access: { type: "api-key", apiKey: "new-key" },
  74. });
  75. });
  76. it("新 Settings View 不含目标 Provider 时拒绝回写旧事实源", async () => {
  77. await expect(
  78. persistProviderApiKey({
  79. providerId: "zai-api",
  80. apiKey: "new-key",
  81. providerSettingsService: {
  82. getView: async () => ({
  83. revision: 1,
  84. addableProviders: [],
  85. providerOrder: [],
  86. providers: [],
  87. }),
  88. savePersonalProviderOverlay: vi.fn(),
  89. },
  90. }),
  91. ).rejects.toThrow("Provider Settings View 中不存在 API Provider zai-api");
  92. });
  93. it("从 Settings Effective Config 读取当前 API Key", () => {
  94. expect(resolveSettingsProviderApiKey(createSettingsView(), "zai-api")).toBe("old-key");
  95. expect(resolveSettingsProviderApiKey(createSettingsView(), "missing")).toBeNull();
  96. });
  97. it("从 Settings Effective Config 读取 API Key 管理链接", () => {
  98. expect(resolveSettingsProviderApiKeyManagementUrl(createSettingsView(), "zai-api")).toBe(
  99. "https://api.z.ai/keys",
  100. );
  101. expect(
  102. resolveSettingsProviderApiKeyManagementUrl(createSettingsView(), "missing"),
  103. ).toBeUndefined();
  104. });
  105. it("读取时优先使用 Settings View,不触发旧 Provider 列表", async () => {
  106. await expect(
  107. loadProviderApiKey({
  108. providerId: "zai-api",
  109. providerSettingsService: { getView: async () => createSettingsView() },
  110. }),
  111. ).resolves.toBe("old-key");
  112. });
  113. it("Settings Service 已装配但读取失败时不回退旧 Provider 列表", async () => {
  114. await expect(
  115. loadProviderApiKey({
  116. providerId: "zai-api",
  117. providerSettingsService: {
  118. getView: async () => {
  119. throw new Error("settings unavailable");
  120. },
  121. },
  122. }),
  123. ).rejects.toThrow("settings unavailable");
  124. });
  125. });