sqry-classpath 7.1.5

JVM classpath analysis for sqry - bytecode parsing, build system resolution, and graph integration
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
//! End-to-end integration tests for the JVM classpath pipeline.
//!
//! Tests cover:
//! 1. Build system detection from fixture projects
//! 2. Pipeline execution with manual classpath files (no subprocess)
//! 3. Index persistence roundtrip (save → load → verify identical)
//! 4. FQN workspace shadowing (workspace FQN takes precedence)
//! 5. Package and annotation index lookup
//! 6. Full pipeline tests requiring JVM toolchain (ignored in CI)

use std::io::Write;
use std::path::{Path, PathBuf};

use sqry_classpath::detect::{BuildSystem, detect_build_system};
use sqry_classpath::pipeline::{ClasspathConfig, ClasspathDepth, run_classpath_pipeline};
use sqry_classpath::stub::index::ClasspathIndex;
use sqry_classpath::stub::model::{
    AccessFlags, AnnotationStub, BaseType, ClassKind, ClassStub, FieldStub, MethodStub,
    TypeSignature,
};
use tempfile::TempDir;
use zip::write::SimpleFileOptions;

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

/// Path to the test-fixtures/jvm-classpath directory relative to the crate root.
fn fixtures_dir() -> PathBuf {
    Path::new(env!("CARGO_MANIFEST_DIR")).join("../test-fixtures/jvm-classpath")
}

/// Build a minimal valid `.class` file for testing.
///
/// Creates a class that extends `java.lang.Object` with no methods, fields, or
/// attributes. Good enough for `scan_jar` to parse and produce a `ClassStub`.
fn build_minimal_class(class_name: &str) -> Vec<u8> {
    let mut bytes = Vec::new();

    // Magic number.
    bytes.extend_from_slice(&0xCAFE_BABEu32.to_be_bytes());
    // Minor version.
    bytes.extend_from_slice(&0u16.to_be_bytes());
    // Major version (52 = Java 8).
    bytes.extend_from_slice(&52u16.to_be_bytes());

    // Constant pool: 5 entries (index 1-4, pool count = 5).
    let class_bytes = class_name.as_bytes();
    let object_bytes = b"java/lang/Object";

    let cp_count: u16 = 5;
    bytes.extend_from_slice(&cp_count.to_be_bytes());

    // #1: CONSTANT_Utf8 <class_name>
    bytes.push(1);
    bytes.extend_from_slice(&(class_bytes.len() as u16).to_be_bytes());
    bytes.extend_from_slice(class_bytes);

    // #2: CONSTANT_Class -> #1
    bytes.push(7);
    bytes.extend_from_slice(&1u16.to_be_bytes());

    // #3: CONSTANT_Utf8 "java/lang/Object"
    bytes.push(1);
    bytes.extend_from_slice(&(object_bytes.len() as u16).to_be_bytes());
    bytes.extend_from_slice(object_bytes);

    // #4: CONSTANT_Class -> #3
    bytes.push(7);
    bytes.extend_from_slice(&3u16.to_be_bytes());

    // Access flags: ACC_PUBLIC | ACC_SUPER
    bytes.extend_from_slice(&0x0021u16.to_be_bytes());
    // This class: #2
    bytes.extend_from_slice(&2u16.to_be_bytes());
    // Super class: #4
    bytes.extend_from_slice(&4u16.to_be_bytes());
    // Interfaces count: 0
    bytes.extend_from_slice(&0u16.to_be_bytes());
    // Fields count: 0
    bytes.extend_from_slice(&0u16.to_be_bytes());
    // Methods count: 0
    bytes.extend_from_slice(&0u16.to_be_bytes());
    // Attributes count: 0
    bytes.extend_from_slice(&0u16.to_be_bytes());

    bytes
}

/// Create an in-memory JAR (ZIP archive) containing the specified `.class` entries.
fn build_test_jar(entries: &[(&str, &[u8])]) -> Vec<u8> {
    let mut buf = Vec::new();
    {
        let mut writer = zip::ZipWriter::new(std::io::Cursor::new(&mut buf));
        let options =
            SimpleFileOptions::default().compression_method(zip::CompressionMethod::Stored);
        for (name, data) in entries {
            writer.start_file(*name, options).unwrap();
            writer.write_all(data).unwrap();
        }
        writer.finish().unwrap();
    }
    buf
}

/// Write a test JAR file to disk and return its path.
fn write_test_jar(dir: &Path, name: &str, classes: &[(&str, &[u8])]) -> PathBuf {
    let jar_bytes = build_test_jar(classes);
    let jar_path = dir.join(name);
    std::fs::write(&jar_path, &jar_bytes).unwrap();
    jar_path
}

/// Create a minimal `ClassStub` for testing (no methods, fields, or annotations).
fn make_stub(fqn: &str) -> ClassStub {
    ClassStub {
        fqn: fqn.to_owned(),
        name: fqn.rsplit('.').next().unwrap_or(fqn).to_owned(),
        kind: ClassKind::Class,
        access: AccessFlags::new(0x0021), // ACC_PUBLIC | ACC_SUPER
        superclass: Some("java.lang.Object".to_owned()),
        interfaces: vec![],
        methods: vec![],
        fields: vec![],
        annotations: vec![],
        generic_signature: None,
        inner_classes: vec![],
        lambda_targets: vec![],
        module: None,
        record_components: vec![],
        enum_constants: vec![],
        source_file: None,
        source_jar: None,
        kotlin_metadata: None,
        scala_signature: None,
    }
}

/// Create a `ClassStub` with methods for testing workspace shadowing.
fn make_stub_with_methods(fqn: &str, method_names: &[&str]) -> ClassStub {
    let mut stub = make_stub(fqn);
    stub.methods = method_names
        .iter()
        .map(|name| MethodStub {
            name: (*name).to_owned(),
            descriptor: "()V".to_owned(),
            access: AccessFlags::new(0x0001), // ACC_PUBLIC
            annotations: vec![],
            parameter_annotations: vec![],
            generic_signature: None,
            parameter_names: vec![],
            return_type: TypeSignature::Base(BaseType::Void),
            parameter_types: vec![],
        })
        .collect();
    stub
}

/// Create a `ClassStub` with annotations for testing annotation index lookup.
fn make_annotated_stub(fqn: &str, annotation_fqns: &[&str]) -> ClassStub {
    let mut stub = make_stub(fqn);
    stub.annotations = annotation_fqns
        .iter()
        .map(|a| AnnotationStub {
            type_fqn: (*a).to_owned(),
            elements: vec![],
            is_runtime_visible: true,
        })
        .collect();
    stub
}

/// Create a `ClassStub` with fields for testing field-level resolution.
fn make_stub_with_fields(fqn: &str, field_names: &[(&str, &str)]) -> ClassStub {
    let mut stub = make_stub(fqn);
    stub.fields = field_names
        .iter()
        .map(|(name, descriptor)| FieldStub {
            name: (*name).to_owned(),
            descriptor: (*descriptor).to_owned(),
            access: AccessFlags::new(0x0001), // ACC_PUBLIC
            annotations: vec![],
            generic_signature: None,
            constant_value: None,
        })
        .collect();
    stub
}

/// Write a manual classpath file listing the given JAR paths, one per line.
fn write_classpath_file(dir: &Path, jar_paths: &[&Path]) -> PathBuf {
    let cp_file = dir.join("classpath.txt");
    let contents: String = jar_paths
        .iter()
        .map(|p| p.display().to_string())
        .collect::<Vec<_>>()
        .join("\n");
    std::fs::write(&cp_file, format!("{contents}\n")).unwrap();
    cp_file
}

// ═══════════════════════════════════════════════════════════════════════════
// 1. Build System Detection Tests (from fixture projects)
// ═══════════════════════════════════════════════════════════════════════════

#[test]
fn detect_gradle_single_module() {
    let fixture = fixtures_dir().join("gradle-single-module");
    assert!(fixture.exists(), "fixture not found: {}", fixture.display());

    let result = detect_build_system(&fixture, None);
    assert_eq!(result.build_system, Some(BuildSystem::Gradle));
    assert!(
        result.markers_found.contains(&"build.gradle".to_string()),
        "expected build.gradle marker, found: {:?}",
        result.markers_found
    );
}

#[test]
fn detect_gradle_multi_module() {
    let fixture = fixtures_dir().join("gradle-multi-module");
    assert!(fixture.exists(), "fixture not found: {}", fixture.display());

    let result = detect_build_system(&fixture, None);
    assert_eq!(result.build_system, Some(BuildSystem::Gradle));
    assert!(
        result
            .markers_found
            .contains(&"settings.gradle".to_string()),
        "expected settings.gradle marker, found: {:?}",
        result.markers_found
    );
    assert!(
        result.markers_found.contains(&"build.gradle".to_string()),
        "expected build.gradle marker in multi-module root"
    );
}

#[test]
fn detect_maven_single_module() {
    let fixture = fixtures_dir().join("maven-single-module");
    assert!(fixture.exists(), "fixture not found: {}", fixture.display());

    let result = detect_build_system(&fixture, None);
    assert_eq!(result.build_system, Some(BuildSystem::Maven));
    assert!(
        result.markers_found.contains(&"pom.xml".to_string()),
        "expected pom.xml marker"
    );
}

#[test]
fn detect_maven_multi_module() {
    let fixture = fixtures_dir().join("maven-multi-module");
    assert!(fixture.exists(), "fixture not found: {}", fixture.display());

    let result = detect_build_system(&fixture, None);
    assert_eq!(result.build_system, Some(BuildSystem::Maven));
    assert!(
        result.markers_found.contains(&"pom.xml".to_string()),
        "expected pom.xml marker"
    );
}

#[test]
fn detect_bazel_java() {
    let fixture = fixtures_dir().join("bazel-java");
    assert!(fixture.exists(), "fixture not found: {}", fixture.display());

    let result = detect_build_system(&fixture, None);
    assert_eq!(result.build_system, Some(BuildSystem::Bazel));
    assert!(
        result.markers_found.contains(&"WORKSPACE".to_string()),
        "expected WORKSPACE marker, found: {:?}",
        result.markers_found
    );
    assert!(
        result.markers_found.contains(&"BUILD".to_string()),
        "expected BUILD marker, found: {:?}",
        result.markers_found
    );
}

#[test]
fn detect_kotlin_gradle_project() {
    let fixture = fixtures_dir().join("kotlin-project");
    assert!(fixture.exists(), "fixture not found: {}", fixture.display());

    let result = detect_build_system(&fixture, None);
    assert_eq!(result.build_system, Some(BuildSystem::Gradle));
    assert!(
        result
            .markers_found
            .contains(&"build.gradle.kts".to_string()),
        "expected build.gradle.kts marker, found: {:?}",
        result.markers_found
    );
}

#[test]
fn detect_scala_sbt_project() {
    let fixture = fixtures_dir().join("scala-project");
    assert!(fixture.exists(), "fixture not found: {}", fixture.display());

    let result = detect_build_system(&fixture, None);
    assert_eq!(result.build_system, Some(BuildSystem::Sbt));
    assert!(
        result.markers_found.contains(&"build.sbt".to_string()),
        "expected build.sbt marker, found: {:?}",
        result.markers_found
    );
}

#[test]
fn detect_no_build_system_in_empty_dir() {
    let tmp = TempDir::new().unwrap();
    let result = detect_build_system(tmp.path(), None);
    assert_eq!(
        result.build_system, None,
        "empty directory should detect no build system"
    );
    assert!(
        result.markers_found.is_empty(),
        "no markers should be found in empty directory"
    );
}

#[test]
fn detect_override_takes_precedence() {
    let fixture = fixtures_dir().join("maven-single-module");
    // Even though pom.xml is present, override should force Gradle.
    let result = detect_build_system(&fixture, Some("gradle"));
    assert_eq!(result.build_system, Some(BuildSystem::Gradle));
    assert_eq!(result.override_source, Some("gradle".to_string()));
    // Override skips marker scanning entirely.
    assert!(
        result.markers_found.is_empty(),
        "override should skip marker scanning"
    );
}

// ═══════════════════════════════════════════════════════════════════════════
// 2. Pipeline with Manual Classpath File (no subprocess)
// ═══════════════════════════════════════════════════════════════════════════

#[test]
fn pipeline_manual_classpath_single_jar() {
    let tmp = TempDir::new().unwrap();

    let class_a = build_minimal_class("com/example/Alpha");
    let class_b = build_minimal_class("com/example/Beta");
    let jar_path = write_test_jar(
        tmp.path(),
        "deps.jar",
        &[
            ("com/example/Alpha.class", &class_a),
            ("com/example/Beta.class", &class_b),
        ],
    );

    let cp_file = write_classpath_file(tmp.path(), &[jar_path.as_path()]);

    let config = ClasspathConfig {
        enabled: true,
        depth: ClasspathDepth::Full,
        build_system_override: None,
        classpath_file: Some(cp_file),
        force: false,
        timeout_secs: 30,
    };

    let result = run_classpath_pipeline(tmp.path(), &config).unwrap();
    assert_eq!(result.jars_scanned, 1);
    assert_eq!(result.classes_parsed, 2);
    assert!(result.index.lookup_fqn("com.example.Alpha").is_some());
    assert!(result.index.lookup_fqn("com.example.Beta").is_some());
    assert!(
        result.index.lookup_fqn("com.example.Gamma").is_none(),
        "non-existent class should not be found"
    );
}

#[test]
fn pipeline_manual_classpath_multiple_jars() {
    let tmp = TempDir::new().unwrap();

    let class_a = build_minimal_class("com/example/Alpha");
    let class_b = build_minimal_class("org/other/Beta");
    let class_c = build_minimal_class("org/other/Gamma");

    let jar_a = write_test_jar(
        tmp.path(),
        "a.jar",
        &[("com/example/Alpha.class", &class_a)],
    );
    let jar_b = write_test_jar(
        tmp.path(),
        "b.jar",
        &[
            ("org/other/Beta.class", &class_b),
            ("org/other/Gamma.class", &class_c),
        ],
    );

    let cp_file = write_classpath_file(tmp.path(), &[jar_a.as_path(), jar_b.as_path()]);

    let config = ClasspathConfig {
        enabled: true,
        depth: ClasspathDepth::Full,
        build_system_override: None,
        classpath_file: Some(cp_file),
        force: false,
        timeout_secs: 30,
    };

    let result = run_classpath_pipeline(tmp.path(), &config).unwrap();
    assert_eq!(result.jars_scanned, 2);
    assert_eq!(result.classes_parsed, 3);
    assert!(result.index.lookup_fqn("com.example.Alpha").is_some());
    assert!(result.index.lookup_fqn("org.other.Beta").is_some());
    assert!(result.index.lookup_fqn("org.other.Gamma").is_some());
    assert_eq!(result.provenance.len(), 2);
}

#[test]
fn pipeline_manual_classpath_with_comments_and_blanks() {
    let tmp = TempDir::new().unwrap();

    let class_a = build_minimal_class("com/example/Only");
    let jar_path = write_test_jar(
        tmp.path(),
        "only.jar",
        &[("com/example/Only.class", &class_a)],
    );

    let cp_file = tmp.path().join("classpath.txt");
    std::fs::write(
        &cp_file,
        format!(
            "# Comment line\n\n{}\n\n# Another comment\n",
            jar_path.display()
        ),
    )
    .unwrap();

    let config = ClasspathConfig {
        enabled: true,
        depth: ClasspathDepth::Full,
        build_system_override: None,
        classpath_file: Some(cp_file),
        force: false,
        timeout_secs: 30,
    };

    let result = run_classpath_pipeline(tmp.path(), &config).unwrap();
    assert_eq!(result.jars_scanned, 1);
    assert_eq!(result.classes_parsed, 1);
    assert!(result.index.lookup_fqn("com.example.Only").is_some());
}

#[test]
fn pipeline_no_build_system_returns_detection_error() {
    let tmp = TempDir::new().unwrap();
    let config = ClasspathConfig {
        enabled: true,
        ..ClasspathConfig::default()
    };

    let result = run_classpath_pipeline(tmp.path(), &config);
    assert!(result.is_err(), "empty project should fail detection");
    let err = result.unwrap_err().to_string();
    assert!(
        err.contains("No JVM build system detected"),
        "expected detection error, got: {err}"
    );
}

#[test]
fn pipeline_empty_classpath_file() {
    let tmp = TempDir::new().unwrap();

    let cp_file = tmp.path().join("classpath.txt");
    std::fs::write(&cp_file, "# Only comments\n\n").unwrap();

    let config = ClasspathConfig {
        enabled: true,
        depth: ClasspathDepth::Full,
        build_system_override: None,
        classpath_file: Some(cp_file),
        force: false,
        timeout_secs: 30,
    };

    let result = run_classpath_pipeline(tmp.path(), &config).unwrap();
    assert_eq!(result.jars_scanned, 0);
    assert_eq!(result.classes_parsed, 0);
    assert_eq!(result.index.classes.len(), 0);
}

#[test]
fn pipeline_nonexistent_classpath_file_returns_error() {
    let tmp = TempDir::new().unwrap();
    let config = ClasspathConfig {
        enabled: true,
        classpath_file: Some(tmp.path().join("nonexistent.txt")),
        ..ClasspathConfig::default()
    };

    let result = run_classpath_pipeline(tmp.path(), &config);
    assert!(result.is_err(), "nonexistent classpath file should fail");
    let err = result.unwrap_err().to_string();
    assert!(
        err.contains("Cannot open classpath file"),
        "expected file open error, got: {err}"
    );
}

// ═══════════════════════════════════════════════════════════════════════════
// 3. Index Persistence Roundtrip
// ═══════════════════════════════════════════════════════════════════════════

#[test]
fn index_persistence_roundtrip_empty() {
    let tmp = TempDir::new().unwrap();
    let index_path = tmp.path().join("index.sqry");

    let index = ClasspathIndex::build(vec![]);
    index.save(&index_path).unwrap();

    let loaded = ClasspathIndex::load(&index_path).unwrap();
    assert_eq!(loaded.classes.len(), 0);
    assert!(loaded.package_index.is_empty());
    assert!(loaded.annotation_index.is_empty());
}

#[test]
fn index_persistence_roundtrip_with_classes() {
    let tmp = TempDir::new().unwrap();
    let index_path = tmp.path().join("classpath/index.sqry");

    let stubs = vec![
        make_stub("com.example.Alpha"),
        make_stub("com.example.Beta"),
        make_stub("java.util.HashMap"),
        make_stub("java.util.ArrayList"),
        make_stub("org.slf4j.Logger"),
    ];

    let index = ClasspathIndex::build(stubs);
    index.save(&index_path).unwrap();

    let loaded = ClasspathIndex::load(&index_path).unwrap();
    assert_eq!(loaded.classes.len(), 5);

    // Verify sorting is preserved after roundtrip.
    for window in loaded.classes.windows(2) {
        assert!(
            window[0].fqn <= window[1].fqn,
            "sort order violated after roundtrip: {} > {}",
            window[0].fqn,
            window[1].fqn
        );
    }

    // Verify FQN lookup works after reload.
    assert!(loaded.lookup_fqn("com.example.Alpha").is_some());
    assert!(loaded.lookup_fqn("java.util.HashMap").is_some());
    assert!(loaded.lookup_fqn("org.slf4j.Logger").is_some());
    assert!(loaded.lookup_fqn("does.not.Exist").is_none());
}

#[test]
fn index_persistence_roundtrip_with_annotations() {
    let tmp = TempDir::new().unwrap();
    let index_path = tmp.path().join("index.sqry");

    let stubs = vec![
        make_annotated_stub(
            "com.example.MyController",
            &["org.springframework.stereotype.Controller"],
        ),
        make_annotated_stub(
            "com.example.MyService",
            &["org.springframework.stereotype.Service"],
        ),
        make_annotated_stub(
            "com.example.AnotherController",
            &[
                "org.springframework.stereotype.Controller",
                "org.springframework.web.bind.annotation.RestController",
            ],
        ),
    ];

    let index = ClasspathIndex::build(stubs);
    index.save(&index_path).unwrap();

    let loaded = ClasspathIndex::load(&index_path).unwrap();
    let controllers = loaded.lookup_annotated("org.springframework.stereotype.Controller");
    assert_eq!(
        controllers.len(),
        2,
        "should find 2 controller-annotated classes after roundtrip"
    );

    let rest_controllers =
        loaded.lookup_annotated("org.springframework.web.bind.annotation.RestController");
    assert_eq!(
        rest_controllers.len(),
        1,
        "should find 1 RestController-annotated class after roundtrip"
    );
}

#[test]
fn index_persistence_roundtrip_via_pipeline() {
    let tmp = TempDir::new().unwrap();

    let class_a = build_minimal_class("com/example/Roundtrip");
    let jar_path = write_test_jar(
        tmp.path(),
        "rt.jar",
        &[("com/example/Roundtrip.class", &class_a)],
    );
    let cp_file = write_classpath_file(tmp.path(), &[jar_path.as_path()]);

    let config = ClasspathConfig {
        enabled: true,
        depth: ClasspathDepth::Full,
        classpath_file: Some(cp_file),
        force: false,
        timeout_secs: 30,
        ..ClasspathConfig::default()
    };

    // Run pipeline — it persists the index.
    let result = run_classpath_pipeline(tmp.path(), &config).unwrap();
    assert_eq!(result.classes_parsed, 1);

    // Load the persisted index directly.
    let index_path = tmp.path().join(".sqry/classpath/index.sqry");
    assert!(
        index_path.exists(),
        "pipeline should persist index at .sqry/classpath/index.sqry"
    );

    let loaded = ClasspathIndex::load(&index_path).unwrap();
    assert_eq!(loaded.classes.len(), 1);
    assert_eq!(loaded.classes[0].fqn, "com.example.Roundtrip");

    // Verify provenance was also persisted.
    let prov_path = tmp.path().join(".sqry/classpath/provenance.json");
    assert!(
        prov_path.exists(),
        "pipeline should persist provenance at .sqry/classpath/provenance.json"
    );
    let prov_json = std::fs::read_to_string(&prov_path).unwrap();
    let prov: serde_json::Value = serde_json::from_str(&prov_json).unwrap();
    assert!(prov.is_array());
    assert_eq!(prov.as_array().unwrap().len(), 1);
}

// ═══════════════════════════════════════════════════════════════════════════
// 4. FQN Workspace Shadowing
// ═══════════════════════════════════════════════════════════════════════════

#[test]
fn workspace_fqn_shadows_classpath_in_index() {
    // Simulate a classpath index where a workspace-defined class would shadow
    // a classpath-provided one. The classpath index itself does not distinguish
    // workspace vs. classpath — that is the graph emitter's responsibility.
    // However, we verify that if the same FQN appears in two different stubs,
    // the index correctly deduplicates via sorted order (last-writer-wins in
    // build order, but binary search returns the first match).

    let classpath_stub = make_stub_with_methods("com.example.AppService", &["handleRequest"]);
    let workspace_stub =
        make_stub_with_methods("com.example.AppService", &["handleRequest", "initialize"]);

    // Building with both — the index should contain both, but lookup_fqn
    // returns the first match (which is deterministic since sort is stable).
    let index = ClasspathIndex::build(vec![classpath_stub, workspace_stub]);

    // The index does not deduplicate same-FQN stubs; both are present.
    let all_in_package = index.lookup_package("com.example");
    assert_eq!(
        all_in_package.len(),
        2,
        "both stubs with same FQN should be in the index"
    );

    // lookup_fqn returns the first binary search match.
    let found = index.lookup_fqn("com.example.AppService");
    assert!(found.is_some(), "should find AppService by FQN");
}

#[test]
fn workspace_shadow_different_packages() {
    // Workspace provides com.example.Foo, classpath provides org.library.Foo.
    // Both should be independently resolvable.
    let ws_stub = make_stub("com.example.Foo");
    let cp_stub = make_stub("org.library.Foo");

    let index = ClasspathIndex::build(vec![ws_stub, cp_stub]);

    assert!(index.lookup_fqn("com.example.Foo").is_some());
    assert!(index.lookup_fqn("org.library.Foo").is_some());

    let com_example = index.lookup_package("com.example");
    assert_eq!(com_example.len(), 1);
    assert_eq!(com_example[0].fqn, "com.example.Foo");

    let org_library = index.lookup_package("org.library");
    assert_eq!(org_library.len(), 1);
    assert_eq!(org_library[0].fqn, "org.library.Foo");
}

// ═══════════════════════════════════════════════════════════════════════════
// 5. Package Lookup
// ═══════════════════════════════════════════════════════════════════════════

#[test]
fn package_lookup_groups_correctly() {
    let stubs = vec![
        make_stub("com.example.Alpha"),
        make_stub("com.example.Beta"),
        make_stub("com.example.Gamma"),
        make_stub("java.util.HashMap"),
        make_stub("java.util.ArrayList"),
        make_stub("java.util.LinkedList"),
        make_stub("org.slf4j.Logger"),
    ];

    let index = ClasspathIndex::build(stubs);

    let com_example = index.lookup_package("com.example");
    assert_eq!(com_example.len(), 3);
    let fqns: Vec<&str> = com_example.iter().map(|s| s.fqn.as_str()).collect();
    assert!(fqns.contains(&"com.example.Alpha"));
    assert!(fqns.contains(&"com.example.Beta"));
    assert!(fqns.contains(&"com.example.Gamma"));

    let java_util = index.lookup_package("java.util");
    assert_eq!(java_util.len(), 3);

    let org_slf4j = index.lookup_package("org.slf4j");
    assert_eq!(org_slf4j.len(), 1);
    assert_eq!(org_slf4j[0].fqn, "org.slf4j.Logger");

    let nonexistent = index.lookup_package("does.not.exist");
    assert!(nonexistent.is_empty());
}

#[test]
fn package_lookup_default_package() {
    let stubs = vec![make_stub("NoPackageClass"), make_stub("AnotherDefault")];

    let index = ClasspathIndex::build(stubs);

    // Default package is the empty string.
    let default_pkg = index.lookup_package("");
    assert_eq!(default_pkg.len(), 2);
}

#[test]
fn package_lookup_deeply_nested() {
    let stubs = vec![
        make_stub("com.example.deep.nested.pkg.ClassA"),
        make_stub("com.example.deep.nested.pkg.ClassB"),
        make_stub("com.example.deep.other.ClassC"),
    ];

    let index = ClasspathIndex::build(stubs);

    let nested = index.lookup_package("com.example.deep.nested.pkg");
    assert_eq!(nested.len(), 2);

    let other = index.lookup_package("com.example.deep.other");
    assert_eq!(other.len(), 1);

    // Partial package should not match.
    let partial = index.lookup_package("com.example.deep");
    assert!(
        partial.is_empty(),
        "partial package path should not match any classes"
    );
}

// ═══════════════════════════════════════════════════════════════════════════
// 6. Annotation Lookup
// ═══════════════════════════════════════════════════════════════════════════

#[test]
fn annotation_lookup_single_annotation() {
    let stubs = vec![
        make_annotated_stub("com.example.UserService", &["javax.inject.Singleton"]),
        make_annotated_stub("com.example.OrderService", &["javax.inject.Singleton"]),
        make_stub("com.example.PlainClass"), // No annotations.
    ];

    let index = ClasspathIndex::build(stubs);

    let singletons = index.lookup_annotated("javax.inject.Singleton");
    assert_eq!(singletons.len(), 2);
    let fqns: Vec<&str> = singletons.iter().map(|s| s.fqn.as_str()).collect();
    assert!(fqns.contains(&"com.example.UserService"));
    assert!(fqns.contains(&"com.example.OrderService"));

    // Non-annotated classes should not appear.
    let plain = index.lookup_annotated("non.existent.Annotation");
    assert!(plain.is_empty());
}

#[test]
fn annotation_lookup_multiple_annotations_per_class() {
    let stubs = vec![make_annotated_stub(
        "com.example.RestEndpoint",
        &[
            "javax.ws.rs.Path",
            "javax.inject.Singleton",
            "javax.enterprise.context.RequestScoped",
        ],
    )];

    let index = ClasspathIndex::build(stubs);

    // Each annotation should index back to the same class.
    assert_eq!(index.lookup_annotated("javax.ws.rs.Path").len(), 1);
    assert_eq!(index.lookup_annotated("javax.inject.Singleton").len(), 1);
    assert_eq!(
        index
            .lookup_annotated("javax.enterprise.context.RequestScoped")
            .len(),
        1
    );

    // All should point to the same class.
    let found = index.lookup_annotated("javax.ws.rs.Path");
    assert_eq!(found[0].fqn, "com.example.RestEndpoint");
}

#[test]
fn annotation_lookup_spring_framework_pattern() {
    // Simulate a typical Spring Boot project's classpath with controllers,
    // services, and repositories.
    let stubs = vec![
        make_annotated_stub(
            "com.example.web.UserController",
            &["org.springframework.web.bind.annotation.RestController"],
        ),
        make_annotated_stub(
            "com.example.web.OrderController",
            &["org.springframework.web.bind.annotation.RestController"],
        ),
        make_annotated_stub(
            "com.example.service.UserService",
            &["org.springframework.stereotype.Service"],
        ),
        make_annotated_stub(
            "com.example.repo.UserRepository",
            &["org.springframework.stereotype.Repository"],
        ),
        make_annotated_stub(
            "com.example.config.AppConfig",
            &["org.springframework.context.annotation.Configuration"],
        ),
    ];

    let index = ClasspathIndex::build(stubs);

    let controllers =
        index.lookup_annotated("org.springframework.web.bind.annotation.RestController");
    assert_eq!(controllers.len(), 2);

    let services = index.lookup_annotated("org.springframework.stereotype.Service");
    assert_eq!(services.len(), 1);
    assert_eq!(services[0].fqn, "com.example.service.UserService");

    let repos = index.lookup_annotated("org.springframework.stereotype.Repository");
    assert_eq!(repos.len(), 1);

    let configs = index.lookup_annotated("org.springframework.context.annotation.Configuration");
    assert_eq!(configs.len(), 1);
}

// ═══════════════════════════════════════════════════════════════════════════
// 7. Bytecode Scanning Integration (via test JARs)
// ═══════════════════════════════════════════════════════════════════════════

#[test]
fn scan_jar_via_pipeline_produces_correct_stubs() {
    let tmp = TempDir::new().unwrap();

    // Build classes with various package depths.
    let class_root = build_minimal_class("RootClass");
    let class_single = build_minimal_class("com/Example");
    let class_deep = build_minimal_class("com/example/deep/nested/DeepClass");

    let jar_path = write_test_jar(
        tmp.path(),
        "mixed.jar",
        &[
            ("RootClass.class", &class_root),
            ("com/Example.class", &class_single),
            ("com/example/deep/nested/DeepClass.class", &class_deep),
        ],
    );

    let cp_file = write_classpath_file(tmp.path(), &[jar_path.as_path()]);

    let config = ClasspathConfig {
        enabled: true,
        depth: ClasspathDepth::Full,
        classpath_file: Some(cp_file),
        force: false,
        timeout_secs: 30,
        ..ClasspathConfig::default()
    };

    let result = run_classpath_pipeline(tmp.path(), &config).unwrap();
    assert_eq!(result.classes_parsed, 3);

    // Verify each class is accessible by FQN (dots, not slashes).
    assert!(result.index.lookup_fqn("RootClass").is_some());
    assert!(result.index.lookup_fqn("com.Example").is_some());
    assert!(
        result
            .index
            .lookup_fqn("com.example.deep.nested.DeepClass")
            .is_some()
    );

    // Verify package index for the deep class.
    let deep_pkg = result.index.lookup_package("com.example.deep.nested");
    assert_eq!(deep_pkg.len(), 1);
    assert_eq!(deep_pkg[0].fqn, "com.example.deep.nested.DeepClass");
}

#[test]
fn scan_jar_ignores_non_class_entries() {
    let tmp = TempDir::new().unwrap();

    let class_a = build_minimal_class("com/example/Valid");

    let jar_path = write_test_jar(
        tmp.path(),
        "mixed-content.jar",
        &[
            ("com/example/Valid.class", &class_a),
            ("META-INF/MANIFEST.MF", b"Manifest-Version: 1.0\n"),
            ("README.txt", b"This is not a class file.\n"),
            ("resources/config.properties", b"key=value\n"),
        ],
    );

    let cp_file = write_classpath_file(tmp.path(), &[jar_path.as_path()]);

    let config = ClasspathConfig {
        enabled: true,
        depth: ClasspathDepth::Full,
        classpath_file: Some(cp_file),
        force: false,
        timeout_secs: 30,
        ..ClasspathConfig::default()
    };

    let result = run_classpath_pipeline(tmp.path(), &config).unwrap();
    assert_eq!(
        result.classes_parsed, 1,
        "only .class entries should be parsed"
    );
    assert!(result.index.lookup_fqn("com.example.Valid").is_some());
}

// ═══════════════════════════════════════════════════════════════════════════
// 8. Index with Methods and Fields
// ═══════════════════════════════════════════════════════════════════════════

#[test]
fn index_preserves_method_stubs() {
    let tmp = TempDir::new().unwrap();
    let index_path = tmp.path().join("index.sqry");

    let stubs = vec![make_stub_with_methods(
        "com.example.Service",
        &["doWork", "initialize", "shutdown"],
    )];

    let index = ClasspathIndex::build(stubs);
    index.save(&index_path).unwrap();

    let loaded = ClasspathIndex::load(&index_path).unwrap();
    let service = loaded.lookup_fqn("com.example.Service").unwrap();
    assert_eq!(service.methods.len(), 3);
    let method_names: Vec<&str> = service.methods.iter().map(|m| m.name.as_str()).collect();
    assert!(method_names.contains(&"doWork"));
    assert!(method_names.contains(&"initialize"));
    assert!(method_names.contains(&"shutdown"));
}

#[test]
fn index_preserves_field_stubs() {
    let tmp = TempDir::new().unwrap();
    let index_path = tmp.path().join("index.sqry");

    let stubs = vec![make_stub_with_fields(
        "com.example.Config",
        &[
            ("port", "I"),
            ("host", "Ljava/lang/String;"),
            ("enabled", "Z"),
        ],
    )];

    let index = ClasspathIndex::build(stubs);
    index.save(&index_path).unwrap();

    let loaded = ClasspathIndex::load(&index_path).unwrap();
    let config = loaded.lookup_fqn("com.example.Config").unwrap();
    assert_eq!(config.fields.len(), 3);
    let field_names: Vec<&str> = config.fields.iter().map(|f| f.name.as_str()).collect();
    assert!(field_names.contains(&"port"));
    assert!(field_names.contains(&"host"));
    assert!(field_names.contains(&"enabled"));
}

// ═══════════════════════════════════════════════════════════════════════════
// 9. Class Kind Variants
// ═══════════════════════════════════════════════════════════════════════════

#[test]
fn index_handles_various_class_kinds() {
    let mut interface_stub = make_stub("com.example.Service");
    interface_stub.kind = ClassKind::Interface;

    let mut enum_stub = make_stub("com.example.Color");
    enum_stub.kind = ClassKind::Enum;

    let mut annotation_stub = make_stub("com.example.MyAnnotation");
    annotation_stub.kind = ClassKind::Annotation;

    let class_stub = make_stub("com.example.Impl");

    let index = ClasspathIndex::build(vec![interface_stub, enum_stub, annotation_stub, class_stub]);

    assert_eq!(index.classes.len(), 4);

    let service = index.lookup_fqn("com.example.Service").unwrap();
    assert_eq!(service.kind, ClassKind::Interface);

    let color = index.lookup_fqn("com.example.Color").unwrap();
    assert_eq!(color.kind, ClassKind::Enum);

    let ann = index.lookup_fqn("com.example.MyAnnotation").unwrap();
    assert_eq!(ann.kind, ClassKind::Annotation);

    let imp = index.lookup_fqn("com.example.Impl").unwrap();
    assert_eq!(imp.kind, ClassKind::Class);
}

// ═══════════════════════════════════════════════════════════════════════════
// 10. Pipeline Cache Behavior
// ═══════════════════════════════════════════════════════════════════════════

#[test]
fn pipeline_second_run_uses_cache() {
    let tmp = TempDir::new().unwrap();

    let class_a = build_minimal_class("com/cache/CacheTest");
    let jar_path = write_test_jar(
        tmp.path(),
        "cache-test.jar",
        &[("com/cache/CacheTest.class", &class_a)],
    );
    let cp_file = write_classpath_file(tmp.path(), &[jar_path.as_path()]);

    let config = ClasspathConfig {
        enabled: true,
        depth: ClasspathDepth::Full,
        classpath_file: Some(cp_file.clone()),
        force: false,
        timeout_secs: 30,
        ..ClasspathConfig::default()
    };

    // First run: fresh scan.
    let result1 = run_classpath_pipeline(tmp.path(), &config).unwrap();
    assert!(!result1.from_cache, "first run should not be from cache");
    assert_eq!(result1.classes_parsed, 1);

    // Second run: should use cache.
    let config2 = ClasspathConfig {
        enabled: true,
        depth: ClasspathDepth::Full,
        classpath_file: Some(cp_file),
        force: false,
        timeout_secs: 30,
        ..ClasspathConfig::default()
    };
    let result2 = run_classpath_pipeline(tmp.path(), &config2).unwrap();
    assert!(result2.from_cache, "second run should use cached stubs");
    assert_eq!(result2.classes_parsed, 1);
    assert!(
        result2.index.lookup_fqn("com.cache.CacheTest").is_some(),
        "cached result should still find classes"
    );
}

#[test]
fn pipeline_force_bypasses_cache() {
    let tmp = TempDir::new().unwrap();

    let class_a = build_minimal_class("com/force/ForceTest");
    let jar_path = write_test_jar(
        tmp.path(),
        "force-test.jar",
        &[("com/force/ForceTest.class", &class_a)],
    );
    let cp_file = write_classpath_file(tmp.path(), &[jar_path.as_path()]);

    // First run: populate cache.
    let config = ClasspathConfig {
        enabled: true,
        depth: ClasspathDepth::Full,
        classpath_file: Some(cp_file.clone()),
        force: false,
        timeout_secs: 30,
        ..ClasspathConfig::default()
    };
    let _ = run_classpath_pipeline(tmp.path(), &config).unwrap();

    // Second run with force: should NOT use cache.
    let config_force = ClasspathConfig {
        enabled: true,
        depth: ClasspathDepth::Full,
        classpath_file: Some(cp_file),
        force: true,
        timeout_secs: 30,
        ..ClasspathConfig::default()
    };
    let result = run_classpath_pipeline(tmp.path(), &config_force).unwrap();
    assert!(!result.from_cache, "force=true should bypass cache");
    assert_eq!(result.classes_parsed, 1);
}

// ═══════════════════════════════════════════════════════════════════════════
// 11. Large Index Stress Test
// ═══════════════════════════════════════════════════════════════════════════

#[test]
fn large_index_build_and_lookup() {
    let stubs: Vec<ClassStub> = (0..2000)
        .map(|i| {
            let pkg = match i % 4 {
                0 => "com.example.alpha",
                1 => "com.example.beta",
                2 => "org.library.core",
                _ => "io.framework.util",
            };
            make_stub(&format!("{pkg}.Class{i:04}"))
        })
        .collect();

    let index = ClasspathIndex::build(stubs);
    assert_eq!(index.classes.len(), 2000);

    // Verify sort order.
    for window in index.classes.windows(2) {
        assert!(
            window[0].fqn <= window[1].fqn,
            "sort violation: {} > {}",
            window[0].fqn,
            window[1].fqn
        );
    }

    // Spot-check lookups.
    assert!(index.lookup_fqn("com.example.alpha.Class0000").is_some());
    assert!(index.lookup_fqn("org.library.core.Class0002").is_some());
    assert!(index.lookup_fqn("io.framework.util.Class1999").is_some());
    assert!(index.lookup_fqn("com.example.alpha.Class9999").is_none());

    // Package grouping.
    let alpha = index.lookup_package("com.example.alpha");
    assert_eq!(alpha.len(), 500);
    let beta = index.lookup_package("com.example.beta");
    assert_eq!(beta.len(), 500);
}

// ═══════════════════════════════════════════════════════════════════════════
// 12. Build System Detection with Override (integration)
// ═══════════════════════════════════════════════════════════════════════════

#[test]
fn detect_override_all_build_systems() {
    let tmp = TempDir::new().unwrap();

    for (name, expected) in [
        ("gradle", BuildSystem::Gradle),
        ("maven", BuildSystem::Maven),
        ("bazel", BuildSystem::Bazel),
        ("sbt", BuildSystem::Sbt),
    ] {
        let result = detect_build_system(tmp.path(), Some(name));
        assert_eq!(
            result.build_system,
            Some(expected),
            "override '{name}' should detect {expected:?}"
        );
    }
}

#[test]
fn detect_override_case_insensitive() {
    let tmp = TempDir::new().unwrap();

    for name in [
        "GRADLE", "Gradle", "gradle", "MAVEN", "Maven", "BAZEL", "SBT",
    ] {
        let result = detect_build_system(tmp.path(), Some(name));
        assert!(
            result.build_system.is_some(),
            "case-insensitive override '{name}' should succeed"
        );
    }
}

#[test]
fn detect_override_invalid_returns_none() {
    let tmp = TempDir::new().unwrap();
    let result = detect_build_system(tmp.path(), Some("cmake"));
    assert_eq!(
        result.build_system, None,
        "invalid override should return None"
    );
}

// ═══════════════════════════════════════════════════════════════════════════
// 13. Full Pipeline Tests Requiring JVM Toolchain (ignored in CI)
// ═══════════════════════════════════════════════════════════════════════════

#[test]
#[ignore = "requires JVM toolchain: Gradle must be installed and on PATH"]
fn pipeline_gradle_single_module_real() {
    let fixture = fixtures_dir().join("gradle-single-module");
    let config = ClasspathConfig {
        enabled: true,
        depth: ClasspathDepth::Full,
        build_system_override: None,
        classpath_file: None,
        force: true,
        timeout_secs: 120,
    };

    let result = run_classpath_pipeline(&fixture, &config).unwrap();
    assert!(
        result.jars_scanned > 0,
        "should scan at least one JAR from Gradle"
    );
    assert!(
        result.classes_parsed > 0,
        "should parse at least one class from Gradle dependencies"
    );
}

#[test]
#[ignore = "requires JVM toolchain: Maven must be installed and on PATH"]
fn pipeline_maven_single_module_real() {
    let fixture = fixtures_dir().join("maven-single-module");
    let config = ClasspathConfig {
        enabled: true,
        depth: ClasspathDepth::Full,
        build_system_override: None,
        classpath_file: None,
        force: true,
        timeout_secs: 120,
    };

    let result = run_classpath_pipeline(&fixture, &config).unwrap();
    assert!(
        result.jars_scanned > 0,
        "should scan at least one JAR from Maven"
    );
}

#[test]
#[ignore = "requires JVM toolchain: Bazel must be installed and on PATH"]
fn pipeline_bazel_java_real() {
    let fixture = fixtures_dir().join("bazel-java");
    let config = ClasspathConfig {
        enabled: true,
        depth: ClasspathDepth::Full,
        build_system_override: None,
        classpath_file: None,
        force: true,
        timeout_secs: 120,
    };

    let result = run_classpath_pipeline(&fixture, &config).unwrap();
    assert!(
        result.jars_scanned > 0,
        "should scan at least one JAR from Bazel"
    );
}

#[test]
#[ignore = "requires JVM toolchain: Gradle (KTS) must be installed and on PATH"]
fn pipeline_kotlin_project_real() {
    let fixture = fixtures_dir().join("kotlin-project");
    let config = ClasspathConfig {
        enabled: true,
        depth: ClasspathDepth::Full,
        build_system_override: None,
        classpath_file: None,
        force: true,
        timeout_secs: 120,
    };

    let result = run_classpath_pipeline(&fixture, &config).unwrap();
    assert!(
        result.jars_scanned > 0,
        "should scan at least one JAR from Kotlin/Gradle"
    );
}

#[test]
#[ignore = "requires JVM toolchain: sbt must be installed and on PATH"]
fn pipeline_scala_project_real() {
    let fixture = fixtures_dir().join("scala-project");
    let config = ClasspathConfig {
        enabled: true,
        depth: ClasspathDepth::Full,
        build_system_override: None,
        classpath_file: None,
        force: true,
        timeout_secs: 120,
    };

    let result = run_classpath_pipeline(&fixture, &config).unwrap();
    assert!(
        result.jars_scanned > 0,
        "should scan at least one JAR from sbt"
    );
}