legacyTeamOrganizationResolver.test.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import { describe, expect, it, vi } from "vitest";
  2. import {
  3. resolveBigModelApiOrigin,
  4. resolveZaiBusinessBaseUrl,
  5. type ApiRequestInit,
  6. } from "@zcode/shared";
  7. import { createLegacyTeamOrganizationResolver } from "../src/model-provider/legacyTeamOrganizationResolver.js";
  8. describe("旧 Team 组织查询", () => {
  9. it.each(["bigmodel", "zai"] as const)(
  10. "%s 只用该账号 OAuth GET 用户信息,不申请 API Key",
  11. async (family) => {
  12. const request = vi.fn(async (_url: string | URL, _init?: ApiRequestInit) =>
  13. Response.json({
  14. code: 200,
  15. data: {
  16. organizations: [{ organizationId: "org", projects: [{ projectId: "project" }] }],
  17. },
  18. }),
  19. );
  20. const load = vi.fn(async () => ({
  21. accessToken: "oauth-only",
  22. zcodeJwtToken: "different-jwt",
  23. }));
  24. const resolve = createLegacyTeamOrganizationResolver({
  25. apiClient: { request },
  26. loadOAuthTokenSet: load,
  27. });
  28. expect(await resolve({ family, productId: "product", projectId: "project" })).toBe("org");
  29. expect(load).toHaveBeenCalledWith(family);
  30. expect(request).toHaveBeenCalledOnce();
  31. expect(request.mock.calls[0]).toEqual([
  32. `${family === "zai" ? resolveZaiBusinessBaseUrl(process.env) : resolveBigModelApiOrigin(process.env)}/api/biz/customer/getCustomerInfo`,
  33. expect.objectContaining({
  34. method: "GET",
  35. headers: { Authorization: "oauth-only", "Content-Type": "application/json" },
  36. }),
  37. ]);
  38. },
  39. );
  40. it.each([
  41. { code: 0, data: { organizations: [] } },
  42. {
  43. code: 0,
  44. data: {
  45. organizations: [
  46. { organizationId: "a", projects: [{ projectId: "project" }] },
  47. { organizationId: "b", projects: [{ projectId: "project" }] },
  48. ],
  49. },
  50. },
  51. {
  52. code: 401,
  53. data: { organizations: [{ organizationId: "a", projects: [{ projectId: "project" }] }] },
  54. },
  55. ])("未确认唯一组织不猜测", async (payload) => {
  56. const resolve = createLegacyTeamOrganizationResolver({
  57. apiClient: { request: async () => Response.json(payload) },
  58. loadOAuthTokenSet: async () => ({ accessToken: "oauth" }),
  59. });
  60. expect(
  61. await resolve({ family: "bigmodel", productId: "product", projectId: "project" }),
  62. ).toBeNull();
  63. });
  64. it("网络期间账号凭据变化不采用旧账号结果", async () => {
  65. let token = "old-account";
  66. const resolve = createLegacyTeamOrganizationResolver({
  67. apiClient: {
  68. request: async () => {
  69. token = "new-account";
  70. return Response.json({
  71. code: 0,
  72. data: {
  73. organizations: [{ organizationId: "old-org", projects: [{ projectId: "project" }] }],
  74. },
  75. });
  76. },
  77. },
  78. loadOAuthTokenSet: async () => ({ accessToken: token }),
  79. });
  80. expect(
  81. await resolve({ family: "bigmodel", productId: "product", projectId: "project" }),
  82. ).toBeNull();
  83. });
  84. });