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
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
use super::*;

// --- precise TP --------------------------------------------------------

#[test]
fn precise_edge_at_a_matching_in_solution_site_scores_as_a_true_positive() {
    let files = [
        (
            "Other/MessageUrn.cs",
            "namespace App.Consumers { public static class MessageUrn { public static string Prefix { get; } } }",
        ),
        (
            "Consumers/UsesProperty.cs",
            "\nnamespace App.Consumers;\n\npublic class UsesProperty\n{\n  public object Get() => MessageUrn.Prefix;\n}\n",
        ),
    ];
    let value = graph_value_for(&files);
    let (graph_defs, edges) = parse_graph(&value).expect("graph.json parses");
    assert_eq!(edges.len(), 1, "exactly one uses-member edge expected");
    assert_eq!(edges[0].tier, Tier::Precise);

    let record = oracle_ref(
        &edges[0].from_file,
        edges[0].from_line,
        "access",
        "ident",
        edges[0].member.as_deref().unwrap_or(""),
        Some(&edges[0].to),
        Some("class"),
        false,
    );
    let universe: HashSet<String> = [edges[0].from_file.clone()].into_iter().collect();
    let report = score(Inputs {
        root: PathBuf::from("/repo"),
        graph_defs,
        oracle_defs: Vec::new(),
        edges,
        records: vec![record],
        units: Vec::new(),
        universe,
        lane: AuditLane::SYNTAX,
        discovered_shapes: HashMap::new(),
        collect_fp_sites: false,
    });

    assert_eq!(report.tiers.len(), 1);
    let (tier, ts) = &report.tiers[0];
    assert_eq!(*tier, Tier::Precise);
    assert_eq!(ts.tp, 1);
    assert_eq!(ts.fp, 0);
    assert_eq!(report.oracle_dropped, 0);
}

// --- guess FP at an external site (leak) --------------------------------

#[test]
fn guessed_edge_at_an_all_external_site_scores_as_a_leaked_false_positive() {
    let files = [
        (
            "Other/Widget.cs",
            "namespace App.Other { public class Widget { public void Frob() { } } }",
        ),
        (
            "Consumers/Guess.cs",
            "\nnamespace App.Consumers;\n\npublic class Guess\n{\n  public void Unknown()\n  {\n    var w = Compute();\n    w.Frob();\n  }\n  private object Compute() => null;\n}\n",
        ),
    ];
    let value = graph_value_for(&files);
    let (graph_defs, edges) = parse_graph(&value).expect("graph.json parses");
    let heuristic_count = edges.iter().filter(|e| e.tier == Tier::Guess).count();
    assert_eq!(heuristic_count, 1, "expected exactly one guessed edge");
    let g_edge = edges.iter().find(|e| e.tier == Tier::Guess).unwrap();

    // The oracle saw a genuinely external member at this same site (e.g.
    // an extension method from a package devscout never indexed) --
    // external, so no guessed target can ever be a true positive here.
    let record = oracle_ref(
        &g_edge.from_file,
        g_edge.from_line,
        "access",
        "ident",
        g_edge.member.as_deref().unwrap_or("Frob"),
        Some("Some.External.Type"),
        Some("class"),
        true,
    );
    let universe: HashSet<String> = [g_edge.from_file.clone()].into_iter().collect();
    let report = score(Inputs {
        root: PathBuf::from("/repo"),
        graph_defs,
        oracle_defs: Vec::new(),
        edges,
        records: vec![record],
        units: Vec::new(),
        universe,
        lane: AuditLane::SYNTAX,
        discovered_shapes: HashMap::new(),
        collect_fp_sites: false,
    });

    assert_eq!(report.tiers.len(), 1);
    let (tier, ts) = &report.tiers[0];
    assert_eq!(*tier, Tier::Guess);
    assert_eq!(ts.tp, 0);
    assert_eq!(ts.fp, 1);
    assert_eq!(ts.fp_external_site, 1);
    assert_eq!(report.silent_leak, 1);
    assert_eq!(report.silent_correct, 0);
    assert_eq!(report.oracle_external_sites, 1);
}

// --- any-match with two records on one site -----------------------------

#[test]
fn a_matching_record_among_several_at_one_site_still_earns_a_true_positive() {
    // No `member` on the edge (the legacy, schema-1 shape) -- unconstrained
    // by the member-join refinement, so this test still exercises pure
    // target-matching across several records at one site, byte-identical
    // to before that refinement existed.
    let edge = EdgeRow {
        from_file: "F.cs".into(),
        from_line: 5,
        to: "Ns.Right".into(),
        to_file: "F.cs".into(),
        tier: Tier::Precise,
        member: None,
    };
    let wrong = oracle_ref(
        "F.cs",
        5,
        "access",
        "ident",
        "M",
        Some("Ns.Wrong"),
        Some("class"),
        false,
    );
    let right = oracle_ref(
        "F.cs",
        5,
        "access",
        "ident",
        "M",
        Some("Ns.Right"),
        Some("class"),
        false,
    );
    let report = score(Inputs {
        root: PathBuf::from("/repo"),
        graph_defs: vec![DefRow {
            id: "Ns.Right".into(),
            file: "F.cs".into(),
            kind: "class".into(),
            test: false,
        }],
        oracle_defs: Vec::new(),
        edges: vec![edge],
        records: vec![wrong, right],
        units: Vec::new(),
        universe: ["F.cs".to_string()].into_iter().collect(),
        lane: AuditLane::SYNTAX,
        discovered_shapes: HashMap::new(),
        collect_fp_sites: false,
    });
    let (_, ts) = &report.tiers[0];
    assert_eq!(ts.tp, 1);
    assert_eq!(ts.fp, 0);
}

// --- member equality picks the right record at a shared site -----------

/// The motivating case for the member-join refinement: a chain-tail call
/// (`Order.Load("x").Validate()`, `tests/App.Tests/WorkerTests.cs` in
/// the fixture) puts TWO oracle records on one `(file, startLine)` --
/// `Load` on the `Order` qualifier, `Validate` on the chain's tail -- but
/// devscout's own extractor only ever emits an edge for the qualifier
/// call. Before the member-join refinement, `Validate`'s record still
/// counted as a recall HIT, because `target_matches` alone can't tell
/// the two references on that line apart: both target `Ns.Order`. With
/// member equality required, only the `Load` edge's own record vouches
/// for it, and `Validate` -- which no edge actually names -- is
/// correctly a miss.
#[test]
fn member_equality_picks_the_right_record_at_a_shared_site_chain_tail_vs_qualifier() {
    let edge = EdgeRow {
        from_file: "F.cs".into(),
        from_line: 5,
        to: "Ns.Order".into(),
        to_file: "F.cs".into(),
        tier: Tier::Precise,
        member: Some("Load".to_string()),
    };
    let load_record = oracle_ref(
        "F.cs",
        5,
        "access",
        "ident",
        "Load",
        Some("Ns.Order"),
        Some("class"),
        false,
    );
    let validate_record = oracle_ref(
        "F.cs",
        5,
        "access",
        "call",
        "Validate",
        Some("Ns.Order"),
        Some("class"),
        false,
    );
    let report = score(Inputs {
        root: PathBuf::from("/repo"),
        graph_defs: vec![DefRow {
            id: "Ns.Order".into(),
            file: "F.cs".into(),
            kind: "class".into(),
            test: false,
        }],
        oracle_defs: Vec::new(),
        edges: vec![edge],
        records: vec![load_record, validate_record],
        units: Vec::new(),
        universe: ["F.cs".to_string()].into_iter().collect(),
        lane: AuditLane::SYNTAX,
        discovered_shapes: HashMap::new(),
        collect_fp_sites: false,
    });

    // The edge itself: a TP against `load_record` only.
    let (_, ts) = &report.tiers[0];
    assert_eq!(ts.tp, 1);
    assert_eq!(ts.fp, 0);

    // Recall: both records are D-eligible, but only `load_record` is a
    // hit -- `validate_record` is a genuine miss, not a false hit
    // borrowed from the `Load` edge sharing its site.
    assert_eq!(report.recall_denominator, 2);
    assert_eq!(report.recall_all, 1);
    assert_eq!(report.top_missed, vec![("Ns.Order".to_string(), 1)]);
}

// --- member-scoped external-site vs wrong-target classification --------

/// The fixture's `entity.Property(e => e.Name)` case
/// (`src/App/AppDbContext.cs`): a guessed `Property(...)` edge landing
/// on the wrong in-tree class shares its source line with an UNRELATED
/// non-external record for a different member (`e.Name`'s lambda
/// parameter access) -- before the member-join refinement, that
/// unrelated record's mere presence at the site was enough to call the
/// guess `fp_wrong_target` instead of the external-API leak it actually
/// is (EF's real `Property(...)` fluent method, external). Scoping the
/// external-vs-wrong split to records sharing the edge's own member
/// fixes this: `e.Name` (member `Name`) no longer speaks for a `Property`
/// edge.
#[test]
fn a_different_member_non_external_record_does_not_block_an_external_leak_classification() {
    let edge = EdgeRow {
        from_file: "F.cs".into(),
        from_line: 24,
        to: "Ns.FilterConfig".into(),
        to_file: "F.cs".into(),
        tier: Tier::Guess,
        member: Some("Property".to_string()),
    };
    // The chain's outer call -- external, same member as the edge.
    let has_max_length = oracle_ref(
        "F.cs",
        24,
        "access",
        "call",
        "HasMaxLength",
        Some("Ext.PropertyBuilder"),
        Some("class"),
        true,
    );
    // The lambda parameter access `e.Name` -- non-external, but a
    // DIFFERENT member than the edge's own `Property`.
    let e_name = oracle_ref(
        "F.cs",
        24,
        "access",
        "ident",
        "Name",
        Some("Ns.Order"),
        Some("class"),
        false,
    );
    // The actual `entity.Property(...)` call the edge is a wrong guess
    // for -- external, same member (`Property`) as the edge.
    let entity_property = oracle_ref(
        "F.cs",
        24,
        "access",
        "ident",
        "Property",
        Some("Ext.EntityTypeBuilder"),
        Some("class"),
        true,
    );
    let report = score(Inputs {
        root: PathBuf::from("/repo"),
        graph_defs: vec![DefRow {
            id: "Ns.Order".into(),
            file: "F.cs".into(),
            kind: "class".into(),
            test: false,
        }],
        oracle_defs: Vec::new(),
        edges: vec![edge],
        records: vec![has_max_length, e_name, entity_property],
        units: Vec::new(),
        universe: ["F.cs".to_string()].into_iter().collect(),
        lane: AuditLane::SYNTAX,
        discovered_shapes: HashMap::new(),
        collect_fp_sites: false,
    });

    let (_, ts) = &report.tiers[0];
    assert_eq!(ts.fp, 1);
    assert_eq!(
        ts.fp_external_site, 1,
        "the member-scoped view sees only `entity_property`, which is external"
    );
    assert_eq!(
        ts.fp_wrong_target, 0,
        "`e_name` is non-external but names a different member, so it must not \
         count as an in-tree answer this edge got wrong"
    );
}

/// The other half of the same rule: when the member scope is EMPTY -- no
/// record at the site names the edge's member at all -- the split falls
/// back to the whole site rather than reading a vacuous "every scoped
/// record is external" off zero records. A site whose records are all
/// in-tree is not an external-API leak just because the edge invented a
/// member nobody wrote there, so it is `fp_wrong_target`.
#[test]
fn an_edge_naming_a_member_no_record_at_the_site_names_falls_back_to_the_whole_site() {
    let edge = EdgeRow {
        from_file: "F.cs".into(),
        from_line: 24,
        to: "Ns.FilterConfig".into(),
        to_file: "F.cs".into(),
        tier: Tier::Guess,
        member: Some("Nowhere".to_string()),
    };
    // The only record at line 24, and it is IN-TREE -- so the site has a
    // real answer, just not one for `Nowhere`.
    let in_tree = oracle_ref(
        "F.cs",
        24,
        "access",
        "ident",
        "Name",
        Some("Ns.Order"),
        Some("class"),
        false,
    );
    let report = score(Inputs {
        root: PathBuf::from("/repo"),
        graph_defs: vec![DefRow {
            id: "Ns.Order".into(),
            file: "F.cs".into(),
            kind: "class".into(),
            test: false,
        }],
        oracle_defs: Vec::new(),
        edges: vec![edge],
        records: vec![in_tree],
        units: Vec::new(),
        universe: ["F.cs".to_string()].into_iter().collect(),
        lane: AuditLane::SYNTAX,
        discovered_shapes: HashMap::new(),
        collect_fp_sites: false,
    });

    let (_, ts) = &report.tiers[0];
    assert_eq!(ts.fp, 1);
    assert_eq!(
        ts.fp_wrong_target, 1,
        "the site's only record is in-tree, so the edge is a wrong target, not a leak"
    );
    assert_eq!(
        ts.fp_external_site, 0,
        "an empty member scope must not be read as `every record here is external`"
    );
}

/// And the same empty-member-scope path at a site whose records are ALL
/// external: there the fallback agrees with the old vacuous answer, and
/// the edge really is a leak.
#[test]
fn an_edge_naming_an_unwritten_member_at_an_all_external_site_is_still_a_leak() {
    let edge = EdgeRow {
        from_file: "F.cs".into(),
        from_line: 24,
        to: "Ns.FilterConfig".into(),
        to_file: "F.cs".into(),
        tier: Tier::Guess,
        member: Some("Nowhere".to_string()),
    };
    let external_only = oracle_ref(
        "F.cs",
        24,
        "access",
        "call",
        "HasMaxLength",
        Some("Ext.PropertyBuilder"),
        Some("class"),
        true,
    );
    let report = score(Inputs {
        root: PathBuf::from("/repo"),
        graph_defs: vec![DefRow {
            id: "Ns.FilterConfig".into(),
            file: "F.cs".into(),
            kind: "class".into(),
            test: false,
        }],
        oracle_defs: Vec::new(),
        edges: vec![edge],
        records: vec![external_only],
        units: Vec::new(),
        universe: ["F.cs".to_string()].into_iter().collect(),
        lane: AuditLane::SYNTAX,
        discovered_shapes: HashMap::new(),
        collect_fp_sites: false,
    });

    let (_, ts) = &report.tiers[0];
    assert_eq!(ts.fp, 1);
    assert_eq!(ts.fp_external_site, 1);
    assert_eq!(ts.fp_wrong_target, 0);
}

// --- enum member, both spellings ----------------------------------------

#[test]
fn enum_member_edge_matches_both_the_full_and_the_bare_spelling() {
    let edge_full = EdgeRow {
        from_file: "F.cs".into(),
        from_line: 10,
        to: "Ns.OrderStatus.Open".into(),
        to_file: "Ns/OrderStatus.cs".into(),
        tier: Tier::Precise,
        member: Some("Open".to_string()),
    };
    let edge_bare = EdgeRow {
        from_file: "F.cs".into(),
        from_line: 20,
        to: "Ns.OrderStatus".into(),
        to_file: "Ns/OrderStatus.cs".into(),
        tier: Tier::Precise,
        member: Some("Open".to_string()),
    };
    let rec_at_full = oracle_ref(
        "F.cs",
        10,
        "access",
        "ident",
        "Open",
        Some("Ns.OrderStatus.Open"),
        Some("enum-member"),
        false,
    );
    let rec_at_bare = oracle_ref(
        "F.cs",
        20,
        "access",
        "ident",
        "Open",
        Some("Ns.OrderStatus.Open"),
        Some("enum-member"),
        false,
    );
    let report = score(Inputs {
        root: PathBuf::from("/repo"),
        graph_defs: vec![DefRow {
            id: "Ns.OrderStatus".into(),
            file: "Ns/OrderStatus.cs".into(),
            kind: "enum".into(),
            test: false,
        }],
        oracle_defs: Vec::new(),
        edges: vec![edge_full, edge_bare],
        records: vec![rec_at_full, rec_at_bare],
        units: Vec::new(),
        universe: ["F.cs".to_string()].into_iter().collect(),
        lane: AuditLane::SYNTAX,
        discovered_shapes: HashMap::new(),
        collect_fp_sites: false,
    });
    let (_, ts) = &report.tiers[0];
    assert_eq!(ts.tp, 2);
    assert_eq!(ts.fp, 0);
}

// --- legacy heuristic:true, and tier:"ext"/"guess" strings --------------

#[test]
fn tier_is_read_from_the_tier_string_when_present_else_from_legacy_heuristic_bool() {
    let text = r#"{"defs":[],"edges":[
        {"kind":"uses-member","from_file":"F.cs","from_line":1,"to":"Ns.A","to_file":"A.cs","heuristic":true},
        {"kind":"uses-member","from_file":"F.cs","from_line":2,"to":"Ns.B","to_file":"B.cs","tier":"ext"},
        {"kind":"uses-member","from_file":"F.cs","from_line":3,"to":"Ns.C","to_file":"C.cs","tier":"guess"},
        {"kind":"uses-member","from_file":"F.cs","from_line":4,"to":"Ns.D","to_file":"D.cs"},
        {"kind":"inherits","from_file":"F.cs","from_line":5,"to":"Ns.E","to_file":"E.cs"}
    ]}"#;
    let value: serde_json::Value = serde_json::from_str(text).unwrap();
    let (_, edges) = parse_graph(&value).unwrap();
    assert_eq!(
        edges.len(),
        4,
        "the 'inherits' edge is not uses-member and must be filtered out"
    );
    let tiers: Vec<Tier> = edges.iter().map(|e| e.tier).collect();
    assert_eq!(
        tiers,
        vec![Tier::Heuristic, Tier::Ext, Tier::Guess, Tier::Precise]
    );
}

// --- universe: an edge from a file outside units[].files is dropped ----

/// The MassTransit-run bug this refinement fixes (480 of 530 false
/// positives on that corpus): with `--units`, a `uses-member` edge whose
/// `from_file` belongs to a project the compiled `.sln` never listed at
/// all -- not `"ok"`, not `"failed"`, simply absent from `units.jsonl`
/// -- carries no oracle ground truth either way. Before this
/// refinement, only oracle RECORDS were dropped for falling outside
/// `universe`; nothing stopped such an edge from being scored, and
/// since no record ever shares its site, it landed as an `fp_no_site`
/// false positive on every run. `universe` = union of `files` across
/// every `"ok"` unit means "App/A.cs" is in it and "Other/B.cs" -- from
/// a project `units` never mentions -- is not, so the edge from
/// "Other/B.cs" is dropped before scoring, not counted as a false
/// positive.
#[test]
fn an_edge_from_a_file_outside_units_files_is_dropped_not_a_false_positive() {
    let edge = EdgeRow {
        from_file: "Other/B.cs".into(),
        from_line: 1,
        to: "Ns.Something".into(),
        to_file: "Other/B.cs".into(),
        tier: Tier::Heuristic,
        member: None,
    };
    let units = vec![Unit {
        name: "App".into(),
        status: "ok".into(),
        refs: vec![],
        files: vec!["App/A.cs".into()],
    }];
    let report = score(Inputs {
        root: PathBuf::from("/repo"),
        graph_defs: Vec::new(),
        oracle_defs: Vec::new(),
        edges: vec![edge],
        records: Vec::new(),
        units,
        // What `build_universe` would produce: the manifest's mapped
        // files ("App/A.cs" AND "Other/B.cs" -- devscout mapped both)
        // intersected with the union of ok units' files ("App/A.cs"
        // only), since "Other/B.cs" belongs to no unit at all.
        universe: ["App/A.cs".to_string()].into_iter().collect(),
        lane: AuditLane::SYNTAX,
        discovered_shapes: HashMap::new(),
        collect_fp_sites: false,
    });

    assert_eq!(report.edges_outside_universe, 1);
    assert!(
        report.tiers.is_empty(),
        "the dropped edge must not appear in any tier's stats, fp_no_site included: {:?}",
        report.tiers
    );
    assert_eq!(report.structural_checked, 0);
}

// --- structural: units method, and test-defs fallback -------------------

#[test]
fn structural_check_via_units_flags_an_edge_the_caller_project_cannot_reach() {
    let edge = EdgeRow {
        from_file: "App/A.cs".into(),
        from_line: 9,
        to: "Tests.Foo".into(),
        to_file: "Tests/Foo.cs".into(),
        tier: Tier::Heuristic,
        member: None,
    };
    let units = vec![
        Unit {
            name: "App".into(),
            status: "ok".into(),
            refs: vec!["Domain".into()],
            files: vec!["App/A.cs".into()],
        },
        Unit {
            name: "Domain".into(),
            status: "ok".into(),
            refs: vec![],
            files: vec![],
        },
        Unit {
            name: "Tests".into(),
            status: "ok".into(),
            refs: vec!["App".into()],
            files: vec!["Tests/Foo.cs".into()],
        },
    ];
    // Also a genuine (non-external) match at the site: App can never
    // reach Tests, so this edge is structurally impossible EVEN THOUGH
    // it is also the right answer to the oracle record at its site --
    // exactly the case `TierStats.structural`'s doc comment calls out.
    // The GLOBAL counts (`structural_checked`/`structural_impossible`)
    // still see it: they come from `is_structural` alone, independent
    // of TP/FP. Only the per-tier `structural` column, which feeds a
    // "these are resolver false positives" reading, excludes it.
    let record = oracle_ref(
        "App/A.cs",
        9,
        "access",
        "ident",
        "Foo",
        Some("Tests.Foo"),
        Some("class"),
        false,
    );
    let report = score(Inputs {
        root: PathBuf::from("/repo"),
        graph_defs: vec![DefRow {
            id: "Tests.Foo".into(),
            file: "Tests/Foo.cs".into(),
            kind: "class".into(),
            test: false,
        }],
        oracle_defs: Vec::new(),
        edges: vec![edge],
        records: vec![record],
        units,
        universe: ["App/A.cs".to_string()].into_iter().collect(),
        lane: AuditLane::SYNTAX,
        discovered_shapes: HashMap::new(),
        collect_fp_sites: false,
    });
    assert_eq!(report.structural_method, "units");
    assert_eq!(report.structural_checked, 1);
    assert_eq!(report.structural_impossible, 1);
    let (_, ts) = &report.tiers[0];
    assert_eq!(ts.tp, 1, "the structurally-impossible edge is still a TP");
    assert_eq!(
        ts.structural, 0,
        "a TP is never counted structural, however structurally impossible its edge -- \
         the tier column reports resolver false positives, not every structural oddity"
    );
}

#[test]
fn structural_fallback_flags_a_non_test_caller_reaching_a_test_attributed_def() {
    let edge_bad = EdgeRow {
        from_file: "App/A.cs".into(),
        from_line: 3,
        to: "Tests.Helper".into(),
        to_file: "Tests/Helper.cs".into(),
        tier: Tier::Heuristic,
        member: None,
    };
    // A second edge from a file that DOES declare a test-attributed def
    // of its own -- the fallback's second clause ("from_file has no
    // test def") should clear this one.
    let edge_ok = EdgeRow {
        from_file: "Tests/Caller.cs".into(),
        from_line: 4,
        to: "Tests.Helper".into(),
        to_file: "Tests/Helper.cs".into(),
        tier: Tier::Heuristic,
        member: None,
    };
    let report = score(Inputs {
        root: PathBuf::from("/repo"),
        graph_defs: vec![
            DefRow {
                id: "Tests.Helper".into(),
                file: "Tests/Helper.cs".into(),
                kind: "class".into(),
                test: true,
            },
            DefRow {
                id: "Tests.Caller".into(),
                file: "Tests/Caller.cs".into(),
                kind: "class".into(),
                test: true,
            },
        ],
        oracle_defs: Vec::new(),
        edges: vec![edge_bad, edge_ok],
        records: Vec::new(),
        units: Vec::new(), // empty -> "test-defs" fallback
        universe: HashSet::new(),
        lane: AuditLane::SYNTAX,
        discovered_shapes: HashMap::new(),
        collect_fp_sites: false,
    });
    assert_eq!(report.structural_method, "test-defs");
    assert_eq!(report.structural_checked, 2);
    assert_eq!(report.structural_impossible, 1);
}

/// The oracle's `--defs` view layers over the graph's for the FILE set as
/// well as the def set: a def the graph called test-attributed and the
/// oracle calls ordinary must stop making its file a test file. Otherwise
/// the caller keeps a clearance the merged view has withdrawn, and an edge
/// into a test-only def goes on being scored as legitimate.
#[test]
fn an_oracle_def_row_saying_test_false_un_marks_the_file_for_the_structural_fallback() {
    let inputs = |oracle_defs: Vec<DefRow>| Inputs {
        root: PathBuf::from("/repo"),
        graph_defs: vec![
            DefRow {
                id: "Tests.Helper".into(),
                file: "Tests/Helper.cs".into(),
                kind: "class".into(),
                test: true,
            },
            // The graph's own attribute scan called this a test def...
            DefRow {
                id: "Tests.Caller".into(),
                file: "Tests/Caller.cs".into(),
                kind: "class".into(),
                test: true,
            },
        ],
        oracle_defs,
        edges: vec![EdgeRow {
            from_file: "Tests/Caller.cs".into(),
            from_line: 4,
            to: "Tests.Helper".into(),
            to_file: "Tests/Helper.cs".into(),
            tier: Tier::Heuristic,
            member: None,
        }],
        records: Vec::new(),
        units: Vec::new(), // empty -> "test-defs" fallback
        universe: HashSet::new(),
        lane: AuditLane::SYNTAX,
        discovered_shapes: HashMap::new(),
        collect_fp_sites: false,
    };

    let without_oracle = score(inputs(Vec::new()));
    assert_eq!(
        without_oracle.structural_impossible, 0,
        "with only the graph's view the caller declares a test def, so the edge is fine"
    );

    // ...and the oracle overrules it.
    let with_oracle = score(inputs(vec![DefRow {
        id: "Tests.Caller".into(),
        file: "Tests/Caller.cs".into(),
        kind: "class".into(),
        test: false,
    }]));
    assert_eq!(with_oracle.structural_checked, 1);
    assert_eq!(
        with_oracle.structural_impossible, 1,
        "the merged view has no test def in Tests/Caller.cs any more, so the file is not a test file and the edge into a test-only def is impossible"
    );
}