codingPlanPricingCards.test.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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("prevents stale in-flight previews from replacing a forced refresh cache", async () => {
  128. type PreviewSnapshot = {
  129. productList: Array<{
  130. productId: string;
  131. productName: string;
  132. payAmount: number;
  133. priceCurrency: string;
  134. }>;
  135. isSubscribed: boolean;
  136. isAuthenticated: boolean;
  137. };
  138. let resolveInitialPreview: ((snapshot: PreviewSnapshot) => void) | null = null;
  139. let resolveForcedPreview: ((snapshot: PreviewSnapshot) => void) | null = null;
  140. const initialPreview = new Promise<PreviewSnapshot>((resolve) => {
  141. resolveInitialPreview = resolve;
  142. });
  143. const forcedPreview = new Promise<PreviewSnapshot>((resolve) => {
  144. resolveForcedPreview = resolve;
  145. });
  146. const batchPreview = vi
  147. .fn()
  148. .mockReturnValueOnce(initialPreview)
  149. .mockReturnValueOnce(forcedPreview);
  150. const service = { batchPreview } as never;
  151. const initialLoad = loadCodingPlanProductsForTest(
  152. BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
  153. service,
  154. false,
  155. );
  156. const forcedLoad = loadCodingPlanProductsForTest(
  157. BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
  158. service,
  159. true,
  160. );
  161. resolveForcedPreview?.({
  162. productList: [
  163. {
  164. productId: "product-e68d08",
  165. productName: "GLM Coding Lite",
  166. payAmount: 50,
  167. priceCurrency: "USD",
  168. },
  169. ],
  170. isSubscribed: false,
  171. isAuthenticated: true,
  172. });
  173. const forced = await forcedLoad;
  174. resolveInitialPreview?.({
  175. productList: [
  176. {
  177. productId: "product-e68d08",
  178. productName: "GLM Coding Lite",
  179. payAmount: 10,
  180. priceCurrency: "USD",
  181. },
  182. ],
  183. isSubscribed: false,
  184. isAuthenticated: false,
  185. });
  186. await initialLoad;
  187. const cached = await loadCodingPlanProductsForTest(
  188. BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
  189. service,
  190. false,
  191. );
  192. expect(batchPreview).toHaveBeenCalledTimes(2);
  193. expect(
  194. forced.productList.find((product) => product.productId === "product-e68d08")?.payAmount,
  195. ).toBe(50);
  196. expect(
  197. cached.productList.find((product) => product.productId === "product-e68d08")?.payAmount,
  198. ).toBe(50);
  199. });
  200. it("loads remote static products once per day while viewing plans", async () => {
  201. const getStaticProducts = vi.fn(async () => ({
  202. [BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan]: [
  203. {
  204. productId: "remote-zai-lite-quarter",
  205. productName: "Remote Lite",
  206. description: "old remote zai lite description",
  207. productEquityList: [
  208. {
  209. productEquityTitle: "Remote quota",
  210. productEquityDetails: "Daily cached",
  211. },
  212. ],
  213. priceUnit: "quarter",
  214. displayOrder: 1,
  215. priceCurrency: "USD",
  216. payAmount: 42,
  217. },
  218. ],
  219. }));
  220. const service = { getStaticProducts } as never;
  221. const first = await loadCodingPlanStaticProductListForTest(
  222. BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
  223. service,
  224. );
  225. const second = await loadCodingPlanStaticProductListForTest(
  226. BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
  227. service,
  228. );
  229. expect(getStaticProducts).toHaveBeenCalledTimes(1);
  230. expect(first[0]?.productId).toBe("remote-zai-lite-quarter");
  231. expect(first[0]?.description).toBe("old remote zai lite description");
  232. expect(second[0]?.productId).toBe("remote-zai-lite-quarter");
  233. });
  234. it("shows static products without calling batch-preview when remote preview is disabled", async () => {
  235. const batchPreview = vi.fn();
  236. const getStaticProducts = vi.fn(async () => ({
  237. [BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan]: [
  238. {
  239. productId: "remote-bigmodel-lite-quarter",
  240. productName: "Remote BigModel Lite",
  241. description: ["旧远端 BigModel Lite 文案", "适配当前卡片布局"],
  242. productEquityList: [
  243. {
  244. productEquityTitle: "Remote quota",
  245. productEquityDetails: "Shown before login",
  246. },
  247. ],
  248. priceUnit: "quarter",
  249. displayOrder: 1,
  250. priceCurrency: "CNY",
  251. payAmount: 128,
  252. },
  253. ],
  254. }));
  255. const service = { batchPreview, getStaticProducts } as never;
  256. const snapshot = await loadCodingPlanStaticProductsSnapshotForTest(
  257. BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
  258. service,
  259. );
  260. expect(batchPreview).not.toHaveBeenCalled();
  261. expect(snapshot.productList).toHaveLength(1);
  262. expect(snapshot.productList[0]?.productId).toBe("remote-bigmodel-lite-quarter");
  263. expect(snapshot.productList[0]?.productDescription).toBe(
  264. "旧远端 BigModel Lite 文案\n适配当前卡片布局",
  265. );
  266. expect(snapshot.productList[0]?.hasPreview).toBe(false);
  267. });
  268. it("keeps remote Coding Plan static product descriptions", async () => {
  269. const getStaticProducts = vi.fn(async () => ({
  270. [BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan]: [
  271. {
  272. productId: "zai-lite",
  273. productName: "GLM Coding Lite",
  274. description: "old zai lite",
  275. productEquityList: [],
  276. priceUnit: "month",
  277. displayOrder: 1,
  278. priceCurrency: "USD",
  279. },
  280. {
  281. productId: "zai-pro",
  282. productName: "GLM Coding Pro",
  283. description: "old zai pro",
  284. productEquityList: [],
  285. priceUnit: "month",
  286. displayOrder: 2,
  287. priceCurrency: "USD",
  288. },
  289. {
  290. productId: "zai-max",
  291. productName: "GLM Coding Max",
  292. description: "old zai max",
  293. productEquityList: [],
  294. priceUnit: "month",
  295. displayOrder: 3,
  296. priceCurrency: "USD",
  297. },
  298. ],
  299. [BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan]: [
  300. {
  301. productId: "bigmodel-lite",
  302. productName: "GLM Coding Lite",
  303. description: "旧 BigModel Lite",
  304. productEquityList: [],
  305. priceUnit: "month",
  306. displayOrder: 1,
  307. priceCurrency: "CNY",
  308. },
  309. {
  310. productId: "bigmodel-pro",
  311. productName: "GLM Coding Pro",
  312. description: "旧 BigModel Pro",
  313. productEquityList: [],
  314. priceUnit: "month",
  315. displayOrder: 2,
  316. priceCurrency: "CNY",
  317. },
  318. {
  319. productId: "bigmodel-max",
  320. productName: "GLM Coding Max",
  321. description: "旧 BigModel Max",
  322. productEquityList: [],
  323. priceUnit: "month",
  324. displayOrder: 3,
  325. priceCurrency: "CNY",
  326. },
  327. ],
  328. }));
  329. const service = { getStaticProducts } as never;
  330. const zaiProducts = await loadCodingPlanStaticProductListForTest(
  331. BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
  332. service,
  333. );
  334. const bigmodelProducts = await loadCodingPlanStaticProductListForTest(
  335. BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
  336. service,
  337. );
  338. expect(zaiProducts.map((product) => product.description)).toEqual([
  339. "old zai lite",
  340. "old zai pro",
  341. "old zai max",
  342. ]);
  343. expect(bigmodelProducts.map((product) => product.description)).toEqual([
  344. "旧 BigModel Lite",
  345. "旧 BigModel Pro",
  346. "旧 BigModel Max",
  347. ]);
  348. });
  349. });
  350. describe("Z.ai Start products", () => {
  351. it("ignores malformed remote card copy items", () => {
  352. expect(
  353. normalizeCodingPlanCardCopyItems([
  354. null,
  355. 42,
  356. {},
  357. { tooltip: "missing text" },
  358. " valid ",
  359. { text: " remote ", tooltip: " detail " },
  360. ]),
  361. ).toEqual([{ text: "valid" }, { text: "remote", tooltip: "detail" }]);
  362. });
  363. it("uses the Z.ai Coding Plan product source when Start Plan upgrades", () => {
  364. expect(
  365. resolveCodingPlanUpgradeProductsProviderId(BUILTIN_MODEL_PROVIDER_IDS.zaiStartPlan),
  366. ).toBe(BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan);
  367. expect(
  368. resolveCodingPlanUpgradeProductsProviderId(
  369. BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
  370. ),
  371. ).toBe(BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan);
  372. expect(
  373. resolveCodingPlanUpgradeProductsProviderId(
  374. BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
  375. ),
  376. ).toBe(BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan);
  377. expect(
  378. resolveCodingPlanUpgradeProductsProviderId(BUILTIN_MODEL_PROVIDER_IDS.bigmodelStartPlan),
  379. ).toBe(BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan);
  380. });
  381. it("prepends the free Start SKUs only when startPlanPreview exists", async () => {
  382. const getStaticProducts = vi.fn(async () => ({
  383. [BUILTIN_MODEL_PROVIDER_IDS.zaiStartPlan]: [
  384. {
  385. productId: "remote-zai-lite-quarter",
  386. productName: "Remote Lite",
  387. productEquityList: [],
  388. priceUnit: "quarter",
  389. displayOrder: 1,
  390. priceCurrency: "USD",
  391. payAmount: 42,
  392. },
  393. ],
  394. }));
  395. const getStartPlanPreview = vi.fn(async () => ({
  396. planId: "zai-start-free",
  397. name: "Z.ai Start",
  398. entitlements: [
  399. {
  400. grantUnits: 1000,
  401. meter: "tokens",
  402. period: "day",
  403. showName: "Start free plan",
  404. unitType: "tokens",
  405. },
  406. ],
  407. }));
  408. const service = { getStaticProducts, getStartPlanPreview } as never;
  409. const products = await loadCodingPlanStaticProductListForTest(
  410. BUILTIN_MODEL_PROVIDER_IDS.zaiStartPlan,
  411. service,
  412. );
  413. expect(products.slice(0, 3).map((product) => product.productId)).toEqual([
  414. "zai-start-free-monthly",
  415. "zai-start-free-quarterly",
  416. "zai-start-free-yearly",
  417. ]);
  418. expect(products[3]?.productId).toBe("remote-zai-lite-quarter");
  419. expect(getStartPlanPreview).toHaveBeenCalledTimes(1);
  420. });
  421. it("does not show the free Start SKUs when startPlanPreview is missing", async () => {
  422. const getStaticProducts = vi.fn(async () => ({
  423. [BUILTIN_MODEL_PROVIDER_IDS.zaiStartPlan]: [
  424. {
  425. productId: "remote-zai-lite-quarter",
  426. productName: "Remote Lite",
  427. productEquityList: [],
  428. priceUnit: "quarter",
  429. displayOrder: 1,
  430. priceCurrency: "USD",
  431. payAmount: 42,
  432. },
  433. ],
  434. }));
  435. const getStartPlanPreview = vi.fn(async () => null);
  436. const service = { getStaticProducts, getStartPlanPreview } as never;
  437. const products = await loadCodingPlanStaticProductListForTest(
  438. BUILTIN_MODEL_PROVIDER_IDS.zaiStartPlan,
  439. service,
  440. );
  441. expect(products.map((product) => product.productId)).toEqual(["remote-zai-lite-quarter"]);
  442. });
  443. it("removes Start SKUs from the Z.ai Coding Plan purchase list", async () => {
  444. const batchPreview = vi.fn(async () => ({
  445. productList: [
  446. {
  447. productId: "zai-start-free-monthly",
  448. productName: "Z.ai Start",
  449. payAmount: 0,
  450. priceCurrency: "USD",
  451. },
  452. {
  453. productId: "product-zai-pro-monthly",
  454. productName: "GLM Coding Pro",
  455. payAmount: 99,
  456. priceCurrency: "USD",
  457. },
  458. ],
  459. isSubscribed: false,
  460. isAuthenticated: true,
  461. }));
  462. const service = { batchPreview } as never;
  463. const snapshot = await loadCodingPlanProductsForTest(
  464. BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
  465. service,
  466. false,
  467. );
  468. expect(snapshot.productList.map((product) => product.productId)).toEqual([
  469. "product-zai-pro-monthly",
  470. ]);
  471. });
  472. });