tui-tool-transcript.test.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. import assert from "node:assert/strict";
  2. import test from "node:test";
  3. import { SessionEventType, type SessionEvent } from "@zcode/contracts";
  4. import type { Message, ToolTranscriptPart } from "../src/app-model.js";
  5. import {
  6. applyToolTranscriptEvent,
  7. buildToolTranscriptProjection,
  8. } from "../src/app-tool-transcript.js";
  9. import { formatToolFilePath } from "../src/app-tool-path-display.js";
  10. import { ToolTranscriptPartView } from "../src/app-tool-components.js";
  11. test("formats Read paths relative to the workspace", () => {
  12. assert.equal(
  13. formatToolFilePath("/workspace/project/src/App.jsx", "/workspace/project"),
  14. "src/App.jsx",
  15. );
  16. assert.equal(
  17. formatToolFilePath("/workspace/other/App.jsx", "/workspace/project"),
  18. "/workspace/other/App.jsx",
  19. );
  20. assert.equal(
  21. formatToolFilePath("../other/App.jsx", "/workspace/project"),
  22. "/workspace/other/App.jsx",
  23. );
  24. assert.equal(formatToolFilePath("src/App.jsx", "/workspace/project"), "src/App.jsx");
  25. });
  26. test("formats Windows Read paths relative to the workspace", () => {
  27. assert.equal(formatToolFilePath("C:\\repo\\src\\App.jsx", "C:\\repo"), "src\\App.jsx");
  28. assert.equal(formatToolFilePath("D:\\other\\App.jsx", "C:\\repo"), "D:\\other\\App.jsx");
  29. });
  30. test("builds a Read title while keeping range details below it", () => {
  31. const projection = buildToolTranscriptProjection(
  32. "Read",
  33. {
  34. file_path: "/workspace/project/src/App.jsx",
  35. limit: 40,
  36. offset: 10,
  37. },
  38. "/workspace/project",
  39. );
  40. assert.equal(projection.title, "Read src/App.jsx");
  41. assert.deepEqual(projection.detailLines, ["offset: 10", "limit: 40"]);
  42. });
  43. test("builds an Edit title and omits duplicated file mutation fields", () => {
  44. const projection = buildToolTranscriptProjection(
  45. "Edit",
  46. {
  47. file_path: "/workspace/project/src/App.jsx",
  48. old_string: "old",
  49. new_string: "new",
  50. replace_all: false,
  51. },
  52. "/workspace/project",
  53. );
  54. assert.equal(projection.title, "Edit src/App.jsx");
  55. assert.deepEqual(projection.detailLines, []);
  56. });
  57. test("keeps workspace-external Edit paths absolute", () => {
  58. const projection = buildToolTranscriptProjection(
  59. "Edit",
  60. {
  61. file_path: "/workspace/other/App.jsx",
  62. old_string: "old",
  63. new_string: "new",
  64. },
  65. "/workspace/project",
  66. );
  67. assert.equal(projection.title, "Edit /workspace/other/App.jsx");
  68. });
  69. test("renders Read errors under the path title with error styling", () => {
  70. const state = createToolProjectionState("/workspace/project");
  71. state.apply(
  72. sessionEvent(SessionEventType.ToolCallScheduled, {
  73. input: {
  74. file_path: "/workspace/project/does-not-exist.js",
  75. },
  76. toolCallId: "call-1",
  77. toolName: "Read",
  78. }),
  79. );
  80. state.apply(
  81. sessionEvent(SessionEventType.ToolCallError, {
  82. error: {
  83. message: "File not found: /workspace/project/does-not-exist.js",
  84. type: "not_found",
  85. },
  86. toolCallId: "call-1",
  87. }),
  88. );
  89. const part = onlyToolPart(state.messages);
  90. assert.equal(part.title, "Read does-not-exist.js");
  91. assert.equal(part.status, "failed");
  92. assert.equal(part.error, "File not found: /workspace/project/does-not-exist.js");
  93. const lines = collectTextNodes(ToolTranscriptPartView({ part }));
  94. assert.equal(lines[0]?.text, "Read does-not-exist.js");
  95. assert.equal(lines[0]?.style.fg, "#94a3b8");
  96. assert.equal(lines[1]?.text, "File not found: /workspace/project/does-not-exist.js");
  97. assert.equal(lines[1]?.style.fg, "#fca5a5");
  98. });
  99. test("renders tool rows without a global left gutter", () => {
  100. const part: ToolTranscriptPart = {
  101. detailLines: ["command: npm test", "cwd: packages/tui"],
  102. status: "running",
  103. toolCallId: "call-1",
  104. toolName: "Bash",
  105. type: "tool",
  106. };
  107. const view = ToolTranscriptPartView({ part });
  108. const lines = collectTextNodes(view);
  109. assert.equal((view.props.style as Record<string, unknown>).paddingLeft, undefined);
  110. assert.equal(lines[0]?.text, "Tool Bash running");
  111. assert.equal(lines[1]?.text, " command: npm test");
  112. assert.equal(lines[2]?.text, " cwd: packages/tui");
  113. });
  114. function createToolProjectionState(workspaceDirectory: string): {
  115. apply: (event: SessionEvent) => void;
  116. messages: Message[];
  117. } {
  118. const state: { messages: Message[] } = { messages: [] };
  119. const toolNamesById = new Map<string, string>();
  120. return {
  121. apply: (event) => {
  122. applyToolTranscriptEvent(event, {
  123. setMessages: (updater) => {
  124. state.messages = typeof updater === "function" ? updater(state.messages) : updater;
  125. },
  126. toolNamesById,
  127. workspaceDirectory,
  128. });
  129. },
  130. get messages() {
  131. return state.messages;
  132. },
  133. };
  134. }
  135. function onlyToolPart(messages: Message[]): ToolTranscriptPart {
  136. const part = messages[0]?.parts?.[0];
  137. assert.equal(part?.type, "tool");
  138. return part;
  139. }
  140. function sessionEvent(type: SessionEventType, payload: unknown): SessionEvent {
  141. return {
  142. id: "event-1",
  143. payload,
  144. sequenceNumber: 1,
  145. sessionId: "session-1",
  146. timestamp: new Date(0),
  147. traceId: "trace-1",
  148. type,
  149. } as SessionEvent;
  150. }
  151. function collectTextNodes(node: unknown): Array<{ style: Record<string, unknown>; text: string }> {
  152. const lines: ReturnType<typeof collectTextNodes> = [];
  153. const visit = (value: unknown) => {
  154. if (typeof value === "string") {
  155. return value;
  156. }
  157. if (Array.isArray(value)) {
  158. return value.map((child) => visit(child)).join("");
  159. }
  160. if (typeof value !== "object" || value === null || !("props" in value)) return "";
  161. const element = value as {
  162. props?: {
  163. children?: unknown;
  164. style?: Record<string, unknown>;
  165. };
  166. type?: unknown;
  167. };
  168. const text = visit(element.props?.children);
  169. if (element.type === "text") {
  170. lines.push({ style: element.props?.style ?? {}, text });
  171. }
  172. return text;
  173. };
  174. visit(node);
  175. return lines;
  176. }