codingPlanEntitlementUsage.test.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import { BUILTIN_MODEL_PROVIDER_IDS, type ApiClient } from "@zcode/shared";
  2. import { describe, expect, it, vi } from "vitest";
  3. import { BigModelUsageQuotaProvider } from "../src/usage-stats/providers/bigmodelUsageQuotaProvider.js";
  4. const json = (data: unknown) => new Response(JSON.stringify({ success: true, data }));
  5. describe("订阅快照与额度/调用 Key 独立", () => {
  6. it.each(["zero", "missing", "throw"])(
  7. "个人混合订阅列表 quota=%s 不撤销有效订阅",
  8. async (quota) => {
  9. const request = vi.fn(async (url: string) => {
  10. if (url.includes("subscription/list"))
  11. return json([
  12. {},
  13. { productName: "Storage" },
  14. {
  15. productId: "coding-lite",
  16. productName: "GLM Coding Lite",
  17. status: "VALID",
  18. inCurrentPeriod: true,
  19. },
  20. ]);
  21. if (quota === "throw") throw new Error("network");
  22. return json(
  23. quota === "missing"
  24. ? null
  25. : { level: "lite", limits: [{ type: "TIME_LIMIT", remaining: 0 }] },
  26. );
  27. });
  28. const provider = new BigModelUsageQuotaProvider({
  29. apiClient: { request },
  30. env: { ZCODE_BIGMODEL_USAGE_API_KEY: "personal.secret" },
  31. accountRequestAuthService: {
  32. resolveAccessCurrent: vi.fn(),
  33. resolveCurrent: vi.fn(),
  34. assertCurrent: vi.fn(),
  35. },
  36. });
  37. const snapshot = await provider.getSnapshot();
  38. expect(snapshot.unavailableReason).toBeUndefined();
  39. expect(snapshot.subscription?.details[0]?.productId).toBe("coding-lite");
  40. },
  41. );
  42. it("quota 有 level 不能构造个人订阅", async () => {
  43. const apiClient: ApiClient = {
  44. request: vi.fn(async (url) =>
  45. json(String(url).includes("subscription/list") ? [] : { level: "max", limits: [] }),
  46. ),
  47. };
  48. const provider = new BigModelUsageQuotaProvider({
  49. apiClient,
  50. env: { ZCODE_BIGMODEL_USAGE_API_KEY: "personal.secret" },
  51. accountRequestAuthService: {
  52. resolveAccessCurrent: vi.fn(),
  53. resolveCurrent: vi.fn(),
  54. assertCurrent: vi.fn(),
  55. },
  56. });
  57. expect(await provider.getSnapshot()).toMatchObject({
  58. subscription: null,
  59. unavailableReason: "no_plan",
  60. });
  61. });
  62. it("团队 Key 不可用仍返回业务 token 查询的团队订阅,不查询个人订阅", async () => {
  63. const request = vi.fn(async (url: string, init?: RequestInit) => {
  64. expect(url).toBe("https://bigmodel.cn/api/biz/team/subscribe/product/querySubscribeDetail");
  65. expect(init?.headers).toEqual({
  66. Authorization: "business",
  67. "bigmodel-organization": "org",
  68. "bigmodel-project": "project",
  69. });
  70. return json({
  71. hasSubscription: true,
  72. status: "EFFECTIVE",
  73. memberGrantStatus: "VALID",
  74. productId: "team-max",
  75. productName: "团队套餐",
  76. subscribeEndTime: "2027-08-25",
  77. });
  78. });
  79. const assertCurrent = vi.fn();
  80. const provider = new BigModelUsageQuotaProvider({
  81. apiClient: { request },
  82. env: {},
  83. credentialService: { load: async () => "business" },
  84. accountRequestAuthService: {
  85. resolveAccessCurrent: vi.fn(),
  86. resolveCurrent: vi.fn(async () => {
  87. throw new Error("key unavailable");
  88. }),
  89. assertCurrent,
  90. },
  91. });
  92. const snapshot = await provider.getSnapshotForRequest({
  93. preferredProviderId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelTeamCodingPlan,
  94. accountAccess: {
  95. type: "zhipu-account",
  96. family: "bigmodel",
  97. planKind: "team-coding-plan",
  98. organizationId: "org",
  99. projectId: "project",
  100. productId: "stale-pricing-id",
  101. },
  102. });
  103. expect(snapshot.unavailableReason).toBeUndefined();
  104. expect(snapshot.subscription?.details[0]).toMatchObject({
  105. productId: "team-max",
  106. expireTime: "2027-08-25",
  107. });
  108. expect(snapshot.quota).toBeNull();
  109. expect(request).toHaveBeenCalledTimes(1);
  110. expect(assertCurrent).toHaveBeenCalledTimes(1);
  111. });
  112. it.each([
  113. ["EXPIRED", "expired"],
  114. ["EFFECTIVE", "unassigned"],
  115. ] as const)("团队 %s 经快照保留明确失效原因", async (status, reason) => {
  116. const request = vi.fn(async (url: string, init?: RequestInit) => {
  117. expect(url).toBe("https://bigmodel.cn/api/biz/team/subscribe/product/querySubscribeDetail");
  118. expect(init?.headers).toEqual({
  119. Authorization: "business",
  120. "bigmodel-organization": "org",
  121. "bigmodel-project": "project",
  122. });
  123. return json({
  124. hasSubscription: true,
  125. status,
  126. memberGrantStatus: "UNASSIGNED",
  127. productId: "team-max",
  128. productName: "团队套餐",
  129. subscribeEndTime: "2027-08-25",
  130. });
  131. });
  132. const assertCurrent = vi.fn();
  133. const provider = new BigModelUsageQuotaProvider({
  134. apiClient: { request },
  135. env: {},
  136. credentialService: { load: async () => "business" },
  137. accountRequestAuthService: {
  138. resolveAccessCurrent: vi.fn(),
  139. resolveCurrent: vi.fn(async () => {
  140. throw new Error("key unavailable");
  141. }),
  142. assertCurrent,
  143. },
  144. });
  145. const snapshot = await provider.getSnapshotForRequest({
  146. preferredProviderId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelTeamCodingPlan,
  147. accountAccess: {
  148. type: "zhipu-account",
  149. family: "bigmodel",
  150. planKind: "team-coding-plan",
  151. organizationId: "org",
  152. projectId: "project",
  153. productId: "stale-pricing-id",
  154. },
  155. });
  156. expect(snapshot.unavailableReason).toBe("no_plan");
  157. expect(snapshot).toHaveProperty("teamPlanUnavailableReason", reason);
  158. expect(snapshot.subscription).toBeNull();
  159. expect(snapshot.quota).toBeNull();
  160. expect(request).toHaveBeenCalledTimes(1);
  161. expect(assertCurrent).toHaveBeenCalledTimes(1);
  162. });
  163. });