provider-data-schema.test.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. ApiKeyAccessConfig,
  4. ProviderApiConfig,
  5. ProviderConfig,
  6. ZhipuAccountAccessConfig,
  7. createRegistryProviderConfig,
  8. parseProviderConfig,
  9. serializeRegistryProviderConfig,
  10. type ProviderApiType,
  11. type ProviderGroup,
  12. } from "../src/index.js";
  13. import { completeProviderConfigDataSchema } from "../src/config/provider-data-schema.js";
  14. const api = new ProviderApiConfig({
  15. type: "anthropic-messages",
  16. baseUrl: "https://api.example/v1",
  17. });
  18. const access = new ApiKeyAccessConfig({ apiKey: "test-key" });
  19. const cases = [
  20. { name: "key", access: access.toJSON(), paths: [["access", "apiKey"]] },
  21. {
  22. name: "account",
  23. access: { type: "zhipu-account", accountType: "zai", mode: "off-peak", entitled: false },
  24. paths: [
  25. ["access", "accountType"],
  26. ["access", "mode"],
  27. ["access", "entitled"],
  28. ],
  29. },
  30. ];
  31. describe("Provider 数据校验与完整性使用同一合同", () => {
  32. it("Todo104:新成员/endpoint字段唯一,null清除保留,旧未发布拼写拒绝", () => {
  33. const config = parseProviderConfig({
  34. group: "standard-personal",
  35. access: access.toJSON(),
  36. api: {
  37. type: "anthropic-messages",
  38. baseUrl: "https://example.test/anthropic",
  39. headers: { "X-Client": "test" },
  40. },
  41. builtinModelIds: ["builtin"],
  42. personalModelIds: ["custom"],
  43. modelOrder: ["custom", "builtin"],
  44. });
  45. expect(createRegistryProviderConfig(config).ok).toBe(true);
  46. expect(config.toJSON()).toMatchObject({
  47. api: { baseUrl: "https://example.test/anthropic", headers: { "X-Client": "test" } },
  48. personalModelIds: ["custom"],
  49. });
  50. const cleared = config.overlay(
  51. parseProviderConfig({ api: { baseUrl: null }, personalModelIds: null }),
  52. );
  53. expect(cleared.toJSON()).toMatchObject({
  54. api: { baseUrl: null },
  55. personalModelIds: null,
  56. builtinModelIds: ["builtin"],
  57. });
  58. expect(createRegistryProviderConfig(cleared).ok).toBe(false);
  59. expect(parseProviderConfig(cleared.toJSON()).toJSON()).toEqual(cleared.toJSON());
  60. expect(() => parseProviderConfig({ api: { baseURL: "https://old.invalid" } })).toThrow();
  61. expect(() => parseProviderConfig({ modelIds: ["old"] })).toThrow();
  62. });
  63. for (const scenario of cases) {
  64. const complete = { group: "zai-family", access: scenario.access, api: api.toJSON() };
  65. it.each(
  66. [...scenario.paths, ["group"], ["access"], ["api"], ["api", "type"], ["api", "baseUrl"]].map(
  67. (path) => ({ path, label: path.join(".") }),
  68. ),
  69. )(`${scenario.name} 的 $label 缺省/null 可保存,但不完整`, ({ path }) => {
  70. for (const replacement of [undefined, null]) {
  71. const value: Record<string, unknown> = structuredClone(complete);
  72. let parent = value;
  73. for (const key of path.slice(0, -1)) parent = parent[key] as Record<string, unknown>;
  74. if (replacement === undefined) delete parent[path.at(-1)!];
  75. else parent[path.at(-1)!] = replacement;
  76. const result = createRegistryProviderConfig(parseProviderConfig(value));
  77. expect(result.ok).toBe(false);
  78. if (!result.ok)
  79. expect(result.issues).toContainEqual(
  80. expect.objectContaining({
  81. code: "required-field-missing",
  82. path: ["provider", ...path],
  83. }),
  84. );
  85. }
  86. });
  87. it(`${scenario.name} 的 Registry 序列化保留完整合同`, () => {
  88. const result = createRegistryProviderConfig(parseProviderConfig(complete));
  89. expect(result.ok).toBe(true);
  90. if (!result.ok) throw new Error("fixture 应为完整配置");
  91. const serialized = serializeRegistryProviderConfig(result.config);
  92. expect(completeProviderConfigDataSchema.parse(serialized)).toEqual(complete);
  93. expect(Object.isFrozen(result.config)).toBe(true);
  94. });
  95. }
  96. it("未知 API type 不能凭借非空绕过 Registry 准入", () => {
  97. // 模拟不可信 JS 调用;不能把 TS 静态类型当作运行时校验。
  98. const config = new ProviderConfig({
  99. group: "standard-personal",
  100. access,
  101. api: new ProviderApiConfig({ type: "unknown" as ProviderApiType, baseUrl: api.baseUrl }),
  102. });
  103. expect(() => parseProviderConfig(config.toJSON())).toThrow();
  104. expect(createRegistryProviderConfig(config).ok).toBe(false);
  105. });
  106. it("未知 group 不能凭借非空绕过 Registry 准入", () => {
  107. const config = new ProviderConfig({ group: "unknown" as ProviderGroup, access, api });
  108. expect(() => parseProviderConfig(config.toJSON())).toThrow();
  109. expect(createRegistryProviderConfig(config).ok).toBe(false);
  110. });
  111. it("可选管理 URL 填写后也遵守 schema 的合法 URL 约束", () => {
  112. const config = new ProviderConfig({
  113. group: "standard-personal",
  114. api,
  115. access: new ApiKeyAccessConfig({ apiKey: "test-key", apiKeyManagementUrl: "not-url" }),
  116. });
  117. expect(() => parseProviderConfig(config.toJSON())).toThrow();
  118. expect(createRegistryProviderConfig(config).ok).toBe(false);
  119. });
  120. it("未声明可选展示/成员字段和显式 null 不影响完整性", () => {
  121. const config = parseProviderConfig({
  122. group: "standard-personal",
  123. api: { ...api.toJSON(), headers: null },
  124. access: { ...access.toJSON(), apiKeyManagementUrl: null },
  125. logo: null,
  126. visibility: null,
  127. personalModelIds: null,
  128. builtinModelIds: null,
  129. modelOrder: null,
  130. });
  131. expect(createRegistryProviderConfig(config).ok).toBe(true);
  132. expect(parseProviderConfig(config.toJSON()).toJSON()).toEqual(config.toJSON());
  133. });
  134. it("Account 的 false 权益完整,清空必填值不完整,切换 Access 不保留旧 Key", () => {
  135. const original = new ProviderConfig({ group: "zai-family", access, api });
  136. const replaced = original.overlay(
  137. new ProviderConfig({
  138. access: new ZhipuAccountAccessConfig({
  139. accountType: "zai",
  140. mode: "off-peak",
  141. entitled: false,
  142. }),
  143. }),
  144. );
  145. expect(createRegistryProviderConfig(replaced).ok).toBe(true);
  146. expect(replaced.access?.toJSON()).toEqual({
  147. type: "zhipu-account",
  148. accountType: "zai",
  149. mode: "off-peak",
  150. entitled: false,
  151. });
  152. const cleared = replaced.overlay(
  153. new ProviderConfig({ access: new ZhipuAccountAccessConfig({ mode: null }) }),
  154. );
  155. expect(createRegistryProviderConfig(cleared).ok).toBe(false);
  156. expect(original.access?.toJSON()).toEqual(access.toJSON());
  157. });
  158. });