accountProviderRequestAuthService.test.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. import { describe, expect, it, vi } from "vitest";
  2. import { BUILTIN_MODEL_PROVIDER_IDS, type ZCodeProviderAccountAccess } from "@zcode/shared";
  3. import {
  4. AccountRequestCredentialUnavailableError,
  5. createAccountProviderRequestAuthService,
  6. } from "../src/model-provider/accountProviderRequestAuthService.js";
  7. function createService(overrides: Record<string, unknown> = {}) {
  8. return createAccountProviderRequestAuthService({
  9. resolveCurrentAccountAccess: vi.fn(async (access: ZCodeProviderAccountAccess) => ({
  10. type: "zhipu-account" as const,
  11. family: access.accountType,
  12. planKind: access.mode === "off-peak" ? ("individual-coding-plan" as const) : access.mode,
  13. })),
  14. loadOAuthTokenSet: vi.fn(async () => ({ accessToken: "oauth", zcodeJwtToken: "jwt" })),
  15. loadIndividualPlanMaterial: vi.fn(async () => null),
  16. resolveTeamPlanMaterial: vi.fn(async () => null),
  17. ...overrides,
  18. } as never);
  19. }
  20. function individualAccess(): ZCodeProviderAccountAccess {
  21. return {
  22. type: "zhipu-account",
  23. accountType: "zai",
  24. mode: "individual-coding-plan",
  25. };
  26. }
  27. describe("AccountProviderRequestAuthService", () => {
  28. it("reads the provider-specific zcode jwt for start plan requests", async () => {
  29. const loadOAuthTokenSet = vi.fn(async () => ({ zcodeJwtToken: "zai-zcode-jwt" }));
  30. const service = createService({ loadOAuthTokenSet });
  31. await expect(
  32. service.resolveCurrent({
  33. providerId: BUILTIN_MODEL_PROVIDER_IDS.zaiStartPlan,
  34. accountAccess: {
  35. type: "zhipu-account",
  36. accountType: "zai",
  37. mode: "start-plan",
  38. },
  39. reason: "model-request",
  40. }),
  41. ).resolves.toEqual({ apiKey: "zai-zcode-jwt" });
  42. expect(loadOAuthTokenSet).toHaveBeenCalledWith("zai");
  43. });
  44. it("reads the latest individual coding plan key for every request", async () => {
  45. const loadIndividualPlanMaterial = vi
  46. .fn()
  47. .mockResolvedValueOnce({ token: "key-v1", apiKeyId: "id-key-v1" })
  48. .mockResolvedValueOnce({ token: "key-v2", apiKeyId: "id-key-v2" });
  49. const service = createService({ loadIndividualPlanMaterial });
  50. const input = {
  51. providerId: BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
  52. accountAccess: individualAccess(),
  53. reason: "model-request" as const,
  54. };
  55. await expect(service.resolveCurrent(input)).resolves.toMatchObject({
  56. apiKey: "key-v1",
  57. apiKeyId: "id-key-v1",
  58. accountScope: expect.any(String),
  59. });
  60. await expect(service.resolveCurrent(input)).resolves.toMatchObject({
  61. apiKey: "key-v2",
  62. apiKeyId: "id-key-v2",
  63. accountScope: expect.any(String),
  64. });
  65. expect(loadIndividualPlanMaterial).toHaveBeenCalledWith(
  66. BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
  67. "zai",
  68. undefined,
  69. );
  70. });
  71. it("passes the complete team access to the team key resolver", async () => {
  72. const providerAccess = {
  73. type: "zhipu-account" as const,
  74. accountType: "bigmodel" as const,
  75. mode: "team-coding-plan" as const,
  76. };
  77. const currentAccess = {
  78. type: "zhipu-account" as const,
  79. family: "bigmodel" as const,
  80. planKind: "team-coding-plan" as const,
  81. productId: "product-a",
  82. organizationId: "organization-a",
  83. projectId: "project-a",
  84. };
  85. const resolveTeamPlanMaterial = vi.fn(async () => ({
  86. token: "team-project-key",
  87. apiKeyId: "id-team-project-key",
  88. }));
  89. const service = createService({
  90. resolveCurrentAccountAccess: vi.fn(async () => currentAccess),
  91. resolveTeamPlanMaterial,
  92. });
  93. await expect(
  94. service.resolveCurrent({
  95. providerId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelTeamCodingPlan,
  96. accountAccess: providerAccess,
  97. reason: "model-request",
  98. }),
  99. ).resolves.toMatchObject({
  100. apiKey: "team-project-key",
  101. apiKeyId: "id-team-project-key",
  102. accountScope: expect.any(String),
  103. });
  104. expect(resolveTeamPlanMaterial).toHaveBeenCalledWith(currentAccess, undefined);
  105. });
  106. it("当前连接与 Active Model 的静态 access 不兼容时 fail-closed", async () => {
  107. const resolveCurrentAccountAccess = vi.fn(async () => null);
  108. const service = createService({ resolveCurrentAccountAccess });
  109. await expect(
  110. service.resolveCurrent({
  111. providerId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
  112. accountAccess: individualAccess(),
  113. reason: "model-request",
  114. }),
  115. ).rejects.toBeInstanceOf(AccountRequestCredentialUnavailableError);
  116. expect(resolveCurrentAccountAccess).toHaveBeenCalledWith(individualAccess());
  117. });
  118. it("uses a typed error when the selected access has no request credential", async () => {
  119. const service = createService();
  120. await expect(
  121. service.resolveCurrent({
  122. providerId: BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
  123. accountAccess: individualAccess(),
  124. reason: "off-peak",
  125. }),
  126. ).rejects.toBeInstanceOf(AccountRequestCredentialUnavailableError);
  127. });
  128. it("does not read credentials when no compatible current account exists", async () => {
  129. const loadIndividualPlanMaterial = vi.fn(async () => ({
  130. token: "new-account-key",
  131. apiKeyId: "id-new-account-key",
  132. }));
  133. const service = createService({
  134. resolveCurrentAccountAccess: vi.fn(async () => null),
  135. loadIndividualPlanMaterial,
  136. });
  137. await expect(
  138. service.resolveCurrent({
  139. providerId: BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
  140. accountAccess: individualAccess(),
  141. reason: "model-request",
  142. }),
  143. ).rejects.toBeInstanceOf(AccountRequestCredentialUnavailableError);
  144. expect(loadIndividualPlanMaterial).not.toHaveBeenCalled();
  145. });
  146. it("非 Model 当前态请求同样解析当前兼容连接", async () => {
  147. const resolveCurrentAccountAccess = vi.fn(async () => ({
  148. type: "zhipu-account" as const,
  149. family: "zai" as const,
  150. planKind: "individual-coding-plan" as const,
  151. }));
  152. const loadIndividualPlanMaterial = vi.fn(async () => ({
  153. token: "current-key",
  154. apiKeyId: "id-current-key",
  155. }));
  156. const service = createService({ resolveCurrentAccountAccess, loadIndividualPlanMaterial });
  157. await expect(
  158. service.resolveCurrent({
  159. providerId: BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
  160. accountAccess: individualAccess(),
  161. reason: "usage",
  162. }),
  163. ).resolves.toEqual({ apiKey: "current-key", apiKeyId: "id-current-key" });
  164. expect(resolveCurrentAccountAccess).toHaveBeenCalledOnce();
  165. });
  166. });