@@ -1,151 +0,0 @@
1-import { afterEach, beforeEach, describe, expect, test } from "bun:test";
2-import { replaceTool } from "./replace.ts";
3-import { createTestHelpers, registerAnchorErrorTests } from "./test-helpers.ts";
4-import { stripWriteResultMeta } from "./write-result.ts";
5-
6-const { setup, teardown, write, read, anchor, getDir } = createTestHelpers();
7-// diff is deferred; strip meta before asserting on public fields.
8-const execute = async (...args: Parameters<typeof replaceTool.execute>) =>
9- stripWriteResultMeta(await replaceTool.execute(...args));
10-
11-// ---------------------------------------------------------------------------
12-// Setup / teardown
13-// ---------------------------------------------------------------------------
14-
15-beforeEach(async () => {
16- await setup("mc-replace-test-");
17-});
18-
19-afterEach(async () => {
20- await teardown();
21-});
22-
23-// ---------------------------------------------------------------------------
24-// Tests
25-// ---------------------------------------------------------------------------
26-
27-describe("replaceTool", () => {
28- test("replaces a single line", async () => {
29- const name = await write("f.txt", "a\nb\nc\n");
30- const result = await execute({
31- path: name,
32- cwd: getDir(),
33- startAnchor: anchor(2, "b"),
34- newContent: "B",
35- });
36-
37- expect(await read(name)).toBe("a\nB\nc\n");
38- expect(result.diff).toContain("-b");
39- expect(result.diff).toContain("+B");
40- });
41-
42- test("replaces a range of lines", async () => {
43- const name = await write("f.txt", "a\nb\nc\nd\ne\n");
44- const result = await execute({
45- path: name,
46- cwd: getDir(),
47- startAnchor: anchor(2, "b"),
48- endAnchor: anchor(4, "d"),
49- newContent: "X\nY",
50- });
51-
52- expect(await read(name)).toBe("a\nX\nY\ne\n");
53- expect(result.diff).toContain("-b");
54- expect(result.diff).toContain("-c");
55- expect(result.diff).toContain("-d");
56- expect(result.diff).toContain("+X");
57- expect(result.diff).toContain("+Y");
58- });
59-
60- test("deletes a single line when newContent is omitted", async () => {
61- const name = await write("f.txt", "a\nb\nc\n");
62- const result = await execute({
63- path: name,
64- cwd: getDir(),
65- startAnchor: anchor(2, "b"),
66- });
67-
68- expect(await read(name)).toBe("a\nc\n");
69- expect(result.diff).toContain("-b");
70- expect(result.diff).not.toMatch(/^\+[^+]/m);
71- });
72-
73- test("deletes a range when newContent is empty string", async () => {
74- const name = await write("f.txt", "a\nb\nc\nd\n");
75- await execute({
76- path: name,
77- cwd: getDir(),
78- startAnchor: anchor(2, "b"),
79- endAnchor: anchor(3, "c"),
80- newContent: "",
81- });
82-
83- expect(await read(name)).toBe("a\nd\n");
84- });
85-
86- test("returns (no changes) diff when replacement is identical to original", async () => {
87- const name = await write("f.txt", "a\nb\nc\n");
88- const result = await execute({
89- path: name,
90- cwd: getDir(),
91- startAnchor: anchor(2, "b"),
92- newContent: "b",
93- });
94-
95- expect(result.diff).toBe("(no changes)");
96- });
97-
98- test("returns path relative to cwd", async () => {
99- const name = await write("f.txt", "a\nb\n");
100- const result = await execute({
101- path: name,
102- cwd: getDir(),
103- startAnchor: anchor(1, "a"),
104- newContent: "A",
105- });
106-
107- expect(result.path).toBe(name);
108- });
109-
110- registerAnchorErrorTests({
111- missingFileCall: (a) =>
112- execute({ path: "missing.txt", cwd: getDir(), startAnchor: a }),
113- badAnchorCall: (name, a) =>
114- execute({ path: name, cwd: getDir(), startAnchor: a }), // wrong hash for "b"
115- write,
116- });
117-
118- test("throws when endAnchor is before startAnchor", async () => {
119- const name = await write("f.txt", "a\nb\nc\n");
120- await expect(
121- execute({
122- path: name,
123- cwd: getDir(),
124- startAnchor: anchor(3, "c"),
125- endAnchor: anchor(1, "a"),
126- }),
127- ).rejects.toThrow();
128- });
129-
130- test("throws on malformed anchor format", async () => {
131- const name = await write("f.txt", "a\n");
132- await expect(
133- execute({
134- path: name,
135- cwd: getDir(),
136- startAnchor: "not-an-anchor",
137- }),
138- ).rejects.toThrow("Invalid startAnchor");
139- });
140- test("accepts anchors with trailing pipe separator", async () => {
141- const name = await write("f.txt", "a\nb\nc\n");
142- await execute({
143- path: name,
144- cwd: getDir(),
145- startAnchor: `${anchor(2, "b")}|`,
146- newContent: "B",
147- });
148-
149- expect(await read(name)).toBe("a\nB\nc\n");
150- });
151-});