| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067 |
- import {
- BIGMODEL_PROVIDER_ID,
- BUILTIN_MODEL_PROVIDER_IDS,
- getModelProviderFamilySpec,
- ZAI_PROVIDER_ID,
- type UsageEntitlementSnapshot,
- } from "@zcode/shared";
- import type { ProviderSettingsFormProvider } from "@/lib/providerSettingsFormTypes.js";
- import { describe, expect, it } from "vitest";
- import {
- buildVisibleFamilyConnectionItems,
- buildVisibleFamilyConnectionKeys,
- resolveCodingPlanEntitlementState,
- } from "@/settings/model-provider-section/providerFamilyConnectionVisibility.js";
- import {
- CODING_PLAN_PROVIDER_SPECS,
- type CodingPlanStatus,
- type ModelProviderNavGroup,
- } from "@/settings/model-provider-section/constants.js";
- type ProviderOverrides = Partial<ProviderSettingsFormProvider> & {
- id?: string;
- name?: string;
- apiKey?: string;
- models?: string[];
- };
- it("待生效套餐可查看订阅,不借 entitled 点亮执行资格", () => {
- const result = resolveCodingPlanEntitlementState({
- providerId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelStartPlan,
- accountEntitled: false,
- accountAvailability: "pending",
- modelProvidersLoading: false,
- entitlement: {
- loading: false,
- error: null,
- snapshot: {
- subscription: {
- details: [{ productId: "start", productName: "Start Plan", effectiveAt: 2000 }],
- },
- } as UsageEntitlementSnapshot,
- },
- });
- expect(result.status).toBe("purchased");
- expect(result.currentProductId).toBe("start");
- });
- it("已登录但服务端明确无个人订阅时显示未开通,而不是未连接", () => {
- const result = resolveCodingPlanEntitlementState({
- providerId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
- accountEntitled: false,
- accountAvailability: "unavailable",
- accountUnavailableReason: "not-entitled",
- modelProvidersLoading: false,
- });
- expect(result.status).toBe("notPurchased");
- });
- it("凭据失效时显示获取失败,既不显示未连接也不显示未开通", () => {
- const result = resolveCodingPlanEntitlementState({
- providerId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
- accountEntitled: false,
- accountAvailability: "unavailable",
- accountUnavailableReason: "credential-failed",
- modelProvidersLoading: false,
- });
- expect(result.status).toBe("unavailable");
- });
- it.each(["not-connected", "not-authenticated", undefined] as const)(
- "未连接 / 未登录 / 原因缺失时仍显示未连接(%s)",
- (accountUnavailableReason) => {
- const result = resolveCodingPlanEntitlementState({
- providerId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
- accountEntitled: false,
- accountAvailability: "unavailable",
- accountUnavailableReason,
- modelProvidersLoading: false,
- });
- expect(result.status).toBe("disconnected");
- },
- );
- it("原因未知时保持未连接,不把网络抖动当成未开通", () => {
- const result = resolveCodingPlanEntitlementState({
- providerId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
- accountEntitled: false,
- accountAvailability: "unknown",
- accountUnavailableReason: "not-entitled",
- modelProvidersLoading: false,
- });
- expect(result.status).toBe("disconnected");
- });
- it.each([
- BUILTIN_MODEL_PROVIDER_IDS.bigmodelStartPlan,
- BUILTIN_MODEL_PROVIDER_IDS.zaiStartPlan,
- ] as const)("Start Plan 的明确无权益显示暂无套餐(%s)", (providerId) => {
- const result = resolveCodingPlanEntitlementState({
- providerId,
- accountEntitled: false,
- accountAvailability: "unavailable",
- accountUnavailableReason: "not-entitled",
- modelProvidersLoading: false,
- });
- expect(result.status).toBe("notPurchased");
- });
- it("Z.AI 个人套餐与服务端无权益判定对称", () => {
- const result = resolveCodingPlanEntitlementState({
- providerId: BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
- accountEntitled: false,
- accountAvailability: "unavailable",
- accountUnavailableReason: "not-entitled",
- modelProvidersLoading: false,
- });
- expect(result.status).toBe("notPurchased");
- });
- function createProvider(overrides: ProviderOverrides): ProviderSettingsFormProvider {
- const providerId =
- overrides.providerId ?? overrides.id ?? BUILTIN_MODEL_PROVIDER_IDS.bigmodelStartPlan;
- const modelIds = overrides.models ?? ["glm-5"];
- return {
- providerId,
- executable: overrides.executable ?? true,
- enabled: overrides.enabled ?? true,
- hasPersonalConfig: false,
- personalConfig: {},
- config: {
- label: overrides.name ?? "BigModel",
- access: { type: "api-key", apiKey: overrides.apiKey ?? "sk-test" },
- api: { type: "anthropic-messages", baseUrl: "https://example.com" },
- builtinModelIds: modelIds,
- enabled: overrides.enabled ?? true,
- ...overrides.config,
- },
- models: modelIds.map((modelId) => ({
- kind: "candidate" as const,
- modelId,
- builtin: true,
- personalConfig: {},
- config: {},
- hasPersonalConfig: false,
- executable: true,
- selectable: true,
- })),
- };
- }
- function createCodingPlanItem(
- status: "notPurchased" | "purchased",
- ): Extract<ModelProviderNavGroup["items"][number], { type: "codingPlan" }> {
- return {
- key: `coding-plan:${BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan}`,
- type: "codingPlan",
- presetId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
- oauthProviderId: BIGMODEL_PROVIDER_ID,
- label: "BigModel - Coding Plan",
- providerName: "BigModel",
- provider: createProvider({
- id: BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
- name: "BigModel - Coding Plan",
- }),
- status,
- statusActive: status === "purchased",
- subscriptionRenewTime: "2026-09-15T00:00:00.000Z",
- subscriptionBillingCycle: "monthly",
- };
- }
- function createStartPlanItem(
- status: CodingPlanStatus,
- ): Extract<ModelProviderNavGroup["items"][number], { type: "codingPlan" }> {
- return {
- ...createCodingPlanItem(status === "purchased" ? "purchased" : "notPurchased"),
- key: `coding-plan:${BUILTIN_MODEL_PROVIDER_IDS.bigmodelStartPlan}`,
- presetId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelStartPlan,
- label: "BigModel - Start Plan",
- provider: createProvider({
- id: BUILTIN_MODEL_PROVIDER_IDS.bigmodelStartPlan,
- name: "BigModel - Start Plan",
- }),
- status,
- statusActive: status === "purchased",
- };
- }
- function createNoTeamQuotaEntitlementSnapshot(): UsageEntitlementSnapshot {
- return {
- generatedAt: 1,
- authenticated: true,
- context: {
- scope: "team",
- organizationId: "org-a",
- projectId: "project-a",
- productId: "product-team",
- displayName: "Org A",
- },
- provider: {
- id: BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
- name: "BigModel - Coding Plan",
- },
- remaining: null,
- quota: null,
- subscription: null,
- unavailableReason: "no_plan",
- };
- }
- describe("buildVisibleFamilyConnectionKeys", () => {
- it("Start 与个人 Coding 同时有效时保留两个连接项", () => {
- const items = buildVisibleFamilyConnectionItems({
- items: [createCodingPlanItem("purchased"), createStartPlanItem("purchased")],
- subscribedTeamProducts: [],
- showPurchasedTeamPlanFallback: false,
- });
- expect(items.map((item) => item.key)).toEqual([
- `coding-plan:${BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan}`,
- `coding-plan:${BUILTIN_MODEL_PROVIDER_IDS.bigmodelStartPlan}`,
- ]);
- });
- it("Start 与 Team Plan 同时有效时仍保留 Start 连接项", () => {
- const items = buildVisibleFamilyConnectionItems({
- items: [createCodingPlanItem("notPurchased"), createStartPlanItem("purchased")],
- subscribedTeamProducts: [
- {
- productId: "team-pro",
- productName: "Team Pro",
- productBigTitle: "Team Pro",
- originalAmount: 598,
- payAmount: 598,
- priceUnit: "year",
- priceCurrency: "CNY",
- productEquityList: [],
- hasPreview: true,
- enterpriseProduct: {
- productId: "team-pro",
- tier: "PRO",
- subscribeMode: "CONTINUOUS",
- subscribePeriod: "YEARLY",
- subscribed: true,
- organizationId: "org-a",
- organizationName: "Org A",
- projectId: "project-a",
- projectName: "Project A",
- },
- tier: "PRO",
- subscribeMode: "CONTINUOUS",
- subscribePeriod: "YEARLY",
- purchaseMethodName: "yearly",
- subscribed: true,
- organizationId: "org-a",
- organizationName: "Org A",
- projectId: "project-a",
- projectName: "Project A",
- },
- ],
- showPurchasedTeamPlanFallback: false,
- });
- expect(items.map((item) => item.key)).toContain(
- `coding-plan:${BUILTIN_MODEL_PROVIDER_IDS.bigmodelStartPlan}`,
- );
- expect(items.some((item) => item.type === "teamPlan")).toBe(true);
- });
- it("已选 Start 临时不可用时保留入口,明确无权益时隐藏", () => {
- const selectedConnectionKey = `coding-plan:${BUILTIN_MODEL_PROVIDER_IDS.bigmodelStartPlan}`;
- const common = {
- subscribedTeamProducts: [],
- showPurchasedTeamPlanFallback: false,
- connectionSelections: { bigmodel: { kind: "start-plan" } } as const,
- };
- expect(
- buildVisibleFamilyConnectionItems({
- ...common,
- items: [createCodingPlanItem("purchased"), createStartPlanItem("unavailable")],
- }).map((item) => item.key),
- ).toContain(selectedConnectionKey);
- expect(
- buildVisibleFamilyConnectionItems({
- ...common,
- items: [createCodingPlanItem("purchased"), createStartPlanItem("notPurchased")],
- }).map((item) => item.key),
- ).not.toContain(selectedConnectionKey);
- });
- it("模型删空后仍按 API Key 和权益保持 Coding Plan 已购状态", () => {
- const provider = createProvider({
- id: BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
- apiKey: "coding-plan-key",
- models: [],
- });
- expect(
- resolveCodingPlanEntitlementState({
- providerId: provider.providerId,
- accountEntitled: true,
- modelProvidersLoading: false,
- entitlement: {
- loading: false,
- error: null,
- snapshot: {
- generatedAt: 1,
- authenticated: true,
- provider: {
- id: provider.providerId,
- name: provider.config.label ?? provider.providerId,
- },
- remaining: null,
- quota: null,
- subscription: {
- identityType: "email",
- identityMasked: "u***@example.com",
- details: [
- {
- productId: "coding-lite",
- productName: "GLM Coding Lite",
- purchaseTime: null,
- beginTime: null,
- expireTime: null,
- },
- ],
- },
- },
- },
- }).status,
- ).toBe("purchased");
- });
- it.each(["not_authenticated", "not_configured", "unavailable"] as const)(
- "账号已连接时 %s 权益快照不会被误报为未购买",
- (unavailableReason) => {
- expect(
- resolveCodingPlanEntitlementState({
- providerId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
- accountEntitled: true,
- modelProvidersLoading: false,
- entitlement: {
- loading: false,
- error: null,
- snapshot: {
- generatedAt: 1,
- authenticated: unavailableReason !== "not_authenticated",
- provider: null,
- remaining: null,
- quota: null,
- subscription: null,
- unavailableReason,
- },
- },
- }).status,
- ).toBe("unavailable");
- },
- );
- it("BigModel 个人套餐管理入口使用当前环境的个人套餐页", () => {
- const bigmodelSpecs = CODING_PLAN_PROVIDER_SPECS.filter(
- (item) =>
- item.id === BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan ||
- item.id === BUILTIN_MODEL_PROVIDER_IDS.bigmodelStartPlan,
- );
- expect(bigmodelSpecs.map((item) => item.purchaseUrl)).toEqual([
- "https://bigmodel.cn/coding-plan/personal/overview",
- "https://bigmodel.cn/coding-plan/personal/overview",
- ]);
- });
- it("输入框连接方式保留未购买 Coding Plan", () => {
- const keys = buildVisibleFamilyConnectionKeys({
- items: [createCodingPlanItem("notPurchased")],
- subscribedTeamProducts: [],
- showPurchasedTeamPlanFallback: false,
- modelProviders: [
- createProvider({
- id: BUILTIN_MODEL_PROVIDER_IDS.bigmodelStartPlan,
- apiKey: "",
- models: ["glm-api-key"],
- }),
- ],
- });
- expect([...keys]).toEqual([
- `coding-plan:${BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan}`,
- ]);
- });
- it("已购 Coding Plan 会进入输入框连接方式", () => {
- const keys = buildVisibleFamilyConnectionKeys({
- items: [createCodingPlanItem("purchased")],
- subscribedTeamProducts: [],
- showPurchasedTeamPlanFallback: false,
- modelProviders: [
- createProvider({
- id: BUILTIN_MODEL_PROVIDER_IDS.bigmodelStartPlan,
- apiKey: "sk-api",
- models: ["glm-api-key"],
- }),
- ],
- });
- expect([...keys]).toEqual([
- `coding-plan:${BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan}`,
- ]);
- });
- it("Team Plan 管理入口使用当前环境的团队套餐列表页", () => {
- const items = buildVisibleFamilyConnectionItems({
- items: [
- {
- ...createCodingPlanItem("purchased"),
- purchaseUrl: "https://bigmodel.cn/coding-plan/personal/overview",
- },
- ],
- subscribedTeamProducts: [
- {
- productId: "product-team",
- productName: "GLM Coding Team",
- productBigTitle: "GLM Coding Team",
- originalAmount: 598,
- payAmount: 598,
- priceUnit: "year",
- priceCurrency: "CNY",
- productEquityList: [],
- hasPreview: true,
- enterpriseProduct: {
- productId: "product-team",
- tier: "PRO",
- subscribeMode: "CONTINUOUS",
- subscribePeriod: "YEARLY",
- subscribed: true,
- organizationId: "org-a",
- organizationName: "Org A",
- projectId: "project-a",
- projectName: "Project A",
- },
- tier: "PRO",
- subscribeMode: "CONTINUOUS",
- subscribePeriod: "YEARLY",
- purchaseMethodName: "yearly",
- subscribed: true,
- organizationId: "org-a",
- organizationName: "Org A",
- projectId: "project-a",
- projectName: "Project A",
- },
- ],
- showPurchasedTeamPlanFallback: false,
- });
- const teamItem = items.find((item) => item.type === "teamPlan");
- expect(teamItem?.purchaseUrl).toBe("https://bigmodel.cn/coding-plan/team/plans");
- expect(teamItem?.teamPlanName).toBe("Org A");
- expect(teamItem?.planLevel).toBe("Org A");
- });
- it("缺少组织名称时不生成空白 Team Plan 连接项", () => {
- const items = buildVisibleFamilyConnectionItems({
- items: [createCodingPlanItem("purchased")],
- subscribedTeamProducts: [
- {
- productId: "product-team",
- productName: "GLM Coding Team",
- productBigTitle: "GLM Coding Team",
- originalAmount: 598,
- payAmount: 598,
- priceUnit: "year",
- priceCurrency: "CNY",
- productEquityList: [],
- hasPreview: true,
- enterpriseProduct: {
- productId: "product-team",
- tier: "PRO",
- subscribeMode: "CONTINUOUS",
- subscribePeriod: "YEARLY",
- subscribed: true,
- organizationId: "org-a",
- organizationName: null,
- projectId: "project-a",
- projectName: "Project A",
- },
- tier: "PRO",
- subscribeMode: "CONTINUOUS",
- subscribePeriod: "YEARLY",
- purchaseMethodName: "yearly",
- subscribed: true,
- organizationId: "org-a",
- organizationName: null,
- projectId: "project-a",
- projectName: "Project A",
- },
- ],
- showPurchasedTeamPlanFallback: false,
- });
- expect(items.some((item) => item.type === "teamPlan")).toBe(false);
- });
- it("Team Plan 项目 key 不可用时保留连接项但标成不可用", () => {
- const items = buildVisibleFamilyConnectionItems({
- items: [createCodingPlanItem("purchased")],
- subscribedTeamProducts: [
- {
- productId: "product-team",
- productName: "GLM Coding Team",
- productBigTitle: "GLM Coding Team",
- originalAmount: 598,
- payAmount: 598,
- priceUnit: "year",
- priceCurrency: "CNY",
- productEquityList: [],
- hasPreview: true,
- enterpriseProduct: {
- productId: "product-team",
- tier: "PRO",
- subscribeMode: "CONTINUOUS",
- subscribePeriod: "YEARLY",
- subscribed: true,
- teamProjects: [
- {
- organizationId: "org-a",
- organizationName: "Org A",
- projectId: "project-a",
- projectName: "Project A",
- apiKeyStatus: "unavailable",
- apiKeyUnavailableReason: "no_valid_team_plan_authorization",
- },
- ],
- },
- tier: "PRO",
- subscribeMode: "CONTINUOUS",
- subscribePeriod: "YEARLY",
- purchaseMethodName: "yearly",
- subscribed: true,
- teamProjects: [
- {
- organizationId: "org-a",
- organizationName: "Org A",
- projectId: "project-a",
- projectName: "Project A",
- apiKeyStatus: "unavailable",
- apiKeyUnavailableReason: "no_valid_team_plan_authorization",
- },
- ],
- },
- ],
- showPurchasedTeamPlanFallback: false,
- });
- const teamItem = items.find((item) => item.type === "teamPlan");
- expect(teamItem).toMatchObject({
- key: `team:bigmodel:product-team:org-a:project-a`,
- status: "unavailable",
- statusActive: false,
- availabilityReason: "credential-unavailable",
- statusLabelId: undefined,
- inactivePlanTitle: "Org A",
- teamPlanName: "Org A",
- });
- });
- it("Team Plan quota 权益失败时不继承个人续费日期且不点亮", () => {
- const items = buildVisibleFamilyConnectionItems({
- items: [createCodingPlanItem("purchased")],
- codingPlanEntitlements: {
- [BUILTIN_MODEL_PROVIDER_IDS.bigmodelTeamCodingPlan]: {
- loading: false,
- error: null,
- snapshot: createNoTeamQuotaEntitlementSnapshot(),
- refresh: async () => {},
- },
- },
- subscribedTeamProducts: [
- {
- productId: "product-team",
- productName: "GLM Coding Team",
- productBigTitle: "GLM Coding Team",
- originalAmount: 598,
- payAmount: 598,
- priceUnit: "year",
- priceCurrency: "CNY",
- productEquityList: [],
- hasPreview: true,
- enterpriseProduct: {
- productId: "product-team",
- tier: "PRO",
- subscribeMode: "CONTINUOUS",
- subscribePeriod: "YEARLY",
- subscribed: true,
- teamProjects: [
- {
- organizationId: "org-a",
- organizationName: "Org A",
- projectId: "project-a",
- projectName: "Project A",
- apiKeyStatus: "available",
- },
- ],
- },
- tier: "PRO",
- subscribeMode: "CONTINUOUS",
- subscribePeriod: "YEARLY",
- purchaseMethodName: "yearly",
- subscribed: true,
- teamProjects: [
- {
- organizationId: "org-a",
- organizationName: "Org A",
- projectId: "project-a",
- projectName: "Project A",
- apiKeyStatus: "available",
- },
- ],
- },
- ],
- showPurchasedTeamPlanFallback: false,
- });
- const teamItem = items.find((item) => item.type === "teamPlan");
- expect(teamItem).toMatchObject({
- status: "unavailable",
- statusActive: false,
- statusLabelId: "settings.modelProvider.codingPlan.status.teamUnavailable",
- });
- expect(teamItem).toMatchObject({
- subscriptionRenewTime: null,
- subscriptionBillingCycle: null,
- });
- });
- it("Team Plan quota 仍在加载或请求报错时不提前标成未分配", () => {
- for (const entitlement of [
- { loading: true, error: null },
- { loading: false, error: new Error("network failed") },
- ]) {
- const items = buildVisibleFamilyConnectionItems({
- items: [createCodingPlanItem("purchased")],
- codingPlanEntitlements: {
- [BUILTIN_MODEL_PROVIDER_IDS.bigmodelTeamCodingPlan]: {
- ...entitlement,
- snapshot: createNoTeamQuotaEntitlementSnapshot(),
- refresh: async () => {},
- },
- },
- subscribedTeamProducts: [
- {
- productId: "product-team",
- productName: "GLM Coding Team",
- productBigTitle: "GLM Coding Team",
- originalAmount: 598,
- payAmount: 598,
- priceUnit: "year",
- priceCurrency: "CNY",
- productEquityList: [],
- hasPreview: true,
- enterpriseProduct: {
- productId: "product-team",
- tier: "PRO",
- subscribeMode: "CONTINUOUS",
- subscribePeriod: "YEARLY",
- subscribed: true,
- teamProjects: [
- {
- organizationId: "org-a",
- organizationName: "Org A",
- projectId: "project-a",
- projectName: "Project A",
- apiKeyStatus: "available",
- },
- ],
- },
- tier: "PRO",
- subscribeMode: "CONTINUOUS",
- subscribePeriod: "YEARLY",
- purchaseMethodName: "yearly",
- subscribed: true,
- teamProjects: [
- {
- organizationId: "org-a",
- organizationName: "Org A",
- projectId: "project-a",
- projectName: "Project A",
- apiKeyStatus: "available",
- },
- ],
- },
- ],
- showPurchasedTeamPlanFallback: false,
- });
- const teamItem = items.find((item) => item.type === "teamPlan");
- expect(teamItem?.statusLabelId).not.toBe(
- "settings.modelProvider.codingPlan.status.teamUnavailable",
- );
- }
- });
- it("优先使用 entitlement snapshot 团队上下文生成 Team Plan 连接项", () => {
- const items = buildVisibleFamilyConnectionItems({
- items: [createCodingPlanItem("purchased")],
- codingPlanEntitlements: {
- [BUILTIN_MODEL_PROVIDER_IDS.bigmodelTeamCodingPlan]: {
- loading: false,
- error: null,
- snapshot: {
- generatedAt: 1,
- authenticated: true,
- context: {
- scope: "team",
- organizationId: "org-snapshot",
- projectId: "project-snapshot",
- productId: "product-snapshot",
- displayName: "Snapshot Team",
- },
- provider: {
- id: BUILTIN_MODEL_PROVIDER_IDS.bigmodelTeamCodingPlan,
- name: "BigModel - Coding Plan",
- },
- remaining: null,
- subscription: null,
- quota: null,
- },
- },
- },
- subscribedTeamProducts: [],
- showPurchasedTeamPlanFallback: false,
- });
- const teamItem = items.find((item) => item.type === "teamPlan");
- expect(teamItem?.key).toBe(`team:bigmodel:product-snapshot:org-snapshot:project-snapshot`);
- expect(teamItem?.teamPlanName).toBe("Snapshot Team");
- expect(teamItem?.organizationId).toBe("org-snapshot");
- expect(teamItem?.projectId).toBe("project-snapshot");
- });
- it("enterprise products 只校正 entitlement 生成的同项目 Team Plan 元数据", () => {
- const items = buildVisibleFamilyConnectionItems({
- items: [createCodingPlanItem("purchased")],
- codingPlanEntitlements: {
- [BUILTIN_MODEL_PROVIDER_IDS.bigmodelTeamCodingPlan]: {
- loading: false,
- error: null,
- snapshot: {
- generatedAt: 1,
- authenticated: true,
- context: {
- scope: "team",
- organizationId: "org-a",
- projectId: "project-a",
- productId: "product-old",
- displayName: "Snapshot Team",
- },
- provider: {
- id: BUILTIN_MODEL_PROVIDER_IDS.bigmodelTeamCodingPlan,
- name: "BigModel - Coding Plan",
- },
- remaining: null,
- subscription: null,
- quota: null,
- },
- },
- },
- subscribedTeamProducts: [
- {
- productId: "product-new",
- productName: "GLM Coding Team",
- productBigTitle: "GLM Coding Team",
- originalAmount: 598,
- payAmount: 598,
- priceUnit: "year",
- priceCurrency: "CNY",
- productEquityList: [],
- hasPreview: true,
- enterpriseProduct: {
- productId: "product-new",
- tier: "PRO",
- subscribeMode: "CONTINUOUS",
- subscribePeriod: "YEARLY",
- subscribed: true,
- organizationId: "org-a",
- organizationName: "Org A",
- projectId: "project-a",
- projectName: "Corrected Project",
- },
- tier: "PRO",
- subscribeMode: "CONTINUOUS",
- subscribePeriod: "YEARLY",
- purchaseMethodName: "yearly",
- subscribed: true,
- organizationId: "org-a",
- organizationName: "Org A",
- projectId: "project-a",
- projectName: "Corrected Project",
- },
- ],
- showPurchasedTeamPlanFallback: false,
- });
- const teamItems = items.filter((item) => item.type === "teamPlan");
- expect(teamItems).toHaveLength(1);
- expect(teamItems[0]?.key).toBe(`team:bigmodel:product-new:org-a:project-a`);
- expect(teamItems[0]?.teamPlanName).toBe("Org A");
- });
- it("enterprise pricing 未返回团队项目时按已保存 selected key 展示 Team Plan fallback", () => {
- const items = buildVisibleFamilyConnectionItems({
- items: [createCodingPlanItem("purchased")],
- subscribedTeamProducts: [],
- showPurchasedTeamPlanFallback: true,
- teamPlanSelections: {
- bigmodel: {
- kind: "team-coding-plan",
- productId: "product-saved",
- organizationId: "org-saved",
- projectId: "project-saved",
- },
- },
- });
- const teamItem = items.find((item) => item.type === "teamPlan");
- expect(teamItem).toMatchObject({
- key: `team:bigmodel:product-saved:org-saved:project-saved`,
- organizationId: "org-saved",
- projectId: "project-saved",
- status: "purchased",
- });
- });
- });
- describe("zai family Team Plan 可见性对称化", () => {
- function createZaiCodingPlanItem(
- status: "notPurchased" | "purchased",
- ): Extract<ModelProviderNavGroup["items"][number], { type: "codingPlan" }> {
- return {
- key: `coding-plan:${BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan}`,
- type: "codingPlan",
- presetId: BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
- oauthProviderId: ZAI_PROVIDER_ID,
- label: "Z.ai - Coding Plan",
- providerName: "Z.ai",
- provider: createProvider({
- id: BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
- name: "Z.ai - Coding Plan",
- }),
- status,
- statusActive: status === "purchased",
- };
- }
- it("zai family 的已订阅 Team Plan 会生成 zai 前缀连接项", () => {
- const items = buildVisibleFamilyConnectionItems({
- items: [createCodingPlanItem("purchased"), createZaiCodingPlanItem("purchased")],
- subscribedTeamProducts: [
- {
- productId: "zai-team-pro",
- productName: "Z.ai Coding Team",
- productBigTitle: "Z.ai Coding Team",
- originalAmount: 216,
- payAmount: 216,
- priceUnit: "year",
- priceCurrency: "USD",
- productEquityList: [],
- hasPreview: true,
- enterpriseProduct: {
- productId: "zai-team-pro",
- tier: "PRO",
- subscribeMode: "CONTINUOUS",
- subscribePeriod: "YEARLY",
- subscribed: true,
- organizationId: "zai-org",
- organizationName: "Zai Org",
- projectId: "zai-project",
- projectName: "Zai Project",
- },
- tier: "PRO",
- subscribeMode: "CONTINUOUS",
- subscribePeriod: "YEARLY",
- purchaseMethodName: "yearly",
- subscribed: true,
- organizationId: "zai-org",
- organizationName: "Zai Org",
- projectId: "zai-project",
- projectName: "Zai Project",
- // 关键:标记 family=zai,触发 zai codingPlanItem 派生 + zai key 前缀
- family: "zai",
- },
- ],
- showPurchasedTeamPlanFallback: false,
- });
- const teamItem = items.find((item) => item.type === "teamPlan");
- expect(teamItem).toBeDefined();
- // 关键:key 前缀必须是 zai-coding-plan,不能误用 bigmodel
- expect(teamItem?.key).toBe(`team:zai:zai-team-pro:zai-org:zai-project`);
- // providerName 应继承自 zai codingPlanItem
- expect(teamItem?.providerName).toBe("Z.ai");
- expect(teamItem?.teamPlanName).toBe("Zai Org");
- expect(teamItem?.purchaseUrl).toBe(getModelProviderFamilySpec("zai").teamCodingPlanManageUrl);
- });
- it("未标记 family 的 product 仍走 bigmodel(向后兼容)", () => {
- const items = buildVisibleFamilyConnectionItems({
- items: [createCodingPlanItem("purchased")],
- subscribedTeamProducts: [
- {
- productId: "product-team",
- productName: "GLM Coding Team",
- productBigTitle: "GLM Coding Team",
- originalAmount: 598,
- payAmount: 598,
- priceUnit: "year",
- priceCurrency: "CNY",
- productEquityList: [],
- hasPreview: true,
- enterpriseProduct: {
- productId: "product-team",
- tier: "PRO",
- subscribeMode: "CONTINUOUS",
- subscribePeriod: "YEARLY",
- subscribed: true,
- organizationId: "org-a",
- organizationName: "Org A",
- projectId: "project-a",
- projectName: "Project A",
- },
- tier: "PRO",
- subscribeMode: "CONTINUOUS",
- subscribePeriod: "YEARLY",
- purchaseMethodName: "yearly",
- subscribed: true,
- organizationId: "org-a",
- organizationName: "Org A",
- projectId: "project-a",
- projectName: "Project A",
- // 不标记 family
- },
- ],
- showPurchasedTeamPlanFallback: false,
- });
- const teamItem = items.find((item) => item.type === "teamPlan");
- expect(teamItem?.key).toBe(`team:bigmodel:product-team:org-a:project-a`);
- });
- it("zai 和 bigmodel 同名团队不会互相覆盖(family 维度去重)", () => {
- const items = buildVisibleFamilyConnectionItems({
- items: [createCodingPlanItem("purchased"), createZaiCodingPlanItem("purchased")],
- subscribedTeamProducts: [
- {
- productId: "shared-product",
- productName: "Team",
- productBigTitle: "Team",
- originalAmount: 100,
- payAmount: 100,
- priceUnit: "year",
- priceCurrency: "CNY",
- productEquityList: [],
- hasPreview: true,
- enterpriseProduct: {
- productId: "shared-product",
- tier: "PRO",
- subscribeMode: "CONTINUOUS",
- subscribePeriod: "YEARLY",
- subscribed: true,
- organizationId: "org-same",
- organizationName: "Same Org",
- projectId: "proj-same",
- projectName: "Same Project",
- },
- tier: "PRO",
- subscribeMode: "CONTINUOUS",
- subscribePeriod: "YEARLY",
- purchaseMethodName: "yearly",
- subscribed: true,
- organizationId: "org-same",
- organizationName: "Same Org",
- projectId: "proj-same",
- projectName: "Same Project",
- family: "bigmodel",
- },
- {
- productId: "shared-product",
- productName: "Team",
- productBigTitle: "Team",
- originalAmount: 100,
- payAmount: 100,
- priceUnit: "year",
- priceCurrency: "USD",
- productEquityList: [],
- hasPreview: true,
- enterpriseProduct: {
- productId: "shared-product",
- tier: "PRO",
- subscribeMode: "CONTINUOUS",
- subscribePeriod: "YEARLY",
- subscribed: true,
- organizationId: "org-same",
- organizationName: "Same Org",
- projectId: "proj-same",
- projectName: "Same Project",
- },
- tier: "PRO",
- subscribeMode: "CONTINUOUS",
- subscribePeriod: "YEARLY",
- purchaseMethodName: "yearly",
- subscribed: true,
- organizationId: "org-same",
- organizationName: "Same Org",
- projectId: "proj-same",
- projectName: "Same Project",
- family: "zai",
- },
- ],
- showPurchasedTeamPlanFallback: false,
- });
- const teamItems = items.filter((item) => item.type === "teamPlan");
- // 关键:两个 family 各生成一个,不能因 productId+org+project 相同而互相覆盖
- expect(teamItems).toHaveLength(2);
- const keys = teamItems.map((item) => item.key);
- expect(keys).toContain(`team:bigmodel:shared-product:org-same:proj-same`);
- expect(keys).toContain(`team:zai:shared-product:org-same:proj-same`);
- });
- it("zai-only 视图(无 bigmodel codingPlan item)仍生成 zai Team Plan 连接项", () => {
- // 修复原因:appendSubscribedTeamPlanItems 原有 `if (!bigmodelCodingPlanItem) return items`
- // 前置守卫。当设置页 providerFamilyDomain === "zai" 时,传入的 codingPlanItems 只含 zai family,
- // 没有 bigmodelCodingPlan,守卫直接短路,zai teamPlan item 永远不生成,
- // 导致 pickFamilyModeNavigationItem 找不到 saved team item → selectedNavItem=null → Plan Card 永远 loading。
- const items = buildVisibleFamilyConnectionItems({
- // 关键:只有 zai codingPlan item,没有 bigmodel
- items: [createZaiCodingPlanItem("notPurchased")],
- subscribedTeamProducts: [],
- showPurchasedTeamPlanFallback: true,
- // 模拟已落盘的 zai team plan selectedKey(和 setting.json 里的格式一致)
- teamPlanSelections: {
- zai: {
- kind: "team-coding-plan",
- productId: "zai-pro",
- organizationId: "zai-org-1",
- projectId: "zai-proj-1",
- },
- },
- });
- const teamItem = items.find((item) => item.type === "teamPlan");
- // 关键:必须生成 zai teamPlan item,不能因为 bigmodelCodingPlanItem 不存在就整体短路
- expect(teamItem).toBeDefined();
- expect(teamItem?.key).toBe(`team:zai:zai-pro:zai-org-1:zai-proj-1`);
- });
- });
- it("Start 查询未知不能显示未登录,查询失败可重试", () => {
- expect(
- resolveCodingPlanEntitlementState({
- providerId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelStartPlan,
- accountEntitled: false,
- accountAvailability: "unknown",
- modelProvidersLoading: false,
- }).status,
- ).toBe("unavailable");
- });
|