@criogaid/pi-hashline-edit 0.9.0 → 0.11.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 +12 -12
- package/README.zh-CN.md +12 -12
- 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 +96 -271
- package/src/core/apply.ts +62 -169
- package/src/core/case-fold.ts +8 -67
- package/src/core/character-set.test.ts +2 -3
- package/src/core/character-set.ts +5 -5
- 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 +2 -1
- package/src/core/ranges.ts +1 -1
- 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 +8 -44
- package/src/index.ts +8 -8
- package/src/integration/config-registration.test.ts +8 -10
- package/src/integration/fusion-dispatch.test.ts +4 -7
- package/src/integration/grep-errors.test.ts +3 -3
- package/src/integration/grep-flags.test.ts +5 -19
- package/src/integration/grep-js-completeness.test.ts +45 -18
- package/src/integration/grep-line-candidates.test.ts +7 -10
- package/src/integration/grep-matrix.test.ts +6 -9
- package/src/integration/grep-order.test.ts +4 -4
- package/src/integration/grep-path-matrix.test.ts +10 -9
- package/src/integration/grep-prefilter-coverage.test.ts +5 -8
- package/src/integration/grep-process-limits.test.ts +76 -0
- package/src/integration/grep-regex.test.ts +10 -10
- package/src/integration/grep-rg.test.ts +166 -66
- package/src/integration/grep-scan-budget.test.ts +9 -9
- package/src/integration/grep-translation.test.ts +18 -32
- package/src/integration/large-text.test.ts +6 -52
- package/src/integration/model-budget-lifecycle.test.ts +10 -9
- package/src/integration/mutation-file-types.testing.ts +4 -4
- package/src/integration/npm-package.testing.ts +3 -3
- package/src/integration/publication-read-changes.test.ts +161 -83
- package/src/integration/read-changes.test.ts +4 -4
- package/src/integration/replace-worker-boundary.test.ts +22 -12
- package/src/integration/report-replay.test.ts +23 -26
- package/src/integration/utf8-errors.test.ts +4 -4
- package/src/pi/AGENTS.md +18 -17
- package/src/pi/action-fusion.test.ts +241 -353
- package/src/pi/action-fusion.testing.ts +38 -0
- package/src/pi/action-fusion.ts +150 -125
- package/src/pi/anchor-format.test.ts +9 -9
- package/src/pi/anchor-format.ts +6 -8
- package/src/pi/argument-diagnostics.ts +25 -28
- package/src/pi/argument-error.ts +2 -1
- package/src/pi/argument-format.test.ts +19 -18
- package/src/pi/argument-validation.test.ts +334 -283
- package/src/pi/argument-validation.ts +5 -8
- package/src/pi/budgets.ts +6 -8
- package/src/pi/codemode.test.ts +7 -2
- package/src/pi/config.test.ts +7 -3
- package/src/pi/config.ts +12 -9
- package/src/pi/diagnostic-buffer.test.ts +34 -13
- package/src/pi/diagnostic-buffer.ts +1 -2
- package/src/pi/directory-sync.test.ts +0 -1
- package/src/pi/edit-tool.ts +86 -211
- package/src/pi/error-text.ts +10 -9
- package/src/pi/execute.test.ts +249 -345
- package/src/pi/failure-context.test.ts +146 -127
- package/src/pi/failure-context.ts +45 -94
- package/src/pi/failure-path.test.ts +182 -155
- package/src/pi/file-commit.test.ts +156 -71
- package/src/pi/file-commit.ts +126 -146
- package/src/pi/file-snapshot.test.ts +3 -5
- package/src/pi/fusion-card.test.ts +26 -22
- package/src/pi/fusion-card.ts +16 -24
- package/src/pi/grep-candidates.test.ts +19 -120
- package/src/pi/grep-candidates.ts +25 -76
- package/src/pi/grep-matrix.test.ts +5 -5
- package/src/pi/grep-order.test.ts +16 -15
- package/src/pi/grep-output.ts +94 -107
- package/src/pi/grep-render.ts +1 -1
- package/src/pi/grep-reread.ts +97 -0
- package/src/pi/grep-rg.testing.ts +70 -0
- package/src/pi/grep-scope.ts +41 -43
- package/src/pi/grep-search.ts +27 -39
- package/src/pi/grep-tool.ts +63 -106
- package/src/pi/grep.test.ts +177 -156
- package/src/pi/model-budgets.test.ts +27 -24
- package/src/pi/model-budgets.ts +6 -38
- package/src/pi/mutation-outcome.testing.ts +0 -1
- package/src/pi/mutation-result.test.ts +44 -35
- package/src/pi/mutation-result.ts +52 -21
- package/src/pi/mutation-runner.ts +134 -74
- package/src/pi/output-budgets.test.ts +86 -42
- package/src/pi/path.ts +1 -1
- package/src/pi/pi.test.ts +18 -13
- package/src/pi/read-tool.ts +18 -23
- package/src/pi/read.test.ts +22 -41
- package/src/pi/regex-worker-contract.ts +3 -13
- package/src/pi/regex-worker.ts +1 -10
- package/src/pi/registration.test.ts +85 -51
- package/src/pi/render.test.ts +88 -59
- package/src/pi/render.ts +64 -65
- package/src/pi/replace-regex.ts +14 -2
- package/src/pi/replace-tool.ts +22 -94
- package/src/pi/replace-worker.ts +1 -1
- package/src/pi/replace.test.ts +53 -38
- package/src/pi/report-cause.ts +3 -8
- package/src/pi/report-failure.testing.ts +10 -3
- package/src/pi/report-schema.ts +251 -148
- package/src/pi/report.test.ts +215 -67
- package/src/pi/report.ts +50 -98
- package/src/pi/result-safety.test.ts +241 -247
- package/src/pi/rg-process.test.ts +77 -63
- package/src/pi/rg-process.ts +53 -75
- package/src/pi/rg-regex.ts +40 -400
- package/src/pi/schema.ts +35 -0
- package/src/pi/text-stream.test.ts +0 -21
- package/src/pi/text-stream.ts +14 -21
- package/src/pi/tool-factories.testing.ts +59 -0
- package/src/pi/write-tool.ts +36 -71
- package/src/pi/write.test.ts +31 -34
- package/src/testing/regex-translation-corpus.ts +12 -12
- package/src/testing/run-tests.ts +1 -1
- package/src/pi/replace-worker-contract.ts +0 -10
|
@@ -1,20 +1,30 @@
|
|
|
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";
|
|
6
7
|
import { createActionFusionExecutor } from "./action-fusion.ts";
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import { makeReadOverride } from "./read-tool.ts";
|
|
11
|
-
import { makeReplaceTool } from "./replace-tool.ts";
|
|
12
|
-
import { makeWriteOverride } from "./write-tool.ts";
|
|
13
|
-
import { callTool } from "./tool-call.testing.ts";
|
|
8
|
+
import { makeMockGrep } from "./grep-rg.testing.ts";
|
|
9
|
+
import { commandCompletion } from "./action-fusion.testing.ts";
|
|
10
|
+
import { callTool, createToolContext } from "./tool-call.testing.ts";
|
|
14
11
|
import { argumentError, rejectsArgument } from "./argument-error.testing.ts";
|
|
15
12
|
import { POSITIVE_SAFE_INTEGER } from "./schema.ts";
|
|
16
13
|
import { reportOf } from "./report.ts";
|
|
17
14
|
import { createAnchorFormatter } from "./anchor-format.ts";
|
|
15
|
+
import { computeLineHash } from "../core/hash.ts";
|
|
16
|
+
|
|
17
|
+
// Load candidate-dependent helpers only after grep-rg.testing registers the module mock.
|
|
18
|
+
const { grepCounts } = await import("./grep-output.testing.ts");
|
|
19
|
+
|
|
20
|
+
const {
|
|
21
|
+
DEFAULT_CONFIG,
|
|
22
|
+
makeReadOverride,
|
|
23
|
+
makeGrepOverride,
|
|
24
|
+
makeEditOverride,
|
|
25
|
+
makeReplaceTool,
|
|
26
|
+
makeWriteOverride,
|
|
27
|
+
} = await import("./tool-factories.testing.ts");
|
|
18
28
|
|
|
19
29
|
const invalidCommand = { command: "", timeout: 0 };
|
|
20
30
|
const anchorHash = "A".repeat(DEFAULT_CONFIG.hashLen);
|
|
@@ -38,24 +48,32 @@ const cases = [
|
|
|
38
48
|
make: (cwd: string, fusion: ReturnType<typeof createActionFusionExecutor>) =>
|
|
39
49
|
makeEditOverride(cwd, DEFAULT_CONFIG, fusion),
|
|
40
50
|
args: {
|
|
41
|
-
|
|
42
|
-
{ action: "replace", anchor: `1#${anchorHash}`,
|
|
43
|
-
{ action: "append",
|
|
51
|
+
steps: [
|
|
52
|
+
{ action: "replace", anchor: `1#${anchorHash}`, lines: [] },
|
|
53
|
+
{ action: "append", lines: ["bad\0", "bad\ud800"] },
|
|
44
54
|
{ action: "delete", anchor: `${Number.MAX_SAFE_INTEGER + 1}#${anchorHash}`, end: "1#A" },
|
|
45
|
-
{ action: "append",
|
|
55
|
+
{ action: "append", lines: [] },
|
|
56
|
+
{ action: "replace", anchor: `3#${anchorHash}`, end: `1#${anchorHash}`, lines: ["text"] },
|
|
57
|
+
{ action: "delete", anchor: `4#${anchorHash}`, end: `2#${anchorHash}` },
|
|
46
58
|
],
|
|
47
59
|
then_run: invalidCommand,
|
|
48
60
|
},
|
|
49
61
|
fields: [
|
|
50
|
-
"
|
|
51
|
-
"
|
|
52
|
-
"
|
|
53
|
-
"
|
|
54
|
-
"
|
|
55
|
-
"
|
|
62
|
+
"steps[0].lines",
|
|
63
|
+
"steps[1].lines[0]",
|
|
64
|
+
"steps[1].lines[1]",
|
|
65
|
+
"steps[2].anchor",
|
|
66
|
+
"steps[2].end",
|
|
67
|
+
"steps[3].lines",
|
|
68
|
+
"steps[4].end",
|
|
69
|
+
"steps[5].end",
|
|
56
70
|
"then_run.command",
|
|
57
71
|
"then_run.timeout",
|
|
58
72
|
],
|
|
73
|
+
reasons: [
|
|
74
|
+
{ field: "steps[4].end", reason: /line 1 is before anchor line 3/ },
|
|
75
|
+
{ field: "steps[5].end", reason: /line 2 is before anchor line 4/ },
|
|
76
|
+
],
|
|
59
77
|
},
|
|
60
78
|
{
|
|
61
79
|
name: "replace",
|
|
@@ -94,27 +112,90 @@ for (const scenario of cases) {
|
|
|
94
112
|
const original = "original\n";
|
|
95
113
|
await writeFile(path, original);
|
|
96
114
|
let commandRuns = 0;
|
|
97
|
-
const fusion = createActionFusionExecutor(
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
115
|
+
const fusion = createActionFusionExecutor(() => {});
|
|
116
|
+
const ctx = {
|
|
117
|
+
...(await createToolContext(cwd, t)),
|
|
118
|
+
executeTool: async () => {
|
|
119
|
+
commandRuns++;
|
|
120
|
+
return commandCompletion({ content: [{ type: "text", text: "ran" }], details: undefined });
|
|
121
|
+
},
|
|
122
|
+
};
|
|
101
123
|
const tool = scenario.make(cwd, fusion);
|
|
102
124
|
const args: unknown = JSON.parse(JSON.stringify({ path, ...scenario.args, unexpected: true }));
|
|
103
125
|
const snapshot = structuredClone(args);
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
126
|
+
const open = t.mock.method(fs, "open");
|
|
127
|
+
syncBuiltinESMExports();
|
|
128
|
+
try {
|
|
129
|
+
await assert.rejects(callTool(tool, args, { ctx }), (error: unknown) => {
|
|
130
|
+
const result = argumentError(error);
|
|
131
|
+
assert.deepEqual(
|
|
132
|
+
new Set(result.issues.map((issue) => issue.field)),
|
|
133
|
+
new Set([...scenario.fields, "unexpected"]),
|
|
134
|
+
);
|
|
135
|
+
if ("reasons" in scenario) {
|
|
136
|
+
for (const expected of scenario.reasons) {
|
|
137
|
+
const issue = result.issues.find((issue) => issue.field === expected.field);
|
|
138
|
+
assert.ok(issue);
|
|
139
|
+
assert.match(issue.reason, expected.reason);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
return true;
|
|
143
|
+
});
|
|
144
|
+
assert.equal(open.mock.callCount(), 0);
|
|
145
|
+
} finally {
|
|
146
|
+
t.mock.restoreAll();
|
|
147
|
+
syncBuiltinESMExports();
|
|
148
|
+
}
|
|
112
149
|
assert.deepEqual(args, snapshot);
|
|
113
150
|
assert.equal(await readFile(path, "utf8"), original);
|
|
114
151
|
assert.equal(commandRuns, 0);
|
|
115
152
|
});
|
|
116
153
|
}
|
|
117
154
|
|
|
155
|
+
test("blank then_run command with Fusion → reports exactly one command issue", async (t) => {
|
|
156
|
+
const cwd = process.cwd();
|
|
157
|
+
const fusion = createActionFusionExecutor(() => {});
|
|
158
|
+
const mutations = [
|
|
159
|
+
{
|
|
160
|
+
name: "edit",
|
|
161
|
+
tool: makeEditOverride(cwd, DEFAULT_CONFIG, fusion),
|
|
162
|
+
args: { steps: [{ action: "append", lines: ["text"] }] },
|
|
163
|
+
},
|
|
164
|
+
{
|
|
165
|
+
name: "replace",
|
|
166
|
+
tool: makeReplaceTool(cwd, DEFAULT_CONFIG, fusion),
|
|
167
|
+
args: { replacements: [{ find: "old", replace: "new", literal: true }] },
|
|
168
|
+
},
|
|
169
|
+
{
|
|
170
|
+
name: "write",
|
|
171
|
+
tool: makeWriteOverride(cwd, fusion),
|
|
172
|
+
args: { content: "text", mode: "create" },
|
|
173
|
+
},
|
|
174
|
+
] as const;
|
|
175
|
+
for (const scenario of mutations) {
|
|
176
|
+
for (const command of ["", " "]) {
|
|
177
|
+
await t.test(
|
|
178
|
+
`${scenario.name}/${JSON.stringify(command)} → rejects one command issue`,
|
|
179
|
+
async () => {
|
|
180
|
+
await assert.rejects(
|
|
181
|
+
callTool(scenario.tool, {
|
|
182
|
+
path: "unused.txt",
|
|
183
|
+
...scenario.args,
|
|
184
|
+
then_run: { command },
|
|
185
|
+
}),
|
|
186
|
+
(error: unknown) => {
|
|
187
|
+
const result = argumentError(error);
|
|
188
|
+
assert.equal(result.issues.length, 1, JSON.stringify(result));
|
|
189
|
+
assert.equal(result.issues[0].field, "then_run.command");
|
|
190
|
+
return true;
|
|
191
|
+
},
|
|
192
|
+
);
|
|
193
|
+
},
|
|
194
|
+
);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
});
|
|
198
|
+
|
|
118
199
|
test("grep and replace flags → accept V8 modes and reject invalid flags before patterns or file reads", async (t) => {
|
|
119
200
|
const cwd = await mkdtemp(join(tmpdir(), "hashline-flags-"));
|
|
120
201
|
t.after(() => rm(cwd, { recursive: true, force: true }));
|
|
@@ -159,70 +240,52 @@ test("grep and replace flags → accept V8 modes and reject invalid flags before
|
|
|
159
240
|
}
|
|
160
241
|
});
|
|
161
242
|
|
|
162
|
-
const editFields = ["anchor", "end", "
|
|
243
|
+
const editFields = ["anchor", "end", "lines"] as const;
|
|
163
244
|
type EditField = (typeof editFields)[number];
|
|
164
245
|
const fieldValues = {
|
|
165
246
|
anchor: `1#${anchorHash}`,
|
|
166
247
|
end: `2#${anchorHash}`,
|
|
167
|
-
|
|
168
|
-
before: `3#${anchorHash}`,
|
|
169
|
-
after: `3#${anchorHash}`,
|
|
248
|
+
lines: ["text"],
|
|
170
249
|
};
|
|
171
250
|
const actionCases: readonly {
|
|
172
251
|
action: string;
|
|
173
252
|
required: readonly EditField[];
|
|
174
253
|
optional: readonly EditField[];
|
|
175
|
-
transfer?: boolean;
|
|
176
254
|
expected: string;
|
|
177
255
|
}[] = [
|
|
178
256
|
{
|
|
179
257
|
action: "replace",
|
|
180
|
-
required: ["anchor", "
|
|
258
|
+
required: ["anchor", "lines"],
|
|
181
259
|
optional: ["end"],
|
|
182
260
|
expected: "text\nbeta\ngamma\n",
|
|
183
261
|
},
|
|
184
262
|
{ action: "delete", required: ["anchor"], optional: ["end"], expected: "beta\ngamma\n" },
|
|
185
263
|
{
|
|
186
264
|
action: "insert_after",
|
|
187
|
-
required: ["anchor", "
|
|
265
|
+
required: ["anchor", "lines"],
|
|
188
266
|
optional: [],
|
|
189
267
|
expected: "alpha\ntext\nbeta\ngamma\n",
|
|
190
268
|
},
|
|
191
269
|
{
|
|
192
270
|
action: "insert_before",
|
|
193
|
-
required: ["anchor", "
|
|
271
|
+
required: ["anchor", "lines"],
|
|
194
272
|
optional: [],
|
|
195
273
|
expected: "text\nalpha\nbeta\ngamma\n",
|
|
196
274
|
},
|
|
197
|
-
{
|
|
198
|
-
|
|
199
|
-
required: ["anchor"],
|
|
200
|
-
optional: ["end", "before", "after"],
|
|
201
|
-
transfer: true,
|
|
202
|
-
expected: "alpha\nbeta\nalpha\ngamma\n",
|
|
203
|
-
},
|
|
204
|
-
{
|
|
205
|
-
action: "move",
|
|
206
|
-
required: ["anchor"],
|
|
207
|
-
optional: ["end", "before", "after"],
|
|
208
|
-
transfer: true,
|
|
209
|
-
expected: "beta\nalpha\ngamma\n",
|
|
210
|
-
},
|
|
211
|
-
{ action: "append", required: ["body"], optional: [], expected: "alpha\nbeta\ngamma\ntext\n" },
|
|
212
|
-
{ action: "prepend", required: ["body"], optional: [], expected: "text\nalpha\nbeta\ngamma\n" },
|
|
275
|
+
{ action: "append", required: ["lines"], optional: [], expected: "alpha\nbeta\ngamma\ntext\n" },
|
|
276
|
+
{ action: "prepend", required: ["lines"], optional: [], expected: "text\nalpha\nbeta\ngamma\n" },
|
|
213
277
|
];
|
|
214
278
|
|
|
215
279
|
for (const scenario of actionCases) {
|
|
216
280
|
test(`${scenario.action} field matrix → requires its fields and rejects fields it does not take`, async (t) => {
|
|
217
281
|
const tool = makeEditOverride(process.cwd(), DEFAULT_CONFIG);
|
|
218
|
-
const fields = schemaProperties(tool).
|
|
282
|
+
const fields = schemaProperties(tool).steps?.items?.properties;
|
|
219
283
|
const choice = fields?.action?.anyOf?.find((option) => option.const === scenario.action);
|
|
220
284
|
assert.ok(choice?.description);
|
|
221
285
|
const description = choice.description;
|
|
222
286
|
const valid = {
|
|
223
287
|
action: scenario.action,
|
|
224
288
|
...Object.fromEntries(scenario.required.map((field) => [field, fieldValues[field]])),
|
|
225
|
-
...(scenario.transfer ? { before: fieldValues.before } : {}),
|
|
226
289
|
};
|
|
227
290
|
for (const field of scenario.required) {
|
|
228
291
|
for (const value of [undefined, null]) {
|
|
@@ -233,11 +296,11 @@ for (const scenario of actionCases) {
|
|
|
233
296
|
Object.entries(valid).filter(([key]) => key !== field),
|
|
234
297
|
);
|
|
235
298
|
if (value === null) edit[field] = null;
|
|
236
|
-
const args = { path: "unused.txt",
|
|
299
|
+
const args = { path: "unused.txt", steps: [edit] };
|
|
237
300
|
await assert.rejects(callTool(tool, args), (error: unknown) => {
|
|
238
301
|
assert.deepEqual(argumentError(error).issues, [
|
|
239
302
|
{
|
|
240
|
-
field: `
|
|
303
|
+
field: `steps[0].${field}`,
|
|
241
304
|
reason: `is required for "${scenario.action}"`,
|
|
242
305
|
fix: fields?.[field]?.description,
|
|
243
306
|
},
|
|
@@ -255,12 +318,12 @@ for (const scenario of actionCases) {
|
|
|
255
318
|
await assert.rejects(
|
|
256
319
|
callTool(tool, {
|
|
257
320
|
path: "unused.txt",
|
|
258
|
-
|
|
321
|
+
steps: [{ ...valid, [field]: fieldValues[field] }],
|
|
259
322
|
}),
|
|
260
323
|
(error: unknown) => {
|
|
261
324
|
assert.deepEqual(argumentError(error).issues, [
|
|
262
325
|
{
|
|
263
|
-
field: `
|
|
326
|
+
field: `steps[0].${field}`,
|
|
264
327
|
reason: "is not allowed",
|
|
265
328
|
fix: `"${scenario.action}": ${description}`,
|
|
266
329
|
},
|
|
@@ -274,44 +337,31 @@ for (const scenario of actionCases) {
|
|
|
274
337
|
});
|
|
275
338
|
}
|
|
276
339
|
|
|
277
|
-
test("edit action is missing
|
|
340
|
+
test("edit action is missing → reports choices without guessing field permissions", async () => {
|
|
278
341
|
const tool = makeEditOverride(process.cwd(), DEFAULT_CONFIG);
|
|
279
|
-
const choices = schemaProperties(tool).
|
|
280
|
-
assert.
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
(
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
inOrder(issue.fix ?? "", [JSON.stringify(choice.const), choice.description]);
|
|
303
|
-
}
|
|
304
|
-
} else {
|
|
305
|
-
assert.equal(
|
|
306
|
-
issue.reason,
|
|
307
|
-
`expected one of: ${choices.map((choice) => JSON.stringify(choice.const)).join(", ")}`,
|
|
308
|
-
);
|
|
309
|
-
assert.equal(issue.fix, undefined);
|
|
310
|
-
}
|
|
311
|
-
return true;
|
|
312
|
-
},
|
|
313
|
-
);
|
|
314
|
-
}
|
|
342
|
+
const choices = schemaProperties(tool).steps?.items?.properties?.action?.anyOf ?? [];
|
|
343
|
+
assert.deepEqual(
|
|
344
|
+
choices.map((choice) => choice.const),
|
|
345
|
+
actionCases.map(({ action }) => action),
|
|
346
|
+
);
|
|
347
|
+
await assert.rejects(
|
|
348
|
+
callTool(tool, {
|
|
349
|
+
path: "unused.txt",
|
|
350
|
+
steps: [{ anchor: `1#${anchorHash}`, lines: ["valid"] }],
|
|
351
|
+
}),
|
|
352
|
+
(error: unknown) => {
|
|
353
|
+
const { issues } = argumentError(error);
|
|
354
|
+
assert.equal(issues.length, 1);
|
|
355
|
+
const issue = issues[0];
|
|
356
|
+
assert.equal(issue.field, "steps[0].action");
|
|
357
|
+
assert.equal(issue.reason, "is required");
|
|
358
|
+
for (const choice of choices) {
|
|
359
|
+
assert.ok(choice.description);
|
|
360
|
+
inOrder(issue.fix ?? "", [JSON.stringify(choice.const), choice.description]);
|
|
361
|
+
}
|
|
362
|
+
return true;
|
|
363
|
+
},
|
|
364
|
+
);
|
|
315
365
|
});
|
|
316
366
|
|
|
317
367
|
test("optional edit fields and then_run are null with Fusion → publishes bytes without rewriting caller arguments", async (t) => {
|
|
@@ -319,72 +369,99 @@ test("optional edit fields and then_run are null with Fusion → publishes bytes
|
|
|
319
369
|
t.after(() => rm(cwd, { recursive: true, force: true }));
|
|
320
370
|
const path = join(cwd, "target.txt");
|
|
321
371
|
let commandRuns = 0;
|
|
322
|
-
const fusion = createActionFusionExecutor(
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
372
|
+
const fusion = createActionFusionExecutor(() => {});
|
|
373
|
+
const ctx = {
|
|
374
|
+
...(await createToolContext(cwd, t)),
|
|
375
|
+
executeTool: async () => {
|
|
376
|
+
commandRuns++;
|
|
377
|
+
return commandCompletion({ content: [{ type: "text", text: "ran" }], details: undefined });
|
|
378
|
+
},
|
|
379
|
+
};
|
|
326
380
|
const tool = makeEditOverride(cwd, DEFAULT_CONFIG, fusion);
|
|
327
|
-
const anchors = createAnchorFormatter(DEFAULT_CONFIG.hashLen);
|
|
328
381
|
for (const scenario of actionCases) {
|
|
329
|
-
|
|
330
|
-
await
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
: {}),
|
|
351
|
-
};
|
|
352
|
-
const args = { path, edits: [edit], then_run: null };
|
|
353
|
-
const snapshot = structuredClone(args);
|
|
354
|
-
const result = await callTool(tool, args);
|
|
355
|
-
assert.equal(result.isError, undefined);
|
|
356
|
-
assert.equal(reportOf(result, "edit")?.publication, "PUBLISHED");
|
|
357
|
-
assert.equal(reportOf(result, "edit")?.then_run, undefined);
|
|
358
|
-
assert.deepEqual(await readFile(path), Buffer.from(scenario.expected));
|
|
359
|
-
assert.deepEqual(args, snapshot);
|
|
360
|
-
},
|
|
361
|
-
);
|
|
362
|
-
}
|
|
382
|
+
await t.test(`${scenario.action} → accepts null optional fields`, async () => {
|
|
383
|
+
await writeFile(path, "alpha\nbeta\ngamma\n");
|
|
384
|
+
const edit = {
|
|
385
|
+
action: scenario.action,
|
|
386
|
+
...Object.fromEntries(editFields.map((field) => [field, null])),
|
|
387
|
+
...Object.fromEntries(
|
|
388
|
+
scenario.required.map((field) => [
|
|
389
|
+
field,
|
|
390
|
+
field === "lines" ? ["text"] : token(1, "alpha"),
|
|
391
|
+
]),
|
|
392
|
+
),
|
|
393
|
+
};
|
|
394
|
+
const args = { path, steps: [edit], then_run: null };
|
|
395
|
+
const snapshot = structuredClone(args);
|
|
396
|
+
const result = await callTool(tool, args, { ctx });
|
|
397
|
+
assert.equal(result.isError, undefined);
|
|
398
|
+
assert.equal(reportOf(result, "edit")?.publication, "PUBLISHED");
|
|
399
|
+
assert.equal(reportOf(result, "edit")?.then_run, undefined);
|
|
400
|
+
assert.deepEqual(await readFile(path), Buffer.from(scenario.expected));
|
|
401
|
+
assert.deepEqual(args, snapshot);
|
|
402
|
+
});
|
|
363
403
|
}
|
|
364
404
|
assert.equal(commandRuns, 0);
|
|
365
405
|
});
|
|
366
406
|
|
|
407
|
+
test("null optional fields → grep applies defaults without filename filters", async (t) => {
|
|
408
|
+
const cwd = await mkdtemp(join(tmpdir(), "hashline-grep-null-"));
|
|
409
|
+
t.after(() => rm(cwd, { recursive: true, force: true }));
|
|
410
|
+
const path = join(cwd, "target.txt");
|
|
411
|
+
const config = DEFAULT_CONFIG;
|
|
412
|
+
await writeFile(path, "x\n".repeat(config.grep.defaultLimit + 1));
|
|
413
|
+
const rgCalls: (readonly string[])[] = [];
|
|
414
|
+
const discover = async (
|
|
415
|
+
args: readonly string[],
|
|
416
|
+
onPath: (path: string) => boolean | Promise<boolean>,
|
|
417
|
+
) => {
|
|
418
|
+
rgCalls.push(args);
|
|
419
|
+
await onPath(path);
|
|
420
|
+
return { code: 0, stderr: "", stopped: false };
|
|
421
|
+
};
|
|
422
|
+
const tool = makeMockGrep(cwd, config, {
|
|
423
|
+
runRgPaths: (args, _signal, onPath) => discover(args, onPath),
|
|
424
|
+
runRgLines: (args, _signal, _maxLines, onFile) =>
|
|
425
|
+
discover(args, (file) => onFile(file, undefined)),
|
|
426
|
+
});
|
|
427
|
+
const result = await callTool(tool, {
|
|
428
|
+
pattern: "x",
|
|
429
|
+
literal: true,
|
|
430
|
+
glob: null,
|
|
431
|
+
limit: null,
|
|
432
|
+
context: null,
|
|
433
|
+
});
|
|
434
|
+
assert.equal(result.isError, undefined);
|
|
435
|
+
assert.equal(grepCounts(result).matches, config.grep.defaultLimit);
|
|
436
|
+
assert.equal(reportOf(result, "grep")?.matchLimit, config.grep.defaultLimit);
|
|
437
|
+
assert.ok(rgCalls.length > 0);
|
|
438
|
+
for (const args of rgCalls) {
|
|
439
|
+
const globs = args.flatMap((arg, index) => (arg === "--glob" ? [args[index + 1]] : []));
|
|
440
|
+
// The built-in .git exclusion remains; null adds no filename filter.
|
|
441
|
+
assert.deepEqual(globs, ["!.git"]);
|
|
442
|
+
}
|
|
443
|
+
});
|
|
444
|
+
|
|
367
445
|
test("edit batch has 64 or 65 operations → publishes the maximum or rejects the oversized batch unchanged", async (t) => {
|
|
368
446
|
const cwd = await mkdtemp(join(tmpdir(), "hashline-edit-limit-"));
|
|
369
447
|
t.after(() => rm(cwd, { recursive: true, force: true }));
|
|
370
448
|
const path = join(cwd, "target.txt");
|
|
371
449
|
const tool = makeEditOverride(cwd, DEFAULT_CONFIG);
|
|
372
|
-
const maxEdits: unknown = Reflect.get(tool.parameters.properties.
|
|
450
|
+
const maxEdits: unknown = Reflect.get(tool.parameters.properties.steps, "maxItems");
|
|
373
451
|
assert.ok(typeof maxEdits === "number");
|
|
374
452
|
assert.equal(maxEdits, 64);
|
|
375
453
|
const lines = Array.from({ length: maxEdits + 2 }, (_, i) => `line ${i + 1}`);
|
|
376
454
|
const original = Buffer.from(`${lines.join("\n")}\n`);
|
|
377
|
-
const anchors = createAnchorFormatter(DEFAULT_CONFIG.hashLen);
|
|
378
455
|
for (const count of [maxEdits, maxEdits + 1]) {
|
|
379
456
|
await t.test(
|
|
380
|
-
`${count}
|
|
457
|
+
`${count} steps → ${count === maxEdits ? "publishes distinct deletions" : `rejects steps above max${maxEdits}`}`,
|
|
381
458
|
async () => {
|
|
382
459
|
await writeFile(path, original);
|
|
383
460
|
const args = {
|
|
384
461
|
path,
|
|
385
|
-
|
|
462
|
+
steps: lines
|
|
386
463
|
.slice(0, count)
|
|
387
|
-
.map((line, i) => ({ action: "delete", anchor:
|
|
464
|
+
.map((line, i) => ({ action: "delete", anchor: token(i + 1, line) })),
|
|
388
465
|
};
|
|
389
466
|
const snapshot = structuredClone(args);
|
|
390
467
|
if (count === maxEdits) {
|
|
@@ -396,7 +473,7 @@ test("edit batch has 64 or 65 operations → publishes the maximum or rejects th
|
|
|
396
473
|
await assert.rejects(callTool(tool, args), (error: unknown) => {
|
|
397
474
|
const report = argumentError(error);
|
|
398
475
|
assert.equal(report.issues.length, 1);
|
|
399
|
-
assert.equal(report.issues[0].field, "
|
|
476
|
+
assert.equal(report.issues[0].field, "steps");
|
|
400
477
|
assert.match(
|
|
401
478
|
report.issues[0].reason,
|
|
402
479
|
new RegExp(`must not have more than ${maxEdits} items`),
|
|
@@ -416,12 +493,12 @@ test("edit item is not an object → report the item type once", async () => {
|
|
|
416
493
|
await assert.rejects(
|
|
417
494
|
callTool(makeEditOverride(process.cwd(), DEFAULT_CONFIG), {
|
|
418
495
|
path: "unused.txt",
|
|
419
|
-
|
|
496
|
+
steps: [item],
|
|
420
497
|
}),
|
|
421
498
|
(error: unknown) => {
|
|
422
499
|
const result = argumentError(error);
|
|
423
500
|
assert.equal(result.issues.length, 1);
|
|
424
|
-
return rejectsArgument("
|
|
501
|
+
return rejectsArgument("steps[0]", /object/)(error);
|
|
425
502
|
},
|
|
426
503
|
);
|
|
427
504
|
}
|
|
@@ -442,17 +519,6 @@ test("grep union value has one applicable type → report only its constraint",
|
|
|
442
519
|
}
|
|
443
520
|
});
|
|
444
521
|
|
|
445
|
-
test("literal enum is invalid → report the permitted values once", async () => {
|
|
446
|
-
await assert.rejects(
|
|
447
|
-
callTool(makeWriteOverride(process.cwd()), { path: "unused.txt", content: "ok", mode: "bad" }),
|
|
448
|
-
(error: unknown) => {
|
|
449
|
-
const result = argumentError(error);
|
|
450
|
-
assert.equal(result.issues.length, 1);
|
|
451
|
-
return rejectsArgument("mode", /create.*overwrite/)(error);
|
|
452
|
-
},
|
|
453
|
-
);
|
|
454
|
-
});
|
|
455
|
-
|
|
456
522
|
test("unknown field names contain punctuation, numeric keys or newlines → quote property keys without treating them as array indices", async () => {
|
|
457
523
|
for (const key of ["odd.\nfield/~", "0", "01", "a/b", "a~b", 'quote"field']) {
|
|
458
524
|
await assert.rejects(
|
|
@@ -474,12 +540,12 @@ test("one field exhausts native diagnostics → label the limit and retain anoth
|
|
|
474
540
|
await assert.rejects(
|
|
475
541
|
callTool(makeEditOverride(process.cwd(), DEFAULT_CONFIG), {
|
|
476
542
|
path: "",
|
|
477
|
-
|
|
543
|
+
steps: Array.from({ length: 30 }, () => ({ action: "unknown" })),
|
|
478
544
|
}),
|
|
479
545
|
(error: unknown) => {
|
|
480
546
|
const result = argumentError(error);
|
|
481
547
|
assert.ok(result.issues.some((issue) => issue.field === "path"));
|
|
482
|
-
assert.ok(result.issues.some((issue) => issue.field === "
|
|
548
|
+
assert.ok(result.issues.some((issue) => issue.field === "steps[0].action"));
|
|
483
549
|
assert.equal(result.schemaLimited, true);
|
|
484
550
|
return true;
|
|
485
551
|
},
|
|
@@ -490,12 +556,12 @@ test("one field violates independent constraints → combine its reasons without
|
|
|
490
556
|
await assert.rejects(
|
|
491
557
|
callTool(makeEditOverride(process.cwd(), DEFAULT_CONFIG), {
|
|
492
558
|
path: "unused.txt",
|
|
493
|
-
|
|
559
|
+
steps: [{ action: "delete", anchor: `${Number.MAX_SAFE_INTEGER + 1}#A` }],
|
|
494
560
|
}),
|
|
495
561
|
(error: unknown) => {
|
|
496
562
|
const result = argumentError(error);
|
|
497
563
|
assert.equal(result.issues.length, 1);
|
|
498
|
-
return rejectsArgument("
|
|
564
|
+
return rejectsArgument("steps[0].anchor", /safe integer[\s\S]*hash characters/)(error);
|
|
499
565
|
},
|
|
500
566
|
);
|
|
501
567
|
await assert.rejects(
|
|
@@ -519,12 +585,11 @@ test("edit fields contain semantic errors → checks only fields accepted by the
|
|
|
519
585
|
const valid = {
|
|
520
586
|
action: scenario.action,
|
|
521
587
|
...Object.fromEntries(scenario.required.map((field) => [field, fieldValues[field]])),
|
|
522
|
-
...(scenario.transfer ? { before: fieldValues.before } : {}),
|
|
523
588
|
};
|
|
524
589
|
for (const field of editFields) {
|
|
525
590
|
const accepted = scenario.required.includes(field) || scenario.optional.includes(field);
|
|
526
591
|
const values: readonly (readonly [unknown, RegExp])[] =
|
|
527
|
-
field === "
|
|
592
|
+
field === "lines"
|
|
528
593
|
? [
|
|
529
594
|
[[], /empty/],
|
|
530
595
|
[["bad\0"], /NUL/],
|
|
@@ -538,27 +603,20 @@ test("edit fields contain semantic errors → checks only fields accepted by the
|
|
|
538
603
|
await t.test(
|
|
539
604
|
`${scenario.action}/${field}/${reason.source} → ${accepted ? "reports value error" : "reports only forbidden field"}`,
|
|
540
605
|
async () => {
|
|
541
|
-
const edit = {
|
|
542
|
-
...Object.fromEntries(
|
|
543
|
-
Object.entries(valid).filter(
|
|
544
|
-
([key]) => !(scenario.transfer && field === "after" && key === "before"),
|
|
545
|
-
),
|
|
546
|
-
),
|
|
547
|
-
[field]: value,
|
|
548
|
-
};
|
|
606
|
+
const edit = { ...valid, [field]: value };
|
|
549
607
|
await assert.rejects(
|
|
550
|
-
callTool(tool, { path: "unused.txt",
|
|
608
|
+
callTool(tool, { path: "unused.txt", steps: [edit] }),
|
|
551
609
|
(error: unknown) => {
|
|
552
610
|
const report = argumentError(error);
|
|
553
611
|
assert.equal(report.issues.length, 1, JSON.stringify(report));
|
|
554
612
|
const issue = report.issues[0];
|
|
555
613
|
assert.equal(
|
|
556
614
|
issue.field,
|
|
557
|
-
`
|
|
615
|
+
`steps[0].${field}${accepted && field === "lines" && Array.isArray(value) && value.length ? "[0]" : ""}`,
|
|
558
616
|
);
|
|
559
617
|
if (accepted) {
|
|
560
618
|
assert.match(issue.reason, reason);
|
|
561
|
-
if (field === "
|
|
619
|
+
if (field === "lines" && Array.isArray(value) && value.length === 0) {
|
|
562
620
|
assert.match(
|
|
563
621
|
issue.fix ?? "",
|
|
564
622
|
scenario.action === "replace"
|
|
@@ -580,87 +638,6 @@ test("edit fields contain semantic errors → checks only fields accepted by the
|
|
|
580
638
|
}
|
|
581
639
|
});
|
|
582
640
|
|
|
583
|
-
test("copy/move destination count and field errors coexist → retains object and field diagnostics", async (t) => {
|
|
584
|
-
const cwd = await mkdtemp(join(tmpdir(), "hashline-object-diagnostics-"));
|
|
585
|
-
t.after(() => rm(cwd, { recursive: true, force: true }));
|
|
586
|
-
const tool = makeEditOverride(cwd, DEFAULT_CONFIG);
|
|
587
|
-
for (const scenario of [
|
|
588
|
-
{
|
|
589
|
-
edit: { action: "copy", anchor: "abc" },
|
|
590
|
-
field: "anchor",
|
|
591
|
-
reason: /must match pattern/,
|
|
592
|
-
fix: /LINE#HASH/,
|
|
593
|
-
},
|
|
594
|
-
{
|
|
595
|
-
edit: { action: "copy", anchor: `1#${anchorHash}`, extra: 1 },
|
|
596
|
-
field: "extra",
|
|
597
|
-
reason: /^is not allowed$/,
|
|
598
|
-
fix: /^Accepted fields:/,
|
|
599
|
-
},
|
|
600
|
-
{
|
|
601
|
-
edit: {
|
|
602
|
-
action: "move",
|
|
603
|
-
anchor: `1#${anchorHash}`,
|
|
604
|
-
end: "x",
|
|
605
|
-
before: `3#${anchorHash}`,
|
|
606
|
-
after: `4#${anchorHash}`,
|
|
607
|
-
},
|
|
608
|
-
field: "end",
|
|
609
|
-
reason: /must match pattern/,
|
|
610
|
-
fix: /LINE#HASH/,
|
|
611
|
-
},
|
|
612
|
-
]) {
|
|
613
|
-
await t.test(`${scenario.edit.action}/${scenario.field} → retains both issues`, async () => {
|
|
614
|
-
await assert.rejects(
|
|
615
|
-
callTool(tool, { path: "missing.txt", edits: [scenario.edit] }),
|
|
616
|
-
(error: unknown) => {
|
|
617
|
-
const { issues } = argumentError(error);
|
|
618
|
-
assert.deepEqual(
|
|
619
|
-
issues.map((issue) => issue.field).sort(),
|
|
620
|
-
["edits[0]", `edits[0].${scenario.field}`].sort(),
|
|
621
|
-
);
|
|
622
|
-
const item = issues.find((issue) => issue.field === "edits[0]");
|
|
623
|
-
assert.equal(item?.reason, "expected exactly one of: before, after");
|
|
624
|
-
assert.equal(item?.fix, undefined);
|
|
625
|
-
const field = issues.find((issue) => issue.field === `edits[0].${scenario.field}`);
|
|
626
|
-
assert.ok(field);
|
|
627
|
-
assert.match(field.reason, scenario.reason);
|
|
628
|
-
assert.match(field.fix ?? "", scenario.fix);
|
|
629
|
-
return true;
|
|
630
|
-
},
|
|
631
|
-
);
|
|
632
|
-
});
|
|
633
|
-
}
|
|
634
|
-
});
|
|
635
|
-
|
|
636
|
-
test("copy/move has zero or two destinations → reports one exactly-one issue without a fix", async (t) => {
|
|
637
|
-
const tool = makeEditOverride(process.cwd(), DEFAULT_CONFIG);
|
|
638
|
-
for (const action of ["copy", "move"]) {
|
|
639
|
-
for (const destinations of [{}, { before: fieldValues.before, after: fieldValues.after }]) {
|
|
640
|
-
await t.test(
|
|
641
|
-
`${action}/${Object.keys(destinations).length} destinations → reports the item`,
|
|
642
|
-
async () => {
|
|
643
|
-
await assert.rejects(
|
|
644
|
-
callTool(tool, {
|
|
645
|
-
path: "unused.txt",
|
|
646
|
-
edits: [{ action, anchor: fieldValues.anchor, ...destinations }],
|
|
647
|
-
}),
|
|
648
|
-
(error: unknown) => {
|
|
649
|
-
assert.deepEqual(argumentError(error).issues, [
|
|
650
|
-
{
|
|
651
|
-
field: "edits[0]",
|
|
652
|
-
reason: "expected exactly one of: before, after",
|
|
653
|
-
},
|
|
654
|
-
]);
|
|
655
|
-
return true;
|
|
656
|
-
},
|
|
657
|
-
);
|
|
658
|
-
},
|
|
659
|
-
);
|
|
660
|
-
}
|
|
661
|
-
}
|
|
662
|
-
});
|
|
663
|
-
|
|
664
641
|
/** Tool schema properties, so expected fix text comes from the schema instead of the test. */
|
|
665
642
|
interface SchemaNode {
|
|
666
643
|
readonly description?: string;
|
|
@@ -690,7 +667,7 @@ test("missing required parameter → fix states the parameter's role from its sc
|
|
|
690
667
|
discoveries++;
|
|
691
668
|
return { code: 1, stderr: "", stopped: false };
|
|
692
669
|
};
|
|
693
|
-
const grep =
|
|
670
|
+
const grep = makeMockGrep(process.cwd(), DEFAULT_CONFIG, {
|
|
694
671
|
runRgPaths: discover,
|
|
695
672
|
runRgLines: discover,
|
|
696
673
|
});
|
|
@@ -733,7 +710,7 @@ test("missing required parameter → fix states the parameter's role from its sc
|
|
|
733
710
|
test("two operations miss the same required field → the shared fix appears once", async () => {
|
|
734
711
|
const tool = makeEditOverride(process.cwd(), DEFAULT_CONFIG);
|
|
735
712
|
await assert.rejects(
|
|
736
|
-
callTool(tool, { path: "unused.txt",
|
|
713
|
+
callTool(tool, { path: "unused.txt", steps: [{ action: "replace" }, { action: "replace" }] }),
|
|
737
714
|
(error: unknown) => {
|
|
738
715
|
const result = argumentError(error);
|
|
739
716
|
const anchors = result.issues.filter((issue) => issue.field.endsWith(".anchor"));
|
|
@@ -741,7 +718,7 @@ test("two operations miss the same required field → the shared fix appears onc
|
|
|
741
718
|
assert.equal(anchors.filter((issue) => issue.fix).length, 1);
|
|
742
719
|
const fixes = result.issues.flatMap((issue) => (issue.fix ? [issue.fix] : []));
|
|
743
720
|
assert.equal(new Set(fixes).size, fixes.length, JSON.stringify(fixes));
|
|
744
|
-
const description = schemaProperties(tool).
|
|
721
|
+
const description = schemaProperties(tool).steps?.items?.properties?.anchor?.description;
|
|
745
722
|
assert.ok(description);
|
|
746
723
|
assert.ok(fixes.includes(description));
|
|
747
724
|
return true;
|
|
@@ -749,23 +726,70 @@ test("two operations miss the same required field → the shared fix appears onc
|
|
|
749
726
|
);
|
|
750
727
|
});
|
|
751
728
|
|
|
752
|
-
test("
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
729
|
+
test("out-of-choice value → reason lists values and fix explains each choice", async (t) => {
|
|
730
|
+
const cwd = process.cwd();
|
|
731
|
+
const cases = [
|
|
732
|
+
{
|
|
733
|
+
tool: makeEditOverride(cwd, DEFAULT_CONFIG),
|
|
734
|
+
field: "steps[0].action",
|
|
735
|
+
values: ["unknown", null, 42, {}],
|
|
736
|
+
args: (action: unknown) => ({
|
|
737
|
+
path: "unused.txt",
|
|
738
|
+
steps: [{ action, anchor: `1#${anchorHash}`, lines: ["valid"] }],
|
|
739
|
+
}),
|
|
740
|
+
choices: [
|
|
741
|
+
["replace", "Replace the cited line(s)"],
|
|
742
|
+
["delete", "Delete the cited line(s)"],
|
|
743
|
+
["insert_after", "Insert lines after the anchor line"],
|
|
744
|
+
["insert_before", "Insert lines before the anchor line"],
|
|
745
|
+
["append", "Add lines at the end"],
|
|
746
|
+
["prepend", "Add lines at the start"],
|
|
747
|
+
],
|
|
767
748
|
},
|
|
768
|
-
|
|
749
|
+
{
|
|
750
|
+
tool: makeWriteOverride(cwd),
|
|
751
|
+
field: "mode",
|
|
752
|
+
values: ["unknown"],
|
|
753
|
+
args: (mode: unknown) => ({ path: "unused.txt", content: "ok", mode }),
|
|
754
|
+
choices: [
|
|
755
|
+
["create", "Fail if the target already exists."],
|
|
756
|
+
["overwrite", "Fail if the target does not exist."],
|
|
757
|
+
],
|
|
758
|
+
},
|
|
759
|
+
{
|
|
760
|
+
tool: makeGrepOverride(cwd, DEFAULT_CONFIG),
|
|
761
|
+
field: "outputMode",
|
|
762
|
+
values: ["unknown"],
|
|
763
|
+
args: (outputMode: unknown) => ({ pattern: "text", literal: true, outputMode }),
|
|
764
|
+
choices: [
|
|
765
|
+
["content", "Matches and context", "anchors", "plain previews"],
|
|
766
|
+
["files", "Paths of the files", "only paths"],
|
|
767
|
+
["count", "Matching lines per file", "total", "only counts"],
|
|
768
|
+
],
|
|
769
|
+
},
|
|
770
|
+
] as const;
|
|
771
|
+
for (const scenario of cases) {
|
|
772
|
+
await t.test(`${scenario.tool.name}/${scenario.field}`, async () => {
|
|
773
|
+
for (const value of scenario.values) {
|
|
774
|
+
await assert.rejects(callTool(scenario.tool, scenario.args(value)), (error: unknown) => {
|
|
775
|
+
const { issues } = argumentError(error);
|
|
776
|
+
assert.equal(issues.length, 1, JSON.stringify(issues));
|
|
777
|
+
const issue = issues[0];
|
|
778
|
+
assert.equal(issue.field, scenario.field);
|
|
779
|
+
assert.equal(
|
|
780
|
+
issue.reason,
|
|
781
|
+
`expected one of: ${scenario.choices.map(([value]) => JSON.stringify(value)).join(", ")}`,
|
|
782
|
+
);
|
|
783
|
+
assert.ok(issue.fix, JSON.stringify(issues));
|
|
784
|
+
inOrder(
|
|
785
|
+
issue.fix,
|
|
786
|
+
scenario.choices.flatMap(([value, ...meaning]) => [JSON.stringify(value), ...meaning]),
|
|
787
|
+
);
|
|
788
|
+
return true;
|
|
789
|
+
});
|
|
790
|
+
}
|
|
791
|
+
});
|
|
792
|
+
}
|
|
769
793
|
});
|
|
770
794
|
|
|
771
795
|
const acceptedFields = (shape: SchemaNode): string =>
|
|
@@ -777,9 +801,9 @@ test("unknown field is supplied → fix lists only the flat object's accepted fi
|
|
|
777
801
|
for (const scenario of [
|
|
778
802
|
{
|
|
779
803
|
tool: edit,
|
|
780
|
-
args: { path: "unused.txt",
|
|
781
|
-
field: "
|
|
782
|
-
fix: "Accepted fields: action, anchor, end,
|
|
804
|
+
args: { path: "unused.txt", steps: [{ action: "append", lines: ["text"], op: "append" }] },
|
|
805
|
+
field: "steps[0].op",
|
|
806
|
+
fix: "Accepted fields: action, anchor, end, lines.",
|
|
783
807
|
},
|
|
784
808
|
{
|
|
785
809
|
tool: grep,
|
|
@@ -799,18 +823,18 @@ test("unknown field is supplied → fix lists only the flat object's accepted fi
|
|
|
799
823
|
|
|
800
824
|
test("value mismatches the field pattern → fix states the field description from the schema", async () => {
|
|
801
825
|
const edit = makeEditOverride(process.cwd(), DEFAULT_CONFIG);
|
|
802
|
-
const fields = schemaProperties(edit).
|
|
826
|
+
const fields = schemaProperties(edit).steps?.items?.properties;
|
|
803
827
|
const cases = [
|
|
804
828
|
{
|
|
805
|
-
name: "
|
|
806
|
-
args: { path: "unused.txt",
|
|
807
|
-
field: "
|
|
808
|
-
fix: fields?.
|
|
829
|
+
name: "line element with a newline",
|
|
830
|
+
args: { path: "unused.txt", steps: [{ action: "append", lines: ["a\nb"] }] },
|
|
831
|
+
field: "steps[0].lines[0]",
|
|
832
|
+
fix: fields?.lines?.description,
|
|
809
833
|
},
|
|
810
834
|
{
|
|
811
835
|
name: "anchor without the line-hash shape",
|
|
812
|
-
args: { path: "unused.txt",
|
|
813
|
-
field: "
|
|
836
|
+
args: { path: "unused.txt", steps: [{ action: "replace", anchor: "abc", lines: ["text"] }] },
|
|
837
|
+
field: "steps[0].anchor",
|
|
814
838
|
fix: fields?.anchor?.description,
|
|
815
839
|
},
|
|
816
840
|
] as const;
|
|
@@ -831,7 +855,7 @@ test("two unknown fields in one object → the shared fix appears once", async (
|
|
|
831
855
|
await assert.rejects(
|
|
832
856
|
callTool(edit, {
|
|
833
857
|
path: "unused.txt",
|
|
834
|
-
|
|
858
|
+
steps: [{ action: "append", lines: ["text"], zzz: 1, yyy: 2 }],
|
|
835
859
|
}),
|
|
836
860
|
(error: unknown) => {
|
|
837
861
|
const result = argumentError(error);
|
|
@@ -857,3 +881,30 @@ test("value has the wrong type → the reason carries no fix", async () => {
|
|
|
857
881
|
},
|
|
858
882
|
);
|
|
859
883
|
});
|
|
884
|
+
|
|
885
|
+
function token(line: number, content: string): string {
|
|
886
|
+
const hashLen = DEFAULT_CONFIG.hashLen;
|
|
887
|
+
return createAnchorFormatter(hashLen).reference(line, computeLineHash(line, content, hashLen));
|
|
888
|
+
}
|
|
889
|
+
|
|
890
|
+
test("20 unknown keys including a slash → bounded issues retain one accepted-fields fix", async () => {
|
|
891
|
+
const tool = makeReadOverride(process.cwd(), DEFAULT_CONFIG);
|
|
892
|
+
const keys = Array.from({ length: 20 }, (_, index) =>
|
|
893
|
+
index === 0 ? "unknown/key" : `unknown${index}`,
|
|
894
|
+
);
|
|
895
|
+
await assert.rejects(
|
|
896
|
+
callTool(tool, { path: "unused.txt", ...Object.fromEntries(keys.map((key) => [key, true])) }),
|
|
897
|
+
(error: unknown) => {
|
|
898
|
+
const result = argumentError(error);
|
|
899
|
+
assert.equal(result.schemaLimited, true);
|
|
900
|
+
assert.ok(result.issues.length >= 8);
|
|
901
|
+
assert.ok(result.issues.some((issue) => issue.field.includes("unknown/key")));
|
|
902
|
+
for (const issue of result.issues) {
|
|
903
|
+
assert.equal(issue.reason, "is not allowed");
|
|
904
|
+
}
|
|
905
|
+
assert.equal(result.issues[0].fix, acceptedFields({ properties: schemaProperties(tool) }));
|
|
906
|
+
assert.ok(result.issues.slice(1).every((issue) => issue.fix === undefined));
|
|
907
|
+
return true;
|
|
908
|
+
},
|
|
909
|
+
);
|
|
910
|
+
});
|