manual-coding-plan-access.test.ts 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. createRegistryProviderConfig,
  4. parseProviderConfig,
  5. serializeRegistryProviderConfig,
  6. } from "../src/index.js";
  7. describe("Manual Coding Plan access", () => {
  8. it("保留显式 type、管理入口和 Key,不要求账号事实", () => {
  9. const base = parseProviderConfig({
  10. group: "standard-personal",
  11. access: {
  12. type: "zhipu-coding-plan-api-key",
  13. apiKeyManagementUrl: "https://z.ai/manage-apikey/apikey-list",
  14. },
  15. api: { type: "anthropic-messages", baseUrl: "https://api.z.ai/api/anthropic" },
  16. });
  17. expect(createRegistryProviderConfig(base).ok).toBe(false);
  18. const configured = base.overlay(
  19. parseProviderConfig({ access: { type: "zhipu-coding-plan-api-key", apiKey: "id.secret" } }),
  20. );
  21. const result = createRegistryProviderConfig(configured);
  22. if (!result.ok) throw new Error("manual key should be complete without account state");
  23. expect(serializeRegistryProviderConfig(result.config)).toEqual(configured.toJSON());
  24. expect(configured.toJSON().access).toEqual({ ...base.toJSON().access, apiKey: "id.secret" });
  25. expect(parseProviderConfig(configured.toJSON()).toJSON()).toEqual(configured.toJSON());
  26. expect(() =>
  27. parseProviderConfig({ access: { type: "zhipu-coding-plan-api-key", entitled: true } }),
  28. ).toThrow();
  29. expect(() =>
  30. parseProviderConfig({
  31. access: { type: "zhipu-coding-plan-api-key", apiKeyManagementUrl: "invalid" },
  32. }),
  33. ).toThrow();
  34. expect(
  35. configured
  36. .overlay(parseProviderConfig({ access: { type: "api-key", apiKey: "ordinary" } }))
  37. .toJSON().access,
  38. ).toEqual({ type: "api-key", apiKey: "ordinary" });
  39. });
  40. it.each(["api-key", "zhipu-coding-plan-api-key"])(
  41. "%s 只允许单一管理入口,不接受已撤销的团队字段",
  42. (type) => {
  43. expect(() =>
  44. parseProviderConfig({
  45. access: { type, teamApiKeyManagementUrl: "https://example.com/keys" },
  46. }),
  47. ).toThrow();
  48. const base = parseProviderConfig({
  49. access: { type, apiKey: "fixture-key", apiKeyManagementUrl: "https://example.com/keys" },
  50. });
  51. const replaced = base.overlay(
  52. parseProviderConfig({
  53. access: { type, apiKeyManagementUrl: "https://example.com/console" },
  54. }),
  55. );
  56. expect(replaced.toJSON().access).toEqual({
  57. type,
  58. apiKey: "fixture-key",
  59. apiKeyManagementUrl: "https://example.com/console",
  60. });
  61. expect(
  62. replaced
  63. .overlay(parseProviderConfig({ access: { type, apiKeyManagementUrl: null } }))
  64. .toJSON().access,
  65. ).toEqual({ type, apiKey: "fixture-key", apiKeyManagementUrl: null });
  66. },
  67. );
  68. });