devscout-rs 0.9.0

Fast code index for C# and TypeScript codebases -- map, find, refs, impact from the CLI.
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
use super::*;

#[test]
fn stage5_schema_every_uses_member_edge_carries_its_member_and_only_heuristic_edges_carry_a_tier() {
    let files = fragments_for(THREE_TIER_FIXTURE);
    let g = resolve_graph(&no_git_root(), &files);

    let member_edges: Vec<&Edge> = g
        .edges
        .iter()
        .filter(|e| matches!(e, Edge::UsesMember { .. }))
        .collect();
    for e in &member_edges {
        let Edge::UsesMember {
            heuristic,
            tier,
            member,
            ..
        } = e
        else {
            unreachable!()
        };
        assert!(
            member.is_some(),
            "every uses-member edge names its member, precise ones included: {e:?}"
        );
        assert_eq!(
            *heuristic,
            tier.is_some(),
            "the flag and the tier are one fact -- `Edge::uses_member` derives one from the other: {e:?}"
        );
    }

    let rows: Vec<(&str, Option<HeuristicTier>, Option<&str>)> = member_edges
        .iter()
        .map(|e| match e {
            Edge::UsesMember {
                to, tier, member, ..
            } => (to.as_str(), *tier, member.as_deref()),
            _ => unreachable!(),
        })
        .collect();
    assert_eq!(
        rows,
        vec![
            ("App.Core.Widget", None, Some("Render")),
            (
                "App.Ext.WidgetExtensions",
                Some(HeuristicTier::Ext),
                Some("Tally")
            ),
            ("App.Alpha.Config", Some(HeuristicTier::Guess), Some("Load")),
            ("App.Beta.Config", Some(HeuristicTier::Guess), Some("Load")),
        ]
    );

    // One serialized sample per tier, pinned: the precise row gains
    // `member` and nothing else, and the two guess rows spell their tier
    // between the flag and the member.
    let bytes: Vec<String> = member_edges
        .iter()
        .map(|e| serde_json::to_string(e).unwrap())
        .collect();
    assert_eq!(
        bytes[0],
        r#"{"kind":"uses-member","from_file":"Consumers/Consumer.cs","from_line":15,"to":"App.Core.Widget","to_file":"Core/Widget.cs","member":"Render"}"#
    );
    assert_eq!(
        bytes[1],
        r#"{"kind":"uses-member","from_file":"Consumers/Consumer.cs","from_line":16,"to":"App.Ext.WidgetExtensions","to_file":"Ext/WidgetExtensions.cs","heuristic":true,"tier":"ext","member":"Tally"}"#
    );
    assert_eq!(
        bytes[2],
        r#"{"kind":"uses-member","from_file":"Consumers/Consumer.cs","from_line":17,"to":"App.Alpha.Config","to_file":"Alpha/Config.cs","heuristic":true,"tier":"guess","member":"Load"}"#
    );

    // And the counters partition the guesses: every heuristic edge is in
    // exactly one tier, so the two add up to the total.
    assert_eq!(
        g.stats.heuristic_by_tier,
        HeuristicByTier { ext: 1, guess: 2 }
    );
    assert_eq!(
        g.stats.heuristic_by_tier.ext + g.stats.heuristic_by_tier.guess,
        g.stats.heuristic_edge_count
    );
    assert_eq!(
        g.schema_version, GRAPH_SCHEMA_VERSION,
        "a graph carrying tier and member is a schema-2 graph"
    );
}

#[test]
fn stage4_stats_heuristic_edge_count_is_appended_last_and_edges_by_kind_never_counts_a_guess() {
    let files = fragments_for(BYTE_IDENTITY_FIXTURE);
    let g = resolve_graph(&no_git_root(), &files);

    // Whole-object bytes rather than a key list: this pins the ORDER the
    // serialized `stats` keys appear in, and the values with them.
    assert_eq!(
        serde_json::to_string(&g.stats).unwrap(),
        r#"{"def_count":9,"file_count":7,"edges_by_kind":{"inherits":1,"uses-type":1,"imports":4,"uses-member":2,"ctor-di":0,"implements":0,"overrides":0},"ambiguous_count":0,"ambiguous_pct":0,"unresolved_external_count":0,"heuristic_edge_count":3,"test_def_count":0,"heuristic_by_tier":{"ext":0,"guess":3}}"#,
        "heuristic_by_tier is appended LAST, after test_def_count -- the stats key order graph.json pins"
    );
    assert_eq!(g.stats.heuristic_edge_count, 3);
    assert_eq!(
        g.stats.edges_by_kind.uses_member, 2,
        "the two precise member edges only -- three heuristic ones landed in the same array and moved this number by zero"
    );
    assert_eq!(g.stats.edges_by_kind.inherits, 1);
    assert_eq!(g.stats.edges_by_kind.uses_type, 1);
    assert_eq!(g.stats.edges_by_kind.imports, 4);
    assert_eq!(
        g.stats.ambiguous_count, 0,
        "ambiguous_count semantics are untouched by this stage"
    );
}

// --- partial classes: also_in accumulation + method union -------------

#[test]
fn partial_class_across_files_merges_into_also_in_with_method_union() {
    let files = vec![
        (
            "A/Product.cs".to_string(),
            frag(
                vec![FragDef {
                    line: 3,
                    ..def_with(
                        "A.Product",
                        "Product",
                        "A",
                        "class",
                        &["Describe"],
                        &["Name"],
                        &["_cache"],
                    )
                }],
                vec![],
                vec![],
            ),
        ),
        (
            "A/Product.Extra.cs".to_string(),
            frag(
                vec![FragDef {
                    line: 3,
                    ..def_with(
                        "A.Product",
                        "Product",
                        "A",
                        "class",
                        &["Refresh"],
                        &["Sku"],
                        &["_extra"],
                    )
                }],
                vec![],
                vec![],
            ),
        ),
    ];
    let g = resolve_graph(&no_git_root(), &files);
    assert_eq!(g.defs.len(), 1, "one def entry, not two");
    let d = &g.defs[0];
    assert_eq!(
        d.file, "A/Product.cs",
        "first-insertion file wins as the primary site"
    );
    assert_eq!(
        d.methods,
        vec!["Describe".to_string(), "Refresh".to_string()],
        "methods union in encounter order"
    );
    assert_eq!(
        d.also_in,
        vec![AlsoIn {
            file: "A/Product.Extra.cs".into(),
            line: 3
        }]
    );
}

// --- resolution ladder: enclosing-namespace walks at steps 2 and 3 ------

#[test]
fn ladder_step2_a_using_directive_is_itself_read_against_the_enclosing_namespaces() {
    let files = fragments_for(&[
        ("A/Configuration/Setting.cs", "namespace A.Configuration { public class Setting { } }"),
        // The collision partner: without it a bare "Setting" would resolve
        // at step 4 (globally unique simple name) and this test would pass
        // on a ladder that never walked anything. With it, step 4 can only
        // report ambiguous.
        ("Other/Setting.cs", "namespace Other { public class Setting { } }"),
        ("A/B/C/Holder.cs", "\nusing Configuration;\n\nnamespace A.B.C;\n\npublic class Holder\n{\n  private Setting _setting;\n}\n"),
    ]);
    let g = resolve_graph(&no_git_root(), &files);
    assert_eq!(
        type_edge_targets_from(&g, "A/B/C/Holder.cs"),
        vec!["A.Configuration.Setting"],
        "`using Configuration;` inside A.B.C reaches A.Configuration.Setting"
    );
    assert!(
        !g.edges.iter().any(
            |e| matches!(e, Edge::Ambiguous { from_file, .. } if from_file == "A/B/C/Holder.cs")
        ),
        "step 2 answers, so the ladder never reaches the ambiguous step-4 pool"
    );
}

#[test]
fn ladder_step3_the_ancestor_namespace_rule_walks_every_enclosing_namespace() {
    let files = fragments_for(&[
        ("A/Shared.cs", "namespace A { public class Shared { } }"),
        // Same role as above -- makes step 4 ambiguous, so only a step-3
        // walk can produce a resolved edge here.
        (
            "Other/Shared.cs",
            "namespace Other { public class Shared { } }",
        ),
        (
            "A/B/C/Deep.cs",
            "\nnamespace A.B.C;\n\npublic class Deep\n{\n  private Shared _shared;\n}\n",
        ),
    ]);
    let g = resolve_graph(&no_git_root(), &files);
    assert_eq!(
        type_edge_targets_from(&g, "A/B/C/Deep.cs"),
        vec!["A.Shared"],
        "no usings at all -- the ancestor-namespace walk is the only step that can answer"
    );
    assert!(
        !g.edges.iter().any(
            |e| matches!(e, Edge::Ambiguous { from_file, .. } if from_file == "A/B/C/Deep.cs")
        ),
        "a walked step-3 hit resolves and never falls through to the ambiguous step-4 pool"
    );
}

#[test]
fn stage4_scored_a_nested_type_candidate_is_refused_from_outside_its_own_file_and_kept_inside_it() {
    let files = fragments_for(&[
        // Cross-file nested candidate: unreachable from Holder.cs without
        // naming Remote first, so a guess landing on it could never be what
        // the code said.
        (
            "Far/Remote.cs",
            "\nnamespace App.Far;\n\npublic class Remote\n{\n  public class Inner\n  {\n    public void Tally() { }\n  }\n}\n",
        ),
        // Same-file nested candidate + the ref itself. `mystery` is a
        // var-from-call local, so no receiver fact and no qualifier
        // resolution at all -- the only door into the scored tier's
        // uniqueness fallback.
        (
            "Nested/Holder.cs",
            "\nnamespace App.Nested;\n\npublic class Outer\n{\n  public class Nested\n  {\n    public void Tally() { }\n  }\n\n  public string Probe()\n  {\n    var mystery = Fetch();\n    mystery.Tally();\n    return \"x\";\n  }\n}\n",
        ),
    ]);
    let g = resolve_graph(&no_git_root(), &files);
    assert_eq!(
        heuristic_member_edge_targets(&g),
        vec!["App.Nested.Outer+Nested"],
        "the same-file nested candidate is nameable and stays; the cross-file one is refused"
    );
}

#[test]
fn heuristic_side_dedup_collapses_byte_identical_guesses_and_keeps_an_identical_precise_pair() {
    let files = fragments_for(&[
        ("Widgets/Widget.cs", "namespace App.Widgets { public class Widget { } }"),
        (
            "Ext/Helpers.cs",
            "\nnamespace App.Ext;\n\npublic static class Helpers\n{\n  public static string Slug(this Widget widget)\n  {\n    return \"s\";\n  }\n\n  public static string Tag(this Widget widget)\n  {\n    return \"t\";\n  }\n}\n",
        ),
        // The SAME extension call twice on ONE line: same declaring static
        // class, same member, same line -- two guesses that serialize to
        // the same bytes.
        (
            "Ops/Caller.cs",
            "\nusing App.Ext;\nusing App.Widgets;\n\nnamespace App.Ops;\n\npublic class Caller\n{\n  public string Run()\n  {\n    Widget widget = new Widget();\n    return widget.Tag() + widget.Tag();\n  }\n}\n",
        ),
        ("Enums/Mode.cs", "namespace App.Enums { public enum Mode { On, Off } }"),
        // The precise counterpart: the same enum member read twice on one
        // line. Two identical PRECISE edges are two real occurrences in the
        // source, and dropping either would lose a fact -- so both survive.
        (
            "Ops/Twice.cs",
            "\nusing App.Enums;\n\nnamespace App.Ops;\n\npublic class Twice\n{\n  public bool Both(Mode a, Mode b)\n  {\n    return a == Mode.On && b == Mode.On;\n  }\n}\n",
        ),
    ]);
    let g = resolve_graph(&no_git_root(), &files);
    assert_eq!(
        heuristic_member_edges_from(&g, "Ops/Caller.cs"),
        vec![("App.Ext.Helpers", 12)],
        "the second byte-identical guess is dropped, the first kept"
    );
    assert_eq!(
        member_edges_from(&g, "Ops/Twice.cs"),
        vec![("App.Enums.Mode.On", 10), ("App.Enums.Mode.On", 10)],
        "the precise side is untouched: two identical rows are two occurrences, not a duplicate"
    );
    assert_eq!(
        g.stats.heuristic_edge_count, 1,
        "the dropped guess leaves the counter too"
    );
    assert_eq!(
        g.stats.heuristic_by_tier,
        HeuristicByTier { ext: 1, guess: 0 },
        "and it leaves ITS tier's counter, not the other one"
    );
}

// The other side of the same rule, and the reason `member` had to join the
// dedup key: two guesses that agree on every key the edge used to carry
// and differ ONLY in the member they name are two facts, not a duplicate.
// Before `member` existed these collapsed into one, and a reader lost a
// call.
#[test]
fn heuristic_side_dedup_keeps_two_guesses_that_name_different_members_of_one_target() {
    let files = fragments_for(&[
        ("Widgets/Widget.cs", "namespace App.Widgets { public class Widget { } }"),
        (
            "Ext/Helpers.cs",
            "\nnamespace App.Ext;\n\npublic static class Helpers\n{\n  public static string Slug(this Widget widget)\n  {\n    return \"s\";\n  }\n\n  public static string Tag(this Widget widget)\n  {\n    return \"t\";\n  }\n}\n",
        ),
        (
            "Ops/Caller.cs",
            "\nusing App.Ext;\nusing App.Widgets;\n\nnamespace App.Ops;\n\npublic class Caller\n{\n  public string Run()\n  {\n    Widget widget = new Widget();\n    return widget.Tag() + widget.Slug();\n  }\n}\n",
        ),
    ]);
    let g = resolve_graph(&no_git_root(), &files);
    assert_eq!(
        heuristic_member_edges_from(&g, "Ops/Caller.cs"),
        vec![("App.Ext.Helpers", 12), ("App.Ext.Helpers", 12)],
        "two calls, two edges -- identical but for the member each names"
    );
    assert_eq!(
        heuristic_member_names_from(&g, "Ops/Caller.cs"),
        vec![Some("Tag"), Some("Slug")],
        "and the member is what tells them apart, in source order"
    );
    assert_eq!(g.stats.heuristic_edge_count, 2);
    assert_eq!(
        g.stats.heuristic_by_tier,
        HeuristicByTier { ext: 2, guess: 0 }
    );
}

// --- test coverage: test_methods on the merged row + the counter ---

#[test]
fn partial_test_class_unions_its_test_methods_across_both_declaring_files() {
    let files = vec![
        (
            "Tests/WidgetTests.Part1.cs".to_string(),
            frag(
                vec![test_def(
                    "App.Tests.WidgetTests",
                    "WidgetTests",
                    "App.Tests",
                    &["Renders"],
                )],
                vec![],
                vec![],
            ),
        ),
        (
            "Tests/WidgetTests.Part2.cs".to_string(),
            frag(
                vec![test_def(
                    "App.Tests.WidgetTests",
                    "WidgetTests",
                    "App.Tests",
                    &["Renders", "Scales"],
                )],
                vec![],
                vec![],
            ),
        ),
    ];
    let g = resolve_graph(&no_git_root(), &files);
    assert_eq!(g.defs.len(), 1);
    assert_eq!(
        g.defs[0].test_methods,
        vec!["Renders".to_string(), "Scales".to_string()],
        "union across parts, deduped, first-seen order"
    );
    assert_eq!(
        serde_json::to_string(&g.defs[0]).unwrap(),
        r#"{"id":"App.Tests.WidgetTests","name":"WidgetTests","namespace":"App.Tests","kind":"class","file":"Tests/WidgetTests.Part1.cs","line":1,"methods":[],"testMethods":["Renders","Scales"],"also_in":[{"file":"Tests/WidgetTests.Part2.cs","line":1}]}"#,
        "the graph ROW keeps testMethods -- between methods and also_in"
    );
}

#[test]
fn test_def_count_counts_merged_def_rows_not_fragment_entries() {
    let files = vec![
        (
            "Tests/WidgetTests.Part1.cs".to_string(),
            frag(
                vec![test_def(
                    "App.Tests.WidgetTests",
                    "WidgetTests",
                    "App.Tests",
                    &["Renders"],
                )],
                vec![],
                vec![],
            ),
        ),
        (
            "Tests/WidgetTests.Part2.cs".to_string(),
            frag(
                vec![test_def(
                    "App.Tests.WidgetTests",
                    "WidgetTests",
                    "App.Tests",
                    &["Scales"],
                )],
                vec![],
                vec![],
            ),
        ),
        (
            "Tests/CartTests.cs".to_string(),
            frag(
                vec![test_def(
                    "App.Tests.CartTests",
                    "CartTests",
                    "App.Tests",
                    &["Places"],
                )],
                vec![],
                vec![],
            ),
        ),
        (
            "Src/Widget.cs".to_string(),
            frag(
                vec![def("App.Src.Widget", "Widget", "App.Src", "class")],
                vec![],
                vec![],
            ),
        ),
    ];
    let g = resolve_graph(&no_git_root(), &files);
    assert_eq!(
        g.stats.test_def_count, 2,
        "the partial class counts once across its two fragments; the production class not at all"
    );
}

// --- imports: always recorded, never resolved --------------------------

#[test]
fn imports_edge_is_recorded_regardless_of_whether_the_target_is_known() {
    let files = vec![(
        "A/Widget.cs".to_string(),
        frag(
            vec![],
            vec![],
            vec![FragRef {
                kind: "imports".into(),
                name: "System.Text".into(),
                qualified: None,
                member: None,
                line: 1,
                namespace: None,
                type_arg_count: None,
                generic: false,
                receiver_type: None,
                arg_count: None,
                receiver_args: None,
                outer_types: Vec::new(),
                args: None,
                receiver_property_owner: None,
                receiver_call_owner: None,
                receiver_call_member: None,
                receiver_base: false,
                receiver_awaited: false,
                receiver_local: false,
                receiver_lambda: None,
                receiver_nullable: false,
                lambda_arg_arity: None,
                method_type_arg_count: None,
                lambda_arg_shape: None,
                receiver_type_qualified: None,
                arg_types: None,
            }],
        ),
    )];
    let g = resolve_graph(&no_git_root(), &files);
    assert_eq!(g.stats.edges_by_kind.imports, 1);
    assert_eq!(
        g.stats.unresolved_external_count, 0,
        "imports never counts toward unresolved"
    );
}

// --- stats math ---------------------------------------------------------

#[test]
fn ambiguous_pct_only_counts_type_ref_attempts_not_uses_member_or_imports() {
    let files = vec![
        (
            "A/Money.cs".to_string(),
            frag(vec![def("A.Money", "Money", "A", "class")], vec![], vec![]),
        ),
        (
            "B/Money.cs".to_string(),
            frag(vec![def("B.Money", "Money", "B", "class")], vec![], vec![]),
        ),
        (
            "C/Mixed.cs".to_string(),
            frag(
                vec![def("C.Mixed", "Mixed", "C", "class")],
                vec![],
                vec![
                    type_ref("uses-type", "Money", None, "C"), // ambiguous
                    FragRef {
                        kind: "imports".into(),
                        name: "System".into(),
                        qualified: None,
                        member: None,
                        line: 2,
                        namespace: None,
                        type_arg_count: None,
                        generic: false,
                        receiver_type: None,
                        arg_count: None,
                        receiver_args: None,
                        outer_types: Vec::new(),
                        args: None,
                        receiver_property_owner: None,
                        receiver_call_owner: None,
                        receiver_call_member: None,
                        receiver_base: false,
                        receiver_awaited: false,
                        receiver_local: false,
                        receiver_lambda: None,
                        receiver_nullable: false,
                        lambda_arg_arity: None,
                        method_type_arg_count: None,
                        lambda_arg_shape: None,
                        receiver_type_qualified: None,
                        arg_types: None,
                    },
                ],
            ),
        ),
    ];
    let g = resolve_graph(&no_git_root(), &files);
    // type_ref_attempts = inherits(0) + uses-type(0) + ambiguous(1) = 1
    assert_eq!(g.stats.ambiguous_pct, Percent1::from_ratio(1, 1));
    assert_eq!(
        serde_json::to_string(&g.stats.ambiguous_pct).unwrap(),
        "100"
    );
}

// --- the enclosing-type step ---