sqry-core 6.0.12

Core library for sqry - semantic code search engine
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
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
//! Unused symbol detection for unified graph.
//!
//! This module provides unused/dead code detection using the unified graph API,
//! replacing the legacy index-based unused detection.

use crate::graph::unified::concurrent::CodeGraph;
use crate::graph::unified::edge::EdgeKind;
use crate::graph::unified::node::{NodeId, NodeKind};
use std::collections::HashSet;
use std::hash::BuildHasher;

/// Scope for unused analysis
///
/// Determines which symbols are candidates for unused detection.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum UnusedScope {
    /// Public symbols with no external references
    Public,
    /// Private symbols with no references
    Private,
    /// Unused functions (any visibility)
    Function,
    /// Unused structs/types (any visibility)
    Struct,
    /// All unused symbols
    All,
}

impl UnusedScope {
    /// Parse scope from query value string
    #[must_use]
    pub fn try_parse(s: &str) -> Option<Self> {
        match s.to_lowercase().as_str() {
            "public" => Some(Self::Public),
            "private" => Some(Self::Private),
            "function" => Some(Self::Function),
            "struct" => Some(Self::Struct),
            "all" | "" => Some(Self::All),
            _ => None,
        }
    }
}

/// Check if a node entry qualifies as an entry point for reachability analysis.
///
/// A node is an entry point if it is:
/// - Public (visibility = "public" or "pub")
/// - A main function (name = "main")
/// - A test function (name starts with "test_" or ends with "_test")
/// - A `Test` or `Export` node kind
fn is_entry_point_node(
    entry: &crate::graph::unified::storage::arena::NodeEntry,
    strings: &crate::graph::unified::storage::StringInterner,
) -> bool {
    let is_public = entry
        .visibility
        .and_then(|id| strings.resolve(id))
        .is_some_and(|v| v.as_ref() == "public" || v.as_ref() == "pub");

    let is_main_or_test = strings.resolve(entry.name).is_some_and(|name| {
        name.as_ref() == "main" || name.starts_with("test_") || name.ends_with("_test")
    });

    let is_export = matches!(entry.kind, NodeKind::Export);
    let is_test_node = matches!(entry.kind, NodeKind::Test);

    is_public || is_main_or_test || is_export || is_test_node
}

/// Check if an edge kind propagates reachability during BFS traversal.
fn is_reachability_edge(kind: &EdgeKind) -> bool {
    matches!(
        kind,
        EdgeKind::Calls { .. }
            | EdgeKind::References
            | EdgeKind::Imports { .. }
            | EdgeKind::Inherits
            | EdgeKind::Implements
            | EdgeKind::TypeOf { .. }
    )
}

/// Compute the set of reachable nodes from entry points via BFS traversal.
///
/// Entry points are automatically identified as:
/// - Public functions/methods (visibility = "public" or "pub")
/// - Main functions (name = "main")
/// - Test functions (name starts with "test_" or ends with "_test")
/// - Nodes of kind `Test` or `Export`
///
/// The traversal follows all outgoing edges to discover transitively reachable nodes.
///
/// # Arguments
///
/// * `graph` - The code graph to analyze
///
/// # Returns
///
/// A `HashSet` of `NodeId`s representing all nodes reachable from entry points.
#[must_use]
pub fn compute_reachable_set_graph(graph: &CodeGraph) -> HashSet<NodeId> {
    let mut reachable = HashSet::new();
    let mut worklist: Vec<NodeId> = Vec::new();

    let strings = graph.strings();

    // Find entry points
    for (node_id, entry) in graph.nodes().iter() {
        if is_entry_point_node(entry, strings) {
            worklist.push(node_id);
            reachable.insert(node_id);
        }
    }

    // BFS to find all reachable nodes
    while let Some(node_id) = worklist.pop() {
        for edge in graph.edges().edges_from(node_id) {
            if !reachable.contains(&edge.target) && is_reachability_edge(&edge.kind) {
                reachable.insert(edge.target);
                worklist.push(edge.target);
            }
        }
    }

    reachable
}

/// Check if a specific node is unused based on the scope filter.
///
/// A node is considered unused if:
/// 1. It matches the scope filter (All, Public, Private, Function, or Struct)
/// 2. It is not an entry point (main, test, or export)
/// 3. It is not reachable from any entry point
///
/// # Arguments
///
/// * `node_id` - The node to check
/// * `scope` - Filter for which types of nodes to check
/// * `graph` - The code graph to analyze
/// * `reachable` - Optional pre-computed reachable set (for efficiency when checking many nodes)
///
/// # Returns
///
/// `true` if the node is unused and matches the scope, `false` otherwise.
/// Returns `false` for invalid or non-existent nodes.
#[must_use]
pub fn is_node_unused<S: BuildHasher>(
    node_id: NodeId,
    scope: UnusedScope,
    graph: &CodeGraph,
    reachable: Option<&HashSet<NodeId, S>>,
) -> bool {
    let Some(entry) = graph.nodes().get(node_id) else {
        return false;
    };

    let strings = graph.strings();

    // Check scope filter
    let matches_scope = match scope {
        UnusedScope::All => true,
        UnusedScope::Public => entry
            .visibility
            .and_then(|id| strings.resolve(id))
            .is_some_and(|v| v.as_ref() == "public" || v.as_ref() == "pub"),
        UnusedScope::Private => {
            let vis = entry.visibility.and_then(|id| strings.resolve(id));
            vis.is_none() || vis.is_some_and(|v| v.as_ref() != "public" && v.as_ref() != "pub")
        }
        UnusedScope::Function => {
            matches!(entry.kind, NodeKind::Function | NodeKind::Method)
        }
        UnusedScope::Struct => {
            matches!(entry.kind, NodeKind::Struct | NodeKind::Class)
        }
    };

    if !matches_scope {
        return false;
    }

    // Skip entry points (they're always "used")
    let is_entry_point = {
        let is_main_or_test = strings.resolve(entry.name).is_some_and(|name| {
            name.as_ref() == "main" || name.starts_with("test_") || name.ends_with("_test")
        });

        let is_export = matches!(entry.kind, NodeKind::Export);
        let is_test_node = matches!(entry.kind, NodeKind::Test);

        is_main_or_test || is_export || is_test_node
    };

    if is_entry_point {
        return false;
    }

    // Check reachability
    if let Some(reachable_set) = reachable {
        return !reachable_set.contains(&node_id);
    }

    let reachable_set = compute_reachable_set_graph(graph);
    !reachable_set.contains(&node_id)
}

/// Find all unused nodes in the graph that match the given scope.
///
/// This function first computes the reachable set once, then iterates through
/// all nodes checking which are unused and match the scope filter.
///
/// # Arguments
///
/// * `scope` - Filter for which types of nodes to check (All, Public, Private, Function, Struct)
/// * `graph` - The code graph to analyze
/// * `max_results` - Maximum number of unused nodes to return
///
/// # Returns
///
/// A vector of `NodeId`s for unused nodes, limited by `max_results`.
#[must_use]
pub fn find_unused_nodes(scope: UnusedScope, graph: &CodeGraph, max_results: usize) -> Vec<NodeId> {
    let reachable = compute_reachable_set_graph(graph);
    let mut unused = Vec::new();

    for (node_id, _entry) in graph.nodes().iter() {
        if unused.len() >= max_results {
            break;
        }

        if is_node_unused(node_id, scope, graph, Some(&reachable)) {
            unused.push(node_id);
        }
    }

    unused
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::graph::unified::storage::arena::NodeEntry;
    use std::collections::hash_map::RandomState;
    use std::path::Path;

    /// Options for creating test nodes.
    struct NodeOptions<'a> {
        name: &'a str,
        kind: NodeKind,
        visibility: Option<&'a str>,
    }

    impl<'a> NodeOptions<'a> {
        fn new(name: &'a str, kind: NodeKind) -> Self {
            Self {
                name,
                kind,
                visibility: None,
            }
        }

        fn with_visibility(mut self, vis: &'a str) -> Self {
            self.visibility = Some(vis);
            self
        }
    }

    /// Helper to create a test graph with configurable nodes and edges.
    fn create_test_graph(
        nodes: &[NodeOptions],
        edges: &[(usize, usize, EdgeKind)],
    ) -> (CodeGraph, Vec<NodeId>) {
        let mut graph = CodeGraph::new();
        let file_id = graph.files_mut().register(Path::new("test.rs")).unwrap();
        let mut node_ids = Vec::new();

        for opts in nodes {
            let name_id = graph.strings_mut().intern(opts.name).unwrap();
            let mut entry =
                NodeEntry::new(opts.kind, name_id, file_id).with_qualified_name(name_id);

            if let Some(vis) = opts.visibility {
                let vis_id = graph.strings_mut().intern(vis).unwrap();
                entry = entry.with_visibility(vis_id);
            }

            let node_id = graph.nodes_mut().alloc(entry).unwrap();
            node_ids.push(node_id);
        }

        for (source_idx, target_idx, kind) in edges {
            let source = node_ids[*source_idx];
            let target = node_ids[*target_idx];
            graph
                .edges_mut()
                .add_edge(source, target, kind.clone(), file_id);
        }

        (graph, node_ids)
    }

    #[test]
    fn test_empty_graph() {
        let graph = CodeGraph::new();
        let reachable = compute_reachable_set_graph(&graph);
        assert!(reachable.is_empty());
    }

    #[test]
    fn test_find_unused_empty() {
        let graph = CodeGraph::new();
        let unused = find_unused_nodes(UnusedScope::All, &graph, 100);
        assert!(unused.is_empty());
    }

    #[test]
    fn test_public_function_is_entry_point() {
        // Public function should be reachable
        let nodes = [NodeOptions::new("public_func", NodeKind::Function).with_visibility("public")];
        let (graph, node_ids) = create_test_graph(&nodes, &[]);

        let reachable = compute_reachable_set_graph(&graph);
        assert!(
            reachable.contains(&node_ids[0]),
            "Public function should be reachable"
        );
    }

    #[test]
    fn test_main_function_is_entry_point() {
        // main function should be reachable regardless of visibility
        let nodes = [NodeOptions::new("main", NodeKind::Function)];
        let (graph, node_ids) = create_test_graph(&nodes, &[]);

        let reachable = compute_reachable_set_graph(&graph);
        assert!(
            reachable.contains(&node_ids[0]),
            "main function should be reachable"
        );
    }

    #[test]
    fn test_test_function_is_entry_point() {
        // test_ prefixed functions should be reachable
        let nodes = [
            NodeOptions::new("test_something", NodeKind::Function),
            NodeOptions::new("something_test", NodeKind::Function),
            NodeOptions::new("TestNode", NodeKind::Test),
        ];
        let (graph, node_ids) = create_test_graph(&nodes, &[]);

        let reachable = compute_reachable_set_graph(&graph);
        assert!(
            reachable.contains(&node_ids[0]),
            "test_ prefixed function should be reachable"
        );
        assert!(
            reachable.contains(&node_ids[1]),
            "_test suffixed function should be reachable"
        );
        assert!(
            reachable.contains(&node_ids[2]),
            "Test node kind should be reachable"
        );
    }

    #[test]
    fn test_export_is_entry_point() {
        let nodes = [NodeOptions::new("exported_value", NodeKind::Export)];
        let (graph, node_ids) = create_test_graph(&nodes, &[]);

        let reachable = compute_reachable_set_graph(&graph);
        assert!(
            reachable.contains(&node_ids[0]),
            "Export should be reachable"
        );
    }

    #[test]
    fn test_reachability_through_calls() {
        // Entry point -> A -> B (all should be reachable)
        let nodes = [
            NodeOptions::new("main", NodeKind::Function),
            NodeOptions::new("helper_a", NodeKind::Function),
            NodeOptions::new("helper_b", NodeKind::Function),
        ];
        let edges = [
            (
                0,
                1,
                EdgeKind::Calls {
                    argument_count: 0,
                    is_async: false,
                },
            ),
            (
                1,
                2,
                EdgeKind::Calls {
                    argument_count: 0,
                    is_async: false,
                },
            ),
        ];
        let (graph, node_ids) = create_test_graph(&nodes, &edges);

        let reachable = compute_reachable_set_graph(&graph);
        assert!(reachable.contains(&node_ids[0]), "main should be reachable");
        assert!(
            reachable.contains(&node_ids[1]),
            "helper_a should be reachable via call"
        );
        assert!(
            reachable.contains(&node_ids[2]),
            "helper_b should be reachable via transitive call"
        );
    }

    #[test]
    fn test_reachability_through_imports() {
        let nodes = [
            NodeOptions::new("main", NodeKind::Function),
            NodeOptions::new("imported_module", NodeKind::Module),
        ];
        let edges = [(
            0,
            1,
            EdgeKind::Imports {
                alias: None,
                is_wildcard: false,
            },
        )];
        let (graph, node_ids) = create_test_graph(&nodes, &edges);

        let reachable = compute_reachable_set_graph(&graph);
        assert!(
            reachable.contains(&node_ids[1]),
            "Imported module should be reachable"
        );
    }

    #[test]
    fn test_reachability_through_references() {
        let nodes = [
            NodeOptions::new("main", NodeKind::Function),
            NodeOptions::new("referenced_var", NodeKind::Variable),
        ];
        let edges = [(0, 1, EdgeKind::References)];
        let (graph, node_ids) = create_test_graph(&nodes, &edges);

        let reachable = compute_reachable_set_graph(&graph);
        assert!(
            reachable.contains(&node_ids[1]),
            "Referenced variable should be reachable"
        );
    }

    #[test]
    fn test_reachability_through_inheritance() {
        let nodes = [
            NodeOptions::new("main", NodeKind::Function),
            NodeOptions::new("BaseClass", NodeKind::Class).with_visibility("public"),
            NodeOptions::new("DerivedClass", NodeKind::Class),
        ];
        let edges = [(2, 1, EdgeKind::Inherits)];
        let (graph, node_ids) = create_test_graph(&nodes, &edges);

        let reachable = compute_reachable_set_graph(&graph);
        // BaseClass is public, so it's an entry point
        // DerivedClass inherits from BaseClass, making it reachable
        assert!(
            reachable.contains(&node_ids[1]),
            "BaseClass should be reachable (public)"
        );
        // Note: inheritance edge goes from derived -> base, so derived is not reachable
        // unless it's also called/used from an entry point
    }

    #[test]
    fn test_private_function_unreachable() {
        // Private function not called by anyone should be unused
        let nodes = [
            NodeOptions::new("main", NodeKind::Function),
            NodeOptions::new("private_helper", NodeKind::Function),
        ];
        let (graph, node_ids) = create_test_graph(&nodes, &[]);

        let reachable = compute_reachable_set_graph(&graph);
        assert!(
            !reachable.contains(&node_ids[1]),
            "Uncalled private function should be unreachable"
        );
    }

    #[test]
    fn test_is_node_unused_scope_all() {
        let nodes = [
            NodeOptions::new("main", NodeKind::Function),
            NodeOptions::new("unused_helper", NodeKind::Function),
        ];
        let (graph, node_ids) = create_test_graph(&nodes, &[]);

        assert!(
            !is_node_unused::<RandomState>(node_ids[0], UnusedScope::All, &graph, None),
            "main should not be unused"
        );
        assert!(
            is_node_unused::<RandomState>(node_ids[1], UnusedScope::All, &graph, None),
            "unused_helper should be unused"
        );
    }

    #[test]
    fn test_is_node_unused_scope_public() {
        let nodes = [
            NodeOptions::new("public_unused", NodeKind::Function).with_visibility("public"),
            NodeOptions::new("private_unused", NodeKind::Function),
        ];
        // Neither is called, but we check scope filtering
        let (graph, node_ids) = create_test_graph(&nodes, &[]);

        // Public scope - only checks public symbols
        // Note: public symbols are entry points, so they're reachable but scope still matches
        assert!(
            !is_node_unused::<RandomState>(node_ids[0], UnusedScope::Public, &graph, None),
            "Public function is entry point, not unused"
        );
        assert!(
            !is_node_unused::<RandomState>(node_ids[1], UnusedScope::Public, &graph, None),
            "Private function doesn't match Public scope"
        );
    }

    #[test]
    fn test_is_node_unused_scope_private() {
        let nodes = [
            NodeOptions::new("main", NodeKind::Function),
            NodeOptions::new("public_unused", NodeKind::Function).with_visibility("public"),
            NodeOptions::new("private_unused", NodeKind::Function),
        ];
        let (graph, node_ids) = create_test_graph(&nodes, &[]);

        // Private scope - only checks private symbols
        assert!(
            !is_node_unused::<RandomState>(node_ids[1], UnusedScope::Private, &graph, None),
            "Public function doesn't match Private scope"
        );
        assert!(
            is_node_unused::<RandomState>(node_ids[2], UnusedScope::Private, &graph, None),
            "Private unused function should be unused"
        );
    }

    #[test]
    fn test_is_node_unused_scope_function() {
        let nodes = [
            NodeOptions::new("main", NodeKind::Function),
            NodeOptions::new("unused_func", NodeKind::Function),
            NodeOptions::new("unused_struct", NodeKind::Struct),
        ];
        let (graph, node_ids) = create_test_graph(&nodes, &[]);

        // Function scope - only checks functions/methods
        assert!(
            is_node_unused::<RandomState>(node_ids[1], UnusedScope::Function, &graph, None),
            "Unused function should match Function scope"
        );
        assert!(
            !is_node_unused::<RandomState>(node_ids[2], UnusedScope::Function, &graph, None),
            "Struct doesn't match Function scope"
        );
    }

    #[test]
    fn test_is_node_unused_scope_struct() {
        let nodes = [
            NodeOptions::new("main", NodeKind::Function),
            NodeOptions::new("unused_struct", NodeKind::Struct),
            NodeOptions::new("unused_class", NodeKind::Class),
            NodeOptions::new("unused_func", NodeKind::Function),
        ];
        let (graph, node_ids) = create_test_graph(&nodes, &[]);

        // Struct scope - only checks structs/classes
        assert!(
            is_node_unused::<RandomState>(node_ids[1], UnusedScope::Struct, &graph, None),
            "Unused struct should match Struct scope"
        );
        assert!(
            is_node_unused::<RandomState>(node_ids[2], UnusedScope::Struct, &graph, None),
            "Unused class should match Struct scope"
        );
        assert!(
            !is_node_unused::<RandomState>(node_ids[3], UnusedScope::Struct, &graph, None),
            "Function doesn't match Struct scope"
        );
    }

    #[test]
    fn test_find_unused_nodes_basic() {
        let nodes = [
            NodeOptions::new("main", NodeKind::Function),
            NodeOptions::new("used_helper", NodeKind::Function),
            NodeOptions::new("unused_helper", NodeKind::Function),
        ];
        let edges = [(
            0,
            1,
            EdgeKind::Calls {
                argument_count: 0,
                is_async: false,
            },
        )];
        let (graph, node_ids) = create_test_graph(&nodes, &edges);

        let unused = find_unused_nodes(UnusedScope::All, &graph, 100);
        assert_eq!(unused.len(), 1);
        assert!(unused.contains(&node_ids[2]));
    }

    #[test]
    fn test_find_unused_nodes_max_results() {
        // Create many unused nodes and verify limit is respected
        let mut nodes = vec![NodeOptions::new("main", NodeKind::Function)];
        for i in 0..10 {
            nodes.push(NodeOptions::new(
                Box::leak(format!("unused_{i}").into_boxed_str()),
                NodeKind::Function,
            ));
        }
        let (graph, _) = create_test_graph(&nodes, &[]);

        let unused = find_unused_nodes(UnusedScope::All, &graph, 3);
        assert_eq!(unused.len(), 3, "Should respect max_results limit");
    }

    #[test]
    fn test_entry_point_not_unused() {
        // Entry points should never be marked as unused
        let nodes = [
            NodeOptions::new("main", NodeKind::Function),
            NodeOptions::new("test_something", NodeKind::Function),
            NodeOptions::new("exported", NodeKind::Export),
        ];
        let (graph, node_ids) = create_test_graph(&nodes, &[]);

        for &node_id in &node_ids {
            assert!(
                !is_node_unused::<RandomState>(node_id, UnusedScope::All, &graph, None),
                "Entry points should not be unused"
            );
        }
    }

    #[test]
    fn test_precomputed_reachable_set() {
        // Test using a pre-computed reachable set for efficiency
        let nodes = [
            NodeOptions::new("main", NodeKind::Function),
            NodeOptions::new("unused", NodeKind::Function),
        ];
        let (graph, node_ids) = create_test_graph(&nodes, &[]);

        let reachable = compute_reachable_set_graph(&graph);
        assert!(
            is_node_unused::<RandomState>(node_ids[1], UnusedScope::All, &graph, Some(&reachable)),
            "Should work with precomputed reachable set"
        );
    }

    #[test]
    fn test_invalid_node_not_unused() {
        let graph = CodeGraph::new();
        let invalid_node = NodeId::new(999, 0);

        assert!(
            !is_node_unused::<RandomState>(invalid_node, UnusedScope::All, &graph, None),
            "Invalid node should return false (not unused)"
        );
    }

    #[test]
    fn test_typeof_edge_reachability() {
        let nodes = [
            NodeOptions::new("main", NodeKind::Function),
            NodeOptions::new("MyType", NodeKind::Type),
        ];
        let edges = [(
            0,
            1,
            EdgeKind::TypeOf {
                context: None,
                index: None,
                name: None,
            },
        )];
        let (graph, node_ids) = create_test_graph(&nodes, &edges);

        let reachable = compute_reachable_set_graph(&graph);
        assert!(
            reachable.contains(&node_ids[1]),
            "TypeOf edge should make type reachable"
        );
    }

    #[test]
    fn test_implements_edge_reachability() {
        let nodes = [
            NodeOptions::new("MyClass", NodeKind::Class).with_visibility("public"),
            NodeOptions::new("MyInterface", NodeKind::Interface),
        ];
        let edges = [(0, 1, EdgeKind::Implements)];
        let (graph, node_ids) = create_test_graph(&nodes, &edges);

        let reachable = compute_reachable_set_graph(&graph);
        // MyClass is public (entry point), and implements MyInterface
        assert!(
            reachable.contains(&node_ids[1]),
            "Implements edge should make interface reachable"
        );
    }

    #[test]
    fn test_pub_visibility_variant() {
        // Test "pub" (Rust style) vs "public" visibility
        let nodes = [NodeOptions::new("rust_public", NodeKind::Function).with_visibility("pub")];
        let (graph, node_ids) = create_test_graph(&nodes, &[]);

        let reachable = compute_reachable_set_graph(&graph);
        assert!(
            reachable.contains(&node_ids[0]),
            "'pub' visibility should be recognized as public"
        );
    }
}