| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660 |
- import { afterEach, describe, expect, it, vi } from "vitest";
- import { BUILTIN_MODEL_PROVIDER_IDS, CODING_PLAN_SYSTEM_BUSY, type ApiClient } from "@zcode/shared";
- import {
- BigModelCodingPlanSubscriptionProvider,
- createBigModelLoginAuthHeaders,
- createZaiLoginAuthHeaders,
- 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("预算读取不触发配置失败请求", async () => {
- const request = vi.fn(async () => {
- throw new Error("offline");
- });
- const provider = new BigModelCodingPlanSubscriptionProvider({
- apiClient: { request },
- credentialService: { load: vi.fn() },
- });
- await expect(provider.getModelContextBudgetStrategy()).resolves.toBe("preflight-v1");
- expect(request).not.toHaveBeenCalled();
- });
- 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",
- },
- }),
- );
- });
- it("鉴权 header helper 只返回裸 token", () => {
- expect(createBigModelLoginAuthHeaders("token-1")).toEqual({
- Authorization: "token-1",
- "Content-Type": "application/json",
- });
- });
- it("Z.ai 鉴权 header helper 只返回裸登录 token", () => {
- expect(createZaiLoginAuthHeaders("zai-token")).toEqual({
- Authorization: "zai-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",
- );
- });
- });
|