| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632 |
- import { afterEach, describe, expect, it, vi } from "vitest";
- import { BUILTIN_MODEL_PROVIDER_IDS, CODING_PLAN_SYSTEM_BUSY, type ApiClient } from "@zcode/shared";
- import {
- BigModelCodingPlanSubscriptionProvider,
- resolveCodingPlanClientConfigUrl,
- } from "../src/coding-plan-subscription/bigmodelCodingPlanSubscriptionProvider.js";
- import { ZaiCodingPlanSubscriptionProvider } from "../src/coding-plan-subscription/zaiCodingPlanSubscriptionProvider.js";
- import { createCodingPlanSubscriptionService } from "../src/coding-plan-subscription/codingPlanSubscriptionService.js";
- function createJsonResponse(body: unknown): Response {
- return new Response(JSON.stringify(body), {
- status: 200,
- headers: { "Content-Type": "application/json" },
- });
- }
- function createZaiBatchPreviewResponse(): Response {
- return createJsonResponse({
- code: 200,
- data: {
- productList: [
- {
- productId: "zai-product-pro",
- productName: "Z.ai Coding Pro",
- payAmount: 72,
- },
- ],
- isSubscribed: false,
- isAuthenticated: true,
- },
- });
- }
- function createHtmlErrorResponse(): Response {
- const body = [
- '<!doctypehtml><html lang="zh-cn"><title>405</title>',
- "<body>Sorry, your request has been blocked.</body></html>",
- ].join("");
- return new Response(body, {
- status: 405,
- headers: { "Content-Type": "text/html" },
- });
- }
- function createBigModelCredentialService(token = "oauth-token") {
- return {
- load: vi.fn(async (key: string) => (key === "oauth:bigmodel:access_token" ? token : null)),
- };
- }
- describe("BigModelCodingPlanSubscriptionProvider", () => {
- afterEach(() => {
- vi.useRealTimers();
- vi.unstubAllEnvs();
- });
- it("off-peak mock 直接使用 Built-in 模型,不等待远端 client config", async () => {
- vi.stubEnv("ZCODE_OFFPEAK_MOCK", "1");
- const apiClient: ApiClient = {
- request: vi.fn(async () => {
- throw new Error("mock mode must not request remote client config");
- }),
- };
- const provider = new BigModelCodingPlanSubscriptionProvider({
- apiClient,
- credentialService: { load: vi.fn() },
- resolveOffPeakModelSelectionView: async () =>
- ({
- revision: 1,
- providers: [
- {
- providerId: "account:zai-offpeak-idle-plan",
- config: { visibility: "hidden" },
- models: [{ modelId: "GLM-5.2", config: {} }],
- },
- ],
- }) as never,
- });
- await expect(provider.getOffPeakClientConfig({ forceRefresh: true })).resolves.toMatchObject({
- enabled: true,
- modelSelectionView: expect.objectContaining({
- providers: [
- expect.objectContaining({
- providerId: "account:zai-offpeak-idle-plan",
- }),
- ],
- }),
- codingPlanActive: true,
- });
- expect(apiClient.request).not.toHaveBeenCalled();
- });
- it("读取手动领取 preview 并归一化 plans", async () => {
- vi.stubEnv("ZCODE_ENDPOINT_ORIGIN", "https://preview.example.com");
- // Bugfix: 开发机 ambient ZCODE_BASE_URL 优先级高于 ZCODE_ENDPOINT_ORIGIN,置空避免 preview 域被生产域覆盖
- vi.stubEnv("ZCODE_BASE_URL", "");
- const apiClient: ApiClient = {
- request: vi.fn(async () =>
- createJsonResponse({
- code: 0,
- data: {
- server_time: 1_788_000_123,
- plans: [
- {
- plan_id: "weekend-plan",
- name: "Weekend Start Plan",
- description: "Weekend plan",
- priority: 100,
- entitlements: [
- {
- entitlement_id: "weekend-model",
- show_name: "GLM-5.1",
- meter: "model_usage",
- unit_type: "token",
- capabilities: ["model:GLM-5.1"],
- grant_units: 100_000,
- period: "daily",
- priority: 10,
- effective_at: 1_788_000_000,
- },
- ],
- },
- ],
- },
- }),
- ),
- };
- const provider = new BigModelCodingPlanSubscriptionProvider({
- apiClient,
- credentialService: {
- load: vi.fn(async (key: string) => (key === "zcodejwttoken" ? "zcode-jwt" : null)),
- },
- });
- await expect(provider.getManualClaimPlanPreviews()).resolves.toEqual({
- serverTime: 1_788_000_123_000,
- plans: [
- expect.objectContaining({
- planId: "weekend-plan",
- priority: 100,
- entitlements: [
- expect.objectContaining({
- entitlementId: "weekend-model",
- grantUnits: 100_000,
- effectiveAt: 1_788_000_000,
- }),
- ],
- }),
- ],
- });
- expect(apiClient.request).toHaveBeenCalledWith(
- expect.objectContaining({
- href: expect.stringContaining(
- "https://preview.example.com/api/v1/zcode-plan/billing/preview",
- ),
- }),
- expect.objectContaining({
- headers: { Authorization: "Bearer zcode-jwt" },
- method: "GET",
- }),
- );
- });
- it("未登录读取手动领取 preview 时不发送 Authorization", async () => {
- const apiClient: ApiClient = {
- request: vi.fn(async () =>
- createJsonResponse({
- code: 0,
- data: { server_time: "invalid", plans: [] },
- }),
- ),
- };
- const provider = new BigModelCodingPlanSubscriptionProvider({
- apiClient,
- credentialService: { load: vi.fn(async () => null) },
- });
- await expect(provider.getManualClaimPlanPreviews()).resolves.toEqual({
- plans: [],
- });
- expect(apiClient.request).toHaveBeenCalledWith(
- expect.any(URL),
- expect.objectContaining({ headers: undefined, method: "GET" }),
- );
- });
- it("保留手动领取失败 code 但忽略顶层 msg", async () => {
- const provider = new BigModelCodingPlanSubscriptionProvider({
- apiClient: {
- request: vi.fn(async () =>
- createJsonResponse({
- code: 1003,
- msg: "already claimed",
- data: null,
- }),
- ),
- },
- credentialService: { load: vi.fn(async () => "zcode-jwt") },
- });
- await expect(
- provider.claimManualPlan({
- planId: "weekend-plan",
- }),
- ).resolves.toEqual({ success: false, code: 1003, message: "" });
- });
- it("保留领取额度耗尽响应中的 plan 结束时间供 UI 生成日期提示", async () => {
- const provider = new BigModelCodingPlanSubscriptionProvider({
- apiClient: {
- request: vi.fn(async () =>
- createJsonResponse({
- code: 1005,
- msg: "quota exhausted",
- data: {
- server_time: 1_788_000_123,
- plan: { ends_at: 1_788_431_999 },
- },
- }),
- ),
- },
- credentialService: { load: vi.fn(async () => "zcode-jwt") },
- });
- await expect(
- provider.claimManualPlan({
- planId: "weekend-plan",
- }),
- ).resolves.toEqual({
- success: false,
- code: 1005,
- message: "",
- serverTime: 1_788_000_123_000,
- failureEndsAt: 1_788_431_999,
- });
- });
- it.each([1005, "1005"])("领取失败 %s 原样传递 data.message 并忽略 msg", async (code) => {
- const message = "今日额度已领完。\n<b>请下次再来</b>";
- const provider = new BigModelCodingPlanSubscriptionProvider({
- apiClient: {
- request: vi.fn(async () =>
- createJsonResponse({
- code,
- msg: "DO NOT SHOW",
- data: { message, server_time: 1788000123 },
- }),
- ),
- },
- credentialService: { load: vi.fn(async () => "zcode-jwt") },
- });
- await expect(
- provider.claimManualPlan({
- planId: "weekend",
- }),
- ).resolves.toMatchObject({
- success: false,
- code: 1005,
- message,
- serverTime: 1788000123000,
- });
- });
- it.each([undefined, null, 42, "", " "])(
- "领取失败不使用 msg 兜底:message=%s",
- async (message) => {
- const provider = new BigModelCodingPlanSubscriptionProvider({
- apiClient: {
- request: vi.fn(async () =>
- createJsonResponse({
- code: "1005",
- msg: "DO NOT SHOW",
- data: { message },
- }),
- ),
- },
- credentialService: { load: vi.fn(async () => "zcode-jwt") },
- });
- const result = await provider.claimManualPlan({
- planId: "weekend",
- });
- expect(result.message).toBe(typeof message === "string" ? message : "");
- },
- );
- it.each(["legacy", "preflight-v1", "unknown", undefined])(
- "预算固定 preflight-v1,不消费远端 strategy=%s 的缓存",
- async (strategy) => {
- const request = vi.fn(async () =>
- createJsonResponse({
- code: 0,
- data: { configs: { modelContextBudget: { strategy } } },
- }),
- );
- const provider = new BigModelCodingPlanSubscriptionProvider({
- apiClient: { request },
- credentialService: { load: vi.fn() },
- });
- // 由其他消费者填充真实共享缓存;预算不能再读出旧算法或发起刷新。
- await provider.getForceUpdateConfig();
- request.mockClear();
- await expect(provider.getModelContextBudgetStrategy()).resolves.toBe("preflight-v1");
- expect(request).not.toHaveBeenCalled();
- },
- );
- it("配置永不返回时预算立即完成,不发请求也不启动超时器", async () => {
- vi.useFakeTimers();
- const request = vi.fn(() => new Promise<Response>(() => {}));
- const provider = new BigModelCodingPlanSubscriptionProvider({
- apiClient: { request },
- credentialService: { load: vi.fn() },
- });
- let value: string | undefined;
- void provider.getModelContextBudgetStrategy().then((result) => {
- value = result;
- });
- await vi.advanceTimersByTimeAsync(0);
- expect(value).toBe("preflight-v1");
- expect(request).not.toHaveBeenCalled();
- expect(vi.getTimerCount()).toBe(0);
- });
- it("batchPreview 使用 BigModel OAuth token 且不添加 Bearer", async () => {
- const apiClient: ApiClient = {
- request: vi.fn(async () =>
- createJsonResponse({
- code: 200,
- data: {
- productList: [
- {
- productId: "product-pro",
- productName: "Coding Plan Pro",
- payAmount: 149,
- },
- ],
- isSubscribed: false,
- isAuthenticated: true,
- },
- }),
- ),
- };
- const provider = new BigModelCodingPlanSubscriptionProvider({
- apiClient,
- credentialService: {
- load: vi.fn(async (key) => (key === "oauth:bigmodel:access_token" ? "oauth-token" : null)),
- },
- });
- const result = await provider.batchPreview();
- expect(result.productList).toHaveLength(1);
- expect(apiClient.request).toHaveBeenCalledWith(
- "https://bigmodel.cn/api/biz/pay/batch-preview",
- expect.objectContaining({
- method: "POST",
- headers: {
- Authorization: "oauth-token",
- "Content-Type": "application/json",
- },
- }),
- );
- });
- it("ZCODE_ENV=test 时 BigModel Coding Plan API 使用 bigmodel.cn", async () => {
- vi.stubEnv("ZCODE_ENV", "test");
- const apiClient: ApiClient = {
- request: vi.fn(async () =>
- createJsonResponse({
- code: 200,
- data: {
- productList: [],
- isSubscribed: false,
- isAuthenticated: true,
- },
- }),
- ),
- };
- const provider = new BigModelCodingPlanSubscriptionProvider({
- apiClient,
- credentialService: createBigModelCredentialService(),
- });
- await provider.batchPreview();
- expect(apiClient.request).toHaveBeenCalledWith(
- "https://bigmodel.cn/api/biz/pay/batch-preview",
- expect.objectContaining({
- method: "POST",
- }),
- );
- });
- it("缺少 BigModel OAuth token 时不回退其他 key", async () => {
- const apiClient: ApiClient = {
- request: vi.fn(),
- };
- const provider = new BigModelCodingPlanSubscriptionProvider({
- apiClient,
- credentialService: {
- load: vi.fn(async () => null),
- },
- });
- await expect(provider.batchPreview()).rejects.toThrow("bigmodel_oauth_required");
- expect(apiClient.request).not.toHaveBeenCalled();
- });
- it("BigModel OAuth token 被旧版 zcode JWT 污染时不请求套餐接口", async () => {
- const apiClient: ApiClient = {
- request: vi.fn(),
- };
- const provider = new BigModelCodingPlanSubscriptionProvider({
- apiClient,
- credentialService: {
- load: vi.fn(async (key) =>
- key === "oauth:bigmodel:access_token" || key === "zcodejwttoken"
- ? "stale-zcode-jwt"
- : null,
- ),
- },
- });
- await expect(provider.batchPreview()).rejects.toThrow("bigmodel_oauth_required");
- expect(apiClient.request).not.toHaveBeenCalled();
- });
- it("支付接口返回 HTML/WAF 页面时统一返回系统繁忙错误", async () => {
- const apiClient: ApiClient = {
- request: vi.fn(async () => createHtmlErrorResponse()),
- };
- const provider = new BigModelCodingPlanSubscriptionProvider({
- apiClient,
- credentialService: createBigModelCredentialService(),
- });
- await expect(
- provider.preview({
- providerId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
- productId: "product-pro",
- }),
- ).rejects.toThrow(CODING_PLAN_SYSTEM_BUSY);
- });
- it("Z.ai batchPreview 直接使用持久化的业务 token", async () => {
- const apiClient: ApiClient = {
- request: vi.fn(async () => createZaiBatchPreviewResponse()),
- };
- const provider = new BigModelCodingPlanSubscriptionProvider({
- apiClient,
- credentialService: {
- load: vi.fn(async (key) => (key === "oauth:zai:access_token" ? "zai-business-jwt" : null)),
- },
- });
- const result = await provider.batchPreview({
- providerId: BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
- });
- expect(result.productList).toHaveLength(1);
- expect(apiClient.request).toHaveBeenCalledTimes(1);
- expect(apiClient.request).toHaveBeenCalledWith(
- "https://api.z.ai/api/biz/pay/batch-preview",
- expect.objectContaining({
- method: "POST",
- headers: {
- Authorization: "zai-business-jwt",
- "Content-Type": "application/json",
- },
- }),
- );
- });
- it("ZCODE_ENV=test 时 Z.ai Coding Plan 业务接口使用 api.z.ai", async () => {
- vi.stubEnv("ZCODE_ENV", "test");
- // 修复原因:开发机若设了 ZAI_BUSINESS_BASE_URL=https://api.z.ai,
- // resolveZaiBusinessBaseUrl 会优先读它,覆盖 ZCODE_ENV fallback,导致断言不稳定。
- vi.stubEnv("ZAI_BUSINESS_BASE_URL", "");
- const apiClient: ApiClient = {
- request: vi.fn(async (input) => {
- const url = String(input);
- if (url.includes("/product/info")) {
- return createJsonResponse({
- code: 200,
- data: { productId: "zai-product-pro" },
- });
- }
- if (url.includes("/pay/check") && !url.includes("check-pending-orders")) {
- return createJsonResponse({ code: 200, data: "SUCCESS" });
- }
- if (url.includes("check-pending-orders") || url.includes("stripe/query")) {
- return createJsonResponse({ code: 200, data: [] });
- }
- return createJsonResponse({
- code: 200,
- data: {
- productList: [],
- productId: "zai-product-pro",
- bizId: "zai-biz-1",
- soldOut: false,
- payAmount: 18,
- orderId: "order-1",
- status: "SUCCEEDED",
- resultStatus: "SUCCESS",
- setupTokenId: "setup-token-1",
- },
- });
- }),
- };
- const provider = new BigModelCodingPlanSubscriptionProvider({
- apiClient,
- credentialService: {
- load: vi.fn(async (key) => (key === "oauth:zai:access_token" ? "zai-business-jwt" : null)),
- },
- });
- await provider.batchPreview({
- providerId: BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
- });
- await provider.productInfo({
- providerId: BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
- productId: "zai-product-pro",
- });
- await provider.preview({
- providerId: BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
- productId: "zai-product-pro",
- });
- await provider.checkPayment({
- providerId: BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
- bizId: "zai-biz-1",
- });
- await provider.checkPendingOrders({
- providerId: BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
- });
- await provider.queryStripeCards({
- providerId: BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
- });
- await provider.payStripe({
- providerId: BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
- productId: "zai-product-pro",
- paymentMethodId: "pm_1",
- returnUrl: "https://chat.z.ai",
- estimatePayAmount: { thirdPartyPayAmount: 18 },
- bizId: "zai-biz-1",
- });
- await provider.createPaypalSetupToken({
- providerId: BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
- returnUrl: "https://chat.z.ai/return",
- cancelUrl: "https://chat.z.ai/cancel",
- });
- expect(vi.mocked(apiClient.request).mock.calls.map((call) => String(call[0]))).toEqual([
- "https://api.z.ai/api/biz/pay/batch-preview",
- "https://api.z.ai/api/biz/product/info?productId=zai-product-pro",
- "https://api.z.ai/api/biz/pay/zai-preview",
- "https://api.z.ai/api/biz/pay/check?bizId=zai-biz-1",
- "https://api.z.ai/api/biz/pay/check-pending-orders",
- "https://api.z.ai/api/pay/stripe/query",
- "https://api.z.ai/api/pay/stripe/pay",
- "https://api.z.ai/api/pay/paypal/setupToken",
- ]);
- });
- it("Z.ai 缺少登录态 token 时返回未登录错误", async () => {
- const apiClient: ApiClient = {
- request: vi.fn(),
- };
- const provider = new BigModelCodingPlanSubscriptionProvider({
- apiClient,
- credentialService: {
- load: vi.fn(async () => null),
- },
- });
- await expect(
- provider.batchPreview({
- providerId: BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
- }),
- ).rejects.toThrow("zai_oauth_required");
- expect(apiClient.request).not.toHaveBeenCalled();
- });
- describe.each(["bigmodel", "zai"] as const)("%s 静态套餐配置 ID 兼容", (family) => {
- it.each(["legacy", "current", "both", "empty", "missing"] as const)(
- "%s 同时覆盖个人与团队目录",
- async (scenario) => {
- const currentId =
- family === "bigmodel"
- ? BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan
- : BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan;
- const legacyId = `builtin:${family}-coding-plan`;
- const product = (productId: string) => ({
- productId,
- productName: productId,
- tier: "LITE",
- subscribeMode: "CONTINUOUS",
- subscribePeriod: "MONTHLY",
- purchaseMethodName: "月付",
- priceCurrency: "CNY",
- payAmount: 123,
- });
- const config = {
- ...(scenario === "legacy" || scenario === "both"
- ? { [legacyId]: [product("legacy")] }
- : {}),
- ...(scenario === "empty" ? { [legacyId]: [] } : {}),
- ...(scenario === "current" || scenario === "both" || scenario === "empty"
- ? { [currentId]: [product("current")] }
- : {}),
- };
- const load = vi.fn();
- const provider = new BigModelCodingPlanSubscriptionProvider({
- apiClient: {
- request: vi.fn(async () =>
- createJsonResponse({
- code: 0,
- data: {
- configs: {
- codingPlanStaticProducts: config,
- codingPlanStaticTeamProducts: config,
- },
- },
- }),
- ),
- },
- credentialService: { load },
- });
- for (const result of [
- await provider.getStaticProducts(),
- await provider.getStaticTeamProducts(),
- ]) {
- expect(result[currentId]?.map((item) => item.productId)).toEqual(
- scenario === "missing"
- ? undefined
- : scenario === "empty"
- ? []
- : [scenario === "current" ? "current" : "legacy"],
- );
- expect(Object.hasOwn(result, legacyId)).toBe(false);
- }
- expect(load).not.toHaveBeenCalled();
- },
- );
- });
- it("getStaticProducts 从 ZCode client config 读取套餐配置且不需要 OAuth", async () => {
- const apiClient: ApiClient = {
- request: vi.fn(async () =>
- createJsonResponse({
- code: 0,
- msg: "",
- data: {
- configs: {
- codingPlanStaticProducts: {
- [BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan]: [
- {
- productId: "remote-zai-lite",
- productName: "Remote Lite",
- description: ["Base usage included", "Small repo iteration"],
- priceUnit: "month",
- displayOrder: 1,
- priceCurrency: "USD",
- payAmount: 18,
- },
- ],
- },
- },
- },
- }),
- ),
- };
- const provider = new BigModelCodingPlanSubscriptionProvider({
- apiClient,
- credentialService: {
- load: vi.fn(),
- },
- });
- const result = await provider.getStaticProducts();
- expect(result[BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan]?.[0]?.productId).toBe(
- "remote-zai-lite",
- );
- expect(result[BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan]?.[0]?.description).toEqual([
- "Base usage included",
- "Small repo iteration",
- ]);
- const requestUrl = String(vi.mocked(apiClient.request).mock.calls[0]?.[0]);
- expect(requestUrl).toContain("https://zcode.z.ai/api/v1/client/configs?");
- expect(requestUrl).toContain("app_version=");
- expect(requestUrl).toContain(`platform=${process.platform}-${process.arch}`);
- expect(apiClient.request).toHaveBeenCalledWith(expect.any(URL), expect.anything());
- expect(vi.mocked(apiClient.request).mock.calls[0]?.[1]).toEqual(
- expect.objectContaining({
- method: "GET",
- }),
- );
- });
- it("getStaticTeamProducts 按 provider 读取团队套餐静态配置", async () => {
- const apiClient: ApiClient = {
- request: vi.fn(async () =>
- createJsonResponse({
- code: 0,
- data: {
- configs: {
- codingPlanStaticTeamProducts: {
- [BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan]: [
- {
- productId: "team-lite-monthly-continuous",
- productName: "标准版",
- tier: "LITE",
- subscribeMode: "CONTINUOUS",
- subscribePeriod: "MONTHLY",
- purchaseMethodName: "连续包月",
- priceCurrency: "CNY",
- payAmount: 199,
- equity: [{ text: "5小时最多:0.6亿 tokens" }],
- description: [{ text: "团队成员席位统一管理" }],
- },
- ],
- },
- },
- },
- }),
- ),
- };
- const provider = new BigModelCodingPlanSubscriptionProvider({
- apiClient,
- credentialService: { load: vi.fn() },
- });
- const result = await provider.getStaticTeamProducts();
- expect(result[BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan]?.[0]).toMatchObject({
- productName: "标准版",
- equity: [{ text: "5小时最多:0.6亿 tokens" }],
- description: [{ text: "团队成员席位统一管理" }],
- });
- });
- it("getStaticTeamProducts 拒绝非数组 provider 配置", async () => {
- const apiClient: ApiClient = {
- request: vi.fn(async () =>
- createJsonResponse({
- code: 0,
- data: {
- configs: {
- codingPlanStaticTeamProducts: {
- [BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan]: {},
- },
- },
- },
- }),
- ),
- };
- const provider = new BigModelCodingPlanSubscriptionProvider({
- apiClient,
- credentialService: { load: vi.fn() },
- });
- await expect(provider.getStaticTeamProducts()).rejects.toThrow(
- "invalid Coding Plan team products",
- );
- });
- it("getStaticTeamProducts 拒绝字段或枚举无效的商品", async () => {
- const apiClient: ApiClient = {
- request: vi.fn(async () =>
- createJsonResponse({
- code: 0,
- data: {
- configs: {
- codingPlanStaticTeamProducts: {
- [BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan]: [
- {
- productId: "",
- productName: "标准版",
- tier: "UNKNOWN",
- subscribeMode: "CONTINUOUS",
- subscribePeriod: "MONTHLY",
- purchaseMethodName: "连续包月",
- priceCurrency: "CNY",
- },
- ],
- },
- },
- },
- }),
- ),
- };
- const provider = new BigModelCodingPlanSubscriptionProvider({
- apiClient,
- credentialService: { load: vi.fn() },
- });
- await expect(provider.getStaticTeamProducts()).rejects.toThrow(
- "invalid Coding Plan team products",
- );
- });
- it("getStaticTeamProducts 拒绝无效的团队文案条目", async () => {
- const apiClient: ApiClient = {
- request: vi.fn(async () =>
- createJsonResponse({
- code: 0,
- data: {
- configs: {
- codingPlanStaticTeamProducts: {
- [BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan]: [
- {
- productId: "team-lite-monthly-continuous",
- productName: "标准版",
- tier: "LITE",
- subscribeMode: "CONTINUOUS",
- subscribePeriod: "MONTHLY",
- purchaseMethodName: "连续包月",
- priceCurrency: "CNY",
- equity: [null],
- },
- ],
- },
- },
- },
- }),
- ),
- };
- const provider = new BigModelCodingPlanSubscriptionProvider({
- apiClient,
- credentialService: { load: vi.fn() },
- });
- await expect(provider.getStaticTeamProducts()).rejects.toThrow(
- "invalid Coding Plan team products",
- );
- });
- it("client configs 根据 ZCODE_ENV 选择测试或生产环境", () => {
- expect(
- resolveCodingPlanClientConfigUrl({
- ZCODE_ENV: "test",
- }).toString(),
- ).toBe("https://zcode.z.ai/api/v1/client/configs");
- expect(
- resolveCodingPlanClientConfigUrl({
- ZCODE_ENV: "production",
- ZCODE_PRODUCTION_BASE_URL: "https://runtime.example.com",
- }).toString(),
- ).toBe("https://runtime.example.com/api/v1/client/configs");
- });
- it("getForceUpdateConfig 从 ZCode client config 读取强制升级配置", async () => {
- const apiClient: ApiClient = {
- request: vi.fn(async () =>
- createJsonResponse({
- code: 0,
- msg: "",
- data: {
- configs: {
- forceUpdate: {
- minimalVersion: " 4.0.0 ",
- },
- },
- },
- }),
- ),
- };
- const provider = new BigModelCodingPlanSubscriptionProvider({
- apiClient,
- credentialService: {
- load: vi.fn(),
- },
- });
- await expect(provider.getForceUpdateConfig()).resolves.toEqual({
- minimalVersion: "4.0.0",
- });
- expect(apiClient.request).toHaveBeenCalledWith(
- expect.any(URL),
- expect.objectContaining({
- method: "GET",
- }),
- );
- });
- it("getForceUpdateConfig 在字段缺失时返回 null", async () => {
- const apiClient: ApiClient = {
- request: vi.fn(async () =>
- createJsonResponse({
- code: 0,
- msg: "",
- data: {
- configs: {},
- },
- }),
- ),
- };
- const provider = new BigModelCodingPlanSubscriptionProvider({
- apiClient,
- credentialService: {
- load: vi.fn(),
- },
- });
- await expect(provider.getForceUpdateConfig()).resolves.toBeNull();
- });
- it("getForceUpdateConfig 在 minimalVersion 类型不合法时返回 null", async () => {
- const apiClient: ApiClient = {
- request: vi.fn(async () =>
- createJsonResponse({
- code: 0,
- msg: "",
- data: {
- configs: {
- forceUpdate: {
- minimalVersion: 4,
- },
- },
- },
- }),
- ),
- };
- const provider = new BigModelCodingPlanSubscriptionProvider({
- apiClient,
- credentialService: {
- load: vi.fn(),
- },
- });
- await expect(provider.getForceUpdateConfig()).resolves.toBeNull();
- });
- it("getStartPlanPreview 从 ZCode client config 读取未登录 Start Plan 预览", async () => {
- const apiClient: ApiClient = {
- request: vi.fn(async () =>
- createJsonResponse({
- code: 0,
- msg: "",
- data: {
- configs: {
- startPlanPreview: {
- planId: "zcode-v3-start-plan",
- name: "ZCode V3 Start Plan",
- entitlements: [
- {
- grantUnits: 10_000_000,
- meter: "model_usage",
- period: "daily",
- showName: "GLM-5.2",
- unitType: "token",
- },
- ],
- },
- },
- },
- }),
- ),
- };
- const provider = new BigModelCodingPlanSubscriptionProvider({
- apiClient,
- credentialService: {
- load: vi.fn(),
- },
- });
- const result = await provider.getStartPlanPreview();
- expect(result).toEqual({
- planId: "zcode-v3-start-plan",
- name: "ZCode V3 Start Plan",
- entitlements: [
- {
- grantUnits: 10_000_000,
- meter: "model_usage",
- period: "daily",
- showName: "GLM-5.2",
- unitType: "token",
- },
- ],
- });
- expect(apiClient.request).toHaveBeenCalledWith(
- expect.any(URL),
- expect.objectContaining({
- method: "GET",
- }),
- );
- });
- it("getBillingDiscount 仅在字段存在时返回活动配置", async () => {
- const apiClient: ApiClient = {
- request: vi.fn(async () =>
- createJsonResponse({
- code: 0,
- msg: "",
- data: {
- configs: {
- codingPlanBillingDiscount: {
- "zh-CN": {
- cardTitle: "限时 150% 配额活动",
- cardBody: "升级 Coding Plan,可在活动期内获得150% 配额",
- badgeBody: "150% 配额",
- },
- "en-US": {
- cardTitle: "Limited-time 150% Quota Campaign",
- cardBody: "Upgrade your Coding Plan to get 150% quota during the campaign period",
- badgeBody: "150% Quota",
- },
- },
- },
- },
- }),
- ),
- };
- const provider = new BigModelCodingPlanSubscriptionProvider({
- apiClient,
- credentialService: {
- load: vi.fn(),
- },
- });
- await expect(provider.getBillingDiscount()).resolves.toEqual({
- "zh-CN": {
- cardTitle: "限时 150% 配额活动",
- cardBody: "升级 Coding Plan,可在活动期内获得150% 配额",
- badgeBody: "150% 配额",
- },
- "en-US": {
- cardTitle: "Limited-time 150% Quota Campaign",
- cardBody: "Upgrade your Coding Plan to get 150% quota during the campaign period",
- badgeBody: "150% Quota",
- },
- });
- });
- it("getBillingDiscount 在字段缺失时返回 undefined", async () => {
- const apiClient: ApiClient = {
- request: vi.fn(async () =>
- createJsonResponse({
- code: 0,
- msg: "",
- data: {
- configs: {},
- },
- }),
- ),
- };
- const provider = new BigModelCodingPlanSubscriptionProvider({
- apiClient,
- credentialService: {
- load: vi.fn(),
- },
- });
- await expect(provider.getBillingDiscount()).resolves.toBeUndefined();
- });
- it("getStartPlanPreview、getStaticProducts 和 getBillingDiscount 共享同一次 client config 请求", async () => {
- let resolveRequest: ((response: Response) => void) | undefined;
- const apiClient: ApiClient = {
- request: vi.fn(
- () =>
- new Promise<Response>((resolve) => {
- resolveRequest = resolve;
- }),
- ),
- };
- const provider = new BigModelCodingPlanSubscriptionProvider({
- apiClient,
- credentialService: {
- load: vi.fn(),
- },
- });
- const previewPromise = provider.getStartPlanPreview();
- const productsPromise = provider.getStaticProducts();
- const discountPromise = provider.getBillingDiscount();
- resolveRequest?.(
- createJsonResponse({
- code: 0,
- msg: "",
- data: {
- configs: {
- startPlanPreview: {
- planId: "zcode-v3-start-plan",
- name: "ZCode V3 Start Plan",
- entitlements: [
- {
- grantUnits: 10_000_000,
- meter: "model_usage",
- period: "daily",
- showName: "GLM-5.2",
- unitType: "token",
- },
- ],
- },
- codingPlanStaticProducts: {
- [BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan]: [
- {
- productId: "remote-bigmodel-lite",
- productName: "Remote BigModel Lite",
- productEquityList: [],
- priceUnit: "month",
- displayOrder: 1,
- priceCurrency: "CNY",
- payAmount: 49,
- },
- ],
- },
- codingPlanBillingDiscount: {
- "zh-CN": {
- cardTitle: "限时 150% 配额活动",
- cardBody: "升级 Coding Plan,可在活动期内获得150% 配额",
- badgeBody: "150% 配额",
- },
- },
- },
- },
- }),
- );
- const [preview, products, discount] = await Promise.all([
- previewPromise,
- productsPromise,
- discountPromise,
- ]);
- expect(apiClient.request).toHaveBeenCalledTimes(1);
- expect(preview?.planId).toBe("zcode-v3-start-plan");
- expect(products[BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan]?.[0]?.productId).toBe(
- "remote-bigmodel-lite",
- );
- expect(discount).toEqual({
- "zh-CN": {
- cardTitle: "限时 150% 配额活动",
- cardBody: "升级 Coding Plan,可在活动期内获得150% 配额",
- badgeBody: "150% 配额",
- },
- });
- await provider.getStartPlanPreview();
- await provider.getStaticProducts();
- await provider.getBillingDiscount();
- expect(apiClient.request).toHaveBeenCalledTimes(1);
- });
- it("client configs 缓存 1 小时后重新请求", async () => {
- vi.useFakeTimers();
- vi.setSystemTime(new Date("2026-06-12T00:00:00.000Z"));
- const apiClient: ApiClient = {
- request: vi
- .fn()
- .mockResolvedValueOnce(
- createJsonResponse({
- code: 0,
- msg: "",
- data: {
- configs: {
- codingPlanBillingDiscount: {
- "zh-CN": {
- badgeBody: "第一版",
- },
- },
- },
- },
- }),
- )
- .mockResolvedValueOnce(
- createJsonResponse({
- code: 0,
- msg: "",
- data: {
- configs: {
- codingPlanBillingDiscount: {
- "zh-CN": {
- badgeBody: "第二版",
- },
- },
- },
- },
- }),
- ),
- };
- const provider = new BigModelCodingPlanSubscriptionProvider({
- apiClient,
- credentialService: {
- load: vi.fn(),
- },
- });
- await expect(provider.getBillingDiscount()).resolves.toEqual({
- "zh-CN": { badgeBody: "第一版" },
- });
- vi.setSystemTime(new Date("2026-06-12T00:59:59.000Z"));
- await expect(provider.getBillingDiscount()).resolves.toEqual({
- "zh-CN": { badgeBody: "第一版" },
- });
- expect(apiClient.request).toHaveBeenCalledTimes(1);
- vi.setSystemTime(new Date("2026-06-12T01:00:01.000Z"));
- await expect(provider.getBillingDiscount()).resolves.toEqual({
- "zh-CN": { badgeBody: "第二版" },
- });
- expect(apiClient.request).toHaveBeenCalledTimes(2);
- });
- it("getEnterprisePricing 未登录时走公开定价接口且不读取 OAuth", async () => {
- const apiClient: ApiClient = {
- request: vi.fn(async () =>
- createJsonResponse({
- code: 200,
- data: {
- productList: [
- {
- productId: "enterprise-lite-monthly",
- tier: "LITE",
- subscribeMode: "ONE_TIME",
- subscribePeriod: "MONTHLY",
- purchaseMethodName: "按月采购",
- originalAmount: 270,
- discountAmount: 27,
- payAmount: 243,
- renewAmount: 243,
- },
- ],
- },
- }),
- ),
- };
- const credentialService = { load: vi.fn() };
- const provider = new BigModelCodingPlanSubscriptionProvider({
- apiClient,
- credentialService,
- });
- const result = await provider.getEnterprisePricing({
- authenticated: false,
- });
- expect(result.productList[0]?.productId).toBe("enterprise-lite-monthly");
- expect(credentialService.load).not.toHaveBeenCalled();
- expect(apiClient.request).toHaveBeenCalledWith(
- "https://bigmodel.cn/api/biz/campaign/partner/enterprise/pricing",
- expect.objectContaining({
- method: "GET",
- }),
- );
- });
- it("getEnterprisePricing 登录态使用 BigModel OAuth token 且不添加 Bearer", async () => {
- const apiClient: ApiClient = {
- request: vi.fn(async () =>
- createJsonResponse({
- code: 200,
- data: { productList: [] },
- }),
- ),
- };
- const provider = new BigModelCodingPlanSubscriptionProvider({
- apiClient,
- credentialService: createBigModelCredentialService("oauth-token"),
- });
- await provider.getEnterprisePricing({ authenticated: true });
- expect(apiClient.request).toHaveBeenCalledTimes(1);
- expect(String(vi.mocked(apiClient.request).mock.calls[0]?.[0])).toBe(
- "https://bigmodel.cn/api/biz/subscription/enterprise/v2/pricing",
- );
- expect(vi.mocked(apiClient.request).mock.calls[0]?.[1]).toMatchObject({
- method: "GET",
- headers: {
- Authorization: "oauth-token",
- "Content-Type": "application/json",
- },
- });
- });
- it("getEnterprisePricing 会用 customerInfo 给已购 Team Plan 补齐项目上下文", async () => {
- const apiClient: ApiClient = {
- request: vi
- .fn()
- .mockResolvedValueOnce(
- createJsonResponse({
- code: 200,
- data: {
- productList: [
- {
- productId: "product-0e2085",
- tier: "PRO",
- subscribeMode: "CONTINUOUS",
- subscribePeriod: "MONTHLY",
- subscribed: true,
- },
- ],
- },
- }),
- )
- .mockResolvedValueOnce(
- createJsonResponse({
- code: 200,
- data: {
- organizations: [
- {
- organizationId: "org-team",
- organizationName: "默认机构",
- projects: [
- {
- projectId: "proj-default",
- projectName: "团队编程套餐项目",
- projectType: 1,
- },
- {
- projectId: "proj-team",
- projectName: "自定义团队项目",
- projectType: 2,
- },
- ],
- },
- ],
- },
- }),
- ),
- };
- const provider = new BigModelCodingPlanSubscriptionProvider({
- apiClient,
- credentialService: createBigModelCredentialService("oauth-token"),
- });
- const result = await provider.getEnterprisePricing({ authenticated: true });
- expect(result.productList[0]).toMatchObject({
- organizationId: "org-team",
- projectId: "proj-team",
- projectName: "自定义团队项目",
- teamProjects: [
- {
- organizationId: "org-team",
- projectId: "proj-team",
- projectName: "自定义团队项目",
- },
- ],
- });
- });
- it("getEnterprisePricing 已购 Team Plan 只有机构 ID 时仍从机构数组补齐项目上下文", async () => {
- const apiClient: ApiClient = {
- request: vi
- .fn()
- .mockResolvedValueOnce(
- createJsonResponse({
- code: 200,
- data: {
- productList: [
- {
- productId: "product-0e2085",
- tier: "PRO",
- subscribeMode: "CONTINUOUS",
- subscribePeriod: "MONTHLY",
- subscribed: true,
- organizationId: "org-target",
- },
- ],
- },
- }),
- )
- .mockResolvedValueOnce(
- createJsonResponse({
- code: 200,
- data: {
- organizations: [
- {
- organizationId: "org-other",
- organizationName: "其他机构",
- projects: [
- {
- projectId: "proj-other",
- projectName: "其他团队项目",
- projectType: 2,
- },
- ],
- },
- {
- organizationId: "org-target",
- organizationName: "目标机构",
- projects: [
- {
- projectId: "proj-default",
- projectName: "默认项目",
- projectType: 1,
- },
- {
- projectId: "proj-team",
- projectName: "目标团队项目",
- projectType: 2,
- },
- ],
- },
- ],
- },
- }),
- ),
- };
- const provider = new BigModelCodingPlanSubscriptionProvider({
- apiClient,
- credentialService: createBigModelCredentialService("oauth-token"),
- });
- const result = await provider.getEnterprisePricing({ authenticated: true });
- expect(result.productList[0]).toMatchObject({
- organizationId: "org-target",
- organizationName: "目标机构",
- projectId: "proj-team",
- projectName: "目标团队项目",
- teamProjects: [
- {
- organizationId: "org-target",
- projectId: "proj-team",
- projectName: "目标团队项目",
- },
- ],
- });
- });
- it("getEnterprisePricing pricing 未标记订阅但 customerInfo 有团队项目时补 Team Plan 入口", async () => {
- const apiClient: ApiClient = {
- request: vi
- .fn()
- .mockResolvedValueOnce(
- createJsonResponse({
- code: 200,
- data: {
- productList: [
- {
- productId: "product-max",
- tier: "MAX",
- subscribeMode: "CONTINUOUS",
- subscribePeriod: "MONTHLY",
- subscribed: false,
- },
- {
- productId: "product-pro-monthly",
- tier: "PRO",
- subscribeMode: "ONE_TIME",
- subscribePeriod: "MONTHLY",
- subscribed: false,
- },
- ],
- },
- }),
- )
- .mockResolvedValueOnce(
- createJsonResponse({
- code: 200,
- data: {
- organizations: [
- {
- organizationId: "org-default",
- organizationName: "默认机构",
- projects: [
- {
- projectId: "proj-default",
- projectName: "默认项目",
- projectType: 1,
- },
- ],
- },
- {
- organizationId: "org-team",
- organizationName: "团队机构",
- projects: [
- {
- projectId: "proj-team",
- projectName: "团队编程套餐项目",
- projectType: 2,
- },
- ],
- },
- ],
- },
- }),
- ),
- };
- const provider = new BigModelCodingPlanSubscriptionProvider({
- apiClient,
- credentialService: createBigModelCredentialService("oauth-token"),
- });
- const result = await provider.getEnterprisePricing({ authenticated: true });
- expect(result.productList[1]).toMatchObject({
- productId: "product-pro-monthly",
- subscribed: true,
- organizationId: "org-team",
- projectId: "proj-team",
- projectName: "团队编程套餐项目",
- });
- });
- it("getEnterprisePricing 会为多个已购 Team Plan 项目预热 zcode-team-api-key", async () => {
- const requestUrls: string[] = [];
- const requestMethods: Array<string | undefined> = [];
- const apiClient: ApiClient = {
- request: vi.fn(async (input, init) => {
- const url = input.toString();
- requestUrls.push(url);
- requestMethods.push(init?.method);
- if (url.endsWith("/api/biz/subscription/enterprise/v2/pricing")) {
- return createJsonResponse({
- code: 200,
- data: {
- productList: [
- {
- productId: "product-pro-monthly",
- tier: "PRO",
- subscribeMode: "ONE_TIME",
- subscribePeriod: "MONTHLY",
- subscribed: true,
- },
- ],
- },
- });
- }
- if (url.endsWith("/api/biz/customer/getCustomerInfo")) {
- return createJsonResponse({
- code: 200,
- data: {
- organizations: [
- {
- organizationId: "org-a",
- organizationName: "团队 A",
- projects: [
- {
- projectId: "proj-a",
- projectName: "项目 A",
- projectType: 2,
- },
- ],
- },
- {
- organizationId: "org-b",
- organizationName: "团队 B",
- projects: [
- {
- projectId: "proj-b",
- projectName: "项目 B",
- projectType: 2,
- },
- ],
- },
- ],
- },
- });
- }
- if (url.endsWith("/organization/org-a/projects/proj-a/api_keys")) {
- if (init?.method === "GET") {
- return createJsonResponse({
- code: 200,
- data: [{ apiKey: "team-a-ak", keyType: 2, name: "zcode-team-api-key" }],
- });
- }
- }
- if (url.endsWith("/organization/org-b/projects/proj-b/api_keys")) {
- if (init?.method === "GET") {
- return createJsonResponse({ code: 200, data: [] });
- }
- if (init?.method === "POST") {
- expect(init.headers).toMatchObject({
- "bigmodel-organization": "org-b",
- "bigmodel-project": "proj-b",
- });
- expect(JSON.parse(String(init.body))).toEqual({
- name: "zcode-team-api-key",
- keyType: 2,
- usageScene: 1,
- });
- return createJsonResponse({
- code: 200,
- data: {
- apiKey: "team-b-ak",
- keyType: 2,
- name: "zcode-team-api-key",
- },
- });
- }
- }
- throw new Error(`unexpected request: ${url}`);
- }),
- };
- const provider = new BigModelCodingPlanSubscriptionProvider({
- apiClient,
- credentialService: createBigModelCredentialService("oauth-token"),
- });
- const result = await provider.getEnterprisePricing({ authenticated: true });
- expect(result.productList[0]?.teamProjects).toHaveLength(2);
- expect(result.productList[0]?.teamProjects).toMatchObject([
- {
- organizationId: "org-a",
- projectId: "proj-a",
- apiKeyStatus: "available",
- },
- {
- organizationId: "org-b",
- projectId: "proj-b",
- apiKeyStatus: "available",
- },
- ]);
- expect(requestUrls).toEqual([
- "https://bigmodel.cn/api/biz/subscription/enterprise/v2/pricing",
- "https://bigmodel.cn/api/biz/customer/getCustomerInfo",
- "https://bigmodel.cn/api/biz/v1/organization/org-a/projects/proj-a/api_keys",
- "https://bigmodel.cn/api/biz/v1/organization/org-b/projects/proj-b/api_keys",
- "https://bigmodel.cn/api/biz/v1/organization/org-b/projects/proj-b/api_keys",
- ]);
- expect(requestMethods).toEqual(["GET", "GET", "GET", "GET", "POST"]);
- });
- it("getEnterprisePricing 单个 Team Plan 项目 key 预热失败不阻断其他团队", async () => {
- const requestUrls: string[] = [];
- const apiClient: ApiClient = {
- request: vi.fn(async (input, init) => {
- const url = input.toString();
- requestUrls.push(url);
- if (url.endsWith("/api/biz/subscription/enterprise/v2/pricing")) {
- return createJsonResponse({
- code: 200,
- data: {
- productList: [
- {
- productId: "product-pro-monthly",
- tier: "PRO",
- subscribeMode: "ONE_TIME",
- subscribePeriod: "MONTHLY",
- subscribed: true,
- },
- ],
- },
- });
- }
- if (url.endsWith("/api/biz/customer/getCustomerInfo")) {
- return createJsonResponse({
- code: 200,
- data: {
- organizations: [
- {
- organizationId: "org-a",
- organizationName: "团队 A",
- projects: [
- {
- projectId: "proj-a",
- projectName: "项目 A",
- projectType: 2,
- },
- ],
- },
- {
- organizationId: "org-b",
- organizationName: "团队 B",
- projects: [
- {
- projectId: "proj-b",
- projectName: "项目 B",
- projectType: 2,
- },
- ],
- },
- ],
- },
- });
- }
- if (url.endsWith("/organization/org-a/projects/proj-a/api_keys")) {
- throw new Error("org-a key failed");
- }
- if (
- url.endsWith("/organization/org-b/projects/proj-b/api_keys") &&
- init?.method === "GET"
- ) {
- return createJsonResponse({
- code: 200,
- data: [{ apiKey: "team-b-ak", keyType: 2, name: "zcode-team-api-key" }],
- });
- }
- throw new Error(`unexpected request: ${url}`);
- }),
- };
- const provider = new BigModelCodingPlanSubscriptionProvider({
- apiClient,
- credentialService: createBigModelCredentialService("oauth-token"),
- });
- const result = await provider.getEnterprisePricing({ authenticated: true });
- expect(result.productList[0]?.teamProjects).toHaveLength(2);
- expect(result.productList[0]?.teamProjects).toMatchObject([
- {
- organizationId: "org-a",
- projectId: "proj-a",
- apiKeyStatus: "unavailable",
- apiKeyUnavailableReason: "request_failed",
- },
- {
- organizationId: "org-b",
- projectId: "proj-b",
- apiKeyStatus: "available",
- },
- ]);
- expect(requestUrls).toContain(
- "https://bigmodel.cn/api/biz/v1/organization/org-b/projects/proj-b/api_keys",
- );
- });
- it("getEnterprisePricing 会把没有有效团队套餐授权的 Team Plan 项目标为不可用", async () => {
- const apiClient: ApiClient = {
- request: vi.fn(async (input, init) => {
- const url = input.toString();
- if (url.endsWith("/api/biz/subscription/enterprise/v2/pricing")) {
- return createJsonResponse({
- code: 200,
- data: {
- productList: [
- {
- productId: "product-pro-monthly",
- tier: "PRO",
- subscribeMode: "ONE_TIME",
- subscribePeriod: "MONTHLY",
- subscribed: true,
- },
- ],
- },
- });
- }
- if (url.endsWith("/api/biz/customer/getCustomerInfo")) {
- return createJsonResponse({
- code: 200,
- data: {
- organizations: [
- {
- organizationId: "org-missing",
- organizationName: "团队 Missing",
- projects: [
- {
- projectId: "proj-missing",
- projectName: "项目 Missing",
- projectType: 2,
- },
- ],
- },
- ],
- },
- });
- }
- if (
- url.endsWith("/organization/org-missing/projects/proj-missing/api_keys") &&
- init?.method === "GET"
- ) {
- return createJsonResponse({ code: 200, data: [] });
- }
- if (
- url.endsWith("/organization/org-missing/projects/proj-missing/api_keys") &&
- init?.method === "POST"
- ) {
- return createJsonResponse({
- code: 500,
- success: false,
- msg: "您当前暂无有效的团队套餐授权记录,无法创建API Key",
- data: null,
- });
- }
- throw new Error(`unexpected request: ${url}`);
- }),
- };
- const provider = new BigModelCodingPlanSubscriptionProvider({
- apiClient,
- credentialService: createBigModelCredentialService("oauth-token"),
- });
- const result = await provider.getEnterprisePricing({ authenticated: true });
- expect(result.productList[0]?.teamProjects?.[0]).toMatchObject({
- organizationId: "org-missing",
- projectId: "proj-missing",
- apiKeyStatus: "unavailable",
- apiKeyUnavailableReason: "no_valid_team_plan_authorization",
- apiKeyUnavailableMessage: "您当前暂无有效的团队套餐授权记录,无法创建API Key",
- });
- });
- it("ZCODE_ENV=test 时 Team Plan 企业接口使用 bigmodel.cn", async () => {
- vi.stubEnv("ZCODE_ENV", "test");
- const apiClient: ApiClient = {
- request: vi
- .fn()
- .mockResolvedValueOnce(
- createJsonResponse({
- code: 200,
- data: { productList: [] },
- }),
- )
- .mockResolvedValueOnce(
- createJsonResponse({
- code: 200,
- data: { productList: [] },
- }),
- )
- .mockResolvedValueOnce(
- createJsonResponse({
- code: 200,
- data: {
- giveBalance: 0,
- cashBalance: 0,
- totalBalance: 0,
- },
- }),
- )
- .mockResolvedValueOnce(
- createJsonResponse({
- code: 200,
- data: [],
- }),
- )
- .mockResolvedValueOnce(
- createJsonResponse({
- code: 200,
- data: { status: "SUCCESS" },
- }),
- ),
- };
- const provider = new BigModelCodingPlanSubscriptionProvider({
- apiClient,
- credentialService: createBigModelCredentialService("oauth-token"),
- });
- await provider.getEnterprisePricing({ authenticated: false });
- await provider.getEnterprisePricing({ authenticated: true });
- await provider.getEnterpriseBalance();
- await provider.getEnterprisePendingOrders();
- await provider.checkEnterpriseOrderStatus({ orderNo: "ORDER-1" });
- expect(vi.mocked(apiClient.request).mock.calls.map((call) => String(call[0]))).toEqual([
- "https://bigmodel.cn/api/biz/campaign/partner/enterprise/pricing",
- "https://bigmodel.cn/api/biz/subscription/enterprise/v2/pricing",
- "https://bigmodel.cn/api/biz/subscription/enterprise/v2/balance",
- "https://bigmodel.cn/api/biz/subscription/enterprise/v2/orders/pending",
- "https://bigmodel.cn/api/biz/subscription/enterprise/v2/order/ORDER-1/status",
- ]);
- });
- it("企业套餐试算和下单使用 BigModel OAuth token 并原样传递金额", async () => {
- const apiClient: ApiClient = {
- request: vi
- .fn()
- .mockResolvedValueOnce(
- createJsonResponse({
- code: 200,
- data: {
- totalOriginalAmount: 2700,
- campaignDiscountAmount: 270,
- discountDetails: [],
- totalPayAmount: 2430,
- giveDeductAmount: 0,
- balanceDeductAmount: 0,
- thirdPayAmount: 2430,
- },
- }),
- )
- .mockResolvedValueOnce(
- createJsonResponse({
- code: 200,
- data: {
- orderNo: "20260507001",
- subscriptionNo: "SUB20260507",
- totalOriginalAmount: 2700,
- campaignDiscountAmount: 270,
- totalPayAmount: 2430,
- thirdPayAmount: 2430,
- discountDetails: [],
- payUrl: "https://openapi.alipay.com/pay",
- alipayJumpSchema: "alipays://platformapi/startApp",
- expireTime: "2026-05-07 10:30:00",
- },
- }),
- ),
- };
- const provider = new BigModelCodingPlanSubscriptionProvider({
- apiClient,
- credentialService: createBigModelCredentialService("oauth-token"),
- });
- const estimate = await provider.calculateEnterpriseOrder({
- productId: "enterprise-lite-monthly",
- maxSeats: 10,
- subscribePeriod: "MONTHLY",
- subscribeMode: "ONE_TIME",
- duration: 1,
- giveAmount: 0,
- balanceDeductAmount: 0,
- });
- await provider.createEnterpriseOrder({
- productId: "enterprise-lite-monthly",
- maxSeats: 10,
- subscribePeriod: "MONTHLY",
- subscribeMode: "ONE_TIME",
- purchaseType: "PAY",
- duration: 1,
- giveAmount: 0,
- balanceDeductAmount: 0,
- totalOriginalAmount: estimate.totalOriginalAmount,
- totalPayAmount: estimate.totalPayAmount,
- thirdPayAmount: estimate.thirdPayAmount,
- });
- expect(apiClient.request).toHaveBeenNthCalledWith(
- 1,
- "https://bigmodel.cn/api/biz/subscription/enterprise/v2/order/calculate",
- expect.objectContaining({
- method: "POST",
- headers: {
- Authorization: "oauth-token",
- "Content-Type": "application/json",
- },
- body: JSON.stringify({
- productId: "enterprise-lite-monthly",
- maxSeats: 10,
- subscribePeriod: "MONTHLY",
- subscribeMode: "ONE_TIME",
- duration: 1,
- giveAmount: 0,
- balanceDeductAmount: 0,
- }),
- }),
- );
- expect(apiClient.request).toHaveBeenNthCalledWith(
- 2,
- "https://bigmodel.cn/api/biz/subscription/enterprise/v2/order",
- expect.objectContaining({
- method: "POST",
- body: JSON.stringify({
- productId: "enterprise-lite-monthly",
- maxSeats: 10,
- subscribePeriod: "MONTHLY",
- subscribeMode: "ONE_TIME",
- purchaseType: "PAY",
- duration: 1,
- giveAmount: 0,
- balanceDeductAmount: 0,
- totalOriginalAmount: 2700,
- totalPayAmount: 2430,
- thirdPayAmount: 2430,
- }),
- }),
- );
- });
- it("getEnterpriseBalance 使用 BigModel OAuth token 查询企业余额", async () => {
- const apiClient: ApiClient = {
- request: vi.fn(async () =>
- createJsonResponse({
- code: 200,
- data: {
- giveBalance: 1000,
- cashBalance: 1500,
- totalBalance: 2500,
- },
- }),
- ),
- };
- const provider = new BigModelCodingPlanSubscriptionProvider({
- apiClient,
- credentialService: createBigModelCredentialService("oauth-token"),
- });
- await expect(provider.getEnterpriseBalance()).resolves.toEqual({
- giveBalance: 1000,
- cashBalance: 1500,
- totalBalance: 2500,
- });
- expect(String(vi.mocked(apiClient.request).mock.calls[0]?.[0])).toBe(
- "https://bigmodel.cn/api/biz/subscription/enterprise/v2/balance",
- );
- expect(vi.mocked(apiClient.request).mock.calls[0]?.[1]).toMatchObject({
- method: "GET",
- headers: {
- Authorization: "oauth-token",
- "Content-Type": "application/json",
- },
- });
- });
- it("createSign 默认 ALI 且固定 isDelay=0", async () => {
- const apiClient: ApiClient = {
- request: vi.fn(async () =>
- createJsonResponse({
- code: 200,
- data: {
- orderId: "order-1",
- sign: "<form></form>",
- signType: "ALI",
- },
- }),
- ),
- };
- const provider = new BigModelCodingPlanSubscriptionProvider({
- apiClient,
- credentialService: createBigModelCredentialService(),
- });
- await provider.createSign({ bizId: "biz-1" });
- const init = (apiClient.request as ReturnType<typeof vi.fn>).mock.calls[0]?.[1] as RequestInit;
- expect(JSON.parse(String(init.body))).toEqual({
- bizId: "biz-1",
- payType: "ALI",
- renew: null,
- isDelay: 0,
- effectiveTime: null,
- });
- });
- it("updateSign 调用套餐变更签约接口", async () => {
- const apiClient: ApiClient = {
- request: vi.fn(async () =>
- createJsonResponse({
- code: 200,
- data: {
- orderId: "order-2",
- sign: "https://pay.example",
- signType: "ALI",
- },
- }),
- ),
- };
- const provider = new BigModelCodingPlanSubscriptionProvider({
- apiClient,
- credentialService: createBigModelCredentialService(),
- });
- await provider.updateSign({ bizId: "biz-2" });
- expect(apiClient.request).toHaveBeenCalledWith(
- "https://bigmodel.cn/api/biz/pay/product/update/sign",
- expect.objectContaining({
- method: "POST",
- }),
- );
- });
- it("preview 透传安全校验 ticket 和 randstr", async () => {
- const apiClient: ApiClient = {
- request: vi.fn(async () =>
- createJsonResponse({
- code: 200,
- data: {
- productId: "product-1",
- bizId: "biz-1",
- soldOut: false,
- payAmount: 149,
- },
- }),
- ),
- };
- const provider = new BigModelCodingPlanSubscriptionProvider({
- apiClient,
- credentialService: createBigModelCredentialService(),
- });
- await provider.preview({
- productId: "product-1",
- ticket: "fixture-ticket",
- randstr: "fixture-randstr",
- });
- const init = (apiClient.request as ReturnType<typeof vi.fn>).mock.calls[0]?.[1] as RequestInit;
- expect(JSON.parse(String(init.body))).toMatchObject({
- productId: "product-1",
- ticket: "fixture-ticket",
- randstr: "fixture-randstr",
- salesChannel: "zcode",
- });
- });
- it("Z.ai preview 使用海外试算接口", async () => {
- const apiClient: ApiClient = {
- request: vi.fn(async () => {
- return createJsonResponse({
- code: 200,
- data: {
- productId: "zai-product-pro",
- bizId: "zai-biz-1",
- soldOut: false,
- payAmount: 18,
- },
- });
- }),
- };
- const provider = new BigModelCodingPlanSubscriptionProvider({
- apiClient,
- credentialService: {
- load: vi.fn(async (key) => (key === "oauth:zai:access_token" ? "zai-business-jwt" : null)),
- },
- });
- await provider.preview({
- providerId: BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
- productId: "zai-product-pro",
- ticket: "fixture-ticket",
- randstr: "fixture-randstr",
- });
- expect(apiClient.request).toHaveBeenNthCalledWith(
- 1,
- "https://api.z.ai/api/biz/pay/zai-preview",
- expect.objectContaining({
- method: "POST",
- headers: {
- Authorization: "zai-business-jwt",
- "Content-Type": "application/json",
- },
- }),
- );
- const init = (apiClient.request as ReturnType<typeof vi.fn>).mock.calls[0]?.[1] as RequestInit;
- expect(JSON.parse(String(init.body))).toMatchObject({
- productId: "zai-product-pro",
- ticket: "fixture-ticket",
- randstr: "fixture-randstr",
- salesChannel: "zcode",
- });
- });
- it("Z.ai 不走 BigModel 支付宝签约接口", async () => {
- const apiClient: ApiClient = {
- request: vi.fn(),
- };
- const provider = new BigModelCodingPlanSubscriptionProvider({
- apiClient,
- credentialService: {
- load: vi.fn(async () => "zai-business-jwt"),
- },
- });
- await expect(
- provider.createSign({
- providerId: BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
- bizId: "zai-biz-1",
- }),
- ).rejects.toThrow("coding_plan_zai_overseas_payment_required");
- expect(apiClient.request).not.toHaveBeenCalled();
- });
- it("Z.ai Stripe 支付使用 pay 服务接口", async () => {
- const apiClient: ApiClient = {
- request: vi.fn(async () => {
- return createJsonResponse({
- code: 200,
- data: {
- orderId: "order-stripe-1",
- paymentIntentId: "pi_1",
- status: "SUCCEEDED",
- },
- });
- }),
- };
- const provider = new BigModelCodingPlanSubscriptionProvider({
- apiClient,
- credentialService: {
- load: vi.fn(async () => "zai-business-jwt"),
- },
- });
- await provider.payStripe({
- providerId: BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
- productId: "zai-product-pro",
- paymentMethodId: "pm_1",
- returnUrl: "https://chat.z.ai",
- estimatePayAmount: {
- cashPayAmount: 0,
- givePayAmount: 0,
- thirdPartyPayAmount: 18,
- },
- bizId: "zai-biz-1",
- renew: true,
- });
- expect(apiClient.request).toHaveBeenNthCalledWith(
- 1,
- "https://api.z.ai/api/pay/stripe/pay",
- expect.objectContaining({
- method: "POST",
- }),
- );
- const init = (apiClient.request as ReturnType<typeof vi.fn>).mock.calls[0]?.[1] as RequestInit;
- expect(JSON.parse(String(init.body))).toMatchObject({
- productId: "zai-product-pro",
- paymentMethodId: "pm_1",
- returnUrl: "https://chat.z.ai",
- estimatePayAmount: {
- thirdPartyPayAmount: 18,
- },
- bizId: "zai-biz-1",
- renew: true,
- });
- });
- it("Z.ai Stripe 绑卡和解绑使用 pay 服务接口", async () => {
- const apiClient: ApiClient = {
- request: vi.fn(async (input) => {
- if (String(input).includes("/stripe/bind")) {
- return createJsonResponse({
- code: 200,
- data: {
- paymentMethodId: "pm_1",
- brand: "visa",
- last4: "4242",
- requiresAction: false,
- },
- });
- }
- return createJsonResponse({
- code: 200,
- data: "ok",
- });
- }),
- };
- const provider = new BigModelCodingPlanSubscriptionProvider({
- apiClient,
- credentialService: {
- load: vi.fn(async () => "zai-business-jwt"),
- },
- });
- await provider.bindStripeCard({
- providerId: BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
- paymentMethodId: "pm_1",
- returnUrl: "https://chat.z.ai",
- });
- await provider.unbindStripeCard({
- providerId: BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
- paymentMethodId: "pm_1",
- });
- expect(apiClient.request).toHaveBeenNthCalledWith(
- 1,
- "https://api.z.ai/api/pay/stripe/bind",
- expect.objectContaining({ method: "POST" }),
- );
- expect(apiClient.request).toHaveBeenNthCalledWith(
- 2,
- "https://api.z.ai/api/pay/stripe/unbind",
- expect.objectContaining({ method: "POST" }),
- );
- });
- it("Z.ai PayPal setupToken 和 subscribe 使用 pay 服务接口", async () => {
- const apiClient: ApiClient = {
- request: vi.fn(async (input) => {
- if (String(input).includes("/paypal/setupToken")) {
- return createJsonResponse({
- code: 200,
- data: {
- resultStatus: "SUCCESS",
- setupTokenId: "setup-token-1",
- approveUrl: "https://paypal.example/approve",
- },
- });
- }
- return createJsonResponse({
- code: 200,
- data: {
- resultStatus: "SUCCESS",
- payStatus: "SUCCESS",
- orderId: "paypal-order-1",
- },
- });
- }),
- };
- const provider = new BigModelCodingPlanSubscriptionProvider({
- apiClient,
- credentialService: {
- load: vi.fn(async () => "zai-business-jwt"),
- },
- });
- await provider.createPaypalSetupToken({
- providerId: BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
- returnUrl: "https://chat.z.ai/return",
- cancelUrl: "https://chat.z.ai/cancel",
- });
- await provider.subscribePaypal({
- providerId: BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
- productId: "zai-product-pro",
- setupTokenId: "setup-token-1",
- amount: 18,
- estimatePayAmount: {
- thirdPartyPayAmount: 18,
- },
- bizId: "zai-biz-1",
- });
- expect(apiClient.request).toHaveBeenNthCalledWith(
- 1,
- "https://api.z.ai/api/pay/paypal/setupToken",
- expect.objectContaining({ method: "POST" }),
- );
- expect(apiClient.request).toHaveBeenNthCalledWith(
- 2,
- "https://api.z.ai/api/pay/paypal/subscribe",
- expect.objectContaining({ method: "POST" }),
- );
- });
- it("productInfo 使用产品详情接口补齐套餐展示信息", async () => {
- const apiClient: ApiClient = {
- request: vi.fn(async () =>
- createJsonResponse({
- code: 200,
- data: {
- productId: "product-lite",
- productName: "GLM Coding Lite",
- productBigTitle: "GLM Coding Plan",
- productSmallTitle: "轻量日常使用",
- },
- }),
- ),
- };
- const provider = new BigModelCodingPlanSubscriptionProvider({
- apiClient,
- credentialService: createBigModelCredentialService(),
- });
- const result = await provider.productInfo({ productId: "product-lite" });
- expect(result.productName).toBe("GLM Coding Lite");
- expect(apiClient.request).toHaveBeenCalledWith(
- new URL("https://bigmodel.cn/api/biz/product/info?productId=product-lite"),
- expect.objectContaining({
- method: "GET",
- headers: {
- Authorization: "oauth-token",
- "Content-Type": "application/json",
- },
- }),
- );
- });
- });
- describe("ZaiCodingPlanSubscriptionProvider (family-aware Team Plan)", () => {
- afterEach(() => {
- vi.unstubAllEnvs();
- });
- // 修复原因:开发机可能设了 ZAI_BUSINESS_BASE_URL=https://api.z.ai 环境变量,
- // resolveZaiBusinessBaseUrl 会优先读它,覆盖 ZCODE_ENV 逻辑导致测试域名断言不稳定。
- // 测试里统一 stub 成空,强制走 ZCODE_ENV fallback 链。
- function createZaiCredentialService(token = "zai-business-jwt") {
- return {
- load: vi.fn(async (key: string) => (key === "oauth:zai:access_token" ? token : null)),
- };
- }
- it("getEnterprisePricing 未登录走 zai 域名公开定价接口", async () => {
- vi.stubEnv("ZAI_BUSINESS_BASE_URL", "");
- const apiClient: ApiClient = {
- request: vi.fn(async () =>
- createJsonResponse({
- code: 200,
- data: {
- productList: [
- {
- productId: "zai-team-pro",
- tier: "PRO",
- subscribeMode: "ONE_TIME",
- subscribePeriod: "MONTHLY",
- originalAmount: 18,
- discountAmount: 0,
- payAmount: 18,
- renewAmount: 18,
- },
- ],
- },
- }),
- ),
- };
- const provider = new ZaiCodingPlanSubscriptionProvider({
- apiClient,
- credentialService: { load: vi.fn() },
- });
- const result = await provider.getEnterprisePricing({
- authenticated: false,
- });
- expect(result.productList[0]?.productId).toBe("zai-team-pro");
- // 关键:zai 域名,不能误用 bigmodel.cn
- expect(String(vi.mocked(apiClient.request).mock.calls[0]?.[0])).toBe(
- "https://api.z.ai/api/biz/campaign/partner/enterprise/pricing",
- );
- });
- it("getEnterprisePricing 登录态使用 zai 域名 + zai OAuth token(非 bigmodel)", async () => {
- vi.stubEnv("ZAI_BUSINESS_BASE_URL", "");
- const apiClient: ApiClient = {
- request: vi.fn(async () => createJsonResponse({ code: 200, data: { productList: [] } })),
- };
- const provider = new ZaiCodingPlanSubscriptionProvider({
- apiClient,
- credentialService: createZaiCredentialService("zai-business-jwt"),
- });
- await provider.getEnterprisePricing({ authenticated: true });
- expect(apiClient.request).toHaveBeenCalledTimes(1);
- // 关键:host 是 zai 域名,不是 bigmodel.cn
- expect(String(vi.mocked(apiClient.request).mock.calls[0]?.[0])).toBe(
- "https://api.z.ai/api/biz/subscription/enterprise/v2/pricing",
- );
- // 关键:token 是 zai 的,不带 Bearer
- expect(vi.mocked(apiClient.request).mock.calls[0]?.[1]).toMatchObject({
- method: "GET",
- headers: {
- Authorization: "zai-business-jwt",
- "Content-Type": "application/json",
- },
- });
- });
- it("ZCODE_ENV=test 时 zai Team Plan 企业接口使用 api.z.ai", async () => {
- vi.stubEnv("ZCODE_ENV", "test");
- // 开发机 ZAI_BUSINESS_BASE_URL 会覆盖 ZCODE_ENV fallback,这里 stub 成空强制走 fallback
- vi.stubEnv("ZAI_BUSINESS_BASE_URL", "");
- const apiClient: ApiClient = {
- request: vi.fn(async () => createJsonResponse({ code: 200, data: { productList: [] } })),
- };
- const provider = new ZaiCodingPlanSubscriptionProvider({
- apiClient,
- credentialService: createZaiCredentialService(),
- });
- await provider.getEnterprisePricing({ authenticated: true });
- // 关键:测试环境走 z.ai,不能把测试 token 发到生产 api.z.ai
- expect(String(vi.mocked(apiClient.request).mock.calls[0]?.[0])).toBe(
- "https://api.z.ai/api/biz/subscription/enterprise/v2/pricing",
- );
- });
- it("getEnterprisePricing 缺少 zai OAuth token 时抛 zai_oauth_required 且不请求", async () => {
- const apiClient: ApiClient = { request: vi.fn() };
- const provider = new ZaiCodingPlanSubscriptionProvider({
- apiClient,
- credentialService: { load: vi.fn(async () => null) },
- });
- await expect(provider.getEnterprisePricing({ authenticated: true })).rejects.toThrow(
- "zai_oauth_required",
- );
- expect(apiClient.request).not.toHaveBeenCalled();
- });
- it("getEnterprisePricing 用 zai 域名的 customerInfo 给已购 Team Plan 补齐项目上下文", async () => {
- vi.stubEnv("ZAI_BUSINESS_BASE_URL", "");
- const requestUrls: string[] = [];
- const apiClient: ApiClient = {
- request: vi.fn(async (input) => {
- const url = input.toString();
- requestUrls.push(url);
- if (url.endsWith("/api/biz/subscription/enterprise/v2/pricing")) {
- return createJsonResponse({
- code: 200,
- data: {
- productList: [
- {
- productId: "zai-team-pro",
- tier: "PRO",
- subscribeMode: "CONTINUOUS",
- subscribePeriod: "MONTHLY",
- subscribed: true,
- },
- ],
- },
- });
- }
- if (url.endsWith("/api/biz/customer/getCustomerInfo")) {
- return createJsonResponse({
- code: 200,
- data: {
- organizations: [
- {
- organizationId: "zai-org",
- organizationName: "zai 默认机构",
- projects: [
- {
- projectId: "zai-proj",
- projectName: "zai 团队项目",
- projectType: 2,
- },
- ],
- },
- ],
- },
- });
- }
- if (url.endsWith("/organization/zai-org/projects/zai-proj/api_keys")) {
- // team-api-key 预热:返回已有可用 key,避免 create 调用
- return createJsonResponse({
- code: 200,
- data: [{ apiKey: "zai-team-ak", keyType: 2, name: "zcode-team-api-key" }],
- });
- }
- throw new Error(`unexpected request: ${url}`);
- }),
- };
- const provider = new ZaiCodingPlanSubscriptionProvider({
- apiClient,
- credentialService: createZaiCredentialService("zai-token"),
- });
- const result = await provider.getEnterprisePricing({ authenticated: true });
- // 关键:pricing + customerInfo + team-api-key 预热都走 zai 域名,
- // 不能串到 bigmodel.cn。预热也必须用 zai host(符合"除购买外路径可复用"契约)。
- expect(requestUrls).toEqual([
- "https://api.z.ai/api/biz/subscription/enterprise/v2/pricing",
- "https://api.z.ai/api/biz/customer/getCustomerInfo",
- "https://api.z.ai/api/biz/v1/organization/zai-org/projects/zai-proj/api_keys",
- ]);
- expect(result.productList[0]).toMatchObject({
- organizationId: "zai-org",
- projectId: "zai-proj",
- projectName: "zai 团队项目",
- });
- });
- });
- describe("createCodingPlanSubscriptionService (family routing)", () => {
- afterEach(() => {
- vi.unstubAllEnvs();
- });
- function createCredentialService() {
- return {
- load: vi.fn(async (key: string) => {
- if (key === "oauth:bigmodel:access_token") return "bigmodel-token";
- if (key === "oauth:zai:access_token") return "zai-token";
- return null;
- }),
- };
- }
- it("getEnterprisePricing 缺省 family 时走 bigmodel 域名(向后兼容)", async () => {
- const requestUrls: string[] = [];
- const apiClient: ApiClient = {
- request: vi.fn(async (input) => {
- requestUrls.push(input.toString());
- return createJsonResponse({ code: 200, data: { productList: [] } });
- }),
- };
- const service = createCodingPlanSubscriptionService({
- apiClient,
- credentialService: createCredentialService(),
- });
- await service.getEnterprisePricing({ authenticated: true });
- expect(requestUrls).toEqual(["https://bigmodel.cn/api/biz/subscription/enterprise/v2/pricing"]);
- });
- it("getEnterprisePricing family=bigmodel 走 bigmodel 域名 + bigmodel token", async () => {
- const apiClient: ApiClient = {
- request: vi.fn(async () => createJsonResponse({ code: 200, data: { productList: [] } })),
- };
- const service = createCodingPlanSubscriptionService({
- apiClient,
- credentialService: createCredentialService(),
- });
- await service.getEnterprisePricing({
- authenticated: true,
- family: "bigmodel",
- });
- expect(String(vi.mocked(apiClient.request).mock.calls[0]?.[0])).toBe(
- "https://bigmodel.cn/api/biz/subscription/enterprise/v2/pricing",
- );
- expect(vi.mocked(apiClient.request).mock.calls[0]?.[1]).toMatchObject({
- headers: { Authorization: "bigmodel-token" },
- });
- });
- it("getEnterprisePricing family=zai 走 zai 域名 + zai token", async () => {
- vi.stubEnv("ZAI_BUSINESS_BASE_URL", "");
- const apiClient: ApiClient = {
- request: vi.fn(async () => createJsonResponse({ code: 200, data: { productList: [] } })),
- };
- const service = createCodingPlanSubscriptionService({
- apiClient,
- credentialService: createCredentialService(),
- });
- await service.getEnterprisePricing({ authenticated: true, family: "zai" });
- // 关键:service 层按 family 路由,zai 不能误用 bigmodel.cn
- expect(String(vi.mocked(apiClient.request).mock.calls[0]?.[0])).toBe(
- "https://api.z.ai/api/biz/subscription/enterprise/v2/pricing",
- );
- expect(vi.mocked(apiClient.request).mock.calls[0]?.[1]).toMatchObject({
- headers: { Authorization: "zai-token" },
- });
- });
- it("企业购买闭环接口仍走 bigmodel 域名(与 family 无关)", async () => {
- const apiClient: ApiClient = {
- request: vi.fn(async () =>
- createJsonResponse({
- code: 200,
- data: { giveBalance: 0, cashBalance: 0, totalBalance: 0 },
- }),
- ),
- };
- const service = createCodingPlanSubscriptionService({
- apiClient,
- credentialService: createCredentialService(),
- });
- await service.getEnterpriseBalance();
- expect(String(vi.mocked(apiClient.request).mock.calls[0]?.[0])).toBe(
- "https://bigmodel.cn/api/biz/subscription/enterprise/v2/balance",
- );
- });
- });
|