dlin-core 0.2.2

Core library for dbt model lineage analysis
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
use super::*;
use std::collections::HashMap;

#[test]
fn test_build_graph_sources_and_models() {
    let (_tmp, project_dir) = setup_temp_project();

    let files = DiscoveredFiles {
        model_sql_files: vec![
            project_dir.join("models/stg_orders.sql"),
            project_dir.join("models/orders.sql"),
        ],
        yaml_files: vec![project_dir.join("models/schema.yml")],
        ..Default::default()
    };

    // Use no_cache: false to exercise the cache-enabled path end-to-end
    let graph = build_graph(&project_dir, &files, None, false, false, &HashMap::new()).unwrap();

    // Should have source + 2 models = 3 nodes
    assert_eq!(graph.node_count(), 3);

    // Check node types
    let mut types: Vec<NodeType> = graph.node_indices().map(|i| graph[i].node_type).collect();
    types.sort_by_key(|t| format!("{:?}", t));
    assert!(types.contains(&NodeType::Source));
    assert!(types.iter().filter(|t| **t == NodeType::Model).count() == 2);

    // Should have 2 edges: source→stg_orders, stg_orders→orders
    assert_eq!(graph.edge_count(), 2);
}

#[test]
fn test_build_graph_with_seeds() {
    let (_tmp, project_dir) = setup_temp_project();

    // Add a seed
    let seeds_dir = project_dir.join("seeds");
    fs::create_dir_all(&seeds_dir).unwrap();
    fs::write(seeds_dir.join("countries.csv"), "id,name\n1,US\n").unwrap();

    let files = DiscoveredFiles {
        seed_files: vec![project_dir.join("seeds/countries.csv")],
        ..Default::default()
    };

    let graph = build_graph(&project_dir, &files, None, true, false, &HashMap::new()).unwrap();
    assert_eq!(graph.node_count(), 1);
    let node = &graph[graph.node_indices().next().unwrap()];
    assert_eq!(node.node_type, NodeType::Seed);
    assert_eq!(node.label, "countries");
}

#[test]
fn test_build_graph_with_snapshots() {
    let (_tmp, project_dir) = setup_temp_project();

    let snap_dir = project_dir.join("snapshots");
    fs::create_dir_all(&snap_dir).unwrap();
    fs::write(snap_dir.join("snap_orders.sql"), "SELECT 1").unwrap();

    let files = DiscoveredFiles {
        snapshot_sql_files: vec![project_dir.join("snapshots/snap_orders.sql")],
        ..Default::default()
    };

    let graph = build_graph(&project_dir, &files, None, true, false, &HashMap::new()).unwrap();
    assert_eq!(graph.node_count(), 1);
    let node = &graph[graph.node_indices().next().unwrap()];
    assert_eq!(node.node_type, NodeType::Snapshot);
    assert_eq!(node.label, "snap_orders");
}

#[test]
fn test_build_graph_with_tests() {
    let (_tmp, project_dir) = setup_temp_project();

    let test_dir = project_dir.join("tests");
    fs::create_dir_all(&test_dir).unwrap();
    fs::write(
        test_dir.join("assert_positive.sql"),
        "SELECT * FROM {{ ref('stg_orders') }} WHERE amount < 0",
    )
    .unwrap();

    // Need the model that the test references
    let models_dir = project_dir.join("models");
    fs::create_dir_all(&models_dir).unwrap();
    fs::write(models_dir.join("stg_orders.sql"), "SELECT 1").unwrap();

    let files = DiscoveredFiles {
        model_sql_files: vec![project_dir.join("models/stg_orders.sql")],
        test_sql_files: vec![project_dir.join("tests/assert_positive.sql")],
        ..Default::default()
    };

    let graph = build_graph(&project_dir, &files, None, true, false, &HashMap::new()).unwrap();
    // model + test = 2 nodes
    assert_eq!(graph.node_count(), 2);
    // test edge: stg_orders → assert_positive
    assert_eq!(graph.edge_count(), 1);

    // Singular SQL tests should use EdgeType::Test
    use petgraph::visit::IntoEdgeReferences;
    let edge = graph.edge_references().next().unwrap();
    assert_eq!(edge.weight().edge_type, EdgeType::Test);
}

#[test]
fn test_build_graph_with_exposures() {
    let (_tmp, project_dir) = setup_temp_project();

    let models_dir = project_dir.join("models");
    fs::create_dir_all(&models_dir).unwrap();
    fs::write(models_dir.join("orders.sql"), "SELECT 1").unwrap();

    fs::write(
        models_dir.join("schema.yml"),
        r#"
version: 2
sources: []
models: []
exposures:
  - name: weekly_report
    description: "Weekly report dashboard"
    depends_on:
      - ref('orders')
"#,
    )
    .unwrap();

    let files = DiscoveredFiles {
        model_sql_files: vec![project_dir.join("models/orders.sql")],
        yaml_files: vec![project_dir.join("models/schema.yml")],
        ..Default::default()
    };

    let graph = build_graph(&project_dir, &files, None, true, false, &HashMap::new()).unwrap();
    // model + exposure = 2 nodes
    assert_eq!(graph.node_count(), 2);
    // exposure edge: orders → weekly_report
    assert_eq!(graph.edge_count(), 1);
}

#[test]
fn test_build_graph_exposure_versioned_ref() {
    let tmp = tempfile::tempdir().unwrap();
    let project_dir = tmp.path().to_path_buf();
    let models_dir = project_dir.join("models");
    fs::create_dir_all(&models_dir).unwrap();

    fs::write(models_dir.join("my_model_v1.sql"), "SELECT 1").unwrap();
    fs::write(models_dir.join("my_model_v2.sql"), "SELECT 2").unwrap();
    fs::write(
        models_dir.join("schema.yml"),
        r#"
version: 2
models:
  - name: my_model
    latest_version: 2
    versions:
      - v: 1
      - v: 2
exposures:
  - name: pinned_report
    depends_on:
      - ref('my_model', version=1)
"#,
    )
    .unwrap();

    let files = DiscoveredFiles {
        model_sql_files: vec![
            project_dir.join("models/my_model_v1.sql"),
            project_dir.join("models/my_model_v2.sql"),
        ],
        yaml_files: vec![project_dir.join("models/schema.yml")],
        ..Default::default()
    };

    let graph = build_graph(&project_dir, &files, None, true, false, &HashMap::new()).unwrap();
    // v1 + v2 + exposure = 3 nodes
    assert_eq!(graph.node_count(), 3);
    // Only one edge: my_model.v1 → pinned_report
    assert_eq!(graph.edge_count(), 1);

    // Confirm the edge is from v1, not v2
    let v1_idx = graph
        .node_indices()
        .find(|&i| graph[i].unique_id == "model.my_model.v1")
        .expect("model.my_model.v1 should exist");
    let exposure_idx = graph
        .node_indices()
        .find(|&i| graph[i].unique_id == "exposure.pinned_report")
        .expect("exposure.pinned_report should exist");
    assert!(
        graph.contains_edge(v1_idx, exposure_idx),
        "exposure edge should be from v1"
    );
}

#[test]
fn test_build_graph_ref_resolves_to_seed() {
    let tmp = tempfile::tempdir().unwrap();
    let project_dir = tmp.path().to_path_buf();

    let models_dir = project_dir.join("models");
    let seeds_dir = project_dir.join("seeds");
    fs::create_dir_all(&models_dir).unwrap();
    fs::create_dir_all(&seeds_dir).unwrap();

    fs::write(seeds_dir.join("countries.csv"), "id,name\n1,US\n").unwrap();
    fs::write(
        models_dir.join("stg_countries.sql"),
        "SELECT * FROM {{ ref('countries') }}",
    )
    .unwrap();

    let files = DiscoveredFiles {
        model_sql_files: vec![project_dir.join("models/stg_countries.sql")],
        seed_files: vec![project_dir.join("seeds/countries.csv")],
        ..Default::default()
    };

    let graph = build_graph(&project_dir, &files, None, true, false, &HashMap::new()).unwrap();
    // seed + model = 2 nodes (no phantom)
    assert_eq!(graph.node_count(), 2);
    // ref edge: countries → stg_countries
    assert_eq!(graph.edge_count(), 1);

    // Verify the seed node is properly typed (not phantom)
    let seed_node = graph
        .node_indices()
        .find(|&i| graph[i].label == "countries")
        .unwrap();
    assert_eq!(graph[seed_node].node_type, NodeType::Seed);
}

#[test]
fn test_build_graph_phantom_node_for_unresolved_ref() {
    let (_tmp, project_dir) = setup_temp_project();

    let models_dir = project_dir.join("models");
    fs::create_dir_all(&models_dir).unwrap();
    fs::write(
        models_dir.join("orders.sql"),
        "SELECT * FROM {{ ref('nonexistent_model') }}",
    )
    .unwrap();

    let files = DiscoveredFiles {
        model_sql_files: vec![project_dir.join("models/orders.sql")],
        ..Default::default()
    };

    let graph = build_graph(&project_dir, &files, None, true, false, &HashMap::new()).unwrap();
    // model + phantom = 2 nodes
    assert_eq!(graph.node_count(), 2);
    let phantom = graph
        .node_indices()
        .find(|&i| graph[i].node_type == NodeType::Phantom)
        .expect("Should have a phantom node");
    assert_eq!(graph[phantom].label, "nonexistent_model");
}

#[test]
fn test_build_graph_phantom_node_for_unresolved_source() {
    let (_tmp, project_dir) = setup_temp_project();

    let models_dir = project_dir.join("models");
    fs::create_dir_all(&models_dir).unwrap();
    fs::write(
        models_dir.join("orders.sql"),
        "SELECT * FROM {{ source('unknown_src', 'unknown_table') }}",
    )
    .unwrap();

    let files = DiscoveredFiles {
        model_sql_files: vec![project_dir.join("models/orders.sql")],
        ..Default::default()
    };

    let graph = build_graph(&project_dir, &files, None, true, false, &HashMap::new()).unwrap();
    // model + phantom source = 2 nodes
    assert_eq!(graph.node_count(), 2);
    let phantom = graph
        .node_indices()
        .find(|&i| graph[i].node_type == NodeType::Phantom)
        .expect("Should have a phantom source node");
    assert_eq!(graph[phantom].label, "unknown_src.unknown_table");
}

#[test]
fn test_build_graph_model_descriptions() {
    let (_tmp, project_dir) = setup_temp_project();

    let files = DiscoveredFiles {
        model_sql_files: vec![project_dir.join("models/stg_orders.sql")],
        yaml_files: vec![project_dir.join("models/schema.yml")],
        ..Default::default()
    };

    let graph = build_graph(&project_dir, &files, None, true, false, &HashMap::new()).unwrap();
    let stg = graph
        .node_indices()
        .find(|&i| graph[i].label == "stg_orders")
        .unwrap();
    assert_eq!(graph[stg].description.as_deref(), Some("Staged orders"));
}

#[test]
fn test_build_graph_edge_types() {
    use petgraph::visit::IntoEdgeReferences;

    let (_tmp, project_dir) = setup_temp_project();

    let files = DiscoveredFiles {
        model_sql_files: vec![
            project_dir.join("models/stg_orders.sql"),
            project_dir.join("models/orders.sql"),
        ],
        yaml_files: vec![project_dir.join("models/schema.yml")],
        ..Default::default()
    };

    let graph = build_graph(&project_dir, &files, None, true, false, &HashMap::new()).unwrap();
    let edge_types: Vec<EdgeType> = graph
        .edge_references()
        .map(|e| e.weight().edge_type)
        .collect();
    assert!(edge_types.contains(&EdgeType::Source));
    assert!(edge_types.contains(&EdgeType::Ref));
}

#[test]
fn test_build_graph_empty_files() {
    let tmp = tempfile::tempdir().unwrap();
    let files = DiscoveredFiles::default();
    let graph = build_graph(tmp.path(), &files, None, true, false, &HashMap::new()).unwrap();
    assert_eq!(graph.node_count(), 0);
    assert_eq!(graph.edge_count(), 0);
}

#[test]
fn test_build_graph_model_config_merge() {
    // Covers lines 168-170: YAML model config with materialization and tags
    let tmp = tempfile::tempdir().unwrap();
    let project_dir = tmp.path().to_path_buf();

    let models_dir = project_dir.join("models");
    fs::create_dir_all(&models_dir).unwrap();

    fs::write(models_dir.join("stg_orders.sql"), "SELECT 1").unwrap();

    fs::write(
        models_dir.join("schema.yml"),
        r#"
version: 2
sources: []
models:
  - name: stg_orders
    description: "Staged orders"
    tags:
      - staging
    config:
      materialized: table
      tags:
        - daily
"#,
    )
    .unwrap();

    let files = DiscoveredFiles {
        model_sql_files: vec![project_dir.join("models/stg_orders.sql")],
        yaml_files: vec![project_dir.join("models/schema.yml")],
        ..Default::default()
    };

    let graph = build_graph(&project_dir, &files, None, true, false, &HashMap::new()).unwrap();
    let stg = graph
        .node_indices()
        .find(|&i| graph[i].label == "stg_orders")
        .unwrap();
    assert_eq!(graph[stg].materialization.as_deref(), Some("table"));
    assert!(graph[stg].tags.contains(&"staging".to_string()));
    assert!(graph[stg].tags.contains(&"daily".to_string()));
}

#[test]
fn test_build_graph_duplicate_model_name() {
    // Covers line 197: duplicate model name warning
    let tmp = tempfile::tempdir().unwrap();
    let project_dir = tmp.path().to_path_buf();

    let models_dir = project_dir.join("models");
    let subdir = models_dir.join("subdir");
    fs::create_dir_all(&subdir).unwrap();

    fs::write(models_dir.join("orders.sql"), "SELECT 1").unwrap();
    fs::write(subdir.join("orders.sql"), "SELECT 2").unwrap();

    let files = DiscoveredFiles {
        model_sql_files: vec![
            project_dir.join("models/orders.sql"),
            project_dir.join("models/subdir/orders.sql"),
        ],
        ..Default::default()
    };

    // Should not panic, just warn on stderr about the duplicate
    let graph = build_graph(&project_dir, &files, None, true, false, &HashMap::new()).unwrap();
    // Both SQL files produce nodes (duplicate warning is informational)
    let order_nodes: Vec<_> = graph
        .node_indices()
        .filter(|&i| graph[i].label == "orders")
        .collect();
    assert_eq!(order_nodes.len(), 2);
}

#[test]
fn test_build_graph_file_paths_are_relative() {
    let (_tmp, project_dir) = setup_temp_project();

    let files = DiscoveredFiles {
        model_sql_files: vec![
            project_dir.join("models/stg_orders.sql"),
            project_dir.join("models/orders.sql"),
        ],
        yaml_files: vec![project_dir.join("models/schema.yml")],
        ..Default::default()
    };

    let graph = build_graph(&project_dir, &files, None, true, false, &HashMap::new()).unwrap();

    for idx in graph.node_indices() {
        let node = &graph[idx];
        if let Some(ref fp) = node.file_path {
            assert!(
                fp.is_relative(),
                "file_path for node '{}' should be relative but got: {}",
                node.label,
                fp.display()
            );
            assert!(
                !fp.starts_with(&project_dir),
                "file_path for node '{}' should not start with project_dir: {}",
                node.label,
                fp.display()
            );
        }
    }

    // Verify source node specifically has relative path
    let source_node = graph
        .node_indices()
        .find(|&i| graph[i].node_type == NodeType::Source)
        .expect("should have a source node");
    assert_eq!(
        graph[source_node].file_path.as_deref(),
        Some(std::path::Path::new("models/schema.yml"))
    );

    // Verify model node has relative path
    let model_node = graph
        .node_indices()
        .find(|&i| graph[i].label == "stg_orders")
        .unwrap();
    assert_eq!(
        graph[model_node].file_path.as_deref(),
        Some(std::path::Path::new("models/stg_orders.sql"))
    );
}

#[test]
fn test_build_graph_with_macros() {
    let tmp = tempfile::tempdir().unwrap();
    let project_dir = tmp.path().to_path_buf();

    let models_dir = project_dir.join("models");
    let macros_dir = project_dir.join("macros");
    fs::create_dir_all(&models_dir).unwrap();
    fs::create_dir_all(&macros_dir).unwrap();

    // Macro that references a model
    fs::write(
        macros_dir.join("my_macro.sql"),
        r#"
{% macro my_cte() %}
    SELECT * FROM {{ ref('base_table') }}
{% endmacro %}
"#,
    )
    .unwrap();

    // Model that uses the macro
    fs::write(models_dir.join("base_table.sql"), "SELECT 1 as id").unwrap();
    fs::write(
        models_dir.join("derived.sql"),
        "SELECT * FROM ({{ my_cte() }})",
    )
    .unwrap();

    let files = DiscoveredFiles {
        model_sql_files: vec![
            project_dir.join("models/base_table.sql"),
            project_dir.join("models/derived.sql"),
        ],
        macro_sql_files: vec![project_dir.join("macros/my_macro.sql")],
        ..Default::default()
    };

    let graph = build_graph(&project_dir, &files, None, true, false, &HashMap::new()).unwrap();
    // base_table + derived = 2 nodes
    assert_eq!(graph.node_count(), 2);
    // ref edge: base_table → derived
    assert_eq!(graph.edge_count(), 1);

    // Verify the edge direction
    let base = graph
        .node_indices()
        .find(|&i| graph[i].label == "base_table")
        .unwrap();
    let derived = graph
        .node_indices()
        .find(|&i| graph[i].label == "derived")
        .unwrap();
    assert!(graph.contains_edge(base, derived));
}

#[test]
fn test_var_list_expansion_resolves_refs() {
    let tmp = tempfile::tempdir().unwrap();
    let project_dir = tmp.path().to_path_buf();
    let models_dir = project_dir.join("models");
    fs::create_dir_all(&models_dir).unwrap();

    // dbt_project.yml with vars
    fs::write(project_dir.join("dbt_project.yml"), "name: var_test\n").unwrap();

    // Model that uses var() to iterate over categories and ref dynamically
    fs::write(
        models_dir.join("combined.sql"),
        r#"
            {%- set categories = var("product_categories") -%}
            {%- for cat in categories -%}
                SELECT * FROM {{ ref('stg_' ~ cat ~ '_summary') }}
                {% if not loop.last %}UNION ALL{% endif %}
            {%- endfor -%}
            "#,
    )
    .unwrap();

    // Stub models that the refs point to
    fs::write(models_dir.join("stg_electronics_summary.sql"), "SELECT 1").unwrap();
    fs::write(models_dir.join("stg_clothing_summary.sql"), "SELECT 1").unwrap();

    let files = DiscoveredFiles {
        model_sql_files: vec![
            project_dir.join("models/combined.sql"),
            project_dir.join("models/stg_electronics_summary.sql"),
            project_dir.join("models/stg_clothing_summary.sql"),
        ],
        ..Default::default()
    };

    // Provide project-level vars
    let mut vars = HashMap::new();
    vars.insert(
        "product_categories".to_string(),
        serde_json::json!(["electronics", "clothing"]),
    );

    let graph = build_graph(&project_dir, &files, None, true, false, &vars).unwrap();

    // 3 model nodes: combined + stg_electronics_summary + stg_clothing_summary
    assert_eq!(graph.node_count(), 3);

    // 2 edges: stg_electronics_summary → combined, stg_clothing_summary → combined
    assert_eq!(graph.edge_count(), 2);

    let combined = graph
        .node_indices()
        .find(|&i| graph[i].label == "combined")
        .unwrap();
    let electronics = graph
        .node_indices()
        .find(|&i| graph[i].label == "stg_electronics_summary")
        .unwrap();
    let clothing = graph
        .node_indices()
        .find(|&i| graph[i].label == "stg_clothing_summary")
        .unwrap();
    assert!(graph.contains_edge(electronics, combined));
    assert!(graph.contains_edge(clothing, combined));
}

#[test]
fn test_build_graph_yaml_only_snapshot() {
    let (_tmp, project_dir) = setup_temp_project();

    let models_dir = project_dir.join("models");
    // Use a simple SQL file with no source refs to keep node count predictable
    fs::write(models_dir.join("stg_orders.sql"), "SELECT 1").unwrap();
    fs::write(
        models_dir.join("snap_schema.yml"),
        r#"
version: 2
snapshots:
  - name: snap_orders
    description: Orders snapshot
    relation: ref('stg_orders')
  - name: snap_no_relation
    description: Snapshot without upstream
"#,
    )
    .unwrap();

    let files = DiscoveredFiles {
        model_sql_files: vec![project_dir.join("models/stg_orders.sql")],
        yaml_files: vec![project_dir.join("models/snap_schema.yml")],
        ..Default::default()
    };

    let graph = build_graph(&project_dir, &files, None, true, false, &HashMap::new()).unwrap();

    // 1 model + 2 snapshots = 3 nodes
    assert_eq!(graph.node_count(), 3);

    let snap_orders_idx = graph
        .node_indices()
        .find(|&i| graph[i].label == "snap_orders")
        .unwrap();
    let snap_no_rel_idx = graph
        .node_indices()
        .find(|&i| graph[i].label == "snap_no_relation")
        .unwrap();
    let model_idx = graph
        .node_indices()
        .find(|&i| graph[i].label == "stg_orders")
        .unwrap();

    assert_eq!(graph[snap_orders_idx].node_type, NodeType::Snapshot);
    assert_eq!(
        graph[snap_orders_idx].description.as_deref(),
        Some("Orders snapshot")
    );
    assert_eq!(graph[snap_no_rel_idx].node_type, NodeType::Snapshot);

    // snap_orders gets an edge from the upstream model via relation
    assert_eq!(graph.edge_count(), 1);
    assert!(graph.contains_edge(model_idx, snap_orders_idx));
    // snap_no_relation has no edge
    assert!(!graph.contains_edge(model_idx, snap_no_rel_idx));
}

#[test]
fn test_build_graph_yaml_snapshot_sql_takes_precedence() {
    // When both a SQL file and YAML definition exist for the same snapshot name,
    // the SQL file registers the node; the YAML definition is skipped (no duplicate).
    let (_tmp, project_dir) = setup_temp_project();

    let models_dir = project_dir.join("models");
    // Use a simple SQL file with no source refs to keep node count predictable
    fs::write(models_dir.join("stg_orders.sql"), "SELECT 1").unwrap();

    let snap_dir = project_dir.join("snapshots");
    fs::create_dir_all(&snap_dir).unwrap();
    fs::write(
        snap_dir.join("snap_orders.sql"),
        "SELECT * FROM {{ ref('stg_orders') }}",
    )
    .unwrap();
    fs::write(
        snap_dir.join("snap_schema.yml"),
        r#"
version: 2
snapshots:
  - name: snap_orders
    relation: ref('stg_orders')
"#,
    )
    .unwrap();

    let files = DiscoveredFiles {
        model_sql_files: vec![project_dir.join("models/stg_orders.sql")],
        snapshot_sql_files: vec![project_dir.join("snapshots/snap_orders.sql")],
        yaml_files: vec![project_dir.join("snapshots/snap_schema.yml")],
        ..Default::default()
    };

    let graph = build_graph(&project_dir, &files, None, true, false, &HashMap::new()).unwrap();

    // 1 model + 1 snapshot = 2 nodes (no duplicate snapshot node)
    let snap_count = graph
        .node_indices()
        .filter(|&i| graph[i].node_type == NodeType::Snapshot)
        .count();
    assert_eq!(snap_count, 1);

    // Edge comes from SQL file's ref() call
    assert_eq!(graph.edge_count(), 1);
}

#[test]
fn test_build_graph_yaml_only_snapshot_source_relation() {
    // YAML-only snapshot with relation: source('schema', 'table') should create
    // a Source edge to the matching source node (or a phantom if undefined).
    let (_tmp, project_dir) = setup_temp_project();

    let models_dir = project_dir.join("models");
    fs::write(
        models_dir.join("snap_schema.yml"),
        r#"
version: 2
sources:
  - name: raw
    tables:
      - name: orders
snapshots:
  - name: snap_raw_orders
    description: Raw orders snapshot
    relation: source('raw', 'orders')
"#,
    )
    .unwrap();

    let files = DiscoveredFiles {
        yaml_files: vec![project_dir.join("models/snap_schema.yml")],
        ..Default::default()
    };

    let graph = build_graph(&project_dir, &files, None, true, false, &HashMap::new()).unwrap();

    // 1 source + 1 snapshot = 2 nodes
    assert_eq!(graph.node_count(), 2);

    let source_idx = graph
        .node_indices()
        .find(|&i| graph[i].node_type == NodeType::Source)
        .unwrap();
    let snap_idx = graph
        .node_indices()
        .find(|&i| graph[i].node_type == NodeType::Snapshot)
        .unwrap();

    assert_eq!(graph[snap_idx].label, "snap_raw_orders");
    assert_eq!(
        graph[snap_idx].description.as_deref(),
        Some("Raw orders snapshot")
    );

    // Source edge: source.raw.orders → snap_raw_orders
    assert_eq!(graph.edge_count(), 1);
    assert!(graph.contains_edge(source_idx, snap_idx));

    use petgraph::visit::IntoEdgeReferences;
    let edge = graph.edge_references().next().unwrap();
    assert_eq!(edge.weight().edge_type, EdgeType::Source);
}

#[test]
fn test_build_graph_yaml_only_snapshot_phantom_source_relation() {
    // When source('schema', 'table') references an undefined source, a phantom
    // node is created and a Source edge is still added.
    let (_tmp, project_dir) = setup_temp_project();

    let models_dir = project_dir.join("models");
    fs::write(
        models_dir.join("snap_schema.yml"),
        r#"
version: 2
snapshots:
  - name: snap_unknown
    relation: source('undefined_schema', 'undefined_table')
"#,
    )
    .unwrap();

    let files = DiscoveredFiles {
        yaml_files: vec![project_dir.join("models/snap_schema.yml")],
        ..Default::default()
    };

    let graph = build_graph(&project_dir, &files, None, true, false, &HashMap::new()).unwrap();

    // 1 phantom source + 1 snapshot = 2 nodes
    assert_eq!(graph.node_count(), 2);

    let phantom_idx = graph
        .node_indices()
        .find(|&i| graph[i].node_type == NodeType::Phantom)
        .unwrap();
    let snap_idx = graph
        .node_indices()
        .find(|&i| graph[i].node_type == NodeType::Snapshot)
        .unwrap();

    assert_eq!(graph[phantom_idx].label, "undefined_schema.undefined_table");
    assert_eq!(graph.edge_count(), 1);
    assert!(graph.contains_edge(phantom_idx, snap_idx));
}

#[test]
fn test_build_graph_yaml_only_snapshot_ref_forward_declaration() {
    // A YAML-only snapshot whose relation: ref(...) points to another YAML-only
    // snapshot declared *later* in the same file must resolve to snapshot.<name>,
    // not create a phantom model.<name> node.
    let (_tmp, project_dir) = setup_temp_project();

    let models_dir = project_dir.join("models");
    fs::write(
        models_dir.join("snap_schema.yml"),
        r#"
version: 2
snapshots:
  - name: snap_downstream
    description: References snap_upstream which is declared after this
    relation: ref('snap_upstream')
  - name: snap_upstream
    description: The upstream snapshot
"#,
    )
    .unwrap();

    let files = DiscoveredFiles {
        yaml_files: vec![project_dir.join("models/snap_schema.yml")],
        ..Default::default()
    };

    let graph = build_graph(&project_dir, &files, None, true, false, &HashMap::new()).unwrap();

    // 2 snapshots, no phantom nodes
    assert_eq!(graph.node_count(), 2);
    assert!(
        graph
            .node_indices()
            .all(|i| graph[i].node_type == NodeType::Snapshot)
    );

    let upstream_idx = graph
        .node_indices()
        .find(|&i| graph[i].label == "snap_upstream")
        .unwrap();
    let downstream_idx = graph
        .node_indices()
        .find(|&i| graph[i].label == "snap_downstream")
        .unwrap();

    assert_eq!(graph.edge_count(), 1);
    assert!(graph.contains_edge(upstream_idx, downstream_idx));
}

#[test]
fn test_build_graph_semantic_layer_full() {
    let (_tmp, project_dir) = setup_temp_project();

    // Add a semantic layer YAML alongside the existing schema.yml
    let models_dir = project_dir.join("models");
    fs::write(
        models_dir.join("semantic.yml"),
        r#"
semantic_models:
  - name: orders
    description: Order semantic model
    model: ref('orders')
    measures:
      - name: order_count
      - name: order_total

metrics:
  - name: orders
    type: simple
    type_params:
      measure: order_count
  - name: order_total
    type: simple
    type_params:
      measure: order_total
  - name: revenue_per_order
    type: ratio
    type_params:
      numerator: order_total
      denominator: orders

saved_queries:
  - name: order_kpis
    description: Key order KPIs
    query_params:
      metrics:
        - orders
        - order_total
        - revenue_per_order
"#,
    )
    .unwrap();

    let files = DiscoveredFiles {
        model_sql_files: vec![
            project_dir.join("models/stg_orders.sql"),
            project_dir.join("models/orders.sql"),
        ],
        yaml_files: vec![
            project_dir.join("models/schema.yml"),
            project_dir.join("models/semantic.yml"),
        ],
        ..Default::default()
    };

    let graph = build_graph(&project_dir, &files, None, true, false, &HashMap::new()).unwrap();

    // Node type counts
    let counts: HashMap<NodeType, usize> =
        graph.node_indices().fold(HashMap::new(), |mut acc, i| {
            *acc.entry(graph[i].node_type).or_insert(0) += 1;
            acc
        });

    // 1 source + 2 models + 1 semantic_model + 3 metrics + 1 saved_query = 8
    assert_eq!(*counts.get(&NodeType::Source).unwrap_or(&0), 1, "sources");
    assert_eq!(*counts.get(&NodeType::Model).unwrap_or(&0), 2, "models");
    assert_eq!(
        *counts.get(&NodeType::SemanticModel).unwrap_or(&0),
        1,
        "semantic_models"
    );
    assert_eq!(*counts.get(&NodeType::Metric).unwrap_or(&0), 3, "metrics");
    assert_eq!(
        *counts.get(&NodeType::SavedQuery).unwrap_or(&0),
        1,
        "saved_queries"
    );

    // Verify semantic_model.orders exists and is linked to model.orders
    let sem_idx = graph
        .node_indices()
        .find(|&i| graph[i].unique_id == "semantic_model.orders")
        .expect("semantic_model.orders not found");
    let model_orders_idx = graph
        .node_indices()
        .find(|&i| graph[i].unique_id == "model.orders")
        .expect("model.orders not found");
    assert!(
        graph.contains_edge(model_orders_idx, sem_idx),
        "model.orders → semantic_model.orders edge missing"
    );

    // Verify metric.orders is linked to semantic_model.orders
    let metric_orders_idx = graph
        .node_indices()
        .find(|&i| graph[i].unique_id == "metric.orders")
        .expect("metric.orders not found");
    assert!(
        graph.contains_edge(sem_idx, metric_orders_idx),
        "semantic_model.orders → metric.orders edge missing"
    );

    // Verify revenue_per_order depends on order_total and orders metrics
    let ratio_idx = graph
        .node_indices()
        .find(|&i| graph[i].unique_id == "metric.revenue_per_order")
        .expect("metric.revenue_per_order not found");
    let metric_total_idx = graph
        .node_indices()
        .find(|&i| graph[i].unique_id == "metric.order_total")
        .expect("metric.order_total not found");
    assert!(
        graph.contains_edge(metric_total_idx, ratio_idx),
        "metric.order_total → metric.revenue_per_order edge missing"
    );
    assert!(
        graph.contains_edge(metric_orders_idx, ratio_idx),
        "metric.orders → metric.revenue_per_order edge missing"
    );

    // Verify saved_query.order_kpis is linked to all 3 metrics
    let sq_idx = graph
        .node_indices()
        .find(|&i| graph[i].unique_id == "saved_query.order_kpis")
        .expect("saved_query.order_kpis not found");
    assert!(
        graph.contains_edge(metric_orders_idx, sq_idx),
        "metric.orders → saved_query.order_kpis edge missing"
    );
    assert!(
        graph.contains_edge(metric_total_idx, sq_idx),
        "metric.order_total → saved_query.order_kpis edge missing"
    );
    assert!(
        graph.contains_edge(ratio_idx, sq_idx),
        "metric.revenue_per_order → saved_query.order_kpis edge missing"
    );
}

#[test]
fn test_build_graph_semantic_layer_metric_reference_shapes() {
    let (_tmp, project_dir) = setup_temp_project();

    let models_dir = project_dir.join("models");
    fs::write(
        models_dir.join("semantic_refs.yml"),
        r#"
semantic_models:
  - name: orders
    model: ref('orders')
    measures:
      - name: order_total
      - name: order_count
      - name: customer_count

metrics:
  - name: order_total
    type: simple
    type_params:
      measure:
        name: order_total
        fill_nulls_with: 0
  - name: orders
    type: simple
    type_params:
      measure: order_count
  - name: customers
    type: simple
    type_params:
      measure: customer_count
  - name: derived_kpi
    type: derived
    type_params:
      metrics:
        - name: order_total
        - orders
        - customers
"#,
    )
    .unwrap();

    let files = DiscoveredFiles {
        model_sql_files: vec![
            project_dir.join("models/stg_orders.sql"),
            project_dir.join("models/orders.sql"),
        ],
        yaml_files: vec![
            project_dir.join("models/schema.yml"),
            project_dir.join("models/semantic_refs.yml"),
        ],
        ..Default::default()
    };

    let graph = build_graph(&project_dir, &files, None, true, false, &HashMap::new()).unwrap();

    let sem_idx = graph
        .node_indices()
        .find(|&i| graph[i].unique_id == "semantic_model.orders")
        .expect("semantic_model.orders not found");
    let metric_total_idx = graph
        .node_indices()
        .find(|&i| graph[i].unique_id == "metric.order_total")
        .expect("metric.order_total not found");
    assert!(
        graph.contains_edge(sem_idx, metric_total_idx),
        "semantic_model.orders → metric.order_total edge missing"
    );

    let derived_idx = graph
        .node_indices()
        .find(|&i| graph[i].unique_id == "metric.derived_kpi")
        .expect("metric.derived_kpi not found");
    for metric_id in ["metric.order_total", "metric.orders", "metric.customers"] {
        let metric_idx = graph
            .node_indices()
            .find(|&i| graph[i].unique_id == metric_id)
            .unwrap_or_else(|| panic!("{metric_id} not found"));
        assert!(
            graph.contains_edge(metric_idx, derived_idx),
            "{metric_id} → metric.derived_kpi edge missing"
        );
    }
}