rsconstruct 0.9.78

Rust based fast build system
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
use std::fs;
use tempfile::TempDir;
#[allow(unused_imports)]
use std::path::Path;
use crate::common::{setup_test_project, run_rsconstruct, run_rsconstruct_with_env, run_rsconstruct_json, run_rsconstruct_json_with_env};

#[test]
fn clean_command() {
    let temp_dir = setup_test_project();
    let project_path = temp_dir.path();

    // Create and build a template
    fs::write(
        project_path.join("config/clean_test.py"),
        "test = 'clean'"
    ).expect("Failed to write config");

    fs::write(
        project_path.join("tera.templates/cleanme.txt.tera"),
        "{% set c = load_python(path='config/clean_test.py') %}{{ c.test }}"
    ).expect("Failed to write template");

    // Build
    let build_output = run_rsconstruct(project_path, &["build"]);
    assert!(build_output.status.success());

    // Verify files exist
    assert!(project_path.join("cleanme.txt").exists());
    assert!(project_path.join(".rsconstruct/db.redb").exists());

    // Clean
    let clean_output = run_rsconstruct(project_path, &["clean", "outputs"]);
    assert!(clean_output.status.success());

    // Verify build outputs are removed but cache is preserved
    assert!(!project_path.join("cleanme.txt").exists());
    assert!(project_path.join(".rsconstruct").exists());
}

#[test]
fn force_rebuild() {
    let temp_dir = setup_test_project();
    let project_path = temp_dir.path();

    // Create template
    fs::write(
        project_path.join("config/force.py"),
        "mode = 'force'"
    ).expect("Failed to write config");

    fs::write(
        project_path.join("tera.templates/force.txt.tera"),
        "{% set c = load_python(path='config/force.py') %}Mode: {{ c.mode }}"
    ).expect("Failed to write template");

    // First build
    let first_build = run_rsconstruct(project_path, &["build"]);
    assert!(first_build.status.success(), "First build failed: {}", String::from_utf8_lossy(&first_build.stderr));

    // Force rebuild - should process, not skip
    let result = run_rsconstruct_json(project_path, &["build", "--force"]);
    assert!(result.exit_success);
    assert_eq!(result.success, 1, "Should have 1 successful build");
    assert_eq!(result.skipped, 0, "Should not skip anything with --force");
}

#[test]
fn no_color_env() {
    let temp_dir = setup_test_project();
    let project_path = temp_dir.path();

    // Create a template so there's something to process
    fs::write(
        project_path.join("tera.templates/color_test.txt.tera"),
        "hello"
    ).unwrap();

    // Run with NO_COLOR set
    let output = run_rsconstruct_with_env(project_path, &["build"], &[("NO_COLOR", "1")]);
    assert!(output.status.success());
    let stdout = String::from_utf8_lossy(&output.stdout);

    // ANSI escape codes start with \x1b[
    assert!(!stdout.contains("\x1b["), "Output should not contain ANSI escape codes when NO_COLOR is set");
}

#[test]
fn timings_flag() {
    let temp_dir = setup_test_project();
    let project_path = temp_dir.path();

    // Create a template
    fs::write(
        project_path.join("tera.templates/timing_test.txt.tera"),
        "hello"
    ).unwrap();

    // Run with --timings
    let output = run_rsconstruct_with_env(project_path, &["build", "--timings"], &[("NO_COLOR", "1")]);
    assert!(output.status.success());
    let stdout = String::from_utf8_lossy(&output.stdout);

    // Should contain timing information
    assert!(stdout.contains("Timing:"), "Output should contain 'Timing:' header");
    assert!(stdout.contains("Total:"), "Output should contain 'Total:' line");
}

#[test]
fn no_timings_by_default() {
    let temp_dir = setup_test_project();
    let project_path = temp_dir.path();

    // Create a template
    fs::write(
        project_path.join("tera.templates/no_timing.txt.tera"),
        "hello"
    ).unwrap();

    // Run without --timings (and without --verbose)
    let output = run_rsconstruct(project_path, &["build"]);
    assert!(output.status.success());
    let stdout = String::from_utf8_lossy(&output.stdout);

    // Should NOT contain timing information
    assert!(!stdout.contains("Timing:"), "Output should not contain timing info without --timings flag");
    assert!(!stdout.contains("Total:"), "Output should not contain total timing without --timings flag");
}

#[test]
fn keep_going_continues_after_failure() {
    let temp_dir = setup_test_project();
    let project_path = temp_dir.path();

    // Create one bad template and one good template
    fs::write(project_path.join("tera.templates/bad.txt.tera"), "{{ invalid").unwrap();
    fs::write(project_path.join("tera.templates/good.txt.tera"), "hello").unwrap();

    // Run with --keep-going
    let output = run_rsconstruct_with_env(project_path, &["build", "-v", "--keep-going"], &[("NO_COLOR", "1")]);

    // Should exit non-zero because of the failure
    assert!(!output.status.success(), "Build should fail with bad template");

    // The good template should still have been processed (verify via output)
    let stdout = String::from_utf8_lossy(&output.stdout);
    // With --keep-going, both files should be attempted to be processed
    assert!(stdout.contains("Processing:"), "Files should be processed with --keep-going");
}

#[test]
fn keep_going_short_flag() {
    let temp_dir = setup_test_project();
    let project_path = temp_dir.path();

    // Create one bad template
    fs::write(project_path.join("tera.templates/bad_k.txt.tera"), "{{ invalid").unwrap();

    // Run with -k (short form)
    let output = run_rsconstruct_with_env(project_path, &["build", "-k"], &[("NO_COLOR", "1")]);

    // Should exit non-zero since the template has invalid content
    assert!(!output.status.success(), "Build should fail with bad template");
    let stderr = String::from_utf8_lossy(&output.stderr);
    let stdout = String::from_utf8_lossy(&output.stdout);

    // Should contain error reporting in stdout or stderr
    let combined = format!("{}{}", stdout, stderr);
    assert!(combined.contains("error") || combined.contains("Error"),
        "Should report errors: stdout={}, stderr={}", stdout, stderr);
}

#[test]
fn build_stops_on_first_error() {
    // Without --keep-going, build should stop immediately on first error
    let temp_dir = setup_test_project();
    let project_path = temp_dir.path();

    // "aaa" sorts before "zzz" alphabetically, so it will be processed first
    fs::write(project_path.join("tera.templates/aaa.txt.tera"), "{{ invalid").unwrap();
    fs::write(project_path.join("tera.templates/zzz.txt.tera"), "hello").unwrap();

    // Build should fail on aaa.txt.tera and stop
    let result = run_rsconstruct_json(project_path, &["build"]);
    assert!(!result.exit_success, "Build should fail with bad template");
    assert_eq!(result.failed, 1, "Should have exactly 1 failure");
    // zzz.txt should NOT be processed because we stop on first error
    assert!(!result.has_product("zzz.txt", "success"),
        "Second file should NOT be processed after first error");
}

#[test]
fn keep_going_continues_after_error() {
    // With --keep-going, independent products should still be processed.
    // Use an undefined variable (runtime error) rather than a parse error,
    // because Tera loads all templates at once and a parse error poisons them all.
    let temp_dir = setup_test_project();
    let project_path = temp_dir.path();

    fs::write(project_path.join("tera.templates/bad.txt.tera"), "{{ undefined_var }}").unwrap();
    fs::write(project_path.join("tera.templates/good.txt.tera"), "hello").unwrap();

    // First build with --keep-going — should fail but process all files
    let result1 = run_rsconstruct_json(project_path, &["build", "--keep-going"]);
    assert!(!result1.exit_success, "Build should fail with bad template");
    assert_eq!(result1.failed, 1, "Should have 1 failure");
    assert_eq!(result1.success, 1, "Should have 1 success (good.txt)");
    assert!(result1.has_product("good.txt", "success"),
        "Good template should be processed with --keep-going");

    // Fix the bad file
    fs::write(project_path.join("tera.templates/bad.txt.tera"), "fixed").unwrap();

    // Second build — good.txt should be skipped (cached)
    let result2 = run_rsconstruct_json(project_path, &["build"]);
    assert!(result2.exit_success, "Second build should succeed");
    assert_eq!(result2.skipped, 1, "Good template should be skipped (cached)");
    assert_eq!(result2.success, 1, "Bad template (now fixed) should be processed");
}

#[test]
fn parallel_build_with_j_flag() {
    // Verify -j flag enables parallel execution and all products are built
    let temp_dir = setup_test_project();
    let project_path = temp_dir.path();

    for name in &["alpha", "beta", "gamma", "delta"] {
        fs::write(
            project_path.join(format!("tera.templates/{}.txt.tera", name)),
            format!("content of {}", name),
        ).unwrap();
    }

    let result = run_rsconstruct_json(project_path, &["build", "-j2"]);
    assert!(result.exit_success, "Parallel build with -j2 should succeed");
    assert_eq!(result.success, 4, "Should process all 4 templates");
    assert_eq!(result.total_products, 4);
}

#[test]
fn parallel_keep_going_continues_after_failure() {
    // Verify --keep-going processes all independent products even when one fails
    // under parallel execution.
    // Use an undefined variable (runtime error) rather than a parse error,
    // because Tera loads all templates at once and a parse error poisons them all.
    let temp_dir = setup_test_project();
    let project_path = temp_dir.path();

    fs::write(project_path.join("tera.templates/aaa_bad.txt.tera"), "{{ undefined_var }}").unwrap();
    fs::write(project_path.join("tera.templates/good1.txt.tera"), "hello1").unwrap();
    fs::write(project_path.join("tera.templates/good2.txt.tera"), "hello2").unwrap();
    fs::write(project_path.join("tera.templates/good3.txt.tera"), "hello3").unwrap();
    fs::write(
        project_path.join("rsconstruct.toml"),
        "[processor.tera]\nsrc_dirs = [\"tera.templates\"]\n\n[build]\nparallel = 2\n"
    ).unwrap();

    let result = run_rsconstruct_json(project_path, &["build", "--keep-going"]);

    // Should fail overall
    assert!(!result.exit_success, "Build should fail with bad template even with --keep-going");
    assert_eq!(result.failed, 1, "Should have 1 failure");
    assert_eq!(result.success, 3, "All 3 good templates should be processed with --keep-going");
}

#[test]
fn parallel_builds_all_independent_products() {
    // Verify parallel config in rsconstruct.toml works and all products complete
    let temp_dir = setup_test_project();
    let project_path = temp_dir.path();

    for i in 0..8 {
        fs::write(
            project_path.join(format!("tera.templates/task_{:02}.txt.tera", i)),
            format!("content of task {}", i),
        ).unwrap();
    }
    fs::write(
        project_path.join("rsconstruct.toml"),
        "[processor.tera]\nsrc_dirs = [\"tera.templates\"]\n\n[build]\nparallel = 4\n"
    ).unwrap();

    let result = run_rsconstruct_json(project_path, &["build"]);
    assert!(result.exit_success, "Parallel build with 8 products and 4 jobs should succeed");
    assert_eq!(result.success, 8, "Should process all 8 templates");
    assert_eq!(result.total_products, 8);

    // Incremental: second build should skip everything
    let result2 = run_rsconstruct_json(project_path, &["build"]);
    assert!(result2.exit_success);
    assert_eq!(result2.skipped, 8, "All 8 products should be skipped on second build");
}

#[test]
fn parallel_timings_flag() {
    // Verify --timings output works with parallel builds.
    //
    // Each template gets distinct content so the produced output blobs
    // also differ. Identical content across products triggers a data race
    // in the object store's store_object check-then-write path under
    // parallel execution: two threads both observe the blob path missing
    // and both try to fs::write it, with the second hitting EACCES after
    // the first has already set the file read-only.
    let temp_dir = setup_test_project();
    let project_path = temp_dir.path();

    for name in &["one", "two", "three"] {
        fs::write(
            project_path.join(format!("tera.templates/{}.txt.tera", name)),
            format!("hello from {}", name),
        ).unwrap();
    }
    fs::write(
        project_path.join("rsconstruct.toml"),
        "[processor.tera]\nsrc_dirs = [\"tera.templates\"]\n\n[build]\nparallel = 2\n"
    ).unwrap();

    let output = run_rsconstruct_with_env(
        project_path, &["build", "--timings"], &[("NO_COLOR", "1")]
    );
    assert!(output.status.success(),
        "Parallel build with --timings should succeed: stdout={}, stderr={}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr));

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("Timing:"), "Should contain 'Timing:' header in parallel mode");
    assert!(stdout.contains("Total:"), "Should contain 'Total:' line in parallel mode");

    // Should have timing entries
    let timing_lines = stdout.lines()
        .filter(|l| l.contains("[tera]") && l.contains("(0."))
        .count();
    assert!(timing_lines >= 1,
        "Should have at least one timing entry: {}", stdout);
}

#[test]
fn deterministic_build_order() {
    // Run two separate builds with multiple templates and verify
    // that the processing order is identical both times.
    let outputs: Vec<Vec<String>> = (0..2).map(|_| {
        let temp_dir = setup_test_project();
        let project_path = temp_dir.path();

        // Create several template files with distinct names and content
        for name in &["zebra", "alpha", "mango", "banana", "cherry"] {
            fs::write(
                project_path.join(format!("tera.templates/{}.txt.tera", name)),
                format!("content of {}", name),
            ).unwrap();
        }

        let output = run_rsconstruct_with_env(project_path, &["build", "-v", "-j", "1"], &[("NO_COLOR", "1")]);
        assert!(output.status.success(),
            "Build failed: {}",
            String::from_utf8_lossy(&output.stderr));

        // Extract the target name from "Processing: <name>" lines
        let stdout = String::from_utf8_lossy(&output.stdout);
        let processing_names: Vec<String> = stdout
            .lines()
            .filter(|l| l.contains("Processing:"))
            .filter_map(|l| {
                l.split("Processing:").nth(1).map(|s| s.trim().to_string())
            })
            .collect();
        assert_eq!(processing_names.len(), 5, "Should process all 5 templates: {}", stdout);
        processing_names
    }).collect();

    assert_eq!(outputs[0], outputs[1],
        "Build order must be deterministic across runs.\nFirst:  {:?}\nSecond: {:?}",
        outputs[0], outputs[1]);
}

/// Test that classify_products propagates dependency changes transitively.
/// Setup: tera generates step1.txt, a second tera template depends on step1.txt via dep_inputs.
/// When the first tera template changes, both products should be classified as needing
/// rebuild — not just the first product.
#[test]
fn classify_propagates_through_dependencies() {
    let temp_dir = setup_test_project();
    let project_path = temp_dir.path();

    // Phase 1: build with tera to create the output file
    fs::write(
        project_path.join("config/gen.py"),
        "val = 1"
    ).unwrap();
    fs::write(
        project_path.join("tera.templates/step1.txt.tera"),
        "{% set c = load_python(path='config/gen.py') %}step1={{ c.val }}"
    ).unwrap();

    let output1 = run_rsconstruct_with_env(project_path, &["build"], &[("NO_COLOR", "1")]);
    assert!(output1.status.success(),
        "Phase 1 build failed: stdout={}, stderr={}",
        String::from_utf8_lossy(&output1.stdout),
        String::from_utf8_lossy(&output1.stderr));
    assert!(project_path.join("step1.txt").exists(), "Tera should generate step1.txt");

    // Phase 2: add a second template with dep_inputs pointing to the first tera output
    fs::write(
        project_path.join("rsconstruct.toml"),
        "[processor.tera]\nsrc_dirs = [\"tera.templates\"]\ndep_inputs = [\"step1.txt\"]\n"
    ).unwrap();
    fs::write(
        project_path.join("tera.templates/step2.txt.tera"),
        "step2"
    ).unwrap();

    // Build both products
    let output2 = run_rsconstruct_with_env(project_path, &["build"], &[("NO_COLOR", "1")]);
    assert!(output2.status.success(),
        "Phase 2 build failed: stdout={}, stderr={}",
        String::from_utf8_lossy(&output2.stdout),
        String::from_utf8_lossy(&output2.stderr));

    // Verify everything is up-to-date
    let output3 = run_rsconstruct_with_env(
        project_path, &["build", "--stop-after", "classify"], &[("NO_COLOR", "1")]
    );
    assert!(output3.status.success());
    let stdout3 = String::from_utf8_lossy(&output3.stdout);
    assert!(stdout3.contains("0 to build, 0 to restore (2 up-to-date)"),
        "Both products should be up-to-date: {}", stdout3);

    // Wait so mtime differs
    std::thread::sleep(std::time::Duration::from_millis(100));

    // Modify the first tera template
    fs::write(
        project_path.join("tera.templates/step1.txt.tera"),
        "{% set c = load_python(path='config/gen.py') %}modified={{ c.val }}"
    ).unwrap();

    // Classify: both products should need work (tera rebuild + second rebuild/restore)
    let output4 = run_rsconstruct_with_env(
        project_path, &["build", "--stop-after", "classify"], &[("NO_COLOR", "1")]
    );
    assert!(output4.status.success());
    let stdout4 = String::from_utf8_lossy(&output4.stdout);
    assert!(stdout4.contains("0 up-to-date"),
        "No products should be up-to-date when root dependency changed: {}", stdout4);
}

#[test]
fn checker_and_generator_both_rebuild_on_shared_input_change() {
    // Regression test: when a checker and generator share the same input file,
    // modifying the input must cause BOTH to rebuild, not just the checker.
    // Bug scenario: checker runs first, updates the input hash in cache,
    // then generator sees matching hash and skips even though its output is stale.
    let temp_dir = setup_test_project();
    let project_path = temp_dir.path();

    // Enable both tera (generator) and script (checker) on .tera files
    fs::write(
        project_path.join("rsconstruct.toml"),
        concat!(
            "[processor.tera]\n",
            "src_dirs = [\"tera.templates\"]\n",
            "\n",
            "[processor.script]\n",
            "src_dirs = [\"tera.templates\"]\n",
            "src_extensions = [\".tera\"]\n",
            "command = \"true\"\n",
        ),
    ).unwrap();

    // Create a template
    fs::write(
        project_path.join("tera.templates/shared.txt.tera"),
        "version1",
    ).unwrap();

    // First build: both checker and generator should run
    let result1 = run_rsconstruct_json(project_path, &["build"]);
    assert!(result1.exit_success, "First build should succeed");
    assert!(result1.has_product("shared.txt", "success"),
        "Tera should process: {:?}", result1.products);
    assert_eq!(result1.failed, 0, "No failures expected");

    // Verify the output was created
    assert!(project_path.join("shared.txt").exists(),
        "Tera output should exist after first build");
    let content1 = fs::read_to_string(project_path.join("shared.txt")).unwrap();
    assert_eq!(content1, "version1");

    // Second build: everything should be skipped (no changes)
    let result2 = run_rsconstruct_json(project_path, &["build"]);
    assert!(result2.exit_success);
    assert_eq!(result2.skipped, result2.total_products,
        "All products should be skipped on second build (no changes)");

    // Wait for mtime to differ
    std::thread::sleep(std::time::Duration::from_millis(100));

    // Modify the shared input file
    fs::write(
        project_path.join("tera.templates/shared.txt.tera"),
        "version2",
    ).unwrap();

    // Third build: BOTH checker and generator must rebuild
    let result3 = run_rsconstruct_json(project_path, &["build"]);
    assert!(result3.exit_success, "Third build should succeed: {:?}", result3.errors);
    assert_eq!(result3.skipped, 0,
        "No products should be skipped after input change, got: {:?}", result3.products);
    assert!(result3.has_product("shared.txt", "success"),
        "Tera generator MUST rebuild after input change: {:?}", result3.products);

    // Verify the output was updated
    let content3 = fs::read_to_string(project_path.join("shared.txt")).unwrap();
    assert_eq!(content3, "version2",
        "Output should contain new content after rebuild");
}

/// Test that cross-processor dependencies work: a downstream processor discovers
/// products whose inputs are declared outputs of an upstream processor, even on
/// a clean build where those output files don't exist on disk yet.
#[test]
fn cross_processor_discovery() {
    let temp_dir = tempfile::TempDir::new().unwrap();
    let project_path = temp_dir.path();

    // Enable tera (generates files) and ascii (checks files).
    // Configure ascii to scan .txt files at the project root — exactly where
    // tera will output them.
    fs::write(
        project_path.join("rsconstruct.toml"),
        r#"
[processor.tera]
src_dirs = ["tera.templates"]

[processor.ascii]
src_dirs = ["."]
src_extensions = [".txt"]
"#,
    ).unwrap();

    // Create a tera template that generates a .txt file
    fs::create_dir_all(project_path.join("tera.templates")).unwrap();
    fs::write(
        project_path.join("tera.templates/generated.txt.tera"),
        "hello world",
    ).unwrap();

    // The generated.txt does NOT exist on disk yet — this is a clean build.
    assert!(!project_path.join("generated.txt").exists());

    // Ask rsconstruct to list processor files (discovery only, no build).
    // The ascii processor should discover generated.txt as an input,
    // because the fixed-point discovery loop injects tera's declared output
    // as a virtual file.
    let output = run_rsconstruct_with_env(
        project_path,
        &["--json", "processors", "files"],
        &[("NO_COLOR", "1")],
    );
    assert!(output.status.success(), "processors files failed: {}",
        String::from_utf8_lossy(&output.stderr));

    let stdout = String::from_utf8_lossy(&output.stdout);
    let parsed: Vec<serde_json::Value> = serde_json::from_str(&stdout)
        .unwrap_or_else(|e| panic!("JSON parse failed: {}\nOutput: {}", e, stdout));

    // Find the ascii processor's entries
    let ascii_products: Vec<&serde_json::Value> = parsed.iter()
        .filter(|p| p["processor"].as_str() == Some("ascii"))
        .collect();

    assert!(!ascii_products.is_empty(),
        "ascii processor should have discovered products from tera's output.\n\
         All products: {:?}", parsed);

    // Verify that generated.txt is an input to ascii
    let has_generated_input = ascii_products.iter().any(|p| {
        p["inputs"].as_array().unwrap().iter()
            .any(|i| i.as_str().unwrap().contains("generated.txt"))
    });
    assert!(has_generated_input,
        "ascii should have generated.txt as input.\nascii products: {:?}", ascii_products);
}

/// Test the explicit processor: declares inputs, input_globs, and outputs explicitly.
/// Verifies discovery creates a single product with all resolved inputs and the declared output.
#[test]
fn explicit_processor_discovery() {
    let temp_dir = tempfile::TempDir::new().unwrap();
    let project_path = temp_dir.path();

    // Create input files
    fs::create_dir_all(project_path.join("data")).unwrap();
    fs::write(project_path.join("config.txt"), "config").unwrap();
    fs::write(project_path.join("data/a.csv"), "a").unwrap();
    fs::write(project_path.join("data/b.csv"), "b").unwrap();
    fs::write(project_path.join("data/skip.txt"), "not a csv").unwrap();

    // Configure an explicit processor with literal inputs and a glob
    fs::write(
        project_path.join("rsconstruct.toml"),
        r#"
[processor.explicit.report]
command = "scripts/build_report.py"
inputs = ["config.txt"]
input_globs = ["data/*.csv"]
output_files = ["out/report.html"]
src_dirs = ["."]
"#,
    ).unwrap();

    let output = run_rsconstruct_with_env(
        project_path,
        &["--json", "processors", "files"],
        &[("NO_COLOR", "1")],
    );
    assert!(output.status.success(), "processors files failed: {}",
        String::from_utf8_lossy(&output.stderr));

    let stdout = String::from_utf8_lossy(&output.stdout);
    let parsed: Vec<serde_json::Value> = serde_json::from_str(&stdout)
        .unwrap_or_else(|e| panic!("JSON parse failed: {}\nOutput: {}", e, stdout));

    // Should have exactly one product
    let explicit_products: Vec<&serde_json::Value> = parsed.iter()
        .filter(|p| p["processor"].as_str().unwrap().contains("explicit"))
        .collect();
    assert_eq!(explicit_products.len(), 1,
        "Expected 1 explicit product, got {}: {:?}", explicit_products.len(), explicit_products);

    let product = explicit_products[0];

    // Check inputs: config.txt (literal) + data/a.csv, data/b.csv (glob, sorted)
    let inputs: Vec<&str> = product["inputs"].as_array().unwrap()
        .iter().map(|v| v.as_str().unwrap()).collect();
    assert_eq!(inputs.len(), 3, "Expected 3 inputs: {:?}", inputs);
    assert_eq!(inputs[0], "config.txt");
    assert!(inputs[1].ends_with("a.csv"), "Expected a.csv, got {}", inputs[1]);
    assert!(inputs[2].ends_with("b.csv"), "Expected b.csv, got {}", inputs[2]);
    // skip.txt should NOT be included (not matching *.csv)
    assert!(!inputs.iter().any(|i| i.contains("skip.txt")), "skip.txt should not be an input");

    // Check output
    let outputs: Vec<&str> = product["outputs"].as_array().unwrap()
        .iter().map(|v| v.as_str().unwrap()).collect();
    assert_eq!(outputs, vec!["out/report.html"]);
}

/// Test that a downstream processor discovers files in a directory that doesn't
/// exist on disk yet — the directory is created by an upstream generator.
/// This is a clean-build scenario: the generator outputs to out/generated/,
/// and ascii is configured to scan out/generated/ for .txt files.
/// The out/generated/ directory does NOT exist before the build.
#[test]
fn cross_processor_nonexistent_output_dir() {
    let temp_dir = tempfile::TempDir::new().unwrap();
    let project_path = temp_dir.path();

    // Create a tera template that generates a .txt file into a subdirectory.
    // Tera scans tera.templates/ and strips the scan_dir prefix, so
    // tera.templates/out/generated/hello.txt.tera → out/generated/hello.txt
    fs::create_dir_all(project_path.join("tera.templates/out/generated")).unwrap();
    fs::write(
        project_path.join("tera.templates/out/generated/hello.txt.tera"),
        "hello world",
    ).unwrap();

    // Configure tera (generator) and ascii (checker scanning out/generated/).
    // The directory out/generated/ does NOT exist on disk.
    fs::write(
        project_path.join("rsconstruct.toml"),
        r#"
[processor.tera]
src_dirs = ["tera.templates"]

[processor.ascii]
src_dirs = ["out/generated"]
src_extensions = [".txt"]
"#,
    ).unwrap();

    // Verify out/generated/ does not exist on disk
    assert!(!project_path.join("out/generated").exists(),
        "out/generated/ should not exist before discovery");

    // Run discovery via processors files (JSON)
    let output = run_rsconstruct_with_env(
        project_path,
        &["--json", "processors", "files"],
        &[("NO_COLOR", "1")],
    );
    assert!(output.status.success(), "processors files failed: {}",
        String::from_utf8_lossy(&output.stderr));

    let stdout = String::from_utf8_lossy(&output.stdout);
    let parsed: Vec<serde_json::Value> = serde_json::from_str(&stdout)
        .unwrap_or_else(|e| panic!("JSON parse failed: {}\nOutput: {}", e, stdout));

    // Tera should have 1 product
    let tera_products: Vec<&serde_json::Value> = parsed.iter()
        .filter(|p| p["processor"].as_str() == Some("tera"))
        .collect();
    assert_eq!(tera_products.len(), 1, "Expected 1 tera product: {:?}", tera_products);

    // ASCII should discover the tera output even though out/generated/ doesn't exist on disk.
    // The fixed-point discovery loop injects tera's declared output as a virtual file.
    let ascii_products: Vec<&serde_json::Value> = parsed.iter()
        .filter(|p| p["processor"].as_str() == Some("ascii"))
        .collect();
    assert_eq!(ascii_products.len(), 1,
        "ascii should discover 1 product from tera's output in nonexistent dir.\n\
         All products: {:?}", parsed);

    // Verify the ascii product's input is the tera output
    let ascii_input = ascii_products[0]["inputs"].as_array().unwrap()[0].as_str().unwrap();
    assert!(ascii_input.contains("out/generated/hello.txt"),
        "ascii input should be out/generated/hello.txt, got: {}", ascii_input);
}

#[test]
fn max_jobs_limits_per_processor_concurrency() {
    // Verify that max_jobs limits concurrency for a specific processor.
    // Uses the script processor with a bash script that tracks peak concurrency
    // via a shared counter file protected by flock.
    //
    // Setup: 8 input files, global -j8, but max_jobs=2 for the script processor.
    // The script sleeps 0.3s per file, so without max_jobs all 8 would run at once.
    // With max_jobs=2, peak concurrency must never exceed 2.

    let temp_dir = tempfile::TempDir::new().unwrap();
    let project_path = temp_dir.path();

    // Create the concurrency-tracking script
    let script_content = r#"#!/bin/bash
# Track concurrency: atomically increment counter, record peak, sleep, decrement.
COUNTER_FILE="$PROJECT_ROOT/.concurrency_counter"
PEAK_FILE="$PROJECT_ROOT/.concurrency_peak"
LOCK_FILE="$PROJECT_ROOT/.concurrency_lock"

# Increment and record peak
(
    flock 9
    current=$(cat "$COUNTER_FILE" 2>/dev/null || echo 0)
    current=$((current + 1))
    echo "$current" > "$COUNTER_FILE"
    peak=$(cat "$PEAK_FILE" 2>/dev/null || echo 0)
    if [ "$current" -gt "$peak" ]; then
        echo "$current" > "$PEAK_FILE"
    fi
) 9>"$LOCK_FILE"

# Hold the slot for a bit so concurrency can be observed
sleep 0.3

# Decrement
(
    flock 9
    current=$(cat "$COUNTER_FILE" 2>/dev/null || echo 0)
    current=$((current - 1))
    echo "$current" > "$COUNTER_FILE"
) 9>"$LOCK_FILE"
"#;

    let script_path = project_path.join("check_concurrency.sh");
    fs::write(&script_path, script_content).unwrap();
        {
        use std::os::unix::fs::PermissionsExt;
        fs::set_permissions(&script_path, fs::Permissions::from_mode(0o755)).unwrap();
    }

    // Create rsconstruct.toml with script processor, max_jobs=2, batch disabled
    let config = format!(
        r#"[processor.script]
command = "bash"
args = ["{script}"]
src_extensions = [".txt"]
src_dirs = ["inputs"]
max_jobs = 2
batch = false
"#,
        script = script_path.display(),
    );
    fs::write(project_path.join("rsconstruct.toml"), &config).unwrap();

    // Create 8 input files
    fs::create_dir_all(project_path.join("inputs")).unwrap();
    for i in 0..8 {
        fs::write(
            project_path.join(format!("inputs/file_{:02}.txt", i)),
            format!("content {}", i),
        ).unwrap();
    }

    // Initialize counter files
    fs::write(project_path.join(".concurrency_counter"), "0").unwrap();
    fs::write(project_path.join(".concurrency_peak"), "0").unwrap();

    // Run build with -j8 global parallelism
    let result = run_rsconstruct_json_with_env(
        project_path,
        &["build", "-j8"],
        &[("NO_COLOR", "1"), ("PROJECT_ROOT", project_path.to_str().unwrap())],
    );

    assert!(result.exit_success,
        "Build should succeed. Errors: {:?}", result.errors);
    assert_eq!(result.success, 8, "All 8 files should be processed");

    // Read peak concurrency
    let peak: usize = fs::read_to_string(project_path.join(".concurrency_peak"))
        .unwrap()
        .trim()
        .parse()
        .unwrap();

    assert!(peak <= 2,
        "Peak concurrency was {} but max_jobs=2 should limit it to 2", peak);
    assert!(peak >= 1,
        "Peak concurrency should be at least 1, got {}", peak);
}

#[test]
fn max_jobs_unset_allows_full_parallelism() {
    // Verify that without max_jobs, the processor uses full global parallelism.
    // Same setup as above but without max_jobs — peak should be > 2.

    let temp_dir = tempfile::TempDir::new().unwrap();
    let project_path = temp_dir.path();

    let script_content = r#"#!/bin/bash
COUNTER_FILE="$PROJECT_ROOT/.concurrency_counter"
PEAK_FILE="$PROJECT_ROOT/.concurrency_peak"
LOCK_FILE="$PROJECT_ROOT/.concurrency_lock"

(
    flock 9
    current=$(cat "$COUNTER_FILE" 2>/dev/null || echo 0)
    current=$((current + 1))
    echo "$current" > "$COUNTER_FILE"
    peak=$(cat "$PEAK_FILE" 2>/dev/null || echo 0)
    if [ "$current" -gt "$peak" ]; then
        echo "$current" > "$PEAK_FILE"
    fi
) 9>"$LOCK_FILE"

sleep 0.3

(
    flock 9
    current=$(cat "$COUNTER_FILE" 2>/dev/null || echo 0)
    current=$((current - 1))
    echo "$current" > "$COUNTER_FILE"
) 9>"$LOCK_FILE"
"#;

    let script_path = project_path.join("check_concurrency.sh");
    fs::write(&script_path, script_content).unwrap();
        {
        use std::os::unix::fs::PermissionsExt;
        fs::set_permissions(&script_path, fs::Permissions::from_mode(0o755)).unwrap();
    }

    // No max_jobs, batch disabled — should use full parallelism
    let config = format!(
        r#"[processor.script]
command = "bash"
args = ["{script}"]
src_extensions = [".txt"]
src_dirs = ["inputs"]
batch = false
"#,
        script = script_path.display(),
    );
    fs::write(project_path.join("rsconstruct.toml"), &config).unwrap();

    fs::create_dir_all(project_path.join("inputs")).unwrap();
    for i in 0..8 {
        fs::write(
            project_path.join(format!("inputs/file_{:02}.txt", i)),
            format!("content {}", i),
        ).unwrap();
    }

    fs::write(project_path.join(".concurrency_counter"), "0").unwrap();
    fs::write(project_path.join(".concurrency_peak"), "0").unwrap();

    let result = run_rsconstruct_json_with_env(
        project_path,
        &["build", "-j8"],
        &[("NO_COLOR", "1"), ("PROJECT_ROOT", project_path.to_str().unwrap())],
    );

    assert!(result.exit_success,
        "Build should succeed. Errors: {:?}", result.errors);
    assert_eq!(result.success, 8, "All 8 files should be processed");

    let peak: usize = fs::read_to_string(project_path.join(".concurrency_peak"))
        .unwrap()
        .trim()
        .parse()
        .unwrap();

    // Without max_jobs and -j8, peak should be higher than 2
    // (on any machine with >= 2 cores)
    assert!(peak > 2,
        "Without max_jobs, peak concurrency should exceed 2 with -j8, got {}", peak);
}

#[test]
fn generator_non_batch_partial_failure_only_rebuilds_failed() {
    // In non-batch mode (default fail-fast with chunk_size=1), each product
    // executes independently. Successful products are cached and skipped on
    // the next run — only the failed product needs rebuilding.
    let temp_dir = tempfile::TempDir::new().unwrap();
    let project_path = temp_dir.path();

    // Script that copies input to output but fails if input contains "FAIL"
    let script_path = project_path.join("transform.sh");
    fs::write(&script_path, r#"#!/bin/bash
input="$1"; output="$2"
if grep -q "FAIL" "$input"; then
    echo "Error: $input contains FAIL" >&2
    exit 1
fi
cp "$input" "$output"
"#).unwrap();
        {
        use std::os::unix::fs::PermissionsExt;
        fs::set_permissions(&script_path, fs::Permissions::from_mode(0o755)).unwrap();
    }

    fs::create_dir_all(project_path.join("src")).unwrap();
    fs::write(project_path.join("src/aaa_good1.txt"), "content1\n").unwrap();
    fs::write(project_path.join("src/aaa_good2.txt"), "content2\n").unwrap();
    fs::write(project_path.join("src/zzz_bad.txt"), "FAIL\n").unwrap();

    fs::write(project_path.join("rsconstruct.toml"), format!(
        r#"[processor.generator]
command = "{script}"
src_extensions = [".txt"]
src_dirs = ["src"]
output_extension = "out"
batch = false
"#,
        script = script_path.display(),
    )).unwrap();

    // First build with --keep-going: good files succeed, bad file fails
    let result1 = run_rsconstruct_json_with_env(
        project_path, &["build", "--keep-going"],
        &[("NO_COLOR", "1")],
    );
    assert!(!result1.exit_success, "First build should fail (zzz_bad.txt)");
    assert_eq!(result1.success, 2, "Two good files should succeed");
    assert_eq!(result1.failed, 1, "One bad file should fail");

    // Fix the bad file
    fs::write(project_path.join("src/zzz_bad.txt"), "fixed\n").unwrap();

    // Second build: good files should be skipped (cached), only bad file rebuilt
    let result2 = run_rsconstruct_json_with_env(
        project_path, &["build"],
        &[("NO_COLOR", "1")],
    );
    assert!(result2.exit_success, "Second build should succeed");
    assert_eq!(result2.skipped, 2, "Two good files should be skipped (cached)");
    assert_eq!(result2.success, 1, "Only the fixed file should be rebuilt");
}

/// Helper: write a minimal 2-processor project (`tera` + `ruff`) so we can
/// exercise `-x` against a known set of processors without pulling in real
/// external tools for actual execution.
fn setup_two_processor_project() -> tempfile::TempDir {
    let temp_dir = tempfile::TempDir::new().expect("Failed to create temp dir");
    let p = temp_dir.path();
    fs::create_dir_all(p.join("tera.templates")).unwrap();
    fs::create_dir_all(p.join("src")).unwrap();
    fs::write(p.join("rsconstruct.toml"), "[processor.tera]\nsrc_dirs = [\"tera.templates\"]\n\n[processor.ruff]\nsrc_dirs = [\"src\"]\n").unwrap();
    fs::write(p.join("src/hello.py"), "print('hi')\n").unwrap();
    temp_dir
}

/// `-x tera` must exclude only tera; other processors still run.
#[test]
fn exclude_processor_runs_everything_else() {
    let temp_dir = setup_two_processor_project();
    let project_path = temp_dir.path();

    // Build with ruff excluded via -x. The tera processor should still be
    // active — we verify by checking the classify line reports at least one
    // product (tera) and zero ruff-attributable activity.
    let output = run_rsconstruct_with_env(
        project_path, &["build", "-x", "ruff"], &[("NO_COLOR", "1")],
    );
    // Build should succeed (tera has no templates so it's a no-op but valid).
    assert!(
        output.status.success(),
        "build with -x ruff must succeed: stdout={}, stderr={}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr),
    );
    let combined = format!(
        "{}{}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr),
    );
    // Stdout/stderr must not mention processing the ruff product for hello.py.
    assert!(
        !combined.contains("[ruff]"),
        "ruff must not run when -x ruff is passed: {}", combined
    );
}

/// `-x unknown` must fail with a CONFIG_ERROR, same as `-p unknown`.
#[test]
fn exclude_unknown_processor_is_config_error() {
    let temp_dir = setup_test_project();
    let project_path = temp_dir.path();

    let output = run_rsconstruct_with_env(
        project_path, &["build", "-x", "nonexistent"], &[("NO_COLOR", "1")],
    );
    assert!(!output.status.success());
    let exit_code = output.status.code().unwrap();
    assert_eq!(exit_code, 2, "Expected CONFIG_ERROR (2), got {}", exit_code);
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("Unknown processor"),
        "Error should name the unknown processor: {}", stderr
    );
}

/// `-p foo -x foo` must reject the conflicting intent.
#[test]
fn include_and_exclude_same_processor_is_error() {
    let temp_dir = setup_two_processor_project();
    let project_path = temp_dir.path();

    let output = run_rsconstruct_with_env(
        project_path, &["build", "-p", "tera", "-x", "tera"], &[("NO_COLOR", "1")],
    );
    assert!(!output.status.success(), "-p tera -x tera must fail");
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("both -p and -x"),
        "Error should explain the conflict: {}", stderr
    );
}

/// Regression test for the chain SVG → marp → ipdfunite.
///
/// The markdown analyzer must surface the SVG referenced by a marp deck as a
/// dependency of the deck's PDF product. When the SVG content changes, the
/// marp PDF must be classified for rebuild, and that change must transitively
/// propagate to the ipdfunite product that merges the deck PDFs — otherwise a
/// stale merged PDF can survive even though its source content changed.
///
/// Requires marp (`node` + the marp-cli npm package); ipdfunite is
/// in-process so always runs when reached.
#[test]
fn svg_change_rebuilds_marp_and_ipdfunite() {
    crate::common::require_tool("marp");

    let temp_dir = tempfile::TempDir::new().expect("Failed to create temp dir");
    let project_path = temp_dir.path();

    // Layout:
    //   marp/courses/deck/a.md   → references svg/img.svg
    //   svg/img.svg              → real SVG, picked up by [analyzer.markdown]
    //   rsconstruct.toml         → marp + ipdfunite + markdown analyzer
    fs::create_dir_all(project_path.join("marp/courses/deck")).unwrap();
    fs::create_dir_all(project_path.join("svg")).unwrap();
    fs::write(
        project_path.join("svg/img.svg"),
        r#"<svg xmlns="http://www.w3.org/2000/svg" width="10" height="10"><rect width="10" height="10" fill="red"/></svg>"#,
    ).unwrap();
    fs::write(
        project_path.join("marp/courses/deck/a.md"),
        "# Slide\n\n![img](svg/img.svg)\n",
    ).unwrap();
    // Generous marp timeout for shared CI hosts — marp/Chrome can be slow
    // to launch under load. We do not care about render performance here.
    fs::write(
        project_path.join("rsconstruct.toml"),
        concat!(
            "[processor.marp]\n",
            "src_dirs = [\"marp\"]\n",
            "timeout_secs = 120\n",
            "max_attempts = 3\n",
            "[processor.ipdfunite]\n",
            "src_dirs = [\"marp\"]\n",
            "[analyzer.markdown]\n",
        ),
    ).unwrap();

    // Phase 1: clean build. Both marp and ipdfunite must succeed.
    let result1 = run_rsconstruct_json(project_path, &["build"]);
    assert!(result1.exit_success,
        "Phase 1 build failed (errors={:?}, products={:?})",
        result1.errors, result1.products);
    assert!(project_path.join("out/marp/courses/deck/a.pdf").exists(),
        "marp output PDF should exist on disk");
    assert!(project_path.join("out/ipdfunite/deck.pdf").exists(),
        "ipdfunite merged PDF should exist on disk");

    // Wait so mtime differs and the markdown analyzer's mtime shortcut
    // doesn't decide nothing changed before reading content.
    std::thread::sleep(std::time::Duration::from_millis(100));

    // Phase 2: modify the SVG that the marp deck references. The point of
    // the test: this change must flow through to BOTH the marp PDF and the
    // ipdfunite merged PDF.
    fs::write(
        project_path.join("svg/img.svg"),
        r#"<svg xmlns="http://www.w3.org/2000/svg" width="10" height="10"><rect width="10" height="10" fill="blue"/></svg>"#,
    ).unwrap();

    // Classify-only first: prove the marp product is flagged for rebuild
    // BEFORE anything runs. This is the load-bearing assertion for the
    // markdown analyzer / SVG-dep wiring — independent of caching quirks
    // downstream.
    let classify = run_rsconstruct_with_env(
        project_path, &["build", "--stop-after", "classify"], &[("NO_COLOR", "1")]
    );
    assert!(classify.status.success(),
        "Classify should succeed: stderr={}", String::from_utf8_lossy(&classify.stderr));
    let classify_out = String::from_utf8_lossy(&classify.stdout);
    assert!(!classify_out.contains("(2 up-to-date)"),
        "After SVG change, not every product should be up-to-date: {}", classify_out);

    // Phase 3: actually rebuild and confirm BOTH products ran (or restored).
    // The user-facing contract: a stale ipdfunite output must NOT survive an
    // SVG change.
    let result3 = run_rsconstruct_json(project_path, &["build"]);
    assert!(result3.exit_success,
        "Phase 3 build failed (errors={:?})", result3.errors);
    let marp_ran = result3.products.iter().any(|p|
        p.processor == "marp" && p.status == "success"
    );
    assert!(marp_ran,
        "marp must rebuild when its referenced SVG changes: {:?}", result3.products);
    let ipdfunite_ran = result3.products.iter().any(|p|
        p.processor == "ipdfunite" && p.status == "success"
    );
    assert!(ipdfunite_ran,
        "ipdfunite must rebuild when its upstream marp PDF changes: {:?}",
        result3.products);
}

/// Under `--json`, stdout carries a machine-readable event stream: every
/// line must parse as JSON. Human progress lines leaking onto stdout make
/// the output unparseable for any consumer that doesn't defensively skip
/// junk (the test harness itself used to silently discard such lines,
/// which is why this went unnoticed).
///
/// Covers the three shapes that leaked: plain `--json` (build phase
/// summaries), `--json -v` (per-product progress), and the incremental
/// re-run (skip lines). `--color=always` is included because JSON mode
/// must never emit ANSI escapes onto stdout either.
#[test]
fn json_mode_stdout_is_pure_json() {
    let temp_dir = setup_test_project();
    let project_path = temp_dir.path();
    for i in 1..=3 {
        fs::write(
            project_path.join(format!("tera.templates/f{i}.txt.tera")),
            format!("Hello {i}\n"),
        ).unwrap();
    }

    let cases: [&[&str]; 4] = [
        &["--json", "build"],
        &["--json", "-v", "build", "--force"],
        &["--json", "-v", "build"],
        &["--json", "--color=always", "-v", "build", "--force"],
    ];

    for args in cases {
        let output = run_rsconstruct_with_env(project_path, args, &[]);
        assert!(output.status.success(),
            "build failed for {args:?}: {}", String::from_utf8_lossy(&output.stderr));

        let stdout = String::from_utf8_lossy(&output.stdout);
        for line in stdout.lines().filter(|l| !l.trim().is_empty()) {
            assert!(!line.contains('\u{1b}'),
                "ANSI escape on stdout in JSON mode ({args:?}): {line:?}");
            assert!(serde_json::from_str::<serde_json::Value>(line).is_ok(),
                "non-JSON line on stdout in JSON mode ({args:?}): {line:?}");
        }
    }
}

/// No processor scans the project tree by default. An omitted or empty
/// `src_dirs` matches nothing, so a processor declared without it builds
/// nothing rather than sweeping node_modules/, .venv/ and vendored code.
/// Scanning everything stays available, but only as a deliberate opt-in via
/// the empty string, which means the project root.
#[test]
fn src_dirs_never_defaults_to_scanning_the_tree() {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let project_path = temp_dir.path();
    fs::create_dir_all(project_path.join("sub")).unwrap();
    fs::write(project_path.join("a.py"), "x = 1\n").unwrap();
    fs::write(project_path.join("sub/b.py"), "y = 2\n").unwrap();

    // (config, expect_any_products) — ruff defaults to src_dirs = [].
    let cases: [(&str, bool); 4] = [
        ("[processor.ruff]\n", false),
        ("[processor.ruff]\nsrc_dirs = []\n", false),
        ("[processor.ruff]\nsrc_dirs = [\"sub\"]\n", true),
        ("[processor.ruff]\nsrc_dirs = [\"\"]\n", true),
    ];

    for (config, expect_products) in cases {
        fs::write(project_path.join("rsconstruct.toml"), config).unwrap();
        let output = run_rsconstruct(project_path, &["build"]);
        assert!(output.status.success(),
            "build should succeed for config {config:?}: {}", String::from_utf8_lossy(&output.stderr));

        let stdout = String::from_utf8_lossy(&output.stdout);
        let built_nothing = stdout.contains("Nothing to build");
        assert_eq!(!built_nothing, expect_products,
            "config {config:?} should {} have matched files, got:\n{stdout}",
            if expect_products { "" } else { "not" });
    }
}

/// The file index must never pick up rsconstruct's own state directory.
///
/// Regression test: `FileIndex::build` used to index `.rsconstruct/` like any
/// other directory — only the user's `.gitignore` accidentally excluded it.
/// In a project without one, a root-scanning checker would lint the tool's
/// own cache internals and fail the build.
#[test]
fn state_dir_is_never_indexed() {
    let temp_dir = setup_test_project();
    let project_path = temp_dir.path();

    // Root-scanning internal JSON checker; the test project has no
    // .gitignore, so nothing but the file index itself excludes .rsconstruct.
    fs::write(
        project_path.join("rsconstruct.toml"),
        "[processor.ijsonlint]\nsrc_dirs = [\"\"]\n",
    ).unwrap();
    fs::write(project_path.join("good.json"), "{\"ok\": true}\n").unwrap();

    // First build creates .rsconstruct/
    let build1 = run_rsconstruct(project_path, &["build"]);
    assert!(build1.status.success(),
        "first build failed: stdout={} stderr={}",
        String::from_utf8_lossy(&build1.stdout),
        String::from_utf8_lossy(&build1.stderr));
    assert!(project_path.join(".rsconstruct").exists());

    // Plant an invalid JSON file inside the state dir. If the index picks it
    // up, ijsonlint discovers it as a product and fails the build.
    fs::write(project_path.join(".rsconstruct/bad.json"), "{not json").unwrap();

    let build2 = run_rsconstruct(project_path, &["build"]);
    let stdout = String::from_utf8_lossy(&build2.stdout);
    let stderr = String::from_utf8_lossy(&build2.stderr);
    assert!(build2.status.success(),
        "build must not lint the tool's own state dir: stdout={stdout} stderr={stderr}");
    assert!(!stdout.contains(".rsconstruct/bad.json") && !stderr.contains(".rsconstruct/bad.json"),
        "state-dir file leaked into the build: stdout={stdout} stderr={stderr}");
}

/// The symlink warning is off by default and on with `[build] warn_symlinks`.
///
/// The walker never follows symlinks, so a symlinked source is absent from
/// the index — never checked, never built. That gap is only reported when
/// the user asks for it; the default is quiet.
#[test]
fn warn_symlinks_knob_controls_the_symlink_warning() {
    let temp_dir = setup_test_project();
    let project_path = temp_dir.path();

    fs::write(project_path.join("good.json"), "{\"ok\": true}\n").unwrap();
    std::os::unix::fs::symlink("good.json", project_path.join("linked.json")).unwrap();

    let quiet_config = "[processor.ijsonlint]\nsrc_dirs = [\"\"]\n";
    let loud_config = "[build]\nwarn_symlinks = true\n\n[processor.ijsonlint]\nsrc_dirs = [\"\"]\n";

    for (config, expect_warning) in [(quiet_config, false), (loud_config, true)] {
        fs::write(project_path.join("rsconstruct.toml"), config).unwrap();
        let build = run_rsconstruct(project_path, &["build"]);
        let stdout = String::from_utf8_lossy(&build.stdout);
        let stderr = String::from_utf8_lossy(&build.stderr);
        let warned = stdout.contains("linked.json") || stderr.contains("linked.json");
        assert_eq!(warned, expect_warning,
            "warn_symlinks={expect_warning} should {} warn about the symlink: stdout={stdout} stderr={stderr}",
            if expect_warning { "" } else { "not" });
    }
}

/// Configured output roots must never be indexed as project source.
///
/// Regression test: the file index used to pick up generated files under the
/// output directory — only the user's `.gitignore` accidentally excluded
/// them, and builds were non-idempotent (build 1 generates a file, build 2
/// lints it). The exclusion must follow the configured name, not a
/// hardcoded "out".
#[test]
fn output_roots_are_not_indexed() {
    let temp_dir = setup_test_project();
    let project_path = temp_dir.path();

    // Renamed global output root plus a per-processor output_dir outside it.
    // No .gitignore exists, so only the file index exclusion protects them.
    fs::write(
        project_path.join("rsconstruct.toml"),
        "[build]\noutput_dir = \"artifacts\"\n\n\
         [processor.tera]\nsrc_dirs = [\"tera.templates\"]\noutput_dir = \"generated\"\n\n\
         [processor.ijsonlint]\nsrc_dirs = [\"\"]\n",
    ).unwrap();
    fs::write(project_path.join("good.json"), "{\"ok\": true}\n").unwrap();
    fs::create_dir_all(project_path.join("artifacts")).unwrap();
    fs::write(project_path.join("artifacts/bad.json"), "{not json").unwrap();
    fs::create_dir_all(project_path.join("generated")).unwrap();
    fs::write(project_path.join("generated/bad.json"), "{not json").unwrap();

    let build = run_rsconstruct(project_path, &["build"]);
    let stdout = String::from_utf8_lossy(&build.stdout);
    let stderr = String::from_utf8_lossy(&build.stderr);
    assert!(build.status.success(),
        "build must not lint files under output roots: stdout={stdout} stderr={stderr}");
    assert!(!stdout.contains("bad.json") && !stderr.contains("bad.json"),
        "output-root file leaked into the build: stdout={stdout} stderr={stderr}");
}

/// Listing a directory under an output root in `src_dirs` is the explicit
/// opt-in to scan generated files there — the exclusion must not apply.
#[test]
fn src_dirs_under_output_root_are_scanned() {
    let temp_dir = setup_test_project();
    let project_path = temp_dir.path();

    fs::write(
        project_path.join("rsconstruct.toml"),
        "[processor.ijsonlint]\nsrc_dirs = [\"out/checkme\"]\n",
    ).unwrap();
    fs::create_dir_all(project_path.join("out/checkme")).unwrap();
    fs::write(project_path.join("out/checkme/bad.json"), "{not json").unwrap();
    // Sibling dir under the same output root, NOT named in src_dirs: excluded.
    fs::create_dir_all(project_path.join("out/other")).unwrap();
    fs::write(project_path.join("out/other/unscanned.json"), "{not json").unwrap();

    let build = run_rsconstruct(project_path, &["build"]);
    let stdout = String::from_utf8_lossy(&build.stdout);
    let stderr = String::from_utf8_lossy(&build.stderr);
    assert!(!build.status.success(),
        "explicitly opted-in dir under the output root must be scanned (and fail on the bad file): stdout={stdout} stderr={stderr}");
    assert!(stdout.contains("out/checkme/bad.json") || stderr.contains("out/checkme/bad.json"),
        "the opted-in file should be the reported failure: stdout={stdout} stderr={stderr}");
    assert!(!stdout.contains("unscanned.json") && !stderr.contains("unscanned.json"),
        "non-opted-in output dir must stay excluded: stdout={stdout} stderr={stderr}");
}

/// Zero values for `[build]` knobs whose zero case silently breaks the
/// build must be rejected at config load, not silently obeyed
/// (`max_discovery_passes = 0` used to skip discovery and report an empty
/// build as success).
#[test]
fn zero_value_build_knobs_are_rejected() {
    let temp_dir = setup_test_project();
    let project_path = temp_dir.path();

    for (toml, needle) in [
        ("[build]\nmax_discovery_passes = 0\n", "max_discovery_passes"),
        ("[build]\nmax_arg_len = 0\n", "max_arg_len"),
    ] {
        fs::write(project_path.join("rsconstruct.toml"), toml).unwrap();
        let out = run_rsconstruct(project_path, &["build"]);
        let stderr = String::from_utf8_lossy(&out.stderr);
        assert!(!out.status.success(),
            "{needle} = 0 must be a config error, not a silent no-op build");
        assert!(stderr.contains(needle),
            "error must name the offending field: {stderr}");
    }
}

/// With mtime checking off, builds over a processor chain must converge.
///
/// Regression test: the in-session checksum cache had no invalidation, so
/// under --no-mtime-cache any input rewritten mid-build by an upstream
/// product kept serving its pre-rewrite checksum for the rest of the run.
/// The downstream product's cache descriptor was then keyed by content that
/// no longer existed: the build after a change either skipped the downstream
/// product entirely (stale output) or rebuilt it again on the next run.
#[test]
fn no_mtime_cache_chain_converges() {
    let temp_dir = setup_test_project();
    let project_path = temp_dir.path();

    // tera renders doc.md in-place at the project root; imarkdown2html
    // consumes it. On an incremental build the classify pass hashes the
    // pre-rewrite doc.md into the in-session cache, then tera rewrites it
    // mid-build.
    fs::write(
        project_path.join("rsconstruct.toml"),
        "[processor.tera]\nsrc_dirs = [\"tera.templates\"]\n\n\
         [processor.imarkdown2html]\nsrc_dirs = [\"\"]\n",
    ).unwrap();
    fs::write(project_path.join("tera.templates/doc.md.tera"), "# one\n").unwrap();

    let b1 = run_rsconstruct(project_path, &["build", "--no-mtime-cache"]);
    assert!(b1.status.success(),
        "first build failed: stderr={}", String::from_utf8_lossy(&b1.stderr));

    // Change the template: doc.md's content changes mid-build in build 2.
    fs::write(project_path.join("tera.templates/doc.md.tera"), "# two\n").unwrap();
    let b2 = run_rsconstruct(project_path, &["build", "--no-mtime-cache"]);
    assert!(b2.status.success(),
        "second build failed: stderr={}", String::from_utf8_lossy(&b2.stderr));

    // Build 3: nothing changed — the whole chain must be cached.
    let b3 = run_rsconstruct_with_env(
        project_path, &["build", "--no-mtime-cache", "-v"], &[("NO_COLOR", "1")]);
    assert!(b3.status.success(),
        "third build failed: stderr={}", String::from_utf8_lossy(&b3.stderr));
    let stdout3 = String::from_utf8_lossy(&b3.stdout);
    assert!(!stdout3.contains("Processing:"),
        "an unchanged project must be fully cached under --no-mtime-cache: {stdout3}");
}

/// A `dep_auto` entry the user listed must exist.
///
/// Every `dep_auto` entry used to be skipped silently when absent, so a
/// typo — or a shared config listing files only some repos carry — left a
/// dependency that tracked nothing and nobody noticed. Now a listed entry
/// that is missing is a config error naming the processor, the file and the
/// config line; entries that exist are not reported; `[build]
/// allow_missing_dep_auto = true` restores the old skip.
#[test]
fn user_listed_dep_auto_must_exist() {
    let temp_dir = setup_test_project();
    let project_path = temp_dir.path();
    fs::write(project_path.join("config/present.py"), "x = 1\n").unwrap();
    fs::write(project_path.join("tera.templates/t.txt.tera"), "hello\n").unwrap();

    let strict = "[processor.tera]\nsrc_dirs = [\"tera.templates\"]\n\
                  dep_auto = [\"config/present.py\", \"config/missing.py\"]\n";
    fs::write(project_path.join("rsconstruct.toml"), strict).unwrap();
    let out = run_rsconstruct(project_path, &["build"]);
    let stderr = String::from_utf8_lossy(&out.stderr);
    assert!(!out.status.success(),
        "a listed dep_auto file that does not exist must fail the build: {stderr}");
    for needle in ["processor.tera", "config/missing.py", "rsconstruct.toml:3", "allow_missing_dep_auto"] {
        assert!(stderr.contains(needle), "error must mention {needle}: {stderr}");
    }
    assert!(!stderr.contains("config/present.py"),
        "the entry that exists must not be reported: {stderr}");

    let lenient = format!("[build]\nallow_missing_dep_auto = true\n\n{strict}");
    fs::write(project_path.join("rsconstruct.toml"), lenient).unwrap();
    let out = run_rsconstruct(project_path, &["build"]);
    assert!(out.status.success(),
        "allow_missing_dep_auto must restore the skip: stderr={}", String::from_utf8_lossy(&out.stderr));
    assert!(project_path.join("t.txt").exists(), "the lenient build must still render the template");
}

/// A processor's default `dep_auto` list stays skip-if-absent.
///
/// The defaults name the well-known config files a tool honours when
/// present (`.yamllint`, `.pylintrc`, ...); a project without them is the
/// normal case, not a misconfiguration, so the existence check applies only
/// to lists the user wrote.
#[test]
fn default_dep_auto_stays_optional() {
    let temp_dir = setup_test_project();
    let project_path = temp_dir.path();
    fs::write(project_path.join("ok.yaml"), "key: value\n").unwrap();
    fs::write(project_path.join("rsconstruct.toml"), "[processor.yamllint]\nsrc_dirs = [\"\"]\n").unwrap();
    assert!(!project_path.join(".yamllint").exists());

    let out = run_rsconstruct(project_path, &["build"]);
    assert!(out.status.success(),
        "an absent default dep_auto file must not fail the build: stderr={}", String::from_utf8_lossy(&out.stderr));
}