@criogaid/pi-hashline-edit 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +59 -50
- package/package.json +8 -3
- package/src/core/apply.test.ts +366 -310
- package/src/core/apply.ts +269 -202
- package/src/core/hash.test.ts +61 -23
- package/src/core/hash.ts +45 -16
- package/src/core/lines.test.ts +46 -27
- package/src/core/lines.ts +36 -28
- package/src/core/ranges.test.ts +73 -19
- package/src/core/ranges.ts +15 -15
- package/src/core/text.test.ts +3 -3
- package/src/core/text.ts +28 -9
- package/src/core/types.ts +54 -36
- package/src/index.ts +22 -16
- package/src/integration/grep-rg.test.ts +834 -219
- package/src/integration/large-text.test.ts +62 -0
- package/src/integration/replace-package.test.ts +37 -0
- package/src/pi/action-fusion.test.ts +535 -206
- package/src/pi/action-fusion.ts +469 -244
- package/src/pi/anchor-format.test.ts +30 -19
- package/src/pi/anchor-format.ts +27 -14
- package/src/pi/budgets.ts +20 -0
- package/src/pi/config.test.ts +60 -30
- package/src/pi/config.ts +45 -32
- package/src/pi/directory-sync.test.ts +55 -0
- package/src/pi/e2e-scenarios.test.ts +359 -0
- package/src/pi/edit-tool.ts +426 -262
- package/src/pi/error-text.ts +21 -0
- package/src/pi/execute.test.ts +1594 -694
- package/src/pi/failure-context.test.ts +105 -83
- package/src/pi/failure-context.ts +83 -71
- package/src/pi/failure-path.test.ts +171 -64
- package/src/pi/file-commit.test.ts +446 -233
- package/src/pi/file-commit.ts +347 -203
- package/src/pi/fusion-card.test.ts +118 -83
- package/src/pi/fusion-card.ts +120 -85
- package/src/pi/grep-output.ts +208 -0
- package/src/pi/grep-render.ts +58 -0
- package/src/pi/grep-scope.ts +152 -0
- package/src/pi/grep-search.ts +217 -0
- package/src/pi/grep-tool.ts +191 -766
- package/src/pi/grep.test.ts +562 -150
- package/src/pi/mutation-result.test.ts +37 -0
- package/src/pi/mutation-result.ts +107 -59
- package/src/pi/mutation-runner.ts +168 -0
- package/src/pi/path.ts +20 -12
- package/src/pi/pi.test.ts +95 -52
- package/src/pi/read-tool.ts +219 -130
- package/src/pi/registration.test.ts +274 -113
- package/src/pi/render.test.ts +218 -83
- package/src/pi/render.ts +152 -95
- package/src/pi/replace-apply.ts +109 -0
- package/src/pi/replace-tool.ts +280 -240
- package/src/pi/replace-worker.mjs +4 -0
- package/src/pi/replace-worker.ts +10 -0
- package/src/pi/replace.test.ts +773 -389
- package/src/pi/result-safety.test.ts +779 -342
- package/src/pi/rg-line-filter.test.ts +64 -36
- package/src/pi/rg-line-filter.ts +40 -124
- package/src/pi/rg-line-ranges.test.ts +57 -31
- package/src/pi/rg-line-ranges.ts +44 -82
- package/src/pi/rg-text-view.ts +106 -36
- package/src/pi/schema.ts +27 -0
- package/src/pi/state.ts +7 -7
- package/src/pi/text-stream.test.ts +118 -0
- package/src/pi/text-stream.ts +136 -0
- package/src/pi/tool-call.testing.ts +37 -0
- package/src/pi/write-tool.ts +115 -52
- package/src/pi/write.test.ts +248 -96
package/src/pi/replace.test.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* pi integration tests for the `replace` tool: literal replaceAll, regex with
|
|
3
|
-
* capture groups, flags,
|
|
3
|
+
* capture groups, flags, 0-match / invalid-pattern
|
|
4
4
|
* failures (signaled by throwing — pi's failure contract), diff + fresh-anchor
|
|
5
5
|
* return, and chaining a hashline `edit` on an anchor returned by `replace`.
|
|
6
6
|
*
|
|
@@ -11,469 +11,853 @@ import assert from "node:assert/strict";
|
|
|
11
11
|
import { mkdtemp, rm, readFile, writeFile } from "node:fs/promises";
|
|
12
12
|
import { tmpdir } from "node:os";
|
|
13
13
|
import { join } from "node:path";
|
|
14
|
-
import { initTheme } from "@earendil-works/pi-coding-agent";
|
|
14
|
+
import { initTheme, type Theme } from "@earendil-works/pi-coding-agent";
|
|
15
15
|
import { makeReplaceTool } from "./replace-tool.ts";
|
|
16
16
|
import { makeEditOverride } from "./edit-tool.ts";
|
|
17
17
|
import { validateToolArguments } from "@earendil-works/pi-ai";
|
|
18
|
+
import { callTool } from "./tool-call.testing.ts";
|
|
18
19
|
import { createActionFusionExecutor } from "./action-fusion.ts";
|
|
20
|
+
import { generateMutationDetails } from "./mutation-result.ts";
|
|
19
21
|
|
|
20
22
|
async function withDir<T>(fn: (dir: string) => Promise<T>): Promise<T> {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
23
|
+
const dir = await mkdtemp(join(tmpdir(), "hl-replace-"));
|
|
24
|
+
try {
|
|
25
|
+
return await fn(dir);
|
|
26
|
+
} finally {
|
|
27
|
+
await rm(dir, { recursive: true, force: true });
|
|
28
|
+
}
|
|
27
29
|
}
|
|
28
30
|
|
|
29
31
|
const call = (tool: any, params: any) => tool.execute("0", params, undefined, undefined);
|
|
30
32
|
|
|
31
33
|
/** Extract a `LINE#HASH` anchor for `line` from a result text block. */
|
|
32
34
|
function anchorLine(block: string, line: number) {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
35
|
+
const m = new RegExp(`^${line}#([0-9A-Z]+)(?:│|$)`, "m").exec(block);
|
|
36
|
+
if (!m) throw new Error(`line ${line} anchor not found in block`);
|
|
37
|
+
return `${line}#${m[1]}`;
|
|
36
38
|
}
|
|
37
39
|
|
|
38
|
-
const stubTheme = { fg: (_k: string, s: string) => s, bold: (s: string) => s };
|
|
40
|
+
const stubTheme = { fg: (_k: string, s: string) => s, bold: (s: string) => s } as Theme;
|
|
39
41
|
|
|
40
42
|
// renderResult delegates to pi's renderDiff, which reads the global TUI theme
|
|
41
43
|
// singleton — initialize it once for this test process (watcher off by default).
|
|
42
44
|
initTheme();
|
|
43
45
|
|
|
44
|
-
test("replace guidance distinguishes literal
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
46
|
+
test("replace find guidance distinguishes literal escapes and regex line anchors", () => {
|
|
47
|
+
const tool = makeReplaceTool(process.cwd());
|
|
48
|
+
const description: unknown = Reflect.get(
|
|
49
|
+
tool.parameters.properties.replacements.items.properties.find,
|
|
50
|
+
"description",
|
|
51
|
+
);
|
|
52
|
+
assert.ok(typeof description === "string");
|
|
53
|
+
assert.match(description, /JavaScript regex/);
|
|
54
|
+
assert.match(description, /not grep's ripgrep syntax/);
|
|
55
|
+
assert.match(description, /CRLF.*LF.*\\n/);
|
|
56
|
+
assert.match(description, /literal mode.*backslash followed by n.*two characters/);
|
|
57
|
+
assert.match(description, /regex mode.*\^ and \$.*m flag.*per line/);
|
|
50
58
|
});
|
|
51
59
|
|
|
60
|
+
test("replace schema requires a non-empty replacements array", () => {
|
|
61
|
+
for (const fusion of [undefined, createActionFusionExecutor()]) {
|
|
62
|
+
const tool = makeReplaceTool(process.cwd(), fusion);
|
|
63
|
+
assert.equal(Object.hasOwn(tool.parameters.properties, "find"), false);
|
|
64
|
+
const check = (args: Parameters<typeof validateToolArguments>[1]["arguments"]) =>
|
|
65
|
+
validateToolArguments(tool, { type: "toolCall", id: "0", name: "replace", arguments: args });
|
|
66
|
+
assert.doesNotThrow(() =>
|
|
67
|
+
check({ path: "f.txt", replacements: [{ find: "old", replace: "new" }] }),
|
|
68
|
+
);
|
|
69
|
+
const invalidArgs: Parameters<typeof validateToolArguments>[1]["arguments"][] = [
|
|
70
|
+
{ path: "f.txt", find: "old", replace: "new" },
|
|
71
|
+
{ path: "f.txt", replacements: [] },
|
|
72
|
+
{ path: "f.txt", replacements: [{ find: "old", replace: "new" }], find: "old" },
|
|
73
|
+
{ path: "f.txt", replacements: '[{"find":"old","replace":"new"}]' },
|
|
74
|
+
...[1, null, false].map((maxMatches) => ({
|
|
75
|
+
path: "f.txt",
|
|
76
|
+
replacements: [{ find: "old", replace: "new", maxMatches }],
|
|
77
|
+
})),
|
|
78
|
+
];
|
|
79
|
+
for (const args of invalidArgs) {
|
|
80
|
+
assert.throws(() => check(args));
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
test("replace schema rejects empty paths, empty find, and unsupported flags", () => {
|
|
86
|
+
const tool = makeReplaceTool(process.cwd());
|
|
87
|
+
for (const args of [
|
|
88
|
+
{ path: "", replacements: [{ find: "old", replace: "new" }] },
|
|
89
|
+
{ path: "f.txt", replacements: [{ find: "", replace: "new" }] },
|
|
90
|
+
{ path: "f.txt", replacements: [{ find: "old", replace: "new", flags: "x" }] },
|
|
91
|
+
]) {
|
|
92
|
+
assert.throws(
|
|
93
|
+
() =>
|
|
94
|
+
validateToolArguments(tool, {
|
|
95
|
+
type: "toolCall",
|
|
96
|
+
id: "invalid",
|
|
97
|
+
name: "replace",
|
|
98
|
+
arguments: args,
|
|
99
|
+
}),
|
|
100
|
+
/Validation failed/,
|
|
101
|
+
JSON.stringify(args),
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
test("replace rejects top-level rules without publishing", async () =>
|
|
107
|
+
withDir(async (dir) => {
|
|
108
|
+
const file = join(dir, "f.txt");
|
|
109
|
+
await writeFile(file, "old\n");
|
|
110
|
+
await assert.rejects(
|
|
111
|
+
callTool(makeReplaceTool(dir), {
|
|
112
|
+
path: file,
|
|
113
|
+
find: "old",
|
|
114
|
+
replace: "new",
|
|
115
|
+
}),
|
|
116
|
+
/Validation failed for tool "replace"[\s\S]*replacements/,
|
|
117
|
+
);
|
|
118
|
+
assert.equal(await readFile(file, "utf8"), "old\n");
|
|
119
|
+
}));
|
|
120
|
+
|
|
121
|
+
test("replace rejects unsupported per-rule fields before publishing", async () =>
|
|
122
|
+
withDir(async (dir) => {
|
|
123
|
+
const file = join(dir, "f.txt");
|
|
124
|
+
const before = "foo foo foo\n";
|
|
125
|
+
await writeFile(file, before);
|
|
126
|
+
const tool = makeReplaceTool(dir);
|
|
127
|
+
for (const maxMatches of [1, null, false]) {
|
|
128
|
+
await assert.rejects(
|
|
129
|
+
callTool(tool, { path: file, replacements: [{ find: "foo", replace: "bar", maxMatches }] }),
|
|
130
|
+
/Validation failed for tool "replace"[\s\S]*maxMatches/,
|
|
131
|
+
);
|
|
132
|
+
assert.equal(await readFile(file, "utf8"), before);
|
|
133
|
+
}
|
|
134
|
+
}));
|
|
135
|
+
|
|
136
|
+
test("replace rejects unwritable text and invalid regex before reading a missing target", async () =>
|
|
137
|
+
withDir(async (dir) => {
|
|
138
|
+
const file = join(dir, "missing.txt");
|
|
139
|
+
const tool = makeReplaceTool(dir);
|
|
140
|
+
const valid = { path: file, replacements: [{ find: "x", replace: "y", regex: true }] };
|
|
141
|
+
assert.equal(tool.prepareArguments(valid), valid);
|
|
142
|
+
for (const [rule, expected] of [
|
|
143
|
+
[
|
|
144
|
+
{ find: "x", replace: "\0" },
|
|
145
|
+
/Invalid argument replacements\[0\]\.replace: UNSUPPORTED_TEXT:/,
|
|
146
|
+
],
|
|
147
|
+
[
|
|
148
|
+
{ find: "x", replace: "\ud800" },
|
|
149
|
+
/Invalid argument replacements\[0\]\.replace: INVALID_UNICODE:/,
|
|
150
|
+
],
|
|
151
|
+
[
|
|
152
|
+
{ find: "(", replace: "x", regex: true },
|
|
153
|
+
/Invalid argument replacements\[0\]\.find: invalid regex/,
|
|
154
|
+
],
|
|
155
|
+
] as const) {
|
|
156
|
+
await assert.rejects(callTool(tool, { path: file, replacements: [rule] }), expected);
|
|
157
|
+
await assert.rejects(readFile(file, "utf8"), { code: "ENOENT" });
|
|
158
|
+
}
|
|
159
|
+
await assert.rejects(
|
|
160
|
+
callTool(tool, {
|
|
161
|
+
path: file,
|
|
162
|
+
replacements: [{ find: "(", replace: "x", regex: true, flags: "z" }],
|
|
163
|
+
}),
|
|
164
|
+
/Validation failed for tool "replace":[\s\S]*replacements\.0\.flags: must match pattern/,
|
|
165
|
+
);
|
|
166
|
+
await assert.rejects(readFile(file, "utf8"), { code: "ENOENT" });
|
|
167
|
+
}));
|
|
168
|
+
|
|
52
169
|
test("replace literal: replaces all occurrences", async () => {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
170
|
+
await withDir(async (dir) => {
|
|
171
|
+
const f = join(dir, "f.txt");
|
|
172
|
+
await writeFile(f, "foo bar foo baz foo\n");
|
|
173
|
+
const r: any = await call(makeReplaceTool(dir), {
|
|
174
|
+
path: "f.txt",
|
|
175
|
+
replacements: [{ find: "foo", replace: "qux" }],
|
|
176
|
+
});
|
|
177
|
+
assert.equal(r.isError, undefined);
|
|
178
|
+
assert.equal(await readFile(f, "utf-8"), "qux bar qux baz qux\n");
|
|
179
|
+
assert.match(r.content[0].text, /3 matches/);
|
|
180
|
+
});
|
|
61
181
|
});
|
|
62
182
|
|
|
63
|
-
test("literal multiline replace matches LF and CRLF while retaining local separators", async () =>
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
})
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
183
|
+
test("literal multiline replace matches LF and CRLF while retaining local separators", async () =>
|
|
184
|
+
withDir(async (dir) => {
|
|
185
|
+
const file = join(dir, "mixed.txt");
|
|
186
|
+
await writeFile(file, "\uFEFFhead\r\nold\r\none\r\nmid\nold\none\nend\r\n");
|
|
187
|
+
const result: any = await call(makeReplaceTool(dir), {
|
|
188
|
+
path: "mixed.txt",
|
|
189
|
+
replacements: [{ find: "old\none", replace: "NEW\nX" }],
|
|
190
|
+
});
|
|
191
|
+
assert.match(result.content[0].text, /2 matches/);
|
|
192
|
+
assert.equal(await readFile(file, "utf8"), "\uFEFFhead\r\nNEW\r\nX\r\nmid\nNEW\nX\nend\r\n");
|
|
193
|
+
await call(makeReplaceTool(dir), {
|
|
194
|
+
path: "mixed.txt",
|
|
195
|
+
replacements: [{ find: "NEW\r\nX", replace: "done" }],
|
|
196
|
+
});
|
|
197
|
+
assert.equal(await readFile(file, "utf8"), "\uFEFFhead\r\ndone\r\nmid\ndone\nend\r\n");
|
|
198
|
+
}));
|
|
199
|
+
|
|
200
|
+
test("both replacement modes preserve each matched separator", async () =>
|
|
201
|
+
withDir(async (dir) => {
|
|
202
|
+
const file = join(dir, "mixed.txt");
|
|
203
|
+
for (const regex of [false, true]) {
|
|
204
|
+
await writeFile(file, "a\r\nb\nc\r\ntail\n");
|
|
205
|
+
await call(makeReplaceTool(dir), {
|
|
206
|
+
path: "mixed.txt",
|
|
207
|
+
replacements: [{ find: "a\nb\nc", replace: "x\ny\nz\nextra", regex }],
|
|
208
|
+
});
|
|
209
|
+
assert.equal(await readFile(file, "utf8"), "x\r\ny\nz\nextra\r\ntail\n");
|
|
210
|
+
}
|
|
211
|
+
}));
|
|
212
|
+
|
|
213
|
+
test("literal replacement uses one LF view and preserves raw bytes across query styles", async () =>
|
|
214
|
+
withDir(async (dir) => {
|
|
215
|
+
const file = join(dir, "f.txt");
|
|
216
|
+
for (const find of ["a\nb\nc", "a\r\nb\r\nc", "a\r\nb\nc"]) {
|
|
217
|
+
const before = "\uFEFFhead\r\na\r\nb\nc\rtail";
|
|
218
|
+
await writeFile(file, before);
|
|
219
|
+
const result = await call(makeReplaceTool(dir), {
|
|
220
|
+
path: file,
|
|
221
|
+
replacements: [{ find, replace: find }],
|
|
222
|
+
});
|
|
223
|
+
assert.equal(await readFile(file, "utf8"), before);
|
|
224
|
+
assert.match(result.content[0].text, /no net change/);
|
|
225
|
+
}
|
|
226
|
+
for (const [before, find, replacement, expected] of [
|
|
227
|
+
["a\r\nb", "a", "A\nextra", "A\r\nextra\r\nb"],
|
|
228
|
+
["a\nb", "a\r\nb", "A\r\nB", "A\nB"],
|
|
229
|
+
["a\r\nb", "\n", "", "ab"],
|
|
230
|
+
["a\r\nb", "\n", "\n", "a\r\nb"],
|
|
231
|
+
["a\rb\r\r\nc", "a\rb\r\r\nc", "x\ry\r\r\nz", "x\ry\r\r\nz"],
|
|
232
|
+
]) {
|
|
233
|
+
await writeFile(file, before);
|
|
234
|
+
await call(makeReplaceTool(dir), {
|
|
235
|
+
path: file,
|
|
236
|
+
replacements: [{ find, replace: replacement }],
|
|
237
|
+
});
|
|
238
|
+
assert.equal(await readFile(file, "utf8"), expected);
|
|
239
|
+
}
|
|
240
|
+
}));
|
|
241
|
+
|
|
242
|
+
test("mixed literal and regex batches share raw offsets after CRLF normalization", async () =>
|
|
243
|
+
withDir(async (dir) => {
|
|
244
|
+
const file = join(dir, "f.txt");
|
|
245
|
+
await writeFile(file, "😀\r\na\r\nb\r\nc");
|
|
246
|
+
await call(makeReplaceTool(dir), {
|
|
247
|
+
path: file,
|
|
248
|
+
replacements: [
|
|
249
|
+
{ find: "a\nb", replace: "A\nB" },
|
|
250
|
+
{ find: "(c)$", replace: "$1!", regex: true },
|
|
251
|
+
],
|
|
252
|
+
});
|
|
253
|
+
assert.equal(await readFile(file, "utf8"), "😀\r\nA\r\nB\r\nc!");
|
|
254
|
+
const before = await readFile(file, "utf8");
|
|
255
|
+
await assert.rejects(
|
|
256
|
+
call(makeReplaceTool(dir), {
|
|
257
|
+
path: file,
|
|
258
|
+
replacements: [
|
|
259
|
+
{ find: "A\nB", replace: "x" },
|
|
260
|
+
{ find: "\\nB", replace: "y", regex: true },
|
|
261
|
+
],
|
|
262
|
+
}),
|
|
263
|
+
/overlap/,
|
|
264
|
+
);
|
|
265
|
+
assert.equal(await readFile(file, "utf8"), before);
|
|
266
|
+
}));
|
|
119
267
|
|
|
120
268
|
test("replace literal: $ in replacement stays literal (no expansion)", async () => {
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
269
|
+
await withDir(async (dir) => {
|
|
270
|
+
const f = join(dir, "f.txt");
|
|
271
|
+
await writeFile(f, "cost: $5 here\n");
|
|
272
|
+
const r: any = await call(makeReplaceTool(dir), {
|
|
273
|
+
path: "f.txt",
|
|
274
|
+
replacements: [{ find: "$5", replace: "$10" }],
|
|
275
|
+
});
|
|
276
|
+
assert.equal(r.isError, undefined);
|
|
277
|
+
// literal mode: "$10" inserted verbatim, NOT interpreted as group-10 + "0"
|
|
278
|
+
assert.equal(await readFile(f, "utf-8"), "cost: $10 here\n");
|
|
279
|
+
});
|
|
129
280
|
});
|
|
130
281
|
|
|
131
282
|
test("replace literal: case-insensitive via flags", async () => {
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
283
|
+
await withDir(async (dir) => {
|
|
284
|
+
const f = join(dir, "f.txt");
|
|
285
|
+
await writeFile(f, "Foo fOo FOO\n");
|
|
286
|
+
await call(makeReplaceTool(dir), {
|
|
287
|
+
path: "f.txt",
|
|
288
|
+
replacements: [{ find: "foo", replace: "x", flags: "i" }],
|
|
289
|
+
});
|
|
290
|
+
assert.equal(await readFile(f, "utf-8"), "x x x\n");
|
|
291
|
+
});
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
test("replace aborts catastrophic regex without blocking the event loop or publishing", async () => {
|
|
295
|
+
await withDir(async (dir) => {
|
|
296
|
+
const file = join(dir, "f.txt");
|
|
297
|
+
const before = `${"a".repeat(30)}!`;
|
|
298
|
+
await writeFile(file, before);
|
|
299
|
+
const controller = new AbortController();
|
|
300
|
+
const timer = setTimeout(() => controller.abort(), 100);
|
|
301
|
+
const start = performance.now();
|
|
302
|
+
try {
|
|
303
|
+
await assert.rejects(
|
|
304
|
+
makeReplaceTool(dir).execute(
|
|
305
|
+
"abort",
|
|
306
|
+
{ path: file, replacements: [{ find: "^(a+)+$", replace: "x", regex: true }] },
|
|
307
|
+
controller.signal,
|
|
308
|
+
undefined,
|
|
309
|
+
{ cwd: dir } as Parameters<ReturnType<typeof makeReplaceTool>["execute"]>[4],
|
|
310
|
+
),
|
|
311
|
+
(error: unknown) =>
|
|
312
|
+
error instanceof Error &&
|
|
313
|
+
error.message === `Operation aborted before apply; ${file} was not changed.`,
|
|
314
|
+
);
|
|
315
|
+
assert.ok(performance.now() - start < 2000);
|
|
316
|
+
assert.equal(await readFile(file, "utf8"), before);
|
|
317
|
+
} finally {
|
|
318
|
+
clearTimeout(timer);
|
|
319
|
+
}
|
|
320
|
+
});
|
|
321
|
+
});
|
|
322
|
+
test("replace times out catastrophic regex without publishing", async () => {
|
|
323
|
+
await withDir(async (dir) => {
|
|
324
|
+
const file = join(dir, "f.txt");
|
|
325
|
+
const before = `${"a".repeat(35)}!`;
|
|
326
|
+
await writeFile(file, before);
|
|
327
|
+
await assert.rejects(
|
|
328
|
+
call(makeReplaceTool(dir), {
|
|
329
|
+
path: file,
|
|
330
|
+
replacements: [{ find: "^(a+)+$", replace: "x", regex: true }],
|
|
331
|
+
}),
|
|
332
|
+
(error: unknown) =>
|
|
333
|
+
error instanceof Error &&
|
|
334
|
+
error.message === `Replace ${file}: regex evaluation timed out after 5000ms`,
|
|
335
|
+
);
|
|
336
|
+
assert.equal(await readFile(file, "utf8"), before);
|
|
337
|
+
});
|
|
138
338
|
});
|
|
139
339
|
|
|
140
340
|
test("replace regex: capture groups in replacement", async () => {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
assert.equal(await readFile(f, "utf-8"), "user: alice\nuser: bob\n");
|
|
151
|
-
});
|
|
341
|
+
await withDir(async (dir) => {
|
|
342
|
+
const f = join(dir, "f.txt");
|
|
343
|
+
await writeFile(f, "name: alice\nname: bob\n");
|
|
344
|
+
await call(makeReplaceTool(dir), {
|
|
345
|
+
path: "f.txt",
|
|
346
|
+
replacements: [{ find: "name: (\\w+)", replace: "user: $1", regex: true }],
|
|
347
|
+
});
|
|
348
|
+
assert.equal(await readFile(f, "utf-8"), "user: alice\nuser: bob\n");
|
|
349
|
+
});
|
|
152
350
|
});
|
|
153
351
|
|
|
154
352
|
test("replace regex: multiline flag matches line-anchored pattern", async () => {
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
353
|
+
await withDir(async (dir) => {
|
|
354
|
+
const f = join(dir, "f.txt");
|
|
355
|
+
await writeFile(f, "a\nb\nc\n");
|
|
356
|
+
// without `m`, ^a$ wouldn't match (a isn't at end of string); with `m` it does
|
|
357
|
+
await call(makeReplaceTool(dir), {
|
|
358
|
+
path: "f.txt",
|
|
359
|
+
replacements: [{ find: "^a$", replace: "A", regex: true, flags: "m" }],
|
|
360
|
+
});
|
|
361
|
+
assert.equal(await readFile(f, "utf-8"), "A\nb\nc\n");
|
|
362
|
+
});
|
|
162
363
|
});
|
|
163
364
|
|
|
164
365
|
test("replace regex: dotall flag makes . match newlines", async () => {
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
366
|
+
await withDir(async (dir) => {
|
|
367
|
+
const f = join(dir, "f.txt");
|
|
368
|
+
await writeFile(f, "a\nb\n");
|
|
369
|
+
// a.b only matches across the newline with the `s` (dotall) flag
|
|
370
|
+
await call(makeReplaceTool(dir), {
|
|
371
|
+
path: "f.txt",
|
|
372
|
+
replacements: [{ find: "a.b", replace: "X", regex: true, flags: "s" }],
|
|
373
|
+
});
|
|
374
|
+
assert.equal(await readFile(f, "utf-8"), "X\n");
|
|
375
|
+
});
|
|
172
376
|
});
|
|
173
377
|
|
|
174
378
|
test("replace: 0 matches throws", async () => {
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
379
|
+
await withDir(async (dir) => {
|
|
380
|
+
const f = join(dir, "f.txt");
|
|
381
|
+
await writeFile(f, "a\nb\n");
|
|
382
|
+
await assert.rejects(
|
|
383
|
+
call(makeReplaceTool(dir), { path: "f.txt", replacements: [{ find: "zzz", replace: "y" }] }),
|
|
384
|
+
/no matches/,
|
|
385
|
+
);
|
|
386
|
+
assert.equal(await readFile(f, "utf-8"), "a\nb\n", "file untouched on 0-match failure");
|
|
387
|
+
});
|
|
184
388
|
});
|
|
185
389
|
|
|
186
390
|
test("replace: empty find throws", async () => {
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
391
|
+
await withDir(async (dir) => {
|
|
392
|
+
await writeFile(join(dir, "f.txt"), "a\n");
|
|
393
|
+
await assert.rejects(
|
|
394
|
+
callTool(makeReplaceTool(dir), { path: "f.txt", replacements: [{ find: "", replace: "x" }] }),
|
|
395
|
+
/Validation failed for tool "replace"[\s\S]*find/,
|
|
396
|
+
);
|
|
397
|
+
});
|
|
194
398
|
});
|
|
195
399
|
|
|
196
|
-
test("replace:
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
400
|
+
test("replace: handles large match counts without an arbitrary cap", async () => {
|
|
401
|
+
await withDir(async (dir) => {
|
|
402
|
+
const f = join(dir, "f.txt");
|
|
403
|
+
await writeFile(f, "a\n".repeat(2500));
|
|
404
|
+
const result: any = await call(makeReplaceTool(dir), {
|
|
405
|
+
path: "f.txt",
|
|
406
|
+
replacements: [{ find: "a", replace: "b" }],
|
|
407
|
+
});
|
|
408
|
+
assert.match(result.content[0].text, /Replaced f\.txt \(2500 matches\)\./);
|
|
409
|
+
assert.equal(await readFile(f, "utf-8"), "b\n".repeat(2500));
|
|
410
|
+
});
|
|
206
411
|
});
|
|
207
412
|
|
|
208
413
|
test("replace: invalid regex throws a friendly message", async () => {
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
414
|
+
await withDir(async (dir) => {
|
|
415
|
+
await writeFile(join(dir, "f.txt"), "a\n");
|
|
416
|
+
await assert.rejects(
|
|
417
|
+
call(makeReplaceTool(dir), {
|
|
418
|
+
path: "f.txt",
|
|
419
|
+
replacements: [{ find: "(unclosed", replace: "x", regex: true }],
|
|
420
|
+
}),
|
|
421
|
+
/invalid regex/,
|
|
422
|
+
);
|
|
423
|
+
});
|
|
216
424
|
});
|
|
217
425
|
|
|
218
426
|
test("replace: invalid flag char throws", async () => {
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
427
|
+
await withDir(async (dir) => {
|
|
428
|
+
await writeFile(join(dir, "f.txt"), "a\n");
|
|
429
|
+
await assert.rejects(
|
|
430
|
+
callTool(makeReplaceTool(dir), {
|
|
431
|
+
path: "f.txt",
|
|
432
|
+
replacements: [{ find: "a", replace: "x", flags: "z" }],
|
|
433
|
+
}),
|
|
434
|
+
/Validation failed for tool "replace"[\s\S]*flags/,
|
|
435
|
+
);
|
|
436
|
+
});
|
|
226
437
|
});
|
|
227
438
|
|
|
228
439
|
test("replace: count-but-no-net-change does not rewrite and reports it", async () => {
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
440
|
+
await withDir(async (dir) => {
|
|
441
|
+
const f = join(dir, "f.txt");
|
|
442
|
+
await writeFile(f, "xx\n");
|
|
443
|
+
// $& = whole match = "x", so the text is unchanged despite 2 matches
|
|
444
|
+
const r: any = await call(makeReplaceTool(dir), {
|
|
445
|
+
path: "f.txt",
|
|
446
|
+
replacements: [{ find: "x", replace: "$&", regex: true }],
|
|
447
|
+
});
|
|
448
|
+
assert.equal(r.isError, undefined);
|
|
449
|
+
assert.equal(await readFile(f, "utf-8"), "xx\n");
|
|
450
|
+
assert.match(r.content[0].text, /no net change/);
|
|
451
|
+
});
|
|
238
452
|
});
|
|
239
453
|
|
|
240
454
|
test("replace: returns details.diff (string) + patch + firstChangedLine", async () => {
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
455
|
+
await withDir(async (dir) => {
|
|
456
|
+
const f = join(dir, "f.txt");
|
|
457
|
+
await writeFile(f, "a\nb\nc\n");
|
|
458
|
+
const r: any = await call(makeReplaceTool(dir), {
|
|
459
|
+
path: "f.txt",
|
|
460
|
+
replacements: [{ find: "b", replace: "B" }],
|
|
461
|
+
});
|
|
462
|
+
assert.equal(typeof r.details.diff, "string");
|
|
463
|
+
assert.equal(typeof r.details.patch, "string");
|
|
464
|
+
assert.equal(typeof r.details.firstChangedLine, "number");
|
|
465
|
+
});
|
|
249
466
|
});
|
|
250
467
|
|
|
251
468
|
test("replace: returns fresh anchors for the changed region", async () => {
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
469
|
+
await withDir(async (dir) => {
|
|
470
|
+
const f = join(dir, "f.txt");
|
|
471
|
+
await writeFile(f, "a\nb\nc\n");
|
|
472
|
+
const r: any = await call(makeReplaceTool(dir), {
|
|
473
|
+
path: "f.txt",
|
|
474
|
+
replacements: [{ find: "b", replace: "B" }],
|
|
475
|
+
});
|
|
476
|
+
const out: string = r.content[0].text;
|
|
477
|
+
assert.match(out, /Updated anchors/);
|
|
478
|
+
// line 2 now holds "B"; its anchor must be present and correct
|
|
479
|
+
const a2 = anchorLine(out, 2);
|
|
480
|
+
assert.match(a2, /^2#[0-9A-Z]{2,8}$/);
|
|
481
|
+
assert.match(out, new RegExp(`^${a2}$`, "m"));
|
|
482
|
+
assert.doesNotMatch(out, /│B/);
|
|
483
|
+
});
|
|
264
484
|
});
|
|
265
485
|
|
|
266
486
|
test("replace: changed-region anchor chains a subsequent hashline edit without a re-read", async () => {
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
487
|
+
await withDir(async (dir) => {
|
|
488
|
+
const f = join(dir, "f.txt");
|
|
489
|
+
await writeFile(f, "old old old\n");
|
|
490
|
+
const replace = makeReplaceTool(dir);
|
|
491
|
+
const r1: any = await call(replace, {
|
|
492
|
+
path: "f.txt",
|
|
493
|
+
replacements: [{ find: "old", replace: "new" }],
|
|
494
|
+
});
|
|
495
|
+
const out: string = r1.content[0].text;
|
|
496
|
+
// line 1 is the changed line; chain an edit on its returned anchor
|
|
497
|
+
const a1 = anchorLine(out, 1);
|
|
498
|
+
const edit = makeEditOverride(dir);
|
|
499
|
+
const r2: any = await call(edit, {
|
|
500
|
+
path: "f.txt",
|
|
501
|
+
edits: [{ op: "replace", anchor: a1, body: ["NEW NEW NEW"] }],
|
|
502
|
+
});
|
|
503
|
+
assert.equal(r2.isError, undefined);
|
|
504
|
+
assert.equal(await readFile(f, "utf-8"), "NEW NEW NEW\n");
|
|
505
|
+
});
|
|
283
506
|
});
|
|
284
507
|
|
|
285
508
|
test("replace: multiline replacement (changes line count) still reports a correct span", async () => {
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
509
|
+
await withDir(async (dir) => {
|
|
510
|
+
const f = join(dir, "f.txt");
|
|
511
|
+
await writeFile(f, "a\nb\nc\n");
|
|
512
|
+
// replace "b" with two lines → line count grows; changed region must cover the insertion
|
|
513
|
+
const r: any = await call(makeReplaceTool(dir), {
|
|
514
|
+
path: "f.txt",
|
|
515
|
+
replacements: [{ find: "b", replace: "B1\nB2" }],
|
|
516
|
+
});
|
|
517
|
+
assert.equal(await readFile(f, "utf-8"), "a\nB1\nB2\nc\n");
|
|
518
|
+
const out: string = r.content[0].text;
|
|
519
|
+
// the two inserted/changed lines (2 and 3) should both be anchored
|
|
520
|
+
assert.doesNotThrow(() => anchorLine(out, 2));
|
|
521
|
+
assert.doesNotThrow(() => anchorLine(out, 3));
|
|
522
|
+
});
|
|
297
523
|
});
|
|
298
524
|
|
|
299
525
|
test("replace renderResult: renders the diff without throwing", async () => {
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
526
|
+
await withDir(async (dir) => {
|
|
527
|
+
const f = join(dir, "f.txt");
|
|
528
|
+
await writeFile(f, "a\nb\nc\n");
|
|
529
|
+
const tool = makeReplaceTool(dir);
|
|
530
|
+
const r: any = await call(tool, { path: "f.txt", replacements: [{ find: "b", replace: "B" }] });
|
|
531
|
+
const comp = tool.renderResult(
|
|
532
|
+
{ content: r.content, details: r.details },
|
|
533
|
+
{ isPartial: false, expanded: true },
|
|
534
|
+
stubTheme,
|
|
535
|
+
{
|
|
536
|
+
args: { path: "f.txt", replacements: [{ find: "b", replace: "B" }] },
|
|
537
|
+
isError: r.isError ?? false,
|
|
538
|
+
state: {},
|
|
539
|
+
invalidate: () => {},
|
|
540
|
+
} as Parameters<typeof tool.renderResult>[3],
|
|
541
|
+
);
|
|
542
|
+
assert.match(comp.render(80).join("\n"), /B/, "rendered diff should contain the new content");
|
|
543
|
+
});
|
|
315
544
|
});
|
|
316
545
|
|
|
317
546
|
test("replace renderResult: renders the error line without throwing", async () => {
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
547
|
+
await withDir(async (dir) => {
|
|
548
|
+
const f = join(dir, "f.txt");
|
|
549
|
+
await writeFile(f, "a\n");
|
|
550
|
+
const tool = makeReplaceTool(dir);
|
|
551
|
+
let thrown: any;
|
|
552
|
+
const r: any = await call(tool, {
|
|
553
|
+
path: "f.txt",
|
|
554
|
+
replacements: [{ find: "zzz", replace: "y" }],
|
|
555
|
+
}).catch((e: any) => {
|
|
556
|
+
thrown = e;
|
|
557
|
+
return null;
|
|
558
|
+
});
|
|
559
|
+
assert.ok(thrown, "expected the call to throw");
|
|
560
|
+
const comp = tool.renderResult(
|
|
561
|
+
{
|
|
562
|
+
content: [{ type: "text", text: thrown.message }],
|
|
563
|
+
details: generateMutationDetails(
|
|
564
|
+
"f.txt",
|
|
565
|
+
"a\n",
|
|
566
|
+
"a\n",
|
|
567
|
+
{ publishedRevision: "r", observedRevision: "r" },
|
|
568
|
+
"PUBLISHED",
|
|
569
|
+
),
|
|
570
|
+
},
|
|
571
|
+
{ isPartial: false, expanded: false },
|
|
572
|
+
stubTheme,
|
|
573
|
+
{
|
|
574
|
+
args: { path: "f.txt", replacements: [{ find: "zzz", replace: "y" }] },
|
|
575
|
+
isError: true,
|
|
576
|
+
} as Parameters<typeof tool.renderResult>[3],
|
|
577
|
+
);
|
|
578
|
+
assert.match(comp.render(80).join("\n"), /zero matches|no matches|match/i);
|
|
579
|
+
});
|
|
337
580
|
});
|
|
338
581
|
|
|
339
582
|
test("replace header: renderResult refreshes the call header in place — no invalidate", async () => {
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
583
|
+
await withDir(async (dir) => {
|
|
584
|
+
const f = join(dir, "f.txt");
|
|
585
|
+
await writeFile(f, "a\nb\nc\n");
|
|
586
|
+
const tool = makeReplaceTool(dir);
|
|
587
|
+
const args = { path: "f.txt", replacements: [{ find: "b", replace: "B1\nB2" }] };
|
|
588
|
+
const r: any = await call(tool, args);
|
|
589
|
+
let invalidated = false;
|
|
590
|
+
const context: any = {
|
|
591
|
+
args,
|
|
592
|
+
isError: false,
|
|
593
|
+
state: {},
|
|
594
|
+
invalidate: () => {
|
|
595
|
+
invalidated = true;
|
|
596
|
+
},
|
|
597
|
+
};
|
|
598
|
+
const header: any = tool.renderCall(args, stubTheme, context);
|
|
599
|
+
assert.match(header.text, /1 rule\b/);
|
|
600
|
+
assert.ok(!header.text.includes("+2"), "pre-execution header must not show counts");
|
|
601
|
+
tool.renderResult(
|
|
602
|
+
{ content: r.content, details: r.details },
|
|
603
|
+
{ isPartial: false, expanded: true },
|
|
604
|
+
stubTheme,
|
|
605
|
+
context,
|
|
606
|
+
);
|
|
607
|
+
assert.deepEqual(context.state.diffCounts, { added: 2, removed: 1 });
|
|
608
|
+
assert.ok(header.text.includes("+2"), "header should show added count");
|
|
609
|
+
assert.ok(header.text.includes("-1"), "header should show removed count");
|
|
610
|
+
assert.ok(!invalidated, "renderResult must not call invalidate");
|
|
611
|
+
});
|
|
612
|
+
});
|
|
613
|
+
|
|
614
|
+
test("replace header retains the rule count when a long path wraps", () => {
|
|
615
|
+
const tool = makeReplaceTool(process.cwd());
|
|
616
|
+
const args = {
|
|
617
|
+
path: "x".repeat(68),
|
|
618
|
+
replacements: [
|
|
619
|
+
{ find: "first", replace: "second" },
|
|
620
|
+
{ find: "third", replace: "fourth" },
|
|
621
|
+
],
|
|
622
|
+
};
|
|
623
|
+
const header = tool.renderCall(args, stubTheme, {} as Parameters<typeof tool.renderCall>[2]);
|
|
624
|
+
assert.match(header.render(120).join("\n"), /2 rules/);
|
|
625
|
+
assert.match(header.render(80).join("\n"), /2\s+rules/);
|
|
356
626
|
});
|
|
357
627
|
|
|
358
|
-
test("replacement batches use one snapshot and return anchors for the final content", async () =>
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
}
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
}
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
628
|
+
test("replacement batches use one snapshot and return anchors for the final content", async () =>
|
|
629
|
+
withDir(async (dir) => {
|
|
630
|
+
const file = join(dir, "batch.txt");
|
|
631
|
+
const tool = makeReplaceTool(dir);
|
|
632
|
+
const args = {
|
|
633
|
+
path: file,
|
|
634
|
+
replacements: [
|
|
635
|
+
{ find: "foo", replace: "bar" },
|
|
636
|
+
{ find: "bar", replace: "baz" },
|
|
637
|
+
],
|
|
638
|
+
};
|
|
639
|
+
validateToolArguments(tool, { type: "toolCall", id: "0", name: "replace", arguments: args });
|
|
640
|
+
await writeFile(file, "\uFEFFfoo\r\nbar\nfoo");
|
|
641
|
+
const result = await call(tool, args);
|
|
642
|
+
assert.equal(await readFile(file, "utf8"), "\uFEFFbar\r\nbaz\nbar");
|
|
643
|
+
assert.match(result.content[0].text, /3 matches/);
|
|
644
|
+
assert.match(result.details.diff, /^\+2 baz/m);
|
|
645
|
+
await call(makeEditOverride(dir), {
|
|
646
|
+
path: file,
|
|
647
|
+
edits: [{ op: "replace", anchor: anchorLine(result.content[0].text, 2), body: ["chained"] }],
|
|
648
|
+
});
|
|
649
|
+
assert.equal(await readFile(file, "utf8"), "\uFEFFbar\r\nchained\nbar");
|
|
650
|
+
}));
|
|
651
|
+
|
|
652
|
+
test("invalid batches reject every change and never run a fused command", async () =>
|
|
653
|
+
withDir(async (dir) => {
|
|
654
|
+
const file = join(dir, "batch.txt");
|
|
655
|
+
const before = "foo bar foo";
|
|
656
|
+
await writeFile(file, before);
|
|
657
|
+
let commands = 0;
|
|
658
|
+
const tool = makeReplaceTool(
|
|
659
|
+
dir,
|
|
660
|
+
createActionFusionExecutor(async () => {
|
|
661
|
+
commands++;
|
|
662
|
+
return "done";
|
|
663
|
+
}),
|
|
664
|
+
);
|
|
665
|
+
const first = { find: "bar", replace: "changed" };
|
|
666
|
+
const cases = [
|
|
667
|
+
[
|
|
668
|
+
/then_run:skipped/,
|
|
669
|
+
[
|
|
670
|
+
{ replacements: [first, { find: "missing", replace: "x" }] },
|
|
671
|
+
{ replacements: [first, { find: "bar foo", replace: "x" }] },
|
|
672
|
+
{ replacements: [first, { find: "bar", replace: "x" }] },
|
|
673
|
+
],
|
|
674
|
+
],
|
|
675
|
+
[
|
|
676
|
+
/Invalid argument replacements\[1\]\.find: invalid regex/,
|
|
677
|
+
[{ replacements: [first, { find: "(", replace: "x", regex: true }] }],
|
|
678
|
+
],
|
|
679
|
+
[
|
|
680
|
+
/Invalid argument replacements\[1\]\.replace: UNSUPPORTED_TEXT: NUL bytes are not editable\./,
|
|
681
|
+
[{ replacements: [first, { find: "foo", replace: "\0" }] }],
|
|
682
|
+
],
|
|
683
|
+
[
|
|
684
|
+
/Invalid argument replacements\[1\]\.replace: INVALID_UNICODE: content cannot be encoded losslessly as UTF-8\./,
|
|
685
|
+
[{ replacements: [first, { find: "foo", replace: "\ud800" }] }],
|
|
686
|
+
],
|
|
687
|
+
[
|
|
688
|
+
/Validation failed for tool "replace"/,
|
|
689
|
+
[
|
|
690
|
+
{ replacements: [first, { find: "", replace: "x" }] },
|
|
691
|
+
{ replacements: [first, { find: "foo" }] },
|
|
692
|
+
{ replacements: [] },
|
|
693
|
+
{ replacements: [first], find: "foo", replace: "x" },
|
|
694
|
+
{ replacements: [first], flags: "i" },
|
|
695
|
+
{},
|
|
696
|
+
],
|
|
697
|
+
],
|
|
698
|
+
] as const;
|
|
699
|
+
for (const [expected, inputs] of cases) {
|
|
700
|
+
for (const args of inputs) {
|
|
701
|
+
await assert.rejects(
|
|
702
|
+
callTool(
|
|
703
|
+
tool,
|
|
704
|
+
{ path: file, ...args, then_run: { command: "check" } },
|
|
705
|
+
{
|
|
706
|
+
ctx: { cwd: dir },
|
|
707
|
+
},
|
|
708
|
+
),
|
|
709
|
+
expected,
|
|
710
|
+
);
|
|
711
|
+
assert.equal(await readFile(file, "utf8"), before);
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
assert.equal(commands, 0);
|
|
715
|
+
}));
|
|
716
|
+
|
|
717
|
+
test("adjacent matches and zero-width boundaries have deterministic batch semantics", async () =>
|
|
718
|
+
withDir(async (dir) => {
|
|
719
|
+
const file = join(dir, "batch.txt");
|
|
720
|
+
const tool = makeReplaceTool(dir);
|
|
721
|
+
for (const rules of [
|
|
722
|
+
[
|
|
723
|
+
{ find: "a", replace: "b" },
|
|
724
|
+
{ find: "b", replace: "a" },
|
|
725
|
+
],
|
|
726
|
+
[
|
|
727
|
+
{ find: "b", replace: "a" },
|
|
728
|
+
{ find: "a", replace: "b" },
|
|
729
|
+
],
|
|
730
|
+
]) {
|
|
731
|
+
await writeFile(file, "ab");
|
|
732
|
+
await call(tool, { path: file, replacements: rules });
|
|
733
|
+
assert.equal(await readFile(file, "utf8"), "ba");
|
|
734
|
+
}
|
|
735
|
+
for (const find of ["^", "(?=b)", "$"]) {
|
|
736
|
+
await writeFile(file, "ab");
|
|
737
|
+
const args = {
|
|
738
|
+
path: file,
|
|
739
|
+
replacements: [
|
|
740
|
+
{ find: "ab", replace: "X" },
|
|
741
|
+
{ find, replace: "!", regex: true },
|
|
742
|
+
],
|
|
743
|
+
};
|
|
744
|
+
if (find === "$") {
|
|
745
|
+
await call(tool, args);
|
|
746
|
+
assert.equal(await readFile(file, "utf8"), "X!");
|
|
747
|
+
} else {
|
|
748
|
+
await assert.rejects(call(tool, args), /overlap/);
|
|
749
|
+
assert.equal(await readFile(file, "utf8"), "ab");
|
|
750
|
+
}
|
|
751
|
+
}
|
|
752
|
+
await writeFile(file, "ab");
|
|
753
|
+
await assert.rejects(
|
|
754
|
+
call(tool, {
|
|
755
|
+
path: file,
|
|
756
|
+
replacements: [
|
|
757
|
+
{ find: "^", replace: "x", regex: true },
|
|
758
|
+
{ find: "(?=a)", replace: "y", regex: true },
|
|
759
|
+
],
|
|
760
|
+
}),
|
|
761
|
+
/overlap/,
|
|
762
|
+
);
|
|
763
|
+
}));
|
|
764
|
+
|
|
765
|
+
test("regex templates agree with native replacement in single and snapshot batch modes", async () =>
|
|
766
|
+
withDir(async (dir) => {
|
|
767
|
+
const file = join(dir, "regex.txt");
|
|
768
|
+
const tool = makeReplaceTool(dir);
|
|
769
|
+
const template =
|
|
770
|
+
"$$|$&|$`|$'|$0|$00|$01|$1|$2|$10|$12|$99|$<x>|$<missing>|$<>|$<$&>|$<unclosed$1|$";
|
|
771
|
+
for (const [source, find, flags] of [
|
|
772
|
+
["ab b#", "(?<x>a)?b", "g"],
|
|
773
|
+
["ab b#", "(a)?b", "g"],
|
|
774
|
+
["ab#", "ab", "g"],
|
|
775
|
+
["abcdefghijkl#", "(a)(b)(c)(d)(e)(f)(g)(h)(i)(j)(k)(l)", "g"],
|
|
776
|
+
["ab ab#", "(?<=a)(b)", "g"],
|
|
777
|
+
["ab#", "(?=b)", "g"],
|
|
778
|
+
["😀#", "(?=[^#])", "gu"],
|
|
779
|
+
["aab#", "(a)", "gy"],
|
|
780
|
+
]) {
|
|
781
|
+
const rule = { find, replace: template, regex: true, flags };
|
|
782
|
+
const expected = source.replace(new RegExp(find, flags), template);
|
|
783
|
+
for (const batch of [false, true]) {
|
|
784
|
+
await writeFile(file, source);
|
|
785
|
+
// The second rule changes only the original final sentinel, not copies inserted by $' or $`.
|
|
786
|
+
const args = batch
|
|
787
|
+
? { replacements: [rule, { find: "#$", replace: "!", regex: true }] }
|
|
788
|
+
: { replacements: [rule] };
|
|
789
|
+
await call(tool, { path: file, ...args });
|
|
790
|
+
assert.equal(
|
|
791
|
+
await readFile(file, "utf8"),
|
|
792
|
+
batch ? expected.slice(0, -1) + "!" : expected,
|
|
793
|
+
`${find}/${flags}; batch=${batch}`,
|
|
794
|
+
);
|
|
795
|
+
}
|
|
796
|
+
}
|
|
797
|
+
}));
|
|
798
|
+
|
|
799
|
+
test("successful batches run one command against the complete result", async () =>
|
|
800
|
+
withDir(async (dir) => {
|
|
801
|
+
const file = join(dir, "batch.txt");
|
|
802
|
+
await writeFile(file, "foo bar");
|
|
803
|
+
let commands = 0;
|
|
804
|
+
const tool = makeReplaceTool(
|
|
805
|
+
dir,
|
|
806
|
+
createActionFusionExecutor(async () => {
|
|
807
|
+
commands++;
|
|
808
|
+
assert.equal(await readFile(file, "utf8"), "bar baz");
|
|
809
|
+
return "checked";
|
|
810
|
+
}),
|
|
811
|
+
);
|
|
812
|
+
const result = await tool.execute(
|
|
813
|
+
"0",
|
|
814
|
+
{
|
|
815
|
+
path: file,
|
|
816
|
+
replacements: [
|
|
817
|
+
{ find: "foo", replace: "bar" },
|
|
818
|
+
{ find: "bar", replace: "baz" },
|
|
819
|
+
],
|
|
820
|
+
then_run: { command: "check" },
|
|
821
|
+
},
|
|
822
|
+
undefined,
|
|
823
|
+
undefined,
|
|
824
|
+
{ cwd: dir } as Parameters<typeof tool.execute>[4],
|
|
825
|
+
);
|
|
826
|
+
assert.equal(commands, 1);
|
|
827
|
+
assert.ok(result.details.actionFusion);
|
|
828
|
+
assert.equal(result.details.actionFusion.command, "succeeded");
|
|
829
|
+
assert.ok(result.content[0].type === "text");
|
|
830
|
+
assert.match(result.content[0].text, /Updated anchors/);
|
|
831
|
+
}));
|
|
832
|
+
|
|
833
|
+
test("regex captures and zero-width insertions use logical offsets without consuming CRLF bytes", async () =>
|
|
834
|
+
withDir(async (dir) => {
|
|
835
|
+
const path = join(dir, "f.txt");
|
|
836
|
+
const before = "a\r\nb\rc\r\r\n";
|
|
837
|
+
await writeFile(path, before);
|
|
838
|
+
await call(makeReplaceTool(dir), {
|
|
839
|
+
path,
|
|
840
|
+
replacements: [{ find: "(b\rc\r)\\n", replace: "$1\n", regex: true }],
|
|
841
|
+
});
|
|
842
|
+
assert.equal(await readFile(path, "utf8"), before);
|
|
843
|
+
await call(makeReplaceTool(dir), {
|
|
844
|
+
path,
|
|
845
|
+
replacements: [{ find: "(?=\\n)", replace: "!", regex: true }],
|
|
846
|
+
});
|
|
847
|
+
assert.equal(await readFile(path, "utf8"), "a!\r\nb\rc\r!\r\n");
|
|
848
|
+
}));
|
|
849
|
+
|
|
850
|
+
test("replace rejects JSON-string rules without publishing", async () =>
|
|
851
|
+
withDir(async (dir) => {
|
|
852
|
+
const file = join(dir, "f.txt");
|
|
853
|
+
await writeFile(file, "hello foo world");
|
|
854
|
+
const tool = makeReplaceTool(dir);
|
|
855
|
+
await assert.rejects(
|
|
856
|
+
callTool(tool, {
|
|
857
|
+
path: "f.txt",
|
|
858
|
+
replacements: JSON.stringify([{ find: "foo", replace: "bar" }]),
|
|
859
|
+
}),
|
|
860
|
+
/Validation failed for tool "replace"[\s\S]*replacements/,
|
|
861
|
+
);
|
|
862
|
+
assert.equal(await readFile(file, "utf8"), "hello foo world");
|
|
863
|
+
}));
|