code-kb-core 1.5.0

Core library for code-kb AST fact querying, slicing, and progressive disclosure
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
use code_kb_core::{
    MatchTier, Occurrence, Workspace, edit_file, find_julie_extract_binary, open_read_only,
    replace_symbol_body, safe_tempdir, scan_workspace, slicer,
};
use std::fs;
#[cfg(unix)]
use std::os::unix::fs::PermissionsExt;

#[test]
fn test_replace_symbol_body_atomic() {
    let _extract_bin =
        find_julie_extract_binary().expect("julie-extract binary must be present for tests");

    let temp_dir = safe_tempdir();
    let root = temp_dir.path().to_path_buf();

    // Create a sample rust file
    let src_dir = root.join("src");
    fs::create_dir_all(&src_dir).unwrap();
    let file_path = src_dir.join("calc.rs");

    let initial_code = r#"pub fn add_numbers(a: i32, b: i32) -> i32 {
    a + b
}
"#;
    fs::write(&file_path, initial_code).unwrap();

    let ws = Workspace::new(root.clone());
    let db_path = root.join("test.db");

    scan_workspace(&ws, &db_path, true).expect("Scan failed");
    let conn = open_read_only(&db_path).unwrap();

    let new_body = r#"{
    let sum = a + b;
    sum * 2
}"#;

    let res = replace_symbol_body(
        &ws,
        &db_path,
        &conn,
        "add_numbers",
        "src/calc.rs",
        new_body,
        None,
    )
    .expect("replace_symbol_body failed");

    assert_eq!(res.symbol_name, "add_numbers");
    assert_eq!(res.file_path, "src/calc.rs");

    // Verify on disk
    let disk_content = fs::read_to_string(&file_path).unwrap();
    assert!(disk_content.contains("let sum = a + b;"));
    assert!(disk_content.contains("sum * 2"));

    // Verify in db
    let updated_symbol =
        code_kb_core::get_symbol_by_name(&conn, "add_numbers", Some("src/calc.rs"))
            .unwrap()
            .unwrap();

    let body = slicer::slice_symbol_body(&file_path, &updated_symbol).unwrap();
    assert_eq!(body.trim(), new_body.trim());
}

#[test]
fn test_replace_symbol_body_shrunk_file_does_not_panic() {
    let _extract_bin =
        find_julie_extract_binary().expect("julie-extract binary must be present for tests");

    let temp_dir = safe_tempdir();
    let root = temp_dir.path().to_path_buf();

    let src_dir = root.join("src");
    fs::create_dir_all(&src_dir).unwrap();
    let file_path = src_dir.join("calc.rs");

    let initial_code = r#"pub fn long_function_name(a: i32, b: i32) -> i32 {
    let mut x = a * 2;
    let mut y = b * 3;
    x + y
}
"#;
    fs::write(&file_path, initial_code).unwrap();

    let ws = Workspace::new(root.clone());
    let db_path = root.join("test.db");

    scan_workspace(&ws, &db_path, true).expect("Scan failed");
    let conn = open_read_only(&db_path).unwrap();

    // Now shrink the file to just 5 bytes without updating the database
    fs::write(&file_path, "short").unwrap();

    // Calling replace_symbol_body must NOT panic! It must return a clean Err
    let res = replace_symbol_body(
        &ws,
        &db_path,
        &conn,
        "long_function_name",
        "src/calc.rs",
        "{\n    42\n}",
        None,
    );

    assert!(
        res.is_err(),
        "Replacing in shrunk file must return an Err, not panic"
    );
}

#[test]
fn test_replace_symbol_body_rejects_syntax_error() {
    let _extract_bin =
        find_julie_extract_binary().expect("julie-extract binary must be present for tests");

    let temp_dir = safe_tempdir();
    let root = temp_dir.path().to_path_buf();

    let src_dir = root.join("src");
    fs::create_dir_all(&src_dir).unwrap();
    let file_path = src_dir.join("calc.rs");

    let initial_code = r#"pub fn my_calc(a: i32) -> i32 {
    a * 2
}
"#;
    fs::write(&file_path, initial_code).unwrap();

    let ws = Workspace::new(root.clone());
    let db_path = root.join("test.db");

    scan_workspace(&ws, &db_path, true).expect("Scan failed");
    let conn = open_read_only(&db_path).unwrap();

    // Attempt replacing body with invalid Rust syntax (e.g. missing operand, syntax error)
    let invalid_body = "{\n    let x = ;\n}";
    let res = replace_symbol_body(
        &ws,
        &db_path,
        &conn,
        "my_calc",
        "src/calc.rs",
        invalid_body,
        None,
    );

    assert!(
        res.is_err(),
        "Replacement with invalid syntax must be rejected"
    );

    // File on disk must remain uncorrupted and unchanged!
    let disk_content = fs::read_to_string(&file_path).unwrap();
    assert_eq!(
        disk_content, initial_code,
        "Disk content must not be modified when syntax error occurs"
    );
}

#[test]
fn test_replace_symbol_body_rejects_stale_indexed_hash_when_disk_differs() {
    let _extract_bin =
        find_julie_extract_binary().expect("julie-extract binary must be present for tests");

    let temp_dir = safe_tempdir();
    let root = temp_dir.path().to_path_buf();

    let src_dir = root.join("src");
    fs::create_dir_all(&src_dir).unwrap();
    let file_path = src_dir.join("calc.rs");

    let initial_code = "pub fn my_calc(a: i32) -> i32 {\n    a * 2\n}\n";
    fs::write(&file_path, initial_code).unwrap();

    let ws = Workspace::new(root.clone());
    let db_path = root.join("test.db");

    scan_workspace(&ws, &db_path, true).expect("Scan failed");
    let conn = open_read_only(&db_path).unwrap();

    let sym = code_kb_core::get_symbol_by_name_exact(&conn, "my_calc", "src/calc.rs")
        .unwrap()
        .unwrap();
    let indexed_hash = sym
        .body_hash
        .clone()
        .expect("Indexed symbol should have body_hash");

    // Manually edit the file on disk so the body is different from indexed state,
    // and keep file length same or different
    let offline_code = "pub fn my_calc(a: i32) -> i32 {\n    a * 9\n}\n";
    fs::write(&file_path, offline_code).unwrap();

    // Calling replace_symbol_body with expected_hash matching the OLD indexed hash
    // MUST fail with HashMismatch because the disk content is no longer that hash!
    let res = replace_symbol_body(
        &ws,
        &db_path,
        &conn,
        "my_calc",
        "src/calc.rs",
        "{\n    a * 10\n}",
        Some(&indexed_hash),
    );

    assert!(
        matches!(res, Err(code_kb_core::EditError::HashMismatch(..))),
        "Must reject edit when expected hash does not match current disk body, got: {:?}",
        res
    );
}

#[cfg(unix)]
#[test]
fn test_replace_symbol_body_preserves_file_permissions() {
    let _extract_bin =
        find_julie_extract_binary().expect("julie-extract binary must be present for tests");

    let temp_dir = safe_tempdir();
    let root = temp_dir.path().to_path_buf();

    let src_dir = root.join("src");
    fs::create_dir_all(&src_dir).unwrap();
    let file_path = src_dir.join("script.rs");

    let initial_code = "pub fn run_script() -> i32 {\n    1\n}\n";
    fs::write(&file_path, initial_code).unwrap();

    // Set 0755 executable permissions
    fs::set_permissions(&file_path, fs::Permissions::from_mode(0o755)).unwrap();

    let ws = Workspace::new(root.clone());
    let db_path = root.join("test.db");

    scan_workspace(&ws, &db_path, true).expect("Scan failed");
    let conn = open_read_only(&db_path).unwrap();

    let res = replace_symbol_body(
        &ws,
        &db_path,
        &conn,
        "run_script",
        "src/script.rs",
        "{\n    42\n}",
        None,
    );
    assert!(res.is_ok(), "replace_symbol_body should succeed: {:?}", res);

    // Verify permissions were preserved (not clobbered to 0600 by tempfile)
    let perms = fs::metadata(&file_path).unwrap().permissions();
    assert_eq!(
        perms.mode() & 0o777,
        0o755,
        "Permissions must remain 0755 after edit, got: {:o}",
        perms.mode() & 0o777
    );
}

#[cfg(unix)]
#[test]
fn test_replace_symbol_body_preserves_symlinks() {
    let _extract_bin =
        find_julie_extract_binary().expect("julie-extract binary must be present for tests");

    let temp_dir = safe_tempdir();
    let root = temp_dir.path().to_path_buf();

    let src_dir = root.join("src");
    fs::create_dir_all(&src_dir).unwrap();
    let real_file = src_dir.join("real.rs");
    let link_file = src_dir.join("link.rs");

    let initial_code = "pub fn linked_fn() -> i32 {\n    10\n}\n";
    fs::write(&real_file, initial_code).unwrap();
    std::os::unix::fs::symlink(&real_file, &link_file).unwrap();

    let ws = Workspace::new(root.clone());
    let db_path = root.join("test.db");

    scan_workspace(&ws, &db_path, true).expect("Scan failed");
    let conn = open_read_only(&db_path).unwrap();

    // Edit through the symlink path
    let res = replace_symbol_body(
        &ws,
        &db_path,
        &conn,
        "linked_fn",
        "src/link.rs",
        "{\n    99\n}",
        None,
    );
    assert!(res.is_ok(), "replace_symbol_body should succeed: {:?}", res);

    // Verify link.rs is STILL a symlink
    let sym_meta = fs::symlink_metadata(&link_file).unwrap();
    assert!(
        sym_meta.is_symlink(),
        "Symlink must NOT be replaced by a regular file"
    );

    // Verify real file received the edit
    let real_content = fs::read_to_string(&real_file).unwrap();
    assert!(
        real_content.contains("99"),
        "Real file must contain edited content"
    );
}

#[test]
fn test_replace_symbol_body_normalizes_crlf() {
    let _extract_bin =
        find_julie_extract_binary().expect("julie-extract binary must be present for tests");

    let temp_dir = safe_tempdir();
    let root = temp_dir.path().to_path_buf();

    let src_dir = root.join("src");
    fs::create_dir_all(&src_dir).unwrap();
    let file_path = src_dir.join("crlf.rs");

    // File with CRLF line endings
    let initial_code = "pub fn crlf_fn() -> i32 {\r\n    1\r\n}\r\n";
    fs::write(&file_path, initial_code).unwrap();

    let ws = Workspace::new(root.clone());
    let db_path = root.join("test.db");

    scan_workspace(&ws, &db_path, true).expect("Scan failed");
    let conn = open_read_only(&db_path).unwrap();

    // Pass replacement body with LF only
    let new_body = "{\n    let a = 10;\n    a * 2\n}";
    let res = replace_symbol_body(
        &ws,
        &db_path,
        &conn,
        "crlf_fn",
        "src/crlf.rs",
        new_body,
        None,
    );
    assert!(res.is_ok(), "replace_symbol_body should succeed: {:?}", res);

    // Verify disk content preserves CRLF consistently throughout
    let disk_bytes = fs::read(&file_path).unwrap();
    let disk_str = String::from_utf8(disk_bytes.clone()).unwrap();
    assert!(
        disk_str.contains("\r\n"),
        "File must maintain CRLF line endings"
    );

    // Check there are no bare LF (\n without \r before it)
    let mut prev_char = ' ';
    for ch in disk_str.chars() {
        if ch == '\n' {
            assert_eq!(
                prev_char, '\r',
                "Every newline in CRLF file must be preceded by carriage return (CRLF)"
            );
        }
        prev_char = ch;
    }
}

#[test]
fn test_replace_symbol_body_chained_edits_with_expected_hash() {
    let _extract_bin =
        find_julie_extract_binary().expect("julie-extract binary must be present for tests");

    let temp_dir = safe_tempdir();
    let root = temp_dir.path().to_path_buf();

    let src_dir = root.join("src");
    fs::create_dir_all(&src_dir).unwrap();
    let file_path = src_dir.join("calc.rs");

    let initial_code = "pub fn add_numbers(a: i32, b: i32) -> i32 {\n    a + b\n}\n";
    fs::write(&file_path, initial_code).unwrap();

    let ws = Workspace::new(root.clone());
    let db_path = root.join("test.db");

    scan_workspace(&ws, &db_path, true).expect("Scan failed");
    let conn = open_read_only(&db_path).unwrap();

    // 1. First edit: no expected hash passed
    let res1 = replace_symbol_body(
        &ws,
        &db_path,
        &conn,
        "add_numbers",
        "src/calc.rs",
        "{\n    let sum = a + b;\n    sum * 2\n}",
        None,
    )
    .expect("first edit should succeed");

    assert!(!res1.new_body_hash.is_empty());

    // 2. Second edit: passing valid expected_hash matching res1.new_body_hash
    let res2 = replace_symbol_body(
        &ws,
        &db_path,
        &conn,
        "add_numbers",
        "src/calc.rs",
        "{\n    let sum = a + b;\n    sum * 3\n}",
        Some(&res1.new_body_hash),
    )
    .expect("second edit with correct expected_hash should succeed");

    assert_ne!(res1.new_body_hash, res2.new_body_hash);

    // 3. Third edit: passing stale expected_hash (res1.new_body_hash) must fail
    let res3 = replace_symbol_body(
        &ws,
        &db_path,
        &conn,
        "add_numbers",
        "src/calc.rs",
        "{\n    let sum = a + b;\n    sum * 4\n}",
        Some(&res1.new_body_hash),
    );

    assert!(res3.is_err(), "Edit with stale expected_hash must fail");
    let err_msg = res3.unwrap_err().to_string();
    assert!(err_msg.contains("Optimistic lock failed") || err_msg.contains("expected body hash"));
}

#[test]
fn test_replace_symbol_body_rejects_syntax_error_in_a_language_without_a_bundled_grammar() {
    let _extract_bin =
        find_julie_extract_binary().expect("julie-extract binary must be present for tests");

    let temp_dir = safe_tempdir();
    let root = temp_dir.path().to_path_buf();
    fs::create_dir_all(root.join("lib")).unwrap();
    let file_path = root.join("lib/calc.rb");
    let initial_code = "def my_calc(a)\n  a * 2\nend\n";
    fs::write(&file_path, initial_code).unwrap();

    let ws = Workspace::new(root.clone());
    let db_path = root.join("test.db");
    scan_workspace(&ws, &db_path, true).expect("Scan failed");
    let conn = open_read_only(&db_path).unwrap();

    let res = replace_symbol_body(
        &ws,
        &db_path,
        &conn,
        "my_calc",
        "lib/calc.rb",
        "def my_calc(a)\n  a * (2\nend\n",
        None,
    );

    let message = res.expect_err("broken Ruby must be rejected").to_string();
    assert!(message.contains("syntax"), "{message}");
    assert!(message.contains("at line "), "{message}");
    assert_eq!(fs::read_to_string(&file_path).unwrap(), initial_code);
}

#[test]
fn validate_syntax_skips_paths_the_extractor_has_no_grammar_for() {
    assert_eq!(
        code_kb_core::validate_syntax("notes.unknown", "anything (\n"),
        Ok(false)
    );
    assert_eq!(
        code_kb_core::validate_syntax("src/lib.rs", "pub fn f() {}\n"),
        Ok(true)
    );
}

fn scan_into(root: &std::path::Path, files: &[(&str, &str)]) -> (Workspace, std::path::PathBuf) {
    find_julie_extract_binary().expect("julie-extract binary must be present for tests");
    for (rel, content) in files {
        let path = root.join(rel);
        fs::create_dir_all(path.parent().unwrap()).unwrap();
        fs::write(&path, content).unwrap();
    }
    let ws = Workspace::new(root.to_path_buf());
    let db_path = root.join("test.db");
    scan_workspace(&ws, &db_path, true).expect("Scan failed");
    (ws, db_path)
}

#[test]
fn test_edit_file_replaces_one_exact_match() {
    let dir = safe_tempdir();
    let (ws, db_path) = scan_into(
        dir.path(),
        &[(
            "src/calc.rs",
            "pub fn add(a: i32, b: i32) -> i32 {\n    a + b\n}\n",
        )],
    );
    let conn = open_read_only(&db_path).unwrap();

    let res = edit_file(
        &ws,
        &db_path,
        &conn,
        "src/calc.rs",
        "a + b",
        "a - b",
        Occurrence::Only,
    )
    .expect("edit_file must succeed");

    assert_eq!(res.file_path, "src/calc.rs");
    assert_eq!(res.replacements, 1);
    assert_eq!(res.first_line, 2);
    assert_eq!(res.match_tier, MatchTier::Exact);
    assert!(res.syntax_checked);
    assert_eq!(
        fs::read_to_string(dir.path().join("src/calc.rs")).unwrap(),
        "pub fn add(a: i32, b: i32) -> i32 {\n    a - b\n}\n"
    );
}

#[test]
fn test_edit_file_matches_ignoring_indentation() {
    let dir = safe_tempdir();
    let (ws, db_path) = scan_into(
        dir.path(),
        &[(
            "src/calc.rs",
            "pub fn add(a: i32, b: i32) -> i32 {\n    let sum = a + b;\n    sum\n}\n",
        )],
    );
    let conn = open_read_only(&db_path).unwrap();

    let res = edit_file(
        &ws,
        &db_path,
        &conn,
        "src/calc.rs",
        "let sum = a + b;\nsum",
        "let sum = a - b;\nsum",
        Occurrence::Only,
    )
    .expect("edit_file must succeed");

    assert_eq!(res.match_tier, MatchTier::Whitespace);
    assert_eq!(res.replacements, 1);
    assert_eq!(res.first_line, 2);
    assert_eq!(
        fs::read_to_string(dir.path().join("src/calc.rs")).unwrap(),
        "pub fn add(a: i32, b: i32) -> i32 {\n    let sum = a - b;\n    sum\n}\n"
    );
}

#[test]
fn test_edit_file_refuses_ambiguous_match_and_lists_lines() {
    let dir = safe_tempdir();
    let (ws, db_path) = scan_into(
        dir.path(),
        &[(
            "src/calc.rs",
            "pub fn a() -> i32 {\n    1 + 1\n}\n\npub fn b() -> i32 {\n    1 + 1\n}\n",
        )],
    );
    let conn = open_read_only(&db_path).unwrap();

    let err = edit_file(
        &ws,
        &db_path,
        &conn,
        "src/calc.rs",
        "1 + 1",
        "2 + 2",
        Occurrence::Only,
    )
    .expect_err("two matches must be refused");

    match &err {
        code_kb_core::EditError::AmbiguousMatch(path, lines) => {
            assert_eq!(path, "src/calc.rs");
            assert_eq!(lines, &vec![2, 6]);
        }
        other => panic!("expected AmbiguousMatch, got {other:?}"),
    }
    let message = err.to_string();
    assert!(message.contains("2"), "{message}");
    assert!(message.contains("6"), "{message}");
    assert!(message.contains("occurrence"), "{message}");
    assert!(
        fs::read_to_string(dir.path().join("src/calc.rs"))
            .unwrap()
            .contains("1 + 1")
    );
}

#[test]
fn test_edit_file_occurrence_all_replaces_every_match() {
    let dir = safe_tempdir();
    let (ws, db_path) = scan_into(
        dir.path(),
        &[(
            "src/calc.rs",
            "pub fn a() -> i32 {\n    1 + 1\n}\n\npub fn b() -> i32 {\n    1 + 1\n}\n",
        )],
    );
    let conn = open_read_only(&db_path).unwrap();

    let res = edit_file(
        &ws,
        &db_path,
        &conn,
        "src/calc.rs",
        "1 + 1",
        "2 + 2",
        Occurrence::All,
    )
    .expect("edit_file must succeed");

    assert_eq!(res.replacements, 2);
    assert_eq!(res.first_line, 2);
    assert_eq!(
        fs::read_to_string(dir.path().join("src/calc.rs")).unwrap(),
        "pub fn a() -> i32 {\n    2 + 2\n}\n\npub fn b() -> i32 {\n    2 + 2\n}\n"
    );
    assert_eq!(res.touched_symbols, vec!["a".to_string(), "b".to_string()]);
}

#[test]
fn test_edit_file_rejects_syntax_error_and_leaves_file_unchanged() {
    let dir = safe_tempdir();
    let initial = "pub fn add(a: i32, b: i32) -> i32 {\n    a + b\n}\n";
    let (ws, db_path) = scan_into(dir.path(), &[("src/calc.rs", initial)]);
    let conn = open_read_only(&db_path).unwrap();

    let err = edit_file(
        &ws,
        &db_path,
        &conn,
        "src/calc.rs",
        "a + b",
        "a + ",
        Occurrence::Only,
    )
    .expect_err("broken Rust must be rejected");

    assert!(err.to_string().contains("syntax"), "{err}");
    assert_eq!(
        fs::read_to_string(dir.path().join("src/calc.rs")).unwrap(),
        initial
    );
}

#[test]
fn test_edit_file_preserves_crlf() {
    let dir = safe_tempdir();
    let (ws, db_path) = scan_into(
        dir.path(),
        &[("src/crlf.rs", "pub fn crlf_fn() -> i32 {\r\n    1\r\n}\r\n")],
    );
    let conn = open_read_only(&db_path).unwrap();

    edit_file(
        &ws,
        &db_path,
        &conn,
        "src/crlf.rs",
        "    1\n",
        "    let a = 2;\n    a\n",
        Occurrence::Only,
    )
    .expect("edit_file must succeed");

    let disk = fs::read_to_string(dir.path().join("src/crlf.rs")).unwrap();
    assert_eq!(
        disk, "pub fn crlf_fn() -> i32 {\r\n    let a = 2;\r\n    a\r\n}\r\n",
        "CRLF endings must survive the edit"
    );
}

#[test]
fn test_edit_file_reindexes_shifted_symbol_offsets() {
    let dir = safe_tempdir();
    let (ws, db_path) = scan_into(
        dir.path(),
        &[(
            "src/calc.rs",
            "pub fn head() -> i32 {\n    1\n}\n\npub fn tail() -> i32 {\n    2\n}\n",
        )],
    );
    let conn = open_read_only(&db_path).unwrap();

    edit_file(
        &ws,
        &db_path,
        &conn,
        "src/calc.rs",
        "pub fn head() -> i32 {\n    1\n}",
        "pub fn head() -> i32 {\n    let x = 1;\n    x\n}",
        Occurrence::Only,
    )
    .expect("edit_file must succeed");

    let tail = code_kb_core::get_symbol_by_name(&conn, "tail", Some("src/calc.rs"))
        .unwrap()
        .unwrap();
    assert_eq!(tail.start_line, 6);
}

#[test]
fn test_edit_file_edits_an_unsupported_text_file() {
    let readme = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
        .join("..")
        .join("..")
        .join("README.md");
    let readme_text = fs::read_to_string(&readme).expect("repository README.md must be readable");
    let heading = "## Why code-kb?";
    assert_eq!(readme_text.matches(heading).count(), 1);

    let dir = safe_tempdir();
    let (ws, db_path) = scan_into(
        dir.path(),
        &[
            ("notes.txt", "hello\nworld\n"),
            ("README.md", readme_text.as_str()),
        ],
    );
    let conn = open_read_only(&db_path).unwrap();

    let text = edit_file(
        &ws,
        &db_path,
        &conn,
        "notes.txt",
        "world",
        "there",
        Occurrence::Only,
    )
    .expect("editing a text file must succeed");
    assert!(!text.syntax_checked);
    assert!(text.touched_symbols.is_empty());
    assert_eq!(
        fs::read_to_string(dir.path().join("notes.txt")).unwrap(),
        "hello\nthere\n"
    );

    edit_file(
        &ws,
        &db_path,
        &conn,
        "README.md",
        heading,
        "## Why use code-kb?",
        Occurrence::Only,
    )
    .expect("editing a Markdown file must succeed");
    assert!(
        fs::read_to_string(dir.path().join("README.md"))
            .unwrap()
            .contains("## Why use code-kb?")
    );
}

#[cfg(unix)]
#[test]
fn test_edit_file_preserves_file_permissions() {
    let dir = safe_tempdir();
    let (ws, db_path) = scan_into(
        dir.path(),
        &[("src/script.rs", "pub fn run_script() -> i32 {\n    1\n}\n")],
    );
    let conn = open_read_only(&db_path).unwrap();
    let target = dir.path().join("src/script.rs");
    fs::set_permissions(&target, fs::Permissions::from_mode(0o755)).unwrap();

    edit_file(
        &ws,
        &db_path,
        &conn,
        "src/script.rs",
        "    1\n",
        "    42\n",
        Occurrence::Only,
    )
    .expect("edit_file must succeed");

    let mode = fs::metadata(&target).unwrap().permissions().mode() & 0o777;
    assert_eq!(mode, 0o755, "permissions must stay 0755, got {mode:o}");
}

#[test]
fn test_edit_file_reports_touched_symbols() {
    let dir = safe_tempdir();
    let (ws, db_path) = scan_into(
        dir.path(),
        &[(
            "src/item.rs",
            "pub struct Item;\n\nimpl Item {\n    pub fn get(&self) -> i32 {\n        1\n    }\n}\n",
        )],
    );
    let conn = open_read_only(&db_path).unwrap();

    let res = edit_file(
        &ws,
        &db_path,
        &conn,
        "src/item.rs",
        "        1\n",
        "        2\n",
        Occurrence::Only,
    )
    .expect("edit_file must succeed");

    assert_eq!(res.touched_symbols, vec!["get".to_string()]);
}

#[test]
fn test_edit_file_rejects_empty_old_text_and_a_no_op_edit() {
    let dir = safe_tempdir();
    let (ws, db_path) = scan_into(
        dir.path(),
        &[("src/calc.rs", "pub fn add() -> i32 {\n    1\n}\n")],
    );
    let conn = open_read_only(&db_path).unwrap();

    assert!(matches!(
        edit_file(
            &ws,
            &db_path,
            &conn,
            "src/calc.rs",
            "",
            "x",
            Occurrence::Only
        ),
        Err(code_kb_core::EditError::EmptyOldText)
    ));
    assert!(matches!(
        edit_file(
            &ws,
            &db_path,
            &conn,
            "src/calc.rs",
            "    1\n",
            "    1\n",
            Occurrence::Only
        ),
        Err(code_kb_core::EditError::NoChange)
    ));
    assert!(matches!(
        edit_file(&ws, &db_path, &conn, "src", "a", "b", Occurrence::Only),
        Err(code_kb_core::EditError::NotAFile(_))
    ));
}

#[test]
fn test_edit_file_reports_the_nearest_lines_when_nothing_matches() {
    let dir = safe_tempdir();
    let (ws, db_path) = scan_into(
        dir.path(),
        &[(
            "src/calc.rs",
            "pub fn add(a: i32, b: i32) -> i32 {\n    let sum = a + b;\n    sum\n}\n",
        )],
    );
    let conn = open_read_only(&db_path).unwrap();

    let err = edit_file(
        &ws,
        &db_path,
        &conn,
        "src/calc.rs",
        "let sum = a * b;",
        "let sum = a / b;",
        Occurrence::Only,
    )
    .expect_err("a missing text must be refused");

    let message = err.to_string();
    assert!(message.contains("src/calc.rs"), "{message}");
    assert!(message.contains("let sum = a + b;"), "{message}");
    assert!(message.contains("2"), "{message}");
}

#[test]
fn test_edit_file_touched_symbols_prefer_the_enclosing_definition_over_a_local() {
    let dir = safe_tempdir();
    let (ws, db_path) = scan_into(
        dir.path(),
        &[(
            "src/run.rs",
            "pub fn run() -> i32 {\n    let total = 1 + 1;\n    total\n}\n",
        )],
    );
    let conn = open_read_only(&db_path).unwrap();

    let res = edit_file(
        &ws,
        &db_path,
        &conn,
        "src/run.rs",
        "1 + 1",
        "2 + 2",
        Occurrence::Only,
    )
    .expect("edit_file must succeed");

    assert_eq!(res.touched_symbols, vec!["run".to_string()]);
}

#[test]
fn test_edit_file_overlapping_matches_are_ambiguous_and_last_picks_the_last_start() {
    let dir = safe_tempdir();
    let (ws, db_path) = scan_into(dir.path(), &[("notes/word.txt", "banana\n")]);
    let conn = open_read_only(&db_path).unwrap();

    let err = edit_file(
        &ws,
        &db_path,
        &conn,
        "notes/word.txt",
        "ana",
        "X",
        Occurrence::Only,
    )
    .expect_err("overlapping matches must be refused");
    match &err {
        code_kb_core::EditError::AmbiguousMatch(_, lines) => assert_eq!(lines, &vec![1, 1]),
        other => panic!("expected AmbiguousMatch, got {other:?}"),
    }

    let res = edit_file(
        &ws,
        &db_path,
        &conn,
        "notes/word.txt",
        "ana",
        "X",
        Occurrence::Last,
    )
    .expect("last must pick the last start");
    assert_eq!(res.replacements, 1);
    assert_eq!(
        fs::read_to_string(dir.path().join("notes/word.txt")).unwrap(),
        "banX\n"
    );
}

#[test]
fn test_edit_file_occurrence_all_replaces_non_overlapping_matches_only() {
    let dir = safe_tempdir();
    let (ws, db_path) = scan_into(dir.path(), &[("notes/word.txt", "aaaa\n")]);
    let conn = open_read_only(&db_path).unwrap();

    let res = edit_file(
        &ws,
        &db_path,
        &conn,
        "notes/word.txt",
        "aa",
        "b",
        Occurrence::All,
    )
    .expect("all must replace the non-overlapping matches");
    assert_eq!(res.replacements, 2);
    assert_eq!(
        fs::read_to_string(dir.path().join("notes/word.txt")).unwrap(),
        "bb\n"
    );
}

#[test]
fn test_edit_file_overlapping_line_windows_are_ambiguous() {
    let dir = safe_tempdir();
    let (ws, db_path) = scan_into(dir.path(), &[("notes/list.txt", "  x\n  x\n  x\n")]);
    let conn = open_read_only(&db_path).unwrap();

    let err = edit_file(
        &ws,
        &db_path,
        &conn,
        "notes/list.txt",
        "x\nx",
        "y",
        Occurrence::Only,
    )
    .expect_err("two line windows must be refused");
    match &err {
        code_kb_core::EditError::AmbiguousMatch(_, lines) => assert_eq!(lines, &vec![1, 2]),
        other => panic!("expected AmbiguousMatch, got {other:?}"),
    }
}

#[test]
fn test_edit_file_many_matches_stay_cheap_and_a_too_large_result_is_refused() {
    let dir = safe_tempdir();
    let big = "a".repeat(1024 * 1024);
    let (ws, db_path) = scan_into(dir.path(), &[("notes/big.txt", &big)]);
    let conn = open_read_only(&db_path).unwrap();
    let replacement = "b".repeat(4096);
    let started = std::time::Instant::now();

    let err = edit_file(
        &ws,
        &db_path,
        &conn,
        "notes/big.txt",
        "a",
        &replacement,
        Occurrence::Only,
    )
    .expect_err("a million matches must be refused as ambiguous");
    let message = err.to_string();
    assert!(message.contains("and 1048566 more"), "{message}");
    assert!(message.len() < 400, "{message}");

    let err = edit_file(
        &ws,
        &db_path,
        &conn,
        "notes/big.txt",
        "a",
        &replacement,
        Occurrence::All,
    )
    .expect_err("a result above the size cap must be refused");
    assert!(
        matches!(err, code_kb_core::EditError::EditTooLarge),
        "{err:?}"
    );
    assert_eq!(
        fs::read_to_string(dir.path().join("notes/big.txt")).unwrap(),
        big
    );

    let res = edit_file(
        &ws,
        &db_path,
        &conn,
        "notes/big.txt",
        "a",
        &replacement,
        Occurrence::First,
    )
    .expect("first must succeed");
    assert_eq!(res.bytes_written, big.len() - 1 + replacement.len());
    assert!(started.elapsed() < std::time::Duration::from_secs(10));
}