codingPlanUsagePanel.test.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. import { describe, expect, it } from "vitest";
  2. import { BUILTIN_MODEL_PROVIDER_IDS } from "@zcode/shared";
  3. import { buildCodingPlanUsageDetailLineChartSeries } from "@/settings/usage-stats/codingPlanUsageChartSeries.js";
  4. import { calculateCodingPlanBarChartMaxValue } from "@/settings/usage-stats/CodingPlanUsageBarChart.js";
  5. import {
  6. buildCodingPlanUsageSources,
  7. formatUsageStatsQuotaResetTime,
  8. hasCodingPlanCreditUsageData,
  9. resolveCodingPlanUsageProviderName,
  10. shouldShowCodingPlanUsageDetailSummary,
  11. } from "@/settings/usage-stats/CodingPlanUsagePanel.js";
  12. import {
  13. findCodingPlanQuotaLimit,
  14. formatQuotaRemainingPercentage,
  15. formatQuotaResetTime,
  16. getQuotaRemainingPercentage,
  17. } from "@/lib/codingPlanQuotaPresentation.js";
  18. import {
  19. buildPersonalCodingPlanUsageSource,
  20. resolveSidebarCurrentCodingPlanUsageSource,
  21. } from "@/lib/codingPlanUsageSources.js";
  22. import { formatSummaryCompactTokenUsage } from "@/settings/usage-stats/usageStatsUiParts.js";
  23. describe("CodingPlanUsagePanel", () => {
  24. it("个人与团队套餐的中文 Token 摘要在数字和单位之间保留空格", () => {
  25. expect(formatSummaryCompactTokenUsage("zh-CN", 370_000_000)).toBe("3.7 亿");
  26. expect(formatSummaryCompactTokenUsage("en-US", 370_000_000)).toBe("370M");
  27. });
  28. it("个人套餐来源使用 Account Access,而不是请求凭据", () => {
  29. expect(
  30. buildPersonalCodingPlanUsageSource({
  31. providerId: BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
  32. accountAccess: {
  33. type: "zhipu-account",
  34. family: "zai",
  35. planKind: "individual-coding-plan",
  36. },
  37. label: "Z.ai - Coding Plan",
  38. }),
  39. ).toEqual({
  40. id: BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
  41. providerId: BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
  42. accountAccess: {
  43. type: "zhipu-account",
  44. family: "zai",
  45. planKind: "individual-coding-plan",
  46. },
  47. label: "Z.ai - Coding Plan",
  48. });
  49. });
  50. it("把已购 Team Plan 展开为可查询的 usage source", () => {
  51. const sources = buildCodingPlanUsageSources({
  52. accountAccesses: {
  53. bigmodel: {
  54. type: "zhipu-account",
  55. family: "bigmodel",
  56. mode: "team-coding-plan",
  57. },
  58. },
  59. subscribedTeamProducts: [
  60. {
  61. productId: "product-team",
  62. productName: "Pro",
  63. projectName: "Team A",
  64. subscribed: true,
  65. teamProjects: [
  66. {
  67. organizationId: "org-team",
  68. organizationName: "Org",
  69. projectId: "proj-team",
  70. projectName: "Team A",
  71. },
  72. ],
  73. } as never,
  74. ],
  75. });
  76. expect(sources).toEqual([
  77. {
  78. id: "team:bigmodel:product-team:org-team:proj-team",
  79. providerId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelTeamCodingPlan,
  80. accountAccess: {
  81. type: "zhipu-account",
  82. family: "bigmodel",
  83. planKind: "team-coding-plan",
  84. productId: "product-team",
  85. organizationId: "org-team",
  86. projectId: "proj-team",
  87. },
  88. label: "BigModel - Org",
  89. },
  90. ]);
  91. });
  92. it("把 zai 已购 Team Plan 展开为带 zai providerId 和 Z.ai 品牌前缀的 usage source", () => {
  93. const sources = buildCodingPlanUsageSources({
  94. accountAccesses: {
  95. zai: {
  96. type: "zhipu-account",
  97. family: "zai",
  98. mode: "team-coding-plan",
  99. },
  100. },
  101. subscribedTeamProducts: [
  102. {
  103. productId: "zai-product-team",
  104. productName: "Pro",
  105. projectName: "ZAI Team A",
  106. subscribed: true,
  107. family: "zai",
  108. teamProjects: [
  109. {
  110. organizationId: "zai-org",
  111. organizationName: "ZAI Org",
  112. projectId: "zai-proj",
  113. projectName: "ZAI Team A",
  114. },
  115. ],
  116. } as never,
  117. ],
  118. });
  119. expect(sources).toEqual([
  120. {
  121. id: "team:zai:zai-product-team:zai-org:zai-proj",
  122. providerId: BUILTIN_MODEL_PROVIDER_IDS.zaiTeamCodingPlan,
  123. accountAccess: {
  124. type: "zhipu-account",
  125. family: "zai",
  126. planKind: "team-coding-plan",
  127. productId: "zai-product-team",
  128. organizationId: "zai-org",
  129. projectId: "zai-proj",
  130. },
  131. label: "Z.ai - ZAI Org",
  132. },
  133. ]);
  134. });
  135. it("resolveSidebarCurrentCodingPlanUsageSource 把 zai team selectedKey 解析成 team source", () => {
  136. const teamSources = buildCodingPlanUsageSources({
  137. accountAccesses: {
  138. zai: {
  139. type: "zhipu-account",
  140. family: "zai",
  141. mode: "team-coding-plan",
  142. },
  143. },
  144. subscribedTeamProducts: [
  145. {
  146. productId: "zai-product-team",
  147. productName: "Pro",
  148. projectName: "ZAI Team A",
  149. subscribed: true,
  150. family: "zai",
  151. teamProjects: [
  152. {
  153. organizationId: "zai-org",
  154. organizationName: "ZAI Org",
  155. projectId: "zai-proj",
  156. projectName: "ZAI Team A",
  157. },
  158. ],
  159. } as never,
  160. ],
  161. });
  162. const resolved = resolveSidebarCurrentCodingPlanUsageSource({
  163. selections: {
  164. zai: {
  165. kind: "team-coding-plan",
  166. productId: "zai-product-team",
  167. organizationId: "zai-org",
  168. projectId: "zai-proj",
  169. },
  170. },
  171. selectedProviderId: BUILTIN_MODEL_PROVIDER_IDS.zaiTeamCodingPlan,
  172. accountAccesses: {},
  173. teamSources,
  174. });
  175. expect(resolved).toEqual({
  176. audience: "team",
  177. providerId: BUILTIN_MODEL_PROVIDER_IDS.zaiTeamCodingPlan,
  178. sourceId: "team:zai:zai-product-team:zai-org:zai-proj",
  179. teamSource: teamSources[0],
  180. });
  181. });
  182. it("resolveSidebarCurrentCodingPlanUsageSource 在 zai team key 未匹配 source 时返回 null(不回退 individual)", () => {
  183. const resolved = resolveSidebarCurrentCodingPlanUsageSource({
  184. selections: {
  185. zai: {
  186. kind: "team-coding-plan",
  187. productId: "unknown",
  188. organizationId: "unknown",
  189. projectId: "unknown",
  190. },
  191. },
  192. selectedProviderId: BUILTIN_MODEL_PROVIDER_IDS.zaiTeamCodingPlan,
  193. accountAccesses: {},
  194. teamSources: [],
  195. });
  196. expect(resolved).toBeNull();
  197. });
  198. it("resolveSidebarCurrentCodingPlanUsageSource 把 zai 个人 coding-plan key 解析成 individual", () => {
  199. const resolved = resolveSidebarCurrentCodingPlanUsageSource({
  200. selections: { zai: { kind: "individual-coding-plan" } },
  201. selectedProviderId: BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
  202. accountAccesses: {
  203. zai: {
  204. type: "zhipu-account",
  205. family: "zai",
  206. mode: "individual-coding-plan",
  207. },
  208. },
  209. teamSources: [],
  210. });
  211. expect(resolved).toEqual({
  212. audience: "individual",
  213. providerId: BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
  214. sourceId: BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
  215. accountAccess: {
  216. type: "zhipu-account",
  217. family: "zai",
  218. mode: "individual-coding-plan",
  219. },
  220. });
  221. });
  222. it("缺少组织名称时不生成空白 Team Plan usage source", () => {
  223. const sources = buildCodingPlanUsageSources({
  224. accountAccesses: {
  225. bigmodel: {
  226. type: "zhipu-account",
  227. family: "bigmodel",
  228. planKind: "team-coding-plan",
  229. productId: "product-team",
  230. organizationId: "org-team",
  231. projectId: "proj-team",
  232. },
  233. },
  234. subscribedTeamProducts: [
  235. {
  236. productId: "product-team",
  237. productName: "Pro",
  238. projectName: "Team A",
  239. subscribed: true,
  240. teamProjects: [
  241. {
  242. organizationId: "org-team",
  243. organizationName: null,
  244. projectId: "proj-team",
  245. projectName: "Team A",
  246. },
  247. ],
  248. } as never,
  249. ],
  250. });
  251. expect(sources).toEqual([]);
  252. });
  253. it("Team Plan 剩余额度标题优先显示团队项目名称", () => {
  254. expect(
  255. resolveCodingPlanUsageProviderName({
  256. fallbackProviderName: "BigModel - Coding Plan",
  257. source: {
  258. id: "team:bigmodel:product-team:org-team:proj-team",
  259. providerId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelTeamCodingPlan,
  260. label: "BigModel - Team A",
  261. accountAccess: {
  262. type: "zhipu-account",
  263. family: "bigmodel",
  264. planKind: "team-coding-plan",
  265. productId: "product-team",
  266. organizationId: "org-team",
  267. projectId: "proj-team",
  268. },
  269. },
  270. snapshot: {
  271. sourceProvider: {
  272. id: BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
  273. name: "BigModel - Coding Plan",
  274. },
  275. } as never,
  276. }),
  277. ).toBe("BigModel - Team A");
  278. });
  279. it("个人套餐剩余额度标题不显示 BigModel 内建连接名", () => {
  280. expect(
  281. resolveCodingPlanUsageProviderName({
  282. fallbackProviderName: "BigModel - Coding Plan",
  283. source: {
  284. id: BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
  285. providerId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
  286. label: "BigModel - Coding Plan",
  287. },
  288. snapshot: {
  289. sourceProvider: {
  290. id: BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
  291. name: "BigModel - Coding Plan",
  292. },
  293. } as never,
  294. }),
  295. ).toBeUndefined();
  296. });
  297. it("使用详情模型维度按真实模型生成柱状图 series", () => {
  298. expect(
  299. buildCodingPlanUsageDetailLineChartSeries({
  300. metric: "credits",
  301. subject: "model",
  302. modelDataList: [
  303. {
  304. modelName: "glm-5",
  305. sortOrder: 1,
  306. tokensUsage: [10, 20],
  307. creditsUsage: [1, 2],
  308. cachedInputCreditsUsage: [0.4, 0.8],
  309. uncachedInputCreditsUsage: [0.5, 1],
  310. outputCreditsUsage: [0.1, 0.2],
  311. totalTokens: 30,
  312. totalCredits: 3,
  313. },
  314. ],
  315. toolDataList: [],
  316. }),
  317. ).toEqual([
  318. {
  319. breakdown: {
  320. cachedInput: [0.4, 0.8],
  321. output: [0.1, 0.2],
  322. uncachedInput: [0.5, 1],
  323. },
  324. name: "glm-5",
  325. values: [1, 2],
  326. },
  327. ]);
  328. });
  329. it("Coding Plan 柱状图按单个并排柱高度计算 Y 轴最大值", () => {
  330. expect(
  331. calculateCodingPlanBarChartMaxValue(
  332. [
  333. {
  334. name: "GLM-5.3",
  335. values: [100],
  336. breakdown: {
  337. cachedInput: [70],
  338. uncachedInput: [20],
  339. output: [10],
  340. },
  341. },
  342. {
  343. name: "GLM-5-Turbo",
  344. values: [90],
  345. breakdown: {
  346. cachedInput: [60],
  347. uncachedInput: [20],
  348. output: [10],
  349. },
  350. },
  351. ],
  352. 1,
  353. ),
  354. ).toBe(100);
  355. });
  356. it("旧版 Coding Plan 只有 token 用量时不展示积分摘要卡", () => {
  357. const legacyUsage = {
  358. summary: {
  359. cacheHitRate: 0.894,
  360. cacheHitRateTrend: -0.06,
  361. totalCredits: 0,
  362. totalCreditsTrend: 0,
  363. averageDailyCredits: 0,
  364. averageDailyCreditsTrend: 0,
  365. },
  366. modelDataList: [
  367. {
  368. modelName: "GLM-5.2",
  369. sortOrder: 1,
  370. tokensUsage: [12_950_000],
  371. totalTokens: 12_950_000,
  372. },
  373. ],
  374. toolDataList: [],
  375. };
  376. expect(hasCodingPlanCreditUsageData(legacyUsage)).toBe(false);
  377. expect(shouldShowCodingPlanUsageDetailSummary(legacyUsage)).toBe(false);
  378. });
  379. it("新版 Coding Plan 有 credits 数据时展示积分摘要卡", () => {
  380. const creditUsage = {
  381. summary: {
  382. cacheHitRate: 0.89,
  383. cacheHitRateTrend: null,
  384. totalCredits: 28.95,
  385. totalCreditsTrend: null,
  386. averageDailyCredits: 28.95,
  387. averageDailyCreditsTrend: null,
  388. },
  389. modelDataList: [
  390. {
  391. modelName: "GLM-5.3",
  392. sortOrder: 1,
  393. tokensUsage: [120],
  394. creditsUsage: [28.95],
  395. totalTokens: 120,
  396. totalCredits: 28.95,
  397. },
  398. ],
  399. toolDataList: [],
  400. };
  401. expect(hasCodingPlanCreditUsageData(creditUsage)).toBe(true);
  402. expect(shouldShowCodingPlanUsageDetailSummary(creditUsage)).toBe(true);
  403. });
  404. it("quota 卡片显示剩余额度百分比而不是已用百分比", () => {
  405. const limit = {
  406. type: "TOKENS_LIMIT",
  407. unit: 3,
  408. number: 5,
  409. percentage: 25,
  410. usageDetails: [],
  411. };
  412. expect(getQuotaRemainingPercentage(limit)).toBe(75);
  413. expect(formatQuotaRemainingPercentage("en-US", limit)).toBe("75%");
  414. });
  415. it("Usage 页和 Usage Remaining 菜单复用相同重置时间格式", () => {
  416. const resetAt = new Date(2026, 6, 10, 8, 30).getTime();
  417. expect(
  418. formatQuotaResetTime({
  419. locale: "en-US",
  420. value: resetAt,
  421. format: "dateTime",
  422. }),
  423. ).toBe("Jul 10, 08:30");
  424. expect(
  425. formatQuotaResetTime({
  426. locale: "en-US",
  427. value: resetAt,
  428. format: "date",
  429. }),
  430. ).toBe("Jul 10");
  431. });
  432. it("只有 Usage stats 的额度卡统一显示月日和时分", () => {
  433. const resetAt = new Date(2026, 8, 16, 23, 0).getTime();
  434. expect(formatUsageStatsQuotaResetTime("zh-CN", resetAt)).toBe("9月16日 23:00");
  435. expect(formatUsageStatsQuotaResetTime("zh-CN", undefined)).toBeUndefined();
  436. });
  437. // 修复原因:zai 业务后端 Team Plan 的 quota/limit 用 CREDIT_LIMIT 作为 type,
  438. // bigmodel 用 TOKENS_LIMIT。unit/number 语义一致,消费方按 TOKENS_LIMIT 查询时
  439. // 必须命中 CREDIT_LIMIT,否则 zai team plan 的 5 小时/每周配额卡不会渲染。
  440. it("findCodingPlanQuotaLimit 按 TOKENS_LIMIT 查询时等价命中 CREDIT_LIMIT", () => {
  441. const zaiTeamLimits = [
  442. {
  443. type: "CREDIT_LIMIT",
  444. unit: 3,
  445. number: 5,
  446. percentage: 27,
  447. usageDetails: [],
  448. },
  449. {
  450. type: "CREDIT_LIMIT",
  451. unit: 6,
  452. number: 1,
  453. percentage: 10,
  454. usageDetails: [],
  455. },
  456. ];
  457. // 5 小时窗口(unit=3, number=5)
  458. expect(findCodingPlanQuotaLimit(zaiTeamLimits, "TOKENS_LIMIT", 3, 5)).toEqual(zaiTeamLimits[0]);
  459. // 每周(unit=6)
  460. expect(findCodingPlanQuotaLimit(zaiTeamLimits, "TOKENS_LIMIT", 6)).toEqual(zaiTeamLimits[1]);
  461. // 缺失的 monthly tool 仍返回 null
  462. expect(findCodingPlanQuotaLimit(zaiTeamLimits, "TIME_LIMIT", 5, 1)).toBeNull();
  463. });
  464. it("findCodingPlanQuotaLimit 对 bigmodel TOKENS_LIMIT 保持原行为", () => {
  465. const bigmodelLimits = [
  466. {
  467. type: "TOKENS_LIMIT",
  468. unit: 3,
  469. number: 5,
  470. percentage: 40,
  471. usageDetails: [],
  472. },
  473. {
  474. type: "TIME_LIMIT",
  475. unit: 5,
  476. number: 1,
  477. percentage: 15,
  478. usageDetails: [],
  479. },
  480. ];
  481. expect(findCodingPlanQuotaLimit(bigmodelLimits, "TOKENS_LIMIT", 3, 5)).toEqual(
  482. bigmodelLimits[0],
  483. );
  484. expect(findCodingPlanQuotaLimit(bigmodelLimits, "TIME_LIMIT", 5, 1)).toEqual(bigmodelLimits[1]);
  485. // CREDIT_LIMIT 不会误匹配 TIME_LIMIT 查询
  486. expect(findCodingPlanQuotaLimit(bigmodelLimits, "CREDIT_LIMIT", 5, 1)).toBeNull();
  487. });
  488. });