tldr-core 0.1.2

Core analysis engine for TLDR code analysis tool
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
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
//! Test module for change-impact CLI command (Session 6 spec)
//!
//! These tests define expected behavior BEFORE implementation.
//! Tests are designed to FAIL until the modules are implemented.
//!
//! # Test Categories
//!
//! ## 1. Git diff detection tests
//! - HEAD (default), base branch, staged, uncommitted
//! - No git repo fallback
//! - Invalid base branch handling
//!
//! ## 2. Call graph traversal tests
//! - Find callers of changed functions
//! - Depth limiting
//! - Circular dependency handling
//!
//! ## 3. Import graph traversal tests
//! - Find importers of changed modules
//! - Transitive import chains
//!
//! ## 4. Test file detection tests
//! - Python, TypeScript, Go, Rust patterns
//! - Custom test patterns
//!
//! ## 5. Test function extraction tests
//! - Extract specific test functions, not just files
//! - Class-based test methods
//!
//! ## 6. Output format tests
//! - JSON, pytest, pytest-k, jest, go-test, cargo-test
//!
//! Reference: session6-spec.md


// =============================================================================
// Test Fixture Setup Module
// =============================================================================

/// Test fixture utilities for change-impact analysis tests
pub mod fixtures {
    use std::path::{Path, PathBuf};
    use std::process::Command;
    use tempfile::TempDir;

    /// A temporary directory for testing change-impact analysis
    pub struct TestDir {
        pub dir: TempDir,
    }

    impl TestDir {
        /// Create a new empty temporary directory
        pub fn new() -> std::io::Result<Self> {
            let dir = TempDir::new()?;
            Ok(Self { dir })
        }

        /// Get the path to the directory
        pub fn path(&self) -> &Path {
            self.dir.path()
        }

        /// Add a file to the directory
        pub fn add_file(&self, name: &str, content: &str) -> std::io::Result<PathBuf> {
            let path = self.dir.path().join(name);
            if let Some(parent) = path.parent() {
                std::fs::create_dir_all(parent)?;
            }
            std::fs::write(&path, content)?;
            Ok(path)
        }

        /// Initialize as a git repository
        pub fn init_git(&self) -> std::io::Result<()> {
            Command::new("git")
                .args(["init"])
                .current_dir(self.path())
                .output()?;
            Command::new("git")
                .args(["config", "user.email", "test@test.com"])
                .current_dir(self.path())
                .output()?;
            Command::new("git")
                .args(["config", "user.name", "Test"])
                .current_dir(self.path())
                .output()?;
            Ok(())
        }

        /// Stage a file in git
        pub fn git_add(&self, file: &str) -> std::io::Result<()> {
            Command::new("git")
                .args(["add", file])
                .current_dir(self.path())
                .output()?;
            Ok(())
        }

        /// Commit staged files
        pub fn git_commit(&self, message: &str) -> std::io::Result<()> {
            Command::new("git")
                .args(["commit", "-m", message])
                .current_dir(self.path())
                .output()?;
            Ok(())
        }

        /// Create a new branch
        pub fn git_branch(&self, name: &str) -> std::io::Result<()> {
            Command::new("git")
                .args(["checkout", "-b", name])
                .current_dir(self.path())
                .output()?;
            Ok(())
        }
    }

    // -------------------------------------------------------------------------
    // Python Project Fixtures
    // -------------------------------------------------------------------------

    pub const PYTHON_AUTH_MODULE: &str = r#"
def login(username: str, password: str) -> bool:
    """Authenticate user credentials."""
    user = get_user(username)
    if user and verify_password(password, user.password_hash):
        return True
    return False

def logout(session_id: str) -> None:
    """Invalidate user session."""
    invalidate_session(session_id)

def get_user(username: str):
    """Fetch user from database."""
    from .db import query
    return query("SELECT * FROM users WHERE username = ?", username)

def verify_password(password: str, hash: str) -> bool:
    """Verify password against hash."""
    import hashlib
    return hashlib.sha256(password.encode()).hexdigest() == hash
"#;

    pub const PYTHON_UTILS_MODULE: &str = r#"
def format_date(timestamp: int) -> str:
    """Format timestamp as human-readable date."""
    from datetime import datetime
    return datetime.fromtimestamp(timestamp).isoformat()

def parse_config(path: str) -> dict:
    """Parse configuration file."""
    import json
    with open(path) as f:
        return json.load(f)
"#;

    pub const PYTHON_TEST_AUTH: &str = r#"
import pytest
from auth import login, logout

class TestAuth:
    def test_login(self):
        """Test successful login."""
        assert login("admin", "secret") == True

    def test_logout(self):
        """Test logout invalidates session."""
        logout("session123")
        # Verify session is gone

def test_login_failure():
    """Test login with wrong password."""
    assert login("admin", "wrong") == False
"#;

    pub const PYTHON_TEST_UTILS: &str = r#"
from utils import format_date, parse_config

def test_format_date():
    """Test date formatting."""
    result = format_date(1609459200)
    assert "2021" in result

def test_parse_config():
    """Test config parsing."""
    config = parse_config("test.json")
    assert "key" in config
"#;

    // -------------------------------------------------------------------------
    // TypeScript Project Fixtures
    // -------------------------------------------------------------------------

    pub const TS_AUTH_MODULE: &str = r#"
export function login(username: string, password: string): boolean {
    const user = getUser(username);
    if (user && verifyPassword(password, user.passwordHash)) {
        return true;
    }
    return false;
}

export function logout(sessionId: string): void {
    invalidateSession(sessionId);
}

function getUser(username: string): User | null {
    return db.query('SELECT * FROM users WHERE username = ?', username);
}
"#;

    pub const TS_TEST_AUTH: &str = r#"
import { login, logout } from './auth';

describe('Auth', () => {
    test('login with valid credentials', () => {
        expect(login('admin', 'secret')).toBe(true);
    });

    test('logout invalidates session', () => {
        logout('session123');
        // Verify session is gone
    });
});
"#;

    // -------------------------------------------------------------------------
    // Go Project Fixtures
    // -------------------------------------------------------------------------

    pub const GO_AUTH_MODULE: &str = r#"
package auth

func Login(username, password string) bool {
    user := getUser(username)
    if user != nil && verifyPassword(password, user.PasswordHash) {
        return true
    }
    return false
}

func Logout(sessionID string) {
    invalidateSession(sessionID)
}
"#;

    pub const GO_TEST_AUTH: &str = r#"
package auth

import "testing"

func TestLogin(t *testing.T) {
    result := Login("admin", "secret")
    if !result {
        t.Error("Expected login to succeed")
    }
}

func TestLogout(t *testing.T) {
    Logout("session123")
}
"#;

    // -------------------------------------------------------------------------
    // Rust Project Fixtures
    // -------------------------------------------------------------------------

    pub const RUST_AUTH_MODULE: &str = r#"
pub fn login(username: &str, password: &str) -> bool {
    if let Some(user) = get_user(username) {
        return verify_password(password, &user.password_hash);
    }
    false
}

pub fn logout(session_id: &str) {
    invalidate_session(session_id);
}
"#;

    pub const RUST_TEST_AUTH: &str = r#"
use super::*;

#[test]
fn test_login() {
    assert!(login("admin", "secret"));
}

#[test]
fn test_logout() {
    logout("session123");
}
"#;

    // -------------------------------------------------------------------------
    // Circular Dependency Fixture
    // -------------------------------------------------------------------------

    pub const CIRCULAR_MODULE_A: &str = r#"
from module_b import func_b

def func_a():
    return func_b()
"#;

    pub const CIRCULAR_MODULE_B: &str = r#"
from module_a import func_a

def func_b():
    return func_a()
"#;
}

// =============================================================================
// Extended DetectionMethod Tests
// =============================================================================

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

    /// Test DetectionMethod::GitHead (default)
    /// Contract: Uses `git diff HEAD` to find changed files
    #[test]
    #[ignore = "Extended DetectionMethod not yet implemented"]
    fn detection_method_git_head() {
        let test_dir = TestDir::new().unwrap();
        test_dir.init_git().unwrap();
        test_dir
            .add_file("src/auth.py", PYTHON_AUTH_MODULE)
            .unwrap();
        test_dir.git_add("src/auth.py").unwrap();
        test_dir.git_commit("initial").unwrap();

        // Modify file after commit
        test_dir
            .add_file(
                "src/auth.py",
                &format!("{}\n# modified", PYTHON_AUTH_MODULE),
            )
            .unwrap();

        // Expected: GitHead detection finds src/auth.py as changed
        // let report = change_impact_extended(
        //     test_dir.path(),
        //     DetectionMethod::GitHead,
        //     Language::Python,
        //     10,
        //     true,
        //     &[],
        // ).unwrap();
        //
        // assert_eq!(report.detection_method, "git:HEAD");
        // assert!(report.changed_files.iter().any(|f| f.ends_with("auth.py")));

        todo!("Implement detection_method_git_head test");
    }

    /// Test DetectionMethod::GitBase for PR workflows
    /// Contract: Uses `git diff <base>...HEAD` to find files changed since branching
    #[test]
    #[ignore = "Extended DetectionMethod not yet implemented"]
    fn detection_method_git_base() {
        let test_dir = TestDir::new().unwrap();
        test_dir.init_git().unwrap();
        test_dir
            .add_file("src/auth.py", PYTHON_AUTH_MODULE)
            .unwrap();
        test_dir.git_add("src/auth.py").unwrap();
        test_dir.git_commit("initial").unwrap();

        // Create feature branch and modify
        test_dir.git_branch("feature").unwrap();
        test_dir
            .add_file("src/utils.py", PYTHON_UTILS_MODULE)
            .unwrap();
        test_dir.git_add("src/utils.py").unwrap();
        test_dir.git_commit("add utils").unwrap();

        // Expected: GitBase detection with base="main" finds utils.py
        // let report = change_impact_extended(
        //     test_dir.path(),
        //     DetectionMethod::GitBase { base: "main".to_string() },
        //     Language::Python,
        //     10,
        //     true,
        //     &[],
        // ).unwrap();
        //
        // assert_eq!(report.detection_method, "git:main...HEAD");
        // assert!(report.changed_files.iter().any(|f| f.ends_with("utils.py")));
        // assert!(!report.changed_files.iter().any(|f| f.ends_with("auth.py")));

        todo!("Implement detection_method_git_base test");
    }

    /// Test DetectionMethod::GitStaged for pre-commit hooks
    /// Contract: Uses `git diff --staged` to find staged files only
    #[test]
    #[ignore = "Extended DetectionMethod not yet implemented"]
    fn detection_method_git_staged() {
        let test_dir = TestDir::new().unwrap();
        test_dir.init_git().unwrap();
        test_dir
            .add_file("src/auth.py", PYTHON_AUTH_MODULE)
            .unwrap();
        test_dir
            .add_file("src/utils.py", PYTHON_UTILS_MODULE)
            .unwrap();
        test_dir.git_add("src/auth.py").unwrap();
        // utils.py not staged

        // Expected: GitStaged detection finds only auth.py
        // let report = change_impact_extended(
        //     test_dir.path(),
        //     DetectionMethod::GitStaged,
        //     Language::Python,
        //     10,
        //     true,
        //     &[],
        // ).unwrap();
        //
        // assert_eq!(report.detection_method, "git:staged");
        // assert!(report.changed_files.iter().any(|f| f.ends_with("auth.py")));
        // assert!(!report.changed_files.iter().any(|f| f.ends_with("utils.py")));

        todo!("Implement detection_method_git_staged test");
    }

    /// Test DetectionMethod::GitUncommitted
    /// Contract: Uses `git diff` to find all uncommitted changes (staged + unstaged)
    #[test]
    #[ignore = "Extended DetectionMethod not yet implemented"]
    fn detection_method_git_uncommitted() {
        let test_dir = TestDir::new().unwrap();
        test_dir.init_git().unwrap();
        test_dir
            .add_file("src/auth.py", PYTHON_AUTH_MODULE)
            .unwrap();
        test_dir.git_add(".").unwrap();
        test_dir.git_commit("initial").unwrap();

        // Stage one file, leave another unstaged
        test_dir
            .add_file("src/auth.py", &format!("{}\n# staged", PYTHON_AUTH_MODULE))
            .unwrap();
        test_dir.git_add("src/auth.py").unwrap();
        test_dir
            .add_file("src/utils.py", PYTHON_UTILS_MODULE)
            .unwrap();
        // utils.py not staged

        // Expected: GitUncommitted finds both auth.py and utils.py
        // let report = change_impact_extended(
        //     test_dir.path(),
        //     DetectionMethod::GitUncommitted,
        //     Language::Python,
        //     10,
        //     true,
        //     &[],
        // ).unwrap();
        //
        // assert_eq!(report.detection_method, "git:uncommitted");
        // assert!(report.changed_files.iter().any(|f| f.ends_with("auth.py")));
        // assert!(report.changed_files.iter().any(|f| f.ends_with("utils.py")));

        todo!("Implement detection_method_git_uncommitted test");
    }

    /// Test DetectionMethod::Explicit
    /// Contract: Uses explicit file list from CLI args
    #[test]
    #[ignore = "Extended DetectionMethod not yet implemented"]
    fn detection_method_explicit() {
        let test_dir = TestDir::new().unwrap();
        test_dir
            .add_file("src/auth.py", PYTHON_AUTH_MODULE)
            .unwrap();
        test_dir
            .add_file("src/utils.py", PYTHON_UTILS_MODULE)
            .unwrap();

        // No git repo needed for explicit mode
        // let explicit_files = vec![
        //     test_dir.path().join("src/auth.py"),
        // ];
        //
        // let report = change_impact_extended(
        //     test_dir.path(),
        //     DetectionMethod::Explicit,
        //     Language::Python,
        //     10,
        //     true,
        //     &[],
        //     Some(&explicit_files),
        // ).unwrap();
        //
        // assert_eq!(report.detection_method, "explicit");
        // assert_eq!(report.changed_files.len(), 1);
        // assert!(report.changed_files[0].ends_with("auth.py"));

        todo!("Implement detection_method_explicit test");
    }

    /// Test fallback when no git repository exists
    /// Contract: Returns empty with detection_method: "session"
    #[test]
    #[ignore = "Extended DetectionMethod not yet implemented"]
    fn detection_method_no_git_fallback() {
        let test_dir = TestDir::new().unwrap();
        // No git init
        test_dir
            .add_file("src/auth.py", PYTHON_AUTH_MODULE)
            .unwrap();

        // Expected: Falls back to session detection with empty changed_files
        // let report = change_impact_extended(
        //     test_dir.path(),
        //     DetectionMethod::GitHead,
        //     Language::Python,
        //     10,
        //     true,
        //     &[],
        // ).unwrap();
        //
        // assert_eq!(report.detection_method, "session");
        // assert!(report.changed_files.is_empty());
        // assert!(report.affected_tests.is_empty());

        todo!("Implement detection_method_no_git_fallback test");
    }

    /// Test invalid base branch handling
    /// Contract: Returns error with suggestion for valid branches
    #[test]
    #[ignore = "Extended DetectionMethod not yet implemented"]
    fn detection_method_invalid_base_branch() {
        let test_dir = TestDir::new().unwrap();
        test_dir.init_git().unwrap();
        test_dir
            .add_file("src/auth.py", PYTHON_AUTH_MODULE)
            .unwrap();
        test_dir.git_add(".").unwrap();
        test_dir.git_commit("initial").unwrap();

        // Expected: Error when base branch doesn't exist
        // let result = change_impact_extended(
        //     test_dir.path(),
        //     DetectionMethod::GitBase { base: "nonexistent".to_string() },
        //     Language::Python,
        //     10,
        //     true,
        //     &[],
        // );
        //
        // assert!(result.is_err());
        // let err = result.unwrap_err();
        // assert!(err.to_string().contains("branch not found"));

        todo!("Implement detection_method_invalid_base_branch test");
    }
}

// =============================================================================
// Call Graph Traversal Tests
// =============================================================================

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

    /// Test finding callers of changed functions
    /// Contract: Traverses call graph to find all functions that call changed functions
    #[test]
    #[ignore = "Extended call graph traversal not yet implemented"]
    fn call_graph_finds_callers() {
        let test_dir = TestDir::new().unwrap();
        test_dir
            .add_file("src/auth.py", PYTHON_AUTH_MODULE)
            .unwrap();
        test_dir
            .add_file("tests/test_auth.py", PYTHON_TEST_AUTH)
            .unwrap();

        // If login() is changed, test_login() should be affected
        // let report = change_impact_extended(
        //     test_dir.path(),
        //     DetectionMethod::Explicit,
        //     Language::Python,
        //     10,
        //     true,
        //     &[],
        //     Some(&[test_dir.path().join("src/auth.py")]),
        // ).unwrap();
        //
        // assert!(report.affected_functions.iter().any(|f| f.name == "test_login"));

        todo!("Implement call_graph_finds_callers test");
    }

    /// Test depth limiting in call graph traversal
    /// Contract: Stops traversal at specified depth
    #[test]
    #[ignore = "Depth limiting not yet implemented"]
    fn call_graph_depth_limiting() {
        let test_dir = TestDir::new().unwrap();
        // Create a deep call chain: a -> b -> c -> d -> e
        test_dir
            .add_file(
                "src/chain.py",
                r#"
def func_a():
    return func_b()

def func_b():
    return func_c()

def func_c():
    return func_d()

def func_d():
    return func_e()

def func_e():
    return 42
"#,
            )
            .unwrap();

        // With depth=2, changing func_e should only affect func_d and func_c
        // let report = change_impact_extended(
        //     test_dir.path(),
        //     DetectionMethod::Explicit,
        //     Language::Python,
        //     2,  // depth limit
        //     true,
        //     &[],
        //     Some(&[test_dir.path().join("src/chain.py")]),
        // ).unwrap();
        //
        // let affected_names: Vec<_> = report.affected_functions.iter()
        //     .map(|f| f.name.as_str())
        //     .collect();
        // assert!(affected_names.contains(&"func_d"));
        // assert!(affected_names.contains(&"func_c"));
        // assert!(!affected_names.contains(&"func_a"));  // Too deep

        todo!("Implement call_graph_depth_limiting test");
    }

    /// Test circular dependency handling
    /// Contract: Terminates without infinite loop when cycles exist
    #[test]
    #[ignore = "Circular dependency handling not yet verified"]
    fn call_graph_circular_deps() {
        let test_dir = TestDir::new().unwrap();
        test_dir
            .add_file("src/module_a.py", CIRCULAR_MODULE_A)
            .unwrap();
        test_dir
            .add_file("src/module_b.py", CIRCULAR_MODULE_B)
            .unwrap();

        // Should complete without hanging
        // let report = change_impact_extended(
        //     test_dir.path(),
        //     DetectionMethod::Explicit,
        //     Language::Python,
        //     10,
        //     true,
        //     &[],
        //     Some(&[test_dir.path().join("src/module_a.py")]),
        // ).unwrap();
        //
        // // Both functions should be affected but no infinite loop
        // assert!(report.affected_functions.iter().any(|f| f.name == "func_a"));
        // assert!(report.affected_functions.iter().any(|f| f.name == "func_b"));

        todo!("Implement call_graph_circular_deps test");
    }
}

// =============================================================================
// Import Graph Traversal Tests
// =============================================================================

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

    /// Test finding importers of changed modules
    /// Contract: Uses import graph to find modules that import changed files
    #[test]
    #[ignore = "Import graph traversal not yet implemented"]
    fn import_graph_finds_importers() {
        let test_dir = TestDir::new().unwrap();
        test_dir
            .add_file("src/utils.py", PYTHON_UTILS_MODULE)
            .unwrap();
        test_dir
            .add_file(
                "src/auth.py",
                r#"
from utils import format_date

def get_login_time():
    return format_date(1234567890)
"#,
            )
            .unwrap();

        // If utils.py changes, auth.py should be affected via import
        // let report = change_impact_extended(
        //     test_dir.path(),
        //     DetectionMethod::Explicit,
        //     Language::Python,
        //     10,
        //     true,  // include_imports
        //     &[],
        //     Some(&[test_dir.path().join("src/utils.py")]),
        // ).unwrap();
        //
        // assert!(report.affected_functions.iter().any(|f|
        //     f.file.ends_with("auth.py")
        // ));

        todo!("Implement import_graph_finds_importers test");
    }

    /// Test include_imports flag can be disabled
    /// Contract: When false, only call graph is used
    #[test]
    #[ignore = "include_imports flag not yet implemented"]
    fn import_graph_can_be_disabled() {
        let test_dir = TestDir::new().unwrap();
        test_dir
            .add_file("src/utils.py", PYTHON_UTILS_MODULE)
            .unwrap();
        test_dir
            .add_file(
                "src/auth.py",
                r#"
from utils import format_date
# But doesn't call any function from utils
"#,
            )
            .unwrap();

        // With include_imports=false, auth.py should NOT be affected
        // let report = change_impact_extended(
        //     test_dir.path(),
        //     DetectionMethod::Explicit,
        //     Language::Python,
        //     10,
        //     false,  // include_imports disabled
        //     &[],
        //     Some(&[test_dir.path().join("src/utils.py")]),
        // ).unwrap();
        //
        // assert!(report.affected_functions.is_empty() ||
        //     !report.affected_functions.iter().any(|f| f.file.ends_with("auth.py")));

        todo!("Implement import_graph_can_be_disabled test");
    }
}

// =============================================================================
// Test File Detection Tests
// =============================================================================

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

    /// Test Python test file patterns
    /// Contract: Detects test_*.py, *_test.py, conftest.py, tests/ directory
    #[test]
    #[ignore = "Test pattern detection to verify"]
    fn detects_python_test_files() {
        let test_dir = TestDir::new().unwrap();
        test_dir.add_file("test_auth.py", PYTHON_TEST_AUTH).unwrap();
        test_dir.add_file("auth_test.py", PYTHON_TEST_AUTH).unwrap();
        test_dir.add_file("conftest.py", "import pytest").unwrap();
        test_dir
            .add_file("tests/test_utils.py", PYTHON_TEST_UTILS)
            .unwrap();
        test_dir.add_file("src/main.py", "print('hello')").unwrap();

        // Expected: 4 test files detected
        // let test_files = detect_test_files(test_dir.path(), Language::Python).unwrap();
        // assert_eq!(test_files.len(), 4);
        // assert!(!test_files.iter().any(|f| f.ends_with("main.py")));

        todo!("Implement detects_python_test_files test");
    }

    /// Test TypeScript test file patterns
    /// Contract: Detects *.test.ts, *.spec.ts, __tests__/ directory
    #[test]
    #[ignore = "Test pattern detection to verify"]
    fn detects_typescript_test_files() {
        let test_dir = TestDir::new().unwrap();
        test_dir.add_file("auth.test.ts", TS_TEST_AUTH).unwrap();
        test_dir.add_file("auth.spec.ts", TS_TEST_AUTH).unwrap();
        test_dir
            .add_file("__tests__/utils.ts", "export {}")
            .unwrap();
        test_dir
            .add_file("src/main.ts", "console.log('hello')")
            .unwrap();

        // Expected: 3 test files detected
        // let test_files = detect_test_files(test_dir.path(), Language::TypeScript).unwrap();
        // assert_eq!(test_files.len(), 3);
        // assert!(!test_files.iter().any(|f| f.ends_with("main.ts")));

        todo!("Implement detects_typescript_test_files test");
    }

    /// Test Go test file patterns
    /// Contract: Detects *_test.go files
    #[test]
    #[ignore = "Test pattern detection to verify"]
    fn detects_go_test_files() {
        let test_dir = TestDir::new().unwrap();
        test_dir.add_file("auth_test.go", GO_TEST_AUTH).unwrap();
        test_dir.add_file("auth.go", GO_AUTH_MODULE).unwrap();

        // Expected: 1 test file detected
        // let test_files = detect_test_files(test_dir.path(), Language::Go).unwrap();
        // assert_eq!(test_files.len(), 1);
        // assert!(test_files[0].ends_with("auth_test.go"));

        todo!("Implement detects_go_test_files test");
    }

    /// Test Rust test file patterns
    /// Contract: Detects tests/*.rs, **/tests.rs
    #[test]
    #[ignore = "Test pattern detection to verify"]
    fn detects_rust_test_files() {
        let test_dir = TestDir::new().unwrap();
        test_dir
            .add_file("tests/auth_tests.rs", RUST_TEST_AUTH)
            .unwrap();
        test_dir
            .add_file("src/auth/tests.rs", RUST_TEST_AUTH)
            .unwrap();
        test_dir.add_file("src/auth.rs", RUST_AUTH_MODULE).unwrap();

        // Expected: 2 test files detected
        // let test_files = detect_test_files(test_dir.path(), Language::Rust).unwrap();
        // assert_eq!(test_files.len(), 2);

        todo!("Implement detects_rust_test_files test");
    }

    /// Test custom test patterns
    /// Contract: --test-patterns flag overrides defaults
    #[test]
    #[ignore = "Custom test patterns not yet implemented"]
    fn custom_test_patterns() {
        let test_dir = TestDir::new().unwrap();
        test_dir
            .add_file("spec/auth_spec.py", PYTHON_TEST_AUTH)
            .unwrap();
        test_dir
            .add_file("tests/test_auth.py", PYTHON_TEST_AUTH)
            .unwrap();

        // With custom pattern "spec/*_spec.py", only spec/ files detected
        // let test_files = detect_test_files_with_patterns(
        //     test_dir.path(),
        //     Language::Python,
        //     &["spec/*_spec.py".to_string()],
        // ).unwrap();
        //
        // assert_eq!(test_files.len(), 1);
        // assert!(test_files[0].ends_with("auth_spec.py"));

        todo!("Implement custom_test_patterns test");
    }
}

// =============================================================================
// Test Function Extraction Tests
// =============================================================================

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

    /// Test extracting specific test functions (not just files)
    /// Contract: Returns TestFunction with file, function, class, line
    #[test]
    #[ignore = "Test function extraction not yet implemented"]
    fn extracts_test_functions() {
        let test_dir = TestDir::new().unwrap();
        test_dir
            .add_file("tests/test_auth.py", PYTHON_TEST_AUTH)
            .unwrap();
        test_dir
            .add_file("src/auth.py", PYTHON_AUTH_MODULE)
            .unwrap();

        // let report = change_impact_extended(
        //     test_dir.path(),
        //     DetectionMethod::Explicit,
        //     Language::Python,
        //     10,
        //     true,
        //     &[],
        //     Some(&[test_dir.path().join("src/auth.py")]),
        // ).unwrap();
        //
        // // Should have specific test functions
        // assert!(!report.affected_test_functions.is_empty());
        // let test_login = report.affected_test_functions.iter()
        //     .find(|t| t.function == "test_login")
        //     .expect("test_login not found");
        // assert_eq!(test_login.class, Some("TestAuth".to_string()));
        // assert!(test_login.line > 0);

        todo!("Implement extracts_test_functions test");
    }

    /// Test extracting class-based test methods
    /// Contract: class field is populated for test methods in classes
    #[test]
    #[ignore = "Class-based test extraction not yet implemented"]
    fn extracts_class_test_methods() {
        let test_dir = TestDir::new().unwrap();
        test_dir
            .add_file("tests/test_auth.py", PYTHON_TEST_AUTH)
            .unwrap();

        // let test_funcs = extract_test_functions(
        //     &test_dir.path().join("tests/test_auth.py"),
        //     Language::Python,
        // ).unwrap();
        //
        // let class_method = test_funcs.iter()
        //     .find(|t| t.function == "test_login" && t.class.is_some())
        //     .expect("Class-based test_login not found");
        // assert_eq!(class_method.class, Some("TestAuth".to_string()));
        //
        // let standalone = test_funcs.iter()
        //     .find(|t| t.function == "test_login_failure")
        //     .expect("Standalone test_login_failure not found");
        // assert!(standalone.class.is_none());

        todo!("Implement extracts_class_test_methods test");
    }
}

// =============================================================================
// Output Format Tests
// =============================================================================

#[cfg(test)]
mod output_format_tests {
    

    /// Test pytest format output
    /// Contract: Space-separated test file paths
    #[test]
    #[ignore = "pytest format not yet implemented"]
    fn format_pytest() {
        // Given affected tests: tests/test_auth.py, tests/test_utils.py
        // let report = ChangeImpactReport {
        //     affected_tests: vec![
        //         PathBuf::from("tests/test_auth.py"),
        //         PathBuf::from("tests/test_utils.py"),
        //     ],
        //     ..Default::default()
        // };
        //
        // let output = format_for_runner(&report, RunnerFormat::Pytest);
        // assert_eq!(output, "tests/test_auth.py tests/test_utils.py");

        todo!("Implement format_pytest test");
    }

    /// Test pytest-k format output
    /// Contract: pytest test_file.py::test_func format
    #[test]
    #[ignore = "pytest-k format not yet implemented"]
    fn format_pytest_k() {
        // Given affected test functions with class
        // let report = ChangeImpactReport {
        //     affected_test_functions: vec![
        //         TestFunction {
        //             file: PathBuf::from("tests/test_auth.py"),
        //             function: "test_login".to_string(),
        //             class: Some("TestAuth".to_string()),
        //             line: 15,
        //         },
        //         TestFunction {
        //             file: PathBuf::from("tests/test_utils.py"),
        //             function: "test_helper".to_string(),
        //             class: None,
        //             line: 8,
        //         },
        //     ],
        //     ..Default::default()
        // };
        //
        // let output = format_for_runner(&report, RunnerFormat::PytestK);
        // assert!(output.contains("tests/test_auth.py::TestAuth::test_login"));
        // assert!(output.contains("tests/test_utils.py::test_helper"));

        todo!("Implement format_pytest_k test");
    }

    /// Test jest format output
    /// Contract: --findRelatedTests followed by changed files
    #[test]
    #[ignore = "jest format not yet implemented"]
    fn format_jest() {
        // let report = ChangeImpactReport {
        //     changed_files: vec![
        //         PathBuf::from("src/auth.ts"),
        //         PathBuf::from("src/utils.ts"),
        //     ],
        //     ..Default::default()
        // };
        //
        // let output = format_for_runner(&report, RunnerFormat::Jest);
        // assert_eq!(output, "--findRelatedTests src/auth.ts src/utils.ts");

        todo!("Implement format_jest test");
    }

    /// Test go test format output
    /// Contract: -run "TestA|TestB|TestC" regex format
    #[test]
    #[ignore = "go test format not yet implemented"]
    fn format_go_test() {
        // let report = ChangeImpactReport {
        //     affected_test_functions: vec![
        //         TestFunction {
        //             file: PathBuf::from("auth_test.go"),
        //             function: "TestLogin".to_string(),
        //             class: None,
        //             line: 10,
        //         },
        //         TestFunction {
        //             file: PathBuf::from("auth_test.go"),
        //             function: "TestLogout".to_string(),
        //             class: None,
        //             line: 20,
        //         },
        //     ],
        //     ..Default::default()
        // };
        //
        // let output = format_for_runner(&report, RunnerFormat::GoTest);
        // assert_eq!(output, "-run \"TestLogin|TestLogout\"");

        todo!("Implement format_go_test test");
    }

    /// Test cargo test format output
    /// Contract: Space-separated test function names
    #[test]
    #[ignore = "cargo test format not yet implemented"]
    fn format_cargo_test() {
        // let report = ChangeImpactReport {
        //     affected_test_functions: vec![
        //         TestFunction {
        //             file: PathBuf::from("tests/auth_tests.rs"),
        //             function: "test_login".to_string(),
        //             class: None,
        //             line: 5,
        //         },
        //         TestFunction {
        //             file: PathBuf::from("tests/auth_tests.rs"),
        //             function: "test_logout".to_string(),
        //             class: None,
        //             line: 15,
        //         },
        //     ],
        //     ..Default::default()
        // };
        //
        // let output = format_for_runner(&report, RunnerFormat::CargoTest);
        // assert_eq!(output, "test_login test_logout");

        todo!("Implement format_cargo_test test");
    }
}

// =============================================================================
// Edge Case Tests
// =============================================================================

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

    /// Test empty project
    /// Contract: Returns empty report, no error
    #[test]
    #[ignore = "Edge case: empty project"]
    fn empty_project() {
        let test_dir = TestDir::new().unwrap();
        test_dir.init_git().unwrap();

        // let report = change_impact_extended(
        //     test_dir.path(),
        //     DetectionMethod::GitHead,
        //     Language::Python,
        //     10,
        //     true,
        //     &[],
        // ).unwrap();
        //
        // assert!(report.changed_files.is_empty());
        // assert!(report.affected_tests.is_empty());

        todo!("Implement empty_project test");
    }

    /// Test no changes detected
    /// Contract: Returns empty affected_tests (success, not error)
    #[test]
    #[ignore = "Edge case: no changes"]
    fn no_changes_detected() {
        let test_dir = TestDir::new().unwrap();
        test_dir.init_git().unwrap();
        test_dir
            .add_file("src/auth.py", PYTHON_AUTH_MODULE)
            .unwrap();
        test_dir.git_add(".").unwrap();
        test_dir.git_commit("initial").unwrap();
        // No changes after commit

        // let report = change_impact_extended(
        //     test_dir.path(),
        //     DetectionMethod::GitHead,
        //     Language::Python,
        //     10,
        //     true,
        //     &[],
        // ).unwrap();
        //
        // assert!(report.changed_files.is_empty());
        // assert!(report.affected_tests.is_empty());
        // // Should not be an error

        todo!("Implement no_changes_detected test");
    }

    /// Test language mismatch filtering
    /// Contract: Only files matching target language are considered
    #[test]
    #[ignore = "Edge case: language mismatch"]
    fn language_mismatch_filtering() {
        let test_dir = TestDir::new().unwrap();
        test_dir.init_git().unwrap();
        test_dir
            .add_file("src/auth.py", PYTHON_AUTH_MODULE)
            .unwrap();
        test_dir.add_file("src/auth.ts", TS_AUTH_MODULE).unwrap();
        test_dir.git_add(".").unwrap();
        test_dir.git_commit("initial").unwrap();

        // Modify both files
        test_dir
            .add_file("src/auth.py", &format!("{}\n# changed", PYTHON_AUTH_MODULE))
            .unwrap();
        test_dir
            .add_file("src/auth.ts", &format!("{}\n// changed", TS_AUTH_MODULE))
            .unwrap();

        // With Language::Python, only auth.py should be in changed_files
        // let report = change_impact_extended(
        //     test_dir.path(),
        //     DetectionMethod::GitHead,
        //     Language::Python,
        //     10,
        //     true,
        //     &[],
        // ).unwrap();
        //
        // assert_eq!(report.changed_files.len(), 1);
        // assert!(report.changed_files[0].ends_with("auth.py"));

        todo!("Implement language_mismatch_filtering test");
    }

    /// Test missing test files
    /// Contract: affected_tests is empty, affected_functions may have items
    #[test]
    #[ignore = "Edge case: missing test files"]
    fn missing_test_files() {
        let test_dir = TestDir::new().unwrap();
        test_dir
            .add_file("src/auth.py", PYTHON_AUTH_MODULE)
            .unwrap();
        // No test files

        // let report = change_impact_extended(
        //     test_dir.path(),
        //     DetectionMethod::Explicit,
        //     Language::Python,
        //     10,
        //     true,
        //     &[],
        //     Some(&[test_dir.path().join("src/auth.py")]),
        // ).unwrap();
        //
        // assert!(report.affected_tests.is_empty());
        // // But affected_functions may have items from call graph

        todo!("Implement missing_test_files test");
    }

    /// Test path not found
    /// Contract: Returns PathNotFound error with exit code 2
    #[test]
    #[ignore = "Edge case: path not found"]
    fn path_not_found() {
        // let result = change_impact_extended(
        //     Path::new("/nonexistent/path"),
        //     DetectionMethod::GitHead,
        //     Language::Python,
        //     10,
        //     true,
        //     &[],
        // );
        //
        // assert!(result.is_err());
        // // Error should indicate path not found

        todo!("Implement path_not_found test");
    }
}

// =============================================================================
// Metadata Tests
// =============================================================================

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

    /// Test report includes call graph metadata
    /// Contract: Metadata includes call_graph_nodes, call_graph_edges, analysis_depth
    #[test]
    #[ignore = "Metadata not yet implemented"]
    fn report_includes_metadata() {
        let test_dir = TestDir::new().unwrap();
        test_dir
            .add_file("src/auth.py", PYTHON_AUTH_MODULE)
            .unwrap();
        test_dir
            .add_file("tests/test_auth.py", PYTHON_TEST_AUTH)
            .unwrap();

        // let report = change_impact_extended(
        //     test_dir.path(),
        //     DetectionMethod::Explicit,
        //     Language::Python,
        //     5,
        //     true,
        //     &[],
        //     Some(&[test_dir.path().join("src/auth.py")]),
        // ).unwrap();
        //
        // assert_eq!(report.metadata.language, "python");
        // assert!(report.metadata.call_graph_nodes > 0);
        // assert!(report.metadata.call_graph_edges >= 0);
        // assert_eq!(report.metadata.analysis_depth, Some(5));

        todo!("Implement report_includes_metadata test");
    }
}