tui-tool-transcript.test.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 the tool title and indented detail rows", () => {
  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(lines[0]?.text, "Tool Bash running");
  110. assert.equal(lines[1]?.text, " command: npm test");
  111. assert.equal(lines[2]?.text, " cwd: packages/tui");
  112. });
  113. function createToolProjectionState(workspaceDirectory: string): {
  114. apply: (event: SessionEvent) => void;
  115. messages: Message[];
  116. } {
  117. const state: { messages: Message[] } = { messages: [] };
  118. const toolNamesById = new Map<string, string>();
  119. return {
  120. apply: (event) => {
  121. applyToolTranscriptEvent(event, {
  122. setMessages: (updater) => {
  123. state.messages = typeof updater === "function" ? updater(state.messages) : updater;
  124. },
  125. toolNamesById,
  126. workspaceDirectory,
  127. });
  128. },
  129. get messages() {
  130. return state.messages;
  131. },
  132. };
  133. }
  134. function onlyToolPart(messages: Message[]): ToolTranscriptPart {
  135. const part = messages[0]?.parts?.[0];
  136. assert.equal(part?.type, "tool");
  137. return part;
  138. }
  139. function sessionEvent(type: SessionEventType, payload: unknown): SessionEvent {
  140. return {
  141. id: "event-1",
  142. payload,
  143. sequenceNumber: 1,
  144. sessionId: "session-1",
  145. timestamp: new Date(0),
  146. traceId: "trace-1",
  147. type,
  148. } as SessionEvent;
  149. }
  150. function collectTextNodes(node: unknown): Array<{ style: Record<string, unknown>; text: string }> {
  151. const lines: ReturnType<typeof collectTextNodes> = [];
  152. const visit = (value: unknown) => {
  153. if (typeof value === "string") {
  154. return value;
  155. }
  156. if (Array.isArray(value)) {
  157. return value.map((child) => visit(child)).join("");
  158. }
  159. if (typeof value !== "object" || value === null || !("props" in value)) return "";
  160. const element = value as {
  161. props?: {
  162. children?: unknown;
  163. style?: Record<string, unknown>;
  164. };
  165. type?: unknown;
  166. };
  167. const text = visit(element.props?.children);
  168. if (element.type === "text") {
  169. lines.push({ style: element.props?.style ?? {}, text });
  170. }
  171. return text;
  172. };
  173. visit(node);
  174. return lines;
  175. }