highspeedSavedTime.test.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import { describe, expect, it, vi } from "vitest";
  2. import {
  3. calculateHighspeedSavedDurationMs,
  4. calculateHighspeedSavedDurationFromMetrics,
  5. readHighspeedRegularTpsFromHealth,
  6. } from "@/highspeed/highspeedSavedTime.js";
  7. describe("highspeed saved time", () => {
  8. it("通过 Service 轻量健康方法读取普通 TPS,不在 Renderer 重复缓存", async () => {
  9. const getCodingPlanRegularTps = vi.fn().mockResolvedValue(73);
  10. const usageStatsService = { getCodingPlanRegularTps } as never;
  11. const accountAccess = {
  12. type: "zhipu-account",
  13. accountType: "zai",
  14. mode: "individual-coding-plan",
  15. entitled: true,
  16. } as const;
  17. await expect(
  18. readHighspeedRegularTpsFromHealth({
  19. usageStatsService,
  20. preferredProviderId: "zai-coding-plan",
  21. accountAccess,
  22. }),
  23. ).resolves.toBe(73);
  24. await expect(
  25. readHighspeedRegularTpsFromHealth({
  26. usageStatsService,
  27. preferredProviderId: "zai-coding-plan",
  28. accountAccess,
  29. }),
  30. ).resolves.toBe(73);
  31. expect(getCodingPlanRegularTps).toHaveBeenCalledTimes(2);
  32. // Provider 重构后请求期鉴权只能由 accountAccess 解析:漏传会让 host 找不到授权并抛错,
  33. // 表现为节省时间静默消失,因此这里显式钉住透传契约。
  34. expect(getCodingPlanRegularTps).toHaveBeenLastCalledWith(
  35. expect.objectContaining({ preferredProviderId: "zai-coding-plan", accountAccess }),
  36. );
  37. });
  38. it("按工具 20% / 推理 80% 估算每轮节省时间并把负值收口为零", () => {
  39. expect(
  40. calculateHighspeedSavedDurationMs({
  41. outputTokens: 9_000,
  42. regularTps: 30,
  43. highspeedTps: 90,
  44. durationMs: 100_000,
  45. }),
  46. ).toBe(114_286);
  47. expect(
  48. calculateHighspeedSavedDurationMs({
  49. outputTokens: 100,
  50. regularTps: 100,
  51. highspeedTps: 50,
  52. durationMs: 2_000,
  53. }),
  54. ).toBe(0);
  55. });
  56. it("优先使用 CLI 持久化的模型/工具真实耗时", () => {
  57. expect(
  58. calculateHighspeedSavedDurationMs({
  59. outputTokens: 9_000,
  60. regularTps: 30,
  61. highspeedTps: 90,
  62. durationMs: 100_000,
  63. modelDurationMs: 40_000,
  64. toolDurationMs: 50_000,
  65. otherDurationMs: 10_000,
  66. }),
  67. ).toBe(80_000);
  68. });
  69. it("持久化前计算节省时间时透传真实耗时拆分", () => {
  70. expect(
  71. calculateHighspeedSavedDurationFromMetrics(
  72. {
  73. outputTokens: 9_000,
  74. durationMs: 100_000,
  75. modelDurationMs: 40_000,
  76. toolDurationMs: 50_000,
  77. otherDurationMs: 10_000,
  78. },
  79. 30,
  80. 90,
  81. ),
  82. ).toBe(80_000);
  83. });
  84. });