sqry-lang-ruby 12.0.3

Ruby language plugin for sqry
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
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
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
//! Integration tests for `TypeOf` and Reference edge creation from YARD annotations
//!
//! Tests validate that edges are created for correct YARD patterns and include
//! nested namespace tests to verify full qualified name handling (Issue #1 fix).

use sqry_core::graph::unified::build::staging::StagingGraph;
use sqry_core::graph::unified::build::test_helpers::collect_edges_by_kind;
use sqry_core::graph::{GraphBuilder, Language};
use sqry_lang_ruby::RubyGraphBuilder;
use std::path::Path;
use tree_sitter::Parser;

fn parse_ruby(source: &str) -> tree_sitter::Tree {
    let mut parser = Parser::new();
    parser
        .set_language(&tree_sitter_ruby::LANGUAGE.into())
        .expect("error loading Ruby grammar");
    parser.parse(source, None).expect("ruby parse failed")
}

/// Helper to build graph from Ruby source
fn build_graph_from_source(source: &str) -> StagingGraph {
    let builder = RubyGraphBuilder::default();
    let path = Path::new("test.rb");
    let tree = parse_ruby(source);
    let mut staging = StagingGraph::new();
    builder
        .build_graph(&tree, source.as_bytes(), path, &mut staging)
        .expect("Failed to build graph");
    staging
}

/// Helper to get the canonical node identity from staging operations.
fn get_node_canonical_name(
    staging: &StagingGraph,
    node_id: sqry_core::graph::unified::NodeId,
) -> Option<String> {
    use sqry_core::graph::unified::build::staging::StagingOp;

    for op in staging.operations() {
        if let StagingOp::AddNode {
            entry,
            expected_id: Some(id),
        } = op
            && *id == node_id
        {
            return staging
                .resolve_node_canonical_name(entry)
                .map(std::borrow::ToOwned::to_owned);
        }
    }
    None
}

/// Helper to get the Ruby-native display identity from staging operations.
fn get_node_display_name(
    staging: &StagingGraph,
    node_id: sqry_core::graph::unified::NodeId,
) -> Option<String> {
    use sqry_core::graph::unified::build::staging::StagingOp;

    for op in staging.operations() {
        if let StagingOp::AddNode {
            entry,
            expected_id: Some(id),
        } = op
            && *id == node_id
        {
            return staging.resolve_node_display_name(Language::Ruby, entry);
        }
    }
    None
}

/// Assert that an edge exists from a node with the given canonical qualified name.
fn assert_edge_from_node(staging: &StagingGraph, edge_kind: &str, expected_from: &str) -> bool {
    use sqry_core::graph::unified::build::staging::StagingOp;
    use sqry_core::graph::unified::edge::EdgeKind;

    for op in staging.operations() {
        if let StagingOp::AddEdge {
            source,
            target: _,
            kind,
            ..
        } = op
        {
            let matches_kind = matches!(
                (edge_kind, kind),
                ("TypeOf", EdgeKind::TypeOf { .. }) | ("References", EdgeKind::References)
            );

            if matches_kind
                && let Some(from_name) = get_node_canonical_name(staging, *source)
                && from_name == expected_from
            {
                return true;
            }
        }
    }
    false
}

/// Assert that an edge exists from a node with the given Ruby-native display name.
fn assert_edge_from_node_display(
    staging: &StagingGraph,
    edge_kind: &str,
    expected_from: &str,
) -> bool {
    use sqry_core::graph::unified::build::staging::StagingOp;
    use sqry_core::graph::unified::edge::EdgeKind;

    for op in staging.operations() {
        if let StagingOp::AddEdge {
            source,
            target: _,
            kind,
            ..
        } = op
        {
            let matches_kind = matches!(
                (edge_kind, kind),
                ("TypeOf", EdgeKind::TypeOf { .. }) | ("References", EdgeKind::References)
            );

            if matches_kind
                && let Some(from_name) = get_node_display_name(staging, *source)
                && from_name == expected_from
            {
                return true;
            }
        }
    }
    false
}

/// Count edges originating from a specific node by canonical qualified name.
fn count_edges_from_node(staging: &StagingGraph, edge_kind: &str, from_node: &str) -> usize {
    use sqry_core::graph::unified::build::staging::StagingOp;
    use sqry_core::graph::unified::edge::EdgeKind;

    let mut count = 0;
    for op in staging.operations() {
        if let StagingOp::AddEdge {
            source,
            target: _,
            kind,
            ..
        } = op
        {
            let matches_kind = matches!(
                (edge_kind, kind),
                ("TypeOf", EdgeKind::TypeOf { .. }) | ("References", EdgeKind::References)
            );

            if matches_kind
                && let Some(from_name) = get_node_canonical_name(staging, *source)
                && from_name == from_node
            {
                count += 1;
            }
        }
    }
    count
}

// ============================================================================
// Method @param Tests
// ============================================================================

#[test]
fn test_method_param_simple_type() {
    let source = r#"
class User
  # @param [String] name The user's name
  def greet(name)
    puts "Hello, #{name}"
  end
end
"#;

    let staging = build_graph_from_source(source);

    // Check that TypeOf edges were created
    let typeof_edges = collect_edges_by_kind(&staging, "TypeOf");
    assert!(
        !typeof_edges.is_empty(),
        "Method should have TypeOf edges from @param"
    );

    // Check that References edges were created
    let ref_edges = collect_edges_by_kind(&staging, "References");
    assert!(!ref_edges.is_empty(), "Method should have References edges");
}

#[test]
fn test_method_param_multiple_params() {
    let source = r"
class User
  # @param [String] first_name
  # @param [String] last_name
  # @param [Integer] age
  def create(first_name, last_name, age)
  end
end
";

    let staging = build_graph_from_source(source);

    // Check TypeOf edges created for all parameters
    let typeof_edges = collect_edges_by_kind(&staging, "TypeOf");
    assert!(
        typeof_edges.len() >= 3,
        "Method should have TypeOf edges for all 3 parameters"
    );
}

#[test]
fn test_method_param_custom_type() {
    let source = r"
class Service
  # @param [User] user The user object
  def process(user)
  end
end
";

    let staging = build_graph_from_source(source);

    // Check References edges for custom types
    let ref_edges = collect_edges_by_kind(&staging, "References");
    assert!(
        !ref_edges.is_empty(),
        "Method should have References edges for custom types"
    );
}

#[test]
fn test_method_param_union_type() {
    let source = r"
class Parser
  # @param [String, Integer] value
  def parse(value)
  end
end
";

    let staging = build_graph_from_source(source);

    // Check References edges for both union types
    let ref_edges = collect_edges_by_kind(&staging, "References");
    assert!(
        ref_edges.len() >= 2,
        "Method should have References edges for both union types"
    );
}

#[test]
fn test_method_param_nullable_type() {
    let source = r"
class Service
  # @param [String, nil] name
  def greet(name)
  end
end
";

    let staging = build_graph_from_source(source);

    // Check TypeOf edge (nil should be stripped)
    let typeof_edges = collect_edges_by_kind(&staging, "TypeOf");
    assert!(
        !typeof_edges.is_empty(),
        "Method should have TypeOf edge with nil stripped"
    );
}

// ============================================================================
// Method @return Tests
// ============================================================================

#[test]
fn test_method_return_simple_type() {
    let source = r"
class User
  # @return [String] The user's name
  def name
    @name
  end
end
";

    let staging = build_graph_from_source(source);

    // Check TypeOf edge for return type
    let typeof_edges = collect_edges_by_kind(&staging, "TypeOf");
    assert!(
        !typeof_edges.is_empty(),
        "Method should have TypeOf edge for return type"
    );
}

#[test]
fn test_method_return_custom_type() {
    let source = r"
class UserFactory
  # @return [User] The created user
  def create
    User.new
  end
end
";

    let staging = build_graph_from_source(source);

    // Check References edge for custom return type
    let ref_edges = collect_edges_by_kind(&staging, "References");
    assert!(
        !ref_edges.is_empty(),
        "Method should have References edge for custom return type"
    );
}

#[test]
fn test_method_return_array_type() {
    let source = r"
class Repository
  # @return [Array<User>] List of users
  def all
    User.all
  end
end
";

    let staging = build_graph_from_source(source);

    // Check References edges for Array and User
    let ref_edges = collect_edges_by_kind(&staging, "References");
    assert!(
        ref_edges.len() >= 2,
        "Method should have References edges for Array and User"
    );
}

#[test]
fn test_method_return_hash_type() {
    let source = r"
class Config
  # @return [Hash{String => Integer}] Configuration mapping
  def settings
    { timeout: 30, retries: 3 }
  end
end
";

    let staging = build_graph_from_source(source);

    // Check References edges for Hash, String, and Integer
    let ref_edges = collect_edges_by_kind(&staging, "References");
    assert!(
        ref_edges.len() >= 3,
        "Method should have References edges for Hash, String, and Integer"
    );
}

#[test]
fn test_method_return_nullable_type() {
    let source = r"
class Finder
  # @return [User, nil] User or nil if not found
  def find(id)
  end
end
";

    let staging = build_graph_from_source(source);

    // Check that edges are created (nil should be excluded from References)
    let typeof_edges = collect_edges_by_kind(&staging, "TypeOf");
    assert!(
        !typeof_edges.is_empty(),
        "Method should have TypeOf edge with nil stripped"
    );
}

// ============================================================================
// Singleton Method Tests
// ============================================================================

#[test]
fn test_singleton_method_param() {
    let source = r"
class User
  # @param [String] name
  # @return [User]
  def self.create(name)
    new(name)
  end
end
";

    let staging = build_graph_from_source(source);

    // Check TypeOf edges for singleton method
    let typeof_edges = collect_edges_by_kind(&staging, "TypeOf");
    assert!(
        typeof_edges.len() >= 2,
        "Singleton method should have TypeOf edges for param and return"
    );
}

#[test]
fn test_singleton_method_return() {
    let source = r"
class Config
  # @return [Hash{Symbol => String}]
  def self.defaults
    { timeout: '30s' }
  end
end
";

    let staging = build_graph_from_source(source);

    // Check References edges for all types in the hash
    let ref_edges = collect_edges_by_kind(&staging, "References");
    assert!(
        ref_edges.len() >= 3,
        "Singleton method should have References edges for Hash, Symbol, and String"
    );
}

#[test]
fn test_singleton_method_multiple_params() {
    let source = r"
class Builder
  # @param [String] name
  # @param [Integer] age
  # @param [Boolean] active
  # @return [User]
  def self.build(name, age, active)
  end
end
";

    let staging = build_graph_from_source(source);

    // Check that multiple TypeOf edges were created
    let typeof_edges = collect_edges_by_kind(&staging, "TypeOf");
    assert!(
        typeof_edges.len() >= 4,
        "Singleton method should have TypeOf edges for all params and return"
    );
}

// ============================================================================
// attr_reader/attr_writer/attr_accessor Tests
// ============================================================================

#[test]
fn test_attr_reader_single() {
    let source = r"
class User
  # @return [String]
  attr_reader :name
end
";

    let staging = build_graph_from_source(source);

    // Check TypeOf edge for attr_reader
    let typeof_edges = collect_edges_by_kind(&staging, "TypeOf");
    assert!(
        !typeof_edges.is_empty(),
        "attr_reader should create TypeOf edge"
    );
}

#[test]
fn test_attr_reader_multiple() {
    let source = r"
class User
  # @return [String]
  attr_reader :first_name, :last_name
end
";

    let staging = build_graph_from_source(source);

    // Check TypeOf edges for both attributes
    let typeof_edges = collect_edges_by_kind(&staging, "TypeOf");
    assert!(
        typeof_edges.len() >= 2,
        "attr_reader should create TypeOf edges for multiple attributes"
    );
}

#[test]
fn test_attr_writer_custom_type() {
    let source = r"
class Service
  # @return [Logger]
  attr_writer :logger
end
";

    let staging = build_graph_from_source(source);

    // Check References edge for custom type
    let ref_edges = collect_edges_by_kind(&staging, "References");
    assert!(
        !ref_edges.is_empty(),
        "attr_writer should create References edge for custom type"
    );
}

#[test]
fn test_attr_accessor_array_type() {
    let source = r"
class Repository
  # @return [Array<User>]
  attr_accessor :users
end
";

    let staging = build_graph_from_source(source);

    // Check References edges for Array and User
    let ref_edges = collect_edges_by_kind(&staging, "References");
    assert!(
        ref_edges.len() >= 2,
        "attr_accessor should create References edges for Array and User"
    );
}

#[test]
fn test_attr_accessor_hash_type() {
    let source = r"
class Cache
  # @return [Hash{String => Object}]
  attr_accessor :data
end
";

    let staging = build_graph_from_source(source);

    // Check References edges for Hash, String, and Object
    let ref_edges = collect_edges_by_kind(&staging, "References");
    assert!(
        ref_edges.len() >= 3,
        "attr_accessor should create References edges for Hash, String, and Object"
    );
}

// ============================================================================
// attr_* with string arguments (Issue #3 fix)
// ============================================================================

#[test]
fn test_attr_reader_string_argument() {
    let source = r#"
class User
  # @return [String]
  attr_reader "username"
end
"#;

    let staging = build_graph_from_source(source);

    // Validate TypeOf edge originates from the canonical attr node and still
    // displays in Ruby-native form.
    let qualified_attr = "User::username";
    let display_attr = "User#username";
    assert!(
        assert_edge_from_node(&staging, "TypeOf", qualified_attr),
        "TypeOf edge should originate from attr with string argument: {qualified_attr}"
    );
    assert!(
        assert_edge_from_node_display(&staging, "TypeOf", display_attr),
        "TypeOf edge should display using native Ruby attr syntax: {display_attr}"
    );

    let ref_edges = collect_edges_by_kind(&staging, "References");
    assert!(
        !ref_edges.is_empty(),
        "attr_reader should have References edge for String type"
    );
}

#[test]
fn test_attr_accessor_command_call() {
    let source = r"
class Service
  # @return [Logger]
  self.attr_accessor :logger
end
";

    let staging = build_graph_from_source(source);

    // Validate TypeOf edge originates from the canonical attr node and still
    // displays in Ruby-native form.
    let qualified_attr = "Service::logger";
    let display_attr = "Service#logger";
    assert!(
        assert_edge_from_node(&staging, "TypeOf", qualified_attr),
        "TypeOf edge should originate from attr with command_call form: {qualified_attr}"
    );
    assert!(
        assert_edge_from_node_display(&staging, "TypeOf", display_attr),
        "TypeOf edge should display using native Ruby attr syntax: {display_attr}"
    );

    let ref_edges = collect_edges_by_kind(&staging, "References");
    assert!(
        !ref_edges.is_empty(),
        "attr_accessor should have References edge for Logger type"
    );
}

// ============================================================================
// Instance Variable @type Tests
// ============================================================================

#[test]
fn test_instance_variable_type() {
    let source = r#"
class User
  def initialize
    # @type [String]
    @name = "John"
  end
end
"#;

    let staging = build_graph_from_source(source);

    // Check TypeOf edge for instance variable
    let typeof_edges = collect_edges_by_kind(&staging, "TypeOf");
    assert!(
        !typeof_edges.is_empty(),
        "Instance variable should have TypeOf edge from @type"
    );
}

#[test]
fn test_instance_variable_custom_type() {
    let source = r"
class Service
  def setup
    # @type [Logger]
    @logger = Logger.new
  end
end
";

    let staging = build_graph_from_source(source);

    // Check References edge for custom type
    let ref_edges = collect_edges_by_kind(&staging, "References");
    assert!(
        !ref_edges.is_empty(),
        "Instance variable should have References edge for custom type"
    );
}

#[test]
fn test_instance_variable_array_type() {
    let source = r"
class Repository
  def initialize
    # @type [Array<User>]
    @users = []
  end
end
";

    let staging = build_graph_from_source(source);

    // Check References edges for Array and User
    let ref_edges = collect_edges_by_kind(&staging, "References");
    assert!(
        ref_edges.len() >= 2,
        "Instance variable should have References edges for Array and User"
    );
}

#[test]
fn test_instance_variable_hash_type() {
    let source = r"
class Config
  def initialize
    # @type [Hash{Symbol => String}]
    @settings = {}
  end
end
";

    let staging = build_graph_from_source(source);

    // Check References edges for Hash, Symbol, and String
    let ref_edges = collect_edges_by_kind(&staging, "References");
    assert!(
        ref_edges.len() >= 3,
        "Instance variable should have References edges for Hash, Symbol, and String"
    );
}

#[test]
fn test_instance_variable_nullable_type() {
    let source = r"
class Finder
  def initialize
    # @type [User, nil]
    @cached_user = nil
  end
end
";

    let staging = build_graph_from_source(source);

    // Check that edges are created (nil should be excluded)
    let typeof_edges = collect_edges_by_kind(&staging, "TypeOf");
    assert!(
        !typeof_edges.is_empty(),
        "Instance variable should have TypeOf edge with nil stripped"
    );
}

// ============================================================================
// Complex Type Tests
// ============================================================================

#[test]
fn test_complex_generic_type() {
    let source = r"
class Service
  # @param [Collection<Result<Data>>] results
  def process(results)
  end
end
";

    let staging = build_graph_from_source(source);

    // Check References edges for all nested types
    let ref_edges = collect_edges_by_kind(&staging, "References");
    assert!(
        ref_edges.len() >= 3,
        "Method should have References edges for Collection, Result, and Data"
    );
}

#[test]
fn test_complex_union_with_generics() {
    let source = r"
class Parser
  # @param [Array<String>, Hash{Symbol => Integer}] value
  def parse(value)
  end
end
";

    let staging = build_graph_from_source(source);

    // Check References edges for all types
    let ref_edges = collect_edges_by_kind(&staging, "References");
    assert!(
        ref_edges.len() >= 5,
        "Method should have References edges for Array, String, Hash, Symbol, and Integer"
    );
}

#[test]
fn test_qualified_type_names() {
    let source = r"
class Service
  # @param [App::Models::User] user
  # @return [App::Services::Logger]
  def log_action(user)
  end
end
";

    let staging = build_graph_from_source(source);

    // Check References edges for all namespace components
    let ref_edges = collect_edges_by_kind(&staging, "References");
    assert!(
        !ref_edges.is_empty(),
        "Method should have References edges for all namespace components"
    );
}

#[test]
fn test_multiple_annotations_on_method() {
    let source = r"
class UserService
  # @param [String] first_name
  # @param [String] last_name
  # @param [Integer] age
  # @param [Hash{Symbol => String}] metadata
  # @return [User]
  def create_user(first_name, last_name, age, metadata)
  end
end
";

    let staging = build_graph_from_source(source);

    // Check multiple TypeOf edges
    let typeof_edges = collect_edges_by_kind(&staging, "TypeOf");
    assert!(
        typeof_edges.len() >= 5,
        "Method should have TypeOf edges for all params and return"
    );

    // Check multiple References edges
    let ref_edges = collect_edges_by_kind(&staging, "References");
    assert!(
        !ref_edges.is_empty(),
        "Method should have References edges for all types"
    );
}

// ============================================================================
// Nested Namespace Tests (Issue #1 fix - CRITICAL)
// ============================================================================

#[test]
fn test_nested_module_class_method() {
    let source = r"
module MyModule
  class MyClass
    # @param [String] value
    # @return [Integer]
    def process(value)
    end
  end
end
";

    let staging = build_graph_from_source(source);

    // CRITICAL: This test validates that YARD edges use full canonical names
    // in the graph and still display as native Ruby method names.
    let qualified_method = "MyModule::MyClass::process";
    let display_method = "MyModule::MyClass#process";
    assert!(
        assert_edge_from_node(&staging, "TypeOf", qualified_method),
        "TypeOf edge should originate from fully-qualified method: {qualified_method}"
    );
    assert!(
        assert_edge_from_node_display(&staging, "TypeOf", display_method),
        "TypeOf edge should display with native Ruby method syntax: {display_method}"
    );

    // Validate edge count for the qualified method
    let typeof_count = count_edges_from_node(&staging, "TypeOf", qualified_method);
    assert!(
        typeof_count >= 2,
        "Method should have 2+ TypeOf edges (param + return), found {typeof_count}"
    );

    // Validate References edges exist
    let ref_edges = collect_edges_by_kind(&staging, "References");
    assert!(
        ref_edges.len() >= 2,
        "Method should have References edges for String and Integer types"
    );
}

#[test]
fn test_nested_module_singleton_method() {
    let source = r"
module Outer
  module Inner
    class Service
      # @param [User] user
      # @return [Result]
      def self.process(user)
      end
    end
  end
end
";

    let staging = build_graph_from_source(source);

    // CRITICAL: Validates full canonical qualification for nested singleton
    // methods and Ruby-native display formatting.
    let qualified_method = "Outer::Inner::Service::process";
    let display_method = "Outer::Inner::Service.process";

    assert!(
        assert_edge_from_node(&staging, "TypeOf", qualified_method),
        "TypeOf edge should originate from fully-qualified singleton method: {qualified_method}"
    );
    assert!(
        assert_edge_from_node_display(&staging, "TypeOf", display_method),
        "TypeOf edge should display with native Ruby singleton syntax: {display_method}"
    );

    let typeof_count = count_edges_from_node(&staging, "TypeOf", qualified_method);
    assert!(
        typeof_count >= 2,
        "Singleton method should have 2+ TypeOf edges (param + return), found {typeof_count}"
    );

    let ref_edges = collect_edges_by_kind(&staging, "References");
    assert!(
        ref_edges.len() >= 2,
        "Method should have References edges for User and Result types"
    );
}

#[test]
fn test_nested_module_attr() {
    let source = r"
module App
  module Models
    class User
      # @return [String]
      attr_reader :username
    end
  end
end
";

    let staging = build_graph_from_source(source);

    // CRITICAL: Validates full canonical qualification for nested attr_* and
    // Ruby-native display formatting.
    let qualified_attr = "App::Models::User::username";
    let display_attr = "App::Models::User#username";

    assert!(
        assert_edge_from_node(&staging, "TypeOf", qualified_attr),
        "TypeOf edge should originate from fully-qualified attr: {qualified_attr}"
    );
    assert!(
        assert_edge_from_node_display(&staging, "TypeOf", display_attr),
        "TypeOf edge should display as fully-qualified Ruby attr: {display_attr}"
    );

    let ref_edges = collect_edges_by_kind(&staging, "References");
    assert!(
        !ref_edges.is_empty(),
        "Nested attr should have References edge for String type"
    );
}

#[test]
fn test_nested_module_instance_variable() {
    let source = r"
module Services
  class Cache
    def initialize
      # @type [Hash{String => Object}]
      @data = {}
    end
  end
end
";

    let staging = build_graph_from_source(source);

    // CRITICAL: Validates full canonical qualification for nested instance
    // variables and Ruby-native display formatting.
    let qualified_var = "Services::Cache::@data";
    let display_var = "Services::Cache#@data";

    assert!(
        assert_edge_from_node(&staging, "TypeOf", qualified_var),
        "TypeOf edge should originate from fully-qualified instance variable: {qualified_var}"
    );
    assert!(
        assert_edge_from_node_display(&staging, "TypeOf", display_var),
        "TypeOf edge should display as fully-qualified Ruby instance variable: {display_var}"
    );

    let ref_edges = collect_edges_by_kind(&staging, "References");
    assert!(
        ref_edges.len() >= 3,
        "Nested instance variable should have References edges for Hash, String, Object"
    );
}

#[test]
fn test_absolute_constant_namespace() {
    let source = r"
module Outer
  class ::AbsoluteClass
    # @param [String] value
    def method(value)
    end
  end
end
";

    let staging = build_graph_from_source(source);

    // CRITICAL: Validates absolute constant handling (::AbsoluteClass).
    // Canonical graph identity must not include the enclosing `Outer::`
    // namespace, and Ruby display should still use `#`.
    let qualified_method = "AbsoluteClass::method";
    let display_method = "AbsoluteClass#method";

    assert!(
        assert_edge_from_node(&staging, "TypeOf", qualified_method),
        "TypeOf edge should use absolute constant name (no Outer:: prefix): {qualified_method}"
    );
    assert!(
        assert_edge_from_node_display(&staging, "TypeOf", display_method),
        "TypeOf edge should display with native Ruby method syntax: {display_method}"
    );
}

// ============================================================================
// Negative Tests (No False Positives)
// ============================================================================

#[test]
fn test_no_yard_comment_no_edges() {
    let source = r#"
class User
  def greet(name)
    puts "Hello, #{name}"
  end
end
"#;

    let staging = build_graph_from_source(source);

    // Should NOT create TypeOf edges without YARD comments
    let typeof_edges = collect_edges_by_kind(&staging, "TypeOf");
    assert!(
        typeof_edges.is_empty(),
        "Should not create TypeOf edges without YARD comments"
    );
}

#[test]
fn test_yard_comment_too_far_away() {
    let source = r#"
class User
  # @param [String] name


  def greet(name)
    puts "Hello, #{name}"
  end
end
"#;

    let staging = build_graph_from_source(source);

    // Should NOT create edges if YARD comment is > 1 blank line away
    let typeof_edges = collect_edges_by_kind(&staging, "TypeOf");
    assert!(
        typeof_edges.is_empty(),
        "Should not create TypeOf edges if YARD comment is too far away"
    );
}