footer-width.test.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import { visibleWidth } from "@earendil-works/pi-tui";
  2. import { beforeAll, describe, expect, it } from "vitest";
  3. import type { AgentSession } from "../src/core/agent-session.ts";
  4. import type { ReadonlyFooterDataProvider } from "../src/core/footer-data-provider.ts";
  5. import { FooterComponent, formatCwdForFooter } from "../src/modes/interactive/components/footer.ts";
  6. import { initTheme } from "../src/modes/interactive/theme/theme.ts";
  7. import { stripAnsi } from "../src/utils/ansi.ts";
  8. type AssistantUsage = {
  9. input: number;
  10. output: number;
  11. cacheRead: number;
  12. cacheWrite: number;
  13. cost: { total: number };
  14. };
  15. function createSession(options: {
  16. sessionName: string;
  17. modelId?: string;
  18. provider?: string;
  19. reasoning?: boolean;
  20. thinkingLevel?: string;
  21. usage?: AssistantUsage;
  22. }): AgentSession {
  23. const usage = options.usage;
  24. const entries =
  25. usage === undefined
  26. ? []
  27. : [
  28. {
  29. type: "message",
  30. message: {
  31. role: "assistant",
  32. usage,
  33. },
  34. },
  35. ];
  36. const session = {
  37. state: {
  38. model: {
  39. id: options.modelId ?? "test-model",
  40. provider: options.provider ?? "test",
  41. contextWindow: 200_000,
  42. reasoning: options.reasoning ?? false,
  43. },
  44. thinkingLevel: options.thinkingLevel ?? "off",
  45. },
  46. sessionManager: {
  47. getEntries: () => entries,
  48. getSessionName: () => options.sessionName,
  49. getCwd: () => "/tmp/project",
  50. },
  51. getContextUsage: () => ({ contextWindow: 200_000, percent: 12.3 }),
  52. modelRegistry: {
  53. isUsingOAuth: () => false,
  54. },
  55. };
  56. return session as unknown as AgentSession;
  57. }
  58. function createFooterData(providerCount: number): ReadonlyFooterDataProvider {
  59. const provider = {
  60. getGitBranch: () => "main",
  61. getExtensionStatuses: () => new Map<string, string>(),
  62. getAvailableProviderCount: () => providerCount,
  63. onBranchChange: (callback: () => void) => {
  64. void callback;
  65. return () => {};
  66. },
  67. };
  68. return provider;
  69. }
  70. describe("formatCwdForFooter", () => {
  71. it("does not abbreviate sibling paths that share the home prefix", () => {
  72. expect(formatCwdForFooter("/home/user2", "/home/user")).toBe("/home/user2");
  73. });
  74. it("abbreviates the home directory and descendants", () => {
  75. expect(formatCwdForFooter("/home/user", "/home/user")).toBe("~");
  76. expect(formatCwdForFooter("/home/user/project", "/home/user")).toBe("~/project");
  77. });
  78. });
  79. describe("FooterComponent width handling", () => {
  80. beforeAll(() => {
  81. initTheme(undefined, false);
  82. });
  83. it("keeps all lines within width for wide session names", () => {
  84. const width = 93;
  85. const session = createSession({ sessionName: "한글".repeat(30) });
  86. const footer = new FooterComponent(session, createFooterData(1));
  87. const lines = footer.render(width);
  88. for (const line of lines) {
  89. expect(visibleWidth(line)).toBeLessThanOrEqual(width);
  90. }
  91. });
  92. it("keeps stats line within width for wide model and provider names", () => {
  93. const width = 60;
  94. const session = createSession({
  95. sessionName: "",
  96. modelId: "模".repeat(30),
  97. provider: "공급자",
  98. reasoning: true,
  99. thinkingLevel: "high",
  100. usage: {
  101. input: 12_345,
  102. output: 6_789,
  103. cacheRead: 0,
  104. cacheWrite: 0,
  105. cost: { total: 1.234 },
  106. },
  107. });
  108. const footer = new FooterComponent(session, createFooterData(2));
  109. const lines = footer.render(width);
  110. for (const line of lines) {
  111. expect(visibleWidth(line)).toBeLessThanOrEqual(width);
  112. }
  113. });
  114. it("shows the latest cache hit rate when cache usage is present", () => {
  115. const session = createSession({
  116. sessionName: "",
  117. usage: {
  118. input: 100,
  119. output: 10,
  120. cacheRead: 50,
  121. cacheWrite: 50,
  122. cost: { total: 0.001 },
  123. },
  124. });
  125. const footer = new FooterComponent(session, createFooterData(1));
  126. const statsLine = stripAnsi(footer.render(120)[1]);
  127. expect(statsLine).toContain("CH25.0%");
  128. });
  129. });