agcodex-core 0.1.0

Core business logic with AST-RAG engine and tree-sitter integration
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
//! Comprehensive tests for the AGCodex Edit Tool.
//!
//! Tests cover line-based editing, text replacement, error handling,
//! performance requirements (<1ms for simple edits), and edge cases.
//!
//! The edit tool provides basic, reliable text editing with LLM-friendly
//! output including context, before/after state tracking, and backup creation.

use std::fs;
use std::time::Duration;
use std::time::Instant;
use tempfile::NamedTempFile;
use tempfile::TempDir;

// Import edit tool from the same crate
use agcodex_core::tools::edit::EditError;
use agcodex_core::tools::edit::EditTool;

/// Sample code files for realistic editing scenarios
const SIMPLE_RUST_CODE: &str = r#"fn main() {
    println!("Hello, world!");
    let x = 42;
    println!("The answer is {}", x);
}"#;

const RUST_STRUCT_CODE: &str = r#"use std::collections::HashMap;

#[derive(Debug, Clone)]
pub struct User {
    pub id: u64,
    pub name: String,
    pub email: String,
    pub settings: HashMap<String, String>,
}

impl User {
    pub fn new(id: u64, name: String, email: String) -> Self {
        Self {
            id,
            name,
            email,
            settings: HashMap::new(),
        }
    }
    
    pub fn update_setting(&mut self, key: String, value: String) {
        self.settings.insert(key, value);
    }
    
    pub fn get_display_name(&self) -> &str {
        &self.name
    }
}"#;

const PYTHON_CLASS_CODE: &str = r#"class Calculator:
    def __init__(self):
        self.history = []
        self.current_value = 0
    
    def add(self, value):
        self.current_value += value
        self.history.append(f"Added {value}")
        return self.current_value
    
    def subtract(self, value):
        self.current_value -= value
        self.history.append(f"Subtracted {value}")
        return self.current_value
    
    def clear(self):
        self.current_value = 0
        self.history = []
    
    def get_history(self):
        return self.history.copy()
"#;

const TYPESCRIPT_INTERFACE_CODE: &str = r#"interface ApiResponse<T> {
    success: boolean;
    data: T;
    error?: string;
    timestamp: number;
}

class ApiClient {
    private baseUrl: string;
    private timeout: number;
    
    constructor(baseUrl: string, timeout: number = 5000) {
        this.baseUrl = baseUrl;
        this.timeout = timeout;
    }
    
    async get<T>(endpoint: string): Promise<ApiResponse<T>> {
        const url = `${this.baseUrl}${endpoint}`;
        const response = await fetch(url);
        
        return {
            success: response.ok,
            data: await response.json(),
            timestamp: Date.now()
        };
    }
}"#;

/// Performance assertion helper (simplified version for this test)
struct PerformanceAssertions;
impl PerformanceAssertions {
    fn assert_duration_under(duration: Duration, target_ms: u64, operation: &str) {
        let actual_ms = duration.as_millis();
        assert!(
            actual_ms < target_ms as u128,
            "{} took {}ms, target was <{}ms",
            operation,
            actual_ms,
            target_ms
        );
    }
}

/// Test timing utility (simplified version)
struct TestTiming;
impl TestTiming {
    fn time_operation<F, R>(operation: F) -> (R, Duration)
    where
        F: FnOnce() -> R,
    {
        let start = Instant::now();
        let result = operation();
        let duration = start.elapsed();
        (result, duration)
    }
}

#[cfg(test)]
mod basic_functionality_tests {
    use super::*;

    #[test]
    fn test_edit_tool_creation() {
        let edit_tool = EditTool::new();
        // Should create successfully (tool is stateless)
        assert_eq!(std::mem::size_of_val(&edit_tool), 0);
    }

    #[test]
    fn test_simple_line_edit() {
        let temp_file = NamedTempFile::new().unwrap();
        fs::write(temp_file.path(), SIMPLE_RUST_CODE).unwrap();

        let result =
            EditTool::edit_line(temp_file.path().to_str().unwrap(), 3, "let x = 100;").unwrap();

        assert!(result.success);
        assert_eq!(result.line_changed, Some(3));
        assert_eq!(result.old_content, "    let x = 42;");
        assert_eq!(result.new_content, "    let x = 100;");
        assert!(result.message.contains("Successfully changed line 3"));

        // Verify file was actually changed
        let new_content = fs::read_to_string(temp_file.path()).unwrap();
        assert!(new_content.contains("let x = 100;"));
        assert!(!new_content.contains("let x = 42;"));

        // Verify backup was created
        let backup_path = format!("{}.backup", temp_file.path().to_str().unwrap());
        assert!(std::path::Path::new(&backup_path).exists());
    }

    #[test]
    fn test_simple_text_replacement() {
        let temp_file = NamedTempFile::new().unwrap();
        fs::write(temp_file.path(), SIMPLE_RUST_CODE).unwrap();

        let result = EditTool::edit_text(
            temp_file.path().to_str().unwrap(),
            "Hello, world!",
            "Hello, AGCodex!",
        )
        .unwrap();

        assert!(result.success);
        assert_eq!(result.line_changed, Some(2));
        assert_eq!(result.old_content, "Hello, world!");
        assert_eq!(result.new_content, "Hello, AGCodex!");
        assert!(result.message.contains("Successfully replaced"));

        // Verify file was changed
        let new_content = fs::read_to_string(temp_file.path()).unwrap();
        assert!(new_content.contains("Hello, AGCodex!"));
        assert!(!new_content.contains("Hello, world!"));
    }

    #[test]
    fn test_indentation_preservation() {
        let temp_file = NamedTempFile::new().unwrap();
        let indented_code =
            "fn test() {\n    let x = 1;\n        let y = 2;\n            let z = 3;\n}";
        fs::write(temp_file.path(), indented_code).unwrap();

        // Edit line with 4-space indentation
        let result =
            EditTool::edit_line(temp_file.path().to_str().unwrap(), 2, "let x = 42;").unwrap();

        assert_eq!(result.new_content, "    let x = 42;");

        // Edit line with 8-space indentation
        let result =
            EditTool::edit_line(temp_file.path().to_str().unwrap(), 3, "let y = 84;").unwrap();

        assert_eq!(result.new_content, "        let y = 84;");

        // Edit line with 12-space indentation
        let result =
            EditTool::edit_line(temp_file.path().to_str().unwrap(), 4, "let z = 126;").unwrap();

        assert_eq!(result.new_content, "            let z = 126;");
    }

    #[test]
    fn test_tab_indentation_preservation() {
        let temp_file = NamedTempFile::new().unwrap();
        let tab_indented = "fn test() {\n\tlet x = 1;\n\t\tlet y = 2;\n}";
        fs::write(temp_file.path(), tab_indented).unwrap();

        let result =
            EditTool::edit_line(temp_file.path().to_str().unwrap(), 2, "let x = 42;").unwrap();

        assert_eq!(result.new_content, "\tlet x = 42;");

        let result =
            EditTool::edit_line(temp_file.path().to_str().unwrap(), 3, "let y = 84;").unwrap();

        assert_eq!(result.new_content, "\t\tlet y = 84;");
    }

    #[test]
    fn test_context_extraction() {
        let temp_file = NamedTempFile::new().unwrap();
        fs::write(temp_file.path(), RUST_STRUCT_CODE).unwrap();

        // Edit line in the middle (should have 3 lines before and after)
        let result = EditTool::edit_line(
            temp_file.path().to_str().unwrap(),
            10, // empty line between struct and impl
            "// Added comment",
        )
        .unwrap();

        // Should have context before
        assert_eq!(result.context_before.len(), 3);
        assert!(result.context_before[0].contains("pub email: String,"));

        // Should have context after
        assert_eq!(result.context_after.len(), 3);
        assert!(result.context_after[0].contains("impl User {"));
    }
}

#[cfg(test)]
mod error_handling_tests {
    use super::*;

    #[test]
    fn test_file_not_found() {
        let result = EditTool::edit_line("/nonexistent/file.txt", 1, "test");

        assert!(matches!(result, Err(EditError::FileNotFound(_))));

        let result = EditTool::edit_text("/nonexistent/file.txt", "old", "new");

        assert!(matches!(result, Err(EditError::FileNotFound(_))));
    }

    #[test]
    fn test_invalid_line_numbers() {
        let temp_file = NamedTempFile::new().unwrap();
        fs::write(temp_file.path(), "line1\nline2\nline3").unwrap();

        // Line 0 (invalid - 1-based indexing)
        let result = EditTool::edit_line(temp_file.path().to_str().unwrap(), 0, "new line");
        assert!(matches!(
            result,
            Err(EditError::InvalidLine {
                line: 0,
                total_lines: 3
            })
        ));

        // Line beyond end of file
        let result = EditTool::edit_line(temp_file.path().to_str().unwrap(), 5, "new line");
        assert!(matches!(
            result,
            Err(EditError::InvalidLine {
                line: 5,
                total_lines: 3
            })
        ));
    }

    #[test]
    fn test_pattern_not_found() {
        let temp_file = NamedTempFile::new().unwrap();
        fs::write(temp_file.path(), SIMPLE_RUST_CODE).unwrap();

        let result = EditTool::edit_text(
            temp_file.path().to_str().unwrap(),
            "nonexistent pattern",
            "replacement",
        );

        assert!(matches!(result, Err(EditError::PatternNotFound(_))));
    }

    #[test]
    fn test_ambiguous_matches() {
        let temp_file = NamedTempFile::new().unwrap();
        let ambiguous_code = "let x = 1;\nlet x = 2;\nlet x = 3;\n";
        fs::write(temp_file.path(), ambiguous_code).unwrap();

        let result = EditTool::edit_text(temp_file.path().to_str().unwrap(), "let x", "let y");

        assert!(matches!(
            result,
            Err(EditError::AmbiguousMatch { count: 3, .. })
        ));
    }

    #[test]
    fn test_find_matches_for_ambiguity() {
        let temp_file = NamedTempFile::new().unwrap();
        let code_with_duplicates = "function test() {\n    console.log('test');\n}\nfunction test2() {\n    console.log('test2');\n}\nfunction test3() {\n    console.log('test3');\n}";
        fs::write(temp_file.path(), code_with_duplicates).unwrap();

        let matches =
            EditTool::find_matches(temp_file.path().to_str().unwrap(), "console.log").unwrap();

        assert_eq!(matches.matches.len(), 3);
        assert_eq!(matches.matches[0].line_number, 2);
        assert_eq!(matches.matches[1].line_number, 5);
        assert_eq!(matches.matches[2].line_number, 8);

        // Check context includes surrounding lines
        assert!(matches.matches[0].context.contains("function test()"));
        assert!(matches.matches[0].context.contains("→ 2:"));
    }

    #[test]
    fn test_invalid_utf8_handling() {
        let temp_dir = TempDir::new().unwrap();
        let file_path = temp_dir.path().join("invalid.txt");

        // Write invalid UTF-8 bytes
        let invalid_utf8 = vec![0xFF, 0xFE, 0xFD];
        fs::write(&file_path, &invalid_utf8).unwrap();

        let result = EditTool::edit_line(file_path.to_str().unwrap(), 1, "replacement");

        // Should handle gracefully (either as InvalidUtf8 or other IO error)
        assert!(result.is_err());
    }
}

#[cfg(test)]
mod edge_cases_tests {
    use super::*;

    #[test]
    fn test_empty_file() {
        let temp_file = NamedTempFile::new().unwrap();
        fs::write(temp_file.path(), "").unwrap();

        let result = EditTool::edit_line(temp_file.path().to_str().unwrap(), 1, "first line");

        assert!(matches!(
            result,
            Err(EditError::InvalidLine {
                line: 1,
                total_lines: 0
            })
        ));

        let result = EditTool::edit_text(
            temp_file.path().to_str().unwrap(),
            "anything",
            "replacement",
        );

        assert!(matches!(result, Err(EditError::PatternNotFound(_))));
    }

    #[test]
    fn test_single_line_file() {
        let temp_file = NamedTempFile::new().unwrap();
        fs::write(temp_file.path(), "single line").unwrap();

        let result =
            EditTool::edit_line(temp_file.path().to_str().unwrap(), 1, "modified line").unwrap();

        assert_eq!(result.line_changed, Some(1));
        assert_eq!(result.old_content, "single line");
        assert_eq!(result.new_content, "modified line");
        assert!(result.context_before.is_empty());
        assert!(result.context_after.is_empty());
    }

    #[test]
    fn test_edit_first_line() {
        let temp_file = NamedTempFile::new().unwrap();
        fs::write(temp_file.path(), "line1\nline2\nline3\nline4\nline5").unwrap();

        let result =
            EditTool::edit_line(temp_file.path().to_str().unwrap(), 1, "modified first line")
                .unwrap();

        assert_eq!(result.line_changed, Some(1));
        assert!(result.context_before.is_empty()); // No lines before first line
        assert_eq!(result.context_after.len(), 3); // Should have 3 lines after
    }

    #[test]
    fn test_edit_last_line() {
        let temp_file = NamedTempFile::new().unwrap();
        fs::write(temp_file.path(), "line1\nline2\nline3\nline4\nline5").unwrap();

        let result =
            EditTool::edit_line(temp_file.path().to_str().unwrap(), 5, "modified last line")
                .unwrap();

        assert_eq!(result.line_changed, Some(5));
        assert_eq!(result.context_before.len(), 3); // Should have 3 lines before
        assert!(result.context_after.is_empty()); // No lines after last line
    }

    #[test]
    fn test_empty_line_replacement() {
        let temp_file = NamedTempFile::new().unwrap();
        fs::write(temp_file.path(), "line1\n    \nline3").unwrap();

        let result = EditTool::edit_line(temp_file.path().to_str().unwrap(), 2, "").unwrap();

        assert_eq!(result.new_content, "");
    }

    #[test]
    fn test_whitespace_only_line() {
        let temp_file = NamedTempFile::new().unwrap();
        fs::write(temp_file.path(), "line1\n    \nline3").unwrap();

        let result = EditTool::edit_line(temp_file.path().to_str().unwrap(), 2, "content").unwrap();

        assert_eq!(result.new_content, "    content");
    }

    #[test]
    fn test_very_long_line() {
        let temp_file = NamedTempFile::new().unwrap();
        let long_line = "x".repeat(10000);
        let content = format!("short\n{}\nshort", long_line);
        fs::write(temp_file.path(), content).unwrap();

        let result =
            EditTool::edit_line(temp_file.path().to_str().unwrap(), 2, "replaced").unwrap();

        assert_eq!(result.new_content, "replaced");
        assert_eq!(result.old_content, long_line);
    }
}

#[cfg(test)]
mod multi_line_replacement_tests {
    use super::*;

    #[test]
    fn test_multi_line_pattern_replacement() {
        let temp_file = NamedTempFile::new().unwrap();
        let multi_line_code = "fn old_function() {\n    println!(\"old\");\n}";
        fs::write(temp_file.path(), multi_line_code).unwrap();

        let result = EditTool::edit_text(
            temp_file.path().to_str().unwrap(),
            "fn old_function() {\n    println!(\"old\");\n}",
            "fn new_function() {\n    println!(\"new\");\n}",
        )
        .unwrap();

        assert!(result.success);
        let new_content = fs::read_to_string(temp_file.path()).unwrap();
        assert!(new_content.contains("fn new_function()"));
        assert!(new_content.contains("println!(\"new\")"));
    }

    #[test]
    fn test_code_block_replacement() {
        let temp_file = NamedTempFile::new().unwrap();
        fs::write(temp_file.path(), RUST_STRUCT_CODE).unwrap();

        // Replace the new function
        let old_function = r#"pub fn new(id: u64, name: String, email: String) -> Self {
        Self {
            id,
            name,
            email,
            settings: HashMap::new(),
        }
    }"#;

        let new_function = r#"pub fn new(id: u64, name: String, email: String) -> Self {
        Self {
            id,
            name,
            email,
            settings: HashMap::new(),
            created_at: std::time::SystemTime::now(),
        }
    }"#;

        let result = EditTool::edit_text(
            temp_file.path().to_str().unwrap(),
            old_function,
            new_function,
        )
        .unwrap();

        assert!(result.success);
        let new_content = fs::read_to_string(temp_file.path()).unwrap();
        assert!(new_content.contains("created_at: std::time::SystemTime::now()"));
    }
}

#[cfg(test)]
mod realistic_code_editing_tests {
    use super::*;

    #[test]
    fn test_rust_function_parameter_change() {
        let temp_file = NamedTempFile::new().unwrap();
        fs::write(temp_file.path(), RUST_STRUCT_CODE).unwrap();

        let result = EditTool::edit_text(
            temp_file.path().to_str().unwrap(),
            "pub fn new(id: u64, name: String, email: String)",
            "pub fn new(id: u64, name: String, email: String, active: bool)",
        )
        .unwrap();

        assert!(result.success);
        let new_content = fs::read_to_string(temp_file.path()).unwrap();
        assert!(new_content.contains("active: bool"));
    }

    #[test]
    fn test_python_method_modification() {
        let temp_file = NamedTempFile::new().unwrap();
        fs::write(temp_file.path(), PYTHON_CLASS_CODE).unwrap();

        let result = EditTool::edit_text(
            temp_file.path().to_str().unwrap(),
            "def add(self, value):",
            "def add(self, value: float) -> float:",
        )
        .unwrap();

        assert!(result.success);
        let new_content = fs::read_to_string(temp_file.path()).unwrap();
        assert!(new_content.contains("def add(self, value: float) -> float:"));
    }

    #[test]
    fn test_typescript_type_annotation() {
        let temp_file = NamedTempFile::new().unwrap();
        fs::write(temp_file.path(), TYPESCRIPT_INTERFACE_CODE).unwrap();

        let result = EditTool::edit_text(
            temp_file.path().to_str().unwrap(),
            "timeout: number = 5000",
            "timeout: number = 10000",
        )
        .unwrap();

        assert!(result.success);
        let new_content = fs::read_to_string(temp_file.path()).unwrap();
        assert!(new_content.contains("timeout: number = 10000"));
    }

    #[test]
    fn test_variable_renaming() {
        let temp_file = NamedTempFile::new().unwrap();
        let code = "let oldVar = 42;\nconsole.log(oldVar);\nreturn oldVar * 2;";
        fs::write(temp_file.path(), code).unwrap();

        // This should fail due to ambiguity (multiple occurrences)
        let result = EditTool::edit_text(temp_file.path().to_str().unwrap(), "oldVar", "newVar");

        assert!(matches!(result, Err(EditError::AmbiguousMatch { .. })));

        // But we can edit each occurrence specifically
        let result = EditTool::edit_text(
            temp_file.path().to_str().unwrap(),
            "let oldVar = 42;",
            "let newVar = 42;",
        )
        .unwrap();

        assert!(result.success);
    }

    #[test]
    fn test_import_statement_modification() {
        let temp_file = NamedTempFile::new().unwrap();
        let import_code = "use std::collections::HashMap;\nuse std::fs;\n\nfn main() {}";
        fs::write(temp_file.path(), import_code).unwrap();

        let result = EditTool::edit_text(
            temp_file.path().to_str().unwrap(),
            "use std::collections::HashMap;",
            "use std::collections::{HashMap, HashSet};",
        )
        .unwrap();

        assert!(result.success);
        let new_content = fs::read_to_string(temp_file.path()).unwrap();
        assert!(new_content.contains("HashMap, HashSet"));
    }
}

#[cfg(test)]
mod performance_tests {
    use super::*;

    #[test]
    fn test_simple_edit_performance() {
        let temp_file = NamedTempFile::new().unwrap();
        fs::write(temp_file.path(), SIMPLE_RUST_CODE).unwrap();

        let (result, duration) = TestTiming::time_operation(|| {
            EditTool::edit_line(temp_file.path().to_str().unwrap(), 2, "modified line")
        });

        assert!(result.is_ok());
        // Should complete in under 10ms for simple edits (relaxed for system load variations)
        PerformanceAssertions::assert_duration_under(duration, 10, "simple line edit");
    }

    #[test]
    fn test_text_replacement_performance() {
        let temp_file = NamedTempFile::new().unwrap();
        fs::write(temp_file.path(), RUST_STRUCT_CODE).unwrap();

        let (result, duration) = TestTiming::time_operation(|| {
            EditTool::edit_text(
                temp_file.path().to_str().unwrap(),
                "HashMap::new()",
                "HashMap::default()",
            )
        });

        assert!(result.is_ok());
        PerformanceAssertions::assert_duration_under(duration, 10, "simple text replacement");
    }

    #[test]
    fn test_large_file_edit_performance() {
        let temp_dir = TempDir::new().unwrap();
        let file_path = temp_dir.path().join("large.rs");

        // Create a larger file by repeating the struct code
        let large_content = RUST_STRUCT_CODE.repeat(100);
        fs::write(&file_path, &large_content).unwrap();

        let (result, duration) = TestTiming::time_operation(|| {
            EditTool::edit_text(
                file_path.to_str().unwrap(),
                "pub struct User {",
                "pub struct Person {",
            )
        });

        // Should handle first occurrence and detect ambiguity quickly
        assert!(result.is_err()); // Should be ambiguous
        // Should still complete quickly even for large files (relaxed to 50ms for system load variations)
        PerformanceAssertions::assert_duration_under(
            duration,
            50,
            "large file ambiguity detection",
        );
    }

    #[test]
    fn test_find_matches_performance() {
        let temp_file = NamedTempFile::new().unwrap();
        let repeated_pattern = "test\n".repeat(1000);
        fs::write(temp_file.path(), repeated_pattern).unwrap();

        let (result, duration) = TestTiming::time_operation(|| {
            EditTool::find_matches(temp_file.path().to_str().unwrap(), "test")
        });

        assert!(result.is_ok());
        let matches = result.unwrap();
        assert_eq!(matches.matches.len(), 1000);

        // Should complete quickly even with many matches
        PerformanceAssertions::assert_duration_under(duration, 50, "find 1000 matches");
    }

    #[test]
    fn test_repeated_edit_performance() {
        let temp_file = NamedTempFile::new().unwrap();
        fs::write(temp_file.path(), SIMPLE_RUST_CODE).unwrap();

        let start = Instant::now();

        for i in 1..=5 {
            let result = EditTool::edit_line(
                temp_file.path().to_str().unwrap(),
                2,
                &format!("println!(\"Hello, iteration {}\");", i),
            );
            assert!(result.is_ok());
        }

        let total_duration = start.elapsed();

        // 5 edits should complete in under 50ms total (relaxed for system load variations)
        PerformanceAssertions::assert_duration_under(total_duration, 50, "5 sequential edits");
    }
}

#[cfg(test)]
mod backup_and_safety_tests {
    use super::*;

    #[test]
    fn test_backup_file_creation() {
        let temp_file = NamedTempFile::new().unwrap();
        let original_content = "original content";
        fs::write(temp_file.path(), original_content).unwrap();

        let result =
            EditTool::edit_line(temp_file.path().to_str().unwrap(), 1, "modified content").unwrap();

        assert!(result.success);

        // Verify backup exists and contains original content
        let backup_path = format!("{}.backup", temp_file.path().to_str().unwrap());
        assert!(std::path::Path::new(&backup_path).exists());

        let backup_content = fs::read_to_string(&backup_path).unwrap();
        assert_eq!(backup_content, original_content);
    }

    #[test]
    fn test_atomic_write() {
        let temp_file = NamedTempFile::new().unwrap();
        let original_content = "line1\nline2\nline3";
        fs::write(temp_file.path(), original_content).unwrap();

        // Perform edit
        let result =
            EditTool::edit_line(temp_file.path().to_str().unwrap(), 2, "modified line2").unwrap();

        assert!(result.success);

        // File should contain complete, valid content (not partial)
        let new_content = fs::read_to_string(temp_file.path()).unwrap();
        let lines: Vec<&str> = new_content.lines().collect();
        assert_eq!(lines.len(), 3);
        assert_eq!(lines[1], "modified line2");

        // Temporary file should be cleaned up
        let temp_path = format!("{}.tmp", temp_file.path().to_str().unwrap());
        assert!(!std::path::Path::new(&temp_path).exists());
    }
}

#[cfg(test)]
mod line_ending_tests {
    use super::*;

    #[test]
    fn test_windows_line_endings() {
        let temp_file = NamedTempFile::new().unwrap();
        let windows_content = "line1\r\nline2\r\nline3\r\n";
        fs::write(temp_file.path(), windows_content).unwrap();

        let result =
            EditTool::edit_line(temp_file.path().to_str().unwrap(), 2, "modified line2").unwrap();

        assert!(result.success);

        // Should normalize to Unix line endings
        let new_content = fs::read_to_string(temp_file.path()).unwrap();
        assert!(!new_content.contains("\r"));
        assert!(new_content.contains("modified line2"));
    }

    #[test]
    fn test_mixed_line_endings() {
        let temp_file = NamedTempFile::new().unwrap();
        let mixed_content = "line1\nline2\r\nline3\n";
        fs::write(temp_file.path(), mixed_content).unwrap();

        let result = EditTool::edit_text(
            temp_file.path().to_str().unwrap(),
            "line2",
            "modified line2",
        )
        .unwrap();

        assert!(result.success);

        let new_content = fs::read_to_string(temp_file.path()).unwrap();
        assert!(!new_content.contains("\r"));
        assert!(new_content.contains("modified line2"));
    }
}

#[cfg(test)]
mod before_after_state_tests {
    use super::*;

    #[test]
    fn test_before_after_state_tracking() {
        let temp_file = NamedTempFile::new().unwrap();
        fs::write(temp_file.path(), RUST_STRUCT_CODE).unwrap();

        let result = EditTool::edit_line(
            temp_file.path().to_str().unwrap(),
            5, // pub id: u64, line
            "    pub id: u32,",
        )
        .unwrap();

        assert_eq!(result.old_content, "    pub id: u64,");
        assert_eq!(result.new_content, "    pub id: u32,");

        // Context should show the struct definition
        assert!(
            result
                .context_before
                .iter()
                .any(|line| line.contains("pub struct User"))
        );
        assert!(
            result
                .context_after
                .iter()
                .any(|line| line.contains("pub email: String,"))
        );
    }

    #[test]
    fn test_message_content() {
        let temp_file = NamedTempFile::new().unwrap();
        fs::write(temp_file.path(), "test line").unwrap();

        let result =
            EditTool::edit_line(temp_file.path().to_str().unwrap(), 1, "modified test line")
                .unwrap();

        assert!(result.message.contains("Successfully changed line 1"));
        assert!(result.message.contains("from 'test line'"));
        assert!(result.message.contains("to 'modified test line'"));
    }

    #[test]
    fn test_file_path_tracking() {
        let temp_file = NamedTempFile::new().unwrap();
        fs::write(temp_file.path(), "content").unwrap();

        let file_path = temp_file.path().to_str().unwrap();
        let result = EditTool::edit_line(file_path, 1, "new content").unwrap();

        assert_eq!(result.file_path, file_path);
    }
}

#[cfg(test)]
mod comprehensive_integration_tests {
    use super::*;

    #[test]
    fn test_complete_code_refactoring_workflow() {
        let temp_file = NamedTempFile::new().unwrap();
        fs::write(temp_file.path(), RUST_STRUCT_CODE).unwrap();

        let file_path = temp_file.path().to_str().unwrap();

        // Step 1: Add a new field
        let result = EditTool::edit_text(
            file_path,
            "pub settings: HashMap<String, String>,",
            "pub settings: HashMap<String, String>,\n    pub active: bool,",
        )
        .unwrap();
        assert!(result.success);

        // Step 2: Update constructor
        let result = EditTool::edit_text(
            file_path,
            "settings: HashMap::new(),",
            "settings: HashMap::new(),\n            active: true,",
        )
        .unwrap();
        assert!(result.success);

        // Step 3: Add a new method
        let result = EditTool::edit_text(
            file_path,
            "    pub fn get_display_name(&self) -> &str {\n        &self.name\n    }",
            "    pub fn get_display_name(&self) -> &str {\n        &self.name\n    }\n    \n    pub fn is_active(&self) -> bool {\n        self.active\n    }"
        ).unwrap();
        assert!(result.success);

        // Verify final result
        let final_content = fs::read_to_string(file_path).unwrap();
        assert!(final_content.contains("pub active: bool,"));
        assert!(final_content.contains("active: true,"));
        assert!(final_content.contains("pub fn is_active(&self) -> bool"));
    }

    #[test]
    fn test_error_recovery_workflow() {
        let temp_file = NamedTempFile::new().unwrap();
        fs::write(temp_file.path(), SIMPLE_RUST_CODE).unwrap();

        let file_path = temp_file.path().to_str().unwrap();

        // Attempt an invalid edit
        let result = EditTool::edit_line(file_path, 999, "invalid");
        assert!(result.is_err());

        // File should be unchanged
        let content = fs::read_to_string(file_path).unwrap();
        assert_eq!(content, SIMPLE_RUST_CODE);

        // Valid edit should still work
        let result = EditTool::edit_line(file_path, 2, "println!(\"Hello, recovery!\");").unwrap();
        assert!(result.success);

        let final_content = fs::read_to_string(file_path).unwrap();
        assert!(final_content.contains("Hello, recovery!"));
    }

    #[test]
    fn test_concurrent_edit_simulation() {
        let temp_file = NamedTempFile::new().unwrap();
        let multi_line = "line1\nline2\nline3\nline4\nline5";
        fs::write(temp_file.path(), multi_line).unwrap();

        let file_path = temp_file.path().to_str().unwrap();

        // Simulate rapid edits (as might happen from multiple agents)
        let start = Instant::now();

        for i in 1..=5 {
            let result = EditTool::edit_line(file_path, i, &format!("modified line{}", i));
            assert!(result.is_ok());
        }

        let duration = start.elapsed();

        // Should complete all edits quickly (relaxed to 100ms for system load variations)
        assert!(duration.as_millis() < 100);

        // Verify all changes were applied
        let final_content = fs::read_to_string(file_path).unwrap();
        for i in 1..=5 {
            assert!(final_content.contains(&format!("modified line{}", i)));
        }
    }
}