kimun 0.20.0

Code metrics tool — health score, complexity, duplication, hotspots, ownership
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
/// Dependency graph builder and analyzer.
///
/// Builds a directed graph of internal file dependencies, computes per-file
/// fan-in (importers) and fan-out (imports), and detects cycles using
/// Tarjan's strongly-connected components (SCC) algorithm.
use std::collections::HashMap;
use std::path::{Path, PathBuf};

use serde::Serialize;

/// A single file's dependency metrics.
pub struct DepEntry {
    pub path: PathBuf,
    pub language: String,
    /// Number of project files that import this file.
    pub fan_in: usize,
    /// Number of project files this file imports.
    pub fan_out: usize,
    /// True if this file participates in a dependency cycle.
    pub in_cycle: bool,
}

/// Full dependency analysis result.
pub struct DepResult {
    pub entries: Vec<DepEntry>,
    /// Each inner Vec is a set of files forming a cycle (SCC size > 1).
    pub cycles: Vec<Vec<PathBuf>>,
}

/// Build the dependency graph from raw edges and compute metrics.
///
/// `edges` maps each file to the list of files it imports.
/// Only files present as keys in `edges` are included as nodes (even if they
/// have no outgoing edges, they must be keyed with an empty vec).
pub fn build_graph(
    files: &[(PathBuf, String)], // (path, language) for all project files
    edges: &HashMap<PathBuf, Vec<PathBuf>>,
) -> DepResult {
    // Index files: path → index
    let index: HashMap<&PathBuf, usize> =
        files.iter().enumerate().map(|(i, (p, _))| (p, i)).collect();

    let n = files.len();

    // Build adjacency list using indices
    let mut adj: Vec<Vec<usize>> = vec![vec![]; n];
    for (src, dsts) in edges {
        if let Some(&si) = index.get(src) {
            for dst in dsts {
                if let Some(&di) = index.get(dst) {
                    adj[si].push(di);
                }
            }
        }
    }

    // Fan-out: number of outgoing edges per node
    let fan_out: Vec<usize> = adj.iter().map(|v| v.len()).collect();

    // Fan-in: number of incoming edges per node
    let mut fan_in = vec![0usize; n];
    for neighbors in &adj {
        for &dst in neighbors {
            fan_in[dst] += 1;
        }
    }

    // Tarjan's SCC to find cycles
    let sccs = tarjan_scc(n, &adj);

    // Mark nodes that are part of a non-trivial SCC (size > 1)
    let mut in_cycle = vec![false; n];
    let mut cycles: Vec<Vec<PathBuf>> = Vec::new();
    for scc in &sccs {
        if scc.len() > 1 {
            for &i in scc {
                in_cycle[i] = true;
            }
            let mut cycle_paths: Vec<PathBuf> = scc.iter().map(|&i| files[i].0.clone()).collect();
            cycle_paths.sort();
            cycles.push(cycle_paths);
        }
    }
    cycles.sort();

    let entries = files
        .iter()
        .enumerate()
        .map(|(i, (path, lang))| DepEntry {
            path: path.clone(),
            language: lang.clone(),
            fan_in: fan_in[i],
            fan_out: fan_out[i],
            in_cycle: in_cycle[i],
        })
        .collect();

    DepResult { entries, cycles }
}

/// Tarjan's strongly-connected components algorithm (iterative to avoid stack overflow).
fn tarjan_scc(n: usize, adj: &[Vec<usize>]) -> Vec<Vec<usize>> {
    let mut index_counter = 0usize;
    let mut stack: Vec<usize> = Vec::new();
    let mut on_stack = vec![false; n];
    let mut index: Vec<Option<usize>> = vec![None; n];
    let mut lowlink = vec![0usize; n];
    let mut sccs: Vec<Vec<usize>> = Vec::new();

    // Iterative DFS using an explicit work stack.
    // Each entry: (node, iterator position in adj[node])
    let mut work: Vec<(usize, usize)> = Vec::new();

    for start in 0..n {
        if index[start].is_some() {
            continue;
        }

        work.push((start, 0));

        while let Some((v, ei)) = work.last_mut() {
            let v = *v;
            if index[v].is_none() {
                // First visit
                index[v] = Some(index_counter);
                lowlink[v] = index_counter;
                index_counter += 1;
                stack.push(v);
                on_stack[v] = true;
            }

            let neighbors = &adj[v];
            if *ei < neighbors.len() {
                let w = neighbors[*ei];
                *ei += 1;
                if index[w].is_none() {
                    work.push((w, 0));
                } else if on_stack[w] {
                    lowlink[v] = lowlink[v].min(index[w].unwrap());
                }
            } else {
                // Done with v's neighbors — pop and update parent's lowlink
                work.pop();
                if let Some((parent, _)) = work.last() {
                    let parent = *parent;
                    lowlink[parent] = lowlink[parent].min(lowlink[v]);
                }
                // Check if v is an SCC root
                if lowlink[v] == index[v].unwrap() {
                    let mut scc = Vec::new();
                    loop {
                        let w = stack.pop().unwrap();
                        on_stack[w] = false;
                        scc.push(w);
                        if w == v {
                            break;
                        }
                    }
                    sccs.push(scc);
                }
            }
        }
    }
    sccs
}

/// JSON-serializable dependency entry.
#[derive(Serialize)]
pub struct JsonDepEntry {
    pub path: String,
    pub language: String,
    pub fan_in: usize,
    pub fan_out: usize,
    pub in_cycle: bool,
}

/// JSON-serializable full result.
#[derive(Serialize)]
pub struct JsonDepResult {
    pub files: Vec<JsonDepEntry>,
    pub cycles: Vec<Vec<String>>,
    pub cycle_count: usize,
}

impl From<&DepResult> for JsonDepResult {
    fn from(r: &DepResult) -> Self {
        JsonDepResult {
            files: r
                .entries
                .iter()
                .map(|e| JsonDepEntry {
                    path: e.path.display().to_string(),
                    language: e.language.clone(),
                    fan_in: e.fan_in,
                    fan_out: e.fan_out,
                    in_cycle: e.in_cycle,
                })
                .collect(),
            cycles: r
                .cycles
                .iter()
                .map(|c| c.iter().map(|p| p.display().to_string()).collect())
                .collect(),
            cycle_count: r.cycles.len(),
        }
    }
}

/// Resolve a raw import string to a project-relative path, given the importer's location.
/// Returns `None` if the import cannot be resolved to a known project file.
pub fn resolve_import(
    importer: &Path,  // project-relative path of the importing file
    import_str: &str, // raw import string from extractor
    language: &str,
    file_set: &std::collections::HashSet<PathBuf>,
    go_module: Option<&str>,
) -> Option<PathBuf> {
    let dir = importer.parent().unwrap_or(Path::new(""));
    match language {
        "Rust" => resolve_rust(dir, import_str, file_set),
        "Python" => resolve_python(dir, import_str, file_set),
        "JavaScript" | "TypeScript" | "JSX" | "TSX" => resolve_js(dir, import_str, file_set),
        "Go" => resolve_go(import_str, go_module, file_set),
        _ => None,
    }
}

fn resolve_rust(
    dir: &Path,
    name: &str,
    file_set: &std::collections::HashSet<PathBuf>,
) -> Option<PathBuf> {
    // `mod foo;` → foo.rs or foo/mod.rs relative to current directory
    let as_file = dir.join(format!("{name}.rs"));
    if file_set.contains(&as_file) {
        return Some(as_file);
    }
    let as_mod = dir.join(name).join("mod.rs");
    if file_set.contains(&as_mod) {
        return Some(as_mod);
    }
    // lib.rs files can also declare submodules as name/lib.rs (rare but possible)
    None
}

fn resolve_python(
    dir: &Path,
    import_str: &str,
    file_set: &std::collections::HashSet<PathBuf>,
) -> Option<PathBuf> {
    // Count leading dots for relative level
    let dots = import_str.chars().take_while(|c| *c == '.').count();
    let module = &import_str[dots..]; // module name after dots

    // Navigate up `dots - 1` directories from current dir
    let mut base = dir.to_path_buf();
    for _ in 1..dots {
        base = base.parent().unwrap_or(Path::new("")).to_path_buf();
    }

    if module.is_empty() {
        return None;
    }

    // Convert dotted module to path: foo.bar → foo/bar
    let rel = module.replace('.', "/");
    let as_file = base.join(format!("{rel}.py"));
    if file_set.contains(&as_file) {
        return Some(as_file);
    }
    let as_init = base.join(&rel).join("__init__.py");
    if file_set.contains(&as_init) {
        return Some(as_init);
    }
    None
}

fn resolve_js(
    dir: &Path,
    import_str: &str,
    file_set: &std::collections::HashSet<PathBuf>,
) -> Option<PathBuf> {
    let base = dir.join(import_str);
    // If already has an extension, try directly
    if base.extension().is_some() {
        let p = normalize_path(&base);
        if file_set.contains(&p) {
            return Some(p);
        }
    }
    // Try common JS/TS extensions
    for ext in &["ts", "tsx", "js", "jsx", "mts", "mjs"] {
        let p = normalize_path(&base.with_extension(ext));
        if file_set.contains(&p) {
            return Some(p);
        }
    }
    // Try index file
    for ext in &["ts", "tsx", "js", "jsx"] {
        let p = normalize_path(&base.join(format!("index.{ext}")));
        if file_set.contains(&p) {
            return Some(p);
        }
    }
    None
}

fn resolve_go(
    import_str: &str,
    go_module: Option<&str>,
    file_set: &std::collections::HashSet<PathBuf>,
) -> Option<PathBuf> {
    let module = go_module?;
    let rel = import_str.strip_prefix(module)?.trim_start_matches('/');
    if rel.is_empty() {
        return None;
    }
    // Find any .go file in that directory
    let dir = PathBuf::from(rel);
    file_set
        .iter()
        .find(|p| p.starts_with(&dir) && p.extension().is_some_and(|e| e == "go"))
        .cloned()
}

/// Normalize a path by resolving `..` components (without touching the filesystem).
fn normalize_path(path: &Path) -> PathBuf {
    let mut components = Vec::new();
    for comp in path.components() {
        match comp {
            std::path::Component::ParentDir => {
                components.pop();
            }
            std::path::Component::CurDir => {}
            other => components.push(other),
        }
    }
    components.iter().collect()
}

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

    fn paths(items: &[&str]) -> Vec<(PathBuf, String)> {
        items
            .iter()
            .map(|s| (PathBuf::from(s), "Rust".to_string()))
            .collect()
    }

    fn edges_map(pairs: &[(&str, &[&str])]) -> HashMap<PathBuf, Vec<PathBuf>> {
        pairs
            .iter()
            .map(|(k, vs)| {
                (
                    PathBuf::from(k),
                    vs.iter().map(|v| PathBuf::from(v)).collect(),
                )
            })
            .collect()
    }

    #[test]
    fn fan_in_fan_out() {
        // a → b → c  (linear chain)
        let files = paths(&["a.rs", "b.rs", "c.rs"]);
        let edges = edges_map(&[("a.rs", &["b.rs"]), ("b.rs", &["c.rs"]), ("c.rs", &[])]);
        let result = build_graph(&files, &edges);
        let by_path: HashMap<_, _> = result
            .entries
            .iter()
            .map(|e| (e.path.as_path(), e))
            .collect();
        assert_eq!(by_path[Path::new("a.rs")].fan_out, 1);
        assert_eq!(by_path[Path::new("a.rs")].fan_in, 0);
        assert_eq!(by_path[Path::new("b.rs")].fan_in, 1);
        assert_eq!(by_path[Path::new("b.rs")].fan_out, 1);
        assert_eq!(by_path[Path::new("c.rs")].fan_in, 1);
        assert_eq!(by_path[Path::new("c.rs")].fan_out, 0);
    }

    #[test]
    fn no_cycles_in_linear_chain() {
        let files = paths(&["a.rs", "b.rs", "c.rs"]);
        let edges = edges_map(&[("a.rs", &["b.rs"]), ("b.rs", &["c.rs"]), ("c.rs", &[])]);
        let result = build_graph(&files, &edges);
        assert!(result.cycles.is_empty());
        assert!(result.entries.iter().all(|e| !e.in_cycle));
    }

    #[test]
    fn detects_simple_cycle() {
        // a → b → a
        let files = paths(&["a.rs", "b.rs"]);
        let edges = edges_map(&[("a.rs", &["b.rs"]), ("b.rs", &["a.rs"])]);
        let result = build_graph(&files, &edges);
        assert_eq!(result.cycles.len(), 1);
        assert_eq!(result.cycles[0].len(), 2);
        assert!(result.entries.iter().all(|e| e.in_cycle));
    }

    #[test]
    fn self_loop_is_not_a_cycle() {
        // a → a (self-loop) — Tarjan SCC of size 1, not reported as cycle
        let files = paths(&["a.rs"]);
        let edges = edges_map(&[("a.rs", &["a.rs"])]);
        let result = build_graph(&files, &edges);
        assert!(result.cycles.is_empty());
    }

    #[test]
    fn three_node_cycle() {
        // a → b → c → a
        let files = paths(&["a.rs", "b.rs", "c.rs"]);
        let edges = edges_map(&[
            ("a.rs", &["b.rs"]),
            ("b.rs", &["c.rs"]),
            ("c.rs", &["a.rs"]),
        ]);
        let result = build_graph(&files, &edges);
        assert_eq!(result.cycles.len(), 1);
        assert_eq!(result.cycles[0].len(), 3);
    }

    #[test]
    fn resolve_rust_file() {
        let mut file_set = std::collections::HashSet::new();
        file_set.insert(PathBuf::from("src/foo.rs"));
        let result = resolve_import(Path::new("src/main.rs"), "foo", "Rust", &file_set, None);
        assert_eq!(result, Some(PathBuf::from("src/foo.rs")));
    }

    #[test]
    fn resolve_rust_mod_dir() {
        let mut file_set = std::collections::HashSet::new();
        file_set.insert(PathBuf::from("src/bar/mod.rs"));
        let result = resolve_import(Path::new("src/main.rs"), "bar", "Rust", &file_set, None);
        assert_eq!(result, Some(PathBuf::from("src/bar/mod.rs")));
    }

    #[test]
    fn resolve_js_relative() {
        let mut file_set = std::collections::HashSet::new();
        file_set.insert(PathBuf::from("src/utils.ts"));
        let result = resolve_import(
            Path::new("src/components/App.tsx"),
            "../utils",
            "TypeScript",
            &file_set,
            None,
        );
        assert_eq!(result, Some(PathBuf::from("src/utils.ts")));
    }

    #[test]
    fn normalize_dotdot() {
        let p = normalize_path(Path::new("src/foo/../bar.rs"));
        assert_eq!(p, PathBuf::from("src/bar.rs"));
    }

    // ── Python resolution ──────────────────────────────────────────────────

    #[test]
    fn resolve_python_relative_as_file() {
        let mut file_set = std::collections::HashSet::new();
        file_set.insert(PathBuf::from("src/utils.py"));
        let result = resolve_import(Path::new("src/main.py"), ".utils", "Python", &file_set, None);
        assert_eq!(result, Some(PathBuf::from("src/utils.py")));
    }

    #[test]
    fn resolve_python_relative_as_package() {
        let mut file_set = std::collections::HashSet::new();
        file_set.insert(PathBuf::from("src/utils/__init__.py"));
        let result = resolve_import(Path::new("src/main.py"), ".utils", "Python", &file_set, None);
        assert_eq!(result, Some(PathBuf::from("src/utils/__init__.py")));
    }

    #[test]
    fn resolve_python_double_dot_goes_up() {
        let mut file_set = std::collections::HashSet::new();
        file_set.insert(PathBuf::from("common.py"));
        // From src/sub/main.py, ..common → src/common.py? No, two dots goes up two levels
        // importer: src/sub/main.py → dir = src/sub
        // dots=2 → base goes up 1 level (for _ in 1..2) → base = src
        // module = "common" → src/common.py
        file_set.insert(PathBuf::from("src/common.py"));
        let result = resolve_import(
            Path::new("src/sub/main.py"),
            "..common",
            "Python",
            &file_set,
            None,
        );
        assert_eq!(result, Some(PathBuf::from("src/common.py")));
    }

    #[test]
    fn resolve_python_dotted_module_path() {
        let mut file_set = std::collections::HashSet::new();
        file_set.insert(PathBuf::from("src/foo/bar.py"));
        let result =
            resolve_import(Path::new("src/main.py"), ".foo.bar", "Python", &file_set, None);
        assert_eq!(result, Some(PathBuf::from("src/foo/bar.py")));
    }

    #[test]
    fn resolve_python_module_only_dots_returns_none() {
        // Single dot with no module name → skip
        let file_set = std::collections::HashSet::new();
        let result = resolve_import(Path::new("src/main.py"), ".", "Python", &file_set, None);
        assert!(result.is_none());
    }

    #[test]
    fn resolve_python_not_found_returns_none() {
        let file_set = std::collections::HashSet::new();
        let result =
            resolve_import(Path::new("src/main.py"), ".missing", "Python", &file_set, None);
        assert!(result.is_none());
    }

    // ── JavaScript/TypeScript resolution ──────────────────────────────────

    #[test]
    fn resolve_js_direct_extension() {
        let mut file_set = std::collections::HashSet::new();
        file_set.insert(PathBuf::from("src/utils.js"));
        let result = resolve_import(
            Path::new("src/app.js"),
            "./utils.js",
            "JavaScript",
            &file_set,
            None,
        );
        assert_eq!(result, Some(PathBuf::from("src/utils.js")));
    }

    #[test]
    fn resolve_js_without_extension_ts() {
        let mut file_set = std::collections::HashSet::new();
        file_set.insert(PathBuf::from("src/utils.ts"));
        let result = resolve_import(
            Path::new("src/app.ts"),
            "./utils",
            "TypeScript",
            &file_set,
            None,
        );
        assert_eq!(result, Some(PathBuf::from("src/utils.ts")));
    }

    #[test]
    fn resolve_js_index_file() {
        let mut file_set = std::collections::HashSet::new();
        file_set.insert(PathBuf::from("src/components/index.tsx"));
        let result = resolve_import(
            Path::new("src/app.tsx"),
            "./components",
            "TSX",
            &file_set,
            None,
        );
        assert_eq!(result, Some(PathBuf::from("src/components/index.tsx")));
    }

    #[test]
    fn resolve_js_jsx_extension() {
        let mut file_set = std::collections::HashSet::new();
        file_set.insert(PathBuf::from("src/Button.jsx"));
        let result =
            resolve_import(Path::new("src/App.jsx"), "./Button", "JSX", &file_set, None);
        assert_eq!(result, Some(PathBuf::from("src/Button.jsx")));
    }

    #[test]
    fn resolve_js_not_found_returns_none() {
        let file_set = std::collections::HashSet::new();
        let result =
            resolve_import(Path::new("src/app.ts"), "./missing", "TypeScript", &file_set, None);
        assert!(result.is_none());
    }

    #[test]
    fn resolve_js_direct_extension_not_found_returns_none() {
        // Has extension but file doesn't exist in file_set
        let file_set = std::collections::HashSet::new();
        let result = resolve_import(
            Path::new("src/app.ts"),
            "./nonexistent.ts",
            "TypeScript",
            &file_set,
            None,
        );
        assert!(result.is_none());
    }

    // ── Go resolution ──────────────────────────────────────────────────────

    #[test]
    fn resolve_go_with_module() {
        let mut file_set = std::collections::HashSet::new();
        file_set.insert(PathBuf::from("pkg/foo/main.go"));
        let result = resolve_import(
            Path::new("main.go"),
            "github.com/user/proj/pkg/foo",
            "Go",
            &file_set,
            Some("github.com/user/proj"),
        );
        assert_eq!(result, Some(PathBuf::from("pkg/foo/main.go")));
    }

    #[test]
    fn resolve_go_no_module_returns_none() {
        let file_set = std::collections::HashSet::new();
        let result = resolve_import(
            Path::new("main.go"),
            "github.com/user/proj/pkg/foo",
            "Go",
            &file_set,
            None, // no go_module
        );
        assert!(result.is_none());
    }

    #[test]
    fn resolve_go_external_package_returns_none() {
        // Import doesn't start with module prefix
        let file_set = std::collections::HashSet::new();
        let result = resolve_import(
            Path::new("main.go"),
            "fmt",
            "Go",
            &file_set,
            Some("github.com/user/proj"),
        );
        assert!(result.is_none());
    }

    #[test]
    fn resolve_go_root_module_path_returns_none() {
        // Strip prefix leaves empty string
        let file_set = std::collections::HashSet::new();
        let result = resolve_import(
            Path::new("main.go"),
            "github.com/user/proj",
            "Go",
            &file_set,
            Some("github.com/user/proj"),
        );
        assert!(result.is_none());
    }

    #[test]
    fn resolve_unknown_language_returns_none() {
        let file_set = std::collections::HashSet::new();
        let result = resolve_import(Path::new("main.sh"), "utils", "Bash", &file_set, None);
        assert!(result.is_none());
    }

    // ── normalize_path ─────────────────────────────────────────────────────

    #[test]
    fn normalize_cur_dir_component() {
        let p = normalize_path(Path::new("./src/foo.rs"));
        assert_eq!(p, PathBuf::from("src/foo.rs"));
    }

    #[test]
    fn normalize_already_clean() {
        let p = normalize_path(Path::new("src/foo.rs"));
        assert_eq!(p, PathBuf::from("src/foo.rs"));
    }

    // ── JsonDepResult conversion ───────────────────────────────────────────

    #[test]
    fn json_dep_result_conversion() {
        let files = paths(&["a.rs", "b.rs"]);
        let edges = edges_map(&[("a.rs", &["b.rs"]), ("b.rs", &[])]);
        let result = build_graph(&files, &edges);
        let json: JsonDepResult = (&result).into();
        assert_eq!(json.files.len(), 2);
        assert_eq!(json.cycles.len(), 0);
        assert_eq!(json.cycle_count, 0);
    }

    #[test]
    fn json_dep_result_with_cycle() {
        let files = paths(&["a.rs", "b.rs"]);
        let edges = edges_map(&[("a.rs", &["b.rs"]), ("b.rs", &["a.rs"])]);
        let result = build_graph(&files, &edges);
        let json: JsonDepResult = (&result).into();
        assert_eq!(json.cycle_count, 1);
        assert_eq!(json.cycles.len(), 1);
        assert!(json.files.iter().all(|f| f.in_cycle));
    }
}