| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783 |
- import { afterEach, describe, expect, it, vi } from "vitest";
- import {
- BIGMODEL_PROVIDER_ID,
- BUILTIN_MODEL_PROVIDER_IDS,
- ZCODE_VERSION,
- type ApiClient,
- } from "@zcode/shared";
- import { BigModelUsageQuotaProvider } from "../src/usage-stats/providers/bigmodelUsageQuotaProvider.js";
- const TEST_BIGMODEL_PROVIDER_ID = "test-bigmodel-api-provider";
- interface TestUsageProvider {
- id: string;
- apiKey: string;
- name?: string;
- }
- interface LegacyModelProviderReader {
- getAllCached(): Promise<TestUsageProvider[]>;
- }
- type LegacyQuotaProviderTestOptions = Omit<
- ConstructorParameters<typeof BigModelUsageQuotaProvider>[0],
- "accountRequestAuthService"
- > & {
- modelProviderService?: LegacyModelProviderReader;
- accountRequestAuthService?: ConstructorParameters<
- typeof BigModelUsageQuotaProvider
- >[0]["accountRequestAuthService"];
- };
- /**
- * 这些测试原本直接把旧 ModelProviderService 注入 quota provider。
- * 迁移后凭据由 Account Request Auth 提供;这里仅保留测试数据装配,避免测试继续
- * 把旧 Provider Config 当成生产事实来源。
- */
- function createProviderForTest(
- options: LegacyQuotaProviderTestOptions,
- ): BigModelUsageQuotaProvider {
- const provider = new BigModelUsageQuotaProvider({
- ...options,
- accountRequestAuthService: options.accountRequestAuthService ?? {
- resolveAccessCurrent: vi.fn(async () => null),
- resolveCurrent: vi.fn(async ({ providerId, accountAccess }) => {
- if (accountAccess.planKind === "team-coding-plan") {
- return {
- apiKey:
- providerId === BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan
- ? "team-ak-zai.team-secret-zai"
- : "team-ak.team-secret",
- };
- }
- if (providerId.includes("start-plan")) {
- const token = await options.credentialService?.load("zcodejwttoken");
- return { apiKey: token ?? undefined };
- }
- const providers = await options.modelProviderService?.getAllCached();
- return {
- apiKey: providers?.find((candidate) => candidate.id === providerId)?.apiKey,
- };
- }),
- assertCurrent: vi.fn(async () => undefined),
- },
- });
- const getSnapshotForRequest = provider.getSnapshotForRequest.bind(provider);
- provider.getSnapshotForRequest = (request = {}) =>
- getSnapshotForRequest(withTestAccountAccess(request));
- return provider;
- }
- function withTestAccountAccess(
- request: Parameters<BigModelUsageQuotaProvider["getSnapshotForRequest"]>[0] = {},
- ) {
- const providerId = request.preferredProviderId ?? "";
- const family = providerId.includes("zai") ? ("zai" as const) : ("bigmodel" as const);
- if (request.organizationId && request.projectId) {
- return {
- ...request,
- accountAccess: {
- type: "zhipu-account" as const,
- family,
- planKind: "team-coding-plan" as const,
- productId: "test-product",
- organizationId: request.organizationId,
- projectId: request.projectId,
- },
- };
- }
- return {
- ...request,
- accountAccess: {
- type: "zhipu-account" as const,
- family,
- planKind: providerId.includes("start-plan")
- ? ("start-plan" as const)
- : ("individual-coding-plan" as const),
- },
- };
- }
- function createBigModelCodingPlanProvider(): TestUsageProvider {
- return {
- id: BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
- apiKey: "bigmodel-api-key",
- };
- }
- function createBigModelApiKeyProvider(): TestUsageProvider {
- return {
- ...createBigModelCodingPlanProvider(),
- id: TEST_BIGMODEL_PROVIDER_ID,
- apiKey: "ordinary-api-key",
- };
- }
- function createZaiCodingPlanProvider(): TestUsageProvider {
- return {
- id: BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
- apiKey: "zai-api-key",
- };
- }
- function createZaiStartPlanProvider(): TestUsageProvider {
- return {
- ...createZaiCodingPlanProvider(),
- id: BUILTIN_MODEL_PROVIDER_IDS.zaiStartPlan,
- apiKey: "provider-zcode-jwt",
- };
- }
- function createJsonResponse(body: unknown): Response {
- return new Response(JSON.stringify(body), {
- status: 200,
- headers: { "Content-Type": "application/json", Date: "Tue, 02 Jun 2026 00:00:00 GMT" },
- });
- }
- describe("BigModelUsageQuotaProvider", () => {
- afterEach(() => {
- vi.unstubAllEnvs();
- });
- it("旧 active 响应超过 HTTP Date 有效期后返回过期无权益快照", async () => {
- const provider = createProviderForTest({
- apiClient: {
- request: vi.fn(
- async () =>
- new Response(
- JSON.stringify({
- code: 0,
- data: {
- server_time: 1788448680,
- plans: [
- { plan_id: "zcode-v3-start-plan", status: "active", ends_at: 1788796799 },
- ],
- balances: [{ plan_id: "zcode-v3-start-plan", remaining_units: 3000000 }],
- },
- }),
- { headers: { Date: "Thu, 17 Sep 2026 13:42:46 GMT" } },
- ),
- ),
- },
- modelProviderService: {
- getAllCached: vi.fn(async () => [createZaiStartPlanProvider()]),
- } as LegacyModelProviderReader,
- credentialService: { load: vi.fn(async () => "credential-zcode-jwt") },
- });
- expect(
- await provider.getSnapshotForRequest({
- preferredProviderId: BUILTIN_MODEL_PROVIDER_IDS.zaiStartPlan,
- requirePreferredProvider: true,
- }),
- ).toMatchObject({
- unavailableReason: "no_plan",
- startPlanExpired: true,
- subscription: null,
- quota: null,
- remaining: null,
- });
- });
- it("Z.ai Start Plan 用 zcodejwttoken 读取当前套餐和到期时间", async () => {
- const balanceUrl = `https://zcode.z.ai/api/v1/zcode-plan/billing/balance?app_version=${ZCODE_VERSION}`;
- const requestUrls: string[] = [];
- const apiClient: ApiClient = {
- request: vi.fn(async (input, init) => {
- const url = input.toString();
- requestUrls.push(url);
- expect(init?.headers).toMatchObject({
- Authorization: "Bearer credential-zcode-jwt",
- });
- expect(url).toBe(balanceUrl);
- return createJsonResponse({
- code: 0,
- msg: "ok",
- data: {
- server_time: 1_780_400_123,
- plans: [
- {
- plan_id: "zcode-v3-start-plan",
- name: "ZCode V3 Start Plan",
- status: "active",
- starts_at: 1_780_291_200,
- ends_at: 1_781_500_800,
- entitlements: [
- {
- entitlement_id: "ent_zcode_v3_glm_52",
- meter: "model_usage",
- period: "daily",
- },
- ],
- },
- ],
- balances: [
- {
- bucket_id: "bucket-glm-52",
- user_plan_id: "user-plan-52",
- period_start: 1_780_416_000,
- period_end: 1_780_502_399,
- meter: "model_usage",
- unit_type: "token",
- plan_id: "zcode-v3-start-plan",
- entitlement_id: "ent_zcode_v3_glm_52",
- show_name: "GLM-5.2",
- capabilities: ["model:glm-5.2"],
- total_units: 3_000_000,
- used_units: 500_000,
- remaining_units: 2_500_000,
- available_units: 2_500_000,
- expires_at: 1_780_502_400,
- },
- ],
- },
- });
- }),
- };
- const provider = createProviderForTest({
- apiClient,
- modelProviderService: {
- getAllCached: vi.fn(async () => [createZaiStartPlanProvider()]),
- } as LegacyModelProviderReader,
- credentialService: {
- load: vi.fn(async (key: string) =>
- key === "zcodejwttoken" ? "credential-zcode-jwt" : null,
- ),
- },
- });
- const snapshot = await provider.getSnapshotForRequest({
- preferredProviderId: BUILTIN_MODEL_PROVIDER_IDS.zaiStartPlan,
- requirePreferredProvider: true,
- });
- expect(requestUrls).toEqual([balanceUrl]);
- expect(snapshot.serverTime).toBe(1_780_400_123_000);
- expect(snapshot.provider).toMatchObject({
- id: BUILTIN_MODEL_PROVIDER_IDS.zaiStartPlan,
- name: "Z.ai - Coding Plan",
- });
- expect(snapshot.subscription?.details[0]).toMatchObject({
- productId: "zcode-v3-start-plan",
- productName: "ZCode V3 Start Plan",
- billingCycle: "daily",
- // Start Plan 卡片不再展示套餐级续期时间;桶刷新时间由各 limit 的 nextResetTime 表达。
- renewTime: null,
- expireTime: new Date(1_781_500_800 * 1000).toISOString(),
- });
- expect(snapshot.quota).toMatchObject({
- level: "Start",
- limits: [
- {
- type: "ent_zcode_v3_glm_52",
- bucketId: "bucket-glm-52",
- userPlanId: "user-plan-52",
- periodStart: 1_780_416_000_000,
- periodEnd: 1_780_502_399_000,
- period: "daily",
- meter: "model_usage",
- unitType: "token",
- number: 3_000_000,
- usage: 500_000,
- remaining: 2_500_000,
- usageDetails: [{ modelCode: "glm-5.2", displayName: "GLM-5.2", usage: 500_000 }],
- },
- ],
- });
- expect(snapshot.remaining).toMatchObject({
- count: 2_500_000,
- isShow: true,
- nextResetTime: 1_780_502_400 * 1000,
- });
- });
- it("Z.ai Start Plan 保留多个套餐顺序和额度桶 plan id", async () => {
- const apiClient: ApiClient = {
- request: vi.fn(async () =>
- createJsonResponse({
- code: 0,
- msg: "ok",
- data: {
- plans: [
- {
- plan_id: "start-plan-welcome",
- name: "Welcome Plan",
- status: "active",
- ends_at: 1_781_500_800,
- },
- {
- plan_id: "start-plan-bonus",
- name: "Bonus Plan",
- status: "active",
- ends_at: 1_782_105_600,
- },
- ],
- balances: [
- {
- plan_id: "start-plan-welcome",
- entitlement_id: "welcome-glm-52",
- show_name: "GLM-5.2",
- capabilities: ["model:glm-5.2"],
- total_units: 3_000_000,
- remaining_units: 2_500_000,
- expires_at: 1_780_506_000,
- },
- {
- plan_id: "start-plan-bonus",
- entitlement_id: "bonus-glm-5-turbo",
- show_name: "GLM-5-Turbo",
- capabilities: ["model:glm-5-turbo"],
- total_units: 1_000_000,
- remaining_units: 750_000,
- expires_at: 1_781_110_800,
- },
- ],
- },
- }),
- ),
- };
- const provider = createProviderForTest({
- apiClient,
- modelProviderService: {
- getAllCached: vi.fn(async () => [createZaiStartPlanProvider()]),
- } as LegacyModelProviderReader,
- credentialService: {
- load: vi.fn(async () => "credential-zcode-jwt"),
- },
- });
- const snapshot = await provider.getSnapshotForRequest({
- preferredProviderId: BUILTIN_MODEL_PROVIDER_IDS.zaiStartPlan,
- requirePreferredProvider: true,
- });
- expect(snapshot.subscription?.details).toMatchObject([
- {
- productId: "start-plan-welcome",
- productName: "Welcome Plan",
- expireTime: new Date(1_781_500_800 * 1000).toISOString(),
- },
- {
- productId: "start-plan-bonus",
- productName: "Bonus Plan",
- expireTime: new Date(1_782_105_600 * 1000).toISOString(),
- },
- ]);
- expect(snapshot.quota?.limits).toMatchObject([
- {
- type: "welcome-glm-52",
- planId: "start-plan-welcome",
- nextResetTime: 1_780_506_000 * 1000,
- },
- {
- type: "bonus-glm-5-turbo",
- planId: "start-plan-bonus",
- nextResetTime: 1_781_110_800 * 1000,
- },
- ]);
- });
- it("Z.ai Start Plan 过期套餐不进入 subscription 且移除其额度桶", async () => {
- // 固化服务端契约被破坏时的行为:subscription 只含 active 套餐;
- // 无法归属的桶由 provider 原样透传(设置页归属层会丢弃,聊天气泡仍展示),
- // 同时 provider 会打 warn tripwire 日志(见 warnOnUnattributedStartPlanBuckets)。
- const apiClient: ApiClient = {
- request: vi.fn(async () =>
- createJsonResponse({
- code: 0,
- msg: "ok",
- data: {
- plans: [
- {
- plan_id: "start-plan-welcome",
- name: "ZCode V3 Start Plan",
- status: "active",
- ends_at: 1_781_500_800,
- },
- {
- plan_id: "start-plan-expired",
- name: "Expired Plan",
- status: "expired",
- ends_at: 1_780_000_000,
- },
- ],
- balances: [
- {
- plan_id: "start-plan-welcome",
- entitlement_id: "welcome-glm-52",
- show_name: "GLM-5.2",
- capabilities: ["model:glm-5.2"],
- total_units: 3_000_000,
- remaining_units: 2_500_000,
- expires_at: 1_780_506_000,
- },
- {
- plan_id: "start-plan-expired",
- entitlement_id: "expired-glm-52",
- show_name: "GLM-5.2",
- capabilities: ["model:glm-5.2"],
- total_units: 900_000,
- remaining_units: 800_000,
- expires_at: 1_780_506_000,
- },
- ],
- },
- }),
- ),
- };
- const provider = createProviderForTest({
- apiClient,
- modelProviderService: {
- getAllCached: vi.fn(async () => [createZaiStartPlanProvider()]),
- } as LegacyModelProviderReader,
- credentialService: {
- load: vi.fn(async () => "credential-zcode-jwt"),
- },
- });
- const snapshot = await provider.getSnapshotForRequest({
- preferredProviderId: BUILTIN_MODEL_PROVIDER_IDS.zaiStartPlan,
- requirePreferredProvider: true,
- });
- expect(snapshot.subscription?.details).toMatchObject([{ productId: "start-plan-welcome" }]);
- expect(snapshot.quota?.limits).toMatchObject([
- { type: "welcome-glm-52", planId: "start-plan-welcome" },
- ]);
- });
- it("Z.ai Start Plan 乱序穿插非active套餐时保持服务端相对顺序", async () => {
- const apiClient: ApiClient = {
- request: vi.fn(async () =>
- createJsonResponse({
- code: 0,
- msg: "ok",
- data: {
- plans: [
- {
- plan_id: "start-plan-expired-a",
- name: "Expired A",
- status: "expired",
- ends_at: 1,
- },
- {
- plan_id: "start-plan-active-1",
- name: "Active One",
- status: "active",
- ends_at: 1_781_500_800,
- },
- {
- plan_id: "start-plan-expired-b",
- name: "Expired B",
- status: "cancelled",
- ends_at: 1,
- },
- {
- plan_id: "start-plan-active-2",
- name: "Active Two",
- status: "active",
- ends_at: 1_782_105_600,
- },
- ],
- balances: [
- {
- plan_id: "start-plan-active-1",
- entitlement_id: "active-1-glm-52",
- show_name: "GLM-5.2",
- capabilities: ["model:glm-5.2"],
- total_units: 3_000_000,
- remaining_units: 2_500_000,
- expires_at: 1_780_506_000,
- },
- {
- plan_id: "start-plan-active-2",
- entitlement_id: "active-2-glm-5-turbo",
- show_name: "GLM-5-Turbo",
- capabilities: ["model:glm-5-turbo"],
- total_units: 1_000_000,
- remaining_units: 750_000,
- expires_at: 1_781_110_800,
- },
- ],
- },
- }),
- ),
- };
- const provider = createProviderForTest({
- apiClient,
- modelProviderService: {
- getAllCached: vi.fn(async () => [createZaiStartPlanProvider()]),
- } as LegacyModelProviderReader,
- credentialService: {
- load: vi.fn(async () => "credential-zcode-jwt"),
- },
- });
- const snapshot = await provider.getSnapshotForRequest({
- preferredProviderId: BUILTIN_MODEL_PROVIDER_IDS.zaiStartPlan,
- requirePreferredProvider: true,
- });
- expect(snapshot.subscription?.details?.map((detail) => detail.productId)).toEqual([
- "start-plan-active-1",
- "start-plan-active-2",
- ]);
- });
- it("Z.ai Start Plan 桶缺失 expires_at 时 nextResetTime 为 undefined 且不抛错", async () => {
- const apiClient: ApiClient = {
- request: vi.fn(async () =>
- createJsonResponse({
- code: 0,
- msg: "ok",
- data: {
- plans: [
- {
- plan_id: "zcode-v3-start-plan",
- name: "ZCode V3 Start Plan",
- status: "active",
- ends_at: 1_781_500_800,
- },
- ],
- balances: [
- {
- plan_id: "zcode-v3-start-plan",
- entitlement_id: "ent_zcode_v3_glm_52",
- show_name: "GLM-5.2",
- capabilities: ["model:glm-5.2"],
- total_units: 3_000_000,
- remaining_units: 2_500_000,
- // 服务端契约保证 expires_at 存在;缺失时桶刷新时间退化为 undefined,
- // 快照构建不能抛错。
- },
- ],
- },
- }),
- ),
- };
- const provider = createProviderForTest({
- apiClient,
- modelProviderService: {
- getAllCached: vi.fn(async () => [createZaiStartPlanProvider()]),
- } as LegacyModelProviderReader,
- credentialService: {
- load: vi.fn(async () => "credential-zcode-jwt"),
- },
- });
- const snapshot = await provider.getSnapshotForRequest({
- preferredProviderId: BUILTIN_MODEL_PROVIDER_IDS.zaiStartPlan,
- requirePreferredProvider: true,
- });
- expect(snapshot.quota?.limits).toMatchObject([
- {
- type: "ent_zcode_v3_glm_52",
- remaining: 2_500_000,
- nextResetTime: undefined,
- },
- ]);
- expect(snapshot.remaining).toMatchObject({ count: 2_500_000 });
- });
- it("Z.ai Start Plan 余额展示优先使用 remaining_units 而不是 available_units", async () => {
- const apiClient: ApiClient = {
- request: vi.fn(async (input) => {
- const url = input.toString();
- expect(url).toContain("/billing/balance");
- return createJsonResponse({
- code: 0,
- msg: "ok",
- data: {
- plans: [
- {
- plan_id: "zcode-v3-start-plan",
- name: "ZCode V3 Start Plan",
- status: "active",
- },
- ],
- balances: [
- {
- entitlement_id: "ent_zcode_v3_glm_52",
- show_name: "GLM-5.2",
- capabilities: ["model:glm-5.2"],
- total_units: 3_000_000,
- used_units: 266_000,
- reserved_units: 1_775_000,
- remaining_units: 2_734_000,
- available_units: 959_000,
- expires_at: 1_780_502_400,
- },
- ],
- },
- });
- }),
- };
- const provider = createProviderForTest({
- apiClient,
- modelProviderService: {
- getAllCached: vi.fn(async () => [createZaiStartPlanProvider()]),
- } as LegacyModelProviderReader,
- credentialService: {
- load: vi.fn(async (key: string) =>
- key === "zcodejwttoken" ? "credential-zcode-jwt" : null,
- ),
- },
- });
- const snapshot = await provider.getSnapshotForRequest({
- preferredProviderId: BUILTIN_MODEL_PROVIDER_IDS.zaiStartPlan,
- requirePreferredProvider: true,
- });
- expect(snapshot.quota?.limits[0]).toMatchObject({
- number: 3_000_000,
- usage: 266_000,
- remaining: 2_734_000,
- percentage: 2_734_000 / 3_000_000,
- });
- expect(snapshot.remaining).toMatchObject({
- count: 2_734_000,
- percentage: 2_734_000 / 3_000_000,
- });
- });
- it("Z.ai Start Plan 只请求一次 billing/balance 并从 plans 判定套餐", async () => {
- const balanceUrl = `https://zcode.z.ai/api/v1/zcode-plan/billing/balance?app_version=${ZCODE_VERSION}`;
- const requestOrder: string[] = [];
- const apiClient: ApiClient = {
- request: vi.fn(async (input) => {
- const url = input.toString();
- expect(url).toBe(balanceUrl);
- requestOrder.push("balance");
- return createJsonResponse({
- code: 0,
- msg: "ok",
- data: {
- plans: [
- {
- plan_id: "zcode-v3-start-plan",
- name: "ZCode V3 Start Plan",
- status: "active",
- starts_at: 1_787_900_000,
- entitlements: [
- {
- entitlement_id: "ent_zcode_v3_glm_52",
- show_name: "GLM-5.2",
- period: "one_time",
- effective_at: 1_788_000_000,
- },
- ],
- },
- ],
- balances: [
- {
- entitlement_id: "ent_zcode_v3_glm_52",
- show_name: "GLM-5.2",
- capabilities: ["model:glm-5.2"],
- total_units: 100,
- used_units: 25,
- remaining_units: 75,
- },
- ],
- },
- });
- }),
- };
- const provider = createProviderForTest({
- apiClient,
- modelProviderService: {
- getAllCached: vi.fn(async () => [createZaiStartPlanProvider()]),
- } as LegacyModelProviderReader,
- credentialService: {
- load: vi.fn(async (key: string) =>
- key === "zcodejwttoken" ? "credential-zcode-jwt" : null,
- ),
- },
- });
- const snapshot = await provider.getSnapshotForRequest({
- preferredProviderId: BUILTIN_MODEL_PROVIDER_IDS.zaiStartPlan,
- requirePreferredProvider: true,
- });
- expect(requestOrder).toEqual(["balance"]);
- expect(apiClient.request).toHaveBeenCalledTimes(1);
- expect(snapshot.quota?.limits[0]).toMatchObject({
- type: "ent_zcode_v3_glm_52",
- remaining: 75,
- });
- expect(snapshot.subscription?.details[0]?.entitlements).toEqual([
- {
- entitlementId: "ent_zcode_v3_glm_52",
- showName: "GLM-5.2",
- effectiveTime: new Date(1_788_000_000 * 1_000).toISOString(),
- },
- ]);
- });
- it("Z.ai Start Plan 缺少 zcodejwttoken 时不调用远端状态接口", async () => {
- const apiClient: ApiClient = {
- request: vi.fn(async () =>
- createJsonResponse({
- code: 0,
- data: { plans: [] },
- }),
- ),
- };
- const provider = createProviderForTest({
- apiClient,
- modelProviderService: {
- getAllCached: vi.fn(async () => [
- {
- ...createZaiStartPlanProvider(),
- apiKey: "",
- },
- ]),
- } as LegacyModelProviderReader,
- credentialService: {
- load: vi.fn(async () => null),
- },
- });
- const snapshot = await provider.getSnapshotForRequest({
- preferredProviderId: BUILTIN_MODEL_PROVIDER_IDS.zaiStartPlan,
- requirePreferredProvider: true,
- });
- expect(apiClient.request).not.toHaveBeenCalled();
- expect(snapshot.unavailableReason).toBe("not_configured");
- expect(snapshot.provider).toBeNull();
- });
- it("BigModel Start Plan 使用 OAuth callback 已落盘的 zcode JWT 读取当前套餐", async () => {
- const requestHeaders: Array<HeadersInit | undefined> = [];
- const apiClient: ApiClient = {
- request: vi.fn(async (input, init) => {
- const url = input.toString();
- requestHeaders.push(init?.headers);
- expect(url).toContain("/billing/balance");
- return createJsonResponse({
- code: 0,
- msg: "ok",
- data: {
- plans: [
- {
- plan_id: "zcode-v3-start-plan",
- name: "ZCode V3 Start Plan",
- status: "active",
- starts_at: 1_780_291_200,
- ends_at: 1_781_500_800,
- },
- ],
- balances: [
- {
- entitlement_id: "ent_bigmodel_glm_52",
- show_name: "GLM-5.2",
- capabilities: ["model:glm-5.2"],
- total_units: 100,
- used_units: 25,
- remaining_units: 75,
- expires_at: 1_780_502_400,
- },
- {
- entitlement_id: "ent_bigmodel_glm_5turbo",
- show_name: "GLM-5-Turbo",
- capabilities: ["model:glm-5-turbo"],
- total_units: 50,
- used_units: 0,
- remaining_units: 50,
- expires_at: 1_780_502_400,
- },
- ],
- },
- });
- }),
- };
- const provider = createProviderForTest({
- apiClient,
- modelProviderService: {
- getAllCached: vi.fn(async () => [
- {
- ...createZaiStartPlanProvider(),
- id: BUILTIN_MODEL_PROVIDER_IDS.bigmodelStartPlan,
- name: "BigModel- Coding Plan",
- apiKey: "stale-bigmodel-token",
- },
- ]),
- } as LegacyModelProviderReader,
- credentialService: {
- load: vi.fn(async (key: string) =>
- key === "oauth:active_provider"
- ? BIGMODEL_PROVIDER_ID
- : key === "zcodejwttoken"
- ? "bigmodel-zcode-jwt"
- : null,
- ),
- },
- });
- const snapshot = await provider.getSnapshotForRequest({
- preferredProviderId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelStartPlan,
- requirePreferredProvider: true,
- });
- expect(requestHeaders).toEqual([
- expect.objectContaining({ Authorization: "Bearer bigmodel-zcode-jwt" }),
- ]);
- expect(apiClient.request).toHaveBeenCalledTimes(1);
- expect(apiClient.request).not.toHaveBeenCalledWith(
- "https://zcode.z.ai/api/v1/oauth/token",
- expect.anything(),
- );
- expect(snapshot.provider).toMatchObject({
- id: BUILTIN_MODEL_PROVIDER_IDS.bigmodelStartPlan,
- name: "BigModel - Coding Plan",
- });
- expect(snapshot.quota?.limits[0]).toMatchObject({
- type: "ent_bigmodel_glm_52",
- remaining: 75,
- usageDetails: [{ modelCode: "glm-5.2", displayName: "GLM-5.2", usage: 25 }],
- });
- expect(snapshot.quota?.limits[1]).toMatchObject({
- type: "ent_bigmodel_glm_5turbo",
- remaining: 50,
- usageDetails: [{ modelCode: "glm-5-turbo", displayName: "GLM-5-Turbo", usage: 0 }],
- });
- });
- it("自动续费套餐用订阅列表的 nextRenewTime 推导续费时间", async () => {
- const requestUrls: string[] = [];
- const apiClient: ApiClient = {
- request: vi.fn(async (input, init) => {
- const url = input.toString();
- requestUrls.push(url);
- if (url.endsWith("/api/monitor/usage/quota/limit")) {
- expect(init?.headers).toMatchObject({
- authorization: "bigmodel-api-key",
- });
- return createJsonResponse({
- code: 200,
- data: {
- level: "pro",
- limits: [
- {
- type: "TIME_LIMIT",
- remaining: 5,
- percentage: 10,
- nextResetTime: Date.UTC(2026, 4, 26),
- },
- ],
- },
- });
- }
- expect(url).toBe("https://bigmodel.cn/api/biz/subscription/list");
- expect(init?.headers).toMatchObject({
- Authorization: "bigmodel-api-key",
- });
- return createJsonResponse({
- code: 200,
- data: [
- {
- productId: "product-pro-quarter",
- productName: "GLM Coding Pro",
- status: "VALID",
- inCurrentPeriod: true,
- autoRenew: 1,
- billingCycle: "monthly",
- nextRenewTime: "2026-06-26",
- valid: "2026-06-26 10:00:00-2026-09-26 10:00:00",
- },
- {
- productId: "product-pro-month",
- productName: "GLM Coding Pro",
- status: "VALID",
- inCurrentPeriod: false,
- nextRenewTime: "2026-07-26",
- valid: "2026-06-26 10:00:00-2026-07-26 10:00:00",
- },
- ],
- });
- }),
- };
- const provider = createProviderForTest({
- apiClient,
- modelProviderService: {
- getAllCached: vi.fn(async () => [createBigModelCodingPlanProvider()]),
- } as LegacyModelProviderReader,
- });
- const snapshot = await provider.getSnapshotForRequest({
- preferredProviderId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
- });
- expect(requestUrls).toEqual([
- "https://bigmodel.cn/api/biz/subscription/list",
- "https://bigmodel.cn/api/monitor/usage/quota/limit",
- ]);
- expect(snapshot.subscription?.details[0]).toMatchObject({
- productId: "product-pro-quarter",
- productName: "GLM Coding Pro",
- billingCycle: "monthly",
- renewTime: new Date("2026-06-26").toISOString(),
- expireTime: new Date("2026-09-26T10:00:00").toISOString(),
- });
- expect(snapshot.remaining?.nextResetTime).toBe(Date.UTC(2026, 4, 26));
- });
- it("缺少订阅时不会用 quota level 伪造商品和权益", async () => {
- const apiClient: ApiClient = {
- request: vi.fn(async (input) => {
- const url = input.toString();
- if (url.endsWith("/api/biz/subscription/list")) {
- return createJsonResponse({ code: 200, data: [] });
- }
- return createJsonResponse({
- code: 200,
- data: {
- level: "pro",
- limits: [
- {
- type: "TIME_LIMIT",
- remaining: 5,
- },
- ],
- },
- });
- }),
- };
- const provider = createProviderForTest({
- apiClient,
- modelProviderService: {
- getAllCached: vi.fn(async () => [createBigModelCodingPlanProvider()]),
- } as LegacyModelProviderReader,
- });
- const snapshot = await provider.getSnapshotForRequest({
- preferredProviderId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
- });
- expect(snapshot.subscription).toBeNull();
- expect(snapshot.unavailableReason).toBe("no_plan");
- expect(snapshot.quota?.level).toBe("pro");
- });
- it("quota 业务失败时保留已读取的 Coding Plan 订阅快照", async () => {
- const apiClient: ApiClient = {
- request: vi.fn(async (input) => {
- const url = input.toString();
- if (url.endsWith("/api/biz/subscription/list")) {
- return createJsonResponse({
- code: 200,
- data: [
- {
- productId: "product-pro",
- productName: "GLM Coding Pro",
- status: "VALID",
- inCurrentPeriod: true,
- },
- ],
- });
- }
- return createJsonResponse({ code: 500, msg: "quota unavailable" });
- }),
- };
- const provider = createProviderForTest({
- apiClient,
- modelProviderService: {
- getAllCached: vi.fn(async () => [createBigModelCodingPlanProvider()]),
- } as LegacyModelProviderReader,
- });
- const snapshot = await provider.getSnapshotForRequest({
- includeSubscription: true,
- preferredProviderId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
- });
- expect(snapshot).toMatchObject({
- provider: { id: BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan },
- subscription: {
- details: [{ productId: "product-pro", productName: "GLM Coding Pro" }],
- },
- quota: null,
- remaining: null,
- });
- expect(snapshot.unavailableReason).toBeUndefined();
- });
- it("ZCODE_ENV=test 时 BigModel Coding Plan quota 使用 bigmodel.cn", async () => {
- const requestUrls: string[] = [];
- const apiClient: ApiClient = {
- request: vi.fn(async (input) => {
- const url = input.toString();
- requestUrls.push(url);
- if (url.endsWith("/api/biz/subscription/list")) {
- return createJsonResponse({ code: 200, data: [] });
- }
- return createJsonResponse({
- code: 200,
- data: {
- level: "pro",
- limits: [],
- },
- });
- }),
- };
- const provider = createProviderForTest({
- apiClient,
- env: {
- ZCODE_ENV: "test",
- },
- modelProviderService: {
- getAllCached: vi.fn(async () => [createBigModelCodingPlanProvider()]),
- } as LegacyModelProviderReader,
- });
- await provider.getSnapshotForRequest({
- preferredProviderId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
- });
- expect(requestUrls).toEqual([
- "https://bigmodel.cn/api/biz/subscription/list",
- "https://bigmodel.cn/api/monitor/usage/quota/limit",
- ]);
- });
- it("BigModel Team Plan 权益查询会携带组织和项目 header", async () => {
- const requestHeaders: Array<HeadersInit | undefined> = [];
- const requestUrls: string[] = [];
- const apiClient: ApiClient = {
- request: vi.fn(async (input, init) => {
- requestHeaders.push(init?.headers);
- const url = input.toString();
- requestUrls.push(url);
- if (url.endsWith("/api_keys")) {
- return createJsonResponse({
- code: 200,
- data: [{ apiKey: "team-ak", keyType: 2, name: "zcode-team-api-key" }],
- });
- }
- if (url.endsWith("/api_keys/copy/team-ak")) {
- return createJsonResponse({
- code: 200,
- data: { secretKey: "team-secret" },
- });
- }
- if (url.endsWith("/api/biz/team/subscribe/product/querySubscribeDetail")) {
- return createJsonResponse({
- code: 200,
- data: {
- hasSubscription: true,
- status: "EFFECTIVE",
- memberGrantStatus: "VALID",
- productId: "team-max",
- productName: "团队套餐",
- },
- });
- }
- expect(url).toBe("https://bigmodel.cn/api/monitor/usage/quota/limit?type=2");
- return createJsonResponse({
- code: 200,
- data: {
- level: "team",
- limits: [],
- },
- });
- }),
- };
- const provider = createProviderForTest({
- apiClient,
- modelProviderService: {
- getAllCached: vi.fn(async () => [createBigModelCodingPlanProvider()]),
- } as LegacyModelProviderReader,
- credentialService: {
- load: vi.fn(async (key) => (key === "oauth:bigmodel:access_token" ? "oauth-token" : null)),
- },
- });
- await provider.getSnapshotForRequest({
- preferredProviderId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
- organizationId: "org-team-a",
- projectId: "proj-team-a",
- });
- expect(requestUrls.some((url) => url.includes("/api_keys"))).toBe(false);
- expect(requestHeaders[0]).toMatchObject({
- Authorization: "oauth-token",
- "bigmodel-organization": "org-team-a",
- "bigmodel-project": "proj-team-a",
- });
- expect(requestHeaders.slice(-2)).toEqual([
- expect.objectContaining({
- Authorization: "oauth-token",
- "bigmodel-organization": "org-team-a",
- "bigmodel-project": "proj-team-a",
- }),
- expect.objectContaining({
- authorization: "team-ak.team-secret",
- "bigmodel-organization": "org-team-a",
- "bigmodel-project": "proj-team-a",
- }),
- ]);
- expect(requestUrls).toEqual([
- "https://bigmodel.cn/api/biz/team/subscribe/product/querySubscribeDetail",
- "https://bigmodel.cn/api/monitor/usage/quota/limit?type=2",
- ]);
- });
- it("BigModel Team Plan 的用量层不再创建项目 API Key", async () => {
- const requestHeaders: Array<HeadersInit | undefined> = [];
- const requestMethods: Array<string | undefined> = [];
- const requestUrls: string[] = [];
- const apiClient: ApiClient = {
- request: vi.fn(async (input, init) => {
- const url = input.toString();
- requestHeaders.push(init?.headers);
- requestMethods.push(init?.method);
- requestUrls.push(url);
- if (url.endsWith("/api_keys") && init?.method === "GET") {
- return createJsonResponse({
- code: 200,
- data: [
- {
- apiKey: "wrong-type-ak",
- keyType: 1,
- name: "zcode-team-api-key",
- },
- { name: "zcode-api-key" },
- ],
- });
- }
- if (url.endsWith("/api_keys") && init?.method === "POST") {
- expect(init.headers).toMatchObject({
- "bigmodel-organization": "org-team-a",
- "bigmodel-project": "proj-team-a",
- });
- expect(init.body).toBe(JSON.stringify({ name: "zcode-team-api-key", keyType: 2 }));
- return createJsonResponse({
- code: 200,
- data: {
- apiKey: "created-team-ak",
- keyType: 2,
- name: "zcode-team-api-key",
- },
- });
- }
- if (url.endsWith("/api_keys/copy/created-team-ak")) {
- return createJsonResponse({
- code: 200,
- data: { secretKey: "created-team-secret" },
- });
- }
- if (url.endsWith("/api/biz/team/subscribe/product/querySubscribeDetail")) {
- return createJsonResponse({
- code: 200,
- data: {
- hasSubscription: true,
- status: "EFFECTIVE",
- memberGrantStatus: "VALID",
- productId: "team-max",
- productName: "团队套餐",
- },
- });
- }
- if (url.endsWith("/api/monitor/usage/quota/limit?type=2")) {
- return createJsonResponse({
- code: 200,
- data: {
- level: "team",
- limits: [],
- },
- });
- }
- throw new Error(`unexpected request: ${url}`);
- }),
- };
- const provider = createProviderForTest({
- apiClient,
- modelProviderService: {
- getAllCached: vi.fn(async () => [createBigModelCodingPlanProvider()]),
- } as LegacyModelProviderReader,
- credentialService: {
- load: vi.fn(async (key) => (key === "oauth:bigmodel:access_token" ? "oauth-token" : null)),
- },
- });
- const snapshot = await provider.getSnapshotForRequest({
- preferredProviderId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
- organizationId: "org-team-a",
- projectId: "proj-team-a",
- });
- expect(requestMethods).toEqual(["GET", "GET"]);
- expect(requestUrls).toEqual([
- "https://bigmodel.cn/api/biz/team/subscribe/product/querySubscribeDetail",
- "https://bigmodel.cn/api/monitor/usage/quota/limit?type=2",
- ]);
- expect(requestHeaders.at(-1)).toMatchObject({
- authorization: "team-ak.team-secret",
- "bigmodel-organization": "org-team-a",
- "bigmodel-project": "proj-team-a",
- });
- expect(snapshot).toMatchObject({
- context: {
- scope: "team",
- organizationId: "org-team-a",
- projectId: "proj-team-a",
- },
- provider: {
- id: BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
- },
- });
- });
- it("BigModel Team Plan quota 使用 type=2 且不回退企业余额", async () => {
- const requestUrls: string[] = [];
- const apiClient: ApiClient = {
- request: vi.fn(async (input) => {
- const url = input.toString();
- requestUrls.push(url);
- if (url.endsWith("/api_keys")) {
- return createJsonResponse({
- code: 200,
- data: [{ apiKey: "team-ak", keyType: 2, name: "zcode-team-api-key" }],
- });
- }
- if (url.endsWith("/api_keys/copy/team-ak")) {
- return createJsonResponse({
- code: 200,
- data: { secretKey: "team-secret" },
- });
- }
- if (url.endsWith("/api/biz/team/subscribe/product/querySubscribeDetail")) {
- return createJsonResponse({
- code: 200,
- data: {
- hasSubscription: true,
- status: "EFFECTIVE",
- memberGrantStatus: "VALID",
- productId: "team-max",
- productName: "团队套餐",
- },
- });
- }
- if (url === "https://bigmodel.cn/api/monitor/usage/quota/limit?type=2") {
- return createJsonResponse({
- code: 200,
- data: {
- level: "team",
- limits: [
- {
- type: "TIME_LIMIT",
- remaining: 1000,
- currentValue: 0,
- percentage: 0,
- },
- ],
- },
- });
- }
- return createJsonResponse({
- code: 500,
- success: false,
- msg: "当前用户不存在coding plan",
- });
- }),
- };
- const provider = createProviderForTest({
- apiClient,
- modelProviderService: {
- getAllCached: vi.fn(async () => [createBigModelCodingPlanProvider()]),
- } as LegacyModelProviderReader,
- credentialService: {
- load: vi.fn(async (key) => (key === "oauth:bigmodel:access_token" ? "oauth-token" : null)),
- },
- });
- const snapshot = await provider.getSnapshotForRequest({
- preferredProviderId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
- organizationId: "org-team-a",
- projectId: "proj-team-a",
- });
- expect(snapshot.unavailableReason).toBeUndefined();
- expect(snapshot.quota?.limits[0]).toMatchObject({
- type: "TIME_LIMIT",
- remaining: 1000,
- percentage: 0,
- });
- expect(
- requestUrls.some((url) => url.endsWith("/api/biz/subscription/enterprise/v2/balance")),
- ).toBe(false);
- });
- it("没有 Coding Plan apiKey 时不调用 quota 和套餐预览接口", async () => {
- const apiClient: ApiClient = {
- request: vi.fn(async () =>
- createJsonResponse({
- code: 200,
- data: {
- level: "pro",
- limits: [
- {
- type: "TIME_LIMIT",
- remaining: 5,
- nextResetTime: Date.UTC(2026, 4, 26),
- },
- ],
- },
- }),
- ),
- };
- const provider = createProviderForTest({
- apiClient,
- modelProviderService: {
- getAllCached: vi.fn(async () => [
- {
- ...createBigModelCodingPlanProvider(),
- apiKey: "",
- },
- ]),
- } as LegacyModelProviderReader,
- accountRequestAuthService: {
- resolveAccessCurrent: vi.fn(async () => null),
- resolveCurrent: vi.fn(async () => ({ apiKey: undefined })),
- assertCurrent: vi.fn(async () => undefined),
- },
- });
- const snapshot = await provider.getSnapshotForRequest({
- preferredProviderId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
- requirePreferredProvider: true,
- allowEnvApiKey: false,
- });
- expect(apiClient.request).not.toHaveBeenCalled();
- expect(snapshot.unavailableReason).toBe("not_configured");
- expect(snapshot.provider).toBeNull();
- });
- it("严格指定 Coding Plan 时不会回退到普通 API Key provider", async () => {
- const apiClient: ApiClient = {
- request: vi.fn(),
- };
- const provider = createProviderForTest({
- apiClient,
- modelProviderService: {
- getAllCached: vi.fn(async () => [
- {
- ...createBigModelCodingPlanProvider(),
- apiKey: "",
- },
- createBigModelApiKeyProvider(),
- ]),
- } as LegacyModelProviderReader,
- });
- const snapshot = await provider.getSnapshotForRequest({
- preferredProviderId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
- requirePreferredProvider: true,
- allowEnvApiKey: false,
- });
- expect(apiClient.request).not.toHaveBeenCalled();
- expect(snapshot.unavailableReason).toBe("not_configured");
- expect(snapshot.provider).toBeNull();
- });
- it("严格指定 Coding Plan 时不会被环境变量 usage key 覆盖", async () => {
- const apiClient: ApiClient = {
- request: vi.fn(async (_input, init) => {
- if (String(_input).includes("subscription/list")) {
- expect(init?.headers).toMatchObject({ Authorization: "bigmodel-api-key" });
- return createJsonResponse({ code: 200, data: [] });
- }
- expect(init?.headers).toMatchObject({
- authorization: "bigmodel-api-key",
- });
- return createJsonResponse({
- code: 200,
- data: {
- level: "pro",
- limits: [],
- },
- });
- }),
- };
- const provider = createProviderForTest({
- apiClient,
- modelProviderService: {
- getAllCached: vi.fn(async () => [createBigModelCodingPlanProvider()]),
- } as LegacyModelProviderReader,
- env: {
- ZCODE_BIGMODEL_USAGE_API_KEY: "env-usage-key",
- },
- });
- const snapshot = await provider.getSnapshotForRequest({
- preferredProviderId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
- requirePreferredProvider: true,
- includeSubscription: false,
- });
- expect(apiClient.request).toHaveBeenCalledTimes(2);
- expect(snapshot.provider).toMatchObject({
- id: BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
- });
- });
- it("Z.ai Coding Plan 用量权益直接使用 provider apiKey 请求 quota", async () => {
- const requestUrls: string[] = [];
- const apiClient: ApiClient = {
- request: vi.fn(async (input, init) => {
- const url = input.toString();
- requestUrls.push(url);
- if (url.includes("subscription/list")) {
- expect(init?.headers).toMatchObject({ Authorization: "zai-api-key" });
- return createJsonResponse({ code: 200, data: [] });
- }
- expect(url).toBe("https://api.z.ai/api/monitor/usage/quota/limit");
- expect(init?.headers).toMatchObject({
- authorization: "zai-api-key",
- });
- return createJsonResponse({
- code: 200,
- data: {
- level: "pro",
- limits: [],
- },
- });
- }),
- };
- const provider = createProviderForTest({
- apiClient,
- modelProviderService: {
- getAllCached: vi.fn(async () => [createZaiCodingPlanProvider()]),
- } as LegacyModelProviderReader,
- });
- const snapshot = await provider.getSnapshotForRequest({
- preferredProviderId: BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
- requirePreferredProvider: true,
- includeSubscription: false,
- });
- expect(requestUrls).toEqual([
- "https://api.z.ai/api/biz/subscription/list",
- "https://api.z.ai/api/monitor/usage/quota/limit",
- ]);
- expect(snapshot.provider).toMatchObject({
- id: BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
- });
- });
- it("Z.ai Coding Plan 团队授权不可用时不会回退个人 key 发送 team quota header", async () => {
- const requestHeaders: Array<HeadersInit | undefined> = [];
- const apiClient: ApiClient = {
- request: vi.fn(async (_input, init) => {
- requestHeaders.push(init?.headers);
- return createJsonResponse({
- code: 200,
- data: {
- level: "pro",
- limits: [],
- },
- });
- }),
- };
- const provider = createProviderForTest({
- apiClient,
- modelProviderService: {
- getAllCached: vi.fn(async () => [createZaiCodingPlanProvider()]),
- } as LegacyModelProviderReader,
- accountRequestAuthService: {
- resolveAccessCurrent: vi.fn(async () => null),
- resolveCurrent: vi.fn(async () => ({ apiKey: undefined })),
- assertCurrent: vi.fn(async () => undefined),
- },
- });
- await expect(
- provider.getSnapshotForRequest({
- preferredProviderId: BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
- requirePreferredProvider: true,
- includeSubscription: false,
- organizationId: "org-should-ignore",
- projectId: "proj-should-ignore",
- }),
- ).rejects.toThrow("Coding Plan entitlement refresh failed");
- // 修复原因:误传 team context 时不能把个人 Z.ai Coding Plan key 和 team header 混用。
- // 团队业务登录态和调用 Key 均缺失时权益未知,不能回退个人 Key。
- expect(requestHeaders).toEqual([]);
- });
- it("Z.ai Coding Plan 用 provider apiKey 读取订阅状态和到期时间", async () => {
- const requestUrls: string[] = [];
- const apiClient: ApiClient = {
- request: vi.fn(async (input, init) => {
- const url = input.toString();
- requestUrls.push(url);
- if (url.endsWith("/api/monitor/usage/quota/limit")) {
- expect(init?.headers).toMatchObject({
- authorization: "zai-api-key",
- });
- return createJsonResponse({
- code: 200,
- data: {
- level: "pro",
- limits: [],
- },
- });
- }
- expect(url).toBe("https://api.z.ai/api/biz/subscription/list");
- expect(init?.headers).toMatchObject({
- Authorization: "zai-api-key",
- });
- return createJsonResponse({
- code: 200,
- data: [
- {
- productId: "product-zai-pro-quarter",
- productName: "GLM Coding Pro",
- status: "VALID",
- inCurrentPeriod: true,
- billingCycle: "quarterly",
- nextRenewTime: "2026-07-10",
- valid: "2026-04-10 16:32:00-2026-07-10 10:00:00",
- },
- ],
- });
- }),
- };
- const provider = createProviderForTest({
- apiClient,
- modelProviderService: {
- getAllCached: vi.fn(async () => [createZaiCodingPlanProvider()]),
- } as LegacyModelProviderReader,
- });
- const snapshot = await provider.getSnapshotForRequest({
- preferredProviderId: BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
- requirePreferredProvider: true,
- });
- expect(requestUrls).toEqual([
- "https://api.z.ai/api/biz/subscription/list",
- "https://api.z.ai/api/monitor/usage/quota/limit",
- ]);
- expect(snapshot.subscription?.details[0]).toMatchObject({
- productId: "product-zai-pro-quarter",
- productName: "GLM Coding Pro",
- billingCycle: "quarterly",
- renewTime: null,
- expireTime: new Date("2026-07-10").toISOString(),
- });
- });
- it("Z.ai Coding Plan quota 失败时仍用订阅列表判断已购买状态", 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/list")) {
- expect(init?.headers).toMatchObject({
- Authorization: "zai-api-key",
- });
- return createJsonResponse({
- code: 200,
- data: [
- {
- productId: "product-zai-pro-quarter",
- productName: "GLM Coding Pro",
- status: "VALID",
- inCurrentPeriod: true,
- billingCycle: "quarterly",
- nextRenewTime: "2026-07-10",
- },
- ],
- });
- }
- expect(url).toBe("https://api.z.ai/api/monitor/usage/quota/limit");
- expect(init?.headers).toMatchObject({
- authorization: "zai-api-key",
- });
- return createJsonResponse({
- code: 401,
- msg: "token expired or incorrect",
- success: false,
- });
- }),
- };
- const provider = createProviderForTest({
- apiClient,
- modelProviderService: {
- getAllCached: vi.fn(async () => [createZaiCodingPlanProvider()]),
- } as LegacyModelProviderReader,
- });
- const snapshot = await provider.getSnapshotForRequest({
- preferredProviderId: BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
- requirePreferredProvider: true,
- });
- expect(requestUrls).toEqual([
- "https://api.z.ai/api/biz/subscription/list",
- "https://api.z.ai/api/monitor/usage/quota/limit",
- ]);
- expect(snapshot.unavailableReason).toBeUndefined();
- expect(snapshot.subscription?.details[0]).toMatchObject({
- productId: "product-zai-pro-quarter",
- productName: "GLM Coding Pro",
- billingCycle: "quarterly",
- renewTime: null,
- expireTime: new Date("2026-07-10").toISOString(),
- });
- expect(snapshot.quota).toBeNull();
- });
- it("Z.ai Coding Plan 订阅列表有效时不被 quota no_plan 覆盖", async () => {
- const apiClient: ApiClient = {
- request: vi.fn(async (input, init) => {
- const url = input.toString();
- if (url.endsWith("/api/biz/subscription/list")) {
- expect(init?.headers).toMatchObject({
- Authorization: "zai-api-key",
- });
- return createJsonResponse({
- code: 200,
- data: [
- {
- productId: "product-zai-pro-monthly",
- productName: "GLM Coding Pro",
- status: "VALID",
- inCurrentPeriod: true,
- billingCycle: "monthly",
- nextRenewTime: "2026-07-10",
- },
- ],
- });
- }
- expect(url).toBe("https://api.z.ai/api/monitor/usage/quota/limit");
- expect(init?.headers).toMatchObject({
- authorization: "zai-api-key",
- });
- return createJsonResponse({
- code: 500,
- msg: "不存在coding plan",
- success: false,
- });
- }),
- };
- const provider = createProviderForTest({
- apiClient,
- modelProviderService: {
- getAllCached: vi.fn(async () => [createZaiCodingPlanProvider()]),
- } as LegacyModelProviderReader,
- });
- const snapshot = await provider.getSnapshotForRequest({
- preferredProviderId: BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
- requirePreferredProvider: true,
- });
- expect(snapshot.unavailableReason).toBeUndefined();
- expect(snapshot.subscription?.details[0]).toMatchObject({
- productId: "product-zai-pro-monthly",
- productName: "GLM Coding Pro",
- });
- expect(snapshot.quota).toBeNull();
- });
- it("Z.ai Coding Plan token 失效时返回同步失败而不是未开通", async () => {
- const apiClient: ApiClient = {
- request: vi.fn(async (input, init) => {
- expect(input.toString()).toBe("https://api.z.ai/api/biz/subscription/list");
- expect(init?.headers).toMatchObject({ Authorization: "zai-api-key" });
- return createJsonResponse({
- code: 401,
- msg: "token expired or incorrect",
- success: false,
- });
- }),
- };
- const provider = createProviderForTest({
- apiClient,
- modelProviderService: {
- getAllCached: vi.fn(async () => [createZaiCodingPlanProvider()]),
- } as LegacyModelProviderReader,
- });
- await expect(
- provider.getSnapshotForRequest({
- preferredProviderId: BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
- requirePreferredProvider: true,
- includeSubscription: false,
- }),
- ).rejects.toThrow("Coding Plan entitlement refresh failed");
- });
- it("Z.ai Coding Plan provider 未同步时不会回退 OAuth token 查询权益", async () => {
- const apiClient: ApiClient = {
- request: vi.fn(),
- };
- const provider = createProviderForTest({
- apiClient,
- modelProviderService: {
- getAllCached: vi.fn(async () => []),
- } as LegacyModelProviderReader,
- credentialService: {
- load: vi.fn(async () => "zai-oauth-token"),
- },
- });
- const snapshot = await provider.getSnapshotForRequest({
- preferredProviderId: BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
- requirePreferredProvider: true,
- includeSubscription: false,
- });
- expect(apiClient.request).not.toHaveBeenCalled();
- expect(snapshot.unavailableReason).toBe("not_configured");
- expect(snapshot.provider).toBeNull();
- });
- it("Z.ai Coding Plan 空 apiKey 时按未配置权益处理且不回退 OAuth token", async () => {
- const apiClient: ApiClient = {
- request: vi.fn(),
- };
- const provider = createProviderForTest({
- apiClient,
- modelProviderService: {
- getAllCached: vi.fn(async () => [
- {
- ...createZaiCodingPlanProvider(),
- apiKey: "",
- },
- ]),
- } as LegacyModelProviderReader,
- credentialService: {
- load: vi.fn(async () => "zai-oauth-token"),
- },
- });
- const snapshot = await provider.getSnapshotForRequest({
- preferredProviderId: BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
- requirePreferredProvider: true,
- includeSubscription: false,
- });
- expect(apiClient.request).not.toHaveBeenCalled();
- expect(snapshot.unavailableReason).toBe("not_configured");
- expect(snapshot.provider).toBeNull();
- });
- });
|