codingPlanPricingCards.test.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. import { afterEach, describe, expect, it, vi } from "vitest";
  2. import { BUILTIN_MODEL_PROVIDER_IDS, CODING_PLAN_SYSTEM_BUSY } from "@zcode/shared";
  3. import { resolveCodingPlanUpgradeProductsProviderId } from "@/settings/model-provider-section/codingPlanPricingCards.js";
  4. import {
  5. formatCodingPlanAmount,
  6. normalizeCodingPlanCardCopyItems,
  7. } from "@/settings/model-provider-section/codingPlanProductPresentation.js";
  8. import {
  9. clearCodingPlanProductsCacheForTest,
  10. loadCodingPlanStaticProductsSnapshotForTest,
  11. loadCodingPlanStaticProductListForTest,
  12. loadCodingPlanProductsForTest,
  13. normalizeErrorMessage,
  14. resolveCodingPlanProductsFailureSnapshot,
  15. } from "@/settings/model-provider-section/useCodingPlanProducts.js";
  16. // 原生购买面板(CodingPlanPurchasePanel / CodingPlanProductCards)已下线,
  17. // 购买流程改为 webview 弹窗;本文件只保留仍针对活代码的用例。
  18. afterEach(() => {
  19. clearCodingPlanProductsCacheForTest();
  20. vi.restoreAllMocks();
  21. });
  22. describe("CodingPlanProductCards", () => {
  23. it("formats coding plan prices by locale and currency", () => {
  24. expect(formatCodingPlanAmount(48.6, "USD", "en-US")).toBe("US$48.60");
  25. expect(formatCodingPlanAmount(48.6, "USD", "zh-CN")).toBe("$48.60 美元");
  26. expect(formatCodingPlanAmount(132.3, "CNY", "en-US")).toBe("CN¥132.30");
  27. expect(formatCodingPlanAmount(132.3, "CNY", "zh-CN")).toBe("¥132.30 人民币");
  28. });
  29. it("maps unrenderable payment errors to system busy guidance", () => {
  30. const htmlError =
  31. '<!doctypehtml><html lang="zh-cn"><title>405</title><body>Sorry, your request has been blocked as it may cause potential threats.</body></html>';
  32. expect(normalizeErrorMessage(new Error(htmlError))).toBe(CODING_PLAN_SYSTEM_BUSY);
  33. expect(
  34. normalizeErrorMessage(new Error("Unexpected token '<', \"<html\" is not valid JSON")),
  35. ).toBe(CODING_PLAN_SYSTEM_BUSY);
  36. });
  37. it("keeps the last preview snapshot when refresh fails", () => {
  38. const previewSnapshot = {
  39. productList: [
  40. {
  41. productId: "product-pro-quarter",
  42. productName: "Pro",
  43. priceUnit: "quarter",
  44. payAmount: 100,
  45. soldOut: true,
  46. hasPreview: true,
  47. },
  48. ],
  49. isSubscribed: false,
  50. isAuthenticated: true,
  51. };
  52. const staticFallback = {
  53. productList: [
  54. {
  55. productId: "product-pro-quarter",
  56. productName: "Pro",
  57. priceUnit: "quarter",
  58. payAmount: 100,
  59. hasPreview: false,
  60. },
  61. ],
  62. isSubscribed: false,
  63. isAuthenticated: null,
  64. };
  65. expect(
  66. resolveCodingPlanProductsFailureSnapshot(previewSnapshot, staticFallback).productList[0]
  67. ?.soldOut,
  68. ).toBe(true);
  69. expect(
  70. resolveCodingPlanProductsFailureSnapshot(null, staticFallback).productList[0]?.hasPreview,
  71. ).toBe(false);
  72. });
  73. it("caches successful batch-preview snapshots briefly and lets force bypass the cache", async () => {
  74. const batchPreview = vi
  75. .fn()
  76. .mockResolvedValueOnce({
  77. productList: [
  78. {
  79. productId: "product-e68d08",
  80. productName: "GLM Coding Lite",
  81. payAmount: 48.6,
  82. priceCurrency: "USD",
  83. },
  84. ],
  85. isSubscribed: false,
  86. isAuthenticated: true,
  87. })
  88. .mockResolvedValueOnce({
  89. productList: [
  90. {
  91. productId: "product-e68d08",
  92. productName: "GLM Coding Lite",
  93. payAmount: 50,
  94. priceCurrency: "USD",
  95. },
  96. ],
  97. isSubscribed: false,
  98. isAuthenticated: true,
  99. });
  100. const service = { batchPreview } as never;
  101. const first = await loadCodingPlanProductsForTest(
  102. BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
  103. service,
  104. false,
  105. );
  106. const second = await loadCodingPlanProductsForTest(
  107. BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
  108. service,
  109. false,
  110. );
  111. const forced = await loadCodingPlanProductsForTest(
  112. BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
  113. service,
  114. true,
  115. );
  116. expect(batchPreview).toHaveBeenCalledTimes(2);
  117. expect(
  118. first.productList.find((product) => product.productId === "product-e68d08")?.payAmount,
  119. ).toBe(48.6);
  120. expect(
  121. second.productList.find((product) => product.productId === "product-e68d08")?.payAmount,
  122. ).toBe(48.6);
  123. expect(
  124. forced.productList.find((product) => product.productId === "product-e68d08")?.payAmount,
  125. ).toBe(50);
  126. });
  127. it("isolates product snapshots for the same provider across service instances", async () => {
  128. const createService = (payAmount: number) => ({
  129. batchPreview: vi.fn(async () => ({
  130. productList: [
  131. {
  132. productId: "product-e68d08",
  133. productName: "GLM Coding Lite",
  134. payAmount,
  135. priceCurrency: "USD",
  136. },
  137. ],
  138. isSubscribed: false,
  139. isAuthenticated: true,
  140. })),
  141. });
  142. const serviceA = createService(10);
  143. const serviceB = createService(20);
  144. const first = await loadCodingPlanProductsForTest(
  145. BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
  146. serviceA as never,
  147. false,
  148. );
  149. const second = await loadCodingPlanProductsForTest(
  150. BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
  151. serviceB as never,
  152. false,
  153. );
  154. expect(serviceA.batchPreview).toHaveBeenCalledOnce();
  155. expect(serviceB.batchPreview).toHaveBeenCalledOnce();
  156. expect(first.productList[0]?.payAmount).toBe(10);
  157. expect(second.productList[0]?.payAmount).toBe(20);
  158. });
  159. it("prevents stale in-flight previews from replacing a forced refresh cache", async () => {
  160. type PreviewSnapshot = {
  161. productList: Array<{
  162. productId: string;
  163. productName: string;
  164. payAmount: number;
  165. priceCurrency: string;
  166. }>;
  167. isSubscribed: boolean;
  168. isAuthenticated: boolean;
  169. };
  170. let resolveInitialPreview: ((snapshot: PreviewSnapshot) => void) | null = null;
  171. let resolveForcedPreview: ((snapshot: PreviewSnapshot) => void) | null = null;
  172. const initialPreview = new Promise<PreviewSnapshot>((resolve) => {
  173. resolveInitialPreview = resolve;
  174. });
  175. const forcedPreview = new Promise<PreviewSnapshot>((resolve) => {
  176. resolveForcedPreview = resolve;
  177. });
  178. const batchPreview = vi
  179. .fn()
  180. .mockReturnValueOnce(initialPreview)
  181. .mockReturnValueOnce(forcedPreview);
  182. const service = { batchPreview } as never;
  183. const initialLoad = loadCodingPlanProductsForTest(
  184. BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
  185. service,
  186. false,
  187. );
  188. const forcedLoad = loadCodingPlanProductsForTest(
  189. BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
  190. service,
  191. true,
  192. );
  193. resolveForcedPreview?.({
  194. productList: [
  195. {
  196. productId: "product-e68d08",
  197. productName: "GLM Coding Lite",
  198. payAmount: 50,
  199. priceCurrency: "USD",
  200. },
  201. ],
  202. isSubscribed: false,
  203. isAuthenticated: true,
  204. });
  205. const forced = await forcedLoad;
  206. resolveInitialPreview?.({
  207. productList: [
  208. {
  209. productId: "product-e68d08",
  210. productName: "GLM Coding Lite",
  211. payAmount: 10,
  212. priceCurrency: "USD",
  213. },
  214. ],
  215. isSubscribed: false,
  216. isAuthenticated: false,
  217. });
  218. await initialLoad;
  219. const cached = await loadCodingPlanProductsForTest(
  220. BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
  221. service,
  222. false,
  223. );
  224. expect(batchPreview).toHaveBeenCalledTimes(2);
  225. expect(
  226. forced.productList.find((product) => product.productId === "product-e68d08")?.payAmount,
  227. ).toBe(50);
  228. expect(
  229. cached.productList.find((product) => product.productId === "product-e68d08")?.payAmount,
  230. ).toBe(50);
  231. });
  232. it("loads remote static products once per day while viewing plans", async () => {
  233. const getStaticProducts = vi.fn(async () => ({
  234. [BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan]: [
  235. {
  236. productId: "remote-zai-lite-quarter",
  237. productName: "Remote Lite",
  238. description: "old remote zai lite description",
  239. productEquityList: [
  240. {
  241. productEquityTitle: "Remote quota",
  242. productEquityDetails: "Daily cached",
  243. },
  244. ],
  245. priceUnit: "quarter",
  246. displayOrder: 1,
  247. priceCurrency: "USD",
  248. payAmount: 42,
  249. },
  250. ],
  251. }));
  252. const service = { getStaticProducts } as never;
  253. const first = await loadCodingPlanStaticProductListForTest(
  254. BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
  255. service,
  256. );
  257. const second = await loadCodingPlanStaticProductListForTest(
  258. BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
  259. service,
  260. );
  261. expect(getStaticProducts).toHaveBeenCalledTimes(1);
  262. expect(first[0]?.productId).toBe("remote-zai-lite-quarter");
  263. expect(first[0]?.description).toBe("old remote zai lite description");
  264. expect(second[0]?.productId).toBe("remote-zai-lite-quarter");
  265. });
  266. it("shows static products without calling batch-preview when remote preview is disabled", async () => {
  267. const batchPreview = vi.fn();
  268. const getStaticProducts = vi.fn(async () => ({
  269. [BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan]: [
  270. {
  271. productId: "remote-bigmodel-lite-quarter",
  272. productName: "Remote BigModel Lite",
  273. description: ["旧远端 BigModel Lite 文案", "适配当前卡片布局"],
  274. productEquityList: [
  275. {
  276. productEquityTitle: "Remote quota",
  277. productEquityDetails: "Shown before login",
  278. },
  279. ],
  280. priceUnit: "quarter",
  281. displayOrder: 1,
  282. priceCurrency: "CNY",
  283. payAmount: 128,
  284. },
  285. ],
  286. }));
  287. const service = { batchPreview, getStaticProducts } as never;
  288. const snapshot = await loadCodingPlanStaticProductsSnapshotForTest(
  289. BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
  290. service,
  291. );
  292. expect(batchPreview).not.toHaveBeenCalled();
  293. expect(snapshot.productList).toHaveLength(1);
  294. expect(snapshot.productList[0]?.productId).toBe("remote-bigmodel-lite-quarter");
  295. expect(snapshot.productList[0]?.productDescription).toBe(
  296. "旧远端 BigModel Lite 文案\n适配当前卡片布局",
  297. );
  298. expect(snapshot.productList[0]?.hasPreview).toBe(false);
  299. });
  300. it("keeps remote Coding Plan static product descriptions", async () => {
  301. const getStaticProducts = vi.fn(async () => ({
  302. [BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan]: [
  303. {
  304. productId: "zai-lite",
  305. productName: "GLM Coding Lite",
  306. description: "old zai lite",
  307. productEquityList: [],
  308. priceUnit: "month",
  309. displayOrder: 1,
  310. priceCurrency: "USD",
  311. },
  312. {
  313. productId: "zai-pro",
  314. productName: "GLM Coding Pro",
  315. description: "old zai pro",
  316. productEquityList: [],
  317. priceUnit: "month",
  318. displayOrder: 2,
  319. priceCurrency: "USD",
  320. },
  321. {
  322. productId: "zai-max",
  323. productName: "GLM Coding Max",
  324. description: "old zai max",
  325. productEquityList: [],
  326. priceUnit: "month",
  327. displayOrder: 3,
  328. priceCurrency: "USD",
  329. },
  330. ],
  331. [BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan]: [
  332. {
  333. productId: "bigmodel-lite",
  334. productName: "GLM Coding Lite",
  335. description: "旧 BigModel Lite",
  336. productEquityList: [],
  337. priceUnit: "month",
  338. displayOrder: 1,
  339. priceCurrency: "CNY",
  340. },
  341. {
  342. productId: "bigmodel-pro",
  343. productName: "GLM Coding Pro",
  344. description: "旧 BigModel Pro",
  345. productEquityList: [],
  346. priceUnit: "month",
  347. displayOrder: 2,
  348. priceCurrency: "CNY",
  349. },
  350. {
  351. productId: "bigmodel-max",
  352. productName: "GLM Coding Max",
  353. description: "旧 BigModel Max",
  354. productEquityList: [],
  355. priceUnit: "month",
  356. displayOrder: 3,
  357. priceCurrency: "CNY",
  358. },
  359. ],
  360. }));
  361. const service = { getStaticProducts } as never;
  362. const zaiProducts = await loadCodingPlanStaticProductListForTest(
  363. BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
  364. service,
  365. );
  366. const bigmodelProducts = await loadCodingPlanStaticProductListForTest(
  367. BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
  368. service,
  369. );
  370. expect(zaiProducts.map((product) => product.description)).toEqual([
  371. "old zai lite",
  372. "old zai pro",
  373. "old zai max",
  374. ]);
  375. expect(bigmodelProducts.map((product) => product.description)).toEqual([
  376. "旧 BigModel Lite",
  377. "旧 BigModel Pro",
  378. "旧 BigModel Max",
  379. ]);
  380. });
  381. });
  382. describe("Z.ai Start products", () => {
  383. it("ignores malformed remote card copy items", () => {
  384. expect(
  385. normalizeCodingPlanCardCopyItems([
  386. null,
  387. 42,
  388. {},
  389. { tooltip: "missing text" },
  390. " valid ",
  391. { text: " remote ", tooltip: " detail " },
  392. ]),
  393. ).toEqual([{ text: "valid" }, { text: "remote", tooltip: "detail" }]);
  394. });
  395. it("uses the Z.ai Coding Plan product source when Start Plan upgrades", () => {
  396. expect(
  397. resolveCodingPlanUpgradeProductsProviderId(BUILTIN_MODEL_PROVIDER_IDS.zaiStartPlan),
  398. ).toBe(BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan);
  399. expect(
  400. resolveCodingPlanUpgradeProductsProviderId(
  401. BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
  402. ),
  403. ).toBe(BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan);
  404. expect(
  405. resolveCodingPlanUpgradeProductsProviderId(
  406. BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
  407. ),
  408. ).toBe(BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan);
  409. expect(
  410. resolveCodingPlanUpgradeProductsProviderId(BUILTIN_MODEL_PROVIDER_IDS.bigmodelStartPlan),
  411. ).toBe(BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan);
  412. });
  413. it("prepends the free Start SKUs only when startPlanPreview exists", async () => {
  414. const getStaticProducts = vi.fn(async () => ({
  415. [BUILTIN_MODEL_PROVIDER_IDS.zaiStartPlan]: [
  416. {
  417. productId: "remote-zai-lite-quarter",
  418. productName: "Remote Lite",
  419. productEquityList: [],
  420. priceUnit: "quarter",
  421. displayOrder: 1,
  422. priceCurrency: "USD",
  423. payAmount: 42,
  424. },
  425. ],
  426. }));
  427. const getStartPlanPreview = vi.fn(async () => ({
  428. planId: "zai-start-free",
  429. name: "Z.ai Start",
  430. entitlements: [
  431. {
  432. grantUnits: 1000,
  433. meter: "tokens",
  434. period: "day",
  435. showName: "Start free plan",
  436. unitType: "tokens",
  437. },
  438. ],
  439. }));
  440. const service = { getStaticProducts, getStartPlanPreview } as never;
  441. const products = await loadCodingPlanStaticProductListForTest(
  442. BUILTIN_MODEL_PROVIDER_IDS.zaiStartPlan,
  443. service,
  444. );
  445. expect(products.slice(0, 3).map((product) => product.productId)).toEqual([
  446. "zai-start-free-monthly",
  447. "zai-start-free-quarterly",
  448. "zai-start-free-yearly",
  449. ]);
  450. expect(products[3]?.productId).toBe("remote-zai-lite-quarter");
  451. expect(getStartPlanPreview).toHaveBeenCalledTimes(1);
  452. });
  453. it("does not show the free Start SKUs when startPlanPreview is missing", async () => {
  454. const getStaticProducts = vi.fn(async () => ({
  455. [BUILTIN_MODEL_PROVIDER_IDS.zaiStartPlan]: [
  456. {
  457. productId: "remote-zai-lite-quarter",
  458. productName: "Remote Lite",
  459. productEquityList: [],
  460. priceUnit: "quarter",
  461. displayOrder: 1,
  462. priceCurrency: "USD",
  463. payAmount: 42,
  464. },
  465. ],
  466. }));
  467. const getStartPlanPreview = vi.fn(async () => null);
  468. const service = { getStaticProducts, getStartPlanPreview } as never;
  469. const products = await loadCodingPlanStaticProductListForTest(
  470. BUILTIN_MODEL_PROVIDER_IDS.zaiStartPlan,
  471. service,
  472. );
  473. expect(products.map((product) => product.productId)).toEqual(["remote-zai-lite-quarter"]);
  474. });
  475. it("removes Start SKUs from the Z.ai Coding Plan purchase list", async () => {
  476. const batchPreview = vi.fn(async () => ({
  477. productList: [
  478. {
  479. productId: "zai-start-free-monthly",
  480. productName: "Z.ai Start",
  481. payAmount: 0,
  482. priceCurrency: "USD",
  483. },
  484. {
  485. productId: "product-zai-pro-monthly",
  486. productName: "GLM Coding Pro",
  487. payAmount: 99,
  488. priceCurrency: "USD",
  489. },
  490. ],
  491. isSubscribed: false,
  492. isAuthenticated: true,
  493. }));
  494. const service = { batchPreview } as never;
  495. const snapshot = await loadCodingPlanProductsForTest(
  496. BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
  497. service,
  498. false,
  499. );
  500. expect(snapshot.productList.map((product) => product.productId)).toEqual([
  501. "product-zai-pro-monthly",
  502. ]);
  503. });
  504. });