atomwrite 0.1.26

Atomic file operations CLI for LLM agents — read, write, edit, search, replace with NDJSON output
Documentation
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
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
// SPDX-License-Identifier: MIT OR Apache-2.0

mod common;

#[test]
fn edit_old_new_replaces_exact_match() {
    let dir = tempfile::tempdir().expect("tempdir");
    let path = common::create_test_file(dir.path(), "edit.txt", "fn old_name() {}\n");

    let output = common::atomwrite()
        .args([
            "--workspace",
            dir.path().to_str().unwrap(),
            "edit",
            "--old",
            "old_name",
            "--new",
            "new_name",
        ])
        .arg(&path)
        .output()
        .expect("run");

    assert!(output.status.success());
    let events = common::parse_ndjson(&output.stdout);
    assert_eq!(events[0]["type"], "edit");
    assert_eq!(events[0]["mode"], "exact");

    let content = std::fs::read_to_string(&path).expect("read");
    assert!(content.contains("new_name"));
    assert!(!content.contains("old_name"));
}

#[test]
fn edit_old_not_found_exits_65() {
    let dir = tempfile::tempdir().expect("tempdir");
    let path = common::create_test_file(dir.path(), "nofind.txt", "hello world\n");

    let output = common::atomwrite()
        .args([
            "--workspace",
            dir.path().to_str().unwrap(),
            "edit",
            "--old",
            "NONEXISTENT",
            "--new",
            "replacement",
        ])
        .arg(&path)
        .output()
        .expect("run");

    assert_eq!(output.status.code(), Some(65));
}

#[test]
fn edit_after_line_inserts_content() {
    let dir = tempfile::tempdir().expect("tempdir");
    let path = common::create_test_file(dir.path(), "lines.txt", "line1\nline2\nline3\n");

    let output = common::atomwrite()
        .args([
            "--workspace",
            dir.path().to_str().unwrap(),
            "edit",
            "--after-line",
            "2",
        ])
        .arg(&path)
        .write_stdin("inserted\n")
        .output()
        .expect("run");

    assert!(output.status.success());
    let content = std::fs::read_to_string(&path).expect("read");
    let lines: Vec<&str> = content.lines().collect();
    assert_eq!(lines[0], "line1");
    assert_eq!(lines[1], "line2");
    assert_eq!(lines[2], "inserted");
    assert_eq!(lines[3], "line3");
}

#[test]
fn edit_delete_range_removes_lines() {
    let dir = tempfile::tempdir().expect("tempdir");
    let path = common::create_test_file(dir.path(), "del.txt", "a\nb\nc\nd\ne\n");

    let output = common::atomwrite()
        .args([
            "--workspace",
            dir.path().to_str().unwrap(),
            "edit",
            "--delete-range",
            "2:4",
        ])
        .arg(&path)
        .output()
        .expect("run");

    assert!(output.status.success());
    let content = std::fs::read_to_string(&path).expect("read");
    let lines: Vec<&str> = content.lines().collect();
    assert_eq!(lines, vec!["a", "e"]);
}

#[test]
fn edit_after_match_inserts_after_marker() {
    let dir = tempfile::tempdir().expect("tempdir");
    let path = common::create_test_file(
        dir.path(),
        "marker.txt",
        "use std::io;\nuse std::fs;\n\nfn main() {}\n",
    );

    let output = common::atomwrite()
        .args([
            "--workspace",
            dir.path().to_str().unwrap(),
            "edit",
            "--after-match",
            "use std::fs;",
        ])
        .arg(&path)
        .write_stdin("use std::path::Path;")
        .output()
        .expect("run");

    assert!(output.status.success());
    let content = std::fs::read_to_string(&path).expect("read");
    assert!(content.contains("use std::fs;\nuse std::path::Path;\n"));
}

// --- GAP 06: --old/--new múltiplos pares ---

#[test]
fn edit_multiple_pairs_without_multi() {
    let dir = tempfile::tempdir().expect("tempdir");
    let path = common::create_test_file(dir.path(), "multi_pair.txt", "alpha bravo charlie\n");

    let output = common::atomwrite()
        .args([
            "--workspace",
            dir.path().to_str().unwrap(),
            "edit",
            "--old",
            "alpha",
            "--new",
            "ALPHA",
            "--old",
            "charlie",
            "--new",
            "CHARLIE",
        ])
        .arg(&path)
        .output()
        .expect("run");

    assert!(
        output.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let content = std::fs::read_to_string(&path).expect("read");
    assert!(content.contains("ALPHA"), "first pair should be applied");
    assert!(content.contains("CHARLIE"), "second pair should be applied");
    assert!(!content.contains("alpha"));
    assert!(!content.contains("charlie"));
}

#[test]
fn edit_old_new_mismatch_count() {
    let dir = tempfile::tempdir().expect("tempdir");
    let path = common::create_test_file(dir.path(), "mismatch.txt", "content\n");

    let output = common::atomwrite()
        .args([
            "--workspace",
            dir.path().to_str().unwrap(),
            "edit",
            "--old",
            "A",
            "--new",
            "B",
            "--old",
            "C",
        ])
        .arg(&path)
        .output()
        .expect("run");

    assert!(
        !output.status.success(),
        "mismatched --old/--new counts should fail"
    );
}

#[test]
fn edit_multi_ndjson_stdin() {
    let dir = tempfile::tempdir().expect("tempdir");
    let path = common::create_test_file(
        dir.path(),
        "multi_stdin.txt",
        "line_one\nline_two\nline_three\n",
    );

    let ndjson = r#"{"op":"exact","old":"line_one","new":"LINE_ONE"}
{"op":"exact","old":"line_three","new":"LINE_THREE"}"#;

    let output = common::atomwrite()
        .args([
            "--workspace",
            dir.path().to_str().unwrap(),
            "edit",
            "--multi",
        ])
        .arg(&path)
        .write_stdin(ndjson)
        .output()
        .expect("run");

    assert!(
        output.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let content = std::fs::read_to_string(&path).expect("read");
    assert!(content.contains("LINE_ONE"));
    assert!(content.contains("LINE_THREE"));
    assert!(content.contains("line_two"));
}

// --- GAP 09: fuzzy com pontuação ---

#[test]
fn edit_fuzzy_punctuation_spaces() {
    let dir = tempfile::tempdir().expect("tempdir");
    let path = common::create_test_file(
        dir.path(),
        "fuzzy_punct.rs",
        "fn calculate( items: Vec<i32> ) -> i32 {\n    items.iter().sum()\n}\n",
    );

    let output = common::atomwrite()
        .args([
            "--workspace",
            dir.path().to_str().unwrap(),
            "edit",
            "--old",
            "fn calculate(items: Vec<i32>) -> i32 {",
            "--new",
            "fn calc(items: Vec<i32>) -> i32 {",
            "--fuzzy",
            "aggressive",
        ])
        .arg(&path)
        .output()
        .expect("run");

    assert!(
        output.status.success(),
        "fuzzy should match despite punctuation whitespace, stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let content = std::fs::read_to_string(&path).expect("read");
    assert!(
        content.contains("fn calc("),
        "replacement should be applied"
    );
}

#[test]
fn edit_fuzzy_single_line_block_anchor() {
    let dir = tempfile::tempdir().expect("tempdir");
    let path = common::create_test_file(dir.path(), "anchor.rs", "let result = calculate(x, y);\n");

    let output = common::atomwrite()
        .args([
            "--workspace",
            dir.path().to_str().unwrap(),
            "edit",
            "--old",
            "let  result = calculate( x,  y );",
            "--new",
            "let result = compute(x, y);",
            "--fuzzy",
            "aggressive",
        ])
        .arg(&path)
        .output()
        .expect("run");

    assert!(
        output.status.success(),
        "fuzzy should match single-line pattern with punctuation spaces, stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let content = std::fs::read_to_string(&path).expect("read");
    assert!(content.contains("compute"), "replacement should be applied");
    assert!(
        !content.contains("calculate"),
        "original should be replaced"
    );
}

#[test]
fn edit_fuzzy_multiline_first_line_variation() {
    let dir = tempfile::tempdir().expect("tempdir");
    let path = common::create_test_file(
        dir.path(),
        "multi.rs",
        "fn process( items: Vec<i32> ) {\n    for item in items {\n        println!(\"{}\", item);\n    }\n}\n",
    );

    let output = common::atomwrite()
        .args([
            "--workspace",
            dir.path().to_str().unwrap(),
            "edit",
            "--old",
            "fn process(items: Vec<i32>) {\n    for item in items {\n        println!(\"{}\", item);\n    }\n}",
            "--new",
            "fn handle(items: Vec<i32>) {\n    for item in items {\n        println!(\"{}\", item);\n    }\n}",
            "--fuzzy",
            "aggressive",
        ])
        .arg(&path)
        .output()
        .expect("run");

    assert!(
        output.status.success(),
        "fuzzy should match multiline with first-line whitespace variation, stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let content = std::fs::read_to_string(&path).expect("read");
    assert!(
        content.contains("fn handle("),
        "replacement should be applied"
    );
    assert!(
        !content.contains("fn process("),
        "original should be replaced"
    );
}

// NOTE: --multi + --old/--new CLI injection not implemented in v0.1.1
// cmd_edit_multi reads only stdin; CLI --old/--new are ignored in multi mode
// This is a known limitation, not a bug — multiple pairs work WITHOUT --multi

#[test]
fn edit_expect_checksum_rejects_drift() {
    let dir = tempfile::tempdir().expect("tempdir");
    let path = common::create_test_file(dir.path(), "edit_drift.txt", "hello world\n");

    let output = common::atomwrite()
        .args([
            "--workspace",
            dir.path().to_str().unwrap(),
            "edit",
            "--old",
            "hello",
            "--new",
            "goodbye",
            "--expect-checksum",
            "wrong_checksum_value",
        ])
        .arg(&path)
        .output()
        .expect("run");

    assert_eq!(
        output.status.code(),
        Some(82),
        "edit with wrong checksum should exit 82"
    );
    let events = common::parse_ndjson(&output.stdout);
    assert_eq!(events[0]["code"], "STATE_DRIFT");

    let content = std::fs::read_to_string(&path).expect("read");
    assert_eq!(content, "hello world\n", "file should not be modified");
}

#[test]
fn edit_expect_checksum_accepts_correct() {
    let dir = tempfile::tempdir().expect("tempdir");
    let path = common::create_test_file(dir.path(), "edit_locked.txt", "old_value = 42;\n");

    let hash_out = common::atomwrite()
        .args(["--workspace", dir.path().to_str().unwrap(), "hash"])
        .arg(&path)
        .output()
        .expect("hash");
    let checksum = common::parse_ndjson(&hash_out.stdout)[0]["checksum"]
        .as_str()
        .expect("checksum")
        .to_string();

    let output = common::atomwrite()
        .args([
            "--workspace",
            dir.path().to_str().unwrap(),
            "edit",
            "--old",
            "old_value",
            "--new",
            "new_value",
            "--expect-checksum",
            &checksum,
        ])
        .arg(&path)
        .output()
        .expect("run");

    assert!(
        output.status.success(),
        "edit with correct checksum should succeed: {:?}",
        output.status
    );
    let content = std::fs::read_to_string(&path).expect("read");
    assert!(content.contains("new_value"));
}

// ─── G117: multi-pair fuzzy parity, per-pair reporting, --partial ────────────

#[test]
fn edit_multi_fuzzy_pair_succeeds() {
    let dir = tempfile::tempdir().expect("tempdir");
    let path = common::create_test_file(dir.path(), "g117a.rs", "fn alpha() {}\n    let x = 1;\n");

    // Pair 2 has divergent whitespace ("let  x" vs "let x"): before G117 the
    // multi path was exact-only and this batch failed entirely.
    let output = common::atomwrite()
        .args([
            "--workspace",
            dir.path().to_str().unwrap(),
            "edit",
            "--old",
            "alpha",
            "--new",
            "ALPHA",
            "--old",
            "let  x = 1;",
            "--new",
            "let y = 2;",
        ])
        .arg(&path)
        .output()
        .expect("run");

    assert!(
        output.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let events = common::parse_ndjson(&output.stdout);
    assert_eq!(events[0]["type"], "edit");
    assert_eq!(events[0]["edits"], 2);
    assert_eq!(events[0]["mode"], "fuzzy-multi(2)");
    assert_eq!(events[0]["fuzzy"], true);

    let content = std::fs::read_to_string(&path).expect("read");
    assert!(content.contains("ALPHA"));
    assert!(content.contains("let y = 2;"));
}

#[test]
fn edit_multi_all_exact_keeps_mode_exact_multi() {
    let dir = tempfile::tempdir().expect("tempdir");
    let path = common::create_test_file(dir.path(), "g117b.txt", "alpha\nbeta\n");

    let output = common::atomwrite()
        .args([
            "--workspace",
            dir.path().to_str().unwrap(),
            "edit",
            "--old",
            "alpha",
            "--new",
            "ALPHA",
            "--old",
            "beta",
            "--new",
            "BETA",
        ])
        .arg(&path)
        .output()
        .expect("run");

    assert!(output.status.success());
    let events = common::parse_ndjson(&output.stdout);
    assert_eq!(events[0]["mode"], "exact-multi(2)");
    assert_eq!(events[0]["fuzzy"], false);
}

#[test]
fn edit_multi_success_includes_pair_results() {
    let dir = tempfile::tempdir().expect("tempdir");
    let path = common::create_test_file(dir.path(), "g117c.txt", "alpha\nbeta\n");

    let output = common::atomwrite()
        .args([
            "--workspace",
            dir.path().to_str().unwrap(),
            "edit",
            "--old",
            "alpha",
            "--new",
            "ALPHA",
            "--old",
            "beta",
            "--new",
            "BETA",
        ])
        .arg(&path)
        .output()
        .expect("run");

    assert!(output.status.success());
    let events = common::parse_ndjson(&output.stdout);
    assert_eq!(events[0]["pairs_total"], 2);
    let pairs = events[0]["pair_results"]
        .as_array()
        .expect("pair_results array");
    assert_eq!(pairs.len(), 2);
    assert_eq!(pairs[0]["index"], 1);
    assert_eq!(pairs[0]["matched"], true);
    assert_eq!(pairs[0]["strategy"], "exact");
    assert_eq!(pairs[1]["index"], 2);
    assert_eq!(pairs[1]["matched"], true);
}

#[test]
fn edit_multi_error_reports_failed_pair_index() {
    let dir = tempfile::tempdir().expect("tempdir");
    let before = "alpha\nbeta\ngamma\n";
    let path = common::create_test_file(dir.path(), "g117d.txt", before);

    let output = common::atomwrite()
        .args([
            "--workspace",
            dir.path().to_str().unwrap(),
            "edit",
            "--old",
            "alpha",
            "--new",
            "ALPHA",
            "--old",
            "NAOEXISTE",
            "--new",
            "X",
        ])
        .arg(&path)
        .output()
        .expect("run");

    assert_eq!(output.status.code(), Some(65));
    let events = common::parse_ndjson(&output.stdout);
    assert_eq!(events[0]["error"], true);
    assert_eq!(events[0]["code"], "INVALID_INPUT");
    assert_eq!(events[0]["failed_pair_index"], 2);
    assert_eq!(events[0]["pairs_total"], 2);
    let pairs = events[0]["pair_results"]
        .as_array()
        .expect("pair_results array");
    assert_eq!(pairs.len(), 2);
    assert_eq!(pairs[0]["matched"], true);
    assert_eq!(pairs[1]["matched"], false);

    // All-or-nothing default: nothing was written, file intact.
    let content = std::fs::read_to_string(&path).expect("read");
    assert_eq!(content, before);
}

#[test]
fn edit_multi_fuzzy_off_preserves_exact_behavior() {
    let dir = tempfile::tempdir().expect("tempdir");
    let before = "    let x = 1;\n";
    let path = common::create_test_file(dir.path(), "g117e.rs", before);

    // Divergent whitespace pair that fuzzy=auto would rescue must still fail
    // with --fuzzy off (pre-G117 exact behavior preserved on opt-out).
    let output = common::atomwrite()
        .args([
            "--workspace",
            dir.path().to_str().unwrap(),
            "edit",
            "--fuzzy",
            "off",
            "--old",
            "let  x = 1;",
            "--new",
            "let y = 2;",
            "--old",
            "let x",
            "--new",
            "let z",
        ])
        .arg(&path)
        .output()
        .expect("run");

    assert_eq!(output.status.code(), Some(65));
    let content = std::fs::read_to_string(&path).expect("read");
    assert_eq!(content, before);
}

#[test]
fn edit_partial_applies_valid_pairs() {
    let dir = tempfile::tempdir().expect("tempdir");
    let path = common::create_test_file(dir.path(), "g117f.txt", "alpha\nbeta\n");

    let output = common::atomwrite()
        .args([
            "--workspace",
            dir.path().to_str().unwrap(),
            "edit",
            "--partial",
            "--old",
            "alpha",
            "--new",
            "ALPHA",
            "--old",
            "NAOEXISTE",
            "--new",
            "X",
        ])
        .arg(&path)
        .output()
        .expect("run");

    assert!(
        output.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let events = common::parse_ndjson(&output.stdout);
    assert_eq!(events[0]["edits"], 1);
    assert_eq!(events[0]["pairs_total"], 2);
    let pairs = events[0]["pair_results"]
        .as_array()
        .expect("pair_results array");
    assert_eq!(pairs.len(), 2);
    assert_eq!(pairs[0]["matched"], true);
    assert_eq!(pairs[1]["matched"], false);

    let content = std::fs::read_to_string(&path).expect("read");
    assert!(content.contains("ALPHA"));
}

#[test]
fn edit_partial_zero_matches_exits_1() {
    let dir = tempfile::tempdir().expect("tempdir");
    let before = "alpha\n";
    let path = common::create_test_file(dir.path(), "g117g.txt", before);

    let output = common::atomwrite()
        .args([
            "--workspace",
            dir.path().to_str().unwrap(),
            "edit",
            "--partial",
            "--old",
            "NAOEXISTE1",
            "--new",
            "X",
            "--old",
            "NAOEXISTE2",
            "--new",
            "Y",
        ])
        .arg(&path)
        .output()
        .expect("run");

    assert_eq!(output.status.code(), Some(1));
    let events = common::parse_ndjson(&output.stdout);
    assert_eq!(events[0]["code"], "NO_MATCHES");
    let content = std::fs::read_to_string(&path).expect("read");
    assert_eq!(content, before);
}

#[test]
fn edit_partial_dry_run_no_write() {
    let dir = tempfile::tempdir().expect("tempdir");
    let before = "alpha\nbeta\n";
    let path = common::create_test_file(dir.path(), "g117h.txt", before);

    let output = common::atomwrite()
        .args([
            "--workspace",
            dir.path().to_str().unwrap(),
            "edit",
            "--partial",
            "--dry-run",
            "--old",
            "alpha",
            "--new",
            "ALPHA",
            "--old",
            "NAOEXISTE",
            "--new",
            "X",
        ])
        .arg(&path)
        .output()
        .expect("run");

    assert!(output.status.success());
    let events = common::parse_ndjson(&output.stdout);
    assert_eq!(events[0]["type"], "plan");
    let content = std::fs::read_to_string(&path).expect("read");
    assert_eq!(content, before);
}

// ============================================================================
// G117 follow-up: edge cases not covered by the original multi-pair suite
// ============================================================================

/// G117 edge case: Unicode (non-ASCII) `--old`/`--new` must work via
/// exact UTF-8 byte match. LLM agents commonly emit identifiers with
/// diacritics (e.g. `José`, `naïve`, `über`) and the fuzzy cascade must
/// not corrupt them via normalization. Single-pair `edit` replaces
/// only the FIRST occurrence (use multi-pair to replace all); this
/// test exercises the byte-exact match for the diacritic `ção`.
#[test]
fn edit_unicode_old_new_exact_match() {
    let dir = tempfile::tempdir().expect("tempdir");
    let before = "alpha ção beta ção\n";
    let path = common::create_test_file(dir.path(), "g117_unicode.txt", before);

    let output = common::atomwrite()
        .args([
            "--workspace",
            dir.path().to_str().unwrap(),
            "edit",
            "--old",
            "ção",
            "--new",
            "AÇÃO",
        ])
        .arg(&path)
        .output()
        .expect("run");

    assert!(
        output.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let content = std::fs::read_to_string(&path).expect("read");
    assert_eq!(content, "alpha AÇÃO beta ção\n");
}

/// G117 edge case: CRLF (Windows-style) line endings must NOT trip the
/// fuzzy cascade. The first occurrence of `--old` must be replaced and
/// the line-ending normalization in `cmd_edit` must keep the original
/// `\r\n` endings intact.
#[test]
fn edit_crlf_line_endings_preserve_eol_after_replace() {
    let dir = tempfile::tempdir().expect("tempdir");
    let before = "alpha\r\nbeta\r\ngamma\r\n";
    let path = common::create_test_file(dir.path(), "g117_crlf.txt", before);

    let output = common::atomwrite()
        .args([
            "--workspace",
            dir.path().to_str().unwrap(),
            "edit",
            "--old",
            "beta",
            "--new",
            "BETA",
        ])
        .arg(&path)
        .output()
        .expect("run");

    assert!(
        output.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let content = std::fs::read_to_string(&path).expect("read");
    // CR-LF must be preserved byte-for-byte (not collapsed to LF).
    assert_eq!(content, "alpha\r\nBETA\r\ngamma\r\n");
}

/// G117 edge case: multi-pair with the SAME `--old` value in two
/// different pairs must apply each pair in sequence against the
/// evolving content. This guards against an off-by-one bug where
/// the second pair would skip the just-inserted first replacement.
#[test]
fn edit_multi_pair_same_old_appears_twice_applies_both() {
    let dir = tempfile::tempdir().expect("tempdir");
    // "foo" appears twice. Two pairs rename the first to "FOO_A" and
    // the second to "FOO_B". After both pairs, both occurrences are
    // renamed and they differ.
    let before = "foo bar foo\n";
    let path = common::create_test_file(dir.path(), "g117_repeat.txt", before);

    let output = common::atomwrite()
        .args([
            "--workspace",
            dir.path().to_str().unwrap(),
            "edit",
            "--old",
            "foo",
            "--new",
            "FOO_A",
            "--old",
            "foo",
            "--new",
            "FOO_B",
        ])
        .arg(&path)
        .output()
        .expect("run");

    assert!(
        output.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let content = std::fs::read_to_string(&path).expect("read");
    assert_eq!(content, "FOO_A bar FOO_B\n");
}

// ============================================================================
// G117 follow-up round 2: 6 additional edge cases from the v0.1.15/v0.1.18 gap list
// ============================================================================

/// G117 edge case: NFC vs NFD Unicode normalization is NOT performed by
/// the byte-exact `exact` strategy. The precomposed `ã` (U+00E3, 2 bytes)
/// and the decomposed `ã` (U+0061 U+0303, 3 bytes) are distinct byte
/// sequences; the cascade must fail (exit 65 INVALID_INPUT) rather than
/// silently match. This is intentional: silent Unicode normalization
/// would corrupt identifiers and string literals.
#[test]
fn edit_nfc_vs_nfd_does_not_match() {
    let dir = tempfile::tempdir().expect("tempdir");
    // NFD: a + combining tilde (3 bytes total: 0x61 0xCC 0x83)
    let nfd_a = "a\u{0303}";
    assert_eq!(nfd_a.len(), 3, "NFD 'ã' must be 3 bytes");
    let before = format!("José{nfd_a} Silva\n");
    let path = common::create_test_file(dir.path(), "g117_nfd.txt", &before);

    // NFC: ã (2 bytes: 0xC3 0xA3)
    let nfc_a = "ã";
    assert_eq!(nfc_a.len(), 2, "NFC 'ã' must be 2 bytes");

    let output = common::atomwrite()
        .args([
            "--workspace",
            dir.path().to_str().unwrap(),
            "edit",
            "--old",
            nfc_a,
            "--new",
            "X",
        ])
        .arg(&path)
        .output()
        .expect("run");

    // Byte-exact: must fail because the file has NFD, --old is NFC.
    assert_eq!(output.status.code(), Some(65));
    let events = common::parse_ndjson(&output.stdout);
    assert_eq!(events[0]["error"], true);
    assert_eq!(events[0]["code"], "INVALID_INPUT");

    // File unchanged.
    let content = std::fs::read_to_string(&path).expect("read");
    assert_eq!(content, before);
}

/// G117 edge case: `--old` containing only whitespace matches the
/// FIRST byte-exact occurrence via the `exact` strategy (no cascade
/// needed). The file has leading and trailing runs of 3 spaces; the
/// first run is replaced and the trailing run stays untouched because
/// `edit` replaces only the first match.
#[test]
fn edit_old_only_whitespace_matches_first_run_exact() {
    let dir = tempfile::tempdir().expect("tempdir");
    let before = "   hello world   \n";
    let path = common::create_test_file(dir.path(), "g117_ws.txt", before);

    let output = common::atomwrite()
        .args([
            "--workspace",
            dir.path().to_str().unwrap(),
            "edit",
            "--old",
            "   ",
            "--new",
            "X",
        ])
        .arg(&path)
        .output()
        .expect("run");

    assert!(
        output.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let events = common::parse_ndjson(&output.stdout);
    // 3 spaces appear byte-exactly at the start of the file → exact
    // strategy wins immediately. The fuzzy cascade is NOT triggered.
    assert_eq!(events[0]["fuzzy"], false);
    assert_eq!(events[0]["strategy"], "exact");

    let content = std::fs::read_to_string(&path).expect("read");
    // First run replaced; trailing 3 spaces preserved.
    assert_eq!(content, "Xhello world   \n");
}

/// G117 edge case: `--new ""` must perform a clean deletion of the
/// matched substring. The result must NOT contain any residual
/// characters (no phantom spaces, no BOM, no leftover punctuation).
#[test]
fn edit_new_empty_string_deletion_removes_only_match() {
    let dir = tempfile::tempdir().expect("tempdir");
    let before = "keep this, remove this, keep this too\n";
    let path = common::create_test_file(dir.path(), "g117_del.txt", before);

    let output = common::atomwrite()
        .args([
            "--workspace",
            dir.path().to_str().unwrap(),
            "edit",
            "--old",
            "remove this, ",
            "--new",
            "",
        ])
        .arg(&path)
        .output()
        .expect("run");

    assert!(
        output.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let content = std::fs::read_to_string(&path).expect("read");
    assert_eq!(content, "keep this, keep this too\n");
    // Sanity: no leftover "remove" substring and no phantom punctuation.
    assert!(!content.contains("remove"));
    assert!(!content.contains(", ,"));
}

/// G117 edge case: a UTF-8 BOM (`\xEF\xBB\xBF`) at the start of the
/// file is stripped by the reader/loader before the byte-exact match.
/// The match still succeeds because the substring after the BOM
/// (`hello`) is what the strategy sees. This documents the BOM-
/// stripping behavior so future maintainers don't lose the
/// regression coverage. NOTE: the BOM is consumed (not preserved
/// through edit) — this is a known design choice, not a bug.
#[test]
fn edit_utf8_bom_is_stripped_before_match() {
    let dir = tempfile::tempdir().expect("tempdir");
    let before = "\u{FEFF}hello\n";
    let path = common::create_test_file(dir.path(), "g117_bom.txt", before);

    let output = common::atomwrite()
        .args([
            "--workspace",
            dir.path().to_str().unwrap(),
            "edit",
            "--old",
            "hello",
            "--new",
            "world",
        ])
        .arg(&path)
        .output()
        .expect("run");

    assert!(
        output.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let events = common::parse_ndjson(&output.stdout);
    // BOM is consumed by the loader — only the post-BOM payload is
    // counted. bytes_before excludes the 3 BOM bytes.
    assert_eq!(events[0]["bytes_before"], 6);
    assert_eq!(events[0]["strategy"], "exact");

    let content = std::fs::read_to_string(&path).expect("read");
    // BOM is stripped; only "world\n" remains.
    assert_eq!(content, "world\n");
    assert!(!content.starts_with('\u{FEFF}'));
}

/// G117 edge case: `--partial` with ZERO applicable pairs must exit
/// with code 1 (`NO_MATCHES`), not 65 (`INVALID_INPUT`). The
/// all-or-nothing default is overridden; partial mode is permissive
/// when at least one pair matches, but if NO pair matches it falls
/// through to NO_MATCHES. This is a documented semantic.
#[test]
fn edit_partial_zero_applicable_pairs_exits_no_matches() {
    let dir = tempfile::tempdir().expect("tempdir");
    let before = "foo bar\n";
    let path = common::create_test_file(dir.path(), "g117_pz.txt", before);

    let output = common::atomwrite()
        .args([
            "--workspace",
            dir.path().to_str().unwrap(),
            "edit",
            "--partial",
            "--old",
            "NAOEXISTE_ZZ",
            "--new",
            "X",
        ])
        .arg(&path)
        .output()
        .expect("run");

    // Documented behavior: single pair with --partial and zero matches
    // returns NO_MATCHES (exit 1), not INVALID_INPUT (exit 65).
    assert_eq!(output.status.code(), Some(1));
    let events = common::parse_ndjson(&output.stdout);
    assert_eq!(events[0]["code"], "NO_MATCHES");
    // File must be untouched.
    let content = std::fs::read_to_string(&path).expect("read");
    assert_eq!(content, before);
}

/// G117 edge case: block_anchor and context_aware strategies are
/// designed to be MORE permissive than 0.70. With one missing token
/// in a 4-token block, the trigram similarity lands at ~0.76, which
/// is BELOW the context_aware threshold (0.80) but WITHIN the
/// ±0.05 fallback window of match_context_aware. This means the
/// cascade DOES match and the file is rewritten. This test pins down
/// the tolerance behavior so any future tightening of the thresholds
/// is caught by the regression suite.
#[test]
fn edit_one_token_drift_in_block_matches_via_context_aware_fallback() {
    let dir = tempfile::tempdir().expect("tempdir");
    let before = "alpha bravo charlie delta\n";
    let path = common::create_test_file(dir.path(), "g117_thr.txt", before);

    // 3 of 4 tokens match; "charlie" is replaced by "echo" in --old.
    let output = common::atomwrite()
        .args([
            "--workspace",
            dir.path().to_str().unwrap(),
            "edit",
            "--old",
            "alpha bravo echo delta",
            "--new",
            "X",
        ])
        .arg(&path)
        .output()
        .expect("run");

    assert!(
        output.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let events = common::parse_ndjson(&output.stdout);
    // Similarity falls in the [0.75, 0.80) band: not high enough for
    // the strict context_aware threshold (0.80) but caught by the
    // ±0.05 fallback window. Strategy must be either block_anchor or
    // context_aware with a similarity in that band.
    assert_eq!(events[0]["fuzzy"], true);
    let strategy = events[0]["strategy"].as_str().expect("strategy");
    assert!(
        strategy == "block_anchor" || strategy == "context_aware" || strategy == "context_aware_jw",
        "expected block_anchor, context_aware, or context_aware_jw, got {strategy}"
    );
    if let Some(sim) = events[0]["similarity"].as_f64() {
        assert!(
            (0.70..=0.95).contains(&sim),
            "similarity {sim} outside expected band [0.70, 0.95]"
        );
    }

    // File rewritten: the entire line collapsed to "X\n".
    let content = std::fs::read_to_string(&path).expect("read");
    assert_eq!(content, "X\n");
}

// ============================================================================
// v0.1.20 GAP-2026-005b: edit --partial single-pair still returns NoMatches
// ============================================================================

/// GAP-005b: single-pair --partial + unmatched pattern returns NoMatches
/// (exit 1) without writing anything. --partial does NOT make single-pair
/// matches "best-effort"; it only applies to multi-pair.
#[test]
fn v0_1_20_edit_partial_single_pair_unmatched_returns_no_matches() {
    let dir = tempfile::tempdir().expect("tempdir");
    common::create_test_file(dir.path(), "foo.txt", "hello world\n");

    let output = common::atomwrite()
        .args([
            "--workspace",
            dir.path().to_str().unwrap(),
            "edit",
            "--old",
            "NONEXISTENT_TEXT",
            "--new",
            "X",
            "--partial",
        ])
        .arg(dir.path().join("foo.txt"))
        .output()
        .expect("run");

    assert_eq!(
        output.status.code(),
        Some(1),
        "single-pair --partial + no match must be NoMatches (exit 1)"
    );
    let events = common::parse_ndjson(&output.stdout);
    assert_eq!(events[0]["code"], "NO_MATCHES");
    let content = std::fs::read_to_string(dir.path().join("foo.txt")).unwrap();
    assert_eq!(content, "hello world\n", "no write on NoMatches");
}

// ============================================================================
// v0.1.20 GAP-2026-006: diff --algorithm myers/patience/lcs
// ============================================================================

/// GAP-006: --algorithm myers runs successfully.
#[test]
fn v0_1_20_diff_algorithm_myers_runs() {
    let dir = tempfile::tempdir().expect("tempdir");
    let a = dir.path().join("a.txt");
    let b = dir.path().join("b.txt");
    std::fs::write(&a, "the quick brown fox\n").unwrap();
    std::fs::write(&b, "the quick red fox\n").unwrap();

    let output = common::atomwrite()
        .args([
            "--workspace",
            dir.path().to_str().unwrap(),
            "diff",
            "--algorithm",
            "myers",
        ])
        .arg(&a)
        .arg(&b)
        .output()
        .expect("run");

    assert!(
        output.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
}

/// GAP-006: --algorithm lcs runs successfully on identical files.
#[test]
fn v0_1_20_diff_algorithm_lcs_on_identical_files() {
    let dir = tempfile::tempdir().expect("tempdir");
    let a = dir.path().join("a.txt");
    let b = dir.path().join("b.txt");
    std::fs::write(&a, "abc\n").unwrap();
    std::fs::write(&b, "abc\n").unwrap();

    let output = common::atomwrite()
        .args([
            "--workspace",
            dir.path().to_str().unwrap(),
            "diff",
            "--algorithm",
            "lcs",
            "--stat",
        ])
        .arg(&a)
        .arg(&b)
        .output()
        .expect("run");

    assert!(output.status.success());
    let events = common::parse_ndjson(&output.stdout);
    assert!(events[0]["identical"].as_bool().unwrap());
}