@@ -0,0 +1,290 @@
1+import { afterEach, beforeEach, describe, expect, test } from "bun:test";
2+import {
3+ mkdirSync,
4+ mkdtempSync,
5+ readFileSync,
6+ rmSync,
7+ writeFileSync,
8+} from "node:fs";
9+import { tmpdir } from "node:os";
10+import { join } from "node:path";
11+import { executeEdit, executeShell, truncateOutput } from "./tools.ts";
12+
13+// ---------------------------------------------------------------------------
14+// Helpers
15+// ---------------------------------------------------------------------------
16+
17+let tmp: string;
18+
19+beforeEach(() => {
20+ tmp = mkdtempSync(join(tmpdir(), "mc-tools-"));
21+});
22+
23+afterEach(() => {
24+ rmSync(tmp, { recursive: true, force: true });
25+});
26+
27+function writeFile(name: string, content: string): string {
28+ const p = join(tmp, name);
29+ writeFileSync(p, content);
30+ return p;
31+}
32+
33+function readFile(name: string): string {
34+ return readFileSync(join(tmp, name), "utf-8");
35+}
36+
37+// ---------------------------------------------------------------------------
38+// edit
39+// ---------------------------------------------------------------------------
40+
41+describe("edit", () => {
42+ test("replaces exact text match", () => {
43+ writeFile("a.txt", "hello world");
44+ const result = executeEdit(
45+ { path: "a.txt", oldText: "hello", newText: "goodbye" },
46+ tmp,
47+ );
48+ expect(result.isError).toBe(false);
49+ expect(readFile("a.txt")).toBe("goodbye world");
50+ });
51+
52+ test("fails when old text is not found", () => {
53+ writeFile("a.txt", "hello world");
54+ const result = executeEdit(
55+ { path: "a.txt", oldText: "missing", newText: "x" },
56+ tmp,
57+ );
58+ expect(result.isError).toBe(true);
59+ expect(result.text).toContain("not found");
60+ // File unchanged
61+ expect(readFile("a.txt")).toBe("hello world");
62+ });
63+
64+ test("fails when old text matches multiple locations", () => {
65+ writeFile("a.txt", "aaa bbb aaa");
66+ const result = executeEdit(
67+ { path: "a.txt", oldText: "aaa", newText: "ccc" },
68+ tmp,
69+ );
70+ expect(result.isError).toBe(true);
71+ expect(result.text).toContain("multiple");
72+ // File unchanged
73+ expect(readFile("a.txt")).toBe("aaa bbb aaa");
74+ });
75+
76+ test("creates new file when oldText is empty", () => {
77+ const result = executeEdit(
78+ { path: "new.txt", oldText: "", newText: "brand new content" },
79+ tmp,
80+ );
81+ expect(result.isError).toBe(false);
82+ expect(readFile("new.txt")).toBe("brand new content");
83+ });
84+
85+ test("creates parent directories for new files", () => {
86+ const result = executeEdit(
87+ { path: "deep/nested/dir/file.txt", oldText: "", newText: "nested" },
88+ tmp,
89+ );
90+ expect(result.isError).toBe(false);
91+ expect(readFile("deep/nested/dir/file.txt")).toBe("nested");
92+ });
93+
94+ test("fails to create file when it already exists and oldText is empty", () => {
95+ writeFile("existing.txt", "already here");
96+ const result = executeEdit(
97+ { path: "existing.txt", oldText: "", newText: "overwrite" },
98+ tmp,
99+ );
100+ expect(result.isError).toBe(true);
101+ expect(result.text).toContain("already exists");
102+ // File unchanged
103+ expect(readFile("existing.txt")).toBe("already here");
104+ });
105+
106+ test("preserves LF line endings", () => {
107+ writeFile("lf.txt", "line1\nline2\nline3\n");
108+ executeEdit({ path: "lf.txt", oldText: "line2", newText: "replaced" }, tmp);
109+ expect(readFile("lf.txt")).toBe("line1\nreplaced\nline3\n");
110+ });
111+
112+ test("preserves CRLF line endings", () => {
113+ writeFile("crlf.txt", "line1\r\nline2\r\nline3\r\n");
114+ executeEdit(
115+ { path: "crlf.txt", oldText: "line2", newText: "replaced" },
116+ tmp,
117+ );
118+ expect(readFile("crlf.txt")).toBe("line1\r\nreplaced\r\nline3\r\n");
119+ });
120+
121+ test("handles UTF-8 content", () => {
122+ writeFile("utf8.txt", "こんにちは世界");
123+ const result = executeEdit(
124+ { path: "utf8.txt", oldText: "世界", newText: "🌍" },
125+ tmp,
126+ );
127+ expect(result.isError).toBe(false);
128+ expect(readFile("utf8.txt")).toBe("こんにちは🌍");
129+ });
130+
131+ test("handles multi-line old text", () => {
132+ writeFile("multi.txt", "function foo() {\n return 1;\n}\n");
133+ const result = executeEdit(
134+ {
135+ path: "multi.txt",
136+ oldText: "function foo() {\n return 1;\n}",
137+ newText: "function foo() {\n return 2;\n}",
138+ },
139+ tmp,
140+ );
141+ expect(result.isError).toBe(false);
142+ expect(readFile("multi.txt")).toBe("function foo() {\n return 2;\n}\n");
143+ });
144+
145+ test("resolves relative paths against cwd", () => {
146+ mkdirSync(join(tmp, "sub"), { recursive: true });
147+ writeFileSync(join(tmp, "sub", "rel.txt"), "original");
148+ const result = executeEdit(
149+ { path: "sub/rel.txt", oldText: "original", newText: "modified" },
150+ tmp,
151+ );
152+ expect(result.isError).toBe(false);
153+ expect(readFileSync(join(tmp, "sub", "rel.txt"), "utf-8")).toBe("modified");
154+ });
155+
156+ test("handles absolute paths directly", () => {
157+ const absPath = writeFile("abs.txt", "content");
158+ const result = executeEdit(
159+ { path: absPath, oldText: "content", newText: "new content" },
160+ tmp,
161+ );
162+ expect(result.isError).toBe(false);
163+ expect(readFile("abs.txt")).toBe("new content");
164+ });
165+
166+ test("fails when file does not exist for non-empty oldText", () => {
167+ const result = executeEdit(
168+ { path: "nonexistent.txt", oldText: "something", newText: "else" },
169+ tmp,
170+ );
171+ expect(result.isError).toBe(true);
172+ expect(result.text).toContain("not found");
173+ });
174+});
175+
176+// ---------------------------------------------------------------------------
177+// shell
178+// ---------------------------------------------------------------------------
179+
180+describe("shell", () => {
181+ test("returns stdout from a command", async () => {
182+ const result = await executeShell({ command: "echo hello" }, tmp);
183+ expect(result.isError).toBe(false);
184+ expect(result.text).toContain("hello");
185+ });
186+
187+ test("returns stderr from a command", async () => {
188+ const result = await executeShell({ command: "echo err >&2" }, tmp);
189+ expect(result.isError).toBe(false);
190+ expect(result.text).toContain("err");
191+ });
192+
193+ test("passes through exit code", async () => {
194+ const result = await executeShell({ command: "exit 42" }, tmp);
195+ expect(result.isError).toBe(true);
196+ expect(result.text).toContain("exit code 42");
197+ });
198+
199+ test("runs in the specified cwd", async () => {
200+ const result = await executeShell({ command: "pwd" }, tmp);
201+ expect(result.isError).toBe(false);
202+ expect(result.text).toContain(tmp);
203+ });
204+
205+ test("truncates large output", async () => {
206+ // Generate 2000 lines, which should exceed the default truncation limit
207+ const result = await executeShell({ command: "seq 1 2000" }, tmp, {
208+ maxLines: 100,
209+ });
210+ expect(result.isError).toBe(false);
211+ expect(result.text).toContain("… truncated");
212+ // Should contain head lines (near the start)
213+ expect(result.text).toContain("1\n");
214+ // Should contain tail lines (near the end)
215+ expect(result.text).toContain("2000");
216+ });
217+
218+ test("does not truncate output within the limit", async () => {
219+ const result = await executeShell({ command: "seq 1 10" }, tmp, {
220+ maxLines: 100,
221+ });
222+ expect(result.isError).toBe(false);
223+ expect(result.text).not.toContain("truncated");
224+ });
225+
226+ test("supports abort signal", async () => {
227+ const controller = new AbortController();
228+ // Abort immediately
229+ controller.abort();
230+ const result = await executeShell({ command: "sleep 10" }, tmp, {
231+ signal: controller.signal,
232+ });
233+ expect(result.isError).toBe(true);
234+ });
235+
236+ test("combines stdout and stderr in output", async () => {
237+ const result = await executeShell(
238+ { command: "echo out && echo err >&2" },
239+ tmp,
240+ );
241+ expect(result.text).toContain("out");
242+ expect(result.text).toContain("err");
243+ });
244+});
245+
246+// ---------------------------------------------------------------------------
247+// truncateOutput (pure function)
248+// ---------------------------------------------------------------------------
249+
250+describe("truncateOutput", () => {
251+ test("returns input unchanged when within limit", () => {
252+ const input = "line1\nline2\nline3";
253+ expect(truncateOutput(input, 10)).toBe(input);
254+ });
255+
256+ test("truncates keeping head and tail with marker", () => {
257+ const lines = Array.from({ length: 100 }, (_, i) => `line${i + 1}`);
258+ const input = lines.join("\n");
259+ const result = truncateOutput(input, 20);
260+
261+ expect(result).toContain("line1");
262+ expect(result).toContain("line100");
263+ expect(result).toContain("… truncated");
264+ });
265+
266+ test("head and tail do not overlap", () => {
267+ const lines = Array.from({ length: 50 }, (_, i) => `L${i + 1}`);
268+ const input = lines.join("\n");
269+ const result = truncateOutput(input, 20);
270+
271+ // Count total lines (head + marker + tail)
272+ const resultLines = result.split("\n");
273+ // Should be ≤ maxLines + 1 (for the marker line)
274+ expect(resultLines.length).toBeLessThanOrEqual(21);
275+ });
276+
277+ test("handles empty input", () => {
278+ expect(truncateOutput("", 10)).toBe("");
279+ });
280+
281+ test("handles single-line input", () => {
282+ expect(truncateOutput("hello", 10)).toBe("hello");
283+ });
284+
285+ test("handles input at exactly the limit", () => {
286+ const lines = Array.from({ length: 10 }, (_, i) => `line${i + 1}`);
287+ const input = lines.join("\n");
288+ expect(truncateOutput(input, 10)).toBe(input);
289+ });
290+});