@criogaid/pi-hashline-edit 0.6.1 → 0.8.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 +78 -369
- package/README.zh-CN.md +162 -0
- package/THIRD_PARTY_NOTICES.md +80 -0
- package/package.json +11 -6
- package/src/core/AGENTS.md +20 -0
- package/src/core/apply.test.ts +328 -161
- package/src/core/apply.ts +129 -95
- package/src/core/case-fold.test.ts +44 -0
- package/src/core/case-fold.ts +77 -0
- package/src/core/character-set.test.ts +103 -0
- package/src/core/character-set.ts +184 -0
- package/src/core/errors.test.ts +23 -0
- package/src/core/errors.ts +70 -4
- package/src/core/hash.test.ts +1 -25
- package/src/core/hash.ts +0 -5
- package/src/core/lines.ts +32 -7
- package/src/core/ranges.test.ts +41 -9
- package/src/core/ranges.ts +11 -4
- package/src/core/regex.ts +51 -0
- package/src/core/replace-schema.ts +41 -0
- package/src/core/replace.test.ts +90 -0
- package/src/core/replace.ts +40 -56
- package/src/core/search-analysis.test.ts +208 -0
- package/src/core/search-analysis.ts +379 -0
- package/src/core/search-matrix.test.ts +64 -0
- package/src/core/search-matrix.testing.ts +183 -0
- package/src/core/search-schema.ts +18 -0
- package/src/core/search.test.ts +145 -0
- package/src/core/search.ts +119 -0
- package/src/core/text.test.ts +16 -8
- package/src/core/text.ts +26 -6
- package/src/core/types.ts +20 -17
- package/src/index.ts +24 -16
- package/src/integration/config-registration.test.ts +42 -28
- package/src/integration/fusion-dispatch.test.ts +62 -127
- package/src/integration/grep-errors.test.ts +50 -0
- package/src/integration/grep-flags.test.ts +273 -0
- package/src/integration/grep-js-completeness.test.ts +218 -0
- package/src/integration/grep-line-candidates.test.ts +233 -0
- package/src/integration/grep-matrix.test.ts +121 -0
- package/src/integration/grep-order.test.ts +78 -0
- package/src/integration/grep-path-matrix.test.ts +133 -0
- package/src/integration/grep-prefilter-coverage.test.ts +189 -0
- package/src/integration/grep-regex.test.ts +407 -0
- package/src/integration/grep-rg.test.ts +294 -436
- package/src/integration/grep-scan-budget.test.ts +111 -0
- package/src/integration/grep-translation.test.ts +206 -0
- package/src/integration/large-text.test.ts +56 -1
- package/src/integration/model-budget-lifecycle.test.ts +137 -0
- package/src/integration/mutation-file-types.test.ts +12 -26
- package/src/integration/mutation-file-types.testing.ts +33 -0
- package/src/integration/npm-package.test.ts +17 -0
- package/src/integration/npm-package.testing.ts +97 -0
- package/src/integration/publication-read-changes.test.ts +376 -0
- package/src/integration/read-changes.test.ts +98 -0
- package/src/integration/replace-package.test.ts +17 -36
- package/src/integration/replace-package.testing.ts +36 -0
- package/src/integration/replace-worker-boundary.test.ts +117 -0
- package/src/integration/report-replay.test.ts +90 -0
- package/src/integration/utf8-errors.test.ts +46 -0
- package/src/pi/AGENTS.md +54 -0
- package/src/pi/action-fusion.test.ts +256 -251
- package/src/pi/action-fusion.ts +121 -332
- package/src/pi/anchor-format.test.ts +16 -0
- package/src/pi/anchor-format.ts +16 -3
- package/src/pi/argument-diagnostics.ts +478 -0
- package/src/pi/argument-error.testing.ts +46 -0
- package/src/pi/argument-error.ts +76 -0
- package/src/pi/argument-format.test.ts +197 -0
- package/src/pi/argument-validation.test.ts +780 -49
- package/src/pi/argument-validation.ts +55 -58
- package/src/pi/budgets.ts +48 -21
- package/src/pi/codemode.test.ts +96 -0
- package/src/pi/config.test.ts +29 -15
- package/src/pi/config.ts +27 -13
- package/src/pi/diagnostic-buffer.test.ts +51 -16
- package/src/pi/diagnostic-buffer.ts +13 -16
- package/src/pi/directory-sync.test.ts +4 -1
- package/src/pi/edit-tool.ts +103 -99
- package/src/pi/error-text.ts +39 -16
- package/src/pi/execute.test.ts +407 -429
- package/src/pi/extension-loading.test.ts +7 -27
- package/src/pi/extension-loading.testing.ts +2 -14
- package/src/pi/failure-context.test.ts +265 -88
- package/src/pi/failure-context.ts +165 -181
- package/src/pi/failure-path.test.ts +179 -117
- package/src/pi/file-commit.test.ts +90 -73
- package/src/pi/file-commit.ts +216 -178
- package/src/pi/file-read.test.ts +29 -0
- package/src/pi/file-read.ts +60 -0
- package/src/pi/file-revision.ts +25 -0
- package/src/pi/file-snapshot.test.ts +1 -1
- package/src/pi/fusion-card-interaction.test.ts +143 -0
- package/src/pi/fusion-card.test.ts +233 -51
- package/src/pi/fusion-card.ts +246 -142
- package/src/pi/grep-candidates.test.ts +127 -0
- package/src/pi/grep-candidates.ts +191 -0
- package/src/pi/grep-matrix.test.ts +83 -0
- package/src/pi/grep-order.test.ts +323 -0
- package/src/pi/grep-output.testing.ts +24 -0
- package/src/pi/grep-output.ts +187 -151
- package/src/pi/grep-render.ts +31 -25
- package/src/pi/grep-scope.ts +62 -54
- package/src/pi/grep-search.ts +327 -241
- package/src/pi/grep-tool.ts +126 -147
- package/src/pi/grep-worker.mjs +4 -0
- package/src/pi/grep-worker.ts +14 -0
- package/src/pi/grep.test.ts +813 -713
- package/src/pi/model-budgets.test.ts +43 -0
- package/src/pi/model-budgets.ts +44 -0
- package/src/pi/mutation-outcome.testing.ts +6 -7
- package/src/pi/mutation-result.test.ts +45 -91
- package/src/pi/mutation-result.ts +86 -114
- package/src/pi/mutation-runner.ts +81 -81
- package/src/pi/output-budgets.test.ts +175 -0
- package/src/pi/path.ts +10 -1
- package/src/pi/pi.test.ts +4 -5
- package/src/pi/read-tool.ts +108 -142
- package/src/pi/read.test.ts +155 -96
- package/src/pi/regex-worker-contract.ts +55 -0
- package/src/pi/regex-worker.ts +95 -0
- package/src/pi/registration.test.ts +148 -124
- package/src/pi/render.test.ts +361 -84
- package/src/pi/render.ts +150 -32
- package/src/pi/replace-regex.ts +13 -48
- package/src/pi/replace-tool.ts +52 -158
- package/src/pi/replace-worker-contract.ts +10 -0
- package/src/pi/replace-worker.ts +12 -8
- package/src/pi/replace.test.ts +191 -340
- package/src/pi/report-cause.ts +103 -0
- package/src/pi/report-failure.testing.ts +20 -0
- package/src/pi/report-schema.ts +524 -0
- package/src/pi/report.test.ts +343 -0
- package/src/pi/report.ts +285 -0
- package/src/pi/result-safety.test.ts +216 -481
- package/src/pi/rg-process.test.ts +101 -76
- package/src/pi/rg-process.ts +130 -177
- package/src/pi/rg-regex.test.ts +67 -0
- package/src/pi/rg-regex.ts +715 -0
- package/src/pi/text-stream.test.ts +97 -69
- package/src/pi/text-stream.ts +97 -76
- package/src/pi/tool-call.testing.ts +20 -46
- package/src/pi/write-tool.ts +50 -41
- package/src/pi/write.test.ts +71 -95
- package/src/testing/extension-loader.testing.ts +24 -0
- package/src/testing/installation.testing.ts +77 -0
- package/src/testing/process.testing.ts +33 -0
- package/src/testing/regex-host.testing.ts +20 -0
- package/src/testing/regex-translation-corpus.ts +324 -0
- package/src/testing/rg-line-listing.fixture.mjs +7 -0
- package/src/testing/run-tests.ts +47 -0
- package/src/testing/session.testing.ts +127 -0
- package/src/integration/forget-grep.test.ts +0 -98
- package/src/pi/forget-tool.ts +0 -268
- package/src/pi/forget.test.ts +0 -343
- package/src/pi/forget.testing.ts +0 -273
- package/src/pi/rg-line-ranges.test.ts +0 -66
- package/src/pi/rg-line-ranges.ts +0 -75
- package/src/pi/rg-match-bytes.ts +0 -95
- package/src/pi/rg-text-view.ts +0 -192
- package/src/pi/tool-prompts.ts +0 -15
|
@@ -6,13 +6,14 @@ import { test } from "node:test";
|
|
|
6
6
|
import { createActionFusionExecutor } from "./action-fusion.ts";
|
|
7
7
|
import { DEFAULT_CONFIG } from "./config.ts";
|
|
8
8
|
import { makeEditOverride } from "./edit-tool.ts";
|
|
9
|
-
import { makeGrepOverride } from "./grep-tool.ts";
|
|
9
|
+
import { makeGrepOverride, makeGrepOverrideWithBackend } from "./grep-tool.ts";
|
|
10
10
|
import { makeReadOverride } from "./read-tool.ts";
|
|
11
11
|
import { makeReplaceTool } from "./replace-tool.ts";
|
|
12
12
|
import { makeWriteOverride } from "./write-tool.ts";
|
|
13
13
|
import { callTool } from "./tool-call.testing.ts";
|
|
14
|
-
import {
|
|
14
|
+
import { argumentError, rejectsArgument } from "./argument-error.testing.ts";
|
|
15
15
|
import { POSITIVE_SAFE_INTEGER } from "./schema.ts";
|
|
16
|
+
import { reportOf } from "./report.ts";
|
|
16
17
|
|
|
17
18
|
const invalidCommand = { command: "", timeout: 0 };
|
|
18
19
|
const anchorHash = "A".repeat(DEFAULT_CONFIG.hashLen);
|
|
@@ -22,17 +23,14 @@ const cases = [
|
|
|
22
23
|
make: (cwd: string, _fusion: ReturnType<typeof createActionFusionExecutor>) =>
|
|
23
24
|
makeReadOverride(cwd, DEFAULT_CONFIG),
|
|
24
25
|
args: { offset: -1, limit: 0 },
|
|
25
|
-
|
|
26
|
-
new RegExp(`\\n - offset:.*>= ${POSITIVE_SAFE_INTEGER.minimum}`),
|
|
27
|
-
new RegExp(`\\n - limit:.*>= ${POSITIVE_SAFE_INTEGER.minimum}`),
|
|
28
|
-
],
|
|
26
|
+
fields: ["offset", "limit"],
|
|
29
27
|
},
|
|
30
28
|
{
|
|
31
29
|
name: "grep",
|
|
32
30
|
make: (cwd: string, _fusion: ReturnType<typeof createActionFusionExecutor>) =>
|
|
33
31
|
makeGrepOverride(cwd, DEFAULT_CONFIG),
|
|
34
32
|
args: { pattern: [], literal: true, limit: 0, context: -1 },
|
|
35
|
-
|
|
33
|
+
fields: ["pattern", "limit", "context"],
|
|
36
34
|
},
|
|
37
35
|
{
|
|
38
36
|
name: "edit",
|
|
@@ -47,15 +45,15 @@ const cases = [
|
|
|
47
45
|
],
|
|
48
46
|
then_run: invalidCommand,
|
|
49
47
|
},
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
48
|
+
fields: [
|
|
49
|
+
"edits[0].body",
|
|
50
|
+
"edits[1].body[0]",
|
|
51
|
+
"edits[1].body[1]",
|
|
52
|
+
"edits[2].anchor",
|
|
53
|
+
"edits[2].end",
|
|
54
|
+
"edits[3].body",
|
|
55
|
+
"then_run.command",
|
|
56
|
+
"then_run.timeout",
|
|
59
57
|
],
|
|
60
58
|
},
|
|
61
59
|
{
|
|
@@ -64,18 +62,18 @@ const cases = [
|
|
|
64
62
|
makeReplaceTool(cwd, DEFAULT_CONFIG, fusion),
|
|
65
63
|
args: {
|
|
66
64
|
replacements: [
|
|
67
|
-
{ find: "(", replace: "bad\0",
|
|
68
|
-
{ find: "[", replace: "bad\ud800",
|
|
65
|
+
{ find: "(", replace: "bad\0", literal: false },
|
|
66
|
+
{ find: "[", replace: "bad\ud800", literal: false },
|
|
69
67
|
],
|
|
70
68
|
then_run: invalidCommand,
|
|
71
69
|
},
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
70
|
+
fields: [
|
|
71
|
+
"replacements[0].replace",
|
|
72
|
+
"replacements[0].find",
|
|
73
|
+
"replacements[1].replace",
|
|
74
|
+
"replacements[1].find",
|
|
75
|
+
"then_run.command",
|
|
76
|
+
"then_run.timeout",
|
|
79
77
|
],
|
|
80
78
|
},
|
|
81
79
|
{
|
|
@@ -83,12 +81,7 @@ const cases = [
|
|
|
83
81
|
make: (cwd: string, fusion: ReturnType<typeof createActionFusionExecutor>) =>
|
|
84
82
|
makeWriteOverride(cwd, fusion),
|
|
85
83
|
args: { content: "bad\0", mode: "invalid", then_run: invalidCommand },
|
|
86
|
-
|
|
87
|
-
/Invalid argument content: UNSUPPORTED_TEXT:/,
|
|
88
|
-
/\n - mode:/,
|
|
89
|
-
/\n - then_run.command:/,
|
|
90
|
-
/\n - then_run.timeout:/,
|
|
91
|
-
],
|
|
84
|
+
fields: ["content", "mode", "then_run.command", "then_run.timeout"],
|
|
92
85
|
},
|
|
93
86
|
] as const;
|
|
94
87
|
|
|
@@ -102,21 +95,16 @@ for (const scenario of cases) {
|
|
|
102
95
|
let commandRuns = 0;
|
|
103
96
|
const fusion = createActionFusionExecutor(async () => {
|
|
104
97
|
commandRuns++;
|
|
105
|
-
return { status: "succeeded",
|
|
98
|
+
return { status: "succeeded", result: { content: [{ type: "text", text: "ran" }] } };
|
|
106
99
|
});
|
|
107
100
|
const tool = scenario.make(cwd, fusion);
|
|
108
|
-
// These values round-trip through JSON exactly as model tool arguments do.
|
|
109
101
|
const args: unknown = JSON.parse(JSON.stringify({ path, ...scenario.args, unexpected: true }));
|
|
110
|
-
await assert.rejects(callTool(tool, args), (error:
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
assert.doesNotMatch(error.message, /Received arguments:\n/);
|
|
116
|
-
const echoedArgs = [...error.message.matchAll(/^Received arguments: (.+)$/gm)].map((match) =>
|
|
117
|
-
JSON.parse(match[1]),
|
|
102
|
+
await assert.rejects(callTool(tool, args), (error: unknown) => {
|
|
103
|
+
const result = argumentError(error);
|
|
104
|
+
assert.deepEqual(
|
|
105
|
+
new Set(result.issues.map((issue) => issue.field)),
|
|
106
|
+
new Set([...scenario.fields, "unexpected"]),
|
|
118
107
|
);
|
|
119
|
-
assert.deepEqual(echoedArgs.at(-1), { unexpected: true });
|
|
120
108
|
return true;
|
|
121
109
|
});
|
|
122
110
|
assert.equal(await readFile(path, "utf8"), original);
|
|
@@ -124,13 +112,756 @@ for (const scenario of cases) {
|
|
|
124
112
|
});
|
|
125
113
|
}
|
|
126
114
|
|
|
127
|
-
test("
|
|
115
|
+
test("grep and replace flags → accept V8 modes and reject invalid flags before patterns or file reads", async (t) => {
|
|
116
|
+
const cwd = await mkdtemp(join(tmpdir(), "hashline-flags-"));
|
|
117
|
+
t.after(() => rm(cwd, { recursive: true, force: true }));
|
|
118
|
+
const path = join(cwd, "missing.txt");
|
|
119
|
+
const tools = [
|
|
120
|
+
{
|
|
121
|
+
tool: makeGrepOverride(cwd, DEFAULT_CONFIG),
|
|
122
|
+
args: (find: string, literal: boolean, flags: string) => ({ pattern: find, literal, flags }),
|
|
123
|
+
field: "flags",
|
|
124
|
+
},
|
|
125
|
+
{
|
|
126
|
+
tool: makeReplaceTool(cwd, DEFAULT_CONFIG),
|
|
127
|
+
args: (find: string, literal: boolean, flags: string) => ({
|
|
128
|
+
replacements: [
|
|
129
|
+
{ literal: true, find: "valid", replace: "first" },
|
|
130
|
+
{ find, replace: "second", literal, flags },
|
|
131
|
+
],
|
|
132
|
+
}),
|
|
133
|
+
field: "replacements[1].flags",
|
|
134
|
+
},
|
|
135
|
+
];
|
|
136
|
+
for (const { tool, args, field } of tools) {
|
|
137
|
+
for (const literal of [false, true]) {
|
|
138
|
+
for (const flags of ["v", "uv", "x"]) {
|
|
139
|
+
await t.test(`${tool.name}/${literal ? "literal" : "regex"}/${flags}`, async () => {
|
|
140
|
+
const input = { path, ...args(flags === "v" ? "a" : "(", literal, flags) };
|
|
141
|
+
if (flags === "v") {
|
|
142
|
+
const result = await callTool(tool, input);
|
|
143
|
+
assert.equal(reportOf(result, tool.name)?.error, "PATH_NOT_FOUND");
|
|
144
|
+
} else {
|
|
145
|
+
await assert.rejects(callTool(tool, input), (error: unknown) => {
|
|
146
|
+
const report = argumentError(error);
|
|
147
|
+
assert.equal(report.issues.length, 1);
|
|
148
|
+
assert.equal(report.issues[0].field, field);
|
|
149
|
+
assert.match(report.issues[0].reason, /Invalid flags supplied to RegExp constructor/);
|
|
150
|
+
return true;
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
test("known edit operations with empty bodies → report the actionable issue once", async () => {
|
|
160
|
+
const tool = makeEditOverride(process.cwd(), DEFAULT_CONFIG);
|
|
161
|
+
for (const op of ["replace", "insert_after", "insert_before", "append", "prepend"]) {
|
|
162
|
+
const operation = {
|
|
163
|
+
op,
|
|
164
|
+
...(op === "append" || op === "prepend" ? {} : { anchor: `22#${anchorHash}` }),
|
|
165
|
+
body: [],
|
|
166
|
+
};
|
|
167
|
+
await assert.rejects(
|
|
168
|
+
callTool(tool, { path: "unused.txt", edits: [operation] }),
|
|
169
|
+
(error: unknown) => {
|
|
170
|
+
const result = argumentError(error);
|
|
171
|
+
assert.equal(result.issues.length, 1);
|
|
172
|
+
assert.equal(result.issues[0].field, "edits[0].body");
|
|
173
|
+
assert.match(result.issues[0].reason, /empty/);
|
|
174
|
+
assert.match(
|
|
175
|
+
result.issues[0].fix ?? "",
|
|
176
|
+
op === "replace" ? /delete/ : /supply at least one line/,
|
|
177
|
+
);
|
|
178
|
+
assert.equal(result.schemaLimited, undefined);
|
|
179
|
+
return true;
|
|
180
|
+
},
|
|
181
|
+
);
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
test("known edit branch has independent errors → report them without unrelated branches", async () => {
|
|
186
|
+
const tool = makeEditOverride(process.cwd(), DEFAULT_CONFIG, createActionFusionExecutor());
|
|
187
|
+
await assert.rejects(
|
|
188
|
+
callTool(tool, {
|
|
189
|
+
path: "unused.txt",
|
|
190
|
+
edits: [
|
|
191
|
+
{ op: "insert_after", body: [], extra: true },
|
|
192
|
+
{ op: "delete", anchor: `2#${anchorHash}`, body: ["wrong"] },
|
|
193
|
+
{ op: "replace", anchor: "bad", body: ["valid"] },
|
|
194
|
+
],
|
|
195
|
+
then_run: invalidCommand,
|
|
196
|
+
unexpected: true,
|
|
197
|
+
}),
|
|
198
|
+
(error: unknown) => {
|
|
199
|
+
const result = argumentError(error);
|
|
200
|
+
assert.deepEqual(
|
|
201
|
+
new Set(result.issues.map((issue) => issue.field)),
|
|
202
|
+
new Set([
|
|
203
|
+
"edits[0].body",
|
|
204
|
+
"edits[0].anchor",
|
|
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
|
+
});
|
|
217
|
+
|
|
218
|
+
test("edit operation tag is missing or invalid → report its field once without guessing a branch", async () => {
|
|
219
|
+
const tool = makeEditOverride(process.cwd(), DEFAULT_CONFIG);
|
|
220
|
+
for (const tag of [undefined, "unknown", null, 42, {}]) {
|
|
221
|
+
await assert.rejects(
|
|
222
|
+
callTool(tool, {
|
|
223
|
+
path: "unused.txt",
|
|
224
|
+
edits: [
|
|
225
|
+
{ ...(tag === undefined ? {} : { op: tag }), anchor: `1#${anchorHash}`, body: ["valid"] },
|
|
226
|
+
],
|
|
227
|
+
}),
|
|
228
|
+
(error: unknown) => {
|
|
229
|
+
const result = argumentError(error);
|
|
230
|
+
assert.equal(result.issues.length, 1);
|
|
231
|
+
assert.equal(result.issues[0].field, "edits[0].op");
|
|
232
|
+
if (tag === undefined) assert.match(result.issues[0].reason, /required/);
|
|
233
|
+
else
|
|
234
|
+
for (const op of [
|
|
235
|
+
"replace",
|
|
236
|
+
"delete",
|
|
237
|
+
"insert_before",
|
|
238
|
+
"insert_after",
|
|
239
|
+
"append",
|
|
240
|
+
"prepend",
|
|
241
|
+
])
|
|
242
|
+
assert.ok(result.issues[0].reason.includes(op));
|
|
243
|
+
return true;
|
|
244
|
+
},
|
|
245
|
+
);
|
|
246
|
+
}
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
test("edit range end is null → preserve the original rejection and its field diagnostic", async () => {
|
|
250
|
+
const tool = makeEditOverride(process.cwd(), DEFAULT_CONFIG);
|
|
251
|
+
for (const operation of [
|
|
252
|
+
{ op: "delete", anchor: `1#${anchorHash}`, end: null },
|
|
253
|
+
{ op: "replace", anchor: `1#${anchorHash}`, end: null, body: ["ok"] },
|
|
254
|
+
{ op: "replace", anchor: `1#${anchorHash}`, end: null, body: [] },
|
|
255
|
+
]) {
|
|
256
|
+
await assert.rejects(
|
|
257
|
+
callTool(tool, { path: "unused.txt", edits: [operation] }),
|
|
258
|
+
(error: unknown) => {
|
|
259
|
+
const result = argumentError(error);
|
|
260
|
+
assert.deepEqual(
|
|
261
|
+
result.issues.map((issue) => issue.field),
|
|
262
|
+
operation.body?.length === 0 ? ["edits[0].body", "edits[0].end"] : ["edits[0].end"],
|
|
263
|
+
);
|
|
264
|
+
return true;
|
|
265
|
+
},
|
|
266
|
+
);
|
|
267
|
+
}
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
test("edit item is not an object → report the item type once", async () => {
|
|
271
|
+
for (const item of [null, 42, "bad", [], true]) {
|
|
272
|
+
await assert.rejects(
|
|
273
|
+
callTool(makeEditOverride(process.cwd(), DEFAULT_CONFIG), {
|
|
274
|
+
path: "unused.txt",
|
|
275
|
+
edits: [item],
|
|
276
|
+
}),
|
|
277
|
+
(error: unknown) => {
|
|
278
|
+
const result = argumentError(error);
|
|
279
|
+
assert.equal(result.issues.length, 1);
|
|
280
|
+
return rejectsArgument("edits[0]", /object/)(error);
|
|
281
|
+
},
|
|
282
|
+
);
|
|
283
|
+
}
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
test("grep union value has one applicable type → report only its constraint", async () => {
|
|
287
|
+
for (const pattern of ["", [], [""]]) {
|
|
288
|
+
await assert.rejects(
|
|
289
|
+
callTool(makeGrepOverride(process.cwd(), DEFAULT_CONFIG), { pattern, literal: true }),
|
|
290
|
+
(error: unknown) => {
|
|
291
|
+
const result = argumentError(error);
|
|
292
|
+
assert.equal(result.issues.length, 1);
|
|
293
|
+
assert.match(result.issues[0].field, /^pattern(?:\[0\])?$/);
|
|
294
|
+
assert.doesNotMatch(result.issues[0].reason, /anyOf/);
|
|
295
|
+
return true;
|
|
296
|
+
},
|
|
297
|
+
);
|
|
298
|
+
}
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
test("literal enum is invalid → report the permitted values once", async () => {
|
|
302
|
+
await assert.rejects(
|
|
303
|
+
callTool(makeWriteOverride(process.cwd()), { path: "unused.txt", content: "ok", mode: "bad" }),
|
|
304
|
+
(error: unknown) => {
|
|
305
|
+
const result = argumentError(error);
|
|
306
|
+
assert.equal(result.issues.length, 1);
|
|
307
|
+
return rejectsArgument("mode", /create.*overwrite/)(error);
|
|
308
|
+
},
|
|
309
|
+
);
|
|
310
|
+
});
|
|
311
|
+
|
|
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
|
+
test("unknown field names contain punctuation, numeric keys or newlines → quote property keys without treating them as array indices", async () => {
|
|
375
|
+
for (const key of ["odd.\nfield/~", "0", "01", "a/b", "a~b", 'quote"field']) {
|
|
376
|
+
await assert.rejects(
|
|
377
|
+
callTool(makeReadOverride(process.cwd(), DEFAULT_CONFIG), {
|
|
378
|
+
path: "unused.txt",
|
|
379
|
+
[key]: true,
|
|
380
|
+
}),
|
|
381
|
+
(error: unknown) => {
|
|
382
|
+
const result = argumentError(error);
|
|
383
|
+
assert.equal(result.issues.length, 1);
|
|
384
|
+
assert.equal(result.issues[0].field, `[${JSON.stringify(key)}]`);
|
|
385
|
+
return true;
|
|
386
|
+
},
|
|
387
|
+
);
|
|
388
|
+
}
|
|
389
|
+
});
|
|
390
|
+
|
|
391
|
+
test("one field exhausts native diagnostics → label the limit and retain another field", async () => {
|
|
392
|
+
await assert.rejects(
|
|
393
|
+
callTool(makeEditOverride(process.cwd(), DEFAULT_CONFIG), {
|
|
394
|
+
path: "",
|
|
395
|
+
edits: Array.from({ length: 30 }, () => ({ op: "unknown" })),
|
|
396
|
+
}),
|
|
397
|
+
(error: unknown) => {
|
|
398
|
+
const result = argumentError(error);
|
|
399
|
+
assert.ok(result.issues.some((issue) => issue.field === "path"));
|
|
400
|
+
assert.ok(result.issues.some((issue) => issue.field === "edits[0].op"));
|
|
401
|
+
assert.equal(result.schemaLimited, true);
|
|
402
|
+
return true;
|
|
403
|
+
},
|
|
404
|
+
);
|
|
405
|
+
});
|
|
406
|
+
|
|
407
|
+
test("one field violates independent constraints → combine its reasons without losing either", async () => {
|
|
408
|
+
await assert.rejects(
|
|
409
|
+
callTool(makeEditOverride(process.cwd(), DEFAULT_CONFIG), {
|
|
410
|
+
path: "unused.txt",
|
|
411
|
+
edits: [{ op: "delete", anchor: `${Number.MAX_SAFE_INTEGER + 1}#A` }],
|
|
412
|
+
}),
|
|
413
|
+
(error: unknown) => {
|
|
414
|
+
const result = argumentError(error);
|
|
415
|
+
assert.equal(result.issues.length, 1);
|
|
416
|
+
return rejectsArgument("edits[0].anchor", /safe integer[\s\S]*hash characters/)(error);
|
|
417
|
+
},
|
|
418
|
+
);
|
|
419
|
+
await assert.rejects(
|
|
420
|
+
callTool(makeReadOverride(process.cwd(), DEFAULT_CONFIG), { path: "unused.txt", offset: 0.5 }),
|
|
421
|
+
(error: unknown) => {
|
|
422
|
+
const result = argumentError(error);
|
|
423
|
+
assert.equal(result.issues.length, 1);
|
|
424
|
+
assert.match(
|
|
425
|
+
result.issues[0].reason,
|
|
426
|
+
new RegExp(`multiple of ${POSITIVE_SAFE_INTEGER.multipleOf}`),
|
|
427
|
+
);
|
|
428
|
+
assert.match(result.issues[0].reason, new RegExp(`>= ${POSITIVE_SAFE_INTEGER.minimum}`));
|
|
429
|
+
return true;
|
|
430
|
+
},
|
|
431
|
+
);
|
|
432
|
+
});
|
|
433
|
+
|
|
434
|
+
test("copy/move destination tokens → reject unsafe positions and wrong hash lengths before reading", async () => {
|
|
435
|
+
const tool = makeEditOverride(process.cwd(), DEFAULT_CONFIG);
|
|
436
|
+
for (const op of ["copy", "move"]) {
|
|
437
|
+
for (const side of ["before", "after"]) {
|
|
438
|
+
for (const [token, reason] of [
|
|
439
|
+
[`${Number.MAX_SAFE_INTEGER + 1}#${anchorHash}`, /safe integer/],
|
|
440
|
+
["1#A", /has 1 hash characters/],
|
|
441
|
+
] as const) {
|
|
442
|
+
await assert.rejects(
|
|
443
|
+
callTool(tool, {
|
|
444
|
+
path: "missing-copy-move.txt",
|
|
445
|
+
edits: [{ op, anchor: `1#${anchorHash}`, [side]: token }],
|
|
446
|
+
}),
|
|
447
|
+
rejectsArgument(`edits[0].${side}`, reason),
|
|
448
|
+
);
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
});
|
|
453
|
+
|
|
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
|
+
/** Tool schema properties, so expected fix text comes from the schema instead of the test. */
|
|
609
|
+
interface SchemaNode {
|
|
610
|
+
readonly description?: string;
|
|
611
|
+
readonly const?: unknown;
|
|
612
|
+
readonly anyOf?: readonly SchemaNode[];
|
|
613
|
+
readonly properties?: Readonly<Record<string, SchemaNode>>;
|
|
614
|
+
readonly items?: SchemaNode;
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
const schemaProperties = (tool: {
|
|
618
|
+
readonly parameters: unknown;
|
|
619
|
+
}): Readonly<Record<string, SchemaNode>> =>
|
|
620
|
+
(tool.parameters as { properties: Readonly<Record<string, SchemaNode>> }).properties;
|
|
621
|
+
|
|
622
|
+
const inOrder = (fix: string, pieces: readonly string[]): void => {
|
|
623
|
+
let cursor = -1;
|
|
624
|
+
for (const piece of pieces) {
|
|
625
|
+
const at = fix.indexOf(piece);
|
|
626
|
+
assert.ok(at > cursor, `missing or out of order: ${piece} in ${fix}`);
|
|
627
|
+
cursor = at;
|
|
628
|
+
}
|
|
629
|
+
};
|
|
630
|
+
|
|
631
|
+
test("missing required parameter → fix states the parameter's role from its schema description", async () => {
|
|
632
|
+
let discoveries = 0;
|
|
633
|
+
const discover = async () => {
|
|
634
|
+
discoveries++;
|
|
635
|
+
return { code: 1, stderr: "", stopped: false };
|
|
636
|
+
};
|
|
637
|
+
const grep = makeGrepOverrideWithBackend(process.cwd(), DEFAULT_CONFIG, {
|
|
638
|
+
runRgPaths: discover,
|
|
639
|
+
runRgLines: discover,
|
|
640
|
+
});
|
|
641
|
+
const write = makeWriteOverride(process.cwd());
|
|
642
|
+
const edit = makeEditOverride(process.cwd(), DEFAULT_CONFIG);
|
|
643
|
+
const cases = [
|
|
644
|
+
{
|
|
645
|
+
name: "grep literal",
|
|
646
|
+
tool: grep,
|
|
647
|
+
args: { pattern: "text" },
|
|
648
|
+
field: "literal",
|
|
649
|
+
verify: (fix: string) => assert.equal(fix, schemaProperties(grep).literal?.description),
|
|
650
|
+
},
|
|
651
|
+
{
|
|
652
|
+
name: "write mode",
|
|
653
|
+
tool: write,
|
|
654
|
+
args: { path: "unused.txt", content: "text" },
|
|
655
|
+
field: "mode",
|
|
656
|
+
verify: (fix: string) => {
|
|
657
|
+
const [create, overwrite] = schemaProperties(write).mode?.anyOf ?? [];
|
|
658
|
+
assert.ok(create?.description && overwrite?.description);
|
|
659
|
+
inOrder(fix, ['"create"', create.description, '"overwrite"', overwrite.description]);
|
|
660
|
+
},
|
|
661
|
+
},
|
|
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
|
+
] as const;
|
|
684
|
+
for (const scenario of cases) {
|
|
685
|
+
await assert.rejects(callTool(scenario.tool, scenario.args), (error: unknown) => {
|
|
686
|
+
const result = argumentError(error);
|
|
687
|
+
const issue = result.issues.find((candidate) => candidate.field === scenario.field);
|
|
688
|
+
assert.ok(issue, JSON.stringify(result.issues));
|
|
689
|
+
assert.match(issue.reason, /is required/);
|
|
690
|
+
assert.ok(issue.fix, JSON.stringify(result.issues));
|
|
691
|
+
scenario.verify(issue.fix);
|
|
692
|
+
return true;
|
|
693
|
+
});
|
|
694
|
+
}
|
|
695
|
+
assert.equal(discoveries, 0);
|
|
696
|
+
});
|
|
697
|
+
|
|
698
|
+
test("two operations miss the same required field → the shared fix appears once", async () => {
|
|
128
699
|
const tool = makeEditOverride(process.cwd(), DEFAULT_CONFIG);
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
700
|
+
await assert.rejects(
|
|
701
|
+
callTool(tool, { path: "unused.txt", edits: [{ op: "replace" }, { op: "replace" }] }),
|
|
702
|
+
(error: unknown) => {
|
|
703
|
+
const result = argumentError(error);
|
|
704
|
+
const anchors = result.issues.filter((issue) => issue.field.endsWith(".anchor"));
|
|
705
|
+
assert.equal(anchors.length, 2);
|
|
706
|
+
assert.equal(anchors.filter((issue) => issue.fix).length, 1);
|
|
707
|
+
const fixes = result.issues.flatMap((issue) => (issue.fix ? [issue.fix] : []));
|
|
708
|
+
assert.equal(new Set(fixes).size, fixes.length, JSON.stringify(fixes));
|
|
709
|
+
const anchorDescriptions = new Set(
|
|
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];
|
|
716
|
+
assert.ok(description);
|
|
717
|
+
assert.ok(fixes.includes(description));
|
|
718
|
+
return true;
|
|
719
|
+
},
|
|
720
|
+
);
|
|
721
|
+
});
|
|
722
|
+
|
|
723
|
+
test("invalid enum value → the reason carries no description fix", async () => {
|
|
724
|
+
await assert.rejects(
|
|
725
|
+
callTool(makeGrepOverride(process.cwd(), DEFAULT_CONFIG), {
|
|
726
|
+
path: "unused.txt",
|
|
727
|
+
pattern: "text",
|
|
728
|
+
literal: true,
|
|
729
|
+
outputMode: "unknown",
|
|
730
|
+
}),
|
|
731
|
+
(error: unknown) => {
|
|
732
|
+
const result = argumentError(error);
|
|
733
|
+
assert.equal(result.issues.length, 1);
|
|
734
|
+
assert.equal(result.issues[0].field, "outputMode");
|
|
735
|
+
assert.match(result.issues[0].reason, /expected one of/);
|
|
736
|
+
assert.equal(result.issues[0].fix, undefined);
|
|
737
|
+
return true;
|
|
738
|
+
},
|
|
739
|
+
);
|
|
740
|
+
});
|
|
741
|
+
|
|
742
|
+
const branchWithOp = (tool: { readonly parameters: unknown }, op: string): SchemaNode => {
|
|
743
|
+
const branch = (schemaProperties(tool).edits?.items?.anyOf ?? []).find((candidate) => {
|
|
744
|
+
const opSchema = candidate.properties?.op;
|
|
745
|
+
return [opSchema?.const, ...(opSchema?.anyOf ?? []).map((option) => option.const)].includes(op);
|
|
135
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
|
+
};
|
|
758
|
+
|
|
759
|
+
test("extra field is not allowed → fix describes the selected shape from its schema", async () => {
|
|
760
|
+
const edit = makeEditOverride(process.cwd(), DEFAULT_CONFIG);
|
|
761
|
+
const grep = makeGrepOverride(process.cwd(), DEFAULT_CONFIG);
|
|
762
|
+
const append = branchWithOp(edit, "append");
|
|
763
|
+
const move = branchWithOp(edit, "move");
|
|
764
|
+
const cases = [
|
|
765
|
+
{
|
|
766
|
+
name: "tagged branch rejects another branch's field",
|
|
767
|
+
tool: edit,
|
|
768
|
+
args: {
|
|
769
|
+
path: "unused.txt",
|
|
770
|
+
edits: [{ op: "append", anchor: `1#${anchorHash}`, body: ["text"] }],
|
|
771
|
+
},
|
|
772
|
+
field: "edits[0].anchor",
|
|
773
|
+
fix: `${opExplanation(append)} ${acceptedFields(append)}`,
|
|
774
|
+
},
|
|
775
|
+
{
|
|
776
|
+
name: "unknown top-level field",
|
|
777
|
+
tool: grep,
|
|
778
|
+
args: { path: "unused.txt", pattern: "text", literal: true, bogus: 1 },
|
|
779
|
+
field: "bogus",
|
|
780
|
+
fix: acceptedFields({ properties: schemaProperties(grep) }),
|
|
781
|
+
},
|
|
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) {
|
|
796
|
+
await assert.rejects(callTool(scenario.tool, scenario.args), (error: unknown) => {
|
|
797
|
+
const result = argumentError(error);
|
|
798
|
+
const issue = result.issues.find((candidate) => candidate.field === scenario.field);
|
|
799
|
+
assert.ok(issue, JSON.stringify(result.issues));
|
|
800
|
+
assert.match(issue.reason, /is not allowed/);
|
|
801
|
+
assert.equal(issue.fix, scenario.fix, JSON.stringify(result.issues));
|
|
802
|
+
return true;
|
|
803
|
+
});
|
|
804
|
+
}
|
|
805
|
+
});
|
|
806
|
+
|
|
807
|
+
test("value mismatches the field pattern → fix states the field description from the schema", async () => {
|
|
808
|
+
const edit = makeEditOverride(process.cwd(), DEFAULT_CONFIG);
|
|
809
|
+
const append = branchWithOp(edit, "append");
|
|
810
|
+
const replace = branchWithOp(edit, "replace");
|
|
811
|
+
const cases = [
|
|
812
|
+
{
|
|
813
|
+
name: "body element with a newline",
|
|
814
|
+
args: { path: "unused.txt", edits: [{ op: "append", body: ["a\nb"] }] },
|
|
815
|
+
field: "edits[0].body[0]",
|
|
816
|
+
fix: append.properties?.body?.description,
|
|
817
|
+
},
|
|
818
|
+
{
|
|
819
|
+
name: "anchor without the line-hash shape",
|
|
820
|
+
args: { path: "unused.txt", edits: [{ op: "replace", anchor: "abc", body: ["text"] }] },
|
|
821
|
+
field: "edits[0].anchor",
|
|
822
|
+
fix: replace.properties?.anchor?.description,
|
|
823
|
+
},
|
|
824
|
+
] as const;
|
|
825
|
+
for (const scenario of cases) {
|
|
826
|
+
await assert.rejects(callTool(edit, scenario.args), (error: unknown) => {
|
|
827
|
+
const result = argumentError(error);
|
|
828
|
+
const issue = result.issues.find((candidate) => candidate.field === scenario.field);
|
|
829
|
+
assert.ok(issue, JSON.stringify(result.issues));
|
|
830
|
+
assert.match(issue.reason, /^must match pattern/);
|
|
831
|
+
assert.equal(issue.fix, scenario.fix, JSON.stringify(result.issues));
|
|
832
|
+
return true;
|
|
833
|
+
});
|
|
834
|
+
}
|
|
835
|
+
});
|
|
836
|
+
|
|
837
|
+
test("two unknown fields in one object → the shared fix appears once", async () => {
|
|
838
|
+
const edit = makeEditOverride(process.cwd(), DEFAULT_CONFIG);
|
|
839
|
+
await assert.rejects(
|
|
840
|
+
callTool(edit, {
|
|
841
|
+
path: "unused.txt",
|
|
842
|
+
edits: [{ op: "append", body: ["text"], zzz: 1, yyy: 2 }],
|
|
843
|
+
}),
|
|
844
|
+
(error: unknown) => {
|
|
845
|
+
const result = argumentError(error);
|
|
846
|
+
assert.equal(result.issues.length, 2, JSON.stringify(result.issues));
|
|
847
|
+
assert.ok(result.issues.every((issue) => issue.reason === "is not allowed"));
|
|
848
|
+
assert.ok(result.issues[0].fix, JSON.stringify(result.issues));
|
|
849
|
+
assert.equal(result.issues[1].fix, undefined);
|
|
850
|
+
return true;
|
|
851
|
+
},
|
|
852
|
+
);
|
|
853
|
+
});
|
|
854
|
+
|
|
855
|
+
test("value has the wrong type → the reason carries no fix", async () => {
|
|
856
|
+
await assert.rejects(
|
|
857
|
+
callTool(makeReadOverride(process.cwd(), DEFAULT_CONFIG), { path: "unused.txt", limit: "abc" }),
|
|
858
|
+
(error: unknown) => {
|
|
859
|
+
const result = argumentError(error);
|
|
860
|
+
assert.equal(result.issues.length, 1);
|
|
861
|
+
assert.equal(result.issues[0].field, "limit");
|
|
862
|
+
assert.match(result.issues[0].reason, /must be number/);
|
|
863
|
+
assert.equal(result.issues[0].fix, undefined);
|
|
864
|
+
return true;
|
|
865
|
+
},
|
|
866
|
+
);
|
|
136
867
|
});
|