usageStatsAccountAccess.test.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import { BUILTIN_MODEL_PROVIDER_IDS, type ApiClient } from "@zcode/shared";
  2. import { describe, expect, it, vi } from "vitest";
  3. import { createUsageStatsService } from "../src/usage-stats/usageStatsService.js";
  4. describe("UsageStats account access", () => {
  5. it("按当前 Account 连接获取 Coding Plan 鉴权,不读取旧 Provider Service", async () => {
  6. const resolve = vi.fn(async () => ({ apiKey: "account-a-plan-key" }));
  7. const request = vi.fn(async (_input: URL | RequestInfo, init?: RequestInit) => {
  8. if (String(_input).includes("subscription/list")) {
  9. expect(init?.headers).toMatchObject({ Authorization: "account-a-plan-key" });
  10. return new Response(JSON.stringify({ code: 200, data: [] }));
  11. }
  12. expect(init?.headers).toMatchObject({
  13. authorization: "account-a-plan-key",
  14. });
  15. return new Response(JSON.stringify({ code: 200, data: { level: "pro", limits: [] } }), {
  16. status: 200,
  17. headers: { "content-type": "application/json" },
  18. });
  19. });
  20. const service = createUsageStatsService({
  21. apiClient: { request } as ApiClient,
  22. accountRequestAuthService: {
  23. resolveAccessCurrent: vi.fn(async () => null),
  24. resolveCurrent: resolve,
  25. assertCurrent: vi.fn(async () => undefined),
  26. },
  27. credentialService: { load: vi.fn(async () => null) },
  28. zcodeAgentService: {
  29. getAppUsageStats: vi.fn(),
  30. },
  31. });
  32. await expect(
  33. service.getEntitlementSnapshot({
  34. includeSubscription: false,
  35. preferredProviderId: BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
  36. accountAccess: {
  37. type: "zhipu-account",
  38. family: "zai",
  39. planKind: "individual-coding-plan",
  40. },
  41. requirePreferredProvider: true,
  42. allowEnvApiKey: false,
  43. }),
  44. ).resolves.toMatchObject({ authenticated: true, quota: { level: "pro" } });
  45. expect(resolve).toHaveBeenCalledWith({
  46. providerId: BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
  47. accountAccess: {
  48. type: "zhipu-account",
  49. family: "zai",
  50. planKind: "individual-coding-plan",
  51. },
  52. reason: "usage",
  53. });
  54. });
  55. it("把 Registry 静态 mode 交给账号服务解析后再读取权益", async () => {
  56. const dynamicAccess = {
  57. type: "zhipu-account" as const,
  58. family: "bigmodel" as const,
  59. planKind: "team-coding-plan" as const,
  60. productId: "product-1",
  61. organizationId: "org-1",
  62. projectId: "project-1",
  63. };
  64. const resolveAccessCurrent = vi.fn(async () => dynamicAccess);
  65. const resolveCurrent = vi.fn(async () => ({ apiKey: "team-plan-key" }));
  66. const request = vi.fn(async (_input: URL | RequestInfo, init?: RequestInit) => {
  67. if (String(_input).includes("querySubscribeDetail")) {
  68. expect(init?.headers).toMatchObject({
  69. Authorization: "team-business-token",
  70. "bigmodel-organization": "org-1",
  71. "bigmodel-project": "project-1",
  72. });
  73. return new Response(JSON.stringify({ code: 200, data: { hasSubscription: false } }));
  74. }
  75. expect(init?.headers).toMatchObject({ authorization: "team-plan-key" });
  76. return new Response(JSON.stringify({ code: 200, data: { level: "team", limits: [] } }), {
  77. status: 200,
  78. headers: { "content-type": "application/json" },
  79. });
  80. });
  81. const service = createUsageStatsService({
  82. apiClient: { request } as ApiClient,
  83. accountRequestAuthService: {
  84. resolveAccessCurrent,
  85. resolveCurrent,
  86. assertCurrent: vi.fn(async () => undefined),
  87. },
  88. credentialService: {
  89. load: vi.fn(async (key) =>
  90. key === "oauth:bigmodel:access_token" ? "team-business-token" : null,
  91. ),
  92. },
  93. zcodeAgentService: { getAppUsageStats: vi.fn() },
  94. });
  95. await expect(
  96. service.getEntitlementSnapshot({
  97. includeSubscription: false,
  98. preferredProviderId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelTeamCodingPlan,
  99. accountAccess: {
  100. type: "zhipu-account",
  101. family: "bigmodel",
  102. mode: "team-coding-plan",
  103. },
  104. requirePreferredProvider: true,
  105. allowEnvApiKey: false,
  106. }),
  107. ).resolves.toMatchObject({
  108. authenticated: true,
  109. context: { scope: "team", organizationId: "org-1", projectId: "project-1" },
  110. quota: { level: "team" },
  111. });
  112. expect(resolveAccessCurrent).toHaveBeenCalledWith({
  113. type: "zhipu-account",
  114. family: "bigmodel",
  115. mode: "team-coding-plan",
  116. });
  117. expect(resolveCurrent).toHaveBeenCalledWith({
  118. providerId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelTeamCodingPlan,
  119. accountAccess: dynamicAccess,
  120. reason: "usage",
  121. });
  122. });
  123. });