@criogaid/pi-hashline-edit 0.9.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 (110) hide show
  1. package/README.md +9 -10
  2. package/README.zh-CN.md +8 -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 +96 -271
  7. package/src/core/apply.ts +62 -169
  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/regex.ts +3 -4
  16. package/src/core/replace.test.ts +47 -1
  17. package/src/core/replace.ts +19 -13
  18. package/src/core/search-analysis.test.ts +24 -27
  19. package/src/core/search-analysis.ts +34 -65
  20. package/src/core/search-matrix.testing.ts +0 -2
  21. package/src/core/search.ts +1 -1
  22. package/src/core/text.test.ts +5 -5
  23. package/src/core/text.ts +5 -12
  24. package/src/core/types.ts +7 -43
  25. package/src/integration/fusion-dispatch.test.ts +2 -5
  26. package/src/integration/grep-flags.test.ts +0 -14
  27. package/src/integration/grep-js-completeness.test.ts +34 -8
  28. package/src/integration/grep-line-candidates.test.ts +4 -8
  29. package/src/integration/grep-matrix.test.ts +3 -7
  30. package/src/integration/grep-path-matrix.test.ts +5 -5
  31. package/src/integration/grep-prefilter-coverage.test.ts +3 -7
  32. package/src/integration/grep-process-limits.test.ts +78 -0
  33. package/src/integration/grep-rg.test.ts +118 -20
  34. package/src/integration/grep-scan-budget.test.ts +2 -2
  35. package/src/integration/grep-translation.test.ts +12 -27
  36. package/src/integration/large-text.test.ts +1 -47
  37. package/src/integration/model-budget-lifecycle.test.ts +2 -1
  38. package/src/integration/publication-read-changes.test.ts +123 -45
  39. package/src/integration/replace-worker-boundary.test.ts +21 -11
  40. package/src/integration/report-replay.test.ts +20 -22
  41. package/src/pi/AGENTS.md +15 -15
  42. package/src/pi/action-fusion.test.ts +164 -296
  43. package/src/pi/action-fusion.ts +148 -111
  44. package/src/pi/anchor-format.test.ts +9 -9
  45. package/src/pi/anchor-format.ts +6 -8
  46. package/src/pi/argument-diagnostics.ts +21 -25
  47. package/src/pi/argument-error.ts +2 -1
  48. package/src/pi/argument-validation.test.ts +129 -157
  49. package/src/pi/argument-validation.ts +5 -8
  50. package/src/pi/budgets.ts +5 -4
  51. package/src/pi/config.test.ts +5 -2
  52. package/src/pi/config.ts +4 -6
  53. package/src/pi/diagnostic-buffer.test.ts +12 -1
  54. package/src/pi/diagnostic-buffer.ts +1 -2
  55. package/src/pi/directory-sync.test.ts +0 -1
  56. package/src/pi/edit-tool.ts +46 -156
  57. package/src/pi/error-text.ts +4 -8
  58. package/src/pi/execute.test.ts +83 -196
  59. package/src/pi/failure-context.test.ts +144 -126
  60. package/src/pi/failure-context.ts +44 -93
  61. package/src/pi/failure-path.test.ts +168 -141
  62. package/src/pi/file-commit.test.ts +110 -53
  63. package/src/pi/file-commit.ts +90 -137
  64. package/src/pi/file-snapshot.test.ts +3 -5
  65. package/src/pi/fusion-card.test.ts +4 -1
  66. package/src/pi/fusion-card.ts +16 -24
  67. package/src/pi/grep-candidates.test.ts +2 -101
  68. package/src/pi/grep-candidates.ts +23 -69
  69. package/src/pi/grep-matrix.test.ts +2 -2
  70. package/src/pi/grep-order.test.ts +4 -4
  71. package/src/pi/grep-output.ts +62 -96
  72. package/src/pi/grep-render.ts +1 -1
  73. package/src/pi/grep-reread.ts +92 -0
  74. package/src/pi/grep-scope.ts +40 -42
  75. package/src/pi/grep-search.ts +22 -31
  76. package/src/pi/grep-tool.ts +55 -82
  77. package/src/pi/grep.test.ts +80 -45
  78. package/src/pi/model-budgets.test.ts +25 -23
  79. package/src/pi/model-budgets.ts +6 -38
  80. package/src/pi/mutation-result.test.ts +44 -35
  81. package/src/pi/mutation-result.ts +51 -20
  82. package/src/pi/mutation-runner.ts +126 -70
  83. package/src/pi/output-budgets.test.ts +36 -14
  84. package/src/pi/path.ts +1 -1
  85. package/src/pi/read-tool.ts +5 -5
  86. package/src/pi/regex-worker-contract.ts +3 -13
  87. package/src/pi/regex-worker.ts +1 -10
  88. package/src/pi/registration.test.ts +64 -36
  89. package/src/pi/render.test.ts +38 -28
  90. package/src/pi/render.ts +59 -63
  91. package/src/pi/replace-regex.ts +14 -2
  92. package/src/pi/replace-tool.ts +20 -82
  93. package/src/pi/replace-worker.ts +1 -1
  94. package/src/pi/replace.test.ts +9 -5
  95. package/src/pi/report-cause.ts +3 -8
  96. package/src/pi/report-failure.testing.ts +1 -2
  97. package/src/pi/report-schema.ts +62 -81
  98. package/src/pi/report.test.ts +171 -25
  99. package/src/pi/report.ts +33 -87
  100. package/src/pi/result-safety.test.ts +149 -176
  101. package/src/pi/rg-process.test.ts +77 -63
  102. package/src/pi/rg-process.ts +48 -64
  103. package/src/pi/rg-regex.ts +40 -400
  104. package/src/pi/schema.ts +12 -0
  105. package/src/pi/text-stream.test.ts +0 -21
  106. package/src/pi/text-stream.ts +14 -21
  107. package/src/pi/write-tool.ts +33 -62
  108. package/src/pi/write.test.ts +1 -3
  109. package/src/testing/regex-translation-corpus.ts +12 -12
  110. 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,37 +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");
148
- assert.match(result.failure.message, /overlap/);
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
+ }
149
142
  }
150
143
  });
151
144
 
@@ -178,14 +171,6 @@ test("insertions sharing a gap β†’ reject the batch", () => {
178
171
  { op: "insert_before", anchor: at(text, 3), body: ["x"] },
179
172
  { op: "insert_before", anchor: at(text, 3), body: ["y"] },
180
173
  ],
181
- [
182
- { op: "copy", anchor: at(text, 1), after: at(text, 2) },
183
- { op: "insert_before", anchor: at(text, 3), body: ["y"] },
184
- ],
185
- [
186
- { op: "move", anchor: at(text, 1), after: at(text, 2) },
187
- { op: "insert_before", anchor: at(text, 3), body: ["y"] },
188
- ],
189
174
  [
190
175
  { op: "append", body: ["x"] },
191
176
  { op: "append", body: ["y"] },
@@ -197,10 +182,16 @@ test("insertions sharing a gap β†’ reject the batch", () => {
197
182
  ];
198
183
  for (const edits of cases) {
199
184
  for (const batch of [edits, [...edits].reverse()]) {
200
- const result = applyEdits(text, batch, 4, 0);
201
- assert(!result.ok && result.failure.kind === "range");
202
- assert.equal(result.failure.code, "OVERLAPPING_EDITS");
203
- assert.match(result.failure.message, /insert more than once/);
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
+ );
204
195
  }
205
196
  }
206
197
  });
@@ -217,7 +208,7 @@ test("byte-identical edits β†’ validate anchors and preserve the source bytes",
217
208
  4,
218
209
  15,
219
210
  );
220
- assert.ok(!invalid.ok && invalid.failure.kind === "anchor");
211
+ assert.ok(!invalid.ok);
221
212
  }
222
213
  });
223
214
 
@@ -379,11 +370,10 @@ test("rescued anchor lets the retry succeed without a re-read", () => {
379
370
  4,
380
371
  15,
381
372
  );
382
- assert.ok(!r1.ok && r1.failure.kind === "anchor");
383
- const f = r1.failure.failures[0];
373
+ assert.ok(!r1.ok);
374
+ const f = r1.failures[0];
384
375
  assert.ok(f.recovery.kind === "found");
385
376
  assert.equal(f.recovery.newLine, 4);
386
- assert.equal(f.recovery.scope, "local");
387
377
  assert.equal(computeLineHash(4, splitLines(currentText)[3], 4), f.recovery.newHash);
388
378
  // Retry with the recovered anchor; the rest of the file stays unchanged.
389
379
  const r2 = applyEdits(
@@ -411,17 +401,16 @@ test("shifted recovery: duplicate content β†’ ambiguous candidates", () => {
411
401
  4,
412
402
  15,
413
403
  );
414
- assert.ok(!r.ok && r.failure.kind === "anchor");
415
- const f = r.failure.failures[0];
404
+ assert.ok(!r.ok);
405
+ const f = r.failures[0];
416
406
  assert.ok(f.recovery.kind === "ambiguous");
417
- assert.equal(f.recovery.scope, "local");
418
407
  assert.deepEqual(
419
408
  f.recovery.candidates.map((c) => c.line),
420
409
  [3, 5],
421
410
  );
422
411
  });
423
412
 
424
- test("shifted recovery: content genuinely changed β†’ none, with live content", () => {
413
+ test("shifted recovery: content genuinely changed β†’ none", () => {
425
414
  const readText = "a\nb\nc\n";
426
415
  const currentText = "a\nBCHANGED\nc\n"; // "b" is gone
427
416
  const r = applyEdits(
@@ -430,10 +419,9 @@ test("shifted recovery: content genuinely changed β†’ none, with live content",
430
419
  4,
431
420
  15,
432
421
  );
433
- assert.ok(!r.ok && r.failure.kind === "anchor");
434
- const f = r.failure.failures[0];
422
+ assert.ok(!r.ok);
423
+ const f = r.failures[0];
435
424
  assert.equal(f.recovery.kind, "none");
436
- assert.equal(f.current?.content, "BCHANGED");
437
425
  });
438
426
 
439
427
  test("full-file recovery finds distant content and anchors beyond the current EOF", () => {
@@ -450,10 +438,9 @@ test("full-file recovery finds distant content and anchors beyond the current EO
450
438
  4,
451
439
  15,
452
440
  );
453
- assert.ok(!result.ok && result.failure.kind === "anchor");
454
- assert.deepEqual(result.failure.failures[0].recovery, {
441
+ assert.ok(!result.ok);
442
+ assert.deepEqual(result.failures[0].recovery, {
455
443
  kind: "found",
456
- scope: "full-file",
457
444
  newLine,
458
445
  newHash: computeLineHash(newLine, "target", 4),
459
446
  });
@@ -461,10 +448,10 @@ test("full-file recovery finds distant content and anchors beyond the current EO
461
448
  });
462
449
 
463
450
  test("full-file recovery collects both sides while preserving local candidate priority", () => {
464
- for (const { positions, selected, scope } of [
465
- { positions: [3, 95], selected: [3, 95], scope: "full-file" },
466
- { positions: [3, 49, 95], selected: [49], scope: "local" },
467
- { 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] },
468
455
  ]) {
469
456
  const lines = Array.from({ length: 100 }, () => "filler");
470
457
  for (const line of positions) lines[line - 1] = "target";
@@ -474,19 +461,17 @@ test("full-file recovery collects both sides while preserving local candidate pr
474
461
  4,
475
462
  15,
476
463
  );
477
- assert.ok(!result.ok && result.failure.kind === "anchor");
464
+ assert.ok(!result.ok);
478
465
  assert.deepEqual(
479
- result.failure.failures[0].recovery,
466
+ result.failures[0].recovery,
480
467
  selected.length === 1
481
468
  ? {
482
469
  kind: "found",
483
- scope,
484
470
  newLine: selected[0],
485
471
  newHash: computeLineHash(selected[0], "target", 4),
486
472
  }
487
473
  : {
488
474
  kind: "ambiguous",
489
- scope,
490
475
  candidates: selected.map((line) => ({
491
476
  line,
492
477
  hash: computeLineHash(line, "target", 4),
@@ -496,7 +481,7 @@ test("full-file recovery collects both sides while preserving local candidate pr
496
481
  }
497
482
  });
498
483
 
499
- 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", () => {
500
485
  const readText = "a\nb\nc\nd\n";
501
486
  const currentText = "X\na\nb\nc\nd\n"; // inserted X at top β†’ all shifted +1
502
487
  const r = applyEdits(
@@ -504,15 +489,15 @@ test("collect-all: two stale anchors in one batch β†’ both failures returned", (
504
489
  [
505
490
  { op: "replace", anchor: at(readText, 2), body: ["B"] },
506
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"] },
507
494
  ],
508
495
  4,
509
496
  15,
510
497
  );
511
- assert.ok(!r.ok && r.failure.kind === "anchor");
512
- assert.equal(r.failure.failures.length, 2);
513
- const found = r.failure.failures.map((f) =>
514
- f.recovery.kind === "found" ? f.recovery.newLine : -1,
515
- );
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));
516
501
  assert.deepEqual(found, [3, 5]);
517
502
  });
518
503
 
@@ -525,8 +510,8 @@ test("shiftRadius=0 disables rescue (always none)", () => {
525
510
  4,
526
511
  0,
527
512
  );
528
- assert.ok(!r.ok && r.failure.kind === "anchor");
529
- assert.equal(r.failure.failures[0].recovery.kind, "none");
513
+ assert.ok(!r.ok);
514
+ assert.equal(r.failures[0].recovery.kind, "none");
530
515
  });
531
516
 
532
517
  test("BOM stays at byte zero through first-line edits while anchors retain their original hashes", () => {
@@ -579,228 +564,68 @@ test("BOM stays at byte zero through first-line edits while anchors retain their
579
564
  assert.equal(result.text, "\uFEFFchanged\ninside\uFEFFcontent\n");
580
565
  });
581
566
 
582
- test("failed batches report every supplied anchor in input order from one snapshot", () => {
583
- const text = "a\nb\nc\nd\ne\nf\n";
584
- const stale = { line: 2, hash: "ZZ" };
585
- const edits: Edit[] = [
586
- { op: "replace", anchor: at(text, 1), end: stale, body: ["A"] },
587
- { op: "delete", anchor: stale, end: at(text, 4) },
588
- { op: "insert_after", anchor: at(text, 5), body: ["E"] },
589
- { op: "insert_before", anchor: stale, body: ["B"] },
590
- { op: "append", body: ["last"] },
591
- { op: "prepend", body: ["first"] },
592
- ];
593
- const result = applyEdits(text, edits, 4, 15);
594
- assert.ok(!result.ok && result.failure.kind === "anchor");
595
- assert.deepEqual(result.failure.checks, [
596
- { opIndex: 0, which: "anchor", status: "matched" },
597
- { opIndex: 0, which: "end", status: "mismatched" },
598
- { opIndex: 1, which: "anchor", status: "mismatched" },
599
- { opIndex: 1, which: "end", status: "matched" },
600
- { opIndex: 2, which: "anchor", status: "matched" },
601
- { opIndex: 3, which: "anchor", status: "mismatched" },
602
- ]);
603
- });
604
-
605
- test("matched anchor checks do not imply valid ranges", () => {
606
- const text = "a\nb\nc\n";
607
- const cases: { edits: Edit[]; kind: "range"; checks: number }[] = [
608
- { edits: [{ op: "delete", anchor: at(text, 3), end: at(text, 1) }], kind: "range", checks: 2 },
609
- {
610
- edits: [
611
- { op: "replace", anchor: at(text, 1), end: at(text, 3), body: ["x"] },
612
- { op: "delete", anchor: at(text, 2) },
613
- ],
614
- kind: "range",
615
- checks: 3,
616
- },
617
- ];
618
- for (const { edits, kind, checks } of cases) {
619
- const result = applyEdits(text, edits, 4, 15);
620
- assert.ok(!result.ok);
621
- assert.equal(result.failure.kind, kind);
622
- assert.equal(result.failure.checks.length, checks);
623
- assert.ok(result.failure.checks.every((check) => check.status === "matched"));
624
- }
625
- });
626
-
627
- test("copy/move original ranges β†’ retain captured text and order", () => {
628
- const text = "A\nB\nC\nD\n";
629
- const cases: { edit: Edit; expected: string }[] = [
630
- {
631
- edit: { op: "copy", anchor: at(text, 2), end: at(text, 3), after: at(text, 4) },
632
- expected: "A\nB\nC\nD\nB\nC\n",
633
- },
634
- {
635
- edit: { op: "move", anchor: at(text, 1), end: at(text, 2), after: at(text, 4) },
636
- expected: "C\nD\nA\nB\n",
637
- },
638
- {
639
- edit: { op: "move", anchor: at(text, 3), end: at(text, 4), before: at(text, 1) },
640
- expected: "C\nD\nA\nB\n",
641
- },
642
- {
643
- edit: { op: "copy", anchor: at(text, 1), end: at(text, 3), after: at(text, 1) },
644
- expected: "A\nA\nB\nC\nB\nC\nD\n",
645
- },
646
- ];
647
- for (const { edit, expected } of cases) {
648
- const result = applyEdits(text, [edit], 4, 0);
649
- assert(result.ok);
650
- assert.equal(result.text, expected);
651
- }
652
- });
653
-
654
- test("copy source also replaced or deleted β†’ capture original text regardless of batch order", () => {
655
- const text = "A\nB\nC\nD\n";
656
- const copy: Edit = { op: "copy", anchor: at(text, 1), end: at(text, 2), after: at(text, 4) };
657
- for (const edit of [
658
- { op: "replace", anchor: at(text, 1), end: at(text, 2), body: ["NEW"] },
659
- { op: "delete", anchor: at(text, 1), end: at(text, 2) },
660
- ] satisfies Edit[]) {
661
- for (const edits of [
662
- [copy, edit],
663
- [edit, copy],
664
- ]) {
665
- const result = applyEdits(text, edits, 4, 0);
666
- assert(result.ok);
667
- assert.equal(result.text, edit.op === "replace" ? "NEW\nC\nD\nA\nB\n" : "C\nD\nA\nB\n");
668
- }
669
- }
670
- });
671
-
672
- 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", () => {
673
568
  const text = "\uFEFFA\r\nB\nC\r\nD\n";
674
- for (const op of ["copy", "move"] as const) {
675
- const result = applyEdits(
676
- text,
677
- [{ op, anchor: at(text, 1), end: at(text, 3), after: at(text, 4) }],
678
- 4,
679
- 0,
680
- );
681
- assert(result.ok);
682
- assert.equal(result.text, op === "copy" ? text + "A\r\nB\nC\r\n" : "\uFEFFD\nA\r\nB\nC\r\n");
683
- }
684
- });
685
-
686
- test("copy/move unterminated final line β†’ add interior connectors and preserve final-newline state", () => {
687
- for (const ending of ["\n", "\r\n"]) {
688
- const text = `A${ending}B`;
689
- for (const op of ["copy", "move"] as const) {
690
- const result = applyEdits(text, [{ op, anchor: at(text, 2), before: at(text, 1) }], 4, 0);
691
- assert(result.ok);
692
- assert.equal(result.text, op === "copy" ? `B${ending}A${ending}B` : `B${ending}A`);
693
- }
694
- }
695
- });
696
-
697
- test("copy/move line ending in standalone CR β†’ connector retains the CR as line content", () => {
698
- const text = "A\nB\r";
699
- for (const op of ["copy", "move"] as const) {
700
- const result = applyEdits(text, [{ op, anchor: at(text, 2), before: at(text, 1) }], 4, 0);
701
- assert(result.ok);
702
- assert.equal(result.text, op === "copy" ? "B\r\r\nA\nB\r" : "B\r\r\nA");
703
- assert.equal(splitLines(result.text)[0], "B\r");
704
- }
705
- });
706
-
707
- test("copy Unicode and standalone-CR content β†’ preserve every source code unit", () => {
708
- const text = "πŸ™‚δΈ­ζ–‡ e\u0301\nkeep\rcontent\n";
709
569
  const result = applyEdits(
710
570
  text,
711
- [{ 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"] }],
712
572
  4,
713
573
  0,
714
574
  );
715
575
  assert(result.ok);
716
- assert.equal(result.text, text + text);
576
+ assert.equal(result.text, "\uFEFFX\r\nY\nZ\r\nD\n");
717
577
  });
718
578
 
719
- test("copy blank or BOM-only line β†’ retain the added logical blank line", () => {
720
- for (const [text, line, expected] of [
721
- ["A\n\n", 2, "A\n\n\n"],
722
- ["\uFEFF", 1, "\uFEFF\n\n"],
723
- ] as const) {
724
- const result = applyEdits(
725
- text,
726
- [{ op: "copy", anchor: at(text, line), after: at(text, line) }],
727
- 4,
728
- 0,
729
- );
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);
730
587
  assert(result.ok);
731
588
  assert.equal(result.text, expected);
732
- assert.equal(splitLines(result.text).length, splitLines(text).length + 1);
589
+ assert.equal(splitLines(result.text)[edit.op === "append" ? 1 : 0], "B\r");
733
590
  }
734
591
  });
735
592
 
736
- test("move to either adjacent gap β†’ byte-identical no-op", () => {
737
- const text = "A\r\nB\nC\r\nD";
738
- for (const destination of [{ before: at(text, 2) }, { after: at(text, 3) }]) {
739
- const result = applyEdits(
740
- text,
741
- [{ op: "move", anchor: at(text, 2), end: at(text, 3), ...destination }],
742
- 4,
743
- 0,
744
- );
745
- assert(result.ok);
746
- assert.equal(result.text, text);
747
- }
748
- });
749
-
750
- test("copy/move bad source and destination anchors β†’ aggregate every supplied field", () => {
751
- const text = "A\nB\nC\nD\n";
752
- for (const destination of [
753
- { before: { line: 4, hash: "XXXX" } },
754
- { after: { line: 4, hash: "XXXX" } },
755
- ]) {
756
- const result = applyEdits(
757
- text,
758
- [
759
- {
760
- op: "copy",
761
- anchor: { line: 1, hash: "XXXX" },
762
- end: { line: 2, hash: "XXXX" },
763
- ...destination,
764
- },
765
- ],
766
- 4,
767
- 0,
768
- );
769
- assert(!result.ok && result.failure.kind === "anchor");
770
- assert.deepEqual(
771
- result.failure.failures.map((failure) => failure.which),
772
- ["anchor", "end", Object.keys(destination)[0]],
773
- );
774
- assert(result.failure.checks.every((check) => check.status === "mismatched"));
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
  });