sqry-lang-elixir 8.0.1

Elixir 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
//! Tests for `TypeOf` and Reference edges in Elixir language plugin.
//!
//! This test suite validates that the Elixir graph builder correctly extracts:
//! - `TypeOf` edges: Full type signatures from @spec annotations
//! - Reference edges: Individual type names from nested type expressions
//!
//! Test organization:
//! 1. Simple Types (5 tests) - basic type annotations
//! 2. Parameters (4 tests) - function parameter types
//! 3. Return Types (4 tests) - function return type annotations
//! 4. Module-Qualified Types (4 tests) - `String.t()`, `Enum.t()`, etc.
//! 5. Complex Types (3 tests) - tuples, unions, nested types
//! 6. Integration (3 tests) - multiple specs, mixed types
//! 7. Edge Cases (2 tests) - missing specs, malformed types

use sqry_core::graph::GraphBuilder;
use sqry_core::graph::Language;
use sqry_core::graph::unified::StagingGraph;
use sqry_core::graph::unified::StagingOp;
use sqry_core::graph::unified::edge::kind::{EdgeKind, TypeOfContext};
use sqry_lang_elixir::relations::ElixirGraphBuilder;
use std::path::Path;
use tree_sitter::Parser;

// ============================================================================
// Test Helpers
// ============================================================================

fn parse_elixir(source: &str) -> tree_sitter::Tree {
    let mut parser = Parser::new();
    let language = tree_sitter_elixir_sqry::language();
    parser
        .set_language(&language)
        .expect("Failed to set language");
    parser.parse(source, None).expect("Failed to parse")
}

fn build_test_graph(source: &str, filename: &str) -> StagingGraph {
    let tree = parse_elixir(source);
    let mut staging = StagingGraph::new();
    let builder = ElixirGraphBuilder::default();
    let path = Path::new(filename);

    builder
        .build_graph(&tree, source.as_bytes(), path, &mut staging)
        .expect("Failed to build graph");

    staging
}

fn collect_typeof_edges_by_context(
    staging: &StagingGraph,
    context: TypeOfContext,
) -> Vec<(String, String)> {
    let mut edges = Vec::new();

    // First collect edge source/target IDs
    let mut typeof_edges = Vec::new();
    for op in staging.operations() {
        if let StagingOp::AddEdge {
            kind:
                EdgeKind::TypeOf {
                    context: edge_context,
                    ..
                },
            source,
            target,
            ..
        } = op
            && edge_context == &Some(context)
        {
            typeof_edges.push((*source, *target));
        }
    }

    // Find node names for each edge
    for (source_id, target_id) in typeof_edges {
        let mut source_name = String::new();
        let mut target_name = String::new();

        for op in staging.operations() {
            if let StagingOp::AddNode {
                entry, expected_id, ..
            } = op
            {
                if let Some(exp_id) = expected_id
                    && *exp_id == source_id
                    && let Some(name) = staging.resolve_node_display_name(Language::Elixir, entry)
                {
                    source_name = name;
                }
                if let Some(exp_id) = expected_id
                    && *exp_id == target_id
                    && let Some(name) = staging.resolve_node_display_name(Language::Elixir, entry)
                {
                    target_name = name;
                }
            }
        }

        edges.push((source_name, target_name));
    }

    edges
}

fn collect_reference_edges(staging: &StagingGraph) -> Vec<(String, String)> {
    let mut edges = Vec::new();

    // First collect edge source/target IDs
    let mut reference_edges = Vec::new();
    for op in staging.operations() {
        if let StagingOp::AddEdge {
            kind: EdgeKind::References,
            source,
            target,
            ..
        } = op
        {
            reference_edges.push((*source, *target));
        }
    }

    // Find node names for each edge
    for (source_id, target_id) in reference_edges {
        let mut source_name = String::new();
        let mut target_name = String::new();

        for op in staging.operations() {
            if let StagingOp::AddNode {
                entry, expected_id, ..
            } = op
            {
                if let Some(exp_id) = expected_id
                    && *exp_id == source_id
                    && let Some(name) = staging.resolve_node_display_name(Language::Elixir, entry)
                {
                    source_name = name;
                }
                if let Some(exp_id) = expected_id
                    && *exp_id == target_id
                    && let Some(name) = staging.resolve_node_display_name(Language::Elixir, entry)
                {
                    target_name = name;
                }
            }
        }

        edges.push((source_name, target_name));
    }

    edges
}

// ============================================================================
// Category 1: Simple Types (5 tests)
// ============================================================================

#[test]
fn test_simple_builtin_type() {
    let source = r"
defmodule Test do
  @spec get_count() :: integer()
  def get_count, do: 42
end
";
    let staging = build_test_graph(source, "test.ex");

    // Should have TypeOf edge from get_count to integer()
    let return_edges = collect_typeof_edges_by_context(&staging, TypeOfContext::Return);
    assert!(
        return_edges
            .iter()
            .any(|(from, to)| from == "get_count" && to == "integer()"),
        "Expected TypeOf edge from get_count to integer(), got: {return_edges:?}"
    );

    // Should have Reference edge to integer
    let ref_edges = collect_reference_edges(&staging);
    assert!(
        ref_edges
            .iter()
            .any(|(from, to)| from == "get_count" && to == "integer"),
        "Expected Reference edge to integer, got: {ref_edges:?}"
    );
}

#[test]
fn test_atom_type() {
    let source = r"
defmodule Test do
  @spec get_status() :: atom()
  def get_status, do: :ok
end
";
    let staging = build_test_graph(source, "test.ex");

    let return_edges = collect_typeof_edges_by_context(&staging, TypeOfContext::Return);
    assert!(
        return_edges
            .iter()
            .any(|(from, to)| from == "get_status" && to == "atom()"),
        "Expected TypeOf edge to atom()"
    );
}

#[test]
fn test_boolean_type() {
    let source = r"
defmodule Test do
  @spec is_valid() :: boolean()
  def is_valid, do: true
end
";
    let staging = build_test_graph(source, "test.ex");

    let return_edges = collect_typeof_edges_by_context(&staging, TypeOfContext::Return);
    assert!(
        return_edges
            .iter()
            .any(|(from, to)| from == "is_valid" && to == "boolean()"),
        "Expected TypeOf edge to boolean()"
    );
}

#[test]
fn test_binary_type() {
    let source = r"
defmodule Test do
  @spec get_data() :: binary()
  def get_data, do: <<1, 2, 3>>
end
";
    let staging = build_test_graph(source, "test.ex");

    let return_edges = collect_typeof_edges_by_context(&staging, TypeOfContext::Return);
    assert!(
        return_edges
            .iter()
            .any(|(from, to)| from == "get_data" && to == "binary()"),
        "Expected TypeOf edge to binary()"
    );
}

#[test]
fn test_any_type() {
    let source = r"
defmodule Test do
  @spec get_value() :: any()
  def get_value, do: nil
end
";
    let staging = build_test_graph(source, "test.ex");

    let return_edges = collect_typeof_edges_by_context(&staging, TypeOfContext::Return);
    assert!(
        return_edges
            .iter()
            .any(|(from, to)| from == "get_value" && to == "any()"),
        "Expected TypeOf edge to any()"
    );
}

// ============================================================================
// Category 2: Parameters (4 tests)
// ============================================================================

#[test]
fn test_single_parameter() {
    let source = r#"
defmodule Test do
  @spec greet(String.t()) :: String.t()
  def greet(name), do: "Hello, #{name}"
end
"#;
    let staging = build_test_graph(source, "test.ex");

    // Check parameter TypeOf edge
    let param_edges = collect_typeof_edges_by_context(&staging, TypeOfContext::Parameter);
    assert!(
        param_edges
            .iter()
            .any(|(from, to)| from == "greet" && to == "String.t()"),
        "Expected TypeOf edge for parameter, got: {param_edges:?}"
    );

    // Check Reference edge to String
    let ref_edges = collect_reference_edges(&staging);
    assert!(
        ref_edges
            .iter()
            .any(|(from, to)| from == "greet" && to == "String"),
        "Expected Reference edge to String"
    );
}

#[test]
fn test_multiple_parameters() {
    let source = r"
defmodule Test do
  @spec add(integer(), integer()) :: integer()
  def add(a, b), do: a + b
end
";
    let staging = build_test_graph(source, "test.ex");

    let param_edges = collect_typeof_edges_by_context(&staging, TypeOfContext::Parameter);
    let add_params: Vec<_> = param_edges
        .iter()
        .filter(|(from, _)| from == "add")
        .collect();

    assert_eq!(
        add_params.len(),
        2,
        "Expected 2 parameter TypeOf edges, got: {add_params:?}"
    );
}

#[test]
fn test_mixed_parameter_types() {
    let source = r"
defmodule Test do
  @spec process(String.t(), integer(), atom()) :: any()
  def process(name, count, status), do: {name, count, status}
end
";
    let staging = build_test_graph(source, "test.ex");

    let param_edges = collect_typeof_edges_by_context(&staging, TypeOfContext::Parameter);
    let process_params: Vec<_> = param_edges
        .iter()
        .filter(|(from, _)| from == "process")
        .collect();

    assert_eq!(process_params.len(), 3, "Expected 3 parameter TypeOf edges");

    // Check Reference edges
    let ref_edges = collect_reference_edges(&staging);
    assert!(
        ref_edges
            .iter()
            .any(|(from, to)| from == "process" && to == "String"),
        "Expected Reference to String"
    );
    assert!(
        ref_edges
            .iter()
            .any(|(from, to)| from == "process" && to == "integer"),
        "Expected Reference to integer"
    );
    assert!(
        ref_edges
            .iter()
            .any(|(from, to)| from == "process" && to == "atom"),
        "Expected Reference to atom"
    );
}

#[test]
fn test_no_parameters() {
    let source = r"
defmodule Test do
  @spec get_value() :: integer()
  def get_value(), do: 42
end
";
    let staging = build_test_graph(source, "test.ex");

    let param_edges = collect_typeof_edges_by_context(&staging, TypeOfContext::Parameter);
    let value_params: Vec<_> = param_edges
        .iter()
        .filter(|(from, _)| from == "get_value")
        .collect();

    assert_eq!(
        value_params.len(),
        0,
        "Expected no parameter TypeOf edges for function with no parameters"
    );
}

// ============================================================================
// Category 3: Return Types (4 tests)
// ============================================================================

#[test]
fn test_simple_return_type() {
    let source = r#"
defmodule Test do
  @spec get_name() :: String.t()
  def get_name, do: "John"
end
"#;
    let staging = build_test_graph(source, "test.ex");

    let return_edges = collect_typeof_edges_by_context(&staging, TypeOfContext::Return);
    assert!(
        return_edges
            .iter()
            .any(|(from, to)| from == "get_name" && to == "String.t()"),
        "Expected TypeOf edge to String.t()"
    );
}

#[test]
fn test_tuple_return_type() {
    let source = r#"
defmodule Test do
  @spec create() :: {:ok, String.t()}
  def create, do: {:ok, "Created"}
end
"#;
    let staging = build_test_graph(source, "test.ex");

    let return_edges = collect_typeof_edges_by_context(&staging, TypeOfContext::Return);
    assert!(
        return_edges.iter().any(|(from, _to)| from == "create"),
        "Expected TypeOf edge for return type"
    );

    // Check Reference edge to String
    let ref_edges = collect_reference_edges(&staging);
    assert!(
        ref_edges
            .iter()
            .any(|(from, to)| from == "create" && to == "String"),
        "Expected Reference edge to String from tuple"
    );
}

#[test]
fn test_list_return_type() {
    let source = r#"
defmodule Test do
  @spec get_names() :: [String.t()]
  def get_names, do: ["Alice", "Bob"]
end
"#;
    let staging = build_test_graph(source, "test.ex");

    let return_edges = collect_typeof_edges_by_context(&staging, TypeOfContext::Return);
    assert!(
        return_edges.iter().any(|(from, _to)| from == "get_names"),
        "Expected TypeOf edge for return type"
    );

    // Check Reference edge to String
    let ref_edges = collect_reference_edges(&staging);
    assert!(
        ref_edges
            .iter()
            .any(|(from, to)| from == "get_names" && to == "String"),
        "Expected Reference edge to String from list"
    );
}

#[test]
fn test_union_return_type() {
    let source = r#"
defmodule Test do
  @spec fetch() :: String.t() | integer()
  def fetch, do: "data"
end
"#;
    let staging = build_test_graph(source, "test.ex");

    let return_edges = collect_typeof_edges_by_context(&staging, TypeOfContext::Return);
    assert!(
        return_edges.iter().any(|(from, _to)| from == "fetch"),
        "Expected TypeOf edge for return type"
    );

    // Check Reference edges to both types in union
    let ref_edges = collect_reference_edges(&staging);
    assert!(
        ref_edges
            .iter()
            .any(|(from, to)| from == "fetch" && to == "String"),
        "Expected Reference edge to String"
    );
    assert!(
        ref_edges
            .iter()
            .any(|(from, to)| from == "fetch" && to == "integer"),
        "Expected Reference edge to integer"
    );
}

// ============================================================================
// Category 4: Module-Qualified Types (4 tests)
// ============================================================================

#[test]
fn test_string_t_type() {
    let source = r#"
defmodule Test do
  @spec get_text() :: String.t()
  def get_text, do: "text"
end
"#;
    let staging = build_test_graph(source, "test.ex");

    let return_edges = collect_typeof_edges_by_context(&staging, TypeOfContext::Return);
    assert!(
        return_edges
            .iter()
            .any(|(from, to)| from == "get_text" && to == "String.t()"),
        "Expected TypeOf edge to String.t()"
    );

    let ref_edges = collect_reference_edges(&staging);
    assert!(
        ref_edges
            .iter()
            .any(|(from, to)| from == "get_text" && to == "String"),
        "Expected Reference edge to String module"
    );
}

#[test]
fn test_enum_t_type() {
    let source = r"
defmodule Test do
  @spec get_items() :: Enum.t()
  def get_items, do: [1, 2, 3]
end
";
    let staging = build_test_graph(source, "test.ex");

    let return_edges = collect_typeof_edges_by_context(&staging, TypeOfContext::Return);
    assert!(
        return_edges
            .iter()
            .any(|(from, to)| from == "get_items" && to == "Enum.t()"),
        "Expected TypeOf edge to Enum.t()"
    );

    let ref_edges = collect_reference_edges(&staging);
    assert!(
        ref_edges
            .iter()
            .any(|(from, to)| from == "get_items" && to == "Enum"),
        "Expected Reference edge to Enum module"
    );
}

#[test]
fn test_custom_module_type() {
    let source = r"
defmodule Test do
  @spec create_user() :: User.t()
  def create_user, do: %User{}
end
";
    let staging = build_test_graph(source, "test.ex");

    let return_edges = collect_typeof_edges_by_context(&staging, TypeOfContext::Return);
    assert!(
        return_edges
            .iter()
            .any(|(from, to)| from == "create_user" && to == "User.t()"),
        "Expected TypeOf edge to User.t()"
    );

    let ref_edges = collect_reference_edges(&staging);
    assert!(
        ref_edges
            .iter()
            .any(|(from, to)| from == "create_user" && to == "User"),
        "Expected Reference edge to User module"
    );
}

#[test]
fn test_nested_module_type() {
    let source = r"
defmodule Test do
  @spec get_config() :: App.Config.t()
  def get_config, do: %App.Config{}
end
";
    let staging = build_test_graph(source, "test.ex");

    let return_edges = collect_typeof_edges_by_context(&staging, TypeOfContext::Return);
    assert!(
        return_edges.iter().any(|(from, _to)| from == "get_config"),
        "Expected TypeOf edge for return type"
    );

    // Note: For nested modules like App.Config.t(), the extraction produces
    // the full qualified name "App.Config" as the reference.
    let ref_edges = collect_reference_edges(&staging);
    assert!(
        ref_edges
            .iter()
            .any(|(from, to)| from == "get_config" && to.contains("Config")),
        "Expected Reference edge containing Config module, got: {ref_edges:?}"
    );
}

// ============================================================================
// Category 5: Complex Types (3 tests)
// ============================================================================

#[test]
fn test_nested_tuple_types() {
    let source = r#"
defmodule Test do
  @spec process() :: {:ok, {String.t(), integer()}}
  def process, do: {:ok, {"data", 42}}
end
"#;
    let staging = build_test_graph(source, "test.ex");

    let return_edges = collect_typeof_edges_by_context(&staging, TypeOfContext::Return);
    assert!(
        return_edges.iter().any(|(from, _to)| from == "process"),
        "Expected TypeOf edge for return type"
    );

    let ref_edges = collect_reference_edges(&staging);
    assert!(
        ref_edges
            .iter()
            .any(|(from, to)| from == "process" && to == "String"),
        "Expected Reference to String in nested tuple"
    );
    assert!(
        ref_edges
            .iter()
            .any(|(from, to)| from == "process" && to == "integer"),
        "Expected Reference to integer in nested tuple"
    );
}

#[test]
fn test_map_type() {
    let source = r#"
defmodule Test do
  @spec get_map() :: %{name: String.t(), age: integer()}
  def get_map, do: %{name: "John", age: 30}
end
"#;
    let staging = build_test_graph(source, "test.ex");

    let return_edges = collect_typeof_edges_by_context(&staging, TypeOfContext::Return);
    assert!(
        return_edges.iter().any(|(from, _to)| from == "get_map"),
        "Expected TypeOf edge for return type"
    );

    let ref_edges = collect_reference_edges(&staging);
    assert!(
        ref_edges
            .iter()
            .any(|(from, to)| from == "get_map" && to == "String"),
        "Expected Reference to String in map"
    );
    assert!(
        ref_edges
            .iter()
            .any(|(from, to)| from == "get_map" && to == "integer"),
        "Expected Reference to integer in map"
    );
}

#[test]
fn test_multiple_union_types() {
    let source = r"
defmodule Test do
  @spec get_value() :: String.t() | integer() | atom()
  def get_value, do: :ok
end
";
    let staging = build_test_graph(source, "test.ex");

    let return_edges = collect_typeof_edges_by_context(&staging, TypeOfContext::Return);
    assert!(
        return_edges.iter().any(|(from, _to)| from == "get_value"),
        "Expected TypeOf edge for return type"
    );

    let ref_edges = collect_reference_edges(&staging);
    assert!(
        ref_edges
            .iter()
            .any(|(from, to)| from == "get_value" && to == "String"),
        "Expected Reference to String"
    );
    assert!(
        ref_edges
            .iter()
            .any(|(from, to)| from == "get_value" && to == "integer"),
        "Expected Reference to integer"
    );
    assert!(
        ref_edges
            .iter()
            .any(|(from, to)| from == "get_value" && to == "atom"),
        "Expected Reference to atom"
    );
}

// ============================================================================
// Category 6: Integration (3 tests)
// ============================================================================

#[test]
fn test_multiple_specs_in_module() {
    let source = r#"
defmodule Test do
  @spec get_name() :: String.t()
  def get_name, do: "John"

  @spec get_age() :: integer()
  def get_age, do: 30
end
"#;
    let staging = build_test_graph(source, "test.ex");

    let return_edges = collect_typeof_edges_by_context(&staging, TypeOfContext::Return);
    assert!(
        return_edges
            .iter()
            .any(|(from, to)| from == "get_name" && to == "String.t()"),
        "Expected TypeOf edge for get_name"
    );
    assert!(
        return_edges
            .iter()
            .any(|(from, to)| from == "get_age" && to == "integer()"),
        "Expected TypeOf edge for get_age"
    );
}

#[test]
fn test_spec_with_params_and_return() {
    let source = r"
defmodule Test do
  @spec create_user(String.t(), integer()) :: User.t()
  def create_user(name, age), do: %User{name: name, age: age}
end
";
    let staging = build_test_graph(source, "test.ex");

    // Check parameter TypeOf edges
    let param_edges = collect_typeof_edges_by_context(&staging, TypeOfContext::Parameter);
    let create_params: Vec<_> = param_edges
        .iter()
        .filter(|(from, _)| from == "create_user")
        .collect();
    assert_eq!(create_params.len(), 2, "Expected 2 parameter TypeOf edges");

    // Check return TypeOf edge
    let return_edges = collect_typeof_edges_by_context(&staging, TypeOfContext::Return);
    assert!(
        return_edges
            .iter()
            .any(|(from, to)| from == "create_user" && to == "User.t()"),
        "Expected TypeOf edge for return type"
    );

    // Check all Reference edges
    let ref_edges = collect_reference_edges(&staging);
    assert!(
        ref_edges
            .iter()
            .any(|(from, to)| from == "create_user" && to == "String"),
        "Expected Reference to String"
    );
    assert!(
        ref_edges
            .iter()
            .any(|(from, to)| from == "create_user" && to == "integer"),
        "Expected Reference to integer"
    );
    assert!(
        ref_edges
            .iter()
            .any(|(from, to)| from == "create_user" && to == "User"),
        "Expected Reference to User"
    );
}

#[test]
fn test_mixed_public_private_specs() {
    let source = r#"
defmodule Test do
  @spec public_func() :: String.t()
  def public_func, do: "public"

  @spec private_func() :: integer()
  defp private_func, do: 42
end
"#;
    let staging = build_test_graph(source, "test.ex");

    let return_edges = collect_typeof_edges_by_context(&staging, TypeOfContext::Return);
    assert!(
        return_edges
            .iter()
            .any(|(from, to)| from == "public_func" && to == "String.t()"),
        "Expected TypeOf edge for public function"
    );
    assert!(
        return_edges
            .iter()
            .any(|(from, to)| from == "private_func" && to == "integer()"),
        "Expected TypeOf edge for private function"
    );
}

// ============================================================================
// Category 7: Edge Cases (2 tests)
// ============================================================================

#[test]
fn test_function_without_spec() {
    let source = r"
defmodule Test do
  def no_spec(), do: 42
end
";
    let staging = build_test_graph(source, "test.ex");

    let return_edges = collect_typeof_edges_by_context(&staging, TypeOfContext::Return);
    let no_spec_edges: Vec<_> = return_edges
        .iter()
        .filter(|(from, _)| from == "no_spec")
        .collect();

    assert_eq!(
        no_spec_edges.len(),
        0,
        "Expected no TypeOf edges for function without @spec"
    );
}

#[test]
fn test_spec_for_nonexistent_function() {
    let source = r"
defmodule Test do
  @spec ghost_func() :: String.t()
  # Function definition is missing
end
";
    let staging = build_test_graph(source, "test.ex");

    // Should still create TypeOf edge even if function def is missing
    // (spec can exist before implementation)
    let return_edges = collect_typeof_edges_by_context(&staging, TypeOfContext::Return);
    assert!(
        return_edges
            .iter()
            .any(|(from, to)| from == "ghost_func" && to == "String.t()"),
        "Expected TypeOf edge for spec without implementation"
    );
}