llmcc-rust 0.2.21

llmcc: llm context compiler
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
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
use std::collections::HashSet;

use llmcc_core::{
    build_llmcc_graph_with_config,
    graph_builder::{BlockKind, GraphNode, ProjectGraph},
    GraphBuildConfig,
};
use llmcc_rust::{bind_symbols, build_llmcc_ir, collect_symbols, CompileCtxt, LangRust};

fn build_graph_with_config(sources: &[&str], config: GraphBuildConfig) -> ProjectGraph<'static> {
    let source_bytes: Vec<Vec<u8>> = sources.iter().map(|s| s.as_bytes().to_vec()).collect();

    let cc = Box::leak(Box::new(CompileCtxt::from_sources::<LangRust>(
        &source_bytes,
    )));
    let globals = cc.create_globals();
    let unit_count = sources.len();
    let mut collections = Vec::new();
    let mut graph = ProjectGraph::new(cc);

    build_llmcc_ir::<LangRust>(cc).unwrap();

    for unit_idx in 0..unit_count {
        let unit = graph.cc.compile_unit(unit_idx);
        collections.push(collect_symbols(unit, globals));
    }

    for unit_idx in 0..unit_count {
        let unit = graph.cc.compile_unit(unit_idx);
        bind_symbols(unit, globals);

        let unit_graph = build_llmcc_graph_with_config::<LangRust>(unit, unit_idx, config).unwrap();
        graph.add_child(unit_graph);
    }

    // Link cross-unit dependencies
    graph.link_units();
    drop(collections);

    graph
}

/// Helper to build a project graph from multiple Rust source files
/// Each source becomes a separate compilation unit in the graph
fn build_graph(sources: &[&str]) -> ProjectGraph<'static> {
    build_graph_with_config(sources, GraphBuildConfig::default())
}

fn block_name(graph: &ProjectGraph<'static>, node: GraphNode) -> Option<String> {
    graph
        .block_info(node.block_id)
        .and_then(|(_, name, _)| name)
}

/// Helper to get dependency names for a block
fn get_depends_on(graph: &ProjectGraph<'static>, block: GraphNode) -> HashSet<String> {
    graph
        .get_block_depends(block)
        .into_iter()
        .filter_map(|node| block_name(graph, node))
        .collect()
}

/// Helper to get dependent names for a block (reverse dependencies)
fn get_depended_by(graph: &ProjectGraph<'static>, block: GraphNode) -> HashSet<String> {
    graph
        .get_block_depended(block)
        .into_iter()
        .filter_map(|node| block_name(graph, node))
        .collect()
}

/// Helper to get all related blocks recursively
fn get_all_related_names(graph: &ProjectGraph<'static>, block: GraphNode) -> HashSet<String> {
    graph
        .find_dpends_blocks_recursive(block)
        .into_iter()
        .filter_map(|node| block_name(graph, node))
        .collect()
}

/// Helper to assert dependency relationship
fn assert_depends_on(graph: &ProjectGraph<'static>, block: GraphNode, expected: &str) {
    let deps = get_depends_on(graph, block);
    assert!(
        deps.contains(expected),
        "Expected '{}' to depend on '{}', but found: {:?}",
        block_name(graph, block).unwrap_or_default(),
        expected,
        deps
    );
}

/// Helper to assert reverse dependency relationship
fn assert_depended_by(graph: &ProjectGraph<'static>, block: GraphNode, expected: &str) {
    let depended = get_depended_by(graph, block);
    assert!(
        depended.contains(expected),
        "Expected '{}' to be depended on by '{}', but found: {:?}",
        block_name(graph, block).unwrap_or_default(),
        expected,
        depended
    );
}

/// Helper to assert multiple dependencies
fn assert_depends_on_all(graph: &ProjectGraph<'static>, block: GraphNode, expected: &[&str]) {
    let deps = get_depends_on(graph, block);
    for exp in expected {
        assert!(
            deps.contains(*exp),
            "Expected '{}' to depend on '{}', but found: {:?}",
            block_name(graph, block).unwrap_or_default(),
            exp,
            deps
        );
    }
}

/// Helper to assert multiple reverse dependencies
fn assert_depended_by_all(graph: &ProjectGraph<'static>, block: GraphNode, expected: &[&str]) {
    let depended = get_depended_by(graph, block);
    for exp in expected {
        assert!(
            depended.contains(*exp),
            "Expected '{}' to be depended on by '{}', but found: {:?}",
            block_name(graph, block).unwrap_or_default(),
            exp,
            depended
        );
    }
}

/// Helper to get block by name in a specific unit
fn get_block_in_unit(graph: &ProjectGraph<'static>, unit_idx: usize, name: &str) -> GraphNode {
    graph
        .block_by_name_in(unit_idx, name)
        .expect(&format!("{} in unit {}", name, unit_idx))
}

/// Helper to get block by name across all units
fn get_block(graph: &ProjectGraph<'static>, name: &str) -> GraphNode {
    graph.block_by_name(name).expect(&format!("block {}", name))
}

#[test]
fn collects_function_blocks_in_unit() {
    let graph = build_graph(&[r#"
        fn helper() {}

        fn caller() {
            helper();
        }
    "#]);

    let helper = get_block(&graph, "helper");
    let caller = get_block(&graph, "caller");

    assert_eq!(helper.unit_index, 0);
    assert_eq!(caller.unit_index, 0);

    let helper_info = graph.block_info(helper.block_id).unwrap();
    assert_eq!(helper_info.2, BlockKind::Func);

    let caller_info = graph.block_info(caller.block_id).unwrap();
    assert_eq!(caller_info.2, BlockKind::Func);

    let unit_blocks: HashSet<_> = graph
        .blocks_in(0)
        .into_iter()
        .filter_map(|node| block_name(&graph, node))
        .collect();
    assert_eq!(unit_blocks.len(), 2);
    assert!(unit_blocks.contains("helper"));
    assert!(unit_blocks.contains("caller"));

    let call_nodes = graph.blocks_by_kind_in(BlockKind::Call, 0);
    assert_eq!(call_nodes.len(), 1);

    // The caller function should depend on helper
    let caller_dependencies = get_depends_on(&graph, caller);
    assert_eq!(caller_dependencies, HashSet::from(["helper".to_string()]));

    // Verify reverse: helper is depended on by caller
    assert_depended_by(&graph, helper, "caller");
}

#[test]
fn finds_transitive_dependencies() {
    let graph = build_graph(&[r#"
        fn leaf() {}

        fn middle_a() {
            leaf();
        }

        fn middle_b() {
            leaf();
        }

        fn top() {
            middle_a();
            middle_b();
        }
    "#]);

    let top = get_block(&graph, "top");

    // Get all dependencies of top (should be middle_a, middle_b)
    let direct_deps = get_depends_on(&graph, top);
    assert!(direct_deps.contains("middle_a"));
    assert!(direct_deps.contains("middle_b"));

    // Verify reverse: middle_a and middle_b are depended on by top
    let leaf = get_block(&graph, "leaf");
    let middle_a = get_block(&graph, "middle_a");
    let middle_b = get_block(&graph, "middle_b");

    let leaf_depended = get_depended_by(&graph, leaf);
    assert!(leaf_depended.contains("middle_a"));
    assert!(leaf_depended.contains("middle_b"));

    assert_depended_by(&graph, middle_a, "top");
    assert_depended_by(&graph, middle_b, "top");

    // Get transitive dependencies via find_dpends_blocks_recursive
    let all_names = get_all_related_names(&graph, top);
    assert!(all_names.contains("leaf"));
}

#[test]
fn filters_blocks_by_kind_and_unit() {
    let graph = build_graph(&[
        r#"
        struct Foo;

        impl Foo {
            fn method(&self) {}
        }

        fn top_level() {}
    "#,
        r#"
        const VALUE: i32 = 42;

        fn helper() {}
    "#,
    ]);

    let unit0_funcs: HashSet<_> = graph
        .blocks_by_kind_in(BlockKind::Func, 0)
        .into_iter()
        .filter_map(|node| block_name(&graph, node))
        .collect();

    assert!(unit0_funcs.contains("top_level"));
    assert!(unit0_funcs.contains("method"));

    let unit1_consts: HashSet<_> = graph
        .blocks_by_kind_in(BlockKind::Const, 1)
        .into_iter()
        .filter_map(|node| block_name(&graph, node))
        .collect();

    assert!(unit1_consts.contains("VALUE"));

    let helper = get_block_in_unit(&graph, 1, "helper");
    assert_eq!(helper.unit_index, 1);
    let helper_info = graph.block_info(helper.block_id).unwrap();
    assert_eq!(helper_info.2, BlockKind::Func);

    let both_helpers = graph.blocks_by_name("helper");
    assert_eq!(both_helpers.len(), 1);
}

#[test]
fn type_records_dependencies_on_methods() {
    let graph = build_graph(&[r#"
        struct Foo;

        impl Foo {
            fn method(&self) {}
        }
    "#]);

    let method = get_block(&graph, "method");

    // method should exist and belong to Foo
    let method_info = graph.block_info(method.block_id).unwrap();
    assert_eq!(method_info.2, BlockKind::Func);
}

#[test]
fn method_depends_on_inherent_method() {
    let graph = build_graph(&[r#"
        struct Foo;

        impl Foo {
            fn helper(&self) {}

            fn caller(&self) {
                self.helper();
            }
        }
    "#]);

    let caller = get_block(&graph, "caller");
    assert_depends_on(&graph, caller, "helper");
}

#[test]
fn function_depends_on_argument_type() {
    let graph = build_graph(&[r#"
        struct Foo;

        fn takes(_: Foo) {}
    "#]);

    let takes = get_block(&graph, "takes");
    let foo = get_block(&graph, "Foo");

    assert_depends_on(&graph, takes, "Foo");
    assert_depended_by(&graph, foo, "takes");
}

#[test]
fn function_depends_on_return_type() {
    let graph = build_graph(&[r#"
        struct Bar;

        fn returns() -> Bar {
            Bar
        }
    "#]);

    let returns = get_block(&graph, "returns");
    let bar = get_block(&graph, "Bar");

    assert_depends_on(&graph, returns, "Bar");
    assert_depended_by(&graph, bar, "returns");
}

#[test]
fn struct_field_creates_dependency() {
    let graph = build_graph(&[r#"
        struct Inner;

        struct Outer {
            field: Inner,
        }
    "#]);

    let outer = get_block(&graph, "Outer");
    let inner = get_block(&graph, "Inner");

    assert_depends_on(&graph, outer, "Inner");
    assert_depended_by(&graph, inner, "Outer");
}

#[test]
fn struct_field_depends_on_enum_type() {
    let graph = build_graph(&[r#"
        enum Status {
            Ready,
            Busy,
        }

        struct Holder {
            status: Status,
        }
    "#]);

    let holder = get_block(&graph, "Holder");
    let status = get_block(&graph, "Status");

    assert_depends_on(&graph, holder, "Status");
    assert_depended_by(&graph, status, "Holder");
}

#[test]
fn enum_variant_depends_on_struct_type() {
    let graph = build_graph(&[r#"
        struct Payload;

        enum Message {
            Empty,
            With(Payload),
        }
    "#]);

    let message = get_block(&graph, "Message");
    let payload = get_block(&graph, "Payload");

    assert_depends_on(&graph, message, "Payload");
    assert_depended_by(&graph, payload, "Message");
}

#[test]
fn nested_struct_fields_create_chain() {
    let graph = build_graph(&[r#"
        struct A;

        struct B {
            a: A,
        }

        struct C {
            b: B,
        }
    "#]);

    let a = get_block(&graph, "A");
    let b = get_block(&graph, "B");
    let c = get_block(&graph, "C");

    assert_depends_on(&graph, b, "A");
    assert_depended_by(&graph, a, "B");

    assert_depends_on(&graph, c, "B");
    assert_depended_by(&graph, b, "C");
}

#[test]
fn function_chain_dependencies() {
    let graph = build_graph(&[r#"
        fn level1() {}

        fn level2() {
            level1();
        }

        fn level3() {
            level2();
        }

        fn level4() {
            level3();
        }
    "#]);

    let level1 = get_block(&graph, "level1");
    let level2 = get_block(&graph, "level2");
    let level3 = get_block(&graph, "level3");
    let level4 = get_block(&graph, "level4");

    // Direct dependencies
    assert_depends_on(&graph, level2, "level1");
    assert_depends_on(&graph, level3, "level2");
    assert_depends_on(&graph, level4, "level3");

    // Reverse dependencies
    assert_depended_by(&graph, level1, "level2");
    assert_depended_by(&graph, level2, "level3");
    assert_depended_by(&graph, level3, "level4");
}

#[test]
fn multiple_dependencies_same_function() {
    let graph = build_graph(&[r#"
        fn dep1() {}
        fn dep2() {}
        fn dep3() {}

        fn caller() {
            dep1();
            dep2();
            dep3();
        }
    "#]);

    let caller = get_block(&graph, "caller");
    let dep1 = get_block(&graph, "dep1");
    let dep2 = get_block(&graph, "dep2");
    let dep3 = get_block(&graph, "dep3");

    assert_depends_on_all(&graph, caller, &["dep1", "dep2", "dep3"]);
    assert_depended_by_all(&graph, dep1, &["caller"]);
    assert_depended_by_all(&graph, dep2, &["caller"]);
    assert_depended_by_all(&graph, dep3, &["caller"]);
}

#[test]
fn method_depends_on_type_and_function() {
    let graph = build_graph(&[r#"
        fn external_helper() {}

        struct Container;

        impl Container {
            fn method(&self) {
                external_helper();
            }
        }
    "#]);

    let method = get_block(&graph, "method");
    let external_helper = get_block(&graph, "external_helper");

    assert_depends_on(&graph, method, "external_helper");
    assert_depended_by(&graph, external_helper, "method");
}

#[test]
fn generic_type_parameter_creates_dependency() {
    let graph = build_graph(&[r#"
        struct Container<T> {
            value: T,
        }

        struct Item;

        fn uses(_: Container<Item>) {}
    "#]);

    let uses = get_block(&graph, "uses");
    let container = get_block(&graph, "Container");
    let item = get_block(&graph, "Item");

    assert_depends_on_all(&graph, uses, &["Container", "Item"]);
    assert_depended_by(&graph, container, "uses");
    assert_depended_by(&graph, item, "uses");
}

#[test]
fn multiple_parameters_multiple_types() {
    let graph = build_graph(&[r#"
        struct First;
        struct Second;
        struct Third;

        fn multi_param(_a: First, _b: Second, _c: Third) {}
    "#]);

    let multi = get_block(&graph, "multi_param");
    let first = get_block(&graph, "First");
    let second = get_block(&graph, "Second");
    let third = get_block(&graph, "Third");

    assert_depends_on_all(&graph, multi, &["First", "Second", "Third"]);
    assert_depended_by(&graph, first, "multi_param");
    assert_depended_by(&graph, second, "multi_param");
    assert_depended_by(&graph, third, "multi_param");
}

#[test]
fn trait_impl_method_dependencies() {
    let graph = build_graph(&[r#"
        trait Processor {
            fn process(&self);
        }

        struct Handler;

        impl Processor for Handler {
            fn process(&self) {}
        }
    "#]);

    let process = get_block(&graph, "process");

    // process method should exist
    let process_info = graph.block_info(process.block_id).unwrap();
    assert_eq!(process_info.2, BlockKind::Func);
}

#[test]
#[ignore = "todo"]
fn associated_function_depends_on_type() {
    let graph = build_graph(&[r#"
        struct Builder;

        impl Builder {
            fn new() -> Builder {
                Builder
            }
        }
    "#]);

    let new = get_block(&graph, "new");
    assert_depends_on(&graph, new, "Builder");
}

#[test]
fn deeply_nested_dependency_chain() {
    let graph = build_graph(&[r#"
        fn level1() {}

        fn level2() {
            level1();
        }

        fn level3() {
            level2();
        }

        fn level4() {
            level3();
        }

        fn level5() {
            level4();
        }

        fn level6() {
            level5();
        }
    "#]);

    let level1 = get_block(&graph, "level1");
    let level6 = get_block(&graph, "level6");

    // Direct dependency: level6 depends on level5
    assert_depends_on(&graph, level6, "level5");

    // Transitive: level6 should have level1-5 in its related blocks
    let all_names = get_all_related_names(&graph, level6);
    assert!(all_names.contains("level1"));
    assert!(all_names.contains("level2"));
    assert!(all_names.contains("level3"));
    assert!(all_names.contains("level4"));
    assert!(all_names.contains("level5"));

    // Reverse: level1 is depended on by level2
    assert_depended_by(&graph, level1, "level2");
}

#[test]
fn multiple_files_cross_unit_dependencies() {
    let graph = build_graph(&[
        r#"
        pub struct SharedType;

        pub fn producer() -> SharedType {
            SharedType
        }
    "#,
        r#"
        pub fn consumer_fn() {
            println!("consuming");
        }
    "#,
    ]);

    let producer = get_block_in_unit(&graph, 0, "producer");
    let shared_type = get_block(&graph, "SharedType");

    assert_depends_on(&graph, producer, "SharedType");
    assert_depended_by(&graph, shared_type, "producer");

    // Verify consumer_fn exists in unit 1
    let consumer_fn = get_block_in_unit(&graph, 1, "consumer_fn");
    assert_eq!(consumer_fn.unit_index, 1);
}

#[test]
fn multiple_files_multiple_levels_nested() {
    let graph = build_graph(&[
        r#"
        pub struct Base;
        pub fn base_fn() {}
    "#,
        r#"
        pub struct Middle {
            b: crate::Base,
        }

        pub fn middle_fn() {
            crate::base_fn();
        }
    "#,
        r#"
        pub struct Top {
            m: crate::Middle,
        }

        pub fn top_fn() {
            crate::middle_fn();
        }
    "#,
    ]);

    let top_fn = get_block_in_unit(&graph, 2, "top_fn");
    let middle = get_block(&graph, "Middle");
    let top = get_block(&graph, "Top");

    assert_depends_on(&graph, top_fn, "middle_fn");
    assert_depends_on(&graph, middle, "Base");
    assert_depends_on(&graph, top, "Middle");
}

#[test]
fn same_name_in_different_units() {
    let graph = build_graph(&[
        r#"
        pub fn process() {
            println!("unit 0");
        }
    "#,
        r#"
        pub fn process() {
            println!("unit 1");
        }
    "#,
        r#"
        pub fn process() {
            println!("unit 2");
        }
    "#,
    ]);

    // All three units should have a "process" function
    let process_unit0 = get_block_in_unit(&graph, 0, "process");
    let process_unit1 = get_block_in_unit(&graph, 1, "process");
    let process_unit2 = get_block_in_unit(&graph, 2, "process");

    // Each should be in their respective unit
    assert_eq!(process_unit0.unit_index, 0);
    assert_eq!(process_unit1.unit_index, 1);
    assert_eq!(process_unit2.unit_index, 2);

    // blocks_by_name should find all three (or handle appropriately)
    let all_process = graph.blocks_by_name("process");
    assert_eq!(all_process.len(), 3);
}

#[test]
fn same_struct_name_in_different_units() {
    let graph = build_graph(&[
        r#"
        pub struct Data {
            value: i32,
        }

        pub fn process_u0(_: Data) {}
    "#,
        r#"
        pub struct Data {
            value: String,
        }

        pub fn process_u1(_: Data) {}
    "#,
    ]);

    // Both units should have Data struct
    let data_unit0 = get_block_in_unit(&graph, 0, "Data");
    let data_unit1 = get_block_in_unit(&graph, 1, "Data");

    assert_eq!(data_unit0.unit_index, 0);
    assert_eq!(data_unit1.unit_index, 1);

    // process_u0 depends on Data from unit 0
    let process_u0 = get_block_in_unit(&graph, 0, "process_u0");
    assert_depends_on(&graph, process_u0, "Data");

    // process_u1 depends on Data from unit 1
    let process_u1 = get_block_in_unit(&graph, 1, "process_u1");
    assert_depends_on(&graph, process_u1, "Data");
}

#[test]
fn diamond_dependency_pattern() {
    let graph = build_graph(&[r#"
        fn base() {}

        fn left() {
            base();
        }

        fn right() {
            base();
        }

        fn top() {
            left();
            right();
        }
    "#]);

    let base = get_block(&graph, "base");
    let left = get_block(&graph, "left");
    let right = get_block(&graph, "right");
    let top = get_block(&graph, "top");

    // top depends on both left and right
    assert_depends_on_all(&graph, top, &["left", "right"]);

    // both left and right depend on base
    assert_depends_on(&graph, left, "base");
    assert_depends_on(&graph, right, "base");

    // base is depended on by both left and right
    assert_depended_by_all(&graph, base, &["left", "right"]);
}

#[test]
fn multiple_nested_structs_with_same_field_names() {
    let graph = build_graph(&[r#"
        struct A;

        struct B {
            field: A,
        }

        struct C {
            field: B,
        }

        struct D {
            field: C,
        }

        fn uses(_: D) {}
    "#]);

    let a = get_block(&graph, "A");
    let b = get_block(&graph, "B");
    let c = get_block(&graph, "C");
    let d = get_block(&graph, "D");
    let uses = get_block(&graph, "uses");

    // Each struct depends on the previous one
    assert_depends_on(&graph, b, "A");
    assert_depends_on(&graph, c, "B");
    assert_depends_on(&graph, d, "C");
    assert_depends_on(&graph, uses, "D");

    // A is depended on by B
    assert_depended_by(&graph, a, "B");
}

#[test]
fn complex_cross_file_diamond() {
    let graph = build_graph(&[
        r#"
        pub struct Base;
        pub fn base_fn() {}
    "#,
        r#"
        pub fn left(_: crate::Base) {
            crate::base_fn();
        }
    "#,
        r#"
        pub fn right(_: crate::Base) {
            crate::base_fn();
        }
    "#,
        r#"
        pub fn coordinator() {
            crate::left(crate::Base);
            crate::right(crate::Base);
        }
    "#,
    ]);

    let left = get_block_in_unit(&graph, 1, "left");
    let right = get_block_in_unit(&graph, 2, "right");

    // left depends on Base (parameter type)
    assert_depends_on(&graph, left, "Base");

    // right depends on Base (parameter type)
    assert_depends_on(&graph, right, "Base");

    // left and right both depend on base_fn through function calls
    let left_deps = get_depends_on(&graph, left);
    let right_deps = get_depends_on(&graph, right);
    assert!(left_deps.contains("base_fn") || left_deps.contains("Base"));
    assert!(right_deps.contains("base_fn") || right_deps.contains("Base"));
}

#[test]
fn multiple_structs_same_type_field() {
    let graph = build_graph(&[r#"
        struct Shared;

        struct A {
            data: Shared,
        }

        struct B {
            data: Shared,
        }

        struct C {
            data: Shared,
        }

        fn use_all(_a: A, _b: B, _c: C) {}
    "#]);

    let shared = get_block(&graph, "Shared");
    let a = get_block(&graph, "A");
    let b = get_block(&graph, "B");
    let c = get_block(&graph, "C");

    // All three structs depend on Shared
    assert_depends_on(&graph, a, "Shared");
    assert_depends_on(&graph, b, "Shared");
    assert_depends_on(&graph, c, "Shared");

    // Shared is depended on by all three
    assert_depended_by_all(&graph, shared, &["A", "B", "C"]);
}

#[test]
fn module_private_types_dependencies() {
    let graph = build_graph(&[r#"
        mod outer {
            pub struct PublicType {
                value: i32,
            }

            struct PrivateType {
                value: i32,
            }

            pub fn uses_public(_: PublicType) {}

            fn uses_private(_: PrivateType) {}
        }
    "#]);

    let public_type = get_block(&graph, "PublicType");
    let private_type = get_block(&graph, "PrivateType");
    let uses_public = get_block(&graph, "uses_public");
    let uses_private = get_block(&graph, "uses_private");

    // Both functions should depend on their respective types
    assert_depends_on(&graph, uses_public, "PublicType");
    assert_depends_on(&graph, uses_private, "PrivateType");

    // Both types should be depended on by their respective functions
    assert_depended_by(&graph, public_type, "uses_public");
    assert_depended_by(&graph, private_type, "uses_private");
}

#[test]
fn nested_module_cross_dependency() {
    let graph = build_graph(&[r#"
        mod outer {
            pub struct OuterType;

            pub mod inner {
                pub fn inner_fn(_: crate::outer::OuterType) {}
            }
        }
    "#]);

    let outer_type = get_block(&graph, "OuterType");
    let inner_fn = get_block(&graph, "inner_fn");

    // inner_fn depends on OuterType
    assert_depends_on(&graph, inner_fn, "OuterType");

    // OuterType is depended on by inner_fn
    assert_depended_by(&graph, outer_type, "inner_fn");
}

#[test]
fn trait_impl_with_type_parameter_dependency() {
    let graph = build_graph(&[r#"
        trait Handler<T> {
            fn handle(&self, value: T);
        }

        struct MyType;

        struct MyHandler;

        impl Handler<MyType> for MyHandler {
            fn handle(&self, _value: MyType) {}
        }
    "#]);

    let my_type = get_block(&graph, "MyType");
    let my_handler = get_block(&graph, "MyHandler");
    let handle_method = get_block(&graph, "handle");

    // handler depends on both MyType and MyHandler
    let my_handler_deps = get_depends_on(&graph, my_handler);
    assert!(!my_handler_deps.is_empty());

    // handle method depends on MyType
    assert_depends_on(&graph, handle_method, "MyType");

    // MyType is depended on by handle method
    assert_depended_by(&graph, my_type, "handle");
}

#[test]
fn struct_with_private_and_public_methods() {
    let graph = build_graph(&[r#"
        struct Container;

        impl Container {
            pub fn public_method(&self) -> Container {
                Container
            }

            fn private_method(&self) -> Container {
                Container
            }
        }
    "#]);

    let container = get_block(&graph, "Container");
    let public_method = get_block(&graph, "public_method");
    let private_method = get_block(&graph, "private_method");

    // Both methods are defined in impl blocks, which may not create direct dependencies
    // in the current implementation. Just verify they exist and their basic properties.
    let public_info = graph.block_info(public_method.block_id).unwrap();
    assert_eq!(public_info.2, BlockKind::Func);

    let private_info = graph.block_info(private_method.block_id).unwrap();
    assert_eq!(private_info.2, BlockKind::Func);

    let container_info = graph.block_info(container.block_id).unwrap();
    assert_eq!(container_info.2, BlockKind::Class);
}

#[test]
fn enum_with_complex_variants_dependency() {
    let graph = build_graph(&[r#"
        struct Data;

        enum Result {
            Ok(Data),
            Err(String),
        }

        fn process_result(_: Result) {}
    "#]);

    let _data = get_block(&graph, "Data");
    let result_enum = get_block(&graph, "Result");
    let process = get_block(&graph, "process_result");

    // process_result depends on Result
    assert_depends_on(&graph, process, "Result");

    // Result is depended on by process_result
    assert_depended_by(&graph, result_enum, "process_result");

    // Result enum should depend on Data (from Ok variant)
    assert_depends_on(&graph, result_enum, "Data");
}

#[test]
fn multiple_impl_blocks_same_type() {
    let graph = build_graph(&[r#"
        struct Point;

        impl Point {
            fn x(&self) -> i32 { 0 }
        }

        impl Point {
            fn y(&self) -> i32 { 0 }
        }
    "#]);

    let point = get_block(&graph, "Point");
    let x_method = get_block(&graph, "x");
    let y_method = get_block(&graph, "y");

    // Both methods should exist
    let x_info = graph.block_info(x_method.block_id).unwrap();
    assert_eq!(x_info.2, BlockKind::Func);

    let y_info = graph.block_info(y_method.block_id).unwrap();
    assert_eq!(y_info.2, BlockKind::Func);

    let point_info = graph.block_info(point.block_id).unwrap();
    assert_eq!(point_info.2, BlockKind::Class);
}

#[test]
fn const_with_type_dependency() {
    let graph = build_graph(&[r#"
        struct Config {
            value: i32,
        }

        const GLOBAL_CONFIG: Config = Config { value: 42 };

        fn uses_config() -> Config {
            Config { value: 0 }
        }
    "#]);

    let config = get_block(&graph, "Config");
    let uses_config = get_block(&graph, "uses_config");

    // uses_config depends on Config (return type)
    assert_depends_on(&graph, uses_config, "Config");

    // Config is depended on by uses_config
    assert_depended_by(&graph, config, "uses_config");
}

#[test]
fn generic_struct_with_bound_dependency() {
    let graph = build_graph(&[r#"
        struct ConcreteProcessor;

        fn uses_concrete(_: ConcreteProcessor) {}
    "#]);

    let concrete = get_block(&graph, "ConcreteProcessor");
    let uses_concrete = get_block(&graph, "uses_concrete");

    // uses_concrete depends on ConcreteProcessor
    assert_depends_on(&graph, uses_concrete, "ConcreteProcessor");

    // ConcreteProcessor is depended on by uses_concrete
    assert_depended_by(&graph, concrete, "uses_concrete");
}

#[test]
fn function_returning_trait_object() {
    let graph = build_graph(&[r#"
        struct FileReader;

        fn get_reader() -> Box<FileReader> {
            Box::new(FileReader)
        }
    "#]);

    let file_reader = get_block(&graph, "FileReader");
    let get_reader = get_block(&graph, "get_reader");

    // get_reader depends on FileReader (used in return type and impl)
    assert_depends_on(&graph, get_reader, "FileReader");

    // FileReader is depended on by get_reader
    assert_depended_by(&graph, file_reader, "get_reader");
}

#[test]
fn enum_variant_with_struct_field() {
    let graph = build_graph(&[r#"
        struct Error {
            message: String,
        }

        enum Result<T> {
            Ok(T),
            Err(Error),
        }

        struct Value;

        fn process() -> Result<Value> {
            Err(Error { message: String::new() })
        }
    "#]);

    let error_struct = get_block(&graph, "Error");
    let result_enum = get_block(&graph, "Result");
    let _value = get_block(&graph, "Value");
    let process = get_block(&graph, "process");

    // process depends on Result
    assert_depends_on(&graph, process, "Result");

    // Result depends on Error (from Err variant)
    assert_depends_on(&graph, result_enum, "Error");

    // Error is depended on by Result
    assert_depended_by(&graph, error_struct, "Result");
}

#[test]
fn recursive_type_dependency() {
    let graph = build_graph(&[r#"
        struct Node {
            value: i32,
            next: Option<Box<Node>>,
        }

        fn traverse(_: Node) {}
    "#]);

    let node = get_block(&graph, "Node");
    let traverse = get_block(&graph, "traverse");

    // traverse depends on Node
    assert_depends_on(&graph, traverse, "Node");

    // Node is depended on by traverse
    assert_depended_by(&graph, node, "traverse");

    // Node references itself in the next field (recursive)
    let node_deps = get_depends_on(&graph, node);
    assert!(node_deps.is_empty() || node_deps.contains("Node"));
}

#[test]
fn compact_graph_matches_default_output() {
    let sources = [r#"
        struct Config;

        struct Handler {
            config: Config,
        }

        struct App {
            handler: Handler,
        }
    "#];

    let default_graph = build_graph_with_config(&sources, GraphBuildConfig::default());
    let project_graph = build_graph_with_config(&sources, GraphBuildConfig::compact());

    let default_render = default_graph.render_compact_graph();
    let compact_render = project_graph.render_compact_graph();

    assert_eq!(default_render, compact_render);
    assert!(compact_render.contains("App\\n(class)"));
    assert!(compact_render.contains("Handler\\n(class)"));
    assert!(compact_render.contains("Config\\n(class)"));
    assert!(compact_render.contains("n0 -> n2;"));
    assert!(compact_render.contains("n2 -> n1;"));
}