@criogaid/pi-hashline-edit 0.13.0 → 0.15.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 (152) hide show
  1. package/README.md +18 -12
  2. package/README.zh-CN.md +18 -12
  3. package/package.json +8 -1
  4. package/src/core/AGENTS.md +3 -1
  5. package/src/core/apply.ts +110 -117
  6. package/src/core/character-set.ts +2 -3
  7. package/src/core/copy-span.ts +15 -0
  8. package/src/core/errors.ts +24 -9
  9. package/src/core/lines.ts +71 -17
  10. package/src/core/ranges.ts +5 -4
  11. package/src/core/replace-schema.ts +3 -1
  12. package/src/core/replace.ts +50 -29
  13. package/src/core/search-analysis.ts +21 -22
  14. package/src/core/search.ts +15 -32
  15. package/src/core/types.ts +3 -0
  16. package/src/index.ts +47 -39
  17. package/src/pi/AGENTS.md +30 -19
  18. package/src/pi/action-fusion.ts +284 -211
  19. package/src/pi/argument-diagnostics.ts +50 -37
  20. package/src/pi/argument-error.ts +2 -2
  21. package/src/pi/argument-validation.ts +163 -66
  22. package/src/pi/budgets.ts +12 -0
  23. package/src/pi/diagnostic-buffer.ts +12 -11
  24. package/src/pi/edit-tool.ts +138 -115
  25. package/src/pi/error-text.ts +10 -2
  26. package/src/pi/failure-context.ts +24 -37
  27. package/src/pi/file-commit.ts +131 -164
  28. package/src/pi/file-read.ts +122 -31
  29. package/src/pi/fusion-arguments.ts +214 -0
  30. package/src/pi/fusion-card.ts +102 -60
  31. package/src/pi/grep-candidates.ts +35 -22
  32. package/src/pi/grep-output.ts +197 -190
  33. package/src/pi/grep-render.ts +22 -13
  34. package/src/pi/grep-reread.ts +7 -6
  35. package/src/pi/grep-scope.ts +31 -16
  36. package/src/pi/grep-search.ts +56 -47
  37. package/src/pi/grep-tool.ts +35 -33
  38. package/src/pi/grep-worker.ts +3 -4
  39. package/src/pi/mutation-diff.ts +238 -0
  40. package/src/pi/mutation-result.ts +102 -79
  41. package/src/pi/mutation-runner.ts +107 -84
  42. package/src/pi/native-bash.ts +41 -0
  43. package/src/pi/path.ts +59 -6
  44. package/src/pi/read-tool.ts +173 -138
  45. package/src/pi/regex-worker.ts +14 -8
  46. package/src/pi/render.ts +101 -54
  47. package/src/pi/replace-regex.ts +1 -1
  48. package/src/pi/replace-tool.ts +14 -7
  49. package/src/pi/report-cause.ts +34 -17
  50. package/src/pi/report-schema.ts +184 -85
  51. package/src/pi/report.ts +133 -64
  52. package/src/pi/rg-process.ts +68 -48
  53. package/src/pi/schema.ts +4 -0
  54. package/src/pi/text-stream.ts +81 -87
  55. package/src/pi/workspace-cwd.js +2 -0
  56. package/src/pi/workspace.ts +46 -0
  57. package/src/pi/write-tool.ts +19 -13
  58. package/src/core/apply.test.ts +0 -644
  59. package/src/core/case-fold.test.ts +0 -35
  60. package/src/core/character-set.test.ts +0 -97
  61. package/src/core/errors.test.ts +0 -39
  62. package/src/core/hash.test.ts +0 -64
  63. package/src/core/lines.test.ts +0 -63
  64. package/src/core/ranges.test.ts +0 -123
  65. package/src/core/replace.test.ts +0 -187
  66. package/src/core/search-analysis.test.ts +0 -210
  67. package/src/core/search-matrix.test.ts +0 -64
  68. package/src/core/search-matrix.testing.ts +0 -171
  69. package/src/core/search.test.ts +0 -132
  70. package/src/core/text.test.ts +0 -52
  71. package/src/integration/config-registration.test.ts +0 -291
  72. package/src/integration/fusion-dispatch.test.ts +0 -320
  73. package/src/integration/grep-errors.test.ts +0 -81
  74. package/src/integration/grep-flags.test.ts +0 -76
  75. package/src/integration/grep-js-completeness.test.ts +0 -278
  76. package/src/integration/grep-line-candidates.test.ts +0 -201
  77. package/src/integration/grep-matrix.test.ts +0 -118
  78. package/src/integration/grep-order.test.ts +0 -78
  79. package/src/integration/grep-path-matrix.test.ts +0 -128
  80. package/src/integration/grep-prefilter-coverage.test.ts +0 -204
  81. package/src/integration/grep-process-limits.test.ts +0 -122
  82. package/src/integration/grep-regex.test.ts +0 -412
  83. package/src/integration/grep-rg.test.ts +0 -1659
  84. package/src/integration/grep-scan-budget.test.ts +0 -111
  85. package/src/integration/grep-translation.test.ts +0 -199
  86. package/src/integration/large-text.test.ts +0 -70
  87. package/src/integration/model-budget-lifecycle.test.ts +0 -186
  88. package/src/integration/mutation-file-types.test.ts +0 -30
  89. package/src/integration/mutation-file-types.testing.ts +0 -33
  90. package/src/integration/npm-package.test.ts +0 -19
  91. package/src/integration/npm-package.testing.ts +0 -243
  92. package/src/integration/publication-read-changes.test.ts +0 -454
  93. package/src/integration/read-changes.test.ts +0 -98
  94. package/src/integration/replace-package.test.ts +0 -22
  95. package/src/integration/replace-package.testing.ts +0 -36
  96. package/src/integration/replace-worker-boundary.test.ts +0 -127
  97. package/src/integration/report-replay.test.ts +0 -91
  98. package/src/integration/utf8-errors.test.ts +0 -46
  99. package/src/pi/action-fusion.test.ts +0 -659
  100. package/src/pi/action-fusion.testing.ts +0 -38
  101. package/src/pi/anchor-format.test.ts +0 -124
  102. package/src/pi/argument-error.testing.ts +0 -46
  103. package/src/pi/argument-format.test.ts +0 -196
  104. package/src/pi/argument-validation.test.ts +0 -1007
  105. package/src/pi/codemode.test.ts +0 -101
  106. package/src/pi/config.test.ts +0 -204
  107. package/src/pi/diagnostic-buffer.test.ts +0 -115
  108. package/src/pi/directory-sync.test.ts +0 -57
  109. package/src/pi/execute.test.ts +0 -1257
  110. package/src/pi/extension-loading.test.ts +0 -14
  111. package/src/pi/extension-loading.testing.ts +0 -3
  112. package/src/pi/failure-context.test.ts +0 -370
  113. package/src/pi/failure-path.test.ts +0 -289
  114. package/src/pi/file-commit.test.ts +0 -628
  115. package/src/pi/file-read.test.ts +0 -29
  116. package/src/pi/file-snapshot.test.ts +0 -22
  117. package/src/pi/fusion-card-interaction.test.ts +0 -143
  118. package/src/pi/fusion-card.test.ts +0 -353
  119. package/src/pi/grep-candidates.test.ts +0 -26
  120. package/src/pi/grep-matrix.test.ts +0 -68
  121. package/src/pi/grep-order.test.ts +0 -447
  122. package/src/pi/grep-output.testing.ts +0 -24
  123. package/src/pi/grep-rg.testing.ts +0 -73
  124. package/src/pi/grep.test.ts +0 -1670
  125. package/src/pi/model-budgets.test.ts +0 -96
  126. package/src/pi/mutation-outcome.testing.ts +0 -20
  127. package/src/pi/mutation-result.test.ts +0 -96
  128. package/src/pi/output-budgets.test.ts +0 -221
  129. package/src/pi/pi.test.ts +0 -158
  130. package/src/pi/read-delegation.test.ts +0 -119
  131. package/src/pi/read.test.ts +0 -735
  132. package/src/pi/regex-worker.test.ts +0 -42
  133. package/src/pi/registration.test.ts +0 -549
  134. package/src/pi/render.test.ts +0 -841
  135. package/src/pi/replace.test.ts +0 -634
  136. package/src/pi/report-failure.testing.ts +0 -27
  137. package/src/pi/report.test.ts +0 -533
  138. package/src/pi/result-safety.test.ts +0 -597
  139. package/src/pi/rg-process.test.ts +0 -133
  140. package/src/pi/rg-regex.test.ts +0 -40
  141. package/src/pi/text-stream.test.ts +0 -281
  142. package/src/pi/tool-call.testing.ts +0 -61
  143. package/src/pi/tool-factories.testing.ts +0 -59
  144. package/src/pi/write.test.ts +0 -256
  145. package/src/testing/extension-loader.testing.ts +0 -24
  146. package/src/testing/installation.testing.ts +0 -77
  147. package/src/testing/process.testing.ts +0 -33
  148. package/src/testing/regex-host.testing.ts +0 -20
  149. package/src/testing/regex-translation-corpus.ts +0 -267
  150. package/src/testing/rg-line-listing.fixture.mjs +0 -7
  151. package/src/testing/run-tests.ts +0 -47
  152. package/src/testing/session.testing.ts +0 -127
@@ -1,644 +0,0 @@
1
- import { test } from "node:test";
2
- import assert from "node:assert/strict";
3
- import { computeLineHash } from "./hash.ts";
4
- import { splitLines } from "./lines.ts";
5
- import { applyEdits } from "./apply.ts";
6
- import { HashlineError } from "./errors.ts";
7
- import type { Anchor, Edit } from "./types.ts";
8
-
9
- /** Live anchor: hash the current content at the given line (1-based). */
10
- function at(text: string, line: number): Anchor {
11
- return { line, hash: computeLineHash(line, splitLines(text)[line - 1], 4) };
12
- }
13
-
14
- // --- happy paths ---
15
-
16
- test("replace single line", () => {
17
- const text = "a\nb\nc\n";
18
- const r = applyEdits(text, [{ op: "replace", anchor: at(text, 2), body: ["B"] }], 4, 15);
19
- assert.equal(r.ok, true);
20
- if (r.ok) assert.equal(r.text, "a\nB\nc\n");
21
- });
22
-
23
- test("editing another line preserves standalone carriage returns at EOF", () => {
24
- for (const ending of ["\n", "\r\n"]) {
25
- const text = `first${ending}last\r`;
26
- const r = applyEdits(text, [{ op: "replace", anchor: at(text, 1), body: ["changed"] }], 4, 15);
27
- assert.equal(r.ok, true);
28
- if (r.ok) assert.equal(r.text, `changed${ending}last\r`);
29
- }
30
- });
31
-
32
- test("replace range", () => {
33
- const text = "a\nb\nc\nd\ne\n";
34
- const r = applyEdits(
35
- text,
36
- [{ op: "replace", anchor: at(text, 2), end: at(text, 4), body: ["X", "Y"] }],
37
- 4,
38
- 15,
39
- );
40
- assert.equal(r.ok, true);
41
- if (r.ok) assert.equal(r.text, "a\nX\nY\ne\n");
42
- });
43
-
44
- test("delete single line / range", () => {
45
- const text = "a\nb\nc\nd\n";
46
- const r = applyEdits(text, [{ op: "delete", anchor: at(text, 2), end: at(text, 3) }], 4, 15);
47
- assert.equal(r.ok, true);
48
- if (r.ok) assert.equal(r.text, "a\nd\n");
49
- });
50
-
51
- test("insert_after", () => {
52
- const text = "a\nb\n";
53
- const r = applyEdits(text, [{ op: "insert_after", anchor: at(text, 1), body: ["x"] }], 4, 15);
54
- assert.equal(r.ok, true);
55
- if (r.ok) assert.equal(r.text, "a\nx\nb\n");
56
- });
57
-
58
- test("insert_before", () => {
59
- const text = "a\nb\n";
60
- const r = applyEdits(text, [{ op: "insert_before", anchor: at(text, 2), body: ["x"] }], 4, 15);
61
- assert.equal(r.ok, true);
62
- if (r.ok) assert.equal(r.text, "a\nx\nb\n");
63
- });
64
-
65
- test("append / prepend", () => {
66
- const text = "a\nb\n";
67
- const r = applyEdits(
68
- text,
69
- [
70
- { op: "prepend", body: ["head"] },
71
- { op: "append", body: ["tail"] },
72
- ],
73
- 4,
74
- 15,
75
- );
76
- assert.equal(r.ok, true);
77
- if (r.ok) assert.equal(r.text, "head\na\nb\ntail\n");
78
- });
79
-
80
- test("multiple out-of-order ops → applied at the right positions", () => {
81
- const text = "a\nb\nc\n";
82
- const r = applyEdits(
83
- text,
84
- [
85
- { op: "insert_after", anchor: at(text, 3), body: ["z"] },
86
- { op: "replace", anchor: at(text, 1), body: ["A"] },
87
- ],
88
- 4,
89
- 15,
90
- );
91
- assert.equal(r.ok, true);
92
- if (r.ok) assert.equal(r.text, "A\nb\nc\nz\n");
93
- });
94
-
95
- // --- error paths ---
96
-
97
- test("line out of range rejected", () => {
98
- const text = "a\n";
99
- const r = applyEdits(
100
- text,
101
- [{ op: "replace", anchor: { line: 5, hash: computeLineHash(1, "a", 4) }, body: ["x"] }],
102
- 4,
103
- 15,
104
- );
105
- assert.equal(r.ok, false);
106
- });
107
-
108
- test("anchor mismatch when the cited line's content differs (live verification)", () => {
109
- // hash for line 2's content, but cited as line 1 → must fail because line 1's live content differs
110
- const text = "a\nb\n";
111
- const r = applyEdits(
112
- text,
113
- [{ op: "replace", anchor: { line: 1, hash: computeLineHash(2, "b", 4) }, body: ["x"] }],
114
- 4,
115
- 15,
116
- );
117
- assert.equal(r.ok, false);
118
- });
119
-
120
- test("overlapping edit spans → report conflicting operations in input order", () => {
121
- const text = "a\nb\nc\nd\ne\nf\n";
122
- const range: Edit = { op: "replace", anchor: at(text, 2), end: at(text, 3), body: ["x"] };
123
- const cases: Edit[][] = [
124
- [range, { op: "replace", anchor: at(text, 3), body: ["y"] }],
125
- [range, { op: "delete", anchor: at(text, 2) }],
126
- [range, { op: "insert_after", anchor: at(text, 2), body: ["y"] }],
127
- ];
128
- for (const edits of cases) {
129
- for (const pair of [edits, [...edits].reverse()]) {
130
- const batch: Edit[] = [{ op: "append", body: ["tail"] }, ...pair];
131
- assert.throws(
132
- () => applyEdits(text, batch, 4, 0),
133
- (error: unknown) => {
134
- assert(error instanceof HashlineError);
135
- assert.equal(error.errorCode, "OVERLAPPING_EDITS");
136
- assert.equal(error.message, "Two steps overlap.");
137
- assert.deepEqual(error.facts?.conflict, ["steps[1]", "steps[2]"]);
138
- return true;
139
- },
140
- );
141
- }
142
- }
143
- });
144
-
145
- test("insertion at a replaced or deleted range start → insert before the range in either batch order", () => {
146
- const text = "a\nb\nc\nd\n";
147
- const insertion: Edit = { op: "insert_after", anchor: at(text, 1), body: ["x"] };
148
- for (const [edit, expected] of [
149
- [{ op: "replace", anchor: at(text, 2), end: at(text, 3), body: ["B"] }, "a\nx\nB\nd\n"],
150
- [{ op: "delete", anchor: at(text, 2), end: at(text, 3) }, "a\nx\nd\n"],
151
- ] satisfies [Edit, string][]) {
152
- for (const edits of [
153
- [insertion, edit],
154
- [edit, insertion],
155
- ]) {
156
- const result = applyEdits(text, edits, 4, 0);
157
- assert(result.ok);
158
- assert.equal(result.text, expected);
159
- }
160
- }
161
- });
162
-
163
- test("insertions sharing a gap → reject the batch", () => {
164
- const text = "a\nb\nc\nd\n";
165
- const cases: Edit[][] = [
166
- [
167
- { op: "insert_after", anchor: at(text, 2), body: ["x"] },
168
- { op: "insert_before", anchor: at(text, 3), body: ["y"] },
169
- ],
170
- [
171
- { op: "insert_before", anchor: at(text, 3), body: ["x"] },
172
- { op: "insert_before", anchor: at(text, 3), body: ["y"] },
173
- ],
174
- [
175
- { op: "append", body: ["x"] },
176
- { op: "append", body: ["y"] },
177
- ],
178
- [
179
- { op: "prepend", body: ["x"] },
180
- { op: "insert_before", anchor: at(text, 1), body: ["y"] },
181
- ],
182
- ];
183
- for (const edits of cases) {
184
- for (const batch of [edits, [...edits].reverse()]) {
185
- assert.throws(
186
- () => applyEdits(text, batch, 4, 0),
187
- (error: unknown) => {
188
- assert(error instanceof HashlineError);
189
- assert.equal(error.errorCode, "OVERLAPPING_EDITS");
190
- assert.equal(error.message, "Two steps insert at one position.");
191
- assert.deepEqual(error.facts?.conflict, ["steps[0]", "steps[1]"]);
192
- return true;
193
- },
194
- );
195
- }
196
- }
197
- });
198
-
199
- test("byte-identical edits → validate anchors and preserve the source bytes", () => {
200
- for (const text of ["a\nb\n", "\uFEFFa\r\nb\n"]) {
201
- const body = text.startsWith("\uFEFF") ? "\uFEFFa" : "a";
202
- const r = applyEdits(text, [{ op: "replace", anchor: at(text, 1), body: [body] }], 4, 15);
203
- assert.ok(r.ok);
204
- assert.equal(r.text, text);
205
- const invalid = applyEdits(
206
- text,
207
- [{ op: "replace", anchor: at("wrong\nb\n", 1), body: [body] }],
208
- 4,
209
- 15,
210
- );
211
- assert.ok(!invalid.ok);
212
- }
213
- });
214
-
215
- test("unrelated change elsewhere does NOT block the edit (no global stale check)", () => {
216
- // read-time text and current text differ at line 3, but we only touch line 1 — must succeed
217
- const readText = "a\nb\nc\n";
218
- const currentText = "a\nb\nCHANGED\n";
219
- const r = applyEdits(
220
- currentText,
221
- [{ op: "replace", anchor: at(readText, 1), body: ["A"] }],
222
- 4,
223
- 15,
224
- );
225
- assert.equal(r.ok, true);
226
- if (r.ok) assert.equal(r.text, "A\nb\nCHANGED\n");
227
- });
228
-
229
- test("CRLF line endings preserved", () => {
230
- const text = "a\r\nb\r\n";
231
- const r = applyEdits(text, [{ op: "replace", anchor: at(text, 1), body: ["A"] }], 4, 15);
232
- assert.equal(r.ok, true);
233
- if (r.ok) assert.equal(r.text, "A\r\nb\r\n");
234
- });
235
-
236
- test("editing a mixed-ending file preserves untouched separators", () => {
237
- const before = "first\r\nsecond\nthird\r\n";
238
- const result = applyEdits(
239
- before,
240
- [{ op: "replace", anchor: at(before, 2), body: ["SECOND"] }],
241
- 4,
242
- 15,
243
- );
244
- assert.equal(result.ok, true);
245
- if (result.ok) assert.equal(result.text, "first\r\nSECOND\nthird\r\n");
246
- });
247
-
248
- test("inserting and deleting in mixed-ending files preserves surviving separators", () => {
249
- const before = "a\r\nb\nc\r\nlast";
250
- const inserted = applyEdits(
251
- before,
252
- [{ op: "insert_after", anchor: at(before, 2), body: ["x"] }],
253
- 4,
254
- 15,
255
- );
256
- assert.equal(inserted.ok, true);
257
- if (inserted.ok) assert.equal(inserted.text, "a\r\nb\nx\r\nc\r\nlast");
258
- const deleted = applyEdits(before, [{ op: "delete", anchor: at(before, 2) }], 4, 15);
259
- assert.equal(deleted.ok, true);
260
- if (deleted.ok) assert.equal(deleted.text, "a\r\nc\r\nlast");
261
- });
262
-
263
- test("replacing one line with several keeps the block's trailing gap", () => {
264
- const before = "a\r\nb\nc\r\nd";
265
- const result = applyEdits(
266
- before,
267
- [{ op: "replace", anchor: at(before, 2), body: ["X", "Y", "Z"] }],
268
- 4,
269
- 15,
270
- );
271
- assert.equal(result.ok, true);
272
- // b's gap (\n) moves to the last new line, so the boundary to c is unchanged;
273
- // b has no internal gap; both new internal gaps use the file style, like replace("b", "X\nY\nZ").
274
- if (result.ok) assert.equal(result.text, "a\r\nX\r\nY\r\nZ\nc\r\nd");
275
- });
276
-
277
- test("a file without a final newline keeps not having one", () => {
278
- const text = "a\nb";
279
- const r = applyEdits(text, [{ op: "replace", anchor: at(text, 2), body: ["B"] }], 4, 15);
280
- assert.equal(r.ok, true);
281
- if (r.ok) assert.equal(r.text, "a\nB");
282
- });
283
-
284
- test("CRLF without a final newline keeps not having one", () => {
285
- const text = "a\r\nb";
286
- const r = applyEdits(text, [{ op: "replace", anchor: at(text, 1), body: ["A"] }], 4, 15);
287
- assert.equal(r.ok, true);
288
- if (r.ok) assert.equal(r.text, "A\r\nb");
289
- });
290
-
291
- test("appending to a file without a final newline keeps it absent", () => {
292
- const text = "a";
293
- const r = applyEdits(text, [{ op: "append", body: ["b"] }], 4, 15);
294
- assert.equal(r.ok, true);
295
- if (r.ok) assert.equal(r.text, "a\nb");
296
- });
297
-
298
- test("edits retain empty final logical lines even without an original final newline", () => {
299
- for (const text of ["old", "head\nold", "head\r\nold"]) {
300
- const line = splitLines(text).length;
301
- const ending = text.includes("\r\n") ? "\r\n" : "\n";
302
- const prefix = text.slice(0, text.lastIndexOf("\n") + 1);
303
- const cases: [Edit, string][] = [
304
- [{ op: "replace", anchor: at(text, line), body: [""] }, prefix + ending],
305
- [
306
- { op: "replace", anchor: at(text, line), body: ["new", ""] },
307
- `${prefix}new${ending}${ending}`,
308
- ],
309
- [{ op: "append", body: [""] }, text + ending + ending],
310
- [{ op: "insert_after", anchor: at(text, line), body: [""] }, text + ending + ending],
311
- ];
312
- for (const [edit, expected] of cases) {
313
- const result = applyEdits(text, [edit], 4, 15);
314
- assert.ok(result.ok);
315
- assert.equal(result.text, expected, `${edit.op}: ${JSON.stringify(text)}`);
316
- }
317
- }
318
- // Deleting an unterminated suffix must also retain an existing blank predecessor.
319
- for (const text of ["\nlast", "head\r\n\r\nlast"]) {
320
- const result = applyEdits(
321
- text,
322
- [{ op: "delete", anchor: at(text, splitLines(text).length) }],
323
- 4,
324
- 15,
325
- );
326
- assert.ok(result.ok);
327
- assert.equal(result.text, text.slice(0, -"last".length));
328
- }
329
- });
330
-
331
- test("a BOM represents an empty first line without a terminator, but not an empty later line", () => {
332
- const cases: [string, Edit[], string][] = [
333
- ["\uFEFFold", [{ op: "replace", anchor: at("\uFEFFold", 1), body: [""] }], "\uFEFF"],
334
- ["\uFEFF", [{ op: "replace", anchor: at("\uFEFF", 1), body: [""] }], "\uFEFF"],
335
- ["\uFEFF", [{ op: "append", body: [""] }], "\uFEFF\n\n"],
336
- ["\uFEFF", [{ op: "prepend", body: ["new"] }], "\uFEFFnew\n\n"],
337
- ["\uFEFF\nlast", [{ op: "delete", anchor: at("\uFEFF\nlast", 2) }], "\uFEFF"],
338
- ];
339
- for (const [text, edits, expected] of cases) {
340
- const result = applyEdits(text, edits, 4, 15);
341
- assert.ok(result.ok);
342
- assert.equal(result.text, expected, JSON.stringify({ text, edits }));
343
- }
344
- });
345
-
346
- test("large replacement bodies retain snapshot boundaries without a function argument limit", () => {
347
- const text = "head\r\nold\ntail";
348
- const body = Array<string>(150_000).fill("x");
349
- const result = applyEdits(text, [{ op: "replace", anchor: at(text, 2), body }], 4, 15);
350
- assert.ok(result.ok);
351
- assert.equal(result.text, `head\r\n${"x\r\n".repeat(body.length - 1)}x\ntail`);
352
- });
353
-
354
- test("noop is still detected when the file lacks a final newline", () => {
355
- const text = "a\nb";
356
- const r = applyEdits(text, [{ op: "replace", anchor: at(text, 1), body: ["a"] }], 4, 15);
357
- assert.ok(r.ok);
358
- assert.equal(r.text, text);
359
- });
360
-
361
- // --- shifted-anchor recovery ---
362
-
363
- test("rescued anchor lets the retry succeed without a re-read", () => {
364
- const readText = "a\nb\nc\nd\ne\n";
365
- const currentText = "a\nX\nb\nc\nd\ne\n";
366
- // first attempt with a stale anchor → rescued
367
- const r1 = applyEdits(
368
- currentText,
369
- [{ op: "replace", anchor: at(readText, 3), body: ["C"] }],
370
- 4,
371
- 15,
372
- );
373
- assert.ok(!r1.ok);
374
- const f = r1.failures[0];
375
- assert.ok(f.recovery.kind === "found");
376
- assert.equal(f.recovery.newLine, 4);
377
- assert.equal(computeLineHash(4, splitLines(currentText)[3], 4), f.recovery.newHash);
378
- // Retry with the recovered anchor; the rest of the file stays unchanged.
379
- const r2 = applyEdits(
380
- currentText,
381
- [
382
- {
383
- op: "replace",
384
- anchor: { line: f.recovery.newLine, hash: f.recovery.newHash },
385
- body: ["C"],
386
- },
387
- ],
388
- 4,
389
- 15,
390
- );
391
- assert.ok(r2.ok);
392
- assert.equal(r2.text, "a\nX\nb\nC\nd\ne\n");
393
- });
394
-
395
- test("shifted recovery: duplicate content → ambiguous candidates", () => {
396
- const readText = "a\nx\nb\nx\nc\n";
397
- const currentText = "a\nY\nx\nb\nx\nc\n"; // both "x" shifted
398
- const r = applyEdits(
399
- currentText,
400
- [{ op: "replace", anchor: at(readText, 2), body: ["Z"] }],
401
- 4,
402
- 15,
403
- );
404
- assert.ok(!r.ok);
405
- const f = r.failures[0];
406
- assert.ok(f.recovery.kind === "ambiguous");
407
- assert.deepEqual(
408
- f.recovery.candidates.map((c) => c.line),
409
- [3, 5],
410
- );
411
- });
412
-
413
- test("shifted recovery: content genuinely changed → none", () => {
414
- const readText = "a\nb\nc\n";
415
- const currentText = "a\nBCHANGED\nc\n"; // "b" is gone
416
- const r = applyEdits(
417
- currentText,
418
- [{ op: "replace", anchor: at(readText, 2), body: ["B"] }],
419
- 4,
420
- 15,
421
- );
422
- assert.ok(!r.ok);
423
- const f = r.failures[0];
424
- assert.equal(f.recovery.kind, "none");
425
- });
426
-
427
- test("full-file recovery finds distant content and anchors beyond the current EOF", () => {
428
- for (const [oldLine, newLine] of [
429
- [2, 90],
430
- [90, 2],
431
- [190, 2],
432
- ]) {
433
- const lines = Array.from({ length: 100 }, () => "filler");
434
- lines[newLine - 1] = "target";
435
- const result = applyEdits(
436
- lines.join("\n"),
437
- [{ op: "delete", anchor: { line: oldLine, hash: computeLineHash(oldLine, "target", 4) } }],
438
- 4,
439
- 15,
440
- );
441
- assert.ok(!result.ok);
442
- assert.deepEqual(result.failures[0].recovery, {
443
- kind: "found",
444
- newLine,
445
- newHash: computeLineHash(newLine, "target", 4),
446
- });
447
- }
448
- });
449
-
450
- test("full-file recovery collects both sides while preserving local candidate priority", () => {
451
- for (const { positions, selected } of [
452
- { positions: [3, 95], selected: [3, 95] },
453
- { positions: [3, 49, 95], selected: [49] },
454
- { positions: [3, 49, 51, 95], selected: [49, 51] },
455
- ]) {
456
- const lines = Array.from({ length: 100 }, () => "filler");
457
- for (const line of positions) lines[line - 1] = "target";
458
- const result = applyEdits(
459
- lines.join("\n"),
460
- [{ op: "delete", anchor: { line: 50, hash: computeLineHash(50, "target", 4) } }],
461
- 4,
462
- 15,
463
- );
464
- assert.ok(!result.ok);
465
- assert.deepEqual(
466
- result.failures[0].recovery,
467
- selected.length === 1
468
- ? {
469
- kind: "found",
470
- newLine: selected[0],
471
- newHash: computeLineHash(selected[0], "target", 4),
472
- }
473
- : {
474
- kind: "ambiguous",
475
- candidates: selected.map((line) => ({
476
- line,
477
- hash: computeLineHash(line, "target", 4),
478
- })),
479
- },
480
- );
481
- }
482
- });
483
-
484
- test("two stale anchors and overlapping verified edits → return both anchor failures first", () => {
485
- const readText = "a\nb\nc\nd\n";
486
- const currentText = "X\na\nb\nc\nd\n"; // inserted X at top → all shifted +1
487
- const r = applyEdits(
488
- currentText,
489
- [
490
- { op: "replace", anchor: at(readText, 2), body: ["B"] },
491
- { op: "replace", anchor: at(readText, 4), body: ["D"] },
492
- { op: "delete", anchor: at(currentText, 1) },
493
- { op: "replace", anchor: at(currentText, 1), body: ["conflict"] },
494
- ],
495
- 4,
496
- 15,
497
- );
498
- assert.ok(!r.ok);
499
- assert.equal(r.failures.length, 2);
500
- const found = r.failures.map((f) => (f.recovery.kind === "found" ? f.recovery.newLine : -1));
501
- assert.deepEqual(found, [3, 5]);
502
- });
503
-
504
- test("shiftRadius=0 disables rescue (always none)", () => {
505
- const readText = "a\nb\nc\nd\ne\n";
506
- const currentText = "a\nX\nb\nc\nd\ne\n";
507
- const r = applyEdits(
508
- currentText,
509
- [{ op: "replace", anchor: at(readText, 3), body: ["C"] }],
510
- 4,
511
- 0,
512
- );
513
- assert.ok(!r.ok);
514
- assert.equal(r.failures[0].recovery.kind, "none");
515
- });
516
-
517
- test("BOM stays at byte zero through first-line edits while anchors retain their original hashes", () => {
518
- for (const text of [
519
- "\uFEFFfirst\nsecond\n",
520
- "\uFEFFfirst\r\nsecond\r\n",
521
- "\uFEFFfirst\r\nsecond\n",
522
- "\uFEFFfirst\nsecond",
523
- ]) {
524
- const ending = text.includes("\r\n") ? "\r\n" : "\n";
525
- const rest = text.slice(text.indexOf("\n") + 1);
526
- const cases: [Edit, string][] = [
527
- [{ op: "replace", anchor: at(text, 1), body: ["changed"] }, `\uFEFFchanged${ending}${rest}`],
528
- [
529
- { op: "replace", anchor: at(text, 1), body: ["\uFEFFchanged"] },
530
- `\uFEFFchanged${ending}${rest}`,
531
- ],
532
- [{ op: "delete", anchor: at(text, 1) }, `\uFEFF${rest}`],
533
- [{ op: "prepend", body: ["new"] }, `\uFEFFnew${ending}${text.slice(1)}`],
534
- [
535
- { op: "insert_before", anchor: at(text, 1), body: ["new"] },
536
- `\uFEFFnew${ending}${text.slice(1)}`,
537
- ],
538
- [{ op: "delete", anchor: at(text, 1), end: at(text, 2) }, "\uFEFF"],
539
- ];
540
- for (const [edit, expected] of cases) {
541
- const result = applyEdits(text, [edit], 4, 15);
542
- assert.ok(result.ok);
543
- assert.equal(result.text, expected, `${edit.op}: ${JSON.stringify(text)}`);
544
- }
545
- }
546
- for (const text of ["\uFEFF", "\uFEFFfirst"]) {
547
- const result = applyEdits(
548
- text,
549
- [{ op: "replace", anchor: at(text, 1), body: ["changed"] }],
550
- 4,
551
- 15,
552
- );
553
- assert.ok(result.ok);
554
- assert.equal(result.text, "\uFEFFchanged");
555
- }
556
- const embedded = "\uFEFFfirst\ninside\uFEFFcontent\n";
557
- const result = applyEdits(
558
- embedded,
559
- [{ op: "replace", anchor: at(embedded, 1), body: ["changed"] }],
560
- 4,
561
- 15,
562
- );
563
- assert.ok(result.ok);
564
- assert.equal(result.text, "\uFEFFchanged\ninside\uFEFFcontent\n");
565
- });
566
-
567
- test("inserting U+FEFF before the first line → keeps it as content beside the file BOM", () => {
568
- const text = "\uFEFFfirst\n";
569
- const body = ["\uFEFFnew"];
570
- for (const edit of [
571
- { op: "prepend", body },
572
- { op: "insert_before", anchor: at(text, 1), body },
573
- ] satisfies Edit[]) {
574
- const result = applyEdits(text, [edit], 4, 0);
575
- assert.ok(result.ok);
576
- assert.equal(result.text, "\uFEFF\uFEFFnew\nfirst\n", edit.op);
577
- }
578
- });
579
-
580
- test("replacing a first-line mixed-ending range → preserve gaps and keep one file BOM", () => {
581
- const text = "\uFEFFA\r\nB\nC\r\nD\n";
582
- const result = applyEdits(
583
- text,
584
- [{ op: "replace", anchor: at(text, 1), end: at(text, 3), body: ["X", "Y", "Z"] }],
585
- 4,
586
- 0,
587
- );
588
- assert(result.ok);
589
- assert.equal(result.text, "\uFEFFX\r\nY\nZ\r\nD\n");
590
- });
591
-
592
- test("inserting beside a line ending in standalone CR → connector retains the CR as content", () => {
593
- const text = "A\nB\r";
594
- const cases: [Edit, string][] = [
595
- [{ op: "append", body: ["C"] }, "A\nB\r\r\nC"],
596
- [{ op: "prepend", body: ["B\r"] }, "B\r\r\nA\nB\r"],
597
- ];
598
- for (const [edit, expected] of cases) {
599
- const result = applyEdits(text, [edit], 4, 0);
600
- assert(result.ok);
601
- assert.equal(result.text, expected);
602
- assert.equal(splitLines(result.text)[edit.op === "append" ? 1 : 0], "B\r");
603
- }
604
- });
605
-
606
- test("editing beside Unicode and standalone-CR content → preserve every untouched code unit", () => {
607
- const text = "🙂中文 e\u0301\nkeep\rcontent\n";
608
- const result = applyEdits(text, [{ op: "append", body: ["tail"] }], 4, 0);
609
- assert(result.ok);
610
- assert.equal(result.text, text + "tail\n");
611
- });
612
-
613
- test("appending a blank line to a terminated blank line → retain the added logical line", () => {
614
- const text = "A\n\n";
615
- const result = applyEdits(text, [{ op: "append", body: [""] }], 4, 0);
616
- assert(result.ok);
617
- assert.equal(result.text, "A\n\n\n");
618
- assert.equal(splitLines(result.text).length, splitLines(text).length + 1);
619
- });
620
-
621
- test("bad range anchors → aggregate anchor and end failures for every operation", () => {
622
- const text = "A\nB\nC\nD\n";
623
- const anchor = { line: 1, hash: "XXXX" };
624
- const end = { line: 2, hash: "XXXX" };
625
- const result = applyEdits(
626
- text,
627
- [
628
- { op: "replace", anchor, end, body: ["new"] },
629
- { op: "delete", anchor, end },
630
- ],
631
- 4,
632
- 0,
633
- );
634
- assert(!result.ok);
635
- assert.deepEqual(
636
- result.failures.map(({ opIndex, op, which }) => ({ opIndex, op, which })),
637
- [
638
- { opIndex: 0, op: "replace", which: "anchor" },
639
- { opIndex: 0, op: "replace", which: "end" },
640
- { opIndex: 1, op: "delete", which: "anchor" },
641
- { opIndex: 1, op: "delete", which: "end" },
642
- ],
643
- );
644
- });
@@ -1,35 +0,0 @@
1
- import assert from "node:assert/strict";
2
- import { test } from "node:test";
3
- import { caseVariants } from "./case-fold.ts";
4
- import { MAX_UNIT } from "./character-set.ts";
5
-
6
- for (const [char, expected] of [
7
- ["k", ["K", "k"]],
8
- ["s", ["S", "s"]],
9
- ["σ", ["Σ", "ς", "σ"]],
10
- ["ß", ["ß"]],
11
- ["😀", ["😀"]],
12
- ] as const) {
13
- test(`${JSON.stringify(char)} → native case variants`, () => {
14
- assert.deepEqual(caseVariants(char), expected);
15
- const native = new RegExp(`^${char}$`, "i");
16
- for (const candidate of ["K", "k", "K", "S", "s", "ſ", "Σ", "σ", "ς", "ß", "é", "😀"])
17
- assert.equal(
18
- expected.some((value) => value === candidate),
19
- native.test(candidate),
20
- candidate,
21
- );
22
- });
23
- }
24
-
25
- test("Cartesian non-Unicode code units → case variants agree with native RegExp", () => {
26
- const allUnits = Array.from({ length: MAX_UNIT + 1 }, (_, unit) =>
27
- String.fromCharCode(unit),
28
- ).join("");
29
- for (let unit = 0; unit <= MAX_UNIT; unit++) {
30
- const char = String.fromCharCode(unit);
31
- const source = `\\u${unit.toString(16).padStart(4, "0")}`;
32
- const expected = allUnits.match(new RegExp(source, "gi"));
33
- assert.deepEqual(caseVariants(char), expected, source);
34
- }
35
- });