chatInputContextUsage.test.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. formatContextCacheHitRateLabel,
  4. formatContextUsageSummary,
  5. formatContextUsageTokenCount,
  6. } from "@/chat-input-toolbar/contextUsage.js";
  7. import { runContextPanelActionWithClose } from "@/chat-input-toolbar/contextPanelAction.js";
  8. describe("chat input context usage formatting", () => {
  9. it("uses uppercase English compact units", () => {
  10. expect(formatContextUsageTokenCount(1_250, "en-US")).toBe("1.3K");
  11. expect(formatContextUsageTokenCount(1_500_000, "en-US")).toBe("1.5M");
  12. expect(formatContextUsageTokenCount(2_300_000_000, "en-US")).toBe("2.3B");
  13. });
  14. it("renders the context window summary with integer compact total", () => {
  15. expect(
  16. formatContextUsageSummary({
  17. locale: "en-US",
  18. percent: 0.125,
  19. size: 1_000_000,
  20. used: 125_000,
  21. }),
  22. ).toBe("125K/1M (12.5%)");
  23. });
  24. it("uses localized compact token units for Chinese locale", () => {
  25. expect(
  26. formatContextUsageSummary({
  27. locale: "zh-CN",
  28. percent: 0.125,
  29. size: 1_000_000,
  30. used: 125_000,
  31. }),
  32. ).toBe("12.5万/100万 (12.5%)");
  33. });
  34. it("only renders cache hit rate at 78 percent or above", () => {
  35. expect(formatContextCacheHitRateLabel(0.779, "en-US")).toBeNull();
  36. expect(formatContextCacheHitRateLabel(0.78, "en-US")).toBe("78%");
  37. expect(formatContextCacheHitRateLabel(0.781, "en-US")).toBe("78.1%");
  38. expect(formatContextCacheHitRateLabel(null, "en-US")).toBeNull();
  39. });
  40. it("closes the context panel before running an inner action", () => {
  41. const calls: string[] = [];
  42. runContextPanelActionWithClose({
  43. action: () => calls.push("action"),
  44. close: () => calls.push("close"),
  45. });
  46. expect(calls).toEqual(["close", "action"]);
  47. });
  48. });