tldr-core 0.1.4

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
//! Tests for Phase 14: Builder V2
//!
//! These tests verify the V2 call graph builder implementation per
//! `migration/spec/phases-14-16-spec.md` Section 14.
//!
//! All tests are designed to fail initially (red phase of TDD) since
//! the implementation does not exist yet. They will pass once the
//! `builder_v2` module is implemented.

use std::collections::HashSet;
use std::fs;
use std::path::{Path, PathBuf};
use tempfile::TempDir;

// Types that will be created in builder_v2.rs
use super::builder_v2::{build_project_call_graph_v2, BuildConfig, BuildError};
use super::cross_file_types::CallType;

// =============================================================================
// Test Fixtures
// =============================================================================

/// Creates a temporary directory with Python test files.
fn create_python_project() -> TempDir {
    let dir = TempDir::new().unwrap();

    // main.py - calls helper.process()
    let main_py = r#"
from helper import process

def main():
    process()

if __name__ == "__main__":
    main()
"#;
    fs::write(dir.path().join("main.py"), main_py).unwrap();

    // helper.py - defines process()
    let helper_py = r#"
def process():
    print("processing")
"#;
    fs::write(dir.path().join("helper.py"), helper_py).unwrap();

    dir
}

/// Creates a project with local (intra-file) calls.
fn create_intra_file_project() -> TempDir {
    let dir = TempDir::new().unwrap();

    let code = r#"
def foo():
    bar()

def bar():
    baz()

def baz():
    pass
"#;
    fs::write(dir.path().join("module.py"), code).unwrap();

    dir
}

/// Creates a project with method calls requiring type resolution.
fn create_method_call_project() -> TempDir {
    let dir = TempDir::new().unwrap();

    // models.py
    let models = r#"
class User:
    def save(self):
        pass

    def delete(self):
        pass
"#;
    fs::write(dir.path().join("models.py"), models).unwrap();

    // service.py
    let service = r#"
from models import User

def create_user():
    user = User()
    user.save()

def remove_user(user: User):
    user.delete()
"#;
    fs::write(dir.path().join("service.py"), service).unwrap();

    dir
}

/// Creates a large project for memory/performance testing.
fn create_large_project(num_files: usize) -> TempDir {
    let dir = TempDir::new().unwrap();

    for i in 0..num_files {
        let code = format!(
            r#"
def func_{i}():
    pass

def caller_{i}():
    func_{i}()
"#,
            i = i
        );
        fs::write(dir.path().join(format!("module_{}.py", i)), code).unwrap();
    }

    dir
}

/// Creates a project with symlinks that could cause cycles.
fn create_symlink_project() -> TempDir {
    let dir = TempDir::new().unwrap();

    // Create a subdirectory
    let subdir = dir.path().join("subdir");
    fs::create_dir(&subdir).unwrap();

    // Create a file
    let code = "def foo(): pass";
    fs::write(subdir.join("module.py"), code).unwrap();

    // Create a symlink that points back to parent (potential cycle)
    #[cfg(unix)]
    {
        use std::os::unix::fs::symlink;
        let _ = symlink(dir.path(), subdir.join("parent_link"));
    }

    dir
}

/// Creates a project with non-UTF8 file (using Latin-1 encoding).
fn create_non_utf8_project() -> TempDir {
    let dir = TempDir::new().unwrap();

    // Write a file with Latin-1 encoded content (invalid UTF-8)
    // This simulates legacy code files with non-UTF8 encoding
    let latin1_bytes: Vec<u8> = vec![
        0x64, 0x65, 0x66, 0x20, 0x66, 0x6f, 0x6f, 0x28, 0x29, 0x3a, 0x0a, // def foo():
        0x20, 0x20, 0x20, 0x20, 0x70, 0x61, 0x73, 0x73, 0x0a, // pass
        0x0a, 0x23, 0x20, 0xe9, 0xe8, 0xe0, // # with some Latin-1 chars
    ];
    fs::write(dir.path().join("legacy.py"), latin1_bytes).unwrap();

    dir
}

// =============================================================================
// Phase 14.2: Main Entry Point Tests
// =============================================================================

mod main_entry_point {
    use super::*;

    /// Test: Build call graph for an empty project.
    /// Spec: "Empty Projects - Return empty CallGraphIR with no files"
    #[test]
    fn test_build_empty_project() {
        let dir = TempDir::new().unwrap();
        let config = BuildConfig {
            language: "python".to_string(),
            ..Default::default()
        };

        let result = build_project_call_graph_v2(dir.path(), config);

        assert!(result.is_ok(), "Empty project should succeed");
        let ir = result.unwrap();
        assert_eq!(ir.file_count(), 0, "Empty project should have 0 files");
        assert_eq!(
            ir.function_count(),
            0,
            "Empty project should have 0 functions"
        );
    }

    /// Test: Build call graph for a single file.
    #[test]
    fn test_build_single_file() {
        let dir = create_intra_file_project();
        let config = BuildConfig {
            language: "python".to_string(),
            ..Default::default()
        };

        let result = build_project_call_graph_v2(dir.path(), config);

        assert!(result.is_ok(), "Single file project should succeed");
        let ir = result.unwrap();
        assert_eq!(ir.file_count(), 1, "Should have 1 file");
        assert!(
            ir.function_count() >= 3,
            "Should have at least 3 functions (foo, bar, baz)"
        );
    }

    /// Test: Build call graph with imports across files.
    #[test]
    fn test_build_with_imports() {
        let dir = create_python_project();
        let config = BuildConfig {
            language: "python".to_string(),
            ..Default::default()
        };

        let result = build_project_call_graph_v2(dir.path(), config);

        assert!(result.is_ok(), "Project with imports should succeed");
        let ir = result.unwrap();
        assert_eq!(
            ir.file_count(),
            2,
            "Should have 2 files (main.py, helper.py)"
        );
    }

    /// Test: Cross-file call resolution.
    /// Spec Section 14.4: "For each call site, resolve callee via imports"
    #[test]
    fn test_build_cross_file_calls() {
        let dir = create_python_project();
        let config = BuildConfig {
            language: "python".to_string(),
            ..Default::default()
        };

        let ir = build_project_call_graph_v2(dir.path(), config).unwrap();

        // main.py calls helper.process()
        // We should have an edge from main.py:main -> helper.py:process
        let main_file = ir.get_file("main.py");
        assert!(main_file.is_some(), "main.py should be in IR");

        let main_ir = main_file.unwrap();
        let calls: Vec<_> = main_ir
            .calls
            .values()
            .flatten()
            .filter(|c| c.target == "process")
            .collect();

        assert!(!calls.is_empty(), "Should have call to 'process'");
    }

    /// Test: Method resolution with type information.
    /// Spec Section 14.6: "Type-aware method resolution"
    #[test]
    fn test_build_method_resolution() {
        let dir = create_method_call_project();
        let config = BuildConfig {
            language: "python".to_string(),
            use_type_resolution: true,
            ..Default::default()
        };

        let ir = build_project_call_graph_v2(dir.path(), config).unwrap();

        // service.py calls user.save() and user.delete()
        let service_file = ir.get_file("service.py");
        assert!(service_file.is_some(), "service.py should be in IR");

        let service_ir = service_file.unwrap();

        // With type resolution enabled, we should resolve user.save() -> User.save
        let method_calls: Vec<_> = service_ir
            .calls
            .values()
            .flatten()
            .filter(|c| c.call_type == CallType::Method)
            .collect();

        assert!(
            method_calls.len() >= 2,
            "Should have at least 2 method calls"
        );
    }
}

// =============================================================================
// Phase 14.5: Parallel Processing Tests
// =============================================================================

mod parallel_processing {
    use super::*;

    /// Test: Parallel processing produces deterministic results.
    /// Spec Section 14.11.2: "Deterministic - Same input -> same output"
    #[test]
    fn test_parallel_processing() {
        let dir = create_large_project(100);
        let config = BuildConfig {
            language: "python".to_string(),
            parallelism: 4, // Force 4 threads
            ..Default::default()
        };

        // Run twice and compare
        let ir1 = build_project_call_graph_v2(dir.path(), config.clone()).unwrap();
        let ir2 = build_project_call_graph_v2(dir.path(), config).unwrap();

        // Results must be identical
        assert_eq!(
            ir1.file_count(),
            ir2.file_count(),
            "File counts should match"
        );
        assert_eq!(
            ir1.function_count(),
            ir2.function_count(),
            "Function counts should match"
        );

        // Edge sets should be identical (order doesn't matter)
        let edges1: HashSet<String> = ir1
            .files
            .values()
            .flat_map(|f| f.calls.values().flatten())
            .map(|c| format!("{}:{}", c.caller, c.target))
            .collect();
        let edges2: HashSet<String> = ir2
            .files
            .values()
            .flat_map(|f| f.calls.values().flatten())
            .map(|c| format!("{}:{}", c.caller, c.target))
            .collect();

        assert_eq!(edges1, edges2, "Edge sets should be identical across runs");
    }

    /// Test: Automatic parallelism detection (0 = auto).
    #[test]
    fn test_parallelism_auto_detect() {
        let dir = create_intra_file_project();
        let config = BuildConfig {
            language: "python".to_string(),
            parallelism: 0, // Auto-detect
            ..Default::default()
        };

        let result = build_project_call_graph_v2(dir.path(), config);
        assert!(result.is_ok(), "Auto parallelism should work");
    }
}

// =============================================================================
// Phase 14.8: Memory Management Tests
// =============================================================================

mod memory_management {
    use super::*;

    /// Test: Memory usage stays bounded for large codebases.
    /// Spec Section 14.8: "Peak memory < 500MB with interning"
    /// Note: This is a heuristic test - we check relative memory, not absolute.
    #[test]
    fn test_memory_bounded() {
        let dir = create_large_project(500);
        let config = BuildConfig {
            language: "python".to_string(),
            ..Default::default()
        };

        // This test verifies the build completes without memory issues
        // A proper memory test would need external instrumentation
        let result = build_project_call_graph_v2(dir.path(), config);
        assert!(result.is_ok(), "Large project should complete without OOM");

        let ir = result.unwrap();
        assert_eq!(ir.file_count(), 500, "Should process all 500 files");
    }

    /// Test: String interning deduplicates paths.
    /// Spec Section 14.8: "String Interning - Expected reduction: 1.9GB -> ~80MB"
    #[test]
    fn test_string_interning_dedup() {
        let dir = create_large_project(100);
        let config = BuildConfig {
            language: "python".to_string(),
            ..Default::default()
        };

        let ir = build_project_call_graph_v2(dir.path(), config).unwrap();

        // Verify we have many files but string storage is efficient
        assert_eq!(ir.file_count(), 100);

        // The interner should show deduplication
        // This would require access to interner stats - placeholder assertion
        // In real impl: assert!(interner.stats().dedup_ratio() > 0.0);
    }
}

// =============================================================================
// Phase 14: Edge Cases
// =============================================================================

mod edge_cases {
    use super::*;

    /// Test: Graceful handling of non-UTF8 files.
    /// Spec: "Non-UTF8 Files - Try UTF-8, fallback to latin-1"
    #[test]
    fn test_non_utf8_fallback() {
        let dir = create_non_utf8_project();
        let config = BuildConfig {
            language: "python".to_string(),
            ..Default::default()
        };

        let result = build_project_call_graph_v2(dir.path(), config);

        // Should not fail - should either parse or skip with warning
        assert!(result.is_ok(), "Non-UTF8 file should not cause failure");
    }

    /// Test: Symlink cycle detection.
    /// Spec: "Symlink Cycles - Break cycle by not following symlink if target already visited"
    #[test]
    #[cfg(unix)]
    fn test_symlink_cycle_handling() {
        let dir = create_symlink_project();
        let config = BuildConfig {
            language: "python".to_string(),
            ..Default::default()
        };

        let result = build_project_call_graph_v2(dir.path(), config);

        // Should complete without infinite loop
        assert!(
            result.is_ok(),
            "Symlink cycles should not cause infinite loop"
        );
    }

    /// Test: Project root validation.
    /// Spec Section 14.10: "RootNotFound if root doesn't exist"
    #[test]
    fn test_root_not_found() {
        let config = BuildConfig {
            language: "python".to_string(),
            ..Default::default()
        };

        let result = build_project_call_graph_v2(Path::new("/nonexistent/path"), config);

        assert!(result.is_err(), "Nonexistent root should fail");
        assert!(matches!(result.unwrap_err(), BuildError::RootNotFound(_)));
    }

    /// Test: Unsupported language handling.
    /// Spec Section 14.10: "UnsupportedLanguage if language not in registry"
    #[test]
    fn test_unsupported_language() {
        let dir = TempDir::new().unwrap();
        let config = BuildConfig {
            language: "brainfuck".to_string(), // Not supported
            ..Default::default()
        };

        let result = build_project_call_graph_v2(dir.path(), config);

        assert!(result.is_err(), "Unsupported language should fail");
        assert!(matches!(
            result.unwrap_err(),
            BuildError::UnsupportedLanguage(_)
        ));
    }
}

// =============================================================================
// Phase 14.3: BuildConfig Tests
// =============================================================================

mod build_config {
    use super::*;

    /// Test: BuildConfig default values.
    #[test]
    fn test_build_config_defaults() {
        let config = BuildConfig::default();

        assert!(config.language.is_empty() || config.language == "python");
        assert!(!config.use_workspace_config);
        assert!(config.workspace_roots.is_empty());
        assert!(!config.use_type_resolution);
        assert!(config.respect_ignore);
        assert_eq!(config.parallelism, 0); // Auto-detect
        assert!(!config.verbose);
    }

    /// Test: Workspace config filtering.
    /// Spec Section 14.4: "scan_project(root, config.language, config)"
    #[test]
    fn test_workspace_config_filtering() {
        let dir = TempDir::new().unwrap();

        // Create multi-package structure
        let pkg1 = dir.path().join("pkg1");
        let pkg2 = dir.path().join("pkg2");
        fs::create_dir(&pkg1).unwrap();
        fs::create_dir(&pkg2).unwrap();

        fs::write(pkg1.join("module.py"), "def foo(): pass").unwrap();
        fs::write(pkg2.join("module.py"), "def bar(): pass").unwrap();

        // Without workspace config - should scan all
        let config_all = BuildConfig {
            language: "python".to_string(),
            use_workspace_config: false,
            ..Default::default()
        };

        let ir_all = build_project_call_graph_v2(dir.path(), config_all).unwrap();
        assert_eq!(
            ir_all.file_count(),
            2,
            "Should find both packages without filtering"
        );

        // With workspace config - should only scan pkg1
        let config_filtered = BuildConfig {
            language: "python".to_string(),
            use_workspace_config: true,
            workspace_roots: vec![PathBuf::from("pkg1")],
            ..Default::default()
        };

        let ir_filtered = build_project_call_graph_v2(dir.path(), config_filtered).unwrap();
        assert_eq!(ir_filtered.file_count(), 1, "Should only scan pkg1");
        assert!(ir_filtered.get_file("pkg1/module.py").is_some());
        assert!(ir_filtered.get_file("pkg2/module.py").is_none());
    }

    /// Test: Respect .tldrignore patterns.
    #[test]
    fn test_respect_ignore_patterns() {
        let dir = TempDir::new().unwrap();

        // Create files
        fs::write(dir.path().join("included.py"), "def foo(): pass").unwrap();
        fs::write(dir.path().join("excluded.py"), "def bar(): pass").unwrap();

        // Create .tldrignore
        fs::write(dir.path().join(".tldrignore"), "excluded.py").unwrap();

        let config = BuildConfig {
            language: "python".to_string(),
            respect_ignore: true,
            ..Default::default()
        };

        let ir = build_project_call_graph_v2(dir.path(), config).unwrap();

        assert!(
            ir.get_file("included.py").is_some(),
            "included.py should be present"
        );
        assert!(
            ir.get_file("excluded.py").is_none(),
            "excluded.py should be ignored"
        );
    }
}