planIdentitySnapshot.test.ts 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. import { describe, expect, it } from "vitest";
  2. import { resolvePlanIdentitySnapshot } from "../src/plan-identity.js";
  3. import type { UsageEntitlementSnapshot } from "../src/usage-stats.js";
  4. function createEntitlementSnapshot(
  5. overrides: Partial<UsageEntitlementSnapshot> = {},
  6. ): UsageEntitlementSnapshot {
  7. return {
  8. generatedAt: 1_000,
  9. authenticated: true,
  10. provider: { id: "provider-a", name: "Provider A" },
  11. remaining: { count: 1, isShow: true },
  12. subscription: {
  13. identityType: "unknown",
  14. identityMasked: null,
  15. details: [
  16. {
  17. productId: "product-a",
  18. productName: "Product A",
  19. purchaseTime: null,
  20. beginTime: null,
  21. expireTime: null,
  22. },
  23. ],
  24. },
  25. quota: { level: "pro", limits: [] },
  26. ...overrides,
  27. };
  28. }
  29. describe("resolvePlanIdentitySnapshot", () => {
  30. it("prefers Coding Plan over Start Plan in the active provider family domain", () => {
  31. expect(
  32. resolvePlanIdentitySnapshot({
  33. providerFamilyDomain: "zai",
  34. codingPlanEntitlement: createEntitlementSnapshot({
  35. generatedAt: 1_100,
  36. subscription: {
  37. identityType: "unknown",
  38. identityMasked: null,
  39. details: [
  40. {
  41. productId: "coding-product",
  42. productName: "Coding Product",
  43. purchaseTime: null,
  44. beginTime: null,
  45. expireTime: null,
  46. },
  47. ],
  48. },
  49. }),
  50. startPlanEntitlement: createEntitlementSnapshot({
  51. generatedAt: 1_200,
  52. subscription: {
  53. identityType: "unknown",
  54. identityMasked: null,
  55. details: [
  56. {
  57. productId: "start-product",
  58. productName: "Start Product",
  59. purchaseTime: null,
  60. beginTime: null,
  61. expireTime: null,
  62. },
  63. ],
  64. },
  65. }),
  66. now: 1_300,
  67. entitlementCacheTtlMs: 1_000,
  68. }),
  69. ).toEqual({
  70. generatedAt: 1_100,
  71. planStatus: "coding_plan",
  72. planProductId: "coding-product",
  73. });
  74. });
  75. it("uses Start Plan only after Coding Plan is confirmed absent", () => {
  76. expect(
  77. resolvePlanIdentitySnapshot({
  78. providerFamilyDomain: "bigmodel",
  79. codingPlanEntitlement: createEntitlementSnapshot({
  80. unavailableReason: "no_plan",
  81. remaining: null,
  82. subscription: null,
  83. quota: null,
  84. }),
  85. startPlanEntitlement: createEntitlementSnapshot({
  86. subscription: {
  87. identityType: "unknown",
  88. identityMasked: null,
  89. details: [
  90. {
  91. productId: "start-plan-id",
  92. productName: "Start Product",
  93. purchaseTime: null,
  94. beginTime: null,
  95. expireTime: null,
  96. },
  97. ],
  98. },
  99. }),
  100. now: 1_300,
  101. entitlementCacheTtlMs: 1_000,
  102. }),
  103. ).toMatchObject({
  104. planStatus: "start_plan",
  105. planProductId: "start-plan-id",
  106. });
  107. });
  108. it("uses the first active Start Plan in server order as plan_product_id and follows server reordering", () => {
  109. // 口径快照(供数据侧评审):Start Plan subscription.details 现在是全部 active 套餐
  110. // 按服务端优先级排序;plan_product_id 固定取 details[0]。服务端调整排序或套餐
  111. // 增减时,该维度会跟随翻转——这是有意的口径,不是回归。
  112. const buildStartPlanDetails = (firstId: string, secondId: string) => ({
  113. identityType: "unknown" as const,
  114. identityMasked: null,
  115. details: [
  116. {
  117. productId: firstId,
  118. productName: "Start Priority One",
  119. purchaseTime: null,
  120. beginTime: null,
  121. expireTime: null,
  122. },
  123. {
  124. productId: secondId,
  125. productName: "Start Priority Two",
  126. purchaseTime: null,
  127. beginTime: null,
  128. expireTime: null,
  129. },
  130. ],
  131. });
  132. const noPlan = createEntitlementSnapshot({
  133. unavailableReason: "no_plan",
  134. remaining: null,
  135. subscription: null,
  136. quota: null,
  137. });
  138. const firstSnapshot = resolvePlanIdentitySnapshot({
  139. providerFamilyDomain: "zai",
  140. codingPlanEntitlement: noPlan,
  141. startPlanEntitlement: createEntitlementSnapshot({
  142. subscription: buildStartPlanDetails("start-first", "start-second"),
  143. }),
  144. now: 1_300,
  145. entitlementCacheTtlMs: 1_000,
  146. });
  147. expect(firstSnapshot).toMatchObject({
  148. planStatus: "start_plan",
  149. planProductId: "start-first",
  150. });
  151. const reorderedSnapshot = resolvePlanIdentitySnapshot({
  152. providerFamilyDomain: "zai",
  153. codingPlanEntitlement: noPlan,
  154. startPlanEntitlement: createEntitlementSnapshot({
  155. subscription: buildStartPlanDetails("start-second", "start-first"),
  156. }),
  157. now: 1_300,
  158. entitlementCacheTtlMs: 1_000,
  159. });
  160. expect(reorderedSnapshot).toMatchObject({
  161. planStatus: "start_plan",
  162. planProductId: "start-second",
  163. });
  164. });
  165. it("keeps Coding Plan active with an empty product id when quota is valid but subscription is missing", () => {
  166. expect(
  167. resolvePlanIdentitySnapshot({
  168. providerFamilyDomain: "zai",
  169. codingPlanEntitlement: createEntitlementSnapshot({
  170. subscription: null,
  171. }),
  172. startPlanEntitlement: null,
  173. now: 1_300,
  174. entitlementCacheTtlMs: 1_000,
  175. }),
  176. ).toMatchObject({
  177. planStatus: "coding_plan",
  178. planProductId: "",
  179. });
  180. });
  181. it("does not derive BigModel Team Plan product id from the selected local connection key", () => {
  182. expect(
  183. resolvePlanIdentitySnapshot({
  184. providerFamilyDomain: "bigmodel",
  185. modelProviderFamilySelectedKeys: {
  186. bigmodel:
  187. "team-plan:account:bigmodel-individual-coding-plan:product-team:org-team:project-team",
  188. },
  189. codingPlanEntitlement: createEntitlementSnapshot({
  190. context: {
  191. scope: "personal",
  192. productId: "product-personal",
  193. },
  194. subscription: {
  195. identityType: "unknown",
  196. identityMasked: null,
  197. details: [
  198. {
  199. productId: "product-personal",
  200. productName: "Personal Coding Plan",
  201. purchaseTime: null,
  202. beginTime: null,
  203. expireTime: null,
  204. },
  205. ],
  206. },
  207. }),
  208. startPlanEntitlement: null,
  209. now: 1_300,
  210. entitlementCacheTtlMs: 1_000,
  211. }),
  212. ).toEqual({
  213. generatedAt: 1_000,
  214. planStatus: "coding_plan",
  215. planProductId: "product-personal",
  216. });
  217. });
  218. it("returns no_plan only when Coding Plan and Start Plan are both explicitly absent", () => {
  219. const noPlan = createEntitlementSnapshot({
  220. unavailableReason: "no_plan",
  221. remaining: null,
  222. subscription: null,
  223. quota: null,
  224. });
  225. expect(
  226. resolvePlanIdentitySnapshot({
  227. providerFamilyDomain: "zai",
  228. codingPlanEntitlement: noPlan,
  229. startPlanEntitlement: noPlan,
  230. now: 1_300,
  231. entitlementCacheTtlMs: 1_000,
  232. }),
  233. ).toMatchObject({
  234. planStatus: "no_plan",
  235. planProductId: "",
  236. });
  237. });
  238. it("treats loading, failures, missing domain and stale snapshots as unknown", () => {
  239. expect(
  240. resolvePlanIdentitySnapshot({
  241. providerFamilyDomain: null,
  242. codingPlanEntitlement: createEntitlementSnapshot(),
  243. startPlanEntitlement: createEntitlementSnapshot(),
  244. now: 1_300,
  245. entitlementCacheTtlMs: 1_000,
  246. }).planStatus,
  247. ).toBe("unknown");
  248. expect(
  249. resolvePlanIdentitySnapshot({
  250. providerFamilyDomain: "zai",
  251. codingPlanEntitlement: createEntitlementSnapshot({
  252. unavailableReason: "unavailable",
  253. remaining: null,
  254. subscription: null,
  255. quota: null,
  256. }),
  257. startPlanEntitlement: createEntitlementSnapshot(),
  258. now: 1_300,
  259. entitlementCacheTtlMs: 1_000,
  260. }).planStatus,
  261. ).toBe("unknown");
  262. expect(
  263. resolvePlanIdentitySnapshot({
  264. providerFamilyDomain: "zai",
  265. codingPlanEntitlement: createEntitlementSnapshot({
  266. generatedAt: 1,
  267. }),
  268. startPlanEntitlement: createEntitlementSnapshot(),
  269. now: 1_300,
  270. entitlementCacheTtlMs: 1_000,
  271. }).planStatus,
  272. ).toBe("unknown");
  273. });
  274. });