@criogaid/pi-hashline-edit 0.8.0 → 0.10.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 (118) hide show
  1. package/README.md +13 -10
  2. package/README.zh-CN.md +12 -9
  3. package/THIRD_PARTY_NOTICES.md +0 -6
  4. package/package.json +1 -1
  5. package/src/core/AGENTS.md +4 -5
  6. package/src/core/apply.test.ts +118 -293
  7. package/src/core/apply.ts +63 -165
  8. package/src/core/case-fold.ts +8 -67
  9. package/src/core/character-set.ts +4 -4
  10. package/src/core/errors.test.ts +23 -7
  11. package/src/core/errors.ts +32 -5
  12. package/src/core/hash.test.ts +8 -16
  13. package/src/core/hash.ts +3 -23
  14. package/src/core/lines.ts +2 -4
  15. package/src/core/ranges.test.ts +16 -8
  16. package/src/core/ranges.ts +10 -6
  17. package/src/core/regex.ts +3 -4
  18. package/src/core/replace.test.ts +47 -1
  19. package/src/core/replace.ts +19 -13
  20. package/src/core/search-analysis.test.ts +24 -27
  21. package/src/core/search-analysis.ts +34 -65
  22. package/src/core/search-matrix.testing.ts +0 -2
  23. package/src/core/search.ts +1 -1
  24. package/src/core/text.test.ts +5 -5
  25. package/src/core/text.ts +5 -12
  26. package/src/core/types.ts +7 -43
  27. package/src/integration/config-registration.test.ts +2 -2
  28. package/src/integration/fusion-dispatch.test.ts +2 -5
  29. package/src/integration/grep-flags.test.ts +0 -14
  30. package/src/integration/grep-js-completeness.test.ts +34 -8
  31. package/src/integration/grep-line-candidates.test.ts +4 -8
  32. package/src/integration/grep-matrix.test.ts +3 -7
  33. package/src/integration/grep-path-matrix.test.ts +5 -5
  34. package/src/integration/grep-prefilter-coverage.test.ts +3 -7
  35. package/src/integration/grep-process-limits.test.ts +78 -0
  36. package/src/integration/grep-rg.test.ts +123 -25
  37. package/src/integration/grep-scan-budget.test.ts +2 -2
  38. package/src/integration/grep-translation.test.ts +12 -27
  39. package/src/integration/large-text.test.ts +1 -47
  40. package/src/integration/model-budget-lifecycle.test.ts +13 -3
  41. package/src/integration/mutation-file-types.testing.ts +1 -1
  42. package/src/integration/npm-package.testing.ts +1 -1
  43. package/src/integration/publication-read-changes.test.ts +125 -47
  44. package/src/integration/replace-worker-boundary.test.ts +21 -11
  45. package/src/integration/report-replay.test.ts +21 -23
  46. package/src/integration/utf8-errors.test.ts +1 -1
  47. package/src/pi/AGENTS.md +16 -16
  48. package/src/pi/action-fusion.test.ts +168 -300
  49. package/src/pi/action-fusion.ts +148 -111
  50. package/src/pi/anchor-format.test.ts +9 -9
  51. package/src/pi/anchor-format.ts +6 -8
  52. package/src/pi/argument-diagnostics.ts +52 -207
  53. package/src/pi/argument-error.ts +2 -1
  54. package/src/pi/argument-format.test.ts +20 -22
  55. package/src/pi/argument-validation.test.ts +382 -418
  56. package/src/pi/argument-validation.ts +5 -8
  57. package/src/pi/budgets.ts +5 -4
  58. package/src/pi/config.test.ts +5 -2
  59. package/src/pi/config.ts +4 -6
  60. package/src/pi/diagnostic-buffer.test.ts +12 -1
  61. package/src/pi/diagnostic-buffer.ts +1 -2
  62. package/src/pi/directory-sync.test.ts +0 -1
  63. package/src/pi/edit-tool.ts +173 -226
  64. package/src/pi/error-text.ts +4 -8
  65. package/src/pi/execute.test.ts +165 -266
  66. package/src/pi/failure-context.test.ts +144 -126
  67. package/src/pi/failure-context.ts +45 -94
  68. package/src/pi/failure-path.test.ts +168 -141
  69. package/src/pi/file-commit.test.ts +110 -53
  70. package/src/pi/file-commit.ts +90 -137
  71. package/src/pi/file-snapshot.test.ts +3 -5
  72. package/src/pi/fusion-card.test.ts +4 -1
  73. package/src/pi/fusion-card.ts +16 -24
  74. package/src/pi/grep-candidates.test.ts +2 -101
  75. package/src/pi/grep-candidates.ts +23 -69
  76. package/src/pi/grep-matrix.test.ts +2 -2
  77. package/src/pi/grep-order.test.ts +4 -4
  78. package/src/pi/grep-output.ts +62 -96
  79. package/src/pi/grep-render.ts +1 -1
  80. package/src/pi/grep-reread.ts +92 -0
  81. package/src/pi/grep-scope.ts +40 -42
  82. package/src/pi/grep-search.ts +22 -31
  83. package/src/pi/grep-tool.ts +55 -82
  84. package/src/pi/grep.test.ts +81 -46
  85. package/src/pi/model-budgets.test.ts +25 -23
  86. package/src/pi/model-budgets.ts +6 -38
  87. package/src/pi/mutation-result.test.ts +44 -35
  88. package/src/pi/mutation-result.ts +51 -20
  89. package/src/pi/mutation-runner.ts +126 -70
  90. package/src/pi/output-budgets.test.ts +38 -16
  91. package/src/pi/path.ts +1 -1
  92. package/src/pi/pi.test.ts +3 -1
  93. package/src/pi/read-tool.ts +5 -5
  94. package/src/pi/regex-worker-contract.ts +3 -13
  95. package/src/pi/regex-worker.ts +1 -10
  96. package/src/pi/registration.test.ts +82 -20
  97. package/src/pi/render.test.ts +79 -42
  98. package/src/pi/render.ts +69 -63
  99. package/src/pi/replace-regex.ts +14 -2
  100. package/src/pi/replace-tool.ts +22 -83
  101. package/src/pi/replace-worker.ts +1 -1
  102. package/src/pi/replace.test.ts +9 -5
  103. package/src/pi/report-cause.ts +3 -8
  104. package/src/pi/report-failure.testing.ts +1 -2
  105. package/src/pi/report-schema.ts +65 -81
  106. package/src/pi/report.test.ts +171 -25
  107. package/src/pi/report.ts +33 -87
  108. package/src/pi/result-safety.test.ts +177 -189
  109. package/src/pi/rg-process.test.ts +77 -63
  110. package/src/pi/rg-process.ts +48 -64
  111. package/src/pi/rg-regex.ts +40 -400
  112. package/src/pi/schema.ts +12 -0
  113. package/src/pi/text-stream.test.ts +0 -21
  114. package/src/pi/text-stream.ts +14 -21
  115. package/src/pi/write-tool.ts +33 -62
  116. package/src/pi/write.test.ts +1 -3
  117. package/src/testing/regex-translation-corpus.ts +12 -12
  118. package/src/pi/replace-worker-contract.ts +0 -10
@@ -3,6 +3,7 @@ import assert from "node:assert/strict";
3
3
  import { computeLineHash } from "./hash.ts";
4
4
  import { splitLines } from "./lines.ts";
5
5
  import { applyEdits } from "./apply.ts";
6
+ import { HashlineError } from "./errors.ts";
6
7
  import type { Anchor, Edit } from "./types.ts";
7
8
 
8
9
  /** Live anchor: hash the current content at the given line (1-based). */
@@ -102,7 +103,6 @@ test("line out of range rejected", () => {
102
103
  15,
103
104
  );
104
105
  assert.equal(r.ok, false);
105
- if (!r.ok) assert.equal(r.failure.kind, "anchor");
106
106
  });
107
107
 
108
108
  test("anchor mismatch when the cited line's content differs (live verification)", () => {
@@ -115,36 +115,30 @@ test("anchor mismatch when the cited line's content differs (live verification)"
115
115
  15,
116
116
  );
117
117
  assert.equal(r.ok, false);
118
- if (!r.ok) assert.equal(r.failure.kind, "anchor");
119
118
  });
120
119
 
121
- test("reverse-order range rejected", () => {
122
- const text = "a\nb\nc\n";
123
- const r = applyEdits(
124
- text,
125
- [{ op: "replace", anchor: at(text, 3), end: at(text, 1), body: ["x"] }],
126
- 4,
127
- 15,
128
- );
129
- assert.equal(r.ok, false);
130
- if (!r.ok) assert.equal(r.failure.kind, "range");
131
- });
132
-
133
- test("range starts inside a range or shares its start → reject overlapping edits", () => {
134
- const text = "a\nb\nc\nd\n";
135
- for (const edit of [
136
- { op: "replace", anchor: at(text, 3), body: ["y"] },
137
- { op: "delete", anchor: at(text, 2) },
138
- { op: "insert_after", anchor: at(text, 2), body: ["y"] },
139
- ] satisfies Edit[]) {
140
- const result = applyEdits(
141
- text,
142
- [{ op: "replace", anchor: at(text, 2), end: at(text, 3), body: ["x"] }, edit],
143
- 4,
144
- 0,
145
- );
146
- assert(!result.ok && result.failure.kind === "range");
147
- assert.equal(result.failure.code, "OVERLAPPING_EDITS");
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 edits overlap.");
137
+ assert.deepEqual(error.facts?.conflict, ["edits[1]", "edits[2]"]);
138
+ return true;
139
+ },
140
+ );
141
+ }
148
142
  }
149
143
  });
150
144
 
@@ -166,19 +160,39 @@ test("insertion at a replaced or deleted range start → insert before the range
166
160
  }
167
161
  });
168
162
 
169
- test("insertions sharing a gap → preserve array order", () => {
170
- const text = "a\nb\n";
171
- const edits: Edit[] = [
172
- { op: "insert_after", anchor: at(text, 1), body: ["x"] },
173
- { op: "insert_before", anchor: at(text, 2), body: ["y"] },
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
+ ],
174
182
  ];
175
- for (const [batch, expected] of [
176
- [edits, "a\nx\ny\nb\n"],
177
- [[...edits].reverse(), "a\ny\nx\nb\n"],
178
- ] satisfies [Edit[], string][]) {
179
- const result = applyEdits(text, batch, 4, 0);
180
- assert(result.ok);
181
- assert.equal(result.text, expected);
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 edits insert at one position.");
191
+ assert.deepEqual(error.facts?.conflict, ["edits[0]", "edits[1]"]);
192
+ return true;
193
+ },
194
+ );
195
+ }
182
196
  }
183
197
  });
184
198
 
@@ -194,7 +208,7 @@ test("byte-identical edits → validate anchors and preserve the source bytes",
194
208
  4,
195
209
  15,
196
210
  );
197
- assert.ok(!invalid.ok && invalid.failure.kind === "anchor");
211
+ assert.ok(!invalid.ok);
198
212
  }
199
213
  });
200
214
 
@@ -356,11 +370,10 @@ test("rescued anchor lets the retry succeed without a re-read", () => {
356
370
  4,
357
371
  15,
358
372
  );
359
- assert.ok(!r1.ok && r1.failure.kind === "anchor");
360
- const f = r1.failure.failures[0];
373
+ assert.ok(!r1.ok);
374
+ const f = r1.failures[0];
361
375
  assert.ok(f.recovery.kind === "found");
362
376
  assert.equal(f.recovery.newLine, 4);
363
- assert.equal(f.recovery.scope, "local");
364
377
  assert.equal(computeLineHash(4, splitLines(currentText)[3], 4), f.recovery.newHash);
365
378
  // Retry with the recovered anchor; the rest of the file stays unchanged.
366
379
  const r2 = applyEdits(
@@ -388,17 +401,16 @@ test("shifted recovery: duplicate content → ambiguous candidates", () => {
388
401
  4,
389
402
  15,
390
403
  );
391
- assert.ok(!r.ok && r.failure.kind === "anchor");
392
- const f = r.failure.failures[0];
404
+ assert.ok(!r.ok);
405
+ const f = r.failures[0];
393
406
  assert.ok(f.recovery.kind === "ambiguous");
394
- assert.equal(f.recovery.scope, "local");
395
407
  assert.deepEqual(
396
408
  f.recovery.candidates.map((c) => c.line),
397
409
  [3, 5],
398
410
  );
399
411
  });
400
412
 
401
- test("shifted recovery: content genuinely changed → none, with live content", () => {
413
+ test("shifted recovery: content genuinely changed → none", () => {
402
414
  const readText = "a\nb\nc\n";
403
415
  const currentText = "a\nBCHANGED\nc\n"; // "b" is gone
404
416
  const r = applyEdits(
@@ -407,10 +419,9 @@ test("shifted recovery: content genuinely changed → none, with live content",
407
419
  4,
408
420
  15,
409
421
  );
410
- assert.ok(!r.ok && r.failure.kind === "anchor");
411
- const f = r.failure.failures[0];
422
+ assert.ok(!r.ok);
423
+ const f = r.failures[0];
412
424
  assert.equal(f.recovery.kind, "none");
413
- assert.equal(f.current?.content, "BCHANGED");
414
425
  });
415
426
 
416
427
  test("full-file recovery finds distant content and anchors beyond the current EOF", () => {
@@ -427,10 +438,9 @@ test("full-file recovery finds distant content and anchors beyond the current EO
427
438
  4,
428
439
  15,
429
440
  );
430
- assert.ok(!result.ok && result.failure.kind === "anchor");
431
- assert.deepEqual(result.failure.failures[0].recovery, {
441
+ assert.ok(!result.ok);
442
+ assert.deepEqual(result.failures[0].recovery, {
432
443
  kind: "found",
433
- scope: "full-file",
434
444
  newLine,
435
445
  newHash: computeLineHash(newLine, "target", 4),
436
446
  });
@@ -438,10 +448,10 @@ test("full-file recovery finds distant content and anchors beyond the current EO
438
448
  });
439
449
 
440
450
  test("full-file recovery collects both sides while preserving local candidate priority", () => {
441
- for (const { positions, selected, scope } of [
442
- { positions: [3, 95], selected: [3, 95], scope: "full-file" },
443
- { positions: [3, 49, 95], selected: [49], scope: "local" },
444
- { positions: [3, 49, 51, 95], selected: [49, 51], scope: "local" },
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] },
445
455
  ]) {
446
456
  const lines = Array.from({ length: 100 }, () => "filler");
447
457
  for (const line of positions) lines[line - 1] = "target";
@@ -451,19 +461,17 @@ test("full-file recovery collects both sides while preserving local candidate pr
451
461
  4,
452
462
  15,
453
463
  );
454
- assert.ok(!result.ok && result.failure.kind === "anchor");
464
+ assert.ok(!result.ok);
455
465
  assert.deepEqual(
456
- result.failure.failures[0].recovery,
466
+ result.failures[0].recovery,
457
467
  selected.length === 1
458
468
  ? {
459
469
  kind: "found",
460
- scope,
461
470
  newLine: selected[0],
462
471
  newHash: computeLineHash(selected[0], "target", 4),
463
472
  }
464
473
  : {
465
474
  kind: "ambiguous",
466
- scope,
467
475
  candidates: selected.map((line) => ({
468
476
  line,
469
477
  hash: computeLineHash(line, "target", 4),
@@ -473,7 +481,7 @@ test("full-file recovery collects both sides while preserving local candidate pr
473
481
  }
474
482
  });
475
483
 
476
- test("collect-all: two stale anchors in one batch → both failures returned", () => {
484
+ test("two stale anchors and overlapping verified edits → return both anchor failures first", () => {
477
485
  const readText = "a\nb\nc\nd\n";
478
486
  const currentText = "X\na\nb\nc\nd\n"; // inserted X at top → all shifted +1
479
487
  const r = applyEdits(
@@ -481,15 +489,15 @@ test("collect-all: two stale anchors in one batch → both failures returned", (
481
489
  [
482
490
  { op: "replace", anchor: at(readText, 2), body: ["B"] },
483
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"] },
484
494
  ],
485
495
  4,
486
496
  15,
487
497
  );
488
- assert.ok(!r.ok && r.failure.kind === "anchor");
489
- assert.equal(r.failure.failures.length, 2);
490
- const found = r.failure.failures.map((f) =>
491
- f.recovery.kind === "found" ? f.recovery.newLine : -1,
492
- );
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));
493
501
  assert.deepEqual(found, [3, 5]);
494
502
  });
495
503
 
@@ -502,8 +510,8 @@ test("shiftRadius=0 disables rescue (always none)", () => {
502
510
  4,
503
511
  0,
504
512
  );
505
- assert.ok(!r.ok && r.failure.kind === "anchor");
506
- assert.equal(r.failure.failures[0].recovery.kind, "none");
513
+ assert.ok(!r.ok);
514
+ assert.equal(r.failures[0].recovery.kind, "none");
507
515
  });
508
516
 
509
517
  test("BOM stays at byte zero through first-line edits while anchors retain their original hashes", () => {
@@ -556,251 +564,68 @@ test("BOM stays at byte zero through first-line edits while anchors retain their
556
564
  assert.equal(result.text, "\uFEFFchanged\ninside\uFEFFcontent\n");
557
565
  });
558
566
 
559
- test("failed batches report every supplied anchor in input order from one snapshot", () => {
560
- const text = "a\nb\nc\nd\ne\nf\n";
561
- const stale = { line: 2, hash: "ZZ" };
562
- const edits: Edit[] = [
563
- { op: "replace", anchor: at(text, 1), end: stale, body: ["A"] },
564
- { op: "delete", anchor: stale, end: at(text, 4) },
565
- { op: "insert_after", anchor: at(text, 5), body: ["E"] },
566
- { op: "insert_before", anchor: stale, body: ["B"] },
567
- { op: "append", body: ["last"] },
568
- { op: "prepend", body: ["first"] },
569
- ];
570
- const result = applyEdits(text, edits, 4, 15);
571
- assert.ok(!result.ok && result.failure.kind === "anchor");
572
- assert.deepEqual(result.failure.checks, [
573
- { opIndex: 0, which: "anchor", status: "matched" },
574
- { opIndex: 0, which: "end", status: "mismatched" },
575
- { opIndex: 1, which: "anchor", status: "mismatched" },
576
- { opIndex: 1, which: "end", status: "matched" },
577
- { opIndex: 2, which: "anchor", status: "matched" },
578
- { opIndex: 3, which: "anchor", status: "mismatched" },
579
- ]);
580
- });
581
-
582
- test("matched anchor checks do not imply valid ranges", () => {
583
- const text = "a\nb\nc\n";
584
- const cases: { edits: Edit[]; kind: "range"; checks: number }[] = [
585
- { edits: [{ op: "delete", anchor: at(text, 3), end: at(text, 1) }], kind: "range", checks: 2 },
586
- {
587
- edits: [
588
- { op: "replace", anchor: at(text, 1), end: at(text, 3), body: ["x"] },
589
- { op: "delete", anchor: at(text, 2) },
590
- ],
591
- kind: "range",
592
- checks: 3,
593
- },
594
- ];
595
- for (const { edits, kind, checks } of cases) {
596
- const result = applyEdits(text, edits, 4, 15);
597
- assert.ok(!result.ok);
598
- assert.equal(result.failure.kind, kind);
599
- assert.equal(result.failure.checks.length, checks);
600
- assert.ok(result.failure.checks.every((check) => check.status === "matched"));
601
- }
602
- });
603
-
604
- test("copy/move original ranges → retain captured text and order", () => {
605
- const text = "A\nB\nC\nD\n";
606
- const cases: { edit: Edit; expected: string }[] = [
607
- {
608
- edit: { op: "copy", anchor: at(text, 2), end: at(text, 3), after: at(text, 4) },
609
- expected: "A\nB\nC\nD\nB\nC\n",
610
- },
611
- {
612
- edit: { op: "move", anchor: at(text, 1), end: at(text, 2), after: at(text, 4) },
613
- expected: "C\nD\nA\nB\n",
614
- },
615
- {
616
- edit: { op: "move", anchor: at(text, 3), end: at(text, 4), before: at(text, 1) },
617
- expected: "C\nD\nA\nB\n",
618
- },
619
- {
620
- edit: { op: "copy", anchor: at(text, 1), end: at(text, 3), after: at(text, 1) },
621
- expected: "A\nA\nB\nC\nB\nC\nD\n",
622
- },
623
- ];
624
- for (const { edit, expected } of cases) {
625
- const result = applyEdits(text, [edit], 4, 0);
626
- assert(result.ok);
627
- assert.equal(result.text, expected);
628
- }
629
- });
630
-
631
- test("copy source also replaced or deleted → capture original text regardless of batch order", () => {
632
- const text = "A\nB\nC\nD\n";
633
- const copy: Edit = { op: "copy", anchor: at(text, 1), end: at(text, 2), after: at(text, 4) };
634
- for (const edit of [
635
- { op: "replace", anchor: at(text, 1), end: at(text, 2), body: ["NEW"] },
636
- { op: "delete", anchor: at(text, 1), end: at(text, 2) },
637
- ] satisfies Edit[]) {
638
- for (const edits of [
639
- [copy, edit],
640
- [edit, copy],
641
- ]) {
642
- const result = applyEdits(text, edits, 4, 0);
643
- assert(result.ok);
644
- assert.equal(result.text, edit.op === "replace" ? "NEW\nC\nD\nA\nB\n" : "C\nD\nA\nB\n");
645
- }
646
- }
647
- });
648
-
649
- test("copy/move first-line mixed-ending range → preserve source gaps and keep one file BOM", () => {
567
+ test("replacing a first-line mixed-ending range → preserve gaps and keep one file BOM", () => {
650
568
  const text = "\uFEFFA\r\nB\nC\r\nD\n";
651
- for (const op of ["copy", "move"] as const) {
652
- const result = applyEdits(
653
- text,
654
- [{ op, anchor: at(text, 1), end: at(text, 3), after: at(text, 4) }],
655
- 4,
656
- 0,
657
- );
658
- assert(result.ok);
659
- assert.equal(result.text, op === "copy" ? text + "A\r\nB\nC\r\n" : "\uFEFFD\nA\r\nB\nC\r\n");
660
- }
661
- });
662
-
663
- test("copy/move unterminated final line → add interior connectors and preserve final-newline state", () => {
664
- for (const ending of ["\n", "\r\n"]) {
665
- const text = `A${ending}B`;
666
- for (const op of ["copy", "move"] as const) {
667
- const result = applyEdits(text, [{ op, anchor: at(text, 2), before: at(text, 1) }], 4, 0);
668
- assert(result.ok);
669
- assert.equal(result.text, op === "copy" ? `B${ending}A${ending}B` : `B${ending}A`);
670
- }
671
- }
672
- });
673
-
674
- test("copy/move line ending in standalone CR → connector retains the CR as line content", () => {
675
- const text = "A\nB\r";
676
- for (const op of ["copy", "move"] as const) {
677
- const result = applyEdits(text, [{ op, anchor: at(text, 2), before: at(text, 1) }], 4, 0);
678
- assert(result.ok);
679
- assert.equal(result.text, op === "copy" ? "B\r\r\nA\nB\r" : "B\r\r\nA");
680
- assert.equal(splitLines(result.text)[0], "B\r");
681
- }
682
- });
683
-
684
- test("copy Unicode and standalone-CR content → preserve every source code unit", () => {
685
- const text = "🙂中文 e\u0301\nkeep\rcontent\n";
686
569
  const result = applyEdits(
687
570
  text,
688
- [{ op: "copy", anchor: at(text, 1), end: at(text, 2), after: at(text, 2) }],
571
+ [{ op: "replace", anchor: at(text, 1), end: at(text, 3), body: ["X", "Y", "Z"] }],
689
572
  4,
690
573
  0,
691
574
  );
692
575
  assert(result.ok);
693
- assert.equal(result.text, text + text);
576
+ assert.equal(result.text, "\uFEFFX\r\nY\nZ\r\nD\n");
694
577
  });
695
578
 
696
- test("copy blank or BOM-only line → retain the added logical blank line", () => {
697
- for (const [text, line, expected] of [
698
- ["A\n\n", 2, "A\n\n\n"],
699
- ["\uFEFF", 1, "\uFEFF\n\n"],
700
- ] as const) {
701
- const result = applyEdits(
702
- text,
703
- [{ op: "copy", anchor: at(text, line), after: at(text, line) }],
704
- 4,
705
- 0,
706
- );
579
+ test("inserting beside a line ending in standalone CR → connector retains the CR as content", () => {
580
+ const text = "A\nB\r";
581
+ const cases: [Edit, string][] = [
582
+ [{ op: "append", body: ["C"] }, "A\nB\r\r\nC"],
583
+ [{ op: "prepend", body: ["B\r"] }, "B\r\r\nA\nB\r"],
584
+ ];
585
+ for (const [edit, expected] of cases) {
586
+ const result = applyEdits(text, [edit], 4, 0);
707
587
  assert(result.ok);
708
588
  assert.equal(result.text, expected);
709
- assert.equal(splitLines(result.text).length, splitLines(text).length + 1);
710
- }
711
- });
712
-
713
- test("move to either adjacent gap → byte-identical no-op", () => {
714
- const text = "A\r\nB\nC\r\nD";
715
- for (const destination of [{ before: at(text, 2) }, { after: at(text, 3) }]) {
716
- const result = applyEdits(
717
- text,
718
- [{ op: "move", anchor: at(text, 2), end: at(text, 3), ...destination }],
719
- 4,
720
- 0,
721
- );
722
- assert(result.ok);
723
- assert.equal(result.text, text);
589
+ assert.equal(splitLines(result.text)[edit.op === "append" ? 1 : 0], "B\r");
724
590
  }
725
591
  });
726
592
 
727
- test("copy/move bad source and destination anchors → aggregate every supplied field", () => {
728
- const text = "A\nB\nC\nD\n";
729
- for (const destination of [
730
- { before: { line: 4, hash: "XXXX" } },
731
- { after: { line: 4, hash: "XXXX" } },
732
- ]) {
733
- const result = applyEdits(
734
- text,
735
- [
736
- {
737
- op: "copy",
738
- anchor: { line: 1, hash: "XXXX" },
739
- end: { line: 2, hash: "XXXX" },
740
- ...destination,
741
- },
742
- ],
743
- 4,
744
- 0,
745
- );
746
- assert(!result.ok && result.failure.kind === "anchor");
747
- assert.deepEqual(
748
- result.failure.failures.map((failure) => failure.which),
749
- ["anchor", "end", Object.keys(destination)[0]],
750
- );
751
- assert(result.failure.checks.every((check) => check.status === "mismatched"));
752
- }
753
- });
754
-
755
- test("copy/move captures sharing a gap → preserve original captures in array order", () => {
756
- const text = "A\nB\nC\nD\nE\n";
757
- for (const [first, second, remaining] of [
758
- ["copy", "copy", text],
759
- ["copy", "move", "A\nC\nD\nE\n"],
760
- ["move", "copy", "B\nC\nD\nE\n"],
761
- ["move", "move", "C\nD\nE\n"],
762
- ] as const) {
763
- const edits: Edit[] = [
764
- { op: first, anchor: at(text, 1), after: at(text, 5) },
765
- { op: second, anchor: at(text, 2), after: at(text, 5) },
766
- ];
767
- for (const [batch, suffix] of [
768
- [edits, "A\nB\n"],
769
- [[...edits].reverse(), "B\nA\n"],
770
- ] satisfies [Edit[], string][]) {
771
- const result = applyEdits(text, batch, 4, 0);
772
- assert(result.ok);
773
- assert.equal(result.text, remaining + suffix);
774
- }
775
- }
593
+ test("editing beside Unicode and standalone-CR content → preserve every untouched code unit", () => {
594
+ const text = "🙂中文 e\u0301\nkeep\rcontent\n";
595
+ const result = applyEdits(text, [{ op: "append", body: ["tail"] }], 4, 0);
596
+ assert(result.ok);
597
+ assert.equal(result.text, text + "tail\n");
776
598
  });
777
599
 
778
- test("move interior destination or reversed capture range → reject invalid ranges", () => {
779
- const text = "A\nB\nC\nD\nE\n";
780
- for (const edit of [
781
- { op: "move", anchor: at(text, 1), end: at(text, 3), after: at(text, 2) },
782
- { op: "move", anchor: at(text, 1), end: at(text, 3), before: at(text, 2) },
783
- { op: "copy", anchor: at(text, 3), end: at(text, 2), after: at(text, 5) },
784
- ] satisfies Edit[]) {
785
- const result = applyEdits(text, [edit], 4, 0);
786
- assert(!result.ok && result.failure.kind === "range");
787
- assert.equal(result.failure.code, "INVALID_RANGE");
788
- assert(result.failure.checks.every((check) => check.status === "matched"));
789
- }
600
+ test("appending a blank line to a terminated blank line → retain the added logical line", () => {
601
+ const text = "A\n\n";
602
+ const result = applyEdits(text, [{ op: "append", body: [""] }], 4, 0);
603
+ assert(result.ok);
604
+ assert.equal(result.text, "A\n\n\n");
605
+ assert.equal(splitLines(result.text).length, splitLines(text).length + 1);
790
606
  });
791
607
 
792
- test("overlapping move sources → reject the complete batch", () => {
793
- const text = "A\nB\nC\nD\nE\n";
608
+ test("bad range anchors → aggregate anchor and end failures for every operation", () => {
609
+ const text = "A\nB\nC\nD\n";
610
+ const anchor = { line: 1, hash: "XXXX" };
611
+ const end = { line: 2, hash: "XXXX" };
794
612
  const result = applyEdits(
795
613
  text,
796
614
  [
797
- { op: "move", anchor: at(text, 1), end: at(text, 2), after: at(text, 5) },
798
- { op: "move", anchor: at(text, 2), end: at(text, 3), before: at(text, 1) },
615
+ { op: "replace", anchor, end, body: ["new"] },
616
+ { op: "delete", anchor, end },
799
617
  ],
800
618
  4,
801
619
  0,
802
620
  );
803
- assert(!result.ok && result.failure.kind === "range");
804
- assert.equal(result.failure.code, "OVERLAPPING_EDITS");
805
- assert(result.failure.checks.every((check) => check.status === "matched"));
621
+ assert(!result.ok);
622
+ assert.deepEqual(
623
+ result.failures.map(({ opIndex, op, which }) => ({ opIndex, op, which })),
624
+ [
625
+ { opIndex: 0, op: "replace", which: "anchor" },
626
+ { opIndex: 0, op: "replace", which: "end" },
627
+ { opIndex: 1, op: "delete", which: "anchor" },
628
+ { opIndex: 1, op: "delete", which: "end" },
629
+ ],
630
+ );
806
631
  });