| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848 |
- 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 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: "团队套餐",
- },
- });
- }
- 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(requestHeaders.at(-1)).toMatchObject({
- authorization: "team-ak.team-secret",
- "bigmodel-organization": "org-team-a",
- "bigmodel-project": "proj-team-a",
- });
- expect(requestUrls.at(-1)).toBe("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();
- });
- });
|