-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcmd-apply.test.ts
More file actions
912 lines (852 loc) · 28.1 KB
/
Copy pathcmd-apply.test.ts
File metadata and controls
912 lines (852 loc) · 28.1 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
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
/**
* End-to-end CLI coverage for `codemap apply <recipe-id>`. Exercises the
* full pipeline (bootstrap → recipe execution → apply-engine → disk-state
* assertions). Uses the same per-test temp project + full-index pattern as
* cmd-query-recency.test.ts.
*
* The TTY-prompt path (Q6 (a) interactive) is NOT tested here — Q9 locked
* "TTY-prompt path tested via --yes flag (skipping prompt); non-TTY-no-yes
* rejection tested explicitly." Bun.spawn's stdout is non-TTY by default,
* so subprocess invocations exercise exactly the non-TTY policy.
*/
import {
afterEach,
beforeAll,
beforeEach,
describe,
expect,
it,
} from "bun:test";
import { execSync } from "node:child_process";
import {
existsSync,
mkdirSync,
mkdtempSync,
readFileSync,
realpathSync,
rmSync,
writeFileSync,
} from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
const repoRoot = join(import.meta.dir, "..", "..");
const indexTs = join(repoRoot, "src", "index.ts");
let bunBin: string | null = null;
interface CliResult {
exitCode: number;
out: string;
err: string;
}
async function runCli(
args: string[],
envOverride: Record<string, string> = {},
): Promise<CliResult> {
if (bunBin === null) {
throw new Error("cmd-apply.test: bunBin not initialised by beforeAll.");
}
const proc = Bun.spawn([bunBin, indexTs, ...args], {
cwd: repoRoot,
stdout: "pipe",
stderr: "pipe",
env: { ...process.env, ...envOverride },
});
const exitCode = await proc.exited;
const out = await new Response(proc.stdout).text();
const err = await new Response(proc.stderr).text();
return { exitCode, out, err };
}
let projectRoot: string;
const tinySource = `import { helper } from "./helper";
export function entry(): number {
return helper() + 1;
}
export const VALUE = "x";
`;
const helperSource = `export function helper(): number {
return 42;
}
`;
beforeAll(() => {
bunBin = Bun.which("bun");
if (!bunBin || !existsSync(indexTs)) {
throw new Error(
`cmd-apply.test: cannot locate Bun (${bunBin}) or src entry (${indexTs}).`,
);
}
});
beforeEach(async () => {
// realpath: oxc-resolver returns the canonical (symlink-dereferenced)
// path for resolved imports. On macOS `tmpdir()` (`/var/folders/...`)
// and `/tmp` are both symlinks, so without realpath the project root
// and `imports.resolved_path` disagree on prefix and the import-rename
// join in `rename-preview.sql` returns 0 rows.
projectRoot = realpathSync(mkdtempSync(join(tmpdir(), "codemap-cli-apply-")));
mkdirSync(join(projectRoot, "src"), { recursive: true });
writeFileSync(join(projectRoot, "src", "entry.ts"), tinySource, "utf8");
writeFileSync(join(projectRoot, "src", "helper.ts"), helperSource, "utf8");
writeFileSync(join(projectRoot, "package.json"), "{}\n", "utf8");
const idx = await runCli(["--full"], { CODEMAP_ROOT: projectRoot });
expect(idx.exitCode).toBe(0);
});
afterEach(() => {
rmSync(projectRoot, { recursive: true, force: true });
});
function readFile(rel: string): string {
return readFileSync(join(projectRoot, rel), "utf8");
}
describe("codemap apply <recipe-id> — CLI integration", () => {
describe("--dry-run", () => {
it("emits the dry-run envelope without touching disk", async () => {
const before = readFile("src/helper.ts");
const r = await runCli(
[
"apply",
"rename-preview",
"--params",
"old=helper,new=worker",
"--dry-run",
"--json",
],
{ CODEMAP_ROOT: projectRoot },
);
expect(r.exitCode).toBe(0);
const env = JSON.parse(r.out);
expect(env.mode).toBe("dry-run");
expect(env.applied).toBe(false);
expect(env.summary.rows_applied).toBe(0);
expect(env.summary.rows).toBeGreaterThan(0);
expect(readFile("src/helper.ts")).toBe(before);
});
it("rejects --dry-run + --yes as mutually exclusive", async () => {
const r = await runCli(
[
"apply",
"rename-preview",
"--params",
"old=helper,new=worker",
"--dry-run",
"--yes",
],
{ CODEMAP_ROOT: projectRoot },
);
expect(r.exitCode).toBe(1);
expect(r.err).toContain("mutually exclusive");
});
});
describe("--yes (apply path)", () => {
it("writes the rename to disk and reports applied=true", async () => {
const r = await runCli(
[
"apply",
"rename-preview",
"--params",
"old=helper,new=worker",
"--yes",
"--json",
],
{ CODEMAP_ROOT: projectRoot },
);
expect(r.exitCode).toBe(0);
const env = JSON.parse(r.out);
expect(env.mode).toBe("apply");
expect(env.applied).toBe(true);
expect(env.summary.rows_applied).toBeGreaterThan(0);
expect(readFile("src/helper.ts")).toContain("function worker(");
expect(readFile("src/helper.ts")).not.toContain("function helper(");
expect(readFile("src/entry.ts")).toContain("import { worker }");
});
it("renaming an already-applied symbol is a no-op after reindex (Q7 (a))", async () => {
const first = await runCli(
[
"apply",
"rename-preview",
"--params",
"old=helper,new=worker",
"--yes",
"--json",
],
{ CODEMAP_ROOT: projectRoot },
);
expect(first.exitCode).toBe(0);
// Re-index so the recipe sees post-rename state.
const reindex = await runCli(["--full"], { CODEMAP_ROOT: projectRoot });
expect(reindex.exitCode).toBe(0);
const second = await runCli(
[
"apply",
"rename-preview",
"--params",
"old=helper,new=worker",
"--yes",
"--json",
],
{ CODEMAP_ROOT: projectRoot },
);
expect(second.exitCode).toBe(0);
const env = JSON.parse(second.out);
expect(env.summary.rows).toBe(0);
expect(env.summary.rows_applied).toBe(0);
});
});
describe("Q6 — non-TTY gate", () => {
it("rejects non-TTY apply without --yes / --dry-run", async () => {
const r = await runCli(
["apply", "rename-preview", "--params", "old=helper,new=worker"],
{ CODEMAP_ROOT: projectRoot },
);
expect(r.exitCode).toBe(1);
expect(r.err).toContain("--yes");
});
it("emits the rejection as JSON envelope under --json", async () => {
const r = await runCli(
[
"apply",
"rename-preview",
"--params",
"old=helper,new=worker",
"--json",
],
{ CODEMAP_ROOT: projectRoot },
);
expect(r.exitCode).toBe(1);
const env = JSON.parse(r.out);
expect(env.error).toContain("--yes");
});
});
describe("error paths", () => {
it("emits unknown-recipe error with the catalog hint", async () => {
const r = await runCli(
["apply", "no-such-recipe-id", "--dry-run", "--json"],
{ CODEMAP_ROOT: projectRoot },
);
expect(r.exitCode).toBe(1);
const env = JSON.parse(r.out);
expect(env.error).toContain("unknown recipe");
expect(env.error).toContain("rename-preview"); // catalog hint includes known ids
});
it("requires a positional recipe id", async () => {
const r = await runCli(["apply", "--dry-run"], {
CODEMAP_ROOT: projectRoot,
});
expect(r.exitCode).toBe(1);
expect(r.err).toMatch(/recipe-id|--rows|--diff-input/);
});
it("rejects unknown options", async () => {
const r = await runCli(["apply", "rename-preview", "--no-such-flag"], {
CODEMAP_ROOT: projectRoot,
});
expect(r.exitCode).toBe(1);
expect(r.err).toContain("unknown option");
});
it("rejects --params with --rows", async () => {
const r = await runCli(
["apply", "--rows", "-", "--params", "old=a,new=b", "--yes"],
{ CODEMAP_ROOT: projectRoot },
);
expect(r.exitCode).toBe(1);
expect(r.err).toContain("--params can only be used with <recipe-id>");
});
});
describe("--help", () => {
it("prints usage without bootstrapping", async () => {
const r = await runCli(["apply", "--help"]);
expect(r.exitCode).toBe(0);
expect(r.out).toContain("codemap apply");
expect(r.out).toContain("--rows");
expect(r.out).toContain("--dry-run");
expect(r.out).toContain("--yes");
});
});
describe("--rows", () => {
it("applies caller-supplied rows from a JSON file", async () => {
const rowsPath = join(projectRoot, "apply-rows.json");
writeFileSync(
rowsPath,
JSON.stringify([
{
file_path: "src/helper.ts",
line_start: 1,
before_pattern: "helper",
after_pattern: "worker",
},
]),
"utf8",
);
const r = await runCli(["apply", "--rows", rowsPath, "--yes", "--json"], {
CODEMAP_ROOT: projectRoot,
});
expect(r.exitCode).toBe(0);
const env = JSON.parse(r.out);
expect(env.mode).toBe("apply");
expect(env.applied).toBe(true);
expect(readFile("src/helper.ts")).toContain("function worker(");
});
});
describe("replace-marker-kind", () => {
beforeEach(async () => {
writeFileSync(
join(projectRoot, "src", "notes.md"),
"# Notes\n\nTODO: fix later\n",
"utf8",
);
const idx = await runCli(["--full"], { CODEMAP_ROOT: projectRoot });
expect(idx.exitCode).toBe(0);
});
it("writes FIXME on disk when applied with --yes", async () => {
const r = await runCli(
[
"apply",
"replace-marker-kind",
"--params",
"from_kind=TODO,to_kind=FIXME",
"--yes",
"--json",
],
{ CODEMAP_ROOT: projectRoot },
);
expect(r.exitCode).toBe(0);
const env = JSON.parse(r.out);
expect(env.applied).toBe(true);
expect(readFile("src/notes.md")).toContain("FIXME:");
expect(readFile("src/notes.md")).not.toMatch(/\bTODO:/);
});
});
describe("migrate-deprecated", () => {
beforeEach(async () => {
writeFileSync(
join(projectRoot, "src", "legacy.ts"),
"/**\n * @deprecated Use newHelper instead.\n */\nexport function oldHelper(): void {}\n",
"utf8",
);
writeFileSync(
join(projectRoot, "src", "use.ts"),
'import { oldHelper } from "./legacy";\n\nexport function run() {\n return oldHelper();\n}\n',
"utf8",
);
const idx = await runCli(["--full"], { CODEMAP_ROOT: projectRoot });
expect(idx.exitCode).toBe(0);
});
it("dry-run emits call-site rows for a deprecated symbol", async () => {
const r = await runCli(
[
"apply",
"migrate-deprecated",
"--params",
"symbol=oldHelper,replacement=newHelper",
"--dry-run",
"--json",
],
{ CODEMAP_ROOT: projectRoot },
);
expect(r.exitCode).toBe(0);
const env = JSON.parse(r.out);
expect(env.summary.rows).toBeGreaterThan(0);
expect(readFile("src/use.ts")).toContain("oldHelper");
});
it("rewrites call sites with --force --yes", async () => {
const r = await runCli(
[
"apply",
"migrate-deprecated",
"--params",
"symbol=oldHelper,replacement=newHelper",
"--force",
"--yes",
"--json",
],
{ CODEMAP_ROOT: projectRoot },
);
expect(r.exitCode).toBe(0);
const body = readFile("src/use.ts");
expect(body).toContain("import { newHelper }");
expect(body).toContain("return newHelper()");
expect(body).not.toMatch(/\boldHelper\b/);
});
});
describe("deprecated-usages", () => {
beforeEach(async () => {
writeFileSync(
join(projectRoot, "src", "legacy.ts"),
"/**\n * @deprecated Use newHelper instead.\n */\nexport function oldHelper(): void {}\n",
"utf8",
);
const idx = await runCli(["--full"], { CODEMAP_ROOT: projectRoot });
expect(idx.exitCode).toBe(0);
});
it("rewrites the @deprecated JSDoc line on disk", async () => {
const r = await runCli(
[
"apply",
"deprecated-usages",
"--params",
"symbol=oldHelper,replacement_message=Prefer newHelper.",
"--force",
"--yes",
"--json",
],
{ CODEMAP_ROOT: projectRoot },
);
expect(r.exitCode).toBe(0);
const body = readFile("src/legacy.ts");
expect(body).toContain("@deprecated Prefer newHelper.");
expect(body).not.toContain("Use newHelper instead");
});
});
describe("stale-imports", () => {
beforeEach(async () => {
writeFileSync(
join(projectRoot, "src", "widget.ts"),
'import { unusedBinding } from "./helper";\n\nexport const widget = 1;\n',
"utf8",
);
const idx = await runCli(["--full"], { CODEMAP_ROOT: projectRoot });
expect(idx.exitCode).toBe(0);
});
it("dry-run finds the sole-specifier import line", async () => {
const r = await runCli(
["apply", "stale-imports", "--dry-run", "--json"],
{ CODEMAP_ROOT: projectRoot },
);
expect(r.exitCode).toBe(0);
const env = JSON.parse(r.out);
expect(env.summary.rows).toBe(1);
expect(readFile("src/widget.ts")).toContain("unusedBinding");
});
it("rejects --yes without --force when recipe is not auto_fixable", async () => {
const r = await runCli(["apply", "stale-imports", "--yes", "--json"], {
CODEMAP_ROOT: projectRoot,
});
expect(r.exitCode).toBe(1);
expect(r.err + r.out).toMatch(/auto_fixable|--force/);
});
it("removes the unused import line with --force --yes", async () => {
const r = await runCli(
["apply", "stale-imports", "--force", "--yes", "--json"],
{ CODEMAP_ROOT: projectRoot },
);
expect(r.exitCode).toBe(0);
const env = JSON.parse(r.out);
expect(env.applied).toBe(true);
const body = readFile("src/widget.ts");
expect(body).not.toContain("unusedBinding");
expect(body).toContain("export const widget");
});
it("removes one specifier from a multi-specifier import line", async () => {
writeFileSync(
join(projectRoot, "src", "helper.ts"),
`${helperSource}export function usedOne(): number { return 1; }
export function staleOne(): number { return 2; }
`,
"utf8",
);
writeFileSync(
join(projectRoot, "src", "multi.ts"),
'import { usedOne, staleOne } from "./helper";\n\nexport const multi = usedOne();\n',
"utf8",
);
const idx = await runCli(["--full"], { CODEMAP_ROOT: projectRoot });
expect(idx.exitCode).toBe(0);
const r = await runCli(
[
"apply",
"stale-imports",
"--params",
"in_file=src/multi",
"--force",
"--yes",
"--json",
],
{ CODEMAP_ROOT: projectRoot },
);
expect(r.exitCode).toBe(0);
const body = readFile("src/multi.ts");
expect(body).toContain('import { usedOne } from "./helper"');
expect(body).not.toContain("staleOne");
expect(body).toContain("export const multi");
});
});
describe("rename-preview homonym define_in", () => {
beforeEach(async () => {
mkdirSync(join(projectRoot, "src", "bench"), { recursive: true });
writeFileSync(
join(projectRoot, "src", "bench", "homonym-helper-a.ts"),
'export function helper(): string {\n return "a";\n}\n',
"utf8",
);
writeFileSync(
join(projectRoot, "src", "bench", "homonym-helper-b.ts"),
'export function helper(): string {\n return "b";\n}\n',
"utf8",
);
writeFileSync(
join(projectRoot, "src", "bench", "homonym-consumer-a.ts"),
'import { helper } from "./homonym-helper-a";\n\nexport function useHelperA(): string {\n return helper();\n}\n',
"utf8",
);
writeFileSync(
join(projectRoot, "src", "bench", "homonym-consumer-b.ts"),
'import { helper } from "./homonym-helper-b";\n\nexport function useHelperB(): string {\n return helper();\n}\n',
"utf8",
);
const idx = await runCli(["--full"], { CODEMAP_ROOT: projectRoot });
expect(idx.exitCode).toBe(0);
});
it("applies only the anchored homonym with define_in", async () => {
const r = await runCli(
[
"apply",
"rename-preview",
"--params",
"old=helper,new=worker,define_in=src/bench/homonym-helper-a.ts",
"--yes",
"--json",
],
{ CODEMAP_ROOT: projectRoot },
);
expect(r.exitCode).toBe(0);
const env = JSON.parse(r.out);
expect(env.applied).toBe(true);
expect(readFile("src/bench/homonym-helper-a.ts")).toContain("worker");
expect(readFile("src/bench/homonym-consumer-a.ts")).toContain("worker");
expect(readFile("src/bench/homonym-helper-b.ts")).toMatch(
/function helper\(\)/,
);
const consumerB = readFile("src/bench/homonym-consumer-b.ts");
expect(consumerB).toContain("helper()");
expect(consumerB).not.toContain("worker");
});
});
describe("codemap rename alias", () => {
beforeEach(async () => {
mkdirSync(join(projectRoot, "src", "bench"), { recursive: true });
writeFileSync(
join(projectRoot, "src", "bench", "homonym-helper-a.ts"),
'export function helper(): string {\n return "a";\n}\n',
"utf8",
);
writeFileSync(
join(projectRoot, "src", "bench", "homonym-helper-b.ts"),
'export function helper(): string {\n return "b";\n}\n',
"utf8",
);
writeFileSync(
join(projectRoot, "src", "bench", "homonym-consumer-a.ts"),
'import { helper } from "./homonym-helper-a";\n\nexport function useHelperA(): string {\n return helper();\n}\n',
"utf8",
);
writeFileSync(
join(projectRoot, "src", "bench", "homonym-consumer-b.ts"),
'import { helper } from "./homonym-helper-b";\n\nexport function useHelperB(): string {\n return helper();\n}\n',
"utf8",
);
const idx = await runCli(["--full"], { CODEMAP_ROOT: projectRoot });
expect(idx.exitCode).toBe(0);
});
it("rewrites to apply rename-preview with homonym scope", async () => {
const r = await runCli(
[
"rename",
"helper",
"worker",
"--define-in",
"src/bench/homonym-helper-a.ts",
"--yes",
"--json",
],
{ CODEMAP_ROOT: projectRoot },
);
expect(r.exitCode).toBe(0);
const env = JSON.parse(r.out);
expect(env.applied).toBe(true);
expect(readFile("src/bench/homonym-helper-a.ts")).toContain("worker");
expect(readFile("src/bench/homonym-consumer-a.ts")).toContain("worker");
expect(readFile("src/bench/homonym-helper-b.ts")).toMatch(
/function helper\(\)/,
);
});
});
describe("rename-preview member JSX", () => {
beforeEach(async () => {
mkdirSync(join(projectRoot, "src", "bench"), { recursive: true });
writeFileSync(
join(projectRoot, "src", "bench", "ui.ts"),
"export function UiPanel(): string { return 'ok'; }\n",
"utf8",
);
writeFileSync(
join(projectRoot, "src", "bench", "host.tsx"),
'import * as UI from "./ui";\n\nexport function Host() {\n return <UI.UiPanel />;\n}\n',
"utf8",
);
const idx = await runCli(["--full"], { CODEMAP_ROOT: projectRoot });
expect(idx.exitCode).toBe(0);
});
it("applies jsx_element_rows for a namespaced member tag", async () => {
const r = await runCli(
[
"apply",
"rename-preview",
"--params",
"old=UiPanel,new=TilePanel,in_file=src/bench/host",
"--yes",
"--json",
],
{ CODEMAP_ROOT: projectRoot },
);
expect(r.exitCode).toBe(0);
const body = readFile("src/bench/host.tsx");
expect(body).toContain("<UI.TilePanel />");
expect(body).not.toContain("UiPanel");
});
});
describe("migrate-jsx-prop", () => {
beforeEach(async () => {
writeFileSync(
join(projectRoot, "src", "Card.tsx"),
"export function Card() {\n return <article data-id={1} hidden />;\n}\n",
"utf8",
);
const idx = await runCli(["--full"], { CODEMAP_ROOT: projectRoot });
expect(idx.exitCode).toBe(0);
});
it("renames a JSX attribute name on disk with --force --yes", async () => {
const r = await runCli(
[
"apply",
"migrate-jsx-prop",
"--params",
"old_name=data-id,new_name=data-testid",
"--force",
"--yes",
"--json",
],
{ CODEMAP_ROOT: projectRoot },
);
expect(r.exitCode).toBe(0);
const body = readFile("src/Card.tsx");
expect(body).toContain("data-testid={1}");
expect(body).not.toContain("data-id=");
});
});
describe("migrate-import-source", () => {
beforeEach(async () => {
mkdirSync(join(projectRoot, "src", "api"), { recursive: true });
writeFileSync(
join(projectRoot, "src", "api", "client.ts"),
"export function createClient() {}\n",
"utf8",
);
writeFileSync(
join(projectRoot, "src", "consumer.ts"),
'import { createClient } from "~/api/client";\n\ncreateClient();\n',
"utf8",
);
const idx = await runCli(["--full"], { CODEMAP_ROOT: projectRoot });
expect(idx.exitCode).toBe(0);
});
it("dry-run emits rows for matching import sources", async () => {
const r = await runCli(
[
"apply",
"migrate-import-source",
"--params",
"old_source=~/api/client,new_source=~/api/client-v2",
"--dry-run",
"--json",
],
{ CODEMAP_ROOT: projectRoot },
);
expect(r.exitCode).toBe(0);
const env = JSON.parse(r.out);
expect(env.mode).toBe("dry-run");
expect(env.summary.rows).toBeGreaterThan(0);
expect(readFile("src/consumer.ts")).toContain('from "~/api/client"');
});
});
describe("--diff-input", () => {
it("applies hunks parsed from a unified diff file", async () => {
const diff = `diff --git a/src/helper.ts b/src/helper.ts
--- a/src/helper.ts
+++ b/src/helper.ts
@@ -1,1 +1,1 @@
-export function helper(): number {
+export function worker(): number {
`;
const diffPath = join(projectRoot, "rename.patch");
writeFileSync(diffPath, diff, "utf8");
const r = await runCli(
["apply", "--diff-input", diffPath, "--yes", "--json"],
{ CODEMAP_ROOT: projectRoot },
);
expect(r.exitCode).toBe(0);
const env = JSON.parse(r.out);
expect(env.applied).toBe(true);
expect(readFile("src/helper.ts")).toContain("function worker()");
expect(readFile("src/helper.ts")).not.toContain("function helper()");
});
});
describe("--until-empty", () => {
beforeEach(async () => {
writeFileSync(
join(projectRoot, "src", "marked.ts"),
"// FIXME: todo item\nexport const MARKED = 1;\n",
"utf8",
);
const idx = await runCli(["--full"], { CODEMAP_ROOT: projectRoot });
expect(idx.exitCode).toBe(0);
});
it("terminates with empty after reindex when recipe rows exhaust", async () => {
const r = await runCli(
[
"apply",
"replace-marker-kind",
"--params",
"from_kind=FIXME,to_kind=XXX",
"--until-empty",
"--yes",
"--json",
],
{ CODEMAP_ROOT: projectRoot },
);
expect(r.exitCode).toBe(0);
const env = JSON.parse(r.out);
expect(env.terminated_by).toBe("empty");
expect(env.passes).toBeGreaterThanOrEqual(2);
expect(readFile("src/marked.ts")).toContain("// XXX:");
expect(readFile("src/marked.ts")).not.toContain("FIXME");
});
});
describe("--commit", () => {
beforeEach(() => {
execSync("git init", { cwd: projectRoot, stdio: "ignore" });
execSync("git config user.email test@codemap.test", {
cwd: projectRoot,
stdio: "ignore",
});
execSync("git config user.name Codemap Test", {
cwd: projectRoot,
stdio: "ignore",
});
execSync("git add -A", { cwd: projectRoot, stdio: "ignore" });
execSync('git commit -m "initial"', {
cwd: projectRoot,
stdio: "ignore",
});
});
it("exits non-zero when --until-empty hits max-passes cap", async () => {
writeFileSync(
join(projectRoot, "src", "marked-cap.ts"),
"// FIXME: todo item\nexport const MARKED_CAP = 1;\n",
"utf8",
);
const idx = await runCli(["--full"], { CODEMAP_ROOT: projectRoot });
expect(idx.exitCode).toBe(0);
const r = await runCli(
[
"apply",
"replace-marker-kind",
"--params",
"from_kind=FIXME,to_kind=XXX",
"--until-empty",
"--max-passes",
"1",
"--yes",
"--json",
],
{ CODEMAP_ROOT: projectRoot },
);
expect(r.exitCode).toBe(1);
const env = JSON.parse(r.out);
expect(env.terminated_by).toBe("cap");
});
it("rejects --commit when --until-empty hits max-passes cap", async () => {
writeFileSync(
join(projectRoot, "src", "marked.ts"),
"// FIXME: todo item\nexport const MARKED = 1;\n",
"utf8",
);
const idx = await runCli(["--full"], { CODEMAP_ROOT: projectRoot });
expect(idx.exitCode).toBe(0);
const r = await runCli(
[
"apply",
"replace-marker-kind",
"--params",
"from_kind=FIXME,to_kind=XXX",
"--until-empty",
"--max-passes",
"1",
"--yes",
"--commit",
"codemap: should not commit partial fixpoint",
"--json",
],
{ CODEMAP_ROOT: projectRoot },
);
expect(r.exitCode).toBe(1);
const env = JSON.parse(r.out);
expect(env.error).toContain('terminated_by "empty"');
expect(env.error).toContain("cap");
const log = execSync("git log --oneline", {
cwd: projectRoot,
encoding: "utf8",
});
expect(log.trim().split("\n").length).toBe(1);
});
it("creates a git commit after until-empty fixpoint reaches empty", async () => {
writeFileSync(
join(projectRoot, "src", "marked.ts"),
"// FIXME: todo item\nexport const MARKED = 1;\n",
"utf8",
);
const idx = await runCli(["--full"], { CODEMAP_ROOT: projectRoot });
expect(idx.exitCode).toBe(0);
const r = await runCli(
[
"apply",
"replace-marker-kind",
"--params",
"from_kind=FIXME,to_kind=XXX",
"--until-empty",
"--yes",
"--commit",
"codemap: migrate FIXME markers",
"--json",
],
{ CODEMAP_ROOT: projectRoot },
);
expect(r.exitCode).toBe(0);
const env = JSON.parse(r.out);
expect(env.terminated_by).toBe("empty");
expect(env.applied).toBe(true);
const log = execSync("git log -1 --format=%s", {
cwd: projectRoot,
encoding: "utf8",
});
expect(log.trim()).toBe("codemap: migrate FIXME markers");
expect(readFile("src/marked.ts")).toContain("// XXX:");
});
it("creates a git commit for touched files after a clean apply", async () => {
const r = await runCli(
[
"apply",
"rename-preview",
"--params",
"old=helper,new=worker",
"--yes",
"--commit",
"codemap: rename helper to worker",
"--json",
],
{ CODEMAP_ROOT: projectRoot },
);
expect(r.exitCode).toBe(0);
const log = execSync("git log -1 --format=%s", {
cwd: projectRoot,
encoding: "utf8",
});
expect(log.trim()).toBe("codemap: rename helper to worker");
expect(readFile("src/helper.ts")).toContain("function worker()");
});
});
});