-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapply-engine.test.ts
More file actions
840 lines (750 loc) · 23.4 KB
/
Copy pathapply-engine.test.ts
File metadata and controls
840 lines (750 loc) · 23.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
import { describe, expect, it } from "bun:test";
import {
chmodSync,
lstatSync,
mkdirSync,
mkdtempSync,
readFileSync,
readdirSync,
symlinkSync,
writeFileSync,
} from "node:fs";
import { tmpdir } from "node:os";
import { dirname, join } from "node:path";
import { applyDiffPayload } from "./apply-engine";
import type { ApplyJsonPayload } from "./apply-engine";
function tmpProject(): string {
return mkdtempSync(join(tmpdir(), "codemap-apply-test-"));
}
function writeSource(root: string, relPath: string, content: string): void {
const absPath = join(root, relPath);
mkdirSync(dirname(absPath), { recursive: true });
writeFileSync(absPath, content, "utf8");
}
function readSource(root: string, relPath: string): string {
return readFileSync(join(root, relPath), "utf8");
}
describe("applyDiffPayload", () => {
describe("happy paths", () => {
it("returns a clean dry-run envelope for a single-row valid input", () => {
const root = tmpProject();
writeSource(root, "a.ts", "const foo = 1;\n");
const result = applyDiffPayload({
rows: [
{
file_path: "a.ts",
line_start: 1,
before_pattern: "foo",
after_pattern: "bar",
},
],
projectRoot: root,
dryRun: true,
});
expect(result.mode).toBe("dry-run");
expect(result.applied).toBe(false);
expect(result.conflicts).toEqual([]);
expect(result.files).toEqual([{ file_path: "a.ts", rows_applied: 0 }]);
expect(result.summary).toEqual({
files: 1,
files_modified: 0,
rows: 1,
rows_applied: 0,
conflicts: 0,
files_with_conflicts: 0,
});
});
it("collapses N rows targeting the same file into one ApplyFile entry", () => {
const root = tmpProject();
writeSource(root, "a.ts", "const foo = 1;\nconst foo2 = 2;\n");
const result = applyDiffPayload({
rows: [
{
file_path: "a.ts",
line_start: 1,
before_pattern: "foo",
after_pattern: "bar",
},
{
file_path: "a.ts",
line_start: 2,
before_pattern: "foo2",
after_pattern: "bar2",
},
],
projectRoot: root,
dryRun: true,
});
expect(result.files).toEqual([{ file_path: "a.ts", rows_applied: 0 }]);
expect(result.summary.files).toBe(1);
expect(result.summary.rows).toBe(2);
});
it("sorts files[] by path for deterministic envelope output", () => {
const root = tmpProject();
writeSource(root, "z.ts", "const foo = 1;\n");
writeSource(root, "a.ts", "const foo = 1;\n");
writeSource(root, "m.ts", "const foo = 1;\n");
const result = applyDiffPayload({
rows: ["z.ts", "a.ts", "m.ts"].map((file_path) => ({
file_path,
line_start: 1,
before_pattern: "foo",
after_pattern: "bar",
})),
projectRoot: root,
dryRun: true,
});
expect(result.files.map((f) => f.file_path)).toEqual([
"a.ts",
"m.ts",
"z.ts",
]);
});
it("matches substrings inside lines (not whole-line exact) — mirrors buildDiffJson", () => {
const root = tmpProject();
writeSource(root, "a.ts", " const foo = 1;\n");
const result = applyDiffPayload({
rows: [
{
file_path: "a.ts",
line_start: 1,
before_pattern: "foo",
after_pattern: "bar",
},
],
projectRoot: root,
dryRun: true,
});
expect(result.conflicts).toEqual([]);
expect(result.files).toHaveLength(1);
});
});
describe("conflict reporting (Q3 scan-and-collect)", () => {
it("reports `file missing` when the path doesn't exist", () => {
const root = tmpProject();
const result = applyDiffPayload({
rows: [
{
file_path: "ghost.ts",
line_start: 1,
before_pattern: "foo",
after_pattern: "bar",
},
],
projectRoot: root,
dryRun: true,
});
expect(result.conflicts).toEqual([
{
file_path: "ghost.ts",
line_start: 1,
before_pattern: "foo",
actual_at_line: "",
reason: "file missing",
},
]);
expect(result.files).toEqual([]);
expect(result.summary.conflicts).toBe(1);
expect(result.summary.files_with_conflicts).toBe(1);
});
it("reports `line out of range` when line_start exceeds EOF", () => {
const root = tmpProject();
writeSource(root, "a.ts", "const foo = 1;\n");
const result = applyDiffPayload({
rows: [
{
file_path: "a.ts",
line_start: 99,
before_pattern: "foo",
after_pattern: "bar",
},
],
projectRoot: root,
dryRun: true,
});
expect(result.conflicts[0]).toMatchObject({
file_path: "a.ts",
line_start: 99,
reason: "line out of range",
actual_at_line: "",
});
});
it("reports `line content drifted` with the actual disk content", () => {
const root = tmpProject();
writeSource(root, "a.ts", "const bar = 1;\n");
const result = applyDiffPayload({
rows: [
{
file_path: "a.ts",
line_start: 1,
before_pattern: "foo",
after_pattern: "baz",
},
],
projectRoot: root,
dryRun: true,
});
expect(result.conflicts[0]).toEqual({
file_path: "a.ts",
line_start: 1,
before_pattern: "foo",
actual_at_line: "const bar = 1;",
reason: "line content drifted",
});
});
it("collects ALL conflicts (Q3 scan-and-collect, not fail-fast)", () => {
const root = tmpProject();
writeSource(root, "a.ts", "const a = 1;\n");
writeSource(root, "b.ts", "const b = 2;\n");
const result = applyDiffPayload({
rows: [
{
file_path: "a.ts",
line_start: 1,
before_pattern: "FOO",
after_pattern: "X",
},
{
file_path: "b.ts",
line_start: 1,
before_pattern: "BAR",
after_pattern: "Y",
},
{
file_path: "ghost.ts",
line_start: 1,
before_pattern: "BAZ",
after_pattern: "Z",
},
],
projectRoot: root,
dryRun: true,
});
expect(result.conflicts).toHaveLength(3);
expect(result.summary.files_with_conflicts).toBe(3);
// Q2 (c) — any conflict aborts the run; nothing reported as applicable.
expect(result.files).toEqual([]);
});
it("clears files[] entirely when ANY row conflicts (Q2 (c) all-or-nothing)", () => {
const root = tmpProject();
writeSource(root, "good.ts", "const foo = 1;\n");
// bad.ts intentionally missing.
const result = applyDiffPayload({
rows: [
{
file_path: "good.ts",
line_start: 1,
before_pattern: "foo",
after_pattern: "bar",
},
{
file_path: "bad.ts",
line_start: 1,
before_pattern: "foo",
after_pattern: "bar",
},
],
projectRoot: root,
dryRun: true,
});
expect(result.conflicts).toHaveLength(1);
expect(result.files).toEqual([]);
});
});
describe("row-shape validation", () => {
it("silently skips rows missing required keys (mirrors buildDiffJson)", () => {
const root = tmpProject();
writeSource(root, "a.ts", "const foo = 1;\n");
const result = applyDiffPayload({
rows: [
{
file_path: "a.ts",
line_start: 1,
before_pattern: "foo",
after_pattern: "bar",
},
{ file_path: "a.ts", line_start: 1, before_pattern: "foo" }, // missing after
{
file_path: "a.ts",
line_start: 0,
before_pattern: "foo",
after_pattern: "bar",
}, // line_start not positive
{ line_start: 1, before_pattern: "foo", after_pattern: "bar" }, // missing file_path
] as Record<string, unknown>[],
projectRoot: root,
dryRun: true,
});
expect(result.summary.rows).toBe(1);
expect(result.conflicts).toEqual([]);
});
it("returns the empty envelope for an empty row set", () => {
const root = tmpProject();
const result: ApplyJsonPayload = applyDiffPayload({
rows: [],
projectRoot: root,
dryRun: true,
});
expect(result).toEqual({
mode: "dry-run",
applied: false,
files: [],
conflicts: [],
summary: {
files: 0,
files_modified: 0,
rows: 0,
rows_applied: 0,
conflicts: 0,
files_with_conflicts: 0,
},
});
});
});
describe("apply-mode (phase-2 writes)", () => {
it("writes the transformed content to disk and reports applied=true", () => {
const root = tmpProject();
writeSource(root, "a.ts", "const foo = 1;\n");
const result = applyDiffPayload({
rows: [
{
file_path: "a.ts",
line_start: 1,
before_pattern: "foo",
after_pattern: "bar",
},
],
projectRoot: root,
dryRun: false,
});
expect(result.mode).toBe("apply");
expect(result.applied).toBe(true);
expect(result.files).toEqual([{ file_path: "a.ts", rows_applied: 1 }]);
expect(result.summary).toEqual({
files: 1,
files_modified: 1,
rows: 1,
rows_applied: 1,
conflicts: 0,
files_with_conflicts: 0,
});
expect(readSource(root, "a.ts")).toBe("const bar = 1;\n");
});
it("applies multiple rows to the same file in descending line order", () => {
const root = tmpProject();
writeSource(
root,
"a.ts",
"const foo = 1;\nconst bar = 2;\nconst baz = 3;\n",
);
const result = applyDiffPayload({
rows: [
{
file_path: "a.ts",
line_start: 1,
before_pattern: "foo",
after_pattern: "ONE",
},
{
file_path: "a.ts",
line_start: 2,
before_pattern: "bar",
after_pattern: "TWO",
},
{
file_path: "a.ts",
line_start: 3,
before_pattern: "baz",
after_pattern: "THREE",
},
],
projectRoot: root,
dryRun: false,
});
expect(result.applied).toBe(true);
expect(result.files[0]?.rows_applied).toBe(3);
expect(readSource(root, "a.ts")).toBe(
"const ONE = 1;\nconst TWO = 2;\nconst THREE = 3;\n",
);
});
it("preserves CRLF line endings via raw `\\n` split + join", () => {
const root = tmpProject();
writeSource(root, "a.ts", "const foo = 1;\r\nconst bar = 2;\r\n");
const result = applyDiffPayload({
rows: [
{
file_path: "a.ts",
line_start: 1,
before_pattern: "foo",
after_pattern: "FOO",
},
],
projectRoot: root,
dryRun: false,
});
expect(result.applied).toBe(true);
expect(readSource(root, "a.ts")).toBe(
"const FOO = 1;\r\nconst bar = 2;\r\n",
);
});
it("escapes `$` in after_pattern per GetSubstitution (mirrors buildDiffJson)", () => {
const root = tmpProject();
writeSource(root, "a.ts", "const inject = 1;\n");
const result = applyDiffPayload({
rows: [
{
file_path: "a.ts",
line_start: 1,
before_pattern: "inject",
after_pattern: "$inject",
},
],
projectRoot: root,
dryRun: false,
});
expect(result.applied).toBe(true);
expect(readSource(root, "a.ts")).toBe("const $inject = 1;\n");
});
it("does NOT write any file when conflicts abort phase 2 (Q2 (c))", () => {
const root = tmpProject();
writeSource(root, "good.ts", "const foo = 1;\n");
// bad.ts intentionally missing.
const goodBefore = readSource(root, "good.ts");
const result = applyDiffPayload({
rows: [
{
file_path: "good.ts",
line_start: 1,
before_pattern: "foo",
after_pattern: "bar",
},
{
file_path: "bad.ts",
line_start: 1,
before_pattern: "foo",
after_pattern: "bar",
},
],
projectRoot: root,
dryRun: false,
});
expect(result.mode).toBe("apply");
expect(result.applied).toBe(false);
expect(result.conflicts).toHaveLength(1);
// good.ts must be untouched even though its row would have validated.
expect(readSource(root, "good.ts")).toBe(goodBefore);
});
it("returns applied=false / mode=apply when no rows are applicable", () => {
const root = tmpProject();
const result = applyDiffPayload({
rows: [],
projectRoot: root,
dryRun: false,
});
expect(result.mode).toBe("apply");
expect(result.applied).toBe(false);
expect(result.files).toEqual([]);
expect(result.summary.rows_applied).toBe(0);
});
it("does NOT leave behind any `*.codemap-apply-*.tmp` siblings", () => {
const root = tmpProject();
writeSource(root, "a.ts", "const foo = 1;\n");
applyDiffPayload({
rows: [
{
file_path: "a.ts",
line_start: 1,
before_pattern: "foo",
after_pattern: "bar",
},
],
projectRoot: root,
dryRun: false,
});
const siblings = readdirSync(root);
expect(siblings.some((s) => s.includes("codemap-apply-"))).toBe(false);
});
});
describe("path-containment guard (F1 — triangulated review 2026-05-06)", () => {
it("rejects `..`-traversal and never writes outside the project root", () => {
const root = tmpProject();
writeSource(root, "in.ts", "const foo = 1;\n");
// Sibling-of-root file the malicious row would otherwise hit.
const outsidePath = join(root, "..", "outside.ts");
writeFileSync(outsidePath, "const foo = 1;\n", "utf8");
const before = readFileSync(outsidePath, "utf8");
const result = applyDiffPayload({
rows: [
{
file_path: "../outside.ts",
line_start: 1,
before_pattern: "foo",
after_pattern: "PWNED",
},
],
projectRoot: root,
dryRun: false,
});
expect(result.applied).toBe(false);
expect(result.conflicts).toEqual([
{
file_path: "../outside.ts",
line_start: 1,
before_pattern: "foo",
actual_at_line: "",
reason: "path escapes project root",
},
]);
expect(readFileSync(outsidePath, "utf8")).toBe(before);
});
it("rejects absolute file_path inputs", () => {
const root = tmpProject();
const result = applyDiffPayload({
rows: [
{
file_path: "/etc/passwd",
line_start: 1,
before_pattern: "root",
after_pattern: "X",
},
],
projectRoot: root,
dryRun: false,
});
expect(result.applied).toBe(false);
expect(result.conflicts[0]).toMatchObject({
reason: "path escapes project root",
});
});
it("rejects nested traversal that resolves outside the root", () => {
const root = tmpProject();
const result = applyDiffPayload({
rows: [
{
file_path: "src/../../sibling.ts",
line_start: 1,
before_pattern: "foo",
after_pattern: "X",
},
],
projectRoot: root,
dryRun: false,
});
expect(result.conflicts[0]?.reason).toBe("path escapes project root");
});
it("rejects symlinked file_path and leaves the symlink intact", () => {
const root = tmpProject();
writeSource(root, "real.ts", "const foo = 1;\n");
symlinkSync(join(root, "real.ts"), join(root, "link.ts"));
const result = applyDiffPayload({
rows: [
{
file_path: "link.ts",
line_start: 1,
before_pattern: "foo",
after_pattern: "bar",
},
],
projectRoot: root,
dryRun: false,
});
expect(result.applied).toBe(false);
expect(result.conflicts).toEqual([
{
file_path: "link.ts",
line_start: 1,
before_pattern: "foo",
actual_at_line: "",
reason: "path is a symlink",
},
]);
expect(lstatSync(join(root, "link.ts")).isSymbolicLink()).toBe(true);
expect(readSource(root, "real.ts")).toBe("const foo = 1;\n");
});
});
describe("overlap detection (F2 — triangulated review 2026-05-06)", () => {
it("emits a conflict on duplicate (file_path, line_start) and writes nothing", () => {
const root = tmpProject();
writeSource(root, "a.ts", "const foo = 1;\n");
writeSource(root, "b.ts", "const foo = 1;\n");
const aBefore = readSource(root, "a.ts");
const bBefore = readSource(root, "b.ts");
const result = applyDiffPayload({
rows: [
{
file_path: "a.ts",
line_start: 1,
before_pattern: "foo",
after_pattern: "AAA",
},
{
file_path: "b.ts",
line_start: 1,
before_pattern: "foo",
after_pattern: "BBB",
},
// Duplicate on b.ts:1 — pre-fix triggered a phase-2 throw after a.ts had already renamed.
{
file_path: "b.ts",
line_start: 1,
before_pattern: "foo",
after_pattern: "CCC",
},
],
projectRoot: root,
dryRun: false,
});
expect(result.applied).toBe(false);
expect(result.conflicts).toHaveLength(1);
expect(result.conflicts[0]).toMatchObject({
file_path: "b.ts",
line_start: 1,
reason: "duplicate edit on same line",
});
// CRITICAL — neither file may have been touched; Q2 (c) holds.
expect(readSource(root, "a.ts")).toBe(aBefore);
expect(readSource(root, "b.ts")).toBe(bBefore);
});
it("dedups `a.ts` and `./a.ts` to the same canonical key", () => {
const root = tmpProject();
writeSource(root, "a.ts", "const foo = 1;\n");
const result = applyDiffPayload({
rows: [
{
file_path: "a.ts",
line_start: 1,
before_pattern: "foo",
after_pattern: "X",
},
// Same disk file via a different spelling — pre-fix this evaded
// the overlap guard and racing writes clobbered each other.
{
file_path: "./a.ts",
line_start: 1,
before_pattern: "foo",
after_pattern: "Y",
},
],
projectRoot: root,
dryRun: false,
});
expect(result.applied).toBe(false);
expect(result.conflicts[0]?.reason).toBe("duplicate edit on same line");
expect(readSource(root, "a.ts")).toBe("const foo = 1;\n");
});
});
describe("after_pattern allows empty string (deletion)", () => {
it("deletes the leftmost match of `before_pattern` when `after_pattern` is empty", () => {
const root = tmpProject();
writeSource(root, "a.ts", "// FIXME(team): this comment\nconst x = 1;\n");
const result = applyDiffPayload({
rows: [
{
file_path: "a.ts",
line_start: 1,
before_pattern: "FIXME(team): ",
after_pattern: "",
},
],
projectRoot: root,
dryRun: false,
});
expect(result.applied).toBe(true);
expect(result.summary.rows_applied).toBe(1);
expect(readSource(root, "a.ts")).toBe("// this comment\nconst x = 1;\n");
});
it("still rejects a row with empty `before_pattern`", () => {
// Empty `before_pattern` would match anywhere on the line — a row
// with `before: ""` is malformed, not a deletion intent.
const root = tmpProject();
writeSource(root, "a.ts", "const foo = 1;\n");
const result = applyDiffPayload({
rows: [
{
file_path: "a.ts",
line_start: 1,
before_pattern: "",
after_pattern: "anything",
},
],
projectRoot: root,
dryRun: true,
});
expect(result.summary.rows).toBe(0); // row silently dropped
expect(result.summary.files).toBe(0);
expect(result.files).toEqual([]);
});
});
describe("same-line ambiguity (F3 — documented limitation)", () => {
it("rewrites only the first occurrence on a line — matches buildDiffJson", () => {
// Pins current behaviour so a future engine change lands as a deliberate
// breaking change. See module docstring § Same-line ambiguity.
const root = tmpProject();
writeSource(root, "x.ts", "const foo = foo();\n");
const result = applyDiffPayload({
rows: [
{
file_path: "x.ts",
line_start: 1,
before_pattern: "foo",
after_pattern: "bar",
},
],
projectRoot: root,
dryRun: false,
});
expect(result.applied).toBe(true);
expect(result.summary.rows_applied).toBe(1);
// Declaration renamed; recursive call site untouched.
expect(readSource(root, "x.ts")).toBe("const bar = foo();\n");
});
});
describe("failure modes", () => {
it("propagates the writeFileSync error when the file is read-only (chmod 0o444)", () => {
const root = tmpProject();
writeSource(root, "a.ts", "const foo = 1;\n");
// Chmod the directory to read-only so the temp-file write fails.
// (The file itself can still be `rename`d to, but writeFileSync of
// the temp sibling needs write permission on the parent dir.)
chmodSync(root, 0o555);
try {
expect(() =>
applyDiffPayload({
rows: [
{
file_path: "a.ts",
line_start: 1,
before_pattern: "foo",
after_pattern: "bar",
},
],
projectRoot: root,
dryRun: false,
}),
).toThrow();
} finally {
chmodSync(root, 0o755);
}
});
it("is a no-op on disk when dry-run is requested even with valid rows", () => {
const root = tmpProject();
writeSource(root, "a.ts", "const foo = 1;\n");
const before = readSource(root, "a.ts");
const result = applyDiffPayload({
rows: [
{
file_path: "a.ts",
line_start: 1,
before_pattern: "foo",
after_pattern: "bar",
},
],
projectRoot: root,
dryRun: true,
});
expect(result.mode).toBe("dry-run");
expect(result.files).toEqual([{ file_path: "a.ts", rows_applied: 0 }]);
expect(readSource(root, "a.ts")).toBe(before);
});
});
});