entitlement-presentation.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import { BUILTIN_MODEL_PROVIDER_IDS } from "@zcode/shared";
  2. import { buildProviderResponsiveStartBalance } from "./provider-family-responsive.js";
  3. /** 专属展示夹具;不改变其他套餐 E2E 的默认响应。 */
  4. export interface EntitlementPresentation {
  5. personal: "active" | "none";
  6. team: "active" | "none" | "expired" | "unassigned" | "failure";
  7. start: "active" | "none" | "expired" | "mixed" | "pending";
  8. }
  9. export function presentationResponse(path: string, state: EntitlementPresentation) {
  10. if (path === "/api/v1/client/configs") {
  11. const provider = BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan;
  12. return {
  13. code: 0,
  14. data: {
  15. configs: {
  16. startPlanPreview: {
  17. planId: "e2e-start-preview",
  18. name: "E2E_TRIAL_PROMO",
  19. entitlements: [
  20. {
  21. grantUnits: 8000000,
  22. meter: "model_usage",
  23. period: "daily",
  24. showName: "GLM-5.3",
  25. unitType: "token",
  26. },
  27. ],
  28. },
  29. codingPlanBillingDiscount: {
  30. "en-US": {
  31. cardTitle: "150% quota",
  32. cardBody: "Upgrade for 150% quota",
  33. badgeBody: "150% quota",
  34. },
  35. },
  36. codingPlanStaticProducts: {
  37. [provider]: [
  38. {
  39. productId: "personal-coding",
  40. productName: "Personal",
  41. priceCurrency: "CNY",
  42. priceUnit: "month",
  43. payAmount: 118,
  44. },
  45. ],
  46. },
  47. codingPlanStaticTeamProducts: {
  48. [provider]: [
  49. {
  50. productId: "product-team-a",
  51. productName: "Team",
  52. tier: "PRO",
  53. subscribeMode: "CONTINUOUS",
  54. subscribePeriod: "MONTHLY",
  55. purchaseMethodName: "Monthly",
  56. priceCurrency: "CNY",
  57. payAmount: 598,
  58. },
  59. ],
  60. },
  61. },
  62. },
  63. };
  64. }
  65. if (path === "/api/biz/subscription/enterprise/v2/pricing") {
  66. return {
  67. code: 200,
  68. data: {
  69. productList: [
  70. {
  71. productId: "product-team-a",
  72. productName: "Team",
  73. tier: "PRO",
  74. subscribeMode: "CONTINUOUS",
  75. subscribePeriod: "MONTHLY",
  76. purchaseMethodName: "Monthly",
  77. priceCurrency: "CNY",
  78. payAmount: 598,
  79. subscribed: true,
  80. },
  81. ],
  82. },
  83. };
  84. }
  85. if (path === "/api/biz/subscription/list") {
  86. return {
  87. code: 200,
  88. data:
  89. state.personal === "active"
  90. ? [
  91. {
  92. productId: "personal-coding",
  93. productName: "Coding Plan",
  94. inCurrentPeriod: true,
  95. status: "VALID",
  96. },
  97. ]
  98. : [],
  99. };
  100. }
  101. if (path === "/api/biz/team/subscribe/product/querySubscribeDetail") {
  102. if (state.team === "failure")
  103. return { code: 500, success: false, msg: "E2E_TEAM_ENTITLEMENT_FAILURE" };
  104. return {
  105. code: 200,
  106. success: true,
  107. data:
  108. state.team === "none"
  109. ? { hasSubscription: false }
  110. : {
  111. hasSubscription: true,
  112. status: state.team === "expired" ? "EXPIRED" : "EFFECTIVE",
  113. memberGrantStatus: state.team === "unassigned" ? "UNASSIGNED" : "VALID",
  114. productId: "product-team-a",
  115. productName: "Team Org",
  116. subscribeEndTime: "2027-08-25",
  117. subscribePeriod: "YEARLY",
  118. },
  119. };
  120. }
  121. if (path === "/api/v1/zcode-plan/billing/balance") {
  122. const result = buildProviderResponsiveStartBalance("GLM-5.3");
  123. if (state.start === "none")
  124. return { ...result, data: { ...result.data, plans: [], balances: [] } };
  125. if (state.start === "active") return result;
  126. const now = Math.floor(Date.now() / 1000);
  127. if (state.start === "pending") {
  128. // 待生效:套餐已存在(active)但权益 effective_at 在未来且暂无额度桶。
  129. // 账号侧会投影为 pending/entitled=false,设置页仍会读到同一份套餐详情。
  130. const pendingPlan = {
  131. ...result.data.plans[0],
  132. starts_at: now + 3600,
  133. ends_at: now + 86400,
  134. entitlements: [
  135. {
  136. entitlement_id: "e2e-pending-start-entitlement",
  137. effective_at: now + 3600,
  138. show_name: "GLM-5.3",
  139. },
  140. ],
  141. };
  142. return { ...result, data: { ...result.data, plans: [pendingPlan], balances: [] } };
  143. }
  144. const expiredPlan = {
  145. ...result.data.plans[0],
  146. plan_id: "e2e-expired-plan",
  147. name: "E2E_EXPIRED_PLAN",
  148. starts_at: now - 86400 * 3,
  149. ends_at: now - 86400,
  150. };
  151. const expiredBalance = {
  152. ...result.data.balances[0],
  153. plan_id: expiredPlan.plan_id,
  154. show_name: "E2E_EXPIRED_BALANCE",
  155. expires_at: now - 86400,
  156. };
  157. // JSON 时钟刻意仍在旧计划有效期内,HTTP Date 才是本轮权威时间。
  158. return {
  159. ...result,
  160. data: {
  161. server_time: now - 86400 * 2,
  162. plans: state.start === "mixed" ? [expiredPlan, ...result.data.plans] : [expiredPlan],
  163. balances:
  164. state.start === "mixed" ? [expiredBalance, ...result.data.balances] : [expiredBalance],
  165. },
  166. };
  167. }
  168. return undefined;
  169. }