@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.
Files changed (75) hide show
  1. package/README.md +166 -61
  2. package/package.json +8 -3
  3. package/src/core/apply.test.ts +459 -313
  4. package/src/core/apply.ts +251 -215
  5. package/src/core/errors.ts +9 -0
  6. package/src/core/hash.test.ts +61 -23
  7. package/src/core/hash.ts +49 -20
  8. package/src/core/lines.test.ts +46 -27
  9. package/src/core/lines.ts +36 -28
  10. package/src/core/ranges.test.ts +73 -19
  11. package/src/core/ranges.ts +15 -15
  12. package/src/core/replace.ts +109 -0
  13. package/src/core/text.test.ts +3 -3
  14. package/src/core/text.ts +26 -9
  15. package/src/core/types.ts +56 -41
  16. package/src/index.ts +23 -19
  17. package/src/integration/config-registration.test.ts +206 -0
  18. package/src/integration/grep-rg.test.ts +879 -251
  19. package/src/integration/large-text.test.ts +63 -0
  20. package/src/integration/replace-package.test.ts +41 -0
  21. package/src/pi/action-fusion.test.ts +612 -207
  22. package/src/pi/action-fusion.ts +442 -254
  23. package/src/pi/anchor-format.test.ts +53 -21
  24. package/src/pi/anchor-format.ts +48 -20
  25. package/src/pi/budgets.ts +25 -0
  26. package/src/pi/config.test.ts +178 -35
  27. package/src/pi/config.ts +96 -37
  28. package/src/pi/directory-sync.test.ts +55 -0
  29. package/src/pi/e2e-scenarios.test.ts +360 -0
  30. package/src/pi/edit-tool.ts +307 -279
  31. package/src/pi/error-text.ts +26 -0
  32. package/src/pi/execute.test.ts +1718 -713
  33. package/src/pi/failure-context.test.ts +110 -83
  34. package/src/pi/failure-context.ts +232 -73
  35. package/src/pi/failure-path.test.ts +180 -66
  36. package/src/pi/file-commit.test.ts +446 -233
  37. package/src/pi/file-commit.ts +348 -203
  38. package/src/pi/fusion-card.test.ts +118 -83
  39. package/src/pi/fusion-card.ts +203 -87
  40. package/src/pi/grep-output.ts +225 -0
  41. package/src/pi/grep-render.ts +59 -0
  42. package/src/pi/grep-scope.ts +139 -0
  43. package/src/pi/grep-search.ts +231 -0
  44. package/src/pi/grep-tool.ts +215 -774
  45. package/src/pi/grep.test.ts +825 -376
  46. package/src/pi/mutation-outcome.testing.ts +22 -0
  47. package/src/pi/mutation-result.test.ts +86 -0
  48. package/src/pi/mutation-result.ts +118 -63
  49. package/src/pi/mutation-runner.ts +151 -0
  50. package/src/pi/path.ts +20 -12
  51. package/src/pi/pi.test.ts +100 -52
  52. package/src/pi/read-tool.ts +245 -134
  53. package/src/pi/registration.test.ts +271 -114
  54. package/src/pi/render.test.ts +227 -84
  55. package/src/pi/render.ts +86 -116
  56. package/src/pi/replace-regex.ts +54 -0
  57. package/src/pi/replace-tool.ts +247 -241
  58. package/src/pi/replace-worker.mjs +4 -0
  59. package/src/pi/replace-worker.ts +10 -0
  60. package/src/pi/replace.test.ts +778 -390
  61. package/src/pi/result-safety.test.ts +821 -344
  62. package/src/pi/rg-line-ranges.test.ts +57 -31
  63. package/src/pi/rg-line-ranges.ts +45 -82
  64. package/src/pi/rg-process.test.ts +113 -0
  65. package/src/pi/{rg-line-filter.ts → rg-process.ts} +72 -125
  66. package/src/pi/rg-text-view.ts +102 -48
  67. package/src/pi/schema.ts +19 -0
  68. package/src/pi/text-stream.test.ts +118 -0
  69. package/src/pi/text-stream.ts +136 -0
  70. package/src/pi/tool-call.testing.ts +37 -0
  71. package/src/pi/write-tool.ts +114 -52
  72. package/src/pi/write.test.ts +250 -97
  73. package/src/core/index.ts +0 -14
  74. package/src/pi/rg-line-filter.test.ts +0 -85
  75. package/src/pi/state.ts +0 -26
@@ -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
- return { line, hash: computeLineHash(line, splitLines(text)[line - 1]) };
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
- const text = "a\nb\nc\n";
17
- const r = applyEdits(text, [{ op: "replace", start: at(text, 2), body: ["B"] }]);
18
- assert.equal(r.ok, true);
19
- if (r.ok) assert.equal(r.text, "a\nB\nc\n");
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
- 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"] }]);
26
- assert.equal(r.ok, true);
27
- if (r.ok) assert.equal(r.text, `changed${ending}last\r`);
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
- const text = "a\nb\nc\nd\ne\n";
33
- const r = applyEdits(text, [{ op: "replace", start: at(text, 2), end: at(text, 4), body: ["X", "Y"] }]);
34
- assert.equal(r.ok, true);
35
- if (r.ok) assert.equal(r.text, "a\nX\nY\ne\n");
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
- const text = "a\nb\nc\nd\n";
40
- const r = applyEdits(text, [{ op: "delete", start: at(text, 2), end: at(text, 3) }]);
41
- assert.equal(r.ok, true);
42
- if (r.ok) assert.equal(r.text, "a\nd\n");
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
- const text = "a\nb\n";
47
- const r = applyEdits(text, [{ op: "insert_after", anchor: at(text, 1), body: ["x"] }]);
48
- assert.equal(r.ok, true);
49
- if (r.ok) assert.equal(r.text, "a\nx\nb\n");
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
- const text = "a\nb\n";
54
- const r = applyEdits(text, [{ op: "insert_before", anchor: at(text, 2), body: ["x"] }]);
55
- assert.equal(r.ok, true);
56
- if (r.ok) assert.equal(r.text, "a\nx\nb\n");
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
- const text = "a\nb\n";
61
- const r = applyEdits(text, [
62
- { op: "prepend", body: ["head"] },
63
- { op: "append", body: ["tail"] },
64
- ]);
65
- assert.equal(r.ok, true);
66
- if (r.ok) assert.equal(r.text, "head\na\nb\ntail\n");
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
- const text = "a\nb\nc\n";
71
- const r = applyEdits(text, [
72
- { op: "insert_after", anchor: at(text, 3), body: ["z"] },
73
- { op: "replace", start: at(text, 1), body: ["A"] },
74
- ]);
75
- assert.equal(r.ok, true);
76
- if (r.ok) assert.equal(r.text, "A\nb\nc\nz\n");
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
- const text = "a\nb\nc\n";
81
- const r = applyEdits(text, [
82
- { op: "insert_after", anchor: at(text, 3), body: ["z"] },
83
- { op: "replace", start: at(text, 1), body: ["A"] },
84
- ]);
85
- assert.equal(r.ok, true);
86
- if (r.ok) {
87
- // new file: [A, b, c, z]; this edit produced line index 0 (A) and 3 (z)
88
- assert.deepEqual([...r.touchedLines], [0, 3]);
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
- const text = "a\n";
94
- const r = applyEdits(text, [{ op: "append", body: ["b", "c"] }]);
95
- assert.equal(r.ok, true);
96
- if (r.ok) assert.deepEqual([...r.touchedLines], [1, 2]);
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
- const text = "a\nb\nc\nd\n";
101
- const r = applyEdits(text, [{ op: "delete", start: at(text, 2) }]);
102
- assert.equal(r.ok, true);
103
- if (r.ok) {
104
- // new file: [a, c, d]; deleting line 2 (b) shifts c into index 1
105
- assert.deepEqual([...r.touchedLines], [1]);
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
- const text = "a\nb\n";
113
- const r = applyEdits(text, [{ op: "replace", start: { line: 1, hash: "WRONG" }, body: ["x"] }]);
114
- assert.equal(r.ok, false);
115
- if (!r.ok) assert.equal(r.failure.kind, "anchor");
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
- const text = "a\n";
120
- const r = applyEdits(text, [{ op: "replace", start: { line: 5, hash: computeLineHash(1, "a") }, body: ["x"] }]);
121
- assert.equal(r.ok, false);
122
- if (!r.ok) assert.equal(r.failure.kind, "anchor");
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
- // hash for line 2's content, but cited as line 1 → must fail because line 1's live content differs
127
- const text = "a\nb\n";
128
- const r = applyEdits(text, [{ op: "replace", start: { line: 1, hash: computeLineHash(2, "b") }, body: ["x"] }]);
129
- assert.equal(r.ok, false);
130
- if (!r.ok) assert.equal(r.failure.kind, "anchor");
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
- const text = "a\nb\nc\n";
135
- const r = applyEdits(text, [{ op: "replace", start: at(text, 3), end: at(text, 1), body: ["x"] }]);
136
- assert.equal(r.ok, false);
137
- if (!r.ok) assert.equal(r.failure.kind, "range");
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
- const text = "a\nb\nc\nd\n";
142
- const r = applyEdits(text, [
143
- { op: "replace", start: at(text, 2), end: at(text, 3), body: ["x"] },
144
- { op: "replace", start: at(text, 3), body: ["y"] },
145
- ]);
146
- assert.equal(r.ok, false);
147
- if (!r.ok) assert.equal(r.failure.kind, "range");
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
- const text = "a\nb\n";
152
- const r = applyEdits(text, [
153
- { op: "insert_after", anchor: at(text, 1), body: ["x"] },
154
- { op: "insert_after", anchor: at(text, 1), body: ["y"] },
155
- ]);
156
- assert.equal(r.ok, false);
157
- if (!r.ok) assert.equal(r.failure.kind, "range");
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
- for (const text of ["a\nb\n", "\uFEFFa\r\nb\n"]) {
162
- const body = text.startsWith("\uFEFF") ? "\uFEFFa" : "a";
163
- const r = applyEdits(text, [{ op: "replace", start: at(text, 1), body: [body] }]);
164
- assert.deepEqual(r, { ok: true, text, changed: false, touchedLines: [], contextLines: [] });
165
- const invalid = applyEdits(text, [{ op: "replace", start: at("wrong\nb\n", 1), body: [body] }]);
166
- assert.ok(!invalid.ok && invalid.failure.kind === "anchor");
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
- // read-time text and current text differ at line 3, but we only touch line 1 — must succeed
172
- const readText = "a\nb\nc\n";
173
- const currentText = "a\nb\nCHANGED\n";
174
- const r = applyEdits(currentText, [{ op: "replace", start: at(readText, 1), body: ["A"] }]);
175
- assert.equal(r.ok, true);
176
- if (r.ok) assert.equal(r.text, "A\nb\nCHANGED\n");
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
- const text = "a\r\nb\r\n";
181
- const r = applyEdits(text, [{ op: "replace", start: at(text, 1), body: ["A"] }]);
182
- assert.equal(r.ok, true);
183
- if (r.ok) assert.equal(r.text, "A\r\nb\r\n");
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
- const before = "first\r\nsecond\nthird\r\n";
188
- const result = applyEdits(before, [{ op: "replace", start: at(before, 2), body: ["SECOND"] }]);
189
- assert.equal(result.ok, true);
190
- if (result.ok) assert.equal(result.text, "first\r\nSECOND\nthird\r\n");
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
- const before = "a\r\nb\nc\r\nlast";
195
- const inserted = applyEdits(before, [{ op: "insert_after", anchor: at(before, 2), body: ["x"] }]);
196
- assert.equal(inserted.ok, true);
197
- if (inserted.ok) assert.equal(inserted.text, "a\r\nb\nx\r\nc\r\nlast");
198
- const deleted = applyEdits(before, [{ op: "delete", start: at(before, 2) }]);
199
- assert.equal(deleted.ok, true);
200
- if (deleted.ok) assert.equal(deleted.text, "a\r\nc\r\nlast");
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
- const before = "a\r\nb\nc\r\nd";
205
- const result = applyEdits(before, [{ op: "replace", start: at(before, 2), body: ["X", "Y", "Z"] }]);
206
- assert.equal(result.ok, true);
207
- // b's gap (\n) moves to the last new line, so the boundary to c is unchanged;
208
- // b has no internal gap; both new internal gaps use the file style, like replace("b", "X\nY\nZ").
209
- if (result.ok) assert.equal(result.text, "a\r\nX\r\nY\r\nZ\nc\r\nd");
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
- const text = "a\nb";
214
- const r = applyEdits(text, [{ op: "replace", start: at(text, 2), body: ["B"] }]);
215
- assert.equal(r.ok, true);
216
- if (r.ok) assert.equal(r.text, "a\nB");
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
- const text = "a\r\nb";
221
- const r = applyEdits(text, [{ op: "replace", start: at(text, 1), body: ["A"] }]);
222
- assert.equal(r.ok, true);
223
- if (r.ok) assert.equal(r.text, "A\r\nb");
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
- const text = "a";
228
- const r = applyEdits(text, [{ op: "append", body: ["b"] }]);
229
- assert.equal(r.ok, true);
230
- if (r.ok) assert.equal(r.text, "a\nb");
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
- const text = "a\nb";
235
- const r = applyEdits(text, [{ op: "replace", start: at(text, 1), body: ["a"] }]);
236
- assert.deepEqual(r, { ok: true, text, changed: false, touchedLines: [], contextLines: [] });
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
- const readText = "a\nb\nc\nd\ne\n";
243
- const currentText = "a\nX\nb\nc\nd\ne\n"; // inserted X after line 1 → "c" moved 3→4
244
- const r = applyEdits(currentText, [{ op: "replace", start: at(readText, 3), body: ["C"] }]);
245
- assert.equal(r.ok, false);
246
- if (!r.ok && r.failure.kind === "anchor") {
247
- const f = r.failure.failures[0];
248
- assert.equal(f.recovery.kind, "found");
249
- if (f.recovery.kind === "found") {
250
- assert.equal(f.recovery.newLine, 4);
251
- assert.equal(f.recovery.scope, "local");
252
- // the rescued anchor must verify against the current file
253
- assert.equal(computeLineHash(4, splitLines(currentText)[3]), f.recovery.newHash);
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
- const readText = "a\nb\nc\nd\ne\n";
260
- const currentText = "a\nX\nb\nc\nd\ne\n";
261
- // first attempt with a stale anchor → rescued
262
- const r1 = applyEdits(currentText, [{ op: "replace", start: at(readText, 3), body: ["C"] }]);
263
- assert.equal(r1.ok, false);
264
- if (r1.ok) return;
265
- if (r1.failure.kind !== "anchor") return;
266
- const f = r1.failure.failures[0];
267
- assert.equal(f.recovery.kind, "found");
268
- if (f.recovery.kind !== "found") return;
269
- // retry with the rescued anchor → succeeds, file unchanged elsewhere
270
- const r2 = applyEdits(currentText, [
271
- { op: "replace", start: { line: f.recovery.newLine, hash: f.recovery.newHash }, body: ["C"] },
272
- ]);
273
- assert.equal(r2.ok, true);
274
- if (r2.ok) assert.equal(r2.text, "a\nX\nb\nC\nd\ne\n");
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
- const readText = "a\nx\nb\nx\nc\n";
279
- const currentText = "a\nY\nx\nb\nx\nc\n"; // both "x" shifted
280
- const r = applyEdits(currentText, [{ op: "replace", start: at(readText, 2), body: ["Z"] }]);
281
- assert.equal(r.ok, false);
282
- if (!r.ok && r.failure.kind === "anchor") {
283
- const f = r.failure.failures[0];
284
- assert.equal(f.recovery.kind, "ambiguous");
285
- if (f.recovery.kind === "ambiguous") {
286
- assert.equal(f.recovery.scope, "local");
287
- assert.deepEqual(
288
- f.recovery.candidates.map((c) => c.line),
289
- [3, 5],
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
- const readText = "a\nb\nc\n";
297
- const currentText = "a\nBCHANGED\nc\n"; // "b" is gone
298
- const r = applyEdits(currentText, [{ op: "replace", start: at(readText, 2), body: ["B"] }]);
299
- assert.equal(r.ok, false);
300
- if (!r.ok && r.failure.kind === "anchor") {
301
- const f = r.failure.failures[0];
302
- assert.equal(f.recovery.kind, "none");
303
- assert.equal(f.current?.content, "BCHANGED");
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
- for (const [oldLine, newLine] of [[2, 90], [90, 2], [190, 2]]) {
309
- const lines = Array.from({ length: 100 }, () => "filler");
310
- lines[newLine - 1] = "target";
311
- const result = applyEdits(lines.join("\n"), [
312
- { op: "delete", start: { line: oldLine, hash: computeLineHash(oldLine, "target") } },
313
- ]);
314
- assert.ok(!result.ok && result.failure.kind === "anchor");
315
- assert.deepEqual(result.failure.failures[0].recovery, {
316
- kind: "found", scope: "full-file", newLine, newHash: computeLineHash(newLine, "target"),
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
- for (const { positions, selected, scope } of [
323
- { positions: [3, 95], selected: [3, 95], scope: "full-file" },
324
- { positions: [3, 49, 95], selected: [49], scope: "local" },
325
- { positions: [3, 49, 51, 95], selected: [49, 51], scope: "local" },
326
- ]) {
327
- const lines = Array.from({ length: 100 }, () => "filler");
328
- for (const line of positions) lines[line - 1] = "target";
329
- const result = applyEdits(lines.join("\n"), [
330
- { op: "delete", start: { line: 50, hash: computeLineHash(50, "target") } },
331
- ]);
332
- assert.ok(!result.ok && result.failure.kind === "anchor");
333
- assert.deepEqual(result.failure.failures[0].recovery, selected.length === 1
334
- ? { kind: "found", scope, newLine: selected[0], newHash: computeLineHash(selected[0], "target") }
335
- : { kind: "ambiguous", scope, candidates: selected.map(line => ({ line, hash: computeLineHash(line, "target") })) });
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
- const readText = "a\nb\nc\nd\n";
341
- const currentText = "X\na\nb\nc\nd\n"; // inserted X at top → all shifted +1
342
- const r = applyEdits(currentText, [
343
- { op: "replace", start: at(readText, 2), body: ["B"] },
344
- { op: "replace", start: at(readText, 4), body: ["D"] },
345
- ]);
346
- assert.equal(r.ok, false);
347
- if (!r.ok && r.failure.kind === "anchor") {
348
- assert.equal(r.failure.failures.length, 2);
349
- const found = r.failure.failures.map((f) => (f.recovery.kind === "found" ? f.recovery.newLine : -1));
350
- assert.deepEqual(found, [3, 5]);
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
- const readText = "a\nb\nc\nd\ne\n";
356
- const currentText = "a\nX\nb\nc\nd\ne\n";
357
- const r = applyEdits(currentText, [{ op: "replace", start: at(readText, 3), body: ["C"] }], 4, 0);
358
- assert.equal(r.ok, false);
359
- if (!r.ok && r.failure.kind === "anchor") {
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
- for (const text of ["\uFEFFfirst\nsecond\n", "\uFEFFfirst\r\nsecond\r\n", "\uFEFFfirst\r\nsecond\n", "\uFEFFfirst\nsecond"]) {
377
- const ending = text.includes("\r\n") ? "\r\n" : "\n";
378
- const rest = text.slice(text.indexOf("\n") + 1);
379
- const cases: [Edit, string][] = [
380
- [{ op: "replace", start: at(text, 1), body: ["changed"] }, `\uFEFFchanged${ending}${rest}`],
381
- [{ op: "replace", start: at(text, 1), body: ["\uFEFFchanged"] }, `\uFEFFchanged${ending}${rest}`],
382
- [{ op: "delete", start: at(text, 1) }, `\uFEFF${rest}`],
383
- [{ op: "prepend", body: ["new"] }, `\uFEFFnew${ending}${text.slice(1)}`],
384
- [{ op: "insert_before", anchor: at(text, 1), body: ["new"] }, `\uFEFFnew${ending}${text.slice(1)}`],
385
- [{ op: "delete", start: at(text, 1), end: at(text, 2) }, "\uFEFF"],
386
- ];
387
- for (const [edit, expected] of cases) {
388
- const result = applyEdits(text, [edit]);
389
- assert.ok(result.ok);
390
- assert.equal(result.text, expected, `${edit.op}: ${JSON.stringify(text)}`);
391
- }
392
- }
393
- for (const text of ["\uFEFF", "\uFEFFfirst"]) {
394
- const result = applyEdits(text, [{ op: "replace", start: at(text, 1), body: ["changed"] }]);
395
- assert.ok(result.ok);
396
- assert.equal(result.text, "\uFEFFchanged");
397
- }
398
- const embedded = "\uFEFFfirst\ninside\uFEFFcontent\n";
399
- const result = applyEdits(embedded, [{ op: "replace", start: at(embedded, 1), body: ["changed"] }]);
400
- assert.ok(result.ok);
401
- assert.equal(result.text, "\uFEFFchanged\ninside\uFEFFcontent\n");
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
- const text = "a\nb\nc\nd\ne\nf\n";
406
- const stale = { line: 2, hash: "ZZ" };
407
- const edits: Edit[] = [
408
- { op: "replace", start: at(text, 1), end: stale, body: ["A"] },
409
- { op: "delete", start: stale, end: at(text, 4) },
410
- { op: "insert_after", anchor: at(text, 5), body: ["E"] },
411
- { op: "insert_before", anchor: stale, body: ["B"] },
412
- { op: "append", body: ["last"] },
413
- { op: "prepend", body: ["first"] },
414
- ];
415
- const result = applyEdits(text, edits);
416
- assert.ok(!result.ok && result.failure.kind === "anchor");
417
- assert.deepEqual(result.failure.checks, [
418
- { opIndex: 0, op: "replace", which: "anchor", cited: at(text, 1), status: "matched" },
419
- { opIndex: 0, op: "replace", which: "end", cited: stale, status: "mismatched" },
420
- { opIndex: 1, op: "delete", which: "anchor", cited: stale, status: "mismatched" },
421
- { opIndex: 1, op: "delete", which: "end", cited: at(text, 4), status: "matched" },
422
- { opIndex: 2, op: "insert_after", which: "anchor", cited: at(text, 5), status: "matched" },
423
- { opIndex: 3, op: "insert_before", which: "anchor", cited: stale, status: "mismatched" },
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
- const text = "a\nb\nc\n";
432
- const cases: { edits: Edit[]; kind: "range"; checks: number }[] = [
433
- { edits: [{ op: "delete", start: at(text, 3), end: at(text, 1) }], kind: "range", checks: 2 },
434
- { edits: [{ op: "replace", start: at(text, 1), end: at(text, 3), body: ["x"] }, { op: "delete", start: at(text, 2) }], kind: "range", checks: 3 },
435
- ];
436
- for (const { edits, kind, checks } of cases) {
437
- const result = applyEdits(text, edits);
438
- assert.ok(!result.ok);
439
- assert.equal(result.failure.kind, kind);
440
- assert.equal(result.failure.checks.length, checks);
441
- assert.ok(result.failure.checks.every((check) => check.status === "matched"));
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
  });