debtmap 0.16.6

Code complexity and technical debt analyzer
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
//! Unit tests for the call graph module

use super::*;
use std::path::{Path, PathBuf};

#[test]
fn test_call_graph_basic() {
    let mut graph = CallGraph::new();
    let (main_id, helper_id) = create_test_functions();

    add_functions_to_graph(&mut graph, &main_id, &helper_id);
    add_call_edge(&mut graph, &main_id, &helper_id);

    verify_basic_graph_properties(&graph, &main_id, &helper_id);
}

fn create_test_functions() -> (FunctionId, FunctionId) {
    let main_id = FunctionId::new(PathBuf::from("main.rs"), "main".to_string(), 1);
    let helper_id = FunctionId::new(PathBuf::from("lib.rs"), "helper".to_string(), 10);
    (main_id, helper_id)
}

fn add_functions_to_graph(graph: &mut CallGraph, main_id: &FunctionId, helper_id: &FunctionId) {
    graph.add_function(main_id.clone(), true, false, 2, 20);
    graph.add_function(helper_id.clone(), false, false, 5, 30);
}

fn add_call_edge(graph: &mut CallGraph, caller: &FunctionId, callee: &FunctionId) {
    graph.add_call(FunctionCall {
        caller: caller.clone(),
        callee: callee.clone(),
        call_type: CallType::Direct,
    });
}

fn verify_basic_graph_properties(graph: &CallGraph, main_id: &FunctionId, helper_id: &FunctionId) {
    assert_eq!(graph.get_callees(main_id).len(), 1);
    assert_eq!(graph.get_callers(helper_id).len(), 1);
    assert!(graph.is_entry_point(main_id));
    assert!(!graph.is_entry_point(helper_id));
}

#[test]
fn test_transitive_dependencies() {
    let mut graph = CallGraph::new();
    let (a, b, c) = create_chain_functions();

    add_chain_to_graph(&mut graph, &a, &b, &c);
    verify_transitive_callees(&graph, &a, &b, &c);
}

fn create_chain_functions() -> (FunctionId, FunctionId, FunctionId) {
    let a = FunctionId::new(PathBuf::from("a.rs"), "a".to_string(), 1);
    let b = FunctionId::new(PathBuf::from("b.rs"), "b".to_string(), 1);
    let c = FunctionId::new(PathBuf::from("c.rs"), "c".to_string(), 1);
    (a, b, c)
}

fn add_chain_to_graph(graph: &mut CallGraph, a: &FunctionId, b: &FunctionId, c: &FunctionId) {
    graph.add_function(a.clone(), true, false, 1, 10);
    graph.add_function(b.clone(), false, false, 2, 20);
    graph.add_function(c.clone(), false, false, 3, 30);

    add_call_edge(graph, a, b);
    add_call_edge(graph, b, c);
}

fn verify_transitive_callees(graph: &CallGraph, a: &FunctionId, b: &FunctionId, c: &FunctionId) {
    let transitive = graph.get_transitive_callees(a, 3);
    assert_eq!(transitive.len(), 2);
    assert!(transitive.contains(b));
    assert!(transitive.contains(c));
}

#[test]
fn test_find_function_at_location() {
    let mut graph = CallGraph::new();
    let file = PathBuf::from("test.rs");
    let functions = create_located_functions(&file);

    add_located_functions(&mut graph, &functions);
    verify_function_location_finding(&graph, &file, &functions);
}

fn create_located_functions(file: &Path) -> Vec<FunctionId> {
    vec![
        FunctionId::new(file.to_path_buf(), "function_one".to_string(), 10),
        FunctionId::new(file.to_path_buf(), "function_two".to_string(), 30),
        FunctionId::new(file.to_path_buf(), "function_three".to_string(), 50),
    ]
}

fn add_located_functions(graph: &mut CallGraph, functions: &[FunctionId]) {
    graph.add_function(functions[0].clone(), false, false, 5, 15);
    graph.add_function(functions[1].clone(), false, false, 3, 10);
    graph.add_function(functions[2].clone(), false, false, 4, 20);
}

fn verify_function_location_finding(graph: &CallGraph, file: &PathBuf, _functions: &[FunctionId]) {
    // Test exact line numbers
    assert_eq!(
        graph
            .find_function_at_location(file, 10)
            .as_ref()
            .map(|f| &f.name),
        Some(&"function_one".to_string())
    );
    assert_eq!(
        graph
            .find_function_at_location(file, 30)
            .as_ref()
            .map(|f| &f.name),
        Some(&"function_two".to_string())
    );

    // Test within range
    assert_eq!(
        graph
            .find_function_at_location(file, 35)
            .as_ref()
            .map(|f| &f.name),
        Some(&"function_two".to_string())
    );

    // Test before any function
    assert_eq!(graph.find_function_at_location(file, 5), None);
}

#[test]
fn test_delegation_detection() {
    let mut graph = CallGraph::new();
    let (orchestrator, workers) = create_delegation_setup();

    setup_delegation_graph(&mut graph, &orchestrator, &workers);
    assert!(graph.detect_delegation_pattern(&orchestrator));
}

fn create_delegation_setup() -> (FunctionId, Vec<FunctionId>) {
    let orchestrator = FunctionId::new(PathBuf::from("orch.rs"), "orchestrate".to_string(), 1);
    let workers = vec![
        FunctionId::new(PathBuf::from("work.rs"), "complex_work1".to_string(), 10),
        FunctionId::new(PathBuf::from("work.rs"), "complex_work2".to_string(), 20),
    ];
    (orchestrator, workers)
}

fn setup_delegation_graph(
    graph: &mut CallGraph,
    orchestrator: &FunctionId,
    workers: &[FunctionId],
) {
    graph.add_function(orchestrator.clone(), false, false, 2, 15);
    graph.add_function(workers[0].clone(), false, false, 10, 50);
    graph.add_function(workers[1].clone(), false, false, 8, 40);

    for worker in workers {
        graph.add_call(FunctionCall {
            caller: orchestrator.clone(),
            callee: worker.clone(),
            call_type: CallType::Delegate,
        });
    }
}

#[test]
fn test_get_transitive_callers_single_level() {
    let mut graph = CallGraph::new();
    let functions = create_caller_test_functions();

    setup_single_level_callers(&mut graph, &functions);
    verify_single_level_callers(&graph, &functions);
}

fn create_caller_test_functions() -> Vec<FunctionId> {
    vec![
        FunctionId::new(PathBuf::from("test.rs"), "func_a".to_string(), 1),
        FunctionId::new(PathBuf::from("test.rs"), "func_b".to_string(), 10),
        FunctionId::new(PathBuf::from("test.rs"), "func_c".to_string(), 20),
    ]
}

fn setup_single_level_callers(graph: &mut CallGraph, funcs: &[FunctionId]) {
    for func in funcs {
        graph.add_function(func.clone(), false, false, 1, 10);
    }

    // a -> b, c -> b (b has two callers)
    add_call_edge(graph, &funcs[0], &funcs[1]);
    add_call_edge(graph, &funcs[2], &funcs[1]);
}

fn verify_single_level_callers(graph: &CallGraph, funcs: &[FunctionId]) {
    let callers = graph.get_transitive_callers(&funcs[1], 1);
    assert_eq!(callers.len(), 2);
    assert!(callers.contains(&funcs[0]));
    assert!(callers.contains(&funcs[2]));
}

#[test]
fn test_get_transitive_callers_multi_level() {
    let mut graph = CallGraph::new();
    let chain = create_caller_chain();

    setup_caller_chain(&mut graph, &chain);
    verify_multi_level_callers(&graph, &chain);
}

fn create_caller_chain() -> Vec<FunctionId> {
    vec![
        FunctionId::new(PathBuf::from("test.rs"), "func_a".to_string(), 1),
        FunctionId::new(PathBuf::from("test.rs"), "func_b".to_string(), 10),
        FunctionId::new(PathBuf::from("test.rs"), "func_c".to_string(), 20),
        FunctionId::new(PathBuf::from("test.rs"), "func_d".to_string(), 30),
    ]
}

fn setup_caller_chain(graph: &mut CallGraph, chain: &[FunctionId]) {
    for func in chain {
        graph.add_function(func.clone(), false, false, 1, 10);
    }

    // a -> b -> c -> d (chain of calls)
    for i in 0..chain.len() - 1 {
        add_call_edge(graph, &chain[i], &chain[i + 1]);
    }
}

fn verify_multi_level_callers(graph: &CallGraph, chain: &[FunctionId]) {
    // Get all transitive callers of d with max_depth 3
    let callers = graph.get_transitive_callers(&chain[3], 3);
    assert_eq!(callers.len(), 3);
    for node in chain.iter().take(3) {
        assert!(callers.contains(node));
    }

    // Test with limited depth
    let callers_depth_1 = graph.get_transitive_callers(&chain[3], 1);
    assert_eq!(callers_depth_1.len(), 1);
    assert!(callers_depth_1.contains(&chain[2]));
}

#[test]
fn test_get_transitive_callers_with_cycles() {
    let mut graph = CallGraph::new();
    let cycle = create_cycle_functions();

    setup_cycle_graph(&mut graph, &cycle);
    verify_cycle_handling(&graph, &cycle);
}

fn create_cycle_functions() -> Vec<FunctionId> {
    vec![
        FunctionId::new(PathBuf::from("test.rs"), "func_a".to_string(), 1),
        FunctionId::new(PathBuf::from("test.rs"), "func_b".to_string(), 10),
        FunctionId::new(PathBuf::from("test.rs"), "func_c".to_string(), 20),
    ]
}

fn setup_cycle_graph(graph: &mut CallGraph, funcs: &[FunctionId]) {
    for func in funcs {
        graph.add_function(func.clone(), false, false, 1, 10);
    }

    // Create a cycle: a -> b -> c -> a
    add_call_edge(graph, &funcs[0], &funcs[1]);
    add_call_edge(graph, &funcs[1], &funcs[2]);
    add_call_edge(graph, &funcs[2], &funcs[0]);
}

fn verify_cycle_handling(graph: &CallGraph, funcs: &[FunctionId]) {
    // Should handle cycles without infinite loop
    let callers = graph.get_transitive_callers(&funcs[0], 10);
    assert_eq!(callers.len(), 2);
    assert!(callers.contains(&funcs[1]));
    assert!(callers.contains(&funcs[2]));
}

#[test]
fn test_get_transitive_callers_no_callers() {
    let mut graph = CallGraph::new();
    let (a, b) = create_simple_pair();

    setup_simple_graph(&mut graph, &a, &b);
    verify_no_callers(&graph, &a);
}

fn create_simple_pair() -> (FunctionId, FunctionId) {
    let a = FunctionId::new(PathBuf::from("test.rs"), "func_a".to_string(), 1);
    let b = FunctionId::new(PathBuf::from("test.rs"), "func_b".to_string(), 10);
    (a, b)
}

fn setup_simple_graph(graph: &mut CallGraph, a: &FunctionId, b: &FunctionId) {
    graph.add_function(a.clone(), false, false, 1, 10);
    graph.add_function(b.clone(), false, false, 1, 10);
    add_call_edge(graph, a, b);
}

fn verify_no_callers(graph: &CallGraph, func: &FunctionId) {
    let callers = graph.get_transitive_callers(func, 5);
    assert_eq!(callers.len(), 0);
}

#[test]
fn test_get_transitive_callers_complex_graph() {
    let mut graph = CallGraph::new();
    let nodes = create_complex_graph_nodes();

    setup_complex_graph(&mut graph, &nodes);
    verify_complex_graph_callers(&graph, &nodes);
}

fn create_complex_graph_nodes() -> Vec<FunctionId> {
    (0..6)
        .map(|i| {
            FunctionId::new(
                PathBuf::from("test.rs"),
                format!("func_{}", (b'a' + i) as char),
                i as usize * 10 + 1,
            )
        })
        .collect()
}

fn setup_complex_graph(graph: &mut CallGraph, nodes: &[FunctionId]) {
    for node in nodes {
        graph.add_function(node.clone(), false, false, 1, 10);
    }

    // Create complex structure:
    //      a
    //     / \
    //    b   c
    //    |\ /|
    //    | X |
    //    |/ \|
    //    d   e
    //     \ /
    //      f
    add_call_edge(graph, &nodes[0], &nodes[1]); // a -> b
    add_call_edge(graph, &nodes[0], &nodes[2]); // a -> c
    add_call_edge(graph, &nodes[1], &nodes[3]); // b -> d
    add_call_edge(graph, &nodes[1], &nodes[4]); // b -> e
    add_call_edge(graph, &nodes[2], &nodes[3]); // c -> d
    add_call_edge(graph, &nodes[2], &nodes[4]); // c -> e
    add_call_edge(graph, &nodes[3], &nodes[5]); // d -> f
    add_call_edge(graph, &nodes[4], &nodes[5]); // e -> f
}

fn verify_complex_graph_callers(graph: &CallGraph, nodes: &[FunctionId]) {
    // Test transitive callers of f
    let callers_f = graph.get_transitive_callers(&nodes[5], 10);
    assert_eq!(callers_f.len(), 5); // All except f itself
    for node in nodes.iter().take(5) {
        assert!(callers_f.contains(node));
    }

    // Test with limited depth
    let callers_f_depth_2 = graph.get_transitive_callers(&nodes[5], 2);
    assert_eq!(callers_f_depth_2.len(), 4); // d, e, b, c
}

#[test]
fn test_call_graph_serialization_roundtrip() {
    let mut graph = CallGraph::new();
    let (func1, func2) = create_serialization_test_functions();

    setup_serialization_graph(&mut graph, &func1, &func2);
    verify_serialization_roundtrip(&graph, &func1, &func2);
}

fn create_serialization_test_functions() -> (FunctionId, FunctionId) {
    let func1 = FunctionId::new(PathBuf::from("src/main.rs"), "main".to_string(), 10);
    let func2 = FunctionId::new(PathBuf::from("src/lib.rs"), "helper".to_string(), 25);
    (func1, func2)
}

fn setup_serialization_graph(graph: &mut CallGraph, func1: &FunctionId, func2: &FunctionId) {
    graph.add_function(func1.clone(), true, false, 5, 50);
    graph.add_function(func2.clone(), false, false, 3, 30);
    add_call_edge(graph, func1, func2);
}

fn verify_serialization_roundtrip(graph: &CallGraph, func1: &FunctionId, func2: &FunctionId) {
    let json = serde_json::to_string(graph).unwrap();
    let deserialized: CallGraph = serde_json::from_str(&json).unwrap();

    assert_eq!(deserialized.get_callees(func1).len(), 1);
    assert_eq!(deserialized.get_callers(func2).len(), 1);
    assert!(deserialized.is_entry_point(func1));
    assert!(!deserialized.is_entry_point(func2));
}

#[test]
fn test_is_cross_file_call_match() {
    verify_exact_match();
    verify_associated_function_match();
    verify_method_with_type_context();
    verify_suffix_matching();
    verify_base_name_extraction();
    verify_non_matches();
}

fn verify_exact_match() {
    assert!(CallGraph::is_cross_file_call_match(
        "my_function",
        "my_function",
        None
    ));
}

fn verify_associated_function_match() {
    assert!(CallGraph::is_cross_file_call_match(
        "ContextualRisk::new",
        "ContextualRisk::new",
        None
    ));
}

fn verify_method_with_type_context() {
    assert!(CallGraph::is_cross_file_call_match(
        "MyStruct::method",
        "method",
        Some("MyStruct")
    ));
}

fn verify_suffix_matching() {
    assert!(CallGraph::is_cross_file_call_match(
        "module::MyStruct::method",
        "MyStruct::method",
        None
    ));
}

fn verify_base_name_extraction() {
    assert!(CallGraph::is_cross_file_call_match(
        "MyStruct::new",
        "new",
        None
    ));
}

fn verify_non_matches() {
    assert!(!CallGraph::is_cross_file_call_match(
        "different_function",
        "my_function",
        None
    ));
    assert!(!CallGraph::is_cross_file_call_match(
        "MyStruct::method",
        "other_method",
        None
    ));
}

#[test]
fn test_select_best_cross_file_match() {
    verify_single_candidate_selection();
    verify_exact_match_preference();
    verify_qualification_preference();
}

fn verify_single_candidate_selection() {
    let caller_file = PathBuf::from("src/caller.rs");
    let other_file = PathBuf::from("src/other.rs");

    let single_candidate = vec![FunctionId::new(
        other_file.clone(),
        "test_func".to_string(),
        10,
    )];

    let result = CallGraph::select_best_cross_file_match(
        single_candidate.clone(),
        &caller_file,
        "test_func",
    );
    assert!(result.is_some());
    assert_eq!(result.unwrap().name, "test_func");
}

fn verify_exact_match_preference() {
    let caller_file = PathBuf::from("src/caller.rs");
    let other_file = PathBuf::from("src/other.rs");
    let third_file = PathBuf::from("src/third.rs");

    let candidates = vec![
        FunctionId::new(other_file.clone(), "test_func".to_string(), 10),
        FunctionId::new(third_file.clone(), "MyStruct::test_func".to_string(), 20),
    ];

    let result = CallGraph::select_best_cross_file_match(candidates, &caller_file, "test_func");
    assert!(result.is_some());
    assert_eq!(result.unwrap().name, "test_func");
}

fn verify_qualification_preference() {
    let caller_file = PathBuf::from("src/caller.rs");
    let other_file = PathBuf::from("src/other.rs");
    let third_file = PathBuf::from("src/third.rs");

    let candidates = vec![
        FunctionId::new(
            other_file.clone(),
            "deep::module::MyStruct::method".to_string(),
            10,
        ),
        FunctionId::new(third_file.clone(), "MyStruct::method".to_string(), 20),
    ];

    let result = CallGraph::select_best_cross_file_match(candidates, &caller_file, "method");
    assert!(result.is_some());
    assert_eq!(result.unwrap().name, "MyStruct::method");
}

#[test]
fn test_is_test_helper_detection() {
    let mut graph = CallGraph::new();
    let functions = create_test_helper_setup();

    setup_test_helper_graph(&mut graph, &functions);
    verify_test_helper_detection(&graph, &functions);
}

fn create_test_helper_setup() -> Vec<FunctionId> {
    vec![
        FunctionId::new(
            PathBuf::from("tests/test.rs"),
            "test_something".to_string(),
            10,
        ),
        FunctionId::new(
            PathBuf::from("tests/test.rs"),
            "test_another".to_string(),
            30,
        ),
        FunctionId::new(
            PathBuf::from("src/lib.rs"),
            "validate_initial_state".to_string(),
            100,
        ),
        FunctionId::new(PathBuf::from("src/lib.rs"), "process_data".to_string(), 200),
        FunctionId::new(PathBuf::from("src/main.rs"), "main".to_string(), 1),
    ]
}

fn setup_test_helper_graph(graph: &mut CallGraph, funcs: &[FunctionId]) {
    // Add functions to graph
    graph.add_function(funcs[0].clone(), false, true, 3, 20); // test_something
    graph.add_function(funcs[1].clone(), false, true, 4, 25); // test_another
    graph.add_function(funcs[2].clone(), false, false, 5, 30); // validate_initial_state
    graph.add_function(funcs[3].clone(), false, false, 6, 40); // process_data
    graph.add_function(funcs[4].clone(), true, false, 2, 15); // main

    // Test functions call the helper
    add_call_edge(graph, &funcs[0], &funcs[2]);
    add_call_edge(graph, &funcs[1], &funcs[2]);

    // Main calls regular_func
    add_call_edge(graph, &funcs[4], &funcs[3]);
}

fn verify_test_helper_detection(graph: &CallGraph, funcs: &[FunctionId]) {
    assert!(
        graph.is_test_helper(&funcs[2]),
        "validate_initial_state should be identified as a test helper"
    );
    assert!(
        !graph.is_test_helper(&funcs[3]),
        "process_data should not be identified as a test helper"
    );
    assert!(
        !graph.is_test_helper(&funcs[0]),
        "Test functions should not be identified as test helpers"
    );
}