@criogaid/pi-hashline-edit 0.3.1 → 0.5.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 +166 -61
- package/package.json +8 -3
- package/src/core/apply.test.ts +459 -313
- package/src/core/apply.ts +251 -215
- package/src/core/errors.ts +9 -0
- package/src/core/hash.test.ts +61 -23
- package/src/core/hash.ts +49 -20
- package/src/core/lines.test.ts +46 -27
- package/src/core/lines.ts +36 -28
- package/src/core/ranges.test.ts +73 -19
- package/src/core/ranges.ts +15 -15
- package/src/core/replace.ts +109 -0
- package/src/core/text.test.ts +3 -3
- package/src/core/text.ts +26 -9
- package/src/core/types.ts +56 -41
- package/src/index.ts +23 -19
- package/src/integration/config-registration.test.ts +206 -0
- package/src/integration/grep-rg.test.ts +879 -251
- package/src/integration/large-text.test.ts +63 -0
- package/src/integration/replace-package.test.ts +41 -0
- package/src/pi/action-fusion.test.ts +612 -207
- package/src/pi/action-fusion.ts +442 -254
- package/src/pi/anchor-format.test.ts +53 -21
- package/src/pi/anchor-format.ts +48 -20
- package/src/pi/budgets.ts +25 -0
- package/src/pi/config.test.ts +178 -35
- package/src/pi/config.ts +96 -37
- package/src/pi/directory-sync.test.ts +55 -0
- package/src/pi/e2e-scenarios.test.ts +360 -0
- package/src/pi/edit-tool.ts +307 -279
- package/src/pi/error-text.ts +26 -0
- package/src/pi/execute.test.ts +1718 -713
- package/src/pi/failure-context.test.ts +110 -83
- package/src/pi/failure-context.ts +232 -73
- package/src/pi/failure-path.test.ts +180 -66
- package/src/pi/file-commit.test.ts +446 -233
- package/src/pi/file-commit.ts +348 -203
- package/src/pi/fusion-card.test.ts +118 -83
- package/src/pi/fusion-card.ts +203 -87
- package/src/pi/grep-output.ts +225 -0
- package/src/pi/grep-render.ts +59 -0
- package/src/pi/grep-scope.ts +139 -0
- package/src/pi/grep-search.ts +231 -0
- package/src/pi/grep-tool.ts +215 -774
- package/src/pi/grep.test.ts +825 -376
- package/src/pi/mutation-outcome.testing.ts +22 -0
- package/src/pi/mutation-result.test.ts +86 -0
- package/src/pi/mutation-result.ts +118 -63
- package/src/pi/mutation-runner.ts +151 -0
- package/src/pi/path.ts +20 -12
- package/src/pi/pi.test.ts +100 -52
- package/src/pi/read-tool.ts +245 -134
- package/src/pi/registration.test.ts +271 -114
- package/src/pi/render.test.ts +227 -84
- package/src/pi/render.ts +86 -116
- package/src/pi/replace-regex.ts +54 -0
- package/src/pi/replace-tool.ts +247 -241
- package/src/pi/replace-worker.mjs +4 -0
- package/src/pi/replace-worker.ts +10 -0
- package/src/pi/replace.test.ts +778 -390
- package/src/pi/result-safety.test.ts +821 -344
- package/src/pi/rg-line-ranges.test.ts +57 -31
- package/src/pi/rg-line-ranges.ts +45 -82
- package/src/pi/rg-process.test.ts +113 -0
- package/src/pi/{rg-line-filter.ts → rg-process.ts} +72 -125
- package/src/pi/rg-text-view.ts +102 -48
- package/src/pi/schema.ts +19 -0
- package/src/pi/text-stream.test.ts +118 -0
- package/src/pi/text-stream.ts +136 -0
- package/src/pi/tool-call.testing.ts +37 -0
- package/src/pi/write-tool.ts +114 -52
- package/src/pi/write.test.ts +250 -97
- package/src/core/index.ts +0 -14
- package/src/pi/rg-line-filter.test.ts +0 -85
- package/src/pi/state.ts +0 -26
package/src/core/apply.test.ts
CHANGED
|
@@ -7,437 +7,583 @@ import type { Anchor, Edit } from "./types.ts";
|
|
|
7
7
|
|
|
8
8
|
/** Live anchor: hash the current content at the given line (1-based). */
|
|
9
9
|
function at(text: string, line: number): Anchor {
|
|
10
|
-
|
|
10
|
+
return { line, hash: computeLineHash(line, splitLines(text)[line - 1], 4) };
|
|
11
11
|
}
|
|
12
12
|
|
|
13
13
|
// --- happy paths ---
|
|
14
14
|
|
|
15
15
|
test("replace single line", () => {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
16
|
+
const text = "a\nb\nc\n";
|
|
17
|
+
const r = applyEdits(text, [{ op: "replace", start: at(text, 2), body: ["B"] }], 4, 15);
|
|
18
|
+
assert.equal(r.ok, true);
|
|
19
|
+
if (r.ok) assert.equal(r.text, "a\nB\nc\n");
|
|
20
20
|
});
|
|
21
21
|
|
|
22
22
|
test("editing another line preserves standalone carriage returns at EOF", () => {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
23
|
+
for (const ending of ["\n", "\r\n"]) {
|
|
24
|
+
const text = `first${ending}last\r`;
|
|
25
|
+
const r = applyEdits(text, [{ op: "replace", start: at(text, 1), body: ["changed"] }], 4, 15);
|
|
26
|
+
assert.equal(r.ok, true);
|
|
27
|
+
if (r.ok) assert.equal(r.text, `changed${ending}last\r`);
|
|
28
|
+
}
|
|
29
29
|
});
|
|
30
30
|
|
|
31
31
|
test("replace range", () => {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
32
|
+
const text = "a\nb\nc\nd\ne\n";
|
|
33
|
+
const r = applyEdits(
|
|
34
|
+
text,
|
|
35
|
+
[{ op: "replace", start: at(text, 2), end: at(text, 4), body: ["X", "Y"] }],
|
|
36
|
+
4,
|
|
37
|
+
15,
|
|
38
|
+
);
|
|
39
|
+
assert.equal(r.ok, true);
|
|
40
|
+
if (r.ok) assert.equal(r.text, "a\nX\nY\ne\n");
|
|
36
41
|
});
|
|
37
42
|
|
|
38
43
|
test("delete single line / range", () => {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
44
|
+
const text = "a\nb\nc\nd\n";
|
|
45
|
+
const r = applyEdits(text, [{ op: "delete", start: at(text, 2), end: at(text, 3) }], 4, 15);
|
|
46
|
+
assert.equal(r.ok, true);
|
|
47
|
+
if (r.ok) assert.equal(r.text, "a\nd\n");
|
|
43
48
|
});
|
|
44
49
|
|
|
45
50
|
test("insert_after", () => {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
51
|
+
const text = "a\nb\n";
|
|
52
|
+
const r = applyEdits(text, [{ op: "insert_after", anchor: at(text, 1), body: ["x"] }], 4, 15);
|
|
53
|
+
assert.equal(r.ok, true);
|
|
54
|
+
if (r.ok) assert.equal(r.text, "a\nx\nb\n");
|
|
50
55
|
});
|
|
51
56
|
|
|
52
57
|
test("insert_before", () => {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
58
|
+
const text = "a\nb\n";
|
|
59
|
+
const r = applyEdits(text, [{ op: "insert_before", anchor: at(text, 2), body: ["x"] }], 4, 15);
|
|
60
|
+
assert.equal(r.ok, true);
|
|
61
|
+
if (r.ok) assert.equal(r.text, "a\nx\nb\n");
|
|
57
62
|
});
|
|
58
63
|
|
|
59
64
|
test("append / prepend", () => {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
65
|
+
const text = "a\nb\n";
|
|
66
|
+
const r = applyEdits(
|
|
67
|
+
text,
|
|
68
|
+
[
|
|
69
|
+
{ op: "prepend", body: ["head"] },
|
|
70
|
+
{ op: "append", body: ["tail"] },
|
|
71
|
+
],
|
|
72
|
+
4,
|
|
73
|
+
15,
|
|
74
|
+
);
|
|
75
|
+
assert.equal(r.ok, true);
|
|
76
|
+
if (r.ok) assert.equal(r.text, "head\na\nb\ntail\n");
|
|
67
77
|
});
|
|
68
78
|
|
|
69
79
|
test("multiple out-of-order ops → applied at the right positions", () => {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
80
|
+
const text = "a\nb\nc\n";
|
|
81
|
+
const r = applyEdits(
|
|
82
|
+
text,
|
|
83
|
+
[
|
|
84
|
+
{ op: "insert_after", anchor: at(text, 3), body: ["z"] },
|
|
85
|
+
{ op: "replace", start: at(text, 1), body: ["A"] },
|
|
86
|
+
],
|
|
87
|
+
4,
|
|
88
|
+
15,
|
|
89
|
+
);
|
|
90
|
+
assert.equal(r.ok, true);
|
|
91
|
+
if (r.ok) assert.equal(r.text, "A\nb\nc\nz\n");
|
|
77
92
|
});
|
|
78
93
|
|
|
79
94
|
test("touchedLines covers exactly the lines this edit produced (new-file indices)", () => {
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
95
|
+
const text = "a\nb\nc\n";
|
|
96
|
+
const r = applyEdits(
|
|
97
|
+
text,
|
|
98
|
+
[
|
|
99
|
+
{ op: "insert_after", anchor: at(text, 3), body: ["z"] },
|
|
100
|
+
{ op: "replace", start: at(text, 1), body: ["A"] },
|
|
101
|
+
],
|
|
102
|
+
4,
|
|
103
|
+
15,
|
|
104
|
+
);
|
|
105
|
+
assert.equal(r.ok, true);
|
|
106
|
+
if (r.ok) {
|
|
107
|
+
// new file: [A, b, c, z]; this edit produced line index 0 (A) and 3 (z)
|
|
108
|
+
assert.deepEqual([...r.touchedLines], [0, 3]);
|
|
109
|
+
}
|
|
90
110
|
});
|
|
91
111
|
|
|
92
112
|
test("touchedLines for append", () => {
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
113
|
+
const text = "a\n";
|
|
114
|
+
const r = applyEdits(text, [{ op: "append", body: ["b", "c"] }], 4, 15);
|
|
115
|
+
assert.equal(r.ok, true);
|
|
116
|
+
if (r.ok) assert.deepEqual([...r.touchedLines], [1, 2]);
|
|
97
117
|
});
|
|
98
118
|
|
|
99
119
|
test("touchedLines for delete re-anchors the line that shifted into the gap", () => {
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
120
|
+
const text = "a\nb\nc\nd\n";
|
|
121
|
+
const r = applyEdits(text, [{ op: "delete", start: at(text, 2) }], 4, 15);
|
|
122
|
+
assert.equal(r.ok, true);
|
|
123
|
+
if (r.ok) {
|
|
124
|
+
// new file: [a, c, d]; deleting line 2 (b) shifts c into index 1
|
|
125
|
+
assert.deepEqual([...r.touchedLines], [1]);
|
|
126
|
+
}
|
|
107
127
|
});
|
|
108
128
|
|
|
109
129
|
// --- error paths ---
|
|
110
130
|
|
|
111
131
|
test("anchor hash mismatch rejected (line changed)", () => {
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
132
|
+
const text = "a\nb\n";
|
|
133
|
+
const r = applyEdits(
|
|
134
|
+
text,
|
|
135
|
+
[{ op: "replace", start: { line: 1, hash: "WRONG" }, body: ["x"] }],
|
|
136
|
+
4,
|
|
137
|
+
15,
|
|
138
|
+
);
|
|
139
|
+
assert.equal(r.ok, false);
|
|
140
|
+
if (!r.ok) assert.equal(r.failure.kind, "anchor");
|
|
116
141
|
});
|
|
117
142
|
|
|
118
143
|
test("line out of range rejected", () => {
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
144
|
+
const text = "a\n";
|
|
145
|
+
const r = applyEdits(
|
|
146
|
+
text,
|
|
147
|
+
[{ op: "replace", start: { line: 5, hash: computeLineHash(1, "a", 4) }, body: ["x"] }],
|
|
148
|
+
4,
|
|
149
|
+
15,
|
|
150
|
+
);
|
|
151
|
+
assert.equal(r.ok, false);
|
|
152
|
+
if (!r.ok) assert.equal(r.failure.kind, "anchor");
|
|
123
153
|
});
|
|
124
154
|
|
|
125
155
|
test("anchor mismatch when the cited line's content differs (live verification)", () => {
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
156
|
+
// hash for line 2's content, but cited as line 1 → must fail because line 1's live content differs
|
|
157
|
+
const text = "a\nb\n";
|
|
158
|
+
const r = applyEdits(
|
|
159
|
+
text,
|
|
160
|
+
[{ op: "replace", start: { line: 1, hash: computeLineHash(2, "b", 4) }, body: ["x"] }],
|
|
161
|
+
4,
|
|
162
|
+
15,
|
|
163
|
+
);
|
|
164
|
+
assert.equal(r.ok, false);
|
|
165
|
+
if (!r.ok) assert.equal(r.failure.kind, "anchor");
|
|
131
166
|
});
|
|
132
167
|
|
|
133
168
|
test("reverse-order range rejected", () => {
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
169
|
+
const text = "a\nb\nc\n";
|
|
170
|
+
const r = applyEdits(
|
|
171
|
+
text,
|
|
172
|
+
[{ op: "replace", start: at(text, 3), end: at(text, 1), body: ["x"] }],
|
|
173
|
+
4,
|
|
174
|
+
15,
|
|
175
|
+
);
|
|
176
|
+
assert.equal(r.ok, false);
|
|
177
|
+
if (!r.ok) assert.equal(r.failure.kind, "range");
|
|
138
178
|
});
|
|
139
179
|
|
|
140
180
|
test("overlapping edits rejected", () => {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
181
|
+
const text = "a\nb\nc\nd\n";
|
|
182
|
+
const r = applyEdits(
|
|
183
|
+
text,
|
|
184
|
+
[
|
|
185
|
+
{ op: "replace", start: at(text, 2), end: at(text, 3), body: ["x"] },
|
|
186
|
+
{ op: "replace", start: at(text, 3), body: ["y"] },
|
|
187
|
+
],
|
|
188
|
+
4,
|
|
189
|
+
15,
|
|
190
|
+
);
|
|
191
|
+
assert.equal(r.ok, false);
|
|
192
|
+
if (!r.ok) assert.equal(r.failure.kind, "range");
|
|
148
193
|
});
|
|
149
194
|
|
|
150
195
|
test("conflict at the same insertion point rejected", () => {
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
196
|
+
const text = "a\nb\n";
|
|
197
|
+
const r = applyEdits(
|
|
198
|
+
text,
|
|
199
|
+
[
|
|
200
|
+
{ op: "insert_after", anchor: at(text, 1), body: ["x"] },
|
|
201
|
+
{ op: "insert_after", anchor: at(text, 1), body: ["y"] },
|
|
202
|
+
],
|
|
203
|
+
4,
|
|
204
|
+
15,
|
|
205
|
+
);
|
|
206
|
+
assert.equal(r.ok, false);
|
|
207
|
+
if (!r.ok) assert.equal(r.failure.kind, "range");
|
|
158
208
|
});
|
|
159
209
|
|
|
160
210
|
test("byte-identical edits succeed without updated anchors after validation", () => {
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
211
|
+
for (const text of ["a\nb\n", "\uFEFFa\r\nb\n"]) {
|
|
212
|
+
const body = text.startsWith("\uFEFF") ? "\uFEFFa" : "a";
|
|
213
|
+
const r = applyEdits(text, [{ op: "replace", start: at(text, 1), body: [body] }], 4, 15);
|
|
214
|
+
assert.deepEqual(r, { ok: true, text, changed: false, touchedLines: [], contextLines: [] });
|
|
215
|
+
const invalid = applyEdits(
|
|
216
|
+
text,
|
|
217
|
+
[{ op: "replace", start: at("wrong\nb\n", 1), body: [body] }],
|
|
218
|
+
4,
|
|
219
|
+
15,
|
|
220
|
+
);
|
|
221
|
+
assert.ok(!invalid.ok && invalid.failure.kind === "anchor");
|
|
222
|
+
}
|
|
168
223
|
});
|
|
169
224
|
|
|
170
225
|
test("unrelated change elsewhere does NOT block the edit (no global stale check)", () => {
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
226
|
+
// read-time text and current text differ at line 3, but we only touch line 1 — must succeed
|
|
227
|
+
const readText = "a\nb\nc\n";
|
|
228
|
+
const currentText = "a\nb\nCHANGED\n";
|
|
229
|
+
const r = applyEdits(
|
|
230
|
+
currentText,
|
|
231
|
+
[{ op: "replace", start: at(readText, 1), body: ["A"] }],
|
|
232
|
+
4,
|
|
233
|
+
15,
|
|
234
|
+
);
|
|
235
|
+
assert.equal(r.ok, true);
|
|
236
|
+
if (r.ok) assert.equal(r.text, "A\nb\nCHANGED\n");
|
|
177
237
|
});
|
|
178
238
|
|
|
179
239
|
test("CRLF line endings preserved", () => {
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
240
|
+
const text = "a\r\nb\r\n";
|
|
241
|
+
const r = applyEdits(text, [{ op: "replace", start: at(text, 1), body: ["A"] }], 4, 15);
|
|
242
|
+
assert.equal(r.ok, true);
|
|
243
|
+
if (r.ok) assert.equal(r.text, "A\r\nb\r\n");
|
|
184
244
|
});
|
|
185
245
|
|
|
186
246
|
test("editing a mixed-ending file preserves untouched separators", () => {
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
247
|
+
const before = "first\r\nsecond\nthird\r\n";
|
|
248
|
+
const result = applyEdits(
|
|
249
|
+
before,
|
|
250
|
+
[{ op: "replace", start: at(before, 2), body: ["SECOND"] }],
|
|
251
|
+
4,
|
|
252
|
+
15,
|
|
253
|
+
);
|
|
254
|
+
assert.equal(result.ok, true);
|
|
255
|
+
if (result.ok) assert.equal(result.text, "first\r\nSECOND\nthird\r\n");
|
|
191
256
|
});
|
|
192
257
|
|
|
193
258
|
test("inserting and deleting in mixed-ending files preserves surviving separators", () => {
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
259
|
+
const before = "a\r\nb\nc\r\nlast";
|
|
260
|
+
const inserted = applyEdits(
|
|
261
|
+
before,
|
|
262
|
+
[{ op: "insert_after", anchor: at(before, 2), body: ["x"] }],
|
|
263
|
+
4,
|
|
264
|
+
15,
|
|
265
|
+
);
|
|
266
|
+
assert.equal(inserted.ok, true);
|
|
267
|
+
if (inserted.ok) assert.equal(inserted.text, "a\r\nb\nx\r\nc\r\nlast");
|
|
268
|
+
const deleted = applyEdits(before, [{ op: "delete", start: at(before, 2) }], 4, 15);
|
|
269
|
+
assert.equal(deleted.ok, true);
|
|
270
|
+
if (deleted.ok) assert.equal(deleted.text, "a\r\nc\r\nlast");
|
|
201
271
|
});
|
|
202
272
|
|
|
203
273
|
test("replacing one line with several keeps the block's trailing gap", () => {
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
274
|
+
const before = "a\r\nb\nc\r\nd";
|
|
275
|
+
const result = applyEdits(
|
|
276
|
+
before,
|
|
277
|
+
[{ op: "replace", start: at(before, 2), body: ["X", "Y", "Z"] }],
|
|
278
|
+
4,
|
|
279
|
+
15,
|
|
280
|
+
);
|
|
281
|
+
assert.equal(result.ok, true);
|
|
282
|
+
// b's gap (\n) moves to the last new line, so the boundary to c is unchanged;
|
|
283
|
+
// b has no internal gap; both new internal gaps use the file style, like replace("b", "X\nY\nZ").
|
|
284
|
+
if (result.ok) assert.equal(result.text, "a\r\nX\r\nY\r\nZ\nc\r\nd");
|
|
210
285
|
});
|
|
211
286
|
|
|
212
287
|
test("a file without a final newline keeps not having one", () => {
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
288
|
+
const text = "a\nb";
|
|
289
|
+
const r = applyEdits(text, [{ op: "replace", start: at(text, 2), body: ["B"] }], 4, 15);
|
|
290
|
+
assert.equal(r.ok, true);
|
|
291
|
+
if (r.ok) assert.equal(r.text, "a\nB");
|
|
217
292
|
});
|
|
218
293
|
|
|
219
294
|
test("CRLF without a final newline keeps not having one", () => {
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
295
|
+
const text = "a\r\nb";
|
|
296
|
+
const r = applyEdits(text, [{ op: "replace", start: at(text, 1), body: ["A"] }], 4, 15);
|
|
297
|
+
assert.equal(r.ok, true);
|
|
298
|
+
if (r.ok) assert.equal(r.text, "A\r\nb");
|
|
224
299
|
});
|
|
225
300
|
|
|
226
301
|
test("appending to a file without a final newline keeps it absent", () => {
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
302
|
+
const text = "a";
|
|
303
|
+
const r = applyEdits(text, [{ op: "append", body: ["b"] }], 4, 15);
|
|
304
|
+
assert.equal(r.ok, true);
|
|
305
|
+
if (r.ok) assert.equal(r.text, "a\nb");
|
|
231
306
|
});
|
|
232
307
|
|
|
233
308
|
test("noop is still detected when the file lacks a final newline", () => {
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
309
|
+
const text = "a\nb";
|
|
310
|
+
const r = applyEdits(text, [{ op: "replace", start: at(text, 1), body: ["a"] }], 4, 15);
|
|
311
|
+
assert.deepEqual(r, { ok: true, text, changed: false, touchedLines: [], contextLines: [] });
|
|
237
312
|
});
|
|
238
313
|
|
|
239
314
|
// --- shifted-anchor recovery ---
|
|
240
315
|
|
|
241
316
|
test("shifted recovery: content moved down → found with a fresh anchor", () => {
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
317
|
+
const readText = "a\nb\nc\nd\ne\n";
|
|
318
|
+
const currentText = "a\nX\nb\nc\nd\ne\n"; // inserted X after line 1 → "c" moved 3→4
|
|
319
|
+
const r = applyEdits(
|
|
320
|
+
currentText,
|
|
321
|
+
[{ op: "replace", start: at(readText, 3), body: ["C"] }],
|
|
322
|
+
4,
|
|
323
|
+
15,
|
|
324
|
+
);
|
|
325
|
+
assert.ok(!r.ok && r.failure.kind === "anchor");
|
|
326
|
+
const f = r.failure.failures[0];
|
|
327
|
+
assert.ok(f.recovery.kind === "found");
|
|
328
|
+
assert.equal(f.recovery.newLine, 4);
|
|
329
|
+
assert.equal(f.recovery.scope, "local");
|
|
330
|
+
// The recovered anchor must verify against the current file.
|
|
331
|
+
assert.equal(computeLineHash(4, splitLines(currentText)[3], 4), f.recovery.newHash);
|
|
256
332
|
});
|
|
257
333
|
|
|
258
334
|
test("rescued anchor lets the retry succeed without a re-read", () => {
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
335
|
+
const readText = "a\nb\nc\nd\ne\n";
|
|
336
|
+
const currentText = "a\nX\nb\nc\nd\ne\n";
|
|
337
|
+
// first attempt with a stale anchor → rescued
|
|
338
|
+
const r1 = applyEdits(
|
|
339
|
+
currentText,
|
|
340
|
+
[{ op: "replace", start: at(readText, 3), body: ["C"] }],
|
|
341
|
+
4,
|
|
342
|
+
15,
|
|
343
|
+
);
|
|
344
|
+
assert.ok(!r1.ok && r1.failure.kind === "anchor");
|
|
345
|
+
const f = r1.failure.failures[0];
|
|
346
|
+
assert.ok(f.recovery.kind === "found");
|
|
347
|
+
// Retry with the recovered anchor; the rest of the file stays unchanged.
|
|
348
|
+
const r2 = applyEdits(
|
|
349
|
+
currentText,
|
|
350
|
+
[{ op: "replace", start: { line: f.recovery.newLine, hash: f.recovery.newHash }, body: ["C"] }],
|
|
351
|
+
4,
|
|
352
|
+
15,
|
|
353
|
+
);
|
|
354
|
+
assert.ok(r2.ok);
|
|
355
|
+
assert.equal(r2.text, "a\nX\nb\nC\nd\ne\n");
|
|
275
356
|
});
|
|
276
357
|
|
|
277
358
|
test("shifted recovery: duplicate content → ambiguous candidates", () => {
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
359
|
+
const readText = "a\nx\nb\nx\nc\n";
|
|
360
|
+
const currentText = "a\nY\nx\nb\nx\nc\n"; // both "x" shifted
|
|
361
|
+
const r = applyEdits(
|
|
362
|
+
currentText,
|
|
363
|
+
[{ op: "replace", start: at(readText, 2), body: ["Z"] }],
|
|
364
|
+
4,
|
|
365
|
+
15,
|
|
366
|
+
);
|
|
367
|
+
assert.ok(!r.ok && r.failure.kind === "anchor");
|
|
368
|
+
const f = r.failure.failures[0];
|
|
369
|
+
assert.ok(f.recovery.kind === "ambiguous");
|
|
370
|
+
assert.equal(f.recovery.scope, "local");
|
|
371
|
+
assert.deepEqual(
|
|
372
|
+
f.recovery.candidates.map((c) => c.line),
|
|
373
|
+
[3, 5],
|
|
374
|
+
);
|
|
293
375
|
});
|
|
294
376
|
|
|
295
377
|
test("shifted recovery: content genuinely changed → none, with live content", () => {
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
378
|
+
const readText = "a\nb\nc\n";
|
|
379
|
+
const currentText = "a\nBCHANGED\nc\n"; // "b" is gone
|
|
380
|
+
const r = applyEdits(
|
|
381
|
+
currentText,
|
|
382
|
+
[{ op: "replace", start: at(readText, 2), body: ["B"] }],
|
|
383
|
+
4,
|
|
384
|
+
15,
|
|
385
|
+
);
|
|
386
|
+
assert.ok(!r.ok && r.failure.kind === "anchor");
|
|
387
|
+
const f = r.failure.failures[0];
|
|
388
|
+
assert.equal(f.recovery.kind, "none");
|
|
389
|
+
assert.equal(f.current?.content, "BCHANGED");
|
|
305
390
|
});
|
|
306
391
|
|
|
307
392
|
test("full-file recovery finds distant content and anchors beyond the current EOF", () => {
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
393
|
+
for (const [oldLine, newLine] of [
|
|
394
|
+
[2, 90],
|
|
395
|
+
[90, 2],
|
|
396
|
+
[190, 2],
|
|
397
|
+
]) {
|
|
398
|
+
const lines = Array.from({ length: 100 }, () => "filler");
|
|
399
|
+
lines[newLine - 1] = "target";
|
|
400
|
+
const result = applyEdits(
|
|
401
|
+
lines.join("\n"),
|
|
402
|
+
[{ op: "delete", start: { line: oldLine, hash: computeLineHash(oldLine, "target", 4) } }],
|
|
403
|
+
4,
|
|
404
|
+
15,
|
|
405
|
+
);
|
|
406
|
+
assert.ok(!result.ok && result.failure.kind === "anchor");
|
|
407
|
+
assert.deepEqual(result.failure.failures[0].recovery, {
|
|
408
|
+
kind: "found",
|
|
409
|
+
scope: "full-file",
|
|
410
|
+
newLine,
|
|
411
|
+
newHash: computeLineHash(newLine, "target", 4),
|
|
412
|
+
});
|
|
413
|
+
}
|
|
319
414
|
});
|
|
320
415
|
|
|
321
416
|
test("full-file recovery collects both sides while preserving local candidate priority", () => {
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
417
|
+
for (const { positions, selected, scope } of [
|
|
418
|
+
{ positions: [3, 95], selected: [3, 95], scope: "full-file" },
|
|
419
|
+
{ positions: [3, 49, 95], selected: [49], scope: "local" },
|
|
420
|
+
{ positions: [3, 49, 51, 95], selected: [49, 51], scope: "local" },
|
|
421
|
+
]) {
|
|
422
|
+
const lines = Array.from({ length: 100 }, () => "filler");
|
|
423
|
+
for (const line of positions) lines[line - 1] = "target";
|
|
424
|
+
const result = applyEdits(
|
|
425
|
+
lines.join("\n"),
|
|
426
|
+
[{ op: "delete", start: { line: 50, hash: computeLineHash(50, "target", 4) } }],
|
|
427
|
+
4,
|
|
428
|
+
15,
|
|
429
|
+
);
|
|
430
|
+
assert.ok(!result.ok && result.failure.kind === "anchor");
|
|
431
|
+
assert.deepEqual(
|
|
432
|
+
result.failure.failures[0].recovery,
|
|
433
|
+
selected.length === 1
|
|
434
|
+
? {
|
|
435
|
+
kind: "found",
|
|
436
|
+
scope,
|
|
437
|
+
newLine: selected[0],
|
|
438
|
+
newHash: computeLineHash(selected[0], "target", 4),
|
|
439
|
+
}
|
|
440
|
+
: {
|
|
441
|
+
kind: "ambiguous",
|
|
442
|
+
scope,
|
|
443
|
+
candidates: selected.map((line) => ({
|
|
444
|
+
line,
|
|
445
|
+
hash: computeLineHash(line, "target", 4),
|
|
446
|
+
})),
|
|
447
|
+
},
|
|
448
|
+
);
|
|
449
|
+
}
|
|
337
450
|
});
|
|
338
451
|
|
|
339
452
|
test("collect-all: two stale anchors in one batch → both failures returned", () => {
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
453
|
+
const readText = "a\nb\nc\nd\n";
|
|
454
|
+
const currentText = "X\na\nb\nc\nd\n"; // inserted X at top → all shifted +1
|
|
455
|
+
const r = applyEdits(
|
|
456
|
+
currentText,
|
|
457
|
+
[
|
|
458
|
+
{ op: "replace", start: at(readText, 2), body: ["B"] },
|
|
459
|
+
{ op: "replace", start: at(readText, 4), body: ["D"] },
|
|
460
|
+
],
|
|
461
|
+
4,
|
|
462
|
+
15,
|
|
463
|
+
);
|
|
464
|
+
assert.ok(!r.ok && r.failure.kind === "anchor");
|
|
465
|
+
assert.equal(r.failure.failures.length, 2);
|
|
466
|
+
const found = r.failure.failures.map((f) =>
|
|
467
|
+
f.recovery.kind === "found" ? f.recovery.newLine : -1,
|
|
468
|
+
);
|
|
469
|
+
assert.deepEqual(found, [3, 5]);
|
|
352
470
|
});
|
|
353
471
|
|
|
354
472
|
test("shiftRadius=0 disables rescue (always none)", () => {
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
assert.equal(r.failure.failures[0].recovery.kind, "none");
|
|
361
|
-
}
|
|
362
|
-
});
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
test("rejects CR or LF embedded in body elements", () => {
|
|
366
|
-
for (const body of [["x\ny"], ["x\ry"]]) {
|
|
367
|
-
const result = applyEdits("a\n", [{ op: "append", body }]);
|
|
368
|
-
assert.deepEqual(result, {
|
|
369
|
-
ok: false,
|
|
370
|
-
failure: { kind: "input", message: "INVALID_BODY: each body element must contain exactly one logical line.", checks: [] },
|
|
371
|
-
});
|
|
372
|
-
}
|
|
473
|
+
const readText = "a\nb\nc\nd\ne\n";
|
|
474
|
+
const currentText = "a\nX\nb\nc\nd\ne\n";
|
|
475
|
+
const r = applyEdits(currentText, [{ op: "replace", start: at(readText, 3), body: ["C"] }], 4, 0);
|
|
476
|
+
assert.ok(!r.ok && r.failure.kind === "anchor");
|
|
477
|
+
assert.equal(r.failure.failures[0].recovery.kind, "none");
|
|
373
478
|
});
|
|
374
479
|
|
|
375
480
|
test("BOM stays at byte zero through first-line edits while anchors retain their original hashes", () => {
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
481
|
+
for (const text of [
|
|
482
|
+
"\uFEFFfirst\nsecond\n",
|
|
483
|
+
"\uFEFFfirst\r\nsecond\r\n",
|
|
484
|
+
"\uFEFFfirst\r\nsecond\n",
|
|
485
|
+
"\uFEFFfirst\nsecond",
|
|
486
|
+
]) {
|
|
487
|
+
const ending = text.includes("\r\n") ? "\r\n" : "\n";
|
|
488
|
+
const rest = text.slice(text.indexOf("\n") + 1);
|
|
489
|
+
const cases: [Edit, string][] = [
|
|
490
|
+
[{ op: "replace", start: at(text, 1), body: ["changed"] }, `\uFEFFchanged${ending}${rest}`],
|
|
491
|
+
[
|
|
492
|
+
{ op: "replace", start: at(text, 1), body: ["\uFEFFchanged"] },
|
|
493
|
+
`\uFEFFchanged${ending}${rest}`,
|
|
494
|
+
],
|
|
495
|
+
[{ op: "delete", start: at(text, 1) }, `\uFEFF${rest}`],
|
|
496
|
+
[{ op: "prepend", body: ["new"] }, `\uFEFFnew${ending}${text.slice(1)}`],
|
|
497
|
+
[
|
|
498
|
+
{ op: "insert_before", anchor: at(text, 1), body: ["new"] },
|
|
499
|
+
`\uFEFFnew${ending}${text.slice(1)}`,
|
|
500
|
+
],
|
|
501
|
+
[{ op: "delete", start: at(text, 1), end: at(text, 2) }, "\uFEFF"],
|
|
502
|
+
];
|
|
503
|
+
for (const [edit, expected] of cases) {
|
|
504
|
+
const result = applyEdits(text, [edit], 4, 15);
|
|
505
|
+
assert.ok(result.ok);
|
|
506
|
+
assert.equal(result.text, expected, `${edit.op}: ${JSON.stringify(text)}`);
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
for (const text of ["\uFEFF", "\uFEFFfirst"]) {
|
|
510
|
+
const result = applyEdits(
|
|
511
|
+
text,
|
|
512
|
+
[{ op: "replace", start: at(text, 1), body: ["changed"] }],
|
|
513
|
+
4,
|
|
514
|
+
15,
|
|
515
|
+
);
|
|
516
|
+
assert.ok(result.ok);
|
|
517
|
+
assert.equal(result.text, "\uFEFFchanged");
|
|
518
|
+
}
|
|
519
|
+
const embedded = "\uFEFFfirst\ninside\uFEFFcontent\n";
|
|
520
|
+
const result = applyEdits(
|
|
521
|
+
embedded,
|
|
522
|
+
[{ op: "replace", start: at(embedded, 1), body: ["changed"] }],
|
|
523
|
+
4,
|
|
524
|
+
15,
|
|
525
|
+
);
|
|
526
|
+
assert.ok(result.ok);
|
|
527
|
+
assert.equal(result.text, "\uFEFFchanged\ninside\uFEFFcontent\n");
|
|
402
528
|
});
|
|
403
529
|
|
|
404
530
|
test("failed batches report every supplied anchor in input order from one snapshot", () => {
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
const invalid = applyEdits(text, [...edits, { op: "append", body: ["bad\nline"] }]);
|
|
426
|
-
assert.ok(!invalid.ok && invalid.failure.kind === "input");
|
|
427
|
-
assert.deepEqual(invalid.failure.checks, result.failure.checks.map((check) => ({ ...check, status: "not_checked" })));
|
|
531
|
+
const text = "a\nb\nc\nd\ne\nf\n";
|
|
532
|
+
const stale = { line: 2, hash: "ZZ" };
|
|
533
|
+
const edits: Edit[] = [
|
|
534
|
+
{ op: "replace", start: at(text, 1), end: stale, body: ["A"] },
|
|
535
|
+
{ op: "delete", start: stale, end: at(text, 4) },
|
|
536
|
+
{ op: "insert_after", anchor: at(text, 5), body: ["E"] },
|
|
537
|
+
{ op: "insert_before", anchor: stale, body: ["B"] },
|
|
538
|
+
{ op: "append", body: ["last"] },
|
|
539
|
+
{ op: "prepend", body: ["first"] },
|
|
540
|
+
];
|
|
541
|
+
const result = applyEdits(text, edits, 4, 15);
|
|
542
|
+
assert.ok(!result.ok && result.failure.kind === "anchor");
|
|
543
|
+
assert.deepEqual(result.failure.checks, [
|
|
544
|
+
{ opIndex: 0, op: "replace", which: "anchor", cited: at(text, 1), status: "matched" },
|
|
545
|
+
{ opIndex: 0, op: "replace", which: "end", cited: stale, status: "mismatched" },
|
|
546
|
+
{ opIndex: 1, op: "delete", which: "anchor", cited: stale, status: "mismatched" },
|
|
547
|
+
{ opIndex: 1, op: "delete", which: "end", cited: at(text, 4), status: "matched" },
|
|
548
|
+
{ opIndex: 2, op: "insert_after", which: "anchor", cited: at(text, 5), status: "matched" },
|
|
549
|
+
{ opIndex: 3, op: "insert_before", which: "anchor", cited: stale, status: "mismatched" },
|
|
550
|
+
]);
|
|
428
551
|
});
|
|
429
552
|
|
|
430
553
|
test("matched anchor checks do not imply valid ranges", () => {
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
554
|
+
const text = "a\nb\nc\n";
|
|
555
|
+
const cases: { edits: Edit[]; kind: "range"; checks: number }[] = [
|
|
556
|
+
{ edits: [{ op: "delete", start: at(text, 3), end: at(text, 1) }], kind: "range", checks: 2 },
|
|
557
|
+
{
|
|
558
|
+
edits: [
|
|
559
|
+
{ op: "replace", start: at(text, 1), end: at(text, 3), body: ["x"] },
|
|
560
|
+
{ op: "delete", start: at(text, 2) },
|
|
561
|
+
],
|
|
562
|
+
kind: "range",
|
|
563
|
+
checks: 3,
|
|
564
|
+
},
|
|
565
|
+
];
|
|
566
|
+
for (const { edits, kind, checks } of cases) {
|
|
567
|
+
const result = applyEdits(text, edits, 4, 15);
|
|
568
|
+
assert.ok(!result.ok);
|
|
569
|
+
assert.equal(result.failure.kind, kind);
|
|
570
|
+
assert.equal(result.failure.checks.length, checks);
|
|
571
|
+
assert.ok(result.failure.checks.every((check) => check.status === "matched"));
|
|
572
|
+
}
|
|
573
|
+
});
|
|
574
|
+
|
|
575
|
+
test("zero-length replacement without line deletion does not produce false deletion successors", () => {
|
|
576
|
+
const text = "a\nb\nc\n";
|
|
577
|
+
// A batch with a real change and an empty body insertion
|
|
578
|
+
const edits: Edit[] = [
|
|
579
|
+
{ op: "replace", start: at(text, 1), body: ["A"] },
|
|
580
|
+
{ op: "insert_after", anchor: at(text, 2), body: [] },
|
|
581
|
+
];
|
|
582
|
+
const result = applyEdits(text, edits, 4, 15);
|
|
583
|
+
assert.ok(result.ok);
|
|
584
|
+
if (result.ok) {
|
|
585
|
+
assert.equal(result.changed, true);
|
|
586
|
+
assert.deepEqual(result.touchedLines, [0]);
|
|
587
|
+
assert.deepEqual(result.contextLines, []);
|
|
588
|
+
}
|
|
443
589
|
});
|