| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508 |
- import { afterEach, describe, expect, it, vi } from "vitest";
- import { BUILTIN_MODEL_PROVIDER_IDS, CODING_PLAN_SYSTEM_BUSY } from "@zcode/shared";
- import { resolveCodingPlanUpgradeProductsProviderId } from "@/settings/model-provider-section/codingPlanPricingCards.js";
- import {
- formatCodingPlanAmount,
- normalizeCodingPlanCardCopyItems,
- } from "@/settings/model-provider-section/codingPlanProductPresentation.js";
- import {
- clearCodingPlanProductsCacheForTest,
- loadCodingPlanStaticProductsSnapshotForTest,
- loadCodingPlanStaticProductListForTest,
- loadCodingPlanProductsForTest,
- normalizeErrorMessage,
- resolveCodingPlanProductsFailureSnapshot,
- } from "@/settings/model-provider-section/useCodingPlanProducts.js";
- // 原生购买面板(CodingPlanPurchasePanel / CodingPlanProductCards)已下线,
- // 购买流程改为 webview 弹窗;本文件只保留仍针对活代码的用例。
- afterEach(() => {
- clearCodingPlanProductsCacheForTest();
- vi.restoreAllMocks();
- });
- describe("CodingPlanProductCards", () => {
- it("formats coding plan prices by locale and currency", () => {
- expect(formatCodingPlanAmount(48.6, "USD", "en-US")).toBe("US$48.60");
- expect(formatCodingPlanAmount(48.6, "USD", "zh-CN")).toBe("$48.60 美元");
- expect(formatCodingPlanAmount(132.3, "CNY", "en-US")).toBe("CN¥132.30");
- expect(formatCodingPlanAmount(132.3, "CNY", "zh-CN")).toBe("¥132.30 人民币");
- });
- it("maps unrenderable payment errors to system busy guidance", () => {
- const htmlError =
- '<!doctypehtml><html lang="zh-cn"><title>405</title><body>Sorry, your request has been blocked as it may cause potential threats.</body></html>';
- expect(normalizeErrorMessage(new Error(htmlError))).toBe(CODING_PLAN_SYSTEM_BUSY);
- expect(
- normalizeErrorMessage(new Error("Unexpected token '<', \"<html\" is not valid JSON")),
- ).toBe(CODING_PLAN_SYSTEM_BUSY);
- });
- it("keeps the last preview snapshot when refresh fails", () => {
- const previewSnapshot = {
- productList: [
- {
- productId: "product-pro-quarter",
- productName: "Pro",
- priceUnit: "quarter",
- payAmount: 100,
- soldOut: true,
- hasPreview: true,
- },
- ],
- isSubscribed: false,
- isAuthenticated: true,
- };
- const staticFallback = {
- productList: [
- {
- productId: "product-pro-quarter",
- productName: "Pro",
- priceUnit: "quarter",
- payAmount: 100,
- hasPreview: false,
- },
- ],
- isSubscribed: false,
- isAuthenticated: null,
- };
- expect(
- resolveCodingPlanProductsFailureSnapshot(previewSnapshot, staticFallback).productList[0]
- ?.soldOut,
- ).toBe(true);
- expect(
- resolveCodingPlanProductsFailureSnapshot(null, staticFallback).productList[0]?.hasPreview,
- ).toBe(false);
- });
- it("caches successful batch-preview snapshots briefly and lets force bypass the cache", async () => {
- const batchPreview = vi
- .fn()
- .mockResolvedValueOnce({
- productList: [
- {
- productId: "product-e68d08",
- productName: "GLM Coding Lite",
- payAmount: 48.6,
- priceCurrency: "USD",
- },
- ],
- isSubscribed: false,
- isAuthenticated: true,
- })
- .mockResolvedValueOnce({
- productList: [
- {
- productId: "product-e68d08",
- productName: "GLM Coding Lite",
- payAmount: 50,
- priceCurrency: "USD",
- },
- ],
- isSubscribed: false,
- isAuthenticated: true,
- });
- const service = { batchPreview } as never;
- const first = await loadCodingPlanProductsForTest(
- BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
- service,
- false,
- );
- const second = await loadCodingPlanProductsForTest(
- BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
- service,
- false,
- );
- const forced = await loadCodingPlanProductsForTest(
- BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
- service,
- true,
- );
- expect(batchPreview).toHaveBeenCalledTimes(2);
- expect(
- first.productList.find((product) => product.productId === "product-e68d08")?.payAmount,
- ).toBe(48.6);
- expect(
- second.productList.find((product) => product.productId === "product-e68d08")?.payAmount,
- ).toBe(48.6);
- expect(
- forced.productList.find((product) => product.productId === "product-e68d08")?.payAmount,
- ).toBe(50);
- });
- it("prevents stale in-flight previews from replacing a forced refresh cache", async () => {
- type PreviewSnapshot = {
- productList: Array<{
- productId: string;
- productName: string;
- payAmount: number;
- priceCurrency: string;
- }>;
- isSubscribed: boolean;
- isAuthenticated: boolean;
- };
- let resolveInitialPreview: ((snapshot: PreviewSnapshot) => void) | null = null;
- let resolveForcedPreview: ((snapshot: PreviewSnapshot) => void) | null = null;
- const initialPreview = new Promise<PreviewSnapshot>((resolve) => {
- resolveInitialPreview = resolve;
- });
- const forcedPreview = new Promise<PreviewSnapshot>((resolve) => {
- resolveForcedPreview = resolve;
- });
- const batchPreview = vi
- .fn()
- .mockReturnValueOnce(initialPreview)
- .mockReturnValueOnce(forcedPreview);
- const service = { batchPreview } as never;
- const initialLoad = loadCodingPlanProductsForTest(
- BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
- service,
- false,
- );
- const forcedLoad = loadCodingPlanProductsForTest(
- BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
- service,
- true,
- );
- resolveForcedPreview?.({
- productList: [
- {
- productId: "product-e68d08",
- productName: "GLM Coding Lite",
- payAmount: 50,
- priceCurrency: "USD",
- },
- ],
- isSubscribed: false,
- isAuthenticated: true,
- });
- const forced = await forcedLoad;
- resolveInitialPreview?.({
- productList: [
- {
- productId: "product-e68d08",
- productName: "GLM Coding Lite",
- payAmount: 10,
- priceCurrency: "USD",
- },
- ],
- isSubscribed: false,
- isAuthenticated: false,
- });
- await initialLoad;
- const cached = await loadCodingPlanProductsForTest(
- BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
- service,
- false,
- );
- expect(batchPreview).toHaveBeenCalledTimes(2);
- expect(
- forced.productList.find((product) => product.productId === "product-e68d08")?.payAmount,
- ).toBe(50);
- expect(
- cached.productList.find((product) => product.productId === "product-e68d08")?.payAmount,
- ).toBe(50);
- });
- it("loads remote static products once per day while viewing plans", async () => {
- const getStaticProducts = vi.fn(async () => ({
- [BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan]: [
- {
- productId: "remote-zai-lite-quarter",
- productName: "Remote Lite",
- description: "old remote zai lite description",
- productEquityList: [
- {
- productEquityTitle: "Remote quota",
- productEquityDetails: "Daily cached",
- },
- ],
- priceUnit: "quarter",
- displayOrder: 1,
- priceCurrency: "USD",
- payAmount: 42,
- },
- ],
- }));
- const service = { getStaticProducts } as never;
- const first = await loadCodingPlanStaticProductListForTest(
- BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
- service,
- );
- const second = await loadCodingPlanStaticProductListForTest(
- BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
- service,
- );
- expect(getStaticProducts).toHaveBeenCalledTimes(1);
- expect(first[0]?.productId).toBe("remote-zai-lite-quarter");
- expect(first[0]?.description).toBe("old remote zai lite description");
- expect(second[0]?.productId).toBe("remote-zai-lite-quarter");
- });
- it("shows static products without calling batch-preview when remote preview is disabled", async () => {
- const batchPreview = vi.fn();
- const getStaticProducts = vi.fn(async () => ({
- [BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan]: [
- {
- productId: "remote-bigmodel-lite-quarter",
- productName: "Remote BigModel Lite",
- description: ["旧远端 BigModel Lite 文案", "适配当前卡片布局"],
- productEquityList: [
- {
- productEquityTitle: "Remote quota",
- productEquityDetails: "Shown before login",
- },
- ],
- priceUnit: "quarter",
- displayOrder: 1,
- priceCurrency: "CNY",
- payAmount: 128,
- },
- ],
- }));
- const service = { batchPreview, getStaticProducts } as never;
- const snapshot = await loadCodingPlanStaticProductsSnapshotForTest(
- BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
- service,
- );
- expect(batchPreview).not.toHaveBeenCalled();
- expect(snapshot.productList).toHaveLength(1);
- expect(snapshot.productList[0]?.productId).toBe("remote-bigmodel-lite-quarter");
- expect(snapshot.productList[0]?.productDescription).toBe(
- "旧远端 BigModel Lite 文案\n适配当前卡片布局",
- );
- expect(snapshot.productList[0]?.hasPreview).toBe(false);
- });
- it("keeps remote Coding Plan static product descriptions", async () => {
- const getStaticProducts = vi.fn(async () => ({
- [BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan]: [
- {
- productId: "zai-lite",
- productName: "GLM Coding Lite",
- description: "old zai lite",
- productEquityList: [],
- priceUnit: "month",
- displayOrder: 1,
- priceCurrency: "USD",
- },
- {
- productId: "zai-pro",
- productName: "GLM Coding Pro",
- description: "old zai pro",
- productEquityList: [],
- priceUnit: "month",
- displayOrder: 2,
- priceCurrency: "USD",
- },
- {
- productId: "zai-max",
- productName: "GLM Coding Max",
- description: "old zai max",
- productEquityList: [],
- priceUnit: "month",
- displayOrder: 3,
- priceCurrency: "USD",
- },
- ],
- [BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan]: [
- {
- productId: "bigmodel-lite",
- productName: "GLM Coding Lite",
- description: "旧 BigModel Lite",
- productEquityList: [],
- priceUnit: "month",
- displayOrder: 1,
- priceCurrency: "CNY",
- },
- {
- productId: "bigmodel-pro",
- productName: "GLM Coding Pro",
- description: "旧 BigModel Pro",
- productEquityList: [],
- priceUnit: "month",
- displayOrder: 2,
- priceCurrency: "CNY",
- },
- {
- productId: "bigmodel-max",
- productName: "GLM Coding Max",
- description: "旧 BigModel Max",
- productEquityList: [],
- priceUnit: "month",
- displayOrder: 3,
- priceCurrency: "CNY",
- },
- ],
- }));
- const service = { getStaticProducts } as never;
- const zaiProducts = await loadCodingPlanStaticProductListForTest(
- BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
- service,
- );
- const bigmodelProducts = await loadCodingPlanStaticProductListForTest(
- BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
- service,
- );
- expect(zaiProducts.map((product) => product.description)).toEqual([
- "old zai lite",
- "old zai pro",
- "old zai max",
- ]);
- expect(bigmodelProducts.map((product) => product.description)).toEqual([
- "旧 BigModel Lite",
- "旧 BigModel Pro",
- "旧 BigModel Max",
- ]);
- });
- });
- describe("Z.ai Start products", () => {
- it("ignores malformed remote card copy items", () => {
- expect(
- normalizeCodingPlanCardCopyItems([
- null,
- 42,
- {},
- { tooltip: "missing text" },
- " valid ",
- { text: " remote ", tooltip: " detail " },
- ]),
- ).toEqual([{ text: "valid" }, { text: "remote", tooltip: "detail" }]);
- });
- it("uses the Z.ai Coding Plan product source when Start Plan upgrades", () => {
- expect(
- resolveCodingPlanUpgradeProductsProviderId(BUILTIN_MODEL_PROVIDER_IDS.zaiStartPlan),
- ).toBe(BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan);
- expect(
- resolveCodingPlanUpgradeProductsProviderId(
- BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
- ),
- ).toBe(BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan);
- expect(
- resolveCodingPlanUpgradeProductsProviderId(
- BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
- ),
- ).toBe(BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan);
- expect(
- resolveCodingPlanUpgradeProductsProviderId(BUILTIN_MODEL_PROVIDER_IDS.bigmodelStartPlan),
- ).toBe(BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan);
- });
- it("prepends the free Start SKUs only when startPlanPreview exists", async () => {
- const getStaticProducts = vi.fn(async () => ({
- [BUILTIN_MODEL_PROVIDER_IDS.zaiStartPlan]: [
- {
- productId: "remote-zai-lite-quarter",
- productName: "Remote Lite",
- productEquityList: [],
- priceUnit: "quarter",
- displayOrder: 1,
- priceCurrency: "USD",
- payAmount: 42,
- },
- ],
- }));
- const getStartPlanPreview = vi.fn(async () => ({
- planId: "zai-start-free",
- name: "Z.ai Start",
- entitlements: [
- {
- grantUnits: 1000,
- meter: "tokens",
- period: "day",
- showName: "Start free plan",
- unitType: "tokens",
- },
- ],
- }));
- const service = { getStaticProducts, getStartPlanPreview } as never;
- const products = await loadCodingPlanStaticProductListForTest(
- BUILTIN_MODEL_PROVIDER_IDS.zaiStartPlan,
- service,
- );
- expect(products.slice(0, 3).map((product) => product.productId)).toEqual([
- "zai-start-free-monthly",
- "zai-start-free-quarterly",
- "zai-start-free-yearly",
- ]);
- expect(products[3]?.productId).toBe("remote-zai-lite-quarter");
- expect(getStartPlanPreview).toHaveBeenCalledTimes(1);
- });
- it("does not show the free Start SKUs when startPlanPreview is missing", async () => {
- const getStaticProducts = vi.fn(async () => ({
- [BUILTIN_MODEL_PROVIDER_IDS.zaiStartPlan]: [
- {
- productId: "remote-zai-lite-quarter",
- productName: "Remote Lite",
- productEquityList: [],
- priceUnit: "quarter",
- displayOrder: 1,
- priceCurrency: "USD",
- payAmount: 42,
- },
- ],
- }));
- const getStartPlanPreview = vi.fn(async () => null);
- const service = { getStaticProducts, getStartPlanPreview } as never;
- const products = await loadCodingPlanStaticProductListForTest(
- BUILTIN_MODEL_PROVIDER_IDS.zaiStartPlan,
- service,
- );
- expect(products.map((product) => product.productId)).toEqual(["remote-zai-lite-quarter"]);
- });
- it("removes Start SKUs from the Z.ai Coding Plan purchase list", async () => {
- const batchPreview = vi.fn(async () => ({
- productList: [
- {
- productId: "zai-start-free-monthly",
- productName: "Z.ai Start",
- payAmount: 0,
- priceCurrency: "USD",
- },
- {
- productId: "product-zai-pro-monthly",
- productName: "GLM Coding Pro",
- payAmount: 99,
- priceCurrency: "USD",
- },
- ],
- isSubscribed: false,
- isAuthenticated: true,
- }));
- const service = { batchPreview } as never;
- const snapshot = await loadCodingPlanProductsForTest(
- BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
- service,
- false,
- );
- expect(snapshot.productList.map((product) => product.productId)).toEqual([
- "product-zai-pro-monthly",
- ]);
- });
- });
|