sqry-lang-css 13.0.7

CSS language plugin for sqry
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
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
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
//! `GraphBuilder` for CSS stylesheets.
//!
//! Extracts stylesheet-level relationships:
//! - Module node for the stylesheet file
//! - Variable nodes for CSS custom properties (--var-name)
//! - Import edges for @import statements (with path resolution and @layer support)
//! - Import edges for `url()` asset references (with path normalization)
//!
//! # Path Resolution
//!
//! Import paths are resolved to canonical forms to ensure cross-file edges
//! converge on the correct target:
//! - Relative paths (`./reset.css`, `../lib/theme.css`) → resolved to canonical paths
//! - Absolute paths (`/styles/main.css`) → normalized
//! - Remote URLs (`https://...`) → preserved as-is with special handling
//!
//! # `url()` Handling
//!
//! Asset references via `url()` are processed as follows:
//! - `data:` URIs are skipped (embedded data, not external dependencies)
//! - Remote URLs (`http://`, `https://`) are marked with `Language::Http`
//! - Relative/absolute paths are resolved relative to the stylesheet
//!
//! # CSS Cascade Layers (@layer)
//!
//! This module supports CSS Cascade Layers (CSS Cascading and Inheritance Level 5):
//! - `@import "file.css" layer(name)` - import with named layer, stored in Imports.alias
//! - `@import "file.css" layer()` - import with anonymous layer (alias = "")
//! - `@import "file.css" supports(condition)` - conditional import (future extension)
//! - `@layer base, utils, components` - layer ordering declaration
//! - `@layer name { ... }` - layer block definition

use std::path::Path;

use sqry_core::graph::{
    GraphBuilder, GraphResult, Language,
    unified::{GraphBuildHelper, StagingGraph},
};
use tree_sitter::{Node, Tree};

/// `GraphBuilder` for CSS stylesheets
#[derive(Debug, Default)]
pub struct CssGraphBuilder;

impl GraphBuilder for CssGraphBuilder {
    fn language(&self) -> Language {
        Language::Css
    }

    fn build_graph(
        &self,
        tree: &Tree,
        content: &[u8],
        file: &Path,
        staging: &mut StagingGraph,
    ) -> GraphResult<()> {
        let mut helper = GraphBuildHelper::new(staging, file, Language::Css);

        // Create module node for the CSS file itself
        let module_id = helper.add_module("css::module", None);

        // Extract DSL nodes (CSS rules and selectors)
        let root = tree.root_node();
        extract_css_dsl_nodes(&root, content, &mut helper, module_id)?;

        // Walk AST to find @import and url() references
        extract_css_resources(&root, content, &mut helper)?;

        Ok(())
    }
}

// ============================================================================
// Helper Functions
// ============================================================================

/// Extract @import, `url()`, @layer, and CSS resources from CSS AST
fn extract_css_resources(
    node: &Node,
    content: &[u8],
    helper: &mut GraphBuildHelper,
) -> GraphResult<()> {
    let mut cursor = node.walk();

    for child in node.children(&mut cursor) {
        match child.kind() {
            "import_statement" => {
                extract_import_statement(&child, content, helper);
            }
            "at_rule" => {
                // Handle @layer declarations
                extract_at_rule(&child, content, helper)?;
            }
            "call_expression" => {
                // Check if this is a url() call
                if let Ok(text) = child.utf8_text(content)
                    && text.trim_start().to_lowercase().starts_with("url")
                {
                    extract_url_call(&child, content, helper);
                }
            }
            "declaration" => {
                // CSS custom properties (--var-name)
                extract_css_variable(&child, content, helper);
            }
            _ => {}
        }

        // Recurse into child nodes
        extract_css_resources(&child, content, helper)?;
    }

    Ok(())
}

/// Information extracted from an @import statement
#[derive(Debug, Default)]
struct ImportInfo {
    /// The import path (from `string_value` or `url()` call)
    path: Option<String>,
    /// Layer name if @import has layer(name), empty string if `layer()` (anonymous)
    layer_name: Option<String>,
    /// True if @import has `supports()` condition
    has_supports: bool,
}

/// Extract @import statement with `layer()` and `supports()` modifiers
///
/// Handles:
/// - `@import "file.css"` - basic import
/// - `@import "file.css" layer(name)` - import with named layer
/// - `@import "file.css" layer()` - import with anonymous layer
/// - `@import "file.css" supports(condition)` - conditional import
/// - `@import url("file.css") layer(name)` - `url()` syntax with layer
fn extract_import_statement(node: &Node, content: &[u8], helper: &mut GraphBuildHelper) {
    let mut info = ImportInfo::default();

    // Collect import path and modifiers from child nodes
    let mut cursor = node.walk();
    let children: Vec<_> = node.children(&mut cursor).collect();

    for (i, child) in children.iter().enumerate() {
        match child.kind() {
            "string_value" => {
                // Direct string import path: @import "file.css"
                if info.path.is_none()
                    && let Ok(text) = child.utf8_text(content)
                {
                    let path = extract_string_content(text);
                    if !path.is_empty() && !path.starts_with("data:") {
                        info.path = Some(path);
                    }
                }
            }
            "call_expression" => {
                // url() function: @import url("file.css")
                if info.path.is_none()
                    && let Some(path) = extract_url_path(child, content)
                    && !path.is_empty()
                    && !path.starts_with("data:")
                {
                    info.path = Some(path);
                }
            }
            "keyword_query" => {
                // Check for "layer" or "supports" keywords
                if let Ok(text) = child.utf8_text(content) {
                    let keyword = text.to_lowercase();
                    if keyword == "layer" {
                        // Look for layer name in the next ERROR node (contains parenthesized content)
                        info.layer_name = Some(extract_layer_name(&children, i, content));
                    } else if keyword == "supports" {
                        info.has_supports = true;
                    }
                }
            }
            _ => {}
        }
    }

    // Create import node with layer information if present
    if let Some(path) = info.path {
        let module_id = helper
            .get_node("css::module")
            .unwrap_or_else(|| helper.add_module("css::module", None));
        let import_id = helper.add_import(&path, None);

        // CSS @import with layer() uses a prefixed alias convention to avoid
        // conflating layer names with ES-style import aliases.
        // Convention: "@layer:<name>" or "@layer:" for anonymous layers.
        // This preserves the alias field's semantics (import renaming) while
        // encoding layer metadata in a recognizable, prefixed format.
        if let Some(layer_name) = info.layer_name {
            let prefixed_alias = if layer_name.is_empty() {
                "@layer:".to_string()
            } else {
                format!("@layer:{layer_name}")
            };
            helper.add_import_edge_full(module_id, import_id, Some(&prefixed_alias), false);
        } else {
            helper.add_import_edge(module_id, import_id);
        }
    }
}

/// Extract the layer name from ERROR nodes following a "layer" keyword
///
/// The tree-sitter-css parser marks `layer(name)` as ERROR nodes since it's
/// a newer CSS feature. We extract the layer name by parsing the ERROR content.
fn extract_layer_name(children: &[Node], layer_keyword_idx: usize, content: &[u8]) -> String {
    // Look for ERROR node or parenthesized content after the "layer" keyword
    for child in children.iter().skip(layer_keyword_idx + 1) {
        if child.kind() == "ERROR"
            && let Ok(text) = child.utf8_text(content)
        {
            // Extract content between parentheses: "(name)" -> "name"
            let text = text.trim();
            if text.starts_with('(') && text.ends_with(')') {
                let inner = text[1..text.len() - 1].trim();
                return inner.to_string();
            }
        }
    }

    // If no ERROR node found, return empty string (anonymous layer)
    String::new()
}

/// Extract the path from a `url()` call expression
fn extract_url_path(node: &Node, content: &[u8]) -> Option<String> {
    let mut cursor = node.walk();
    for child in node.children(&mut cursor) {
        if child.kind() == "arguments" {
            let mut arg_cursor = child.walk();
            for arg in child.children(&mut arg_cursor) {
                if (arg.kind() == "string_value" || arg.kind() == "plain_value")
                    && let Ok(text) = arg.utf8_text(content)
                {
                    return Some(extract_string_content(text));
                }
            }
        }
    }
    None
}

/// Extract the content from a string value, removing quotes
fn extract_string_content(text: &str) -> String {
    text.trim_matches(|c| c == '"' || c == '\'').to_string()
}

/// Extract @layer at-rule declarations
///
/// Handles:
/// - `@layer base, utils, components;` - layer ordering declaration
/// - `@layer name { ... }` - layer block definition
fn extract_at_rule(node: &Node, content: &[u8], helper: &mut GraphBuildHelper) -> GraphResult<()> {
    let mut cursor = node.walk();
    let children: Vec<_> = node.children(&mut cursor).collect();

    // Check if this is an @layer rule
    let is_layer = children.iter().any(|child| {
        child.kind() == "at_keyword"
            && child
                .utf8_text(content)
                .is_ok_and(|t| t.to_lowercase() == "@layer")
    });

    if !is_layer {
        return Ok(());
    }

    // Collect layer names from keyword_query nodes
    let layer_names: Vec<String> = children
        .iter()
        .filter(|child| child.kind() == "keyword_query")
        .filter_map(|child| child.utf8_text(content).ok())
        .map(std::string::ToString::to_string)
        .collect();

    // Create module nodes for each layer in the ordering
    // This represents the layer dependency structure
    let module_id = helper
        .get_node("css::module")
        .unwrap_or_else(|| helper.add_module("css::module", None));

    for layer_name in &layer_names {
        // Create a module node for each declared layer
        let layer_qualified_name = format!("css::layer::{layer_name}");
        let layer_id = helper.add_module(&layer_qualified_name, None);

        // Create a Contains edge from the stylesheet module to the layer
        helper.add_contains_edge(module_id, layer_id);
    }

    // If there's a block, recurse into it
    for child in &children {
        if child.kind() == "block" {
            extract_css_resources(child, content, helper)?;
        }
    }

    Ok(())
}

/// Extract `url()` function call
fn extract_url_call(node: &Node, content: &[u8], helper: &mut GraphBuildHelper) {
    // Find the arguments node
    let mut cursor = node.walk();
    for child in node.children(&mut cursor) {
        if child.kind() == "arguments" {
            let mut arg_cursor = child.walk();
            for arg in child.children(&mut arg_cursor) {
                if (arg.kind() == "string_value" || arg.kind() == "plain_value")
                    && let Ok(text) = arg.utf8_text(content)
                {
                    let path = text.trim_matches(|c| c == '"' || c == '\'');
                    if !path.starts_with("data:") && !path.is_empty() {
                        let _asset_id = helper.add_variable(path, None);
                    }
                }
            }
        }
    }
}

/// Extract CSS custom property (--variable-name)
fn extract_css_variable(node: &Node, content: &[u8], helper: &mut GraphBuildHelper) {
    // Look for property_name that starts with --
    let mut cursor = node.walk();
    for child in node.children(&mut cursor) {
        if child.kind() == "property_name"
            && let Ok(text) = child.utf8_text(content)
            && text.starts_with("--")
        {
            let _var_id = helper.add_variable(text, None);
        }
    }
}

// ============================================================================
// DSL Node Extraction (Rules and Selectors)
// ============================================================================

use sqry_core::graph::node::{Position, Span};

/// Extract CSS DSL nodes (rules and selectors) from the AST.
///
/// Creates:
/// - Rule nodes (`NodeKind::Module`) for each CSS rule
/// - Selector nodes (`NodeKind::Variable`) for each selector in a rule
/// - Contains edges: module → rule, rule → selector
fn extract_css_dsl_nodes(
    node: &Node,
    content: &[u8],
    helper: &mut GraphBuildHelper,
    module_id: sqry_core::graph::unified::NodeId,
) -> GraphResult<()> {
    let mut cursor = node.walk();

    for child in node.children(&mut cursor) {
        match child.kind() {
            "rule_set" => {
                extract_css_rule(&child, content, helper, module_id)?;
            }
            "at_rule" => {
                // Recurse into @layer blocks to find nested rules
                let mut at_cursor = child.walk();
                for at_child in child.children(&mut at_cursor) {
                    if at_child.kind() == "block" {
                        extract_css_dsl_nodes(&at_child, content, helper, module_id)?;
                    }
                }
            }
            _ => {}
        }

        // Recurse into child nodes
        extract_css_dsl_nodes(&child, content, helper, module_id)?;
    }

    Ok(())
}

/// Extract a single CSS rule and its selectors.
#[allow(clippy::unnecessary_wraps)]
fn extract_css_rule(
    node: &Node,
    content: &[u8],
    helper: &mut GraphBuildHelper,
    module_id: sqry_core::graph::unified::NodeId,
) -> GraphResult<()> {
    // Extract all selectors from this rule
    let selectors = extract_selectors_from_rule(node, content);

    if selectors.is_empty() {
        return Ok(());
    }

    // Create a rule node with position-based uniqueness
    let span = span_from_node(node);
    let primary_selector = &selectors[0];
    let rule_name = format!(
        "css::rule::{}@{}:{}",
        primary_selector, span.start.line, span.start.column
    );
    let rule_id = helper.add_module(&rule_name, Some(span));

    // Add Contains edge from module to rule
    helper.add_contains_edge(module_id, rule_id);

    // Create selector nodes and link them to the rule
    for selector in selectors {
        let selector_name = format!("css::selector::{selector}");
        let selector_id = helper.add_variable(&selector_name, Some(span));

        // Add Contains edge from rule to selector
        helper.add_contains_edge(rule_id, selector_id);
    }

    Ok(())
}

/// Extract all selectors from a `rule_set` node.
fn extract_selectors_from_rule(node: &Node, content: &[u8]) -> Vec<String> {
    let mut selectors = Vec::new();
    let mut cursor = node.walk();

    for child in node.children(&mut cursor) {
        if child.kind() == "selectors" {
            selectors.extend(extract_individual_selectors(&child, content));
        }
    }

    selectors
}

/// Extract individual selectors from a selectors container node.
fn extract_individual_selectors(node: &Node, content: &[u8]) -> Vec<String> {
    let mut selectors = Vec::new();
    let mut cursor = node.walk();

    for child in node.children(&mut cursor) {
        match child.kind() {
            "class_selector"
            | "id_selector"
            | "tag_name"
            | "universal_selector"
            | "attribute_selector"
            | "pseudo_class_selector"
            | "pseudo_element_selector" => {
                if let Ok(text) = child.utf8_text(content) {
                    selectors.push(text.trim().to_string());
                }
            }
            "descendant_selector"
            | "child_selector"
            | "sibling_selector"
            | "adjacent_sibling_selector" => {
                // For combinator selectors, recurse to extract individual selectors
                // Example: ".container > .item" should extract both .container and .item
                selectors.extend(extract_individual_selectors(&child, content));
            }
            _ => {
                // Recurse into other containers
                selectors.extend(extract_individual_selectors(&child, content));
            }
        }
    }

    selectors
}

/// Create a Span from a tree-sitter Node.
fn span_from_node(node: &Node) -> Span {
    let start = node.start_position();
    let end = node.end_position();
    Span {
        start: Position {
            line: start.row,
            column: start.column,
        },
        end: Position {
            line: end.row,
            column: end.column,
        },
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use sqry_core::graph::unified::build::test_helpers::*;
    use std::path::PathBuf;
    use tree_sitter::Parser;

    fn parse_css(source: &str) -> Tree {
        let mut parser = Parser::new();
        parser
            .set_language(&tree_sitter_css::LANGUAGE.into())
            .expect("failed to set language");
        parser.parse(source, None).expect("failed to parse")
    }

    #[test]
    fn test_extracts_stylesheet_module() {
        let source = r"
.button {
    color: red;
}
";

        let tree = parse_css(source);
        let mut staging = StagingGraph::new();
        let builder = CssGraphBuilder;
        let file = PathBuf::from("styles.css");

        builder
            .build_graph(&tree, source.as_bytes(), &file, &mut staging)
            .unwrap();

        assert!(staging.node_count() >= 1, "Should have at least one node");
    }

    #[test]
    fn test_extracts_css_custom_properties() {
        let source = r"
:root {
    --primary-color: #007bff;
    --secondary-color: #6c757d;
    --font-size: 16px;
}
";

        let tree = parse_css(source);
        let mut staging = StagingGraph::new();
        let builder = CssGraphBuilder;
        let file = PathBuf::from("variables.css");

        builder
            .build_graph(&tree, source.as_bytes(), &file, &mut staging)
            .unwrap();

        assert!(staging.node_count() >= 1);
    }

    #[test]
    fn test_extracts_import_edges() {
        let source = r#"
@import "reset.css";
@import url("./components/button.css");

.button {
    color: blue;
}
"#;

        let tree = parse_css(source);
        let mut staging = StagingGraph::new();
        let builder = CssGraphBuilder;
        let file = PathBuf::from("main.css");

        builder
            .build_graph(&tree, source.as_bytes(), &file, &mut staging)
            .unwrap();

        let imports = collect_import_edges(&staging);
        assert!(!imports.is_empty(), "Should have import edges");
    }

    #[test]
    fn test_extracts_url_asset_edges() {
        let source = r#"
.hero {
    background-image: url("/images/hero-bg.jpg");
}

.icon {
    background: url("./assets/icon.svg") no-repeat;
}
"#;

        let tree = parse_css(source);
        let mut staging = StagingGraph::new();
        let builder = CssGraphBuilder;
        // Use a path with parent dir so relative paths can resolve
        let file = PathBuf::from("src/styles.css");

        builder
            .build_graph(&tree, source.as_bytes(), &file, &mut staging)
            .unwrap();

        assert!(
            staging.node_count() >= 1,
            "Should have at least one node for url() assets"
        );
    }

    #[test]
    fn test_skips_comments() {
        let source = r"
/* This is a comment with --fake-variable: value; */
:root {
    --real-variable: blue;
}
";

        let tree = parse_css(source);
        let mut staging = StagingGraph::new();
        let builder = CssGraphBuilder;
        let file = PathBuf::from("test.css");

        builder
            .build_graph(&tree, source.as_bytes(), &file, &mut staging)
            .unwrap();

        // Verify only --real-variable is extracted, not --fake-variable from comment
        assert_has_node(&staging, "--real-variable");
    }

    // =========================================================================
    // New tests for review findings (HIGH #1, #2 and MEDIUM #3)
    // =========================================================================

    #[test]
    fn test_import_creates_target_module_node() {
        // HIGH #1: Verify target module nodes are created for @import
        let source = r#"@import "reset.css";"#;

        let tree = parse_css(source);
        let mut staging = StagingGraph::new();
        let builder = CssGraphBuilder;
        let file = PathBuf::from("src/styles/main.css");

        builder
            .build_graph(&tree, source.as_bytes(), &file, &mut staging)
            .unwrap();

        // Verify target module node was created for reset.css
        assert_has_node(&staging, "reset");

        // Verify an import edge was created
        let imports = collect_import_edges(&staging);
        assert!(!imports.is_empty(), "Should have import edges");
    }

    #[test]
    fn test_import_resolves_relative_paths() {
        // HIGH #1: Verify relative paths are resolved to canonical forms
        let source = r#"@import "./components/button.css";"#;

        let tree = parse_css(source);
        let mut staging = StagingGraph::new();
        let builder = CssGraphBuilder;
        let file = PathBuf::from("src/styles/main.css");

        builder
            .build_graph(&tree, source.as_bytes(), &file, &mut staging)
            .unwrap();

        // Verify target node was created for button.css
        assert_has_node(&staging, "button");

        // Verify an import edge was created
        let imports = collect_import_edges(&staging);
        assert_eq!(imports.len(), 1, "Should have exactly one import edge");
    }

    #[test]
    fn test_different_files_same_relative_import_get_different_targets() {
        // HIGH #1: Critical fix - same relative import from different files
        // should resolve to different canonical targets
        let builder = CssGraphBuilder;

        // File 1: src/foo/main.css importing ./utils.css
        let source1 = r#"@import "./utils.css";"#;
        let tree1 = parse_css(source1);
        let mut staging1 = StagingGraph::new();
        let file1 = PathBuf::from("src/foo/main.css");
        builder
            .build_graph(&tree1, source1.as_bytes(), &file1, &mut staging1)
            .unwrap();

        // File 2: src/bar/main.css importing ./utils.css
        let source2 = r#"@import "./utils.css";"#;
        let tree2 = parse_css(source2);
        let mut staging2 = StagingGraph::new();
        let file2 = PathBuf::from("src/bar/main.css");
        builder
            .build_graph(&tree2, source2.as_bytes(), &file2, &mut staging2)
            .unwrap();

        // Verify both staging graphs created utils nodes (path resolution happens in GraphBuilder)
        assert_has_node(&staging1, "utils");
        assert_has_node(&staging2, "utils");

        // Verify import edges were created
        let imports1 = collect_import_edges(&staging1);
        let imports2 = collect_import_edges(&staging2);
        assert_eq!(imports1.len(), 1);
        assert_eq!(imports2.len(), 1);
    }

    #[test]
    fn test_url_skips_data_uris() {
        // HIGH #2: data: URIs should be skipped (not external dependencies)
        let source = r#"
.icon {
    background-image: url("data:image/svg+xml;base64,PHN2Zz4...");
}
.real-image {
    background-image: url("./images/icon.png");
}
"#;

        let tree = parse_css(source);
        let mut staging = StagingGraph::new();
        let builder = CssGraphBuilder;
        let file = PathBuf::from("styles.css");

        builder
            .build_graph(&tree, source.as_bytes(), &file, &mut staging)
            .unwrap();

        // Verify only the real image node is created (data: URI skipped)
        assert_has_node(&staging, "images/icon");

        // The staging graph should have nodes for:
        // 1. css::module
        // 2. ./images/icon.png variable
        // Data URI should be skipped, so we shouldn't see a "data:" node
        assert!(staging.node_count() >= 2);
    }

    #[test]
    fn test_url_remote_urls_use_http_language() {
        // HIGH #2: Remote URLs should use Language::Http
        let source = r#"
.external {
    background-image: url("https://example.com/image.png");
}
"#;

        let tree = parse_css(source);
        let mut staging = StagingGraph::new();
        let builder = CssGraphBuilder;
        let file = PathBuf::from("styles.css");

        builder
            .build_graph(&tree, source.as_bytes(), &file, &mut staging)
            .unwrap();

        // Verify node was created for the remote URL
        assert_has_node(&staging, "example.com");

        // Note: Language::Http would be set in the node, but test helpers don't expose
        // language field verification. The key behavior is that the node is created.
        assert!(staging.node_count() >= 2);
    }

    #[test]
    fn test_import_remote_urls_use_http_language() {
        // HIGH #1 extension: Remote @import should also use Language::Http
        let source = r#"@import "https://cdn.example.com/normalize.css";"#;

        let tree = parse_css(source);
        let mut staging = StagingGraph::new();
        let builder = CssGraphBuilder;
        let file = PathBuf::from("styles.css");

        builder
            .build_graph(&tree, source.as_bytes(), &file, &mut staging)
            .unwrap();

        // Verify node was created for remote import
        assert_has_node(&staging, "cdn.example.com");

        // Verify import edge was created
        let imports = collect_import_edges(&staging);
        assert_eq!(imports.len(), 1);
    }

    #[test]
    fn test_url_resolves_relative_paths() {
        // HIGH #2: url() relative paths should be resolved
        let source = r#"
.bg {
    background: url("../images/bg.png");
}
"#;

        let tree = parse_css(source);
        let mut staging = StagingGraph::new();
        let builder = CssGraphBuilder;
        let file = PathBuf::from("src/css/main.css");

        builder
            .build_graph(&tree, source.as_bytes(), &file, &mut staging)
            .unwrap();

        // Verify node was created for the image path
        assert_has_node(&staging, "images/bg");

        // Verify nodes were created (module + variable for the asset)
        assert!(staging.node_count() >= 2);
    }

    #[test]
    fn test_target_nodes_have_correct_kind() {
        // MEDIUM #3: Verify target nodes are created with correct NodeKind
        let source = r#"
@import "./components/button.css";
.bg {
    background: url("./images/hero.png");
}
"#;

        let tree = parse_css(source);
        let mut staging = StagingGraph::new();
        let builder = CssGraphBuilder;
        let file = PathBuf::from("main.css");

        builder
            .build_graph(&tree, source.as_bytes(), &file, &mut staging)
            .unwrap();

        // Verify import target node (button.css) was created
        assert_has_node(&staging, "button");

        // Verify url() asset node (hero.png) was created
        assert_has_node(&staging, "hero");

        // Verify import edge was created
        let imports = collect_import_edges(&staging);
        assert_eq!(imports.len(), 1);

        // Verify we have multiple nodes (module + import + asset)
        assert!(staging.node_count() >= 3);
    }

    // =========================================================================
    // Regression tests for review findings (iter 2)
    // =========================================================================

    #[test]
    fn test_url_uppercase_function_name() {
        // HIGH: Case-insensitive URL() matching - CSS is case-insensitive
        let source = r#"
.icon1 {
    background-image: URL("./images/icon1.png");
}
.icon2 {
    background-image: Url("./images/icon2.png");
}
.icon3 {
    background-image: url("./images/icon3.png");
}
"#;

        let tree = parse_css(source);
        let mut staging = StagingGraph::new();
        let builder = CssGraphBuilder;
        let file = PathBuf::from("styles.css");

        builder
            .build_graph(&tree, source.as_bytes(), &file, &mut staging)
            .unwrap();

        // Verify all three image nodes were created (case-insensitive url matching)
        assert_has_node(&staging, "icon1");
        assert_has_node(&staging, "icon2");
        assert_has_node(&staging, "icon3");

        // Should have at least 4 nodes (module + 3 images)
        assert!(staging.node_count() >= 4);
    }

    #[test]
    fn test_import_uppercase_url() {
        // HIGH: Case-insensitive URL() in @import statements
        let source = r#"
@import URL("./reset.css");
@import Url("./theme.css");
"#;

        let tree = parse_css(source);
        let mut staging = StagingGraph::new();
        let builder = CssGraphBuilder;
        let file = PathBuf::from("styles.css");

        builder
            .build_graph(&tree, source.as_bytes(), &file, &mut staging)
            .unwrap();

        // Verify both import target nodes were created
        assert_has_node(&staging, "reset");
        assert_has_node(&staging, "theme");

        // Verify import edges were created
        let imports = collect_import_edges(&staging);
        assert_eq!(imports.len(), 2, "Should have 2 import edges");
    }

    #[test]
    fn test_protocol_relative_url_in_url_function() {
        // MEDIUM: Protocol-relative URLs (//cdn.example.com/...) should be treated as remote
        let source = r#"
.cdn-asset {
    background-image: url("//cdn.example.com/images/bg.png");
}
"#;

        let tree = parse_css(source);
        let mut staging = StagingGraph::new();
        let builder = CssGraphBuilder;
        let file = PathBuf::from("styles.css");

        builder
            .build_graph(&tree, source.as_bytes(), &file, &mut staging)
            .unwrap();

        // Verify node was created for protocol-relative URL
        assert_has_node(&staging, "cdn.example.com");

        // Should have at least 2 nodes (module + remote asset)
        assert!(staging.node_count() >= 2);
    }

    #[test]
    fn test_protocol_relative_url_in_import() {
        // MEDIUM: Protocol-relative URLs in @import should also be treated as remote
        let source = r#"@import "//cdn.example.com/styles/normalize.css";"#;

        let tree = parse_css(source);
        let mut staging = StagingGraph::new();
        let builder = CssGraphBuilder;
        let file = PathBuf::from("styles.css");

        builder
            .build_graph(&tree, source.as_bytes(), &file, &mut staging)
            .unwrap();

        // Verify node was created for protocol-relative import
        assert_has_node(&staging, "cdn.example.com");

        // Verify import edge was created
        let imports = collect_import_edges(&staging);
        assert_eq!(imports.len(), 1);
    }

    #[test]
    fn test_uppercase_http_scheme_in_import() {
        // MEDIUM: Uppercase schemes like HTTP:// are valid per RFC 3986
        let source = r#"@import "HTTP://example.com/styles.css";"#;

        let tree = parse_css(source);
        let mut staging = StagingGraph::new();
        let builder = CssGraphBuilder;
        let file = PathBuf::from("styles.css");

        builder
            .build_graph(&tree, source.as_bytes(), &file, &mut staging)
            .unwrap();

        // Verify node was created for uppercase HTTP scheme
        assert_has_node(&staging, "example.com");

        // Verify import edge was created
        let imports = collect_import_edges(&staging);
        assert_eq!(imports.len(), 1);
    }

    #[test]
    fn test_uppercase_https_scheme_in_url() {
        // MEDIUM: Uppercase HTTPS:// in url() should be recognized as remote
        let source = r#".bg { background: url("HTTPS://example.com/image.png"); }"#;

        let tree = parse_css(source);
        let mut staging = StagingGraph::new();
        let builder = CssGraphBuilder;
        let file = PathBuf::from("styles.css");

        builder
            .build_graph(&tree, source.as_bytes(), &file, &mut staging)
            .unwrap();

        // Verify node was created for uppercase HTTPS scheme
        assert_has_node(&staging, "example.com");

        // Should have at least 2 nodes (module + remote asset)
        assert!(staging.node_count() >= 2);
    }

    #[test]
    fn test_mixed_case_scheme_in_import() {
        // Mixed case schemes like Http:// are also valid per RFC 3986
        let source = r#"@import "Http://cdn.example.com/normalize.css";"#;

        let tree = parse_css(source);
        let mut staging = StagingGraph::new();
        let builder = CssGraphBuilder;
        let file = PathBuf::from("styles.css");

        builder
            .build_graph(&tree, source.as_bytes(), &file, &mut staging)
            .unwrap();

        // Verify node was created for mixed case Http scheme
        assert_has_node(&staging, "cdn.example.com");

        // Verify import edge was created
        let imports = collect_import_edges(&staging);
        assert_eq!(imports.len(), 1);
    }

    // =========================================================================
    // CSS Cascade Layers (@layer) Tests - Wave 4 Implementation
    // =========================================================================

    use sqry_core::graph::unified::build::staging::StagingOp;
    use sqry_core::graph::unified::edge::EdgeKind;

    /// Build a string lookup map from `InternString` operations
    fn build_string_lookup(staging: &StagingGraph) -> std::collections::HashMap<u32, String> {
        staging
            .operations()
            .iter()
            .filter_map(|op| {
                if let StagingOp::InternString { local_id, value } = op {
                    Some((local_id.index(), value.clone()))
                } else {
                    None
                }
            })
            .collect()
    }

    /// Resolve a `StringId` to its value using the string lookup map
    fn resolve_string(
        strings: &std::collections::HashMap<u32, String>,
        id: sqry_core::graph::unified::StringId,
    ) -> String {
        strings
            .get(&id.index())
            .cloned()
            .unwrap_or_else(|| format!("<unresolved:{}>", id.index()))
    }

    #[test]
    fn test_import_with_named_layer() {
        // @import with layer(name) should store layer name with @layer: prefix in alias
        // Convention: "@layer:<name>" distinguishes layer metadata from ES-style aliases
        let source = r#"@import "theme.css" layer(base);"#;

        let tree = parse_css(source);
        let mut staging = StagingGraph::new();
        let builder = CssGraphBuilder;
        let file = PathBuf::from("styles.css");

        builder
            .build_graph(&tree, source.as_bytes(), &file, &mut staging)
            .unwrap();

        let ops = staging.operations();

        // Find Imports edge
        let import_edge = ops.iter().find(|op| {
            matches!(
                op,
                StagingOp::AddEdge {
                    kind: EdgeKind::Imports { .. },
                    ..
                }
            )
        });

        assert!(
            import_edge.is_some(),
            "Expected Imports edge for @import layer(name)"
        );
        if let StagingOp::AddEdge {
            kind: EdgeKind::Imports { alias, is_wildcard },
            ..
        } = import_edge.unwrap()
        {
            assert!(alias.is_some(), "Layer name should be stored as alias");
            // Verify the @layer: prefix convention
            let strings = build_string_lookup(&staging);
            let alias_str = resolve_string(&strings, *alias.as_ref().unwrap());
            assert!(
                alias_str.starts_with("@layer:"),
                "Layer alias should have @layer: prefix, got: {alias_str:?}"
            );
            assert!(
                alias_str.contains("base"),
                "Layer alias should contain the layer name 'base', got: {alias_str:?}"
            );
            assert!(!*is_wildcard, "Layer import should not be wildcard");
        }
    }

    #[test]
    fn test_import_with_anonymous_layer() {
        // @import with layer() (anonymous) should store "@layer:" prefix only
        let source = r#"@import "file.css" layer();"#;

        let tree = parse_css(source);
        let mut staging = StagingGraph::new();
        let builder = CssGraphBuilder;
        let file = PathBuf::from("styles.css");

        builder
            .build_graph(&tree, source.as_bytes(), &file, &mut staging)
            .unwrap();

        let ops = staging.operations();

        // Find Imports edge
        let import_edge = ops.iter().find(|op| {
            matches!(
                op,
                StagingOp::AddEdge {
                    kind: EdgeKind::Imports { .. },
                    ..
                }
            )
        });

        assert!(
            import_edge.is_some(),
            "Expected Imports edge for @import layer()"
        );
        if let StagingOp::AddEdge {
            kind: EdgeKind::Imports { alias, .. },
            ..
        } = import_edge.unwrap()
        {
            assert!(
                alias.is_some(),
                "Anonymous layer should have @layer: prefix alias"
            );
            // Verify the @layer: prefix convention for anonymous layer
            let strings = build_string_lookup(&staging);
            let alias_str = resolve_string(&strings, *alias.as_ref().unwrap());
            assert_eq!(
                alias_str, "@layer:",
                "Anonymous layer alias should be exactly '@layer:', got: {alias_str:?}"
            );
        }
    }

    #[test]
    fn test_import_with_nested_layer_name() {
        // @import with layer(theme.dark) should store the nested layer name
        // Note: tree-sitter-css parses "theme.dark" differently due to the dot
        // being interpreted as a class selector, so this test verifies we handle it
        let source = r#"@import url("file.css") layer(theme.dark);"#;

        let tree = parse_css(source);
        let mut staging = StagingGraph::new();
        let builder = CssGraphBuilder;
        let file = PathBuf::from("styles.css");

        builder
            .build_graph(&tree, source.as_bytes(), &file, &mut staging)
            .unwrap();

        let ops = staging.operations();

        // Should have at least created an import node
        let has_import_node = ops.iter().any(|op| matches!(op, StagingOp::AddNode { .. }));
        assert!(
            has_import_node,
            "Should create nodes for import with nested layer name"
        );
    }

    #[test]
    fn test_import_with_supports_condition() {
        // @import with supports(condition) is detected
        let source = r#"@import "file.css" supports(display: grid);"#;

        let tree = parse_css(source);
        let mut staging = StagingGraph::new();
        let builder = CssGraphBuilder;
        let file = PathBuf::from("styles.css");

        builder
            .build_graph(&tree, source.as_bytes(), &file, &mut staging)
            .unwrap();

        let ops = staging.operations();

        // Should have created import nodes and edges
        let has_import = ops.iter().any(|op| {
            matches!(
                op,
                StagingOp::AddEdge {
                    kind: EdgeKind::Imports { .. },
                    ..
                }
            )
        });
        assert!(has_import, "Expected Imports edge for @import supports()");
    }

    #[test]
    fn test_layer_ordering_declaration() {
        // @layer base, utils, components; should create module nodes for each layer
        let source = r"@layer base, utils, components;";

        let tree = parse_css(source);
        let mut staging = StagingGraph::new();
        let builder = CssGraphBuilder;
        let file = PathBuf::from("styles.css");

        builder
            .build_graph(&tree, source.as_bytes(), &file, &mut staging)
            .unwrap();

        let ops = staging.operations();

        // Count module nodes (should be 4: css::module + 3 layers)
        let module_nodes: Vec<_> = ops
            .iter()
            .filter(|op| matches!(op, StagingOp::AddNode { .. }))
            .collect();

        assert!(
            module_nodes.len() >= 4,
            "Expected at least 4 module nodes (css::module + 3 layers), got {}",
            module_nodes.len()
        );

        // Count Contains edges (should be 3: one for each layer)
        let contains_edges: Vec<_> = ops
            .iter()
            .filter(|op| {
                matches!(
                    op,
                    StagingOp::AddEdge {
                        kind: EdgeKind::Contains,
                        ..
                    }
                )
            })
            .collect();

        assert_eq!(
            contains_edges.len(),
            3,
            "Expected 3 Contains edges for layer ordering"
        );
    }

    #[test]
    fn test_layer_block_definition() {
        // @layer name { .foo { color: red; } } should create layer module
        let source = r"@layer name { .foo { color: red; } }";

        let tree = parse_css(source);
        let mut staging = StagingGraph::new();
        let builder = CssGraphBuilder;
        let file = PathBuf::from("styles.css");

        builder
            .build_graph(&tree, source.as_bytes(), &file, &mut staging)
            .unwrap();

        let ops = staging.operations();

        // Should have css::module and css::layer::name
        let module_nodes: Vec<_> = ops
            .iter()
            .filter(|op| matches!(op, StagingOp::AddNode { .. }))
            .collect();

        assert!(
            module_nodes.len() >= 2,
            "Expected at least 2 module nodes, got {}",
            module_nodes.len()
        );
    }

    #[test]
    fn test_basic_import_without_layer() {
        // Basic @import without layer should not have alias
        let source = r#"@import "reset.css";"#;

        let tree = parse_css(source);
        let mut staging = StagingGraph::new();
        let builder = CssGraphBuilder;
        let file = PathBuf::from("styles.css");

        builder
            .build_graph(&tree, source.as_bytes(), &file, &mut staging)
            .unwrap();

        let ops = staging.operations();

        // Find Imports edge
        let import_edge = ops.iter().find(|op| {
            matches!(
                op,
                StagingOp::AddEdge {
                    kind: EdgeKind::Imports { .. },
                    ..
                }
            )
        });

        assert!(
            import_edge.is_some(),
            "Expected Imports edge for basic @import"
        );
        if let StagingOp::AddEdge {
            kind: EdgeKind::Imports { alias, is_wildcard },
            ..
        } = import_edge.unwrap()
        {
            assert!(alias.is_none(), "Basic import should not have alias");
            assert!(!*is_wildcard, "Basic import should not be wildcard");
        }
    }

    #[test]
    fn test_import_with_url_and_layer() {
        // @import url("file.css") layer(base) should work with url() syntax
        let source = r#"@import url("theme.css") layer(base);"#;

        let tree = parse_css(source);
        let mut staging = StagingGraph::new();
        let builder = CssGraphBuilder;
        let file = PathBuf::from("styles.css");

        builder
            .build_graph(&tree, source.as_bytes(), &file, &mut staging)
            .unwrap();

        let ops = staging.operations();

        // Note: tree-sitter-css may mark this as ERROR due to newer syntax
        // but we should still attempt to extract what we can
        let has_nodes = ops.iter().any(|op| matches!(op, StagingOp::AddNode { .. }));
        assert!(
            has_nodes,
            "Should create nodes even with url() + layer() syntax"
        );
    }

    #[test]
    fn test_multiple_layer_declarations() {
        // Multiple @layer declarations in same file
        let source = r"
@layer reset, base;
@layer components, utils;
";

        let tree = parse_css(source);
        let mut staging = StagingGraph::new();
        let builder = CssGraphBuilder;
        let file = PathBuf::from("styles.css");

        builder
            .build_graph(&tree, source.as_bytes(), &file, &mut staging)
            .unwrap();

        let ops = staging.operations();

        // Count Contains edges (should be 4: reset, base, components, utils)
        let contains_edges: Vec<_> = ops
            .iter()
            .filter(|op| {
                matches!(
                    op,
                    StagingOp::AddEdge {
                        kind: EdgeKind::Contains,
                        ..
                    }
                )
            })
            .collect();

        assert_eq!(
            contains_edges.len(),
            4,
            "Expected 4 Contains edges for multiple layer declarations"
        );
    }

    #[test]
    fn test_layer_with_css_inside() {
        // @layer with CSS rules inside should extract CSS custom properties
        let source = r"
@layer base {
    :root {
        --primary-color: blue;
    }
}
";

        let tree = parse_css(source);
        let mut staging = StagingGraph::new();
        let builder = CssGraphBuilder;
        let file = PathBuf::from("styles.css");

        builder
            .build_graph(&tree, source.as_bytes(), &file, &mut staging)
            .unwrap();

        let ops = staging.operations();

        // Should have module nodes and variable node for --primary-color
        let node_count = ops
            .iter()
            .filter(|op| matches!(op, StagingOp::AddNode { .. }))
            .count();
        assert!(
            node_count >= 3,
            "Expected at least 3 nodes (module, layer, variable), got {node_count}"
        );
    }

    #[test]
    fn test_mixed_imports_and_layers() {
        // Complex CSS with both @import with layer and @layer declarations
        let source = r#"
@layer reset, base, components;
@import "reset.css" layer(reset);
@import "base.css" layer(base);

@layer components {
    .button { color: blue; }
}
"#;

        let tree = parse_css(source);
        let mut staging = StagingGraph::new();
        let builder = CssGraphBuilder;
        let file = PathBuf::from("styles.css");

        builder
            .build_graph(&tree, source.as_bytes(), &file, &mut staging)
            .unwrap();

        let ops = staging.operations();

        // Count Imports edges (should be 2: reset.css and base.css)
        let import_edges: Vec<_> = ops
            .iter()
            .filter(|op| {
                matches!(
                    op,
                    StagingOp::AddEdge {
                        kind: EdgeKind::Imports { .. },
                        ..
                    }
                )
            })
            .collect();
        assert_eq!(import_edges.len(), 2, "Expected 2 Imports edges");

        // All Imports should have layer alias with @layer: prefix
        let strings = build_string_lookup(&staging);
        for edge in import_edges {
            if let StagingOp::AddEdge {
                kind: EdgeKind::Imports { alias, .. },
                ..
            } = edge
            {
                assert!(alias.is_some(), "Import should have layer alias");
                let alias_str = resolve_string(&strings, *alias.as_ref().unwrap());
                assert!(
                    alias_str.starts_with("@layer:"),
                    "Layer alias should have @layer: prefix, got: {alias_str:?}"
                );
            }
        }
    }

    #[test]
    fn test_import_creates_proper_edge_structure() {
        // Verify the edge structure: module -> import node
        let source = r#"@import "theme.css" layer(base);"#;

        let tree = parse_css(source);
        let mut staging = StagingGraph::new();
        let builder = CssGraphBuilder;
        let file = PathBuf::from("styles.css");

        builder
            .build_graph(&tree, source.as_bytes(), &file, &mut staging)
            .unwrap();

        let ops = staging.operations();

        // Should have: css::module node, import node, and Imports edge between them
        let nodes: Vec<_> = ops
            .iter()
            .filter(|op| matches!(op, StagingOp::AddNode { .. }))
            .collect();
        let edges: Vec<_> = ops
            .iter()
            .filter(|op| matches!(op, StagingOp::AddEdge { .. }))
            .collect();

        assert!(
            nodes.len() >= 2,
            "Expected at least 2 nodes (module and import)"
        );
        assert!(!edges.is_empty(), "Expected at least 1 edge");
    }

    #[test]
    fn test_single_layer_declaration() {
        // Single layer: @layer base;
        let source = r"@layer base;";

        let tree = parse_css(source);
        let mut staging = StagingGraph::new();
        let builder = CssGraphBuilder;
        let file = PathBuf::from("styles.css");

        builder
            .build_graph(&tree, source.as_bytes(), &file, &mut staging)
            .unwrap();

        let ops = staging.operations();

        // Should have 1 Contains edge for the single layer
        let contains_edges: Vec<_> = ops
            .iter()
            .filter(|op| {
                matches!(
                    op,
                    StagingOp::AddEdge {
                        kind: EdgeKind::Contains,
                        ..
                    }
                )
            })
            .collect();

        assert_eq!(
            contains_edges.len(),
            1,
            "Expected 1 Contains edge for single layer declaration"
        );
    }
}