codingPlanEntitlement.test.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import { describe, expect, it, vi } from "vitest";
  2. import {
  3. fetchPersonalCodingPlanEntitlement,
  4. fetchTeamCodingPlanEntitlement,
  5. } from "../src/bigmodel/codingPlanEntitlement.js";
  6. const active = {
  7. productId: "personal",
  8. productName: "GLM Coding Lite",
  9. status: "VALID",
  10. inCurrentPeriod: true,
  11. };
  12. function client(data: unknown) {
  13. return {
  14. request: vi.fn(async () => new Response(JSON.stringify({ code: 200, success: true, data }))),
  15. };
  16. }
  17. describe("订阅权益不依赖 quota", () => {
  18. it.each([
  19. { data: [active], expected: "available" },
  20. { data: [{ ...active, inCurrentPeriod: false }], expected: "unavailable" },
  21. { data: [], expected: "unavailable" },
  22. { data: null, expected: "unknown" },
  23. { data: [{}], expected: "unavailable" },
  24. { data: [{}, active], expected: "available" },
  25. { data: [{ productName: "Storage" }, active], expected: "available" },
  26. { data: [null, 42, active], expected: "available" },
  27. { data: [{ productName: "Coding legacy" }], expected: "unknown" },
  28. { data: [{ productId: "coding-plan", status: "VALID" }], expected: "unknown" },
  29. {
  30. data: [{ productId: 42, productName: "Coding", status: "VALID", inCurrentPeriod: true }],
  31. expected: "unknown",
  32. },
  33. { data: [{ productName: "Coding legacy" }, active], expected: "available" },
  34. { data: [active, { productName: "Storage" }], expected: "available" },
  35. ])("个人响应 $expected", async ({ data, expected }) => {
  36. const apiClient = client(data);
  37. const result = await fetchPersonalCodingPlanEntitlement({
  38. apiClient,
  39. authorization: "personal.secret",
  40. url: "https://bigmodel.cn/api/biz/subscription/list",
  41. timeoutMs: 100,
  42. });
  43. expect(result.kind).toBe(expected);
  44. expect(apiClient.request).toHaveBeenCalledExactlyOnceWith(
  45. "https://bigmodel.cn/api/biz/subscription/list",
  46. expect.objectContaining({
  47. headers: { Authorization: "personal.secret" },
  48. }),
  49. );
  50. });
  51. it.each([
  52. {
  53. data: {
  54. hasSubscription: true,
  55. status: "EFFECTIVE",
  56. memberGrantStatus: "VALID",
  57. productId: "team",
  58. productName: "团队",
  59. subscribeEndTime: "2027-08-25",
  60. },
  61. expected: "available",
  62. },
  63. { data: { hasSubscription: false }, expected: "unavailable" },
  64. {
  65. data: {
  66. hasSubscription: true,
  67. status: "EXPIRED",
  68. memberGrantStatus: "UNASSIGNED",
  69. subscribeEndTime: "2026-08-06",
  70. },
  71. expected: "unavailable",
  72. },
  73. {
  74. data: { hasSubscription: true, status: "EFFECTIVE", memberGrantStatus: "UNASSIGNED" },
  75. expected: "unavailable",
  76. },
  77. {
  78. data: { hasSubscription: true, status: "FUTURE_STATE", memberGrantStatus: "UNASSIGNED" },
  79. expected: "unknown",
  80. },
  81. {
  82. data: {
  83. hasSubscription: true,
  84. status: "EFFECTIVE",
  85. memberGrantStatus: "FUTURE_STATE",
  86. },
  87. expected: "unknown",
  88. },
  89. { data: {}, expected: "unknown" },
  90. { data: null, expected: "unknown" },
  91. ])("团队响应 $expected", async ({ data, expected }) => {
  92. const apiClient = client(data);
  93. const result = await fetchTeamCodingPlanEntitlement({
  94. apiClient,
  95. authorization: "business-token",
  96. host: "https://bigmodel.cn",
  97. teamContext: { organizationId: "org", projectId: "project" },
  98. timeoutMs: 100,
  99. });
  100. expect(result.kind).toBe(expected);
  101. expect(apiClient.request).toHaveBeenCalledExactlyOnceWith(
  102. "https://bigmodel.cn/api/biz/team/subscribe/product/querySubscribeDetail",
  103. expect.objectContaining({
  104. headers: {
  105. Authorization: "business-token",
  106. "bigmodel-organization": "org",
  107. "bigmodel-project": "project",
  108. },
  109. }),
  110. );
  111. if (result.kind === "available")
  112. expect(result.subscription).toMatchObject({
  113. productId: "team",
  114. subscribeEndTime: "2027-08-25",
  115. });
  116. });
  117. });
  118. describe("个人与团队共享响应 envelope 兼容", () => {
  119. it.each([
  120. { envelope: { success: true }, expected: "available" },
  121. { envelope: {}, expected: "available" },
  122. { envelope: { code: 0 }, expected: "available" },
  123. { envelope: { code: 200 }, expected: "available" },
  124. { envelope: { code: 500, success: true }, expected: "unknown" },
  125. { envelope: { code: 0, success: false }, expected: "unknown" },
  126. { envelope: { code: null }, expected: "unknown" },
  127. { envelope: { code: "200" }, expected: "unknown" },
  128. ])("$envelope => $expected", async ({ envelope, expected }) => {
  129. const apiClient = {
  130. request: vi.fn(
  131. async (url: string) =>
  132. new Response(
  133. JSON.stringify({
  134. ...envelope,
  135. data: url.endsWith("subscription/list")
  136. ? [active]
  137. : {
  138. hasSubscription: true,
  139. status: "EFFECTIVE",
  140. memberGrantStatus: "VALID",
  141. },
  142. }),
  143. ),
  144. ),
  145. };
  146. expect(
  147. (
  148. await fetchPersonalCodingPlanEntitlement({
  149. apiClient,
  150. authorization: "key",
  151. url: "https://bigmodel.cn/api/biz/subscription/list",
  152. timeoutMs: 100,
  153. })
  154. ).kind,
  155. ).toBe(expected);
  156. expect(
  157. (
  158. await fetchTeamCodingPlanEntitlement({
  159. apiClient,
  160. authorization: "business",
  161. host: "https://bigmodel.cn",
  162. teamContext: { organizationId: "org", projectId: "project" },
  163. timeoutMs: 100,
  164. })
  165. ).kind,
  166. ).toBe(expected);
  167. });
  168. });