import { readSourceTextAsync } from "./readSourceText.js"; import { describe, expect, it } from "vitest"; import { BUILTIN_MODEL_PROVIDER_IDS } from "@zcode/shared"; import { buildCodingPlanModelLineChartSeries, buildCodingPlanToolLineChartSeries, buildCodingPlanUsageDetailLineChartSeries, } from "@/settings/usage-stats/codingPlanUsageChartSeries.js"; import { calculateCodingPlanBarChartMaxValue } from "@/settings/usage-stats/CodingPlanUsageBarChart.js"; import { buildCodingPlanUsageSources, formatUsageStatsQuotaResetTime, hasCodingPlanCreditUsageData, resolveCodingPlanUsageProviderName, shouldShowCodingPlanUsageDetailSummary, } from "@/settings/usage-stats/CodingPlanUsagePanel.js"; import enMessages from "@/i18n/locales/en-US.js"; import zhMessages from "@/i18n/locales/zh-CN.js"; import { findCodingPlanQuotaLimit, formatQuotaRemainingPercentage, formatQuotaResetTime, getQuotaRemainingPercentage, } from "@/lib/codingPlanQuotaPresentation.js"; import { buildPersonalCodingPlanUsageSource, matchesTeamPlanUsageSourceKey, resolveSidebarCurrentCodingPlanUsageSource, } from "@/lib/codingPlanUsageSources.js"; import { formatSummaryCompactTokenUsage } from "@/settings/usage-stats/usageStatsUiParts.js"; describe("CodingPlanUsagePanel", () => { it("个人与团队套餐的中文 Token 摘要在数字和单位之间保留空格", () => { expect(formatSummaryCompactTokenUsage("zh-CN", 370_000_000)).toBe("3.7 亿"); expect(formatSummaryCompactTokenUsage("en-US", 370_000_000)).toBe("370M"); }); it("Usage stats 和模型设置的 ZCode MCP 卡片都提供统一 info tooltip", async () => { const [usageSource, modelSettingsSource] = await Promise.all([ readSourceTextAsync( new URL("../src/settings/usage-stats/CodingPlanUsagePanel.tsx", import.meta.url), "utf8", ), readSourceTextAsync( new URL("../src/settings/model-provider-section/StatusCards.tsx", import.meta.url), "utf8", ), ]); expect(usageSource).toContain('data-zcode-mcp-info="usage-stats"'); expect(modelSettingsSource).toContain('data-zcode-mcp-info="model-settings"'); expect(usageSource).toContain('id: "sidebar.usage.plan.zcodeMcpDescription"'); expect(modelSettingsSource).toContain('id: "sidebar.usage.plan.zcodeMcpDescription"'); }); it("统一使用工具调用和 ZCode MCP 的额度名称", () => { expect(zhMessages["settings.usage.entitlementMonthlyMcpUsage"]).toBe("工具调用"); expect(enMessages["settings.usage.entitlementMonthlyMcpUsage"]).toBe("Tool calls"); expect(zhMessages["settings.usage.entitlementServerMcpUsage"]).toBe("ZCode MCP"); expect(enMessages["settings.usage.entitlementServerMcpUsage"]).toBe("ZCode MCP"); expect(zhMessages["sidebar.usage.plan.mcp"]).toBe("ZCode MCP"); expect(enMessages["sidebar.usage.plan.mcp"]).toBe("ZCode MCP"); }); it("个人套餐来源使用 Account Access,而不是请求凭据", () => { expect( buildPersonalCodingPlanUsageSource({ providerId: BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan, accountAccess: { type: "zhipu-account", family: "zai", planKind: "individual-coding-plan", }, label: "Z.ai - Coding Plan", }), ).toEqual({ id: BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan, providerId: BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan, accountAccess: { type: "zhipu-account", family: "zai", planKind: "individual-coding-plan", }, label: "Z.ai - Coding Plan", }); }); it("把已购 Team Plan 展开为可查询的 usage source", () => { const sources = buildCodingPlanUsageSources({ accountAccesses: { bigmodel: { type: "zhipu-account", family: "bigmodel", mode: "team-coding-plan", }, }, subscribedTeamProducts: [ { productId: "product-team", productName: "Pro", projectName: "Team A", subscribed: true, teamProjects: [ { organizationId: "org-team", organizationName: "Org", projectId: "proj-team", projectName: "Team A", }, ], } as never, ], }); expect(sources).toEqual([ { id: "team:bigmodel:product-team:org-team:proj-team", providerId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelTeamCodingPlan, accountAccess: { type: "zhipu-account", family: "bigmodel", planKind: "team-coding-plan", productId: "product-team", organizationId: "org-team", projectId: "proj-team", }, label: "BigModel - Org", }, ]); }); it("把 zai 已购 Team Plan 展开为带 zai providerId 和 Z.ai 品牌前缀的 usage source", () => { const sources = buildCodingPlanUsageSources({ accountAccesses: { zai: { type: "zhipu-account", family: "zai", mode: "team-coding-plan", }, }, subscribedTeamProducts: [ { productId: "zai-product-team", productName: "Pro", projectName: "ZAI Team A", subscribed: true, family: "zai", teamProjects: [ { organizationId: "zai-org", organizationName: "ZAI Org", projectId: "zai-proj", projectName: "ZAI Team A", }, ], } as never, ], }); expect(sources).toEqual([ { id: "team:zai:zai-product-team:zai-org:zai-proj", providerId: BUILTIN_MODEL_PROVIDER_IDS.zaiTeamCodingPlan, accountAccess: { type: "zhipu-account", family: "zai", planKind: "team-coding-plan", productId: "zai-product-team", organizationId: "zai-org", projectId: "zai-proj", }, label: "Z.ai - ZAI Org", }, ]); }); it("resolveSidebarCurrentCodingPlanUsageSource 把 zai team selectedKey 解析成 team source", () => { const teamSources = buildCodingPlanUsageSources({ accountAccesses: { zai: { type: "zhipu-account", family: "zai", mode: "team-coding-plan", }, }, subscribedTeamProducts: [ { productId: "zai-product-team", productName: "Pro", projectName: "ZAI Team A", subscribed: true, family: "zai", teamProjects: [ { organizationId: "zai-org", organizationName: "ZAI Org", projectId: "zai-proj", projectName: "ZAI Team A", }, ], } as never, ], }); const resolved = resolveSidebarCurrentCodingPlanUsageSource({ selections: { zai: { kind: "team-coding-plan", productId: "zai-product-team", organizationId: "zai-org", projectId: "zai-proj", }, }, selectedProviderId: BUILTIN_MODEL_PROVIDER_IDS.zaiTeamCodingPlan, accountAccesses: {}, teamSources, }); expect(resolved).toEqual({ audience: "team", providerId: BUILTIN_MODEL_PROVIDER_IDS.zaiTeamCodingPlan, sourceId: "team:zai:zai-product-team:zai-org:zai-proj", teamSource: teamSources[0], }); }); it("resolveSidebarCurrentCodingPlanUsageSource 在 zai team key 未匹配 source 时返回 null(不回退 individual)", () => { const resolved = resolveSidebarCurrentCodingPlanUsageSource({ selections: { zai: { kind: "team-coding-plan", productId: "unknown", organizationId: "unknown", projectId: "unknown", }, }, selectedProviderId: BUILTIN_MODEL_PROVIDER_IDS.zaiTeamCodingPlan, accountAccesses: {}, teamSources: [], }); expect(resolved).toBeNull(); }); it("resolveSidebarCurrentCodingPlanUsageSource 把 zai 个人 coding-plan key 解析成 individual", () => { const resolved = resolveSidebarCurrentCodingPlanUsageSource({ selections: { zai: { kind: "individual-coding-plan" } }, selectedProviderId: BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan, accountAccesses: { zai: { type: "zhipu-account", family: "zai", mode: "individual-coding-plan", }, }, teamSources: [], }); expect(resolved).toEqual({ audience: "individual", providerId: BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan, sourceId: BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan, accountAccess: { type: "zhipu-account", family: "zai", mode: "individual-coding-plan", }, }); }); it("matchesTeamPlanUsageSourceKey 对 zai team sourceId/selectedKey 正确匹配", () => { const zaiSourceId = "team:zai:prod:org:proj"; const zaiSelectedKey = "team:zai:prod:org:proj"; expect(matchesTeamPlanUsageSourceKey(zaiSourceId, zaiSelectedKey)).toBe(true); // 不同 projectId 不匹配 expect(matchesTeamPlanUsageSourceKey(zaiSourceId, "team:zai:prod:org:other")).toBe(false); }); it("缺少组织名称时不生成空白 Team Plan usage source", () => { const sources = buildCodingPlanUsageSources({ accountAccesses: { bigmodel: { type: "zhipu-account", family: "bigmodel", planKind: "team-coding-plan", productId: "product-team", organizationId: "org-team", projectId: "proj-team", }, }, subscribedTeamProducts: [ { productId: "product-team", productName: "Pro", projectName: "Team A", subscribed: true, teamProjects: [ { organizationId: "org-team", organizationName: null, projectId: "proj-team", projectName: "Team A", }, ], } as never, ], }); expect(sources).toEqual([]); }); it("Team Plan 剩余额度标题优先显示团队项目名称", () => { expect( resolveCodingPlanUsageProviderName({ fallbackProviderName: "BigModel - Coding Plan", source: { id: "team:bigmodel:product-team:org-team:proj-team", providerId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelTeamCodingPlan, label: "BigModel - Team A", accountAccess: { type: "zhipu-account", family: "bigmodel", planKind: "team-coding-plan", productId: "product-team", organizationId: "org-team", projectId: "proj-team", }, }, snapshot: { sourceProvider: { id: BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan, name: "BigModel - Coding Plan", }, } as never, }), ).toBe("BigModel - Team A"); }); it("个人套餐剩余额度标题不显示 BigModel 内建连接名", () => { expect( resolveCodingPlanUsageProviderName({ fallbackProviderName: "BigModel - Coding Plan", source: { id: BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan, providerId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan, label: "BigModel - Coding Plan", }, snapshot: { sourceProvider: { id: BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan, name: "BigModel - Coding Plan", }, } as never, }), ).toBeUndefined(); }); it("使用统计编程套餐不再跟随当前 workspace 连接方式", async () => { const source = await readSourceTextAsync( new URL("../src/settings/usage-stats/CodingPlanUsagePanel.tsx", import.meta.url), "utf8", ); expect(source).not.toContain("selectWorkspaceZCodeState"); expect(source).not.toContain("parseCustomProviderIdFromSupplierKey"); expect(source).not.toContain("resolveSidebarCurrentCodingPlanUsageSource"); expect(source).toContain("selectedSource"); expect(source).not.toContain("function ProviderSelector"); expect(source).not.toContain("onSourceChange"); }); it("使用统计顶部把 Coding Plan sources 展开成独立 tab", async () => { const source = await readSourceTextAsync( new URL("../src/SettingsPage.tsx", import.meta.url), "utf8", ); expect(source).toContain("createSettingsUsageCodingPlanTabId"); expect(source).toContain("usageCodingPlanSources"); expect(source).toContain("codingPlanSources.map"); // 修复原因:JSX 格式化可能把属性换行,断言应验证绑定语义而不是排版。 expect(source).toMatch(/selectedCodingPlanSource=\{\s*selectedUsageCodingPlanSource\s*\}/); }); it("使用统计打开时主动探测个人 Coding Plan entitlement", async () => { const source = await readSourceTextAsync( new URL("../src/SettingsPage.tsx", import.meta.url), "utf8", ); // Bugfix 回归:个人 source 依赖 entitlement snapshot。冷启动无缓存时必须先进行 // access 探测,否则 source 不会出现,子面板也没有机会触发后续刷新。 const usageEntitlementBlock = source.slice( source.indexOf("const usageZaiEntitlement"), source.indexOf("const usageBigmodelEnterpriseProducts"), ); expect(usageEntitlementBlock.match(/refreshOnMount: true/g)).toHaveLength(2); }); it("使用统计个人 Coding Plan tab 显示个人套餐文案", () => { expect(zhMessages["settings.usage.tab.codingPlan"]).toBe("个人套餐"); expect(enMessages["settings.usage.tab.codingPlan"]).toBe("Individual Plan"); }); it("把 Coding Plan 模型和工具用量映射为图表 series", () => { expect( buildCodingPlanModelLineChartSeries([ { modelName: "glm-4.5", sortOrder: 1, tokensUsage: [10, 20], totalTokens: 30, }, ]), ).toEqual([{ name: "glm-4.5", values: [10, 20] }]); expect( buildCodingPlanToolLineChartSeries([ { toolCode: "search", toolName: "Search", sortOrder: 1, usageCount: [1, 2], totalUsageCount: 3, }, ]), ).toEqual([{ name: "Search", values: [1, 2] }]); }); it("使用详情的新旧数据都统一通过 Recharts 柱状图展示", async () => { const source = await readSourceTextAsync( new URL("../src/settings/usage-stats/CodingPlanUsagePanel.tsx", import.meta.url), "utf8", ); const barChartSource = await readSourceTextAsync( new URL("../src/settings/usage-stats/CodingPlanUsageBarChart.tsx", import.meta.url), "utf8", ); const lineChartSource = await readSourceTextAsync( new URL("../src/settings/usage-stats/CodingPlanUsageLineChart.tsx", import.meta.url), "utf8", ); const uiPartsSource = await readSourceTextAsync( new URL("../src/settings/usage-stats/usageStatsUiParts.tsx", import.meta.url), "utf8", ); expect(source).not.toMatch(/series=\{snapshot\.[^}]+\.map\(/); expect(source).toContain("buildCodingPlanUsageDetailLineChartSeries"); expect(source).toContain("buildCodingPlanModelLineChartSeries"); expect(source).toContain("buildCodingPlanToolLineChartSeries"); expect(source).toContain("series={visibleSeries}"); expect(source).toContain("{hasCreditUsageData ? ("); expect(source).toContain('
'); expect(source).not.toContain( '{hasCreditUsageData ? (\n
', ); expect(source).toContain("