@criogaid/pi-hashline-edit 0.8.0 → 0.10.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 +13 -10
- package/README.zh-CN.md +12 -9
- package/THIRD_PARTY_NOTICES.md +0 -6
- package/package.json +1 -1
- package/src/core/AGENTS.md +4 -5
- package/src/core/apply.test.ts +118 -293
- package/src/core/apply.ts +63 -165
- package/src/core/case-fold.ts +8 -67
- package/src/core/character-set.ts +4 -4
- package/src/core/errors.test.ts +23 -7
- package/src/core/errors.ts +32 -5
- package/src/core/hash.test.ts +8 -16
- package/src/core/hash.ts +3 -23
- package/src/core/lines.ts +2 -4
- package/src/core/ranges.test.ts +16 -8
- package/src/core/ranges.ts +10 -6
- package/src/core/regex.ts +3 -4
- package/src/core/replace.test.ts +47 -1
- package/src/core/replace.ts +19 -13
- package/src/core/search-analysis.test.ts +24 -27
- package/src/core/search-analysis.ts +34 -65
- package/src/core/search-matrix.testing.ts +0 -2
- package/src/core/search.ts +1 -1
- package/src/core/text.test.ts +5 -5
- package/src/core/text.ts +5 -12
- package/src/core/types.ts +7 -43
- package/src/integration/config-registration.test.ts +2 -2
- package/src/integration/fusion-dispatch.test.ts +2 -5
- package/src/integration/grep-flags.test.ts +0 -14
- package/src/integration/grep-js-completeness.test.ts +34 -8
- package/src/integration/grep-line-candidates.test.ts +4 -8
- package/src/integration/grep-matrix.test.ts +3 -7
- package/src/integration/grep-path-matrix.test.ts +5 -5
- package/src/integration/grep-prefilter-coverage.test.ts +3 -7
- package/src/integration/grep-process-limits.test.ts +78 -0
- package/src/integration/grep-rg.test.ts +123 -25
- package/src/integration/grep-scan-budget.test.ts +2 -2
- package/src/integration/grep-translation.test.ts +12 -27
- package/src/integration/large-text.test.ts +1 -47
- package/src/integration/model-budget-lifecycle.test.ts +13 -3
- package/src/integration/mutation-file-types.testing.ts +1 -1
- package/src/integration/npm-package.testing.ts +1 -1
- package/src/integration/publication-read-changes.test.ts +125 -47
- package/src/integration/replace-worker-boundary.test.ts +21 -11
- package/src/integration/report-replay.test.ts +21 -23
- package/src/integration/utf8-errors.test.ts +1 -1
- package/src/pi/AGENTS.md +16 -16
- package/src/pi/action-fusion.test.ts +168 -300
- package/src/pi/action-fusion.ts +148 -111
- package/src/pi/anchor-format.test.ts +9 -9
- package/src/pi/anchor-format.ts +6 -8
- package/src/pi/argument-diagnostics.ts +52 -207
- package/src/pi/argument-error.ts +2 -1
- package/src/pi/argument-format.test.ts +20 -22
- package/src/pi/argument-validation.test.ts +382 -418
- package/src/pi/argument-validation.ts +5 -8
- package/src/pi/budgets.ts +5 -4
- package/src/pi/config.test.ts +5 -2
- package/src/pi/config.ts +4 -6
- package/src/pi/diagnostic-buffer.test.ts +12 -1
- package/src/pi/diagnostic-buffer.ts +1 -2
- package/src/pi/directory-sync.test.ts +0 -1
- package/src/pi/edit-tool.ts +173 -226
- package/src/pi/error-text.ts +4 -8
- package/src/pi/execute.test.ts +165 -266
- package/src/pi/failure-context.test.ts +144 -126
- package/src/pi/failure-context.ts +45 -94
- package/src/pi/failure-path.test.ts +168 -141
- package/src/pi/file-commit.test.ts +110 -53
- package/src/pi/file-commit.ts +90 -137
- package/src/pi/file-snapshot.test.ts +3 -5
- package/src/pi/fusion-card.test.ts +4 -1
- package/src/pi/fusion-card.ts +16 -24
- package/src/pi/grep-candidates.test.ts +2 -101
- package/src/pi/grep-candidates.ts +23 -69
- package/src/pi/grep-matrix.test.ts +2 -2
- package/src/pi/grep-order.test.ts +4 -4
- package/src/pi/grep-output.ts +62 -96
- package/src/pi/grep-render.ts +1 -1
- package/src/pi/grep-reread.ts +92 -0
- package/src/pi/grep-scope.ts +40 -42
- package/src/pi/grep-search.ts +22 -31
- package/src/pi/grep-tool.ts +55 -82
- package/src/pi/grep.test.ts +81 -46
- package/src/pi/model-budgets.test.ts +25 -23
- package/src/pi/model-budgets.ts +6 -38
- package/src/pi/mutation-result.test.ts +44 -35
- package/src/pi/mutation-result.ts +51 -20
- package/src/pi/mutation-runner.ts +126 -70
- package/src/pi/output-budgets.test.ts +38 -16
- package/src/pi/path.ts +1 -1
- package/src/pi/pi.test.ts +3 -1
- package/src/pi/read-tool.ts +5 -5
- package/src/pi/regex-worker-contract.ts +3 -13
- package/src/pi/regex-worker.ts +1 -10
- package/src/pi/registration.test.ts +82 -20
- package/src/pi/render.test.ts +79 -42
- package/src/pi/render.ts +69 -63
- package/src/pi/replace-regex.ts +14 -2
- package/src/pi/replace-tool.ts +22 -83
- package/src/pi/replace-worker.ts +1 -1
- package/src/pi/replace.test.ts +9 -5
- package/src/pi/report-cause.ts +3 -8
- package/src/pi/report-failure.testing.ts +1 -2
- package/src/pi/report-schema.ts +65 -81
- package/src/pi/report.test.ts +171 -25
- package/src/pi/report.ts +33 -87
- package/src/pi/result-safety.test.ts +177 -189
- package/src/pi/rg-process.test.ts +77 -63
- package/src/pi/rg-process.ts +48 -64
- package/src/pi/rg-regex.ts +40 -400
- package/src/pi/schema.ts +12 -0
- package/src/pi/text-stream.test.ts +0 -21
- package/src/pi/text-stream.ts +14 -21
- package/src/pi/write-tool.ts +33 -62
- package/src/pi/write.test.ts +1 -3
- package/src/testing/regex-translation-corpus.ts +12 -12
- package/src/pi/replace-worker-contract.ts +0 -10
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import assert from "node:assert/strict";
|
|
2
|
-
import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
|
|
2
|
+
import fs, { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
|
|
3
|
+
import { syncBuiltinESMExports } from "node:module";
|
|
3
4
|
import { tmpdir } from "node:os";
|
|
4
5
|
import { join } from "node:path";
|
|
5
6
|
import { test } from "node:test";
|
|
@@ -14,6 +15,9 @@ import { callTool } from "./tool-call.testing.ts";
|
|
|
14
15
|
import { argumentError, rejectsArgument } from "./argument-error.testing.ts";
|
|
15
16
|
import { POSITIVE_SAFE_INTEGER } from "./schema.ts";
|
|
16
17
|
import { reportOf } from "./report.ts";
|
|
18
|
+
import { createAnchorFormatter } from "./anchor-format.ts";
|
|
19
|
+
import { computeLineHash } from "../core/hash.ts";
|
|
20
|
+
import { grepCounts } from "./grep-output.testing.ts";
|
|
17
21
|
|
|
18
22
|
const invalidCommand = { command: "", timeout: 0 };
|
|
19
23
|
const anchorHash = "A".repeat(DEFAULT_CONFIG.hashLen);
|
|
@@ -38,10 +42,12 @@ const cases = [
|
|
|
38
42
|
makeEditOverride(cwd, DEFAULT_CONFIG, fusion),
|
|
39
43
|
args: {
|
|
40
44
|
edits: [
|
|
41
|
-
{
|
|
42
|
-
{
|
|
43
|
-
{
|
|
44
|
-
{
|
|
45
|
+
{ action: "replace", anchor: `1#${anchorHash}`, body: [] },
|
|
46
|
+
{ action: "append", body: ["bad\0", "bad\ud800"] },
|
|
47
|
+
{ action: "delete", anchor: `${Number.MAX_SAFE_INTEGER + 1}#${anchorHash}`, end: "1#A" },
|
|
48
|
+
{ action: "append", body: [] },
|
|
49
|
+
{ action: "replace", anchor: `3#${anchorHash}`, end: `1#${anchorHash}`, body: ["text"] },
|
|
50
|
+
{ action: "delete", anchor: `4#${anchorHash}`, end: `2#${anchorHash}` },
|
|
45
51
|
],
|
|
46
52
|
then_run: invalidCommand,
|
|
47
53
|
},
|
|
@@ -52,9 +58,15 @@ const cases = [
|
|
|
52
58
|
"edits[2].anchor",
|
|
53
59
|
"edits[2].end",
|
|
54
60
|
"edits[3].body",
|
|
61
|
+
"edits[4].end",
|
|
62
|
+
"edits[5].end",
|
|
55
63
|
"then_run.command",
|
|
56
64
|
"then_run.timeout",
|
|
57
65
|
],
|
|
66
|
+
reasons: [
|
|
67
|
+
{ field: "edits[4].end", reason: /line 1 is before anchor line 3/ },
|
|
68
|
+
{ field: "edits[5].end", reason: /line 2 is before anchor line 4/ },
|
|
69
|
+
],
|
|
58
70
|
},
|
|
59
71
|
{
|
|
60
72
|
name: "replace",
|
|
@@ -99,14 +111,31 @@ for (const scenario of cases) {
|
|
|
99
111
|
});
|
|
100
112
|
const tool = scenario.make(cwd, fusion);
|
|
101
113
|
const args: unknown = JSON.parse(JSON.stringify({ path, ...scenario.args, unexpected: true }));
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
114
|
+
const snapshot = structuredClone(args);
|
|
115
|
+
const open = t.mock.method(fs, "open");
|
|
116
|
+
syncBuiltinESMExports();
|
|
117
|
+
try {
|
|
118
|
+
await assert.rejects(callTool(tool, args), (error: unknown) => {
|
|
119
|
+
const result = argumentError(error);
|
|
120
|
+
assert.deepEqual(
|
|
121
|
+
new Set(result.issues.map((issue) => issue.field)),
|
|
122
|
+
new Set([...scenario.fields, "unexpected"]),
|
|
123
|
+
);
|
|
124
|
+
if ("reasons" in scenario) {
|
|
125
|
+
for (const expected of scenario.reasons) {
|
|
126
|
+
const issue = result.issues.find((issue) => issue.field === expected.field);
|
|
127
|
+
assert.ok(issue);
|
|
128
|
+
assert.match(issue.reason, expected.reason);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return true;
|
|
132
|
+
});
|
|
133
|
+
assert.equal(open.mock.callCount(), 0);
|
|
134
|
+
} finally {
|
|
135
|
+
t.mock.restoreAll();
|
|
136
|
+
syncBuiltinESMExports();
|
|
137
|
+
}
|
|
138
|
+
assert.deepEqual(args, snapshot);
|
|
110
139
|
assert.equal(await readFile(path, "utf8"), original);
|
|
111
140
|
assert.equal(commandRuns, 0);
|
|
112
141
|
});
|
|
@@ -156,112 +185,261 @@ test("grep and replace flags → accept V8 modes and reject invalid flags before
|
|
|
156
185
|
}
|
|
157
186
|
});
|
|
158
187
|
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
188
|
+
const editFields = ["anchor", "end", "body"] as const;
|
|
189
|
+
type EditField = (typeof editFields)[number];
|
|
190
|
+
const fieldValues = {
|
|
191
|
+
anchor: `1#${anchorHash}`,
|
|
192
|
+
end: `2#${anchorHash}`,
|
|
193
|
+
body: ["text"],
|
|
194
|
+
};
|
|
195
|
+
const actionCases: readonly {
|
|
196
|
+
action: string;
|
|
197
|
+
required: readonly EditField[];
|
|
198
|
+
optional: readonly EditField[];
|
|
199
|
+
expected: string;
|
|
200
|
+
}[] = [
|
|
201
|
+
{
|
|
202
|
+
action: "replace",
|
|
203
|
+
required: ["anchor", "body"],
|
|
204
|
+
optional: ["end"],
|
|
205
|
+
expected: "text\nbeta\ngamma\n",
|
|
206
|
+
},
|
|
207
|
+
{ action: "delete", required: ["anchor"], optional: ["end"], expected: "beta\ngamma\n" },
|
|
208
|
+
{
|
|
209
|
+
action: "insert_after",
|
|
210
|
+
required: ["anchor", "body"],
|
|
211
|
+
optional: [],
|
|
212
|
+
expected: "alpha\ntext\nbeta\ngamma\n",
|
|
213
|
+
},
|
|
214
|
+
{
|
|
215
|
+
action: "insert_before",
|
|
216
|
+
required: ["anchor", "body"],
|
|
217
|
+
optional: [],
|
|
218
|
+
expected: "text\nalpha\nbeta\ngamma\n",
|
|
219
|
+
},
|
|
220
|
+
{ action: "append", required: ["body"], optional: [], expected: "alpha\nbeta\ngamma\ntext\n" },
|
|
221
|
+
{ action: "prepend", required: ["body"], optional: [], expected: "text\nalpha\nbeta\ngamma\n" },
|
|
222
|
+
];
|
|
223
|
+
|
|
224
|
+
for (const scenario of actionCases) {
|
|
225
|
+
test(`${scenario.action} field matrix → requires its fields and rejects fields it does not take`, async (t) => {
|
|
226
|
+
const tool = makeEditOverride(process.cwd(), DEFAULT_CONFIG);
|
|
227
|
+
const fields = schemaProperties(tool).edits?.items?.properties;
|
|
228
|
+
const choice = fields?.action?.anyOf?.find((option) => option.const === scenario.action);
|
|
229
|
+
assert.ok(choice?.description);
|
|
230
|
+
const description = choice.description;
|
|
231
|
+
const valid = {
|
|
232
|
+
action: scenario.action,
|
|
233
|
+
...Object.fromEntries(scenario.required.map((field) => [field, fieldValues[field]])),
|
|
166
234
|
};
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
235
|
+
for (const field of scenario.required) {
|
|
236
|
+
for (const value of [undefined, null]) {
|
|
237
|
+
await t.test(
|
|
238
|
+
`${field} is ${value === null ? "null" : "missing"} → reports the required field`,
|
|
239
|
+
async () => {
|
|
240
|
+
const edit: Record<string, unknown> = Object.fromEntries(
|
|
241
|
+
Object.entries(valid).filter(([key]) => key !== field),
|
|
242
|
+
);
|
|
243
|
+
if (value === null) edit[field] = null;
|
|
244
|
+
const args = { path: "unused.txt", edits: [edit] };
|
|
245
|
+
await assert.rejects(callTool(tool, args), (error: unknown) => {
|
|
246
|
+
assert.deepEqual(argumentError(error).issues, [
|
|
247
|
+
{
|
|
248
|
+
field: `edits[0].${field}`,
|
|
249
|
+
reason: `is required for "${scenario.action}"`,
|
|
250
|
+
fix: fields?.[field]?.description,
|
|
251
|
+
},
|
|
252
|
+
]);
|
|
253
|
+
return true;
|
|
254
|
+
});
|
|
255
|
+
},
|
|
177
256
|
);
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
})
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
"edits[0].extra",
|
|
206
|
-
"edits[1].body",
|
|
207
|
-
"edits[2].anchor",
|
|
208
|
-
"then_run.command",
|
|
209
|
-
"then_run.timeout",
|
|
210
|
-
"unexpected",
|
|
211
|
-
]),
|
|
212
|
-
);
|
|
213
|
-
return true;
|
|
214
|
-
},
|
|
215
|
-
);
|
|
216
|
-
});
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
for (const field of editFields.filter(
|
|
260
|
+
(field) => !scenario.required.includes(field) && !scenario.optional.includes(field),
|
|
261
|
+
)) {
|
|
262
|
+
await t.test(`${field} is supplied → reports only that forbidden field`, async () => {
|
|
263
|
+
await assert.rejects(
|
|
264
|
+
callTool(tool, {
|
|
265
|
+
path: "unused.txt",
|
|
266
|
+
edits: [{ ...valid, [field]: fieldValues[field] }],
|
|
267
|
+
}),
|
|
268
|
+
(error: unknown) => {
|
|
269
|
+
assert.deepEqual(argumentError(error).issues, [
|
|
270
|
+
{
|
|
271
|
+
field: `edits[0].${field}`,
|
|
272
|
+
reason: "is not allowed",
|
|
273
|
+
fix: `"${scenario.action}": ${description}`,
|
|
274
|
+
},
|
|
275
|
+
]);
|
|
276
|
+
assert.match(description, /Takes /);
|
|
277
|
+
return true;
|
|
278
|
+
},
|
|
279
|
+
);
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
});
|
|
283
|
+
}
|
|
217
284
|
|
|
218
|
-
test("edit
|
|
285
|
+
test("edit action is missing or invalid → reports choices without guessing field permissions", async () => {
|
|
219
286
|
const tool = makeEditOverride(process.cwd(), DEFAULT_CONFIG);
|
|
220
|
-
|
|
287
|
+
const choices = schemaProperties(tool).edits?.items?.properties?.action?.anyOf ?? [];
|
|
288
|
+
assert.deepEqual(
|
|
289
|
+
choices.map((choice) => choice.const),
|
|
290
|
+
actionCases.map(({ action }) => action),
|
|
291
|
+
);
|
|
292
|
+
for (const action of [undefined, "unknown", null, 42, {}]) {
|
|
221
293
|
await assert.rejects(
|
|
222
294
|
callTool(tool, {
|
|
223
295
|
path: "unused.txt",
|
|
224
296
|
edits: [
|
|
225
|
-
{
|
|
297
|
+
{
|
|
298
|
+
...(action === undefined ? {} : { action }),
|
|
299
|
+
anchor: `1#${anchorHash}`,
|
|
300
|
+
body: ["valid"],
|
|
301
|
+
},
|
|
226
302
|
],
|
|
227
303
|
}),
|
|
228
304
|
(error: unknown) => {
|
|
229
|
-
const
|
|
230
|
-
assert.equal(
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
"
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
305
|
+
const { issues } = argumentError(error);
|
|
306
|
+
assert.equal(issues.length, 1);
|
|
307
|
+
const issue = issues[0];
|
|
308
|
+
assert.equal(issue.field, "edits[0].action");
|
|
309
|
+
if (action === undefined) {
|
|
310
|
+
assert.equal(issue.reason, "is required");
|
|
311
|
+
for (const choice of choices) {
|
|
312
|
+
assert.ok(choice.description);
|
|
313
|
+
inOrder(issue.fix ?? "", [JSON.stringify(choice.const), choice.description]);
|
|
314
|
+
}
|
|
315
|
+
} else {
|
|
316
|
+
assert.equal(
|
|
317
|
+
issue.reason,
|
|
318
|
+
`expected one of: ${choices.map((choice) => JSON.stringify(choice.const)).join(", ")}`,
|
|
319
|
+
);
|
|
320
|
+
assert.equal(issue.fix, undefined);
|
|
321
|
+
}
|
|
243
322
|
return true;
|
|
244
323
|
},
|
|
245
324
|
);
|
|
246
325
|
}
|
|
247
326
|
});
|
|
248
327
|
|
|
249
|
-
test("edit
|
|
250
|
-
const
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
328
|
+
test("optional edit fields and then_run are null with Fusion → publishes bytes without rewriting caller arguments", async (t) => {
|
|
329
|
+
const cwd = await mkdtemp(join(tmpdir(), "hashline-null-"));
|
|
330
|
+
t.after(() => rm(cwd, { recursive: true, force: true }));
|
|
331
|
+
const path = join(cwd, "target.txt");
|
|
332
|
+
let commandRuns = 0;
|
|
333
|
+
const fusion = createActionFusionExecutor(async () => {
|
|
334
|
+
commandRuns++;
|
|
335
|
+
return { status: "succeeded", result: { content: [{ type: "text", text: "ran" }] } };
|
|
336
|
+
});
|
|
337
|
+
const tool = makeEditOverride(cwd, DEFAULT_CONFIG, fusion);
|
|
338
|
+
for (const scenario of actionCases) {
|
|
339
|
+
await t.test(`${scenario.action} → accepts null optional fields`, async () => {
|
|
340
|
+
await writeFile(path, "alpha\nbeta\ngamma\n");
|
|
341
|
+
const edit = {
|
|
342
|
+
action: scenario.action,
|
|
343
|
+
...Object.fromEntries(editFields.map((field) => [field, null])),
|
|
344
|
+
...Object.fromEntries(
|
|
345
|
+
scenario.required.map((field) => [
|
|
346
|
+
field,
|
|
347
|
+
field === "body" ? ["text"] : token(1, "alpha"),
|
|
348
|
+
]),
|
|
349
|
+
),
|
|
350
|
+
};
|
|
351
|
+
const args = { path, edits: [edit], then_run: null };
|
|
352
|
+
const snapshot = structuredClone(args);
|
|
353
|
+
const result = await callTool(tool, args);
|
|
354
|
+
assert.equal(result.isError, undefined);
|
|
355
|
+
assert.equal(reportOf(result, "edit")?.publication, "PUBLISHED");
|
|
356
|
+
assert.equal(reportOf(result, "edit")?.then_run, undefined);
|
|
357
|
+
assert.deepEqual(await readFile(path), Buffer.from(scenario.expected));
|
|
358
|
+
assert.deepEqual(args, snapshot);
|
|
359
|
+
});
|
|
360
|
+
}
|
|
361
|
+
assert.equal(commandRuns, 0);
|
|
362
|
+
});
|
|
363
|
+
|
|
364
|
+
test("null optional fields → grep applies defaults without filename filters", async (t) => {
|
|
365
|
+
const cwd = await mkdtemp(join(tmpdir(), "hashline-grep-null-"));
|
|
366
|
+
t.after(() => rm(cwd, { recursive: true, force: true }));
|
|
367
|
+
const path = join(cwd, "target.txt");
|
|
368
|
+
const config = DEFAULT_CONFIG;
|
|
369
|
+
await writeFile(path, "x\n".repeat(config.grep.defaultLimit + 1));
|
|
370
|
+
const rgCalls: (readonly string[])[] = [];
|
|
371
|
+
const discover = async (
|
|
372
|
+
args: readonly string[],
|
|
373
|
+
onPath: (path: string) => boolean | Promise<boolean>,
|
|
374
|
+
) => {
|
|
375
|
+
rgCalls.push(args);
|
|
376
|
+
await onPath(path);
|
|
377
|
+
return { code: 0, stderr: "", stopped: false };
|
|
378
|
+
};
|
|
379
|
+
const tool = makeGrepOverrideWithBackend(cwd, config, {
|
|
380
|
+
runRgPaths: (args, _signal, onPath) => discover(args, onPath),
|
|
381
|
+
runRgLines: (args, _signal, _maxLines, onFile) =>
|
|
382
|
+
discover(args, (file) => onFile(file, undefined)),
|
|
383
|
+
});
|
|
384
|
+
const result = await callTool(tool, {
|
|
385
|
+
pattern: "x",
|
|
386
|
+
literal: true,
|
|
387
|
+
glob: null,
|
|
388
|
+
limit: null,
|
|
389
|
+
context: null,
|
|
390
|
+
});
|
|
391
|
+
assert.equal(result.isError, undefined);
|
|
392
|
+
assert.equal(grepCounts(result).matches, config.grep.defaultLimit);
|
|
393
|
+
assert.equal(reportOf(result, "grep")?.matchLimit, config.grep.defaultLimit);
|
|
394
|
+
assert.ok(rgCalls.length > 0);
|
|
395
|
+
for (const args of rgCalls) {
|
|
396
|
+
const globs = args.flatMap((arg, index) => (arg === "--glob" ? [args[index + 1]] : []));
|
|
397
|
+
// The built-in .git exclusion remains; null adds no filename filter.
|
|
398
|
+
assert.deepEqual(globs, ["!.git"]);
|
|
399
|
+
}
|
|
400
|
+
});
|
|
401
|
+
|
|
402
|
+
test("edit batch has 64 or 65 operations → publishes the maximum or rejects the oversized batch unchanged", async (t) => {
|
|
403
|
+
const cwd = await mkdtemp(join(tmpdir(), "hashline-edit-limit-"));
|
|
404
|
+
t.after(() => rm(cwd, { recursive: true, force: true }));
|
|
405
|
+
const path = join(cwd, "target.txt");
|
|
406
|
+
const tool = makeEditOverride(cwd, DEFAULT_CONFIG);
|
|
407
|
+
const maxEdits: unknown = Reflect.get(tool.parameters.properties.edits, "maxItems");
|
|
408
|
+
assert.ok(typeof maxEdits === "number");
|
|
409
|
+
assert.equal(maxEdits, 64);
|
|
410
|
+
const lines = Array.from({ length: maxEdits + 2 }, (_, i) => `line ${i + 1}`);
|
|
411
|
+
const original = Buffer.from(`${lines.join("\n")}\n`);
|
|
412
|
+
for (const count of [maxEdits, maxEdits + 1]) {
|
|
413
|
+
await t.test(
|
|
414
|
+
`${count} edits → ${count === maxEdits ? "publishes distinct deletions" : `rejects edits above max${maxEdits}`}`,
|
|
415
|
+
async () => {
|
|
416
|
+
await writeFile(path, original);
|
|
417
|
+
const args = {
|
|
418
|
+
path,
|
|
419
|
+
edits: lines
|
|
420
|
+
.slice(0, count)
|
|
421
|
+
.map((line, i) => ({ action: "delete", anchor: token(i + 1, line) })),
|
|
422
|
+
};
|
|
423
|
+
const snapshot = structuredClone(args);
|
|
424
|
+
if (count === maxEdits) {
|
|
425
|
+
const result = await callTool(tool, args);
|
|
426
|
+
assert.equal(result.isError, undefined);
|
|
427
|
+
assert.equal(reportOf(result, "edit")?.publication, "PUBLISHED");
|
|
428
|
+
assert.deepEqual(await readFile(path), Buffer.from(`${lines.slice(count).join("\n")}\n`));
|
|
429
|
+
} else {
|
|
430
|
+
await assert.rejects(callTool(tool, args), (error: unknown) => {
|
|
431
|
+
const report = argumentError(error);
|
|
432
|
+
assert.equal(report.issues.length, 1);
|
|
433
|
+
assert.equal(report.issues[0].field, "edits");
|
|
434
|
+
assert.match(
|
|
435
|
+
report.issues[0].reason,
|
|
436
|
+
new RegExp(`must not have more than ${maxEdits} items`),
|
|
437
|
+
);
|
|
438
|
+
return true;
|
|
439
|
+
});
|
|
440
|
+
assert.deepEqual(await readFile(path), original);
|
|
441
|
+
}
|
|
442
|
+
assert.deepEqual(args, snapshot);
|
|
265
443
|
},
|
|
266
444
|
);
|
|
267
445
|
}
|
|
@@ -309,68 +487,6 @@ test("literal enum is invalid → report the permitted values once", async () =>
|
|
|
309
487
|
);
|
|
310
488
|
});
|
|
311
489
|
|
|
312
|
-
test("specific child errors exist → omit parent summaries and retain independent fields", async () => {
|
|
313
|
-
await assert.rejects(
|
|
314
|
-
callTool(makeEditOverride(process.cwd(), DEFAULT_CONFIG, createActionFusionExecutor()), {
|
|
315
|
-
path: "unused.txt",
|
|
316
|
-
edits: [{ op: "insert_after", body: ["ok"], extra: true }, { op: "unknown" }],
|
|
317
|
-
then_run: { command: "echo ok", extra: true },
|
|
318
|
-
unexpected: true,
|
|
319
|
-
}),
|
|
320
|
-
(error: unknown) => {
|
|
321
|
-
const result = argumentError(error);
|
|
322
|
-
assert.deepEqual(
|
|
323
|
-
new Set(result.issues.map((issue) => issue.field)),
|
|
324
|
-
new Set([
|
|
325
|
-
"edits[0].anchor",
|
|
326
|
-
"edits[0].extra",
|
|
327
|
-
"edits[1].op",
|
|
328
|
-
"then_run.extra",
|
|
329
|
-
"unexpected",
|
|
330
|
-
]),
|
|
331
|
-
);
|
|
332
|
-
return true;
|
|
333
|
-
},
|
|
334
|
-
);
|
|
335
|
-
});
|
|
336
|
-
|
|
337
|
-
test("operation shape does not permit a field → omit inapplicable semantic advice", async () => {
|
|
338
|
-
for (const edits of [
|
|
339
|
-
[{ op: "unknown", anchor: "1#A", body: [] }],
|
|
340
|
-
[{ op: "delete", anchor: `1#${anchorHash}`, body: [] }],
|
|
341
|
-
"bad",
|
|
342
|
-
]) {
|
|
343
|
-
await assert.rejects(
|
|
344
|
-
callTool(makeEditOverride(process.cwd(), DEFAULT_CONFIG), { path: "unused.txt", edits }),
|
|
345
|
-
(error: unknown) => {
|
|
346
|
-
const result = argumentError(error);
|
|
347
|
-
assert.equal(result.issues.length, 1);
|
|
348
|
-
assert.doesNotMatch(result.issues[0].reason, /empty|hash length mismatch/);
|
|
349
|
-
return true;
|
|
350
|
-
},
|
|
351
|
-
);
|
|
352
|
-
}
|
|
353
|
-
});
|
|
354
|
-
|
|
355
|
-
test("multiple required fields are missing → report every missing field", async () => {
|
|
356
|
-
await assert.rejects(
|
|
357
|
-
callTool(makeEditOverride(process.cwd(), DEFAULT_CONFIG), {
|
|
358
|
-
path: "unused.txt",
|
|
359
|
-
edits: [{ op: "replace" }],
|
|
360
|
-
}),
|
|
361
|
-
(error: unknown) => {
|
|
362
|
-
const result = argumentError(error);
|
|
363
|
-
assert.deepEqual(
|
|
364
|
-
new Set(result.issues.map((issue) => issue.field)),
|
|
365
|
-
new Set(["edits[0].anchor", "edits[0].body"]),
|
|
366
|
-
);
|
|
367
|
-
assert.ok(result.issues.every((issue) => /required/.test(issue.reason)));
|
|
368
|
-
assert.equal(result.schemaLimited, undefined);
|
|
369
|
-
return true;
|
|
370
|
-
},
|
|
371
|
-
);
|
|
372
|
-
});
|
|
373
|
-
|
|
374
490
|
test("unknown field names contain punctuation, numeric keys or newlines → quote property keys without treating them as array indices", async () => {
|
|
375
491
|
for (const key of ["odd.\nfield/~", "0", "01", "a/b", "a~b", 'quote"field']) {
|
|
376
492
|
await assert.rejects(
|
|
@@ -392,12 +508,12 @@ test("one field exhausts native diagnostics → label the limit and retain anoth
|
|
|
392
508
|
await assert.rejects(
|
|
393
509
|
callTool(makeEditOverride(process.cwd(), DEFAULT_CONFIG), {
|
|
394
510
|
path: "",
|
|
395
|
-
edits: Array.from({ length: 30 }, () => ({
|
|
511
|
+
edits: Array.from({ length: 30 }, () => ({ action: "unknown" })),
|
|
396
512
|
}),
|
|
397
513
|
(error: unknown) => {
|
|
398
514
|
const result = argumentError(error);
|
|
399
515
|
assert.ok(result.issues.some((issue) => issue.field === "path"));
|
|
400
|
-
assert.ok(result.issues.some((issue) => issue.field === "edits[0].
|
|
516
|
+
assert.ok(result.issues.some((issue) => issue.field === "edits[0].action"));
|
|
401
517
|
assert.equal(result.schemaLimited, true);
|
|
402
518
|
return true;
|
|
403
519
|
},
|
|
@@ -408,7 +524,7 @@ test("one field violates independent constraints → combine its reasons without
|
|
|
408
524
|
await assert.rejects(
|
|
409
525
|
callTool(makeEditOverride(process.cwd(), DEFAULT_CONFIG), {
|
|
410
526
|
path: "unused.txt",
|
|
411
|
-
edits: [{
|
|
527
|
+
edits: [{ action: "delete", anchor: `${Number.MAX_SAFE_INTEGER + 1}#A` }],
|
|
412
528
|
}),
|
|
413
529
|
(error: unknown) => {
|
|
414
530
|
const result = argumentError(error);
|
|
@@ -431,180 +547,65 @@ test("one field violates independent constraints → combine its reasons without
|
|
|
431
547
|
);
|
|
432
548
|
});
|
|
433
549
|
|
|
434
|
-
test("
|
|
550
|
+
test("edit fields contain semantic errors → checks only fields accepted by their action", async (t) => {
|
|
435
551
|
const tool = makeEditOverride(process.cwd(), DEFAULT_CONFIG);
|
|
436
|
-
for (const
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
552
|
+
for (const scenario of actionCases) {
|
|
553
|
+
const valid = {
|
|
554
|
+
action: scenario.action,
|
|
555
|
+
...Object.fromEntries(scenario.required.map((field) => [field, fieldValues[field]])),
|
|
556
|
+
};
|
|
557
|
+
for (const field of editFields) {
|
|
558
|
+
const accepted = scenario.required.includes(field) || scenario.optional.includes(field);
|
|
559
|
+
const values: readonly (readonly [unknown, RegExp])[] =
|
|
560
|
+
field === "body"
|
|
561
|
+
? [
|
|
562
|
+
[[], /empty/],
|
|
563
|
+
[["bad\0"], /NUL/],
|
|
564
|
+
[["bad\ud800"], /surrogate|UTF-8/],
|
|
565
|
+
]
|
|
566
|
+
: [
|
|
567
|
+
[`${Number.MAX_SAFE_INTEGER + 1}#${anchorHash}`, /safe integer/],
|
|
568
|
+
["1#A", /hash characters/],
|
|
569
|
+
];
|
|
570
|
+
for (const [value, reason] of values) {
|
|
571
|
+
await t.test(
|
|
572
|
+
`${scenario.action}/${field}/${reason.source} → ${accepted ? "reports value error" : "reports only forbidden field"}`,
|
|
573
|
+
async () => {
|
|
574
|
+
const edit = { ...valid, [field]: value };
|
|
575
|
+
await assert.rejects(
|
|
576
|
+
callTool(tool, { path: "unused.txt", edits: [edit] }),
|
|
577
|
+
(error: unknown) => {
|
|
578
|
+
const report = argumentError(error);
|
|
579
|
+
assert.equal(report.issues.length, 1, JSON.stringify(report));
|
|
580
|
+
const issue = report.issues[0];
|
|
581
|
+
assert.equal(
|
|
582
|
+
issue.field,
|
|
583
|
+
`edits[0].${field}${accepted && field === "body" && Array.isArray(value) && value.length ? "[0]" : ""}`,
|
|
584
|
+
);
|
|
585
|
+
if (accepted) {
|
|
586
|
+
assert.match(issue.reason, reason);
|
|
587
|
+
if (field === "body" && Array.isArray(value) && value.length === 0) {
|
|
588
|
+
assert.match(
|
|
589
|
+
issue.fix ?? "",
|
|
590
|
+
scenario.action === "replace"
|
|
591
|
+
? /"action":"delete"/
|
|
592
|
+
: /supply at least one line/,
|
|
593
|
+
);
|
|
594
|
+
}
|
|
595
|
+
} else {
|
|
596
|
+
assert.equal(issue.reason, "is not allowed");
|
|
597
|
+
assert.match(issue.fix ?? "", new RegExp(`^"${scenario.action}":.*Takes `));
|
|
598
|
+
}
|
|
599
|
+
return true;
|
|
600
|
+
},
|
|
601
|
+
);
|
|
602
|
+
},
|
|
448
603
|
);
|
|
449
604
|
}
|
|
450
605
|
}
|
|
451
606
|
}
|
|
452
607
|
});
|
|
453
608
|
|
|
454
|
-
test("copy/move illegal argument shapes → reject before any file access", async () => {
|
|
455
|
-
const tool = makeEditOverride(process.cwd(), DEFAULT_CONFIG);
|
|
456
|
-
const anchor = `1#${anchorHash}`;
|
|
457
|
-
for (const op of ["copy", "move"]) {
|
|
458
|
-
for (const fields of [
|
|
459
|
-
{},
|
|
460
|
-
{ before: anchor, after: anchor },
|
|
461
|
-
{ before: anchor, body: ["unused"] },
|
|
462
|
-
{ after: anchor, end: null },
|
|
463
|
-
{ before: 1 },
|
|
464
|
-
{ before: anchor, end: "1#IXXX" },
|
|
465
|
-
]) {
|
|
466
|
-
await assert.rejects(
|
|
467
|
-
callTool(tool, { path: "missing-copy-move.txt", edits: [{ op, anchor, ...fields }] }),
|
|
468
|
-
(error: unknown) => {
|
|
469
|
-
const result = argumentError(error);
|
|
470
|
-
assert(result.issues.length > 0);
|
|
471
|
-
if ("body" in fields)
|
|
472
|
-
assert(result.issues.some((issue) => issue.field === "edits[0].body"));
|
|
473
|
-
if ("end" in fields)
|
|
474
|
-
assert(result.issues.some((issue) => issue.field === "edits[0].end"));
|
|
475
|
-
if (fields.before === 1)
|
|
476
|
-
assert(result.issues.some((issue) => issue.field === "edits[0].before"));
|
|
477
|
-
return true;
|
|
478
|
-
},
|
|
479
|
-
);
|
|
480
|
-
}
|
|
481
|
-
}
|
|
482
|
-
});
|
|
483
|
-
|
|
484
|
-
const exactlyOneDestination = /expected exactly one of: before, after/;
|
|
485
|
-
|
|
486
|
-
for (const op of ["copy", "move"] as const) {
|
|
487
|
-
test(`${op} without a destination → one exactly-one issue without a description fix`, async () => {
|
|
488
|
-
const tool = makeEditOverride(process.cwd(), DEFAULT_CONFIG);
|
|
489
|
-
await assert.rejects(
|
|
490
|
-
callTool(tool, {
|
|
491
|
-
path: "unused.txt",
|
|
492
|
-
edits: [{ op, anchor: `1#${anchorHash}` }],
|
|
493
|
-
}),
|
|
494
|
-
(error: unknown) => {
|
|
495
|
-
const result = argumentError(error);
|
|
496
|
-
assert.equal(result.issues.length, 1);
|
|
497
|
-
assert.equal(result.issues[0].field, "edits[0]");
|
|
498
|
-
assert.match(result.issues[0].reason, exactlyOneDestination);
|
|
499
|
-
assert.equal(result.issues[0].fix, undefined);
|
|
500
|
-
assert.equal(result.schemaLimited, undefined);
|
|
501
|
-
return true;
|
|
502
|
-
},
|
|
503
|
-
);
|
|
504
|
-
});
|
|
505
|
-
|
|
506
|
-
test(`${op} with both destinations → the same single exactly-one issue, no not-allowed pair`, async () => {
|
|
507
|
-
const tool = makeEditOverride(process.cwd(), DEFAULT_CONFIG);
|
|
508
|
-
await assert.rejects(
|
|
509
|
-
callTool(tool, {
|
|
510
|
-
path: "unused.txt",
|
|
511
|
-
edits: [
|
|
512
|
-
{ op, anchor: `1#${anchorHash}`, before: `2#${anchorHash}`, after: `3#${anchorHash}` },
|
|
513
|
-
],
|
|
514
|
-
}),
|
|
515
|
-
(error: unknown) => {
|
|
516
|
-
const result = argumentError(error);
|
|
517
|
-
assert.equal(result.issues.length, 1);
|
|
518
|
-
assert.equal(result.issues[0].field, "edits[0]");
|
|
519
|
-
assert.match(result.issues[0].reason, exactlyOneDestination);
|
|
520
|
-
assert.equal(result.schemaLimited, undefined);
|
|
521
|
-
return true;
|
|
522
|
-
},
|
|
523
|
-
);
|
|
524
|
-
});
|
|
525
|
-
}
|
|
526
|
-
|
|
527
|
-
test("mixed copy and move batch → every operation keeps its exactly-one and unknown-field issues", async () => {
|
|
528
|
-
const tool = makeEditOverride(process.cwd(), DEFAULT_CONFIG);
|
|
529
|
-
await assert.rejects(
|
|
530
|
-
callTool(tool, {
|
|
531
|
-
path: "unused.txt",
|
|
532
|
-
edits: [
|
|
533
|
-
{ op: "copy", anchor: `1#${anchorHash}`, start: { x: 1 }, to: `2#${anchorHash}` },
|
|
534
|
-
{ op: "move", anchor: `3#${anchorHash}` },
|
|
535
|
-
],
|
|
536
|
-
}),
|
|
537
|
-
(error: unknown) => {
|
|
538
|
-
const result = argumentError(error);
|
|
539
|
-
assert.deepEqual(
|
|
540
|
-
new Set(result.issues.map((issue) => issue.field)),
|
|
541
|
-
new Set(["edits[0]", "edits[0].start", "edits[0].to", "edits[1]"]),
|
|
542
|
-
);
|
|
543
|
-
for (const field of ["edits[0]", "edits[1]"])
|
|
544
|
-
assert.match(
|
|
545
|
-
result.issues.find((issue) => issue.field === field)?.reason ?? "",
|
|
546
|
-
exactlyOneDestination,
|
|
547
|
-
);
|
|
548
|
-
for (const field of ["edits[0].start", "edits[0].to"])
|
|
549
|
-
assert.match(
|
|
550
|
-
result.issues.find((issue) => issue.field === field)?.reason ?? "",
|
|
551
|
-
/not allowed/,
|
|
552
|
-
);
|
|
553
|
-
assert.equal(result.schemaLimited, undefined);
|
|
554
|
-
return true;
|
|
555
|
-
},
|
|
556
|
-
);
|
|
557
|
-
});
|
|
558
|
-
|
|
559
|
-
for (const op of ["copy", "move"] as const) {
|
|
560
|
-
test(`${op} with both destinations and a wrong before hash → keeps the hash-length issue beside exactly-one`, async () => {
|
|
561
|
-
const tool = makeEditOverride(process.cwd(), DEFAULT_CONFIG);
|
|
562
|
-
const shortHash = `2#${"A".repeat(DEFAULT_CONFIG.hashLen - 1)}`;
|
|
563
|
-
await assert.rejects(
|
|
564
|
-
callTool(tool, {
|
|
565
|
-
path: "unused.txt",
|
|
566
|
-
edits: [{ op, anchor: `1#${anchorHash}`, before: shortHash, after: `3#${anchorHash}` }],
|
|
567
|
-
}),
|
|
568
|
-
(error: unknown) => {
|
|
569
|
-
const result = argumentError(error);
|
|
570
|
-
const exactly = result.issues.filter((issue) => issue.field === "edits[0]");
|
|
571
|
-
assert.equal(exactly.length, 1);
|
|
572
|
-
assert.match(exactly[0].reason, exactlyOneDestination);
|
|
573
|
-
const hashIssue = result.issues.find((issue) => issue.field === "edits[0].before");
|
|
574
|
-
assert.ok(hashIssue, JSON.stringify(result.issues));
|
|
575
|
-
assert.match(hashIssue.reason, /hash characters/);
|
|
576
|
-
assert.match(hashIssue.fix ?? "", /Read or grep/);
|
|
577
|
-
assert.ok(!result.issues.some((issue) => issue.field === "edits[0].after"));
|
|
578
|
-
return true;
|
|
579
|
-
},
|
|
580
|
-
);
|
|
581
|
-
});
|
|
582
|
-
}
|
|
583
|
-
|
|
584
|
-
for (const op of ["copy", "move"] as const) {
|
|
585
|
-
for (const side of ["before", "after"] as const) {
|
|
586
|
-
test(`${op} with one ${side} destination and an unknown field → only the unknown field is reported`, async () => {
|
|
587
|
-
const tool = makeEditOverride(process.cwd(), DEFAULT_CONFIG);
|
|
588
|
-
await assert.rejects(
|
|
589
|
-
callTool(tool, {
|
|
590
|
-
path: "unused.txt",
|
|
591
|
-
edits: [
|
|
592
|
-
{ op, anchor: `1#${anchorHash}`, [side]: `2#${anchorHash}`, to: `3#${anchorHash}` },
|
|
593
|
-
],
|
|
594
|
-
}),
|
|
595
|
-
(error: unknown) => {
|
|
596
|
-
const result = argumentError(error);
|
|
597
|
-
assert.equal(result.issues.length, 1);
|
|
598
|
-
assert.equal(result.issues[0].field, "edits[0].to");
|
|
599
|
-
assert.match(result.issues[0].reason, /not allowed/);
|
|
600
|
-
assert.equal(result.schemaLimited, undefined);
|
|
601
|
-
return true;
|
|
602
|
-
},
|
|
603
|
-
);
|
|
604
|
-
});
|
|
605
|
-
}
|
|
606
|
-
}
|
|
607
|
-
|
|
608
609
|
/** Tool schema properties, so expected fix text comes from the schema instead of the test. */
|
|
609
610
|
interface SchemaNode {
|
|
610
611
|
readonly description?: string;
|
|
@@ -659,27 +660,6 @@ test("missing required parameter → fix states the parameter's role from its sc
|
|
|
659
660
|
inOrder(fix, ['"create"', create.description, '"overwrite"', overwrite.description]);
|
|
660
661
|
},
|
|
661
662
|
},
|
|
662
|
-
{
|
|
663
|
-
name: "edit op",
|
|
664
|
-
tool: edit,
|
|
665
|
-
args: { path: "unused.txt", edits: [{ anchor: `1#${anchorHash}`, body: ["text"] }] },
|
|
666
|
-
field: "edits[0].op",
|
|
667
|
-
verify: (fix: string) => {
|
|
668
|
-
for (const op of [
|
|
669
|
-
"replace",
|
|
670
|
-
"delete",
|
|
671
|
-
"insert_before",
|
|
672
|
-
"insert_after",
|
|
673
|
-
"copy",
|
|
674
|
-
"move",
|
|
675
|
-
"append",
|
|
676
|
-
"prepend",
|
|
677
|
-
])
|
|
678
|
-
assert.ok(fix.includes(`\"${op}\"`), `${op} missing from ${fix}`);
|
|
679
|
-
assert.equal(fix.split('"copy"').length - 1, 1);
|
|
680
|
-
assert.equal(fix.split('"move"').length - 1, 1);
|
|
681
|
-
},
|
|
682
|
-
},
|
|
683
663
|
] as const;
|
|
684
664
|
for (const scenario of cases) {
|
|
685
665
|
await assert.rejects(callTool(scenario.tool, scenario.args), (error: unknown) => {
|
|
@@ -698,7 +678,7 @@ test("missing required parameter → fix states the parameter's role from its sc
|
|
|
698
678
|
test("two operations miss the same required field → the shared fix appears once", async () => {
|
|
699
679
|
const tool = makeEditOverride(process.cwd(), DEFAULT_CONFIG);
|
|
700
680
|
await assert.rejects(
|
|
701
|
-
callTool(tool, { path: "unused.txt", edits: [{
|
|
681
|
+
callTool(tool, { path: "unused.txt", edits: [{ action: "replace" }, { action: "replace" }] }),
|
|
702
682
|
(error: unknown) => {
|
|
703
683
|
const result = argumentError(error);
|
|
704
684
|
const anchors = result.issues.filter((issue) => issue.field.endsWith(".anchor"));
|
|
@@ -706,13 +686,7 @@ test("two operations miss the same required field → the shared fix appears onc
|
|
|
706
686
|
assert.equal(anchors.filter((issue) => issue.fix).length, 1);
|
|
707
687
|
const fixes = result.issues.flatMap((issue) => (issue.fix ? [issue.fix] : []));
|
|
708
688
|
assert.equal(new Set(fixes).size, fixes.length, JSON.stringify(fixes));
|
|
709
|
-
const
|
|
710
|
-
(schemaProperties(tool).edits?.items?.anyOf ?? []).flatMap((branch) =>
|
|
711
|
-
branch.properties?.anchor ? [branch.properties.anchor.description] : [],
|
|
712
|
-
),
|
|
713
|
-
);
|
|
714
|
-
assert.equal(anchorDescriptions.size, 1);
|
|
715
|
-
const description = [...anchorDescriptions][0];
|
|
689
|
+
const description = schemaProperties(tool).edits?.items?.properties?.anchor?.description;
|
|
716
690
|
assert.ok(description);
|
|
717
691
|
assert.ok(fixes.includes(description));
|
|
718
692
|
return true;
|
|
@@ -739,66 +713,30 @@ test("invalid enum value → the reason carries no description fix", async () =>
|
|
|
739
713
|
);
|
|
740
714
|
});
|
|
741
715
|
|
|
742
|
-
const
|
|
743
|
-
|
|
744
|
-
const opSchema = candidate.properties?.op;
|
|
745
|
-
return [opSchema?.const, ...(opSchema?.anyOf ?? []).map((option) => option.const)].includes(op);
|
|
746
|
-
});
|
|
747
|
-
assert.ok(branch, `no edit branch for op ${op}`);
|
|
748
|
-
return branch;
|
|
749
|
-
};
|
|
750
|
-
|
|
751
|
-
const acceptedFields = (branch: SchemaNode): string =>
|
|
752
|
-
`Accepted fields: ${Object.keys(branch.properties ?? {}).join(", ")}.`;
|
|
753
|
-
|
|
754
|
-
const opExplanation = (branch: SchemaNode): string => {
|
|
755
|
-
const options = branch.properties?.op?.anyOf ?? [{ const: branch.properties?.op?.const }];
|
|
756
|
-
return `${options.map((option) => JSON.stringify(option.const)).join(" or ")}: ${branch.properties?.op?.description}`;
|
|
757
|
-
};
|
|
716
|
+
const acceptedFields = (shape: SchemaNode): string =>
|
|
717
|
+
`Accepted fields: ${Object.keys(shape.properties ?? {}).join(", ")}.`;
|
|
758
718
|
|
|
759
|
-
test("
|
|
719
|
+
test("unknown field is supplied → fix lists only the flat object's accepted fields", async () => {
|
|
760
720
|
const edit = makeEditOverride(process.cwd(), DEFAULT_CONFIG);
|
|
761
721
|
const grep = makeGrepOverride(process.cwd(), DEFAULT_CONFIG);
|
|
762
|
-
const
|
|
763
|
-
const move = branchWithOp(edit, "move");
|
|
764
|
-
const cases = [
|
|
722
|
+
for (const scenario of [
|
|
765
723
|
{
|
|
766
|
-
name: "tagged branch rejects another branch's field",
|
|
767
724
|
tool: edit,
|
|
768
|
-
args: {
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
},
|
|
772
|
-
field: "edits[0].anchor",
|
|
773
|
-
fix: `${opExplanation(append)} ${acceptedFields(append)}`,
|
|
725
|
+
args: { path: "unused.txt", edits: [{ action: "append", body: ["text"], op: "append" }] },
|
|
726
|
+
field: "edits[0].op",
|
|
727
|
+
fix: "Accepted fields: action, anchor, end, body.",
|
|
774
728
|
},
|
|
775
729
|
{
|
|
776
|
-
name: "unknown top-level field",
|
|
777
730
|
tool: grep,
|
|
778
|
-
args: {
|
|
731
|
+
args: { pattern: "text", literal: true, bogus: 1 },
|
|
779
732
|
field: "bogus",
|
|
780
733
|
fix: acceptedFields({ properties: schemaProperties(grep) }),
|
|
781
734
|
},
|
|
782
|
-
|
|
783
|
-
name: "transfer branch selected by before",
|
|
784
|
-
tool: edit,
|
|
785
|
-
args: {
|
|
786
|
-
path: "unused.txt",
|
|
787
|
-
edits: [
|
|
788
|
-
{ op: "move", anchor: `1#${anchorHash}`, before: `2#${anchorHash}`, body: ["text"] },
|
|
789
|
-
],
|
|
790
|
-
},
|
|
791
|
-
field: "edits[0].body",
|
|
792
|
-
fix: `${opExplanation(move)} ${acceptedFields(move)}`,
|
|
793
|
-
},
|
|
794
|
-
] as const;
|
|
795
|
-
for (const scenario of cases) {
|
|
735
|
+
]) {
|
|
796
736
|
await assert.rejects(callTool(scenario.tool, scenario.args), (error: unknown) => {
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
assert.match(issue.reason, /is not allowed/);
|
|
801
|
-
assert.equal(issue.fix, scenario.fix, JSON.stringify(result.issues));
|
|
737
|
+
assert.deepEqual(argumentError(error).issues, [
|
|
738
|
+
{ field: scenario.field, reason: "is not allowed", fix: scenario.fix },
|
|
739
|
+
]);
|
|
802
740
|
return true;
|
|
803
741
|
});
|
|
804
742
|
}
|
|
@@ -806,20 +744,19 @@ test("extra field is not allowed → fix describes the selected shape from its s
|
|
|
806
744
|
|
|
807
745
|
test("value mismatches the field pattern → fix states the field description from the schema", async () => {
|
|
808
746
|
const edit = makeEditOverride(process.cwd(), DEFAULT_CONFIG);
|
|
809
|
-
const
|
|
810
|
-
const replace = branchWithOp(edit, "replace");
|
|
747
|
+
const fields = schemaProperties(edit).edits?.items?.properties;
|
|
811
748
|
const cases = [
|
|
812
749
|
{
|
|
813
750
|
name: "body element with a newline",
|
|
814
|
-
args: { path: "unused.txt", edits: [{
|
|
751
|
+
args: { path: "unused.txt", edits: [{ action: "append", body: ["a\nb"] }] },
|
|
815
752
|
field: "edits[0].body[0]",
|
|
816
|
-
fix:
|
|
753
|
+
fix: fields?.body?.description,
|
|
817
754
|
},
|
|
818
755
|
{
|
|
819
756
|
name: "anchor without the line-hash shape",
|
|
820
|
-
args: { path: "unused.txt", edits: [{
|
|
757
|
+
args: { path: "unused.txt", edits: [{ action: "replace", anchor: "abc", body: ["text"] }] },
|
|
821
758
|
field: "edits[0].anchor",
|
|
822
|
-
fix:
|
|
759
|
+
fix: fields?.anchor?.description,
|
|
823
760
|
},
|
|
824
761
|
] as const;
|
|
825
762
|
for (const scenario of cases) {
|
|
@@ -839,7 +776,7 @@ test("two unknown fields in one object → the shared fix appears once", async (
|
|
|
839
776
|
await assert.rejects(
|
|
840
777
|
callTool(edit, {
|
|
841
778
|
path: "unused.txt",
|
|
842
|
-
edits: [{
|
|
779
|
+
edits: [{ action: "append", body: ["text"], zzz: 1, yyy: 2 }],
|
|
843
780
|
}),
|
|
844
781
|
(error: unknown) => {
|
|
845
782
|
const result = argumentError(error);
|
|
@@ -865,3 +802,30 @@ test("value has the wrong type → the reason carries no fix", async () => {
|
|
|
865
802
|
},
|
|
866
803
|
);
|
|
867
804
|
});
|
|
805
|
+
|
|
806
|
+
function token(line: number, content: string): string {
|
|
807
|
+
const hashLen = DEFAULT_CONFIG.hashLen;
|
|
808
|
+
return createAnchorFormatter(hashLen).reference(line, computeLineHash(line, content, hashLen));
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
test("20 unknown keys including a slash → bounded issues retain one accepted-fields fix", async () => {
|
|
812
|
+
const tool = makeReadOverride(process.cwd(), DEFAULT_CONFIG);
|
|
813
|
+
const keys = Array.from({ length: 20 }, (_, index) =>
|
|
814
|
+
index === 0 ? "unknown/key" : `unknown${index}`,
|
|
815
|
+
);
|
|
816
|
+
await assert.rejects(
|
|
817
|
+
callTool(tool, { path: "unused.txt", ...Object.fromEntries(keys.map((key) => [key, true])) }),
|
|
818
|
+
(error: unknown) => {
|
|
819
|
+
const result = argumentError(error);
|
|
820
|
+
assert.equal(result.schemaLimited, true);
|
|
821
|
+
assert.ok(result.issues.length >= 8);
|
|
822
|
+
assert.ok(result.issues.some((issue) => issue.field.includes("unknown/key")));
|
|
823
|
+
for (const issue of result.issues) {
|
|
824
|
+
assert.equal(issue.reason, "is not allowed");
|
|
825
|
+
}
|
|
826
|
+
assert.equal(result.issues[0].fix, acceptedFields({ properties: schemaProperties(tool) }));
|
|
827
|
+
assert.ok(result.issues.slice(1).every((issue) => issue.fix === undefined));
|
|
828
|
+
return true;
|
|
829
|
+
},
|
|
830
|
+
);
|
|
831
|
+
});
|