accountProviderTeamPlanRequestKey.test.ts 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import { describe, expect, it, vi } from "vitest";
  2. import { BIGMODEL_PROVIDER_ID, ZAI_PROVIDER_ID, type ApiClient } from "@zcode/shared";
  3. import { resolveAccountTeamPlanRuntimeApiKey } from "../src/model-provider/accountProviderTeamPlanRequestKey.js";
  4. describe("accountProviderTeamPlanRequestKey", () => {
  5. it.each([
  6. {
  7. family: "bigmodel" as const,
  8. oauthProviderId: BIGMODEL_PROVIDER_ID,
  9. },
  10. {
  11. family: "zai" as const,
  12. oauthProviderId: ZAI_PROVIDER_ID,
  13. },
  14. ])("resolves $family Team Plan request key from the selected org/project", async (fixture) => {
  15. const request = vi.fn(async (input: string | URL) => {
  16. const url = String(input);
  17. if (url.endsWith("/api/biz/customer/getCustomerInfo")) {
  18. return jsonResponse({
  19. code: 0,
  20. data: {
  21. organizations: [
  22. {
  23. organizationId: "org-1",
  24. projects: [{ projectId: "project-1" }],
  25. },
  26. ],
  27. },
  28. });
  29. }
  30. if (url.endsWith("/api_keys/team-api-key/access_tokens")) {
  31. return jsonResponse({
  32. code: 200,
  33. data: {
  34. accessToken: "project-token",
  35. tokenType: "Bearer",
  36. expiresIn: 600,
  37. expiresAt: Date.now() / 1000 + 600,
  38. },
  39. });
  40. }
  41. if (url.endsWith("/api_keys")) {
  42. return jsonResponse({
  43. code: 0,
  44. data: [
  45. {
  46. apiKey: "team-api-key",
  47. keyType: 2,
  48. name: "zcode-team-api-key",
  49. },
  50. ],
  51. });
  52. }
  53. return new Response("not found", { status: 404 });
  54. });
  55. const credentialService = {
  56. load: vi.fn(async (key: string) =>
  57. key === `oauth:${fixture.oauthProviderId}:access_token`
  58. ? `${fixture.family}-oauth-token`
  59. : null,
  60. ),
  61. };
  62. await expect(
  63. resolveAccountTeamPlanRuntimeApiKey({
  64. apiClient: { request } satisfies ApiClient,
  65. credentialService,
  66. access: {
  67. type: "zhipu-account",
  68. family: fixture.family,
  69. planKind: "team-coding-plan",
  70. productId: "product-1",
  71. organizationId: "org-1",
  72. projectId: "project-1",
  73. },
  74. }),
  75. ).resolves.toBe("project-token");
  76. expect(request).toHaveBeenCalledTimes(3);
  77. });
  78. });
  79. function jsonResponse(body: unknown): Response {
  80. return new Response(JSON.stringify(body), {
  81. status: 200,
  82. headers: { "content-type": "application/json" },
  83. });
  84. }