onetaskgraph 0.2.13

One interface over the ticketing systems your work lives in.
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
//! The configuration journeys, driven the way a user drives them.
//!
//! Journeys 16 (precedence and the verb that names the layer), 17 (a named source's
//! own field at each of the three layers) and 18 (the credentials file supplying and
//! deferring) of `AGENTS.md`, plus the part of 21 the machine-readable configuration
//! output owes and the refusals 22 owes for a malformed configuration. Every one of
//! them spawns the compiled binary as a subprocess against real files in a temporary
//! directory and a real process environment, and asserts on the exit code and on what
//! the binary wrote. Nothing here stands in for the filesystem or for the
//! environment: those *are* the layer under test.

mod common;

use std::process::Output;

use common::{SOURCE_BOUNDARIES, Sandbox, SourceBoundary, one_source, stderr, stdout};
use serde_json::Value;

/// The whole `config show --json` document.
fn shown(output: &Output) -> Value {
    serde_json::from_str(&stdout(output)).expect("`config show --json` emits one JSON document")
}

/// One setting from a `config show --json` document.
fn setting<'a>(shown: &'a Value, key: &str) -> &'a Value {
    shown["settings"]
        .as_array()
        .expect("settings is a list")
        .iter()
        .find(|setting| setting["key"] == key)
        .unwrap_or_else(|| panic!("{key} is reported; the document was {shown:#}"))
}

#[test]
fn the_file_layer_alone_sets_a_setting_and_the_verb_names_the_file() {
    let sandbox = Sandbox::new();
    let document = sandbox.project_document("page_size: 25\n");

    let output = sandbox
        .command()
        .args(["config", "show", "--json"])
        .assert()
        .success()
        .get_output()
        .clone();

    let page_size = setting(&shown(&output), "page_size").clone();
    assert_eq!(page_size["value"], 25);
    assert_eq!(page_size["origin"]["layer"], "file");
    assert_eq!(
        page_size["origin"]["path"],
        document.to_string_lossy().to_string()
    );
}

#[test]
fn the_environment_layer_alone_sets_a_setting_and_the_verb_names_the_variable() {
    let sandbox = Sandbox::new();

    let output = sandbox
        .command()
        .env("ONETASKGRAPH_PAGE_SIZE", "70")
        .args(["config", "show", "--json"])
        .assert()
        .success()
        .get_output()
        .clone();

    let page_size = setting(&shown(&output), "page_size").clone();
    assert_eq!(page_size["value"], 70);
    assert_eq!(page_size["origin"]["layer"], "environment");
    assert_eq!(page_size["origin"]["variable"], "ONETASKGRAPH_PAGE_SIZE");
}

#[test]
fn the_flag_layer_alone_sets_a_setting_and_the_verb_names_the_flag() {
    let sandbox = Sandbox::new();

    let output = sandbox
        .command()
        .args(["config", "show", "--json", "--page-size", "9"])
        .assert()
        .success()
        .get_output()
        .clone();

    let page_size = setting(&shown(&output), "page_size").clone();
    assert_eq!(page_size["value"], 9);
    assert_eq!(page_size["origin"]["layer"], "flag");
    assert_eq!(page_size["origin"]["flag"], "--page-size");
}

#[test]
fn the_environment_beats_the_file() {
    let sandbox = Sandbox::new();
    sandbox.project_document("page_size: 25\n");

    let output = sandbox
        .command()
        .env("ONETASKGRAPH_PAGE_SIZE", "70")
        .args(["config", "show", "--json"])
        .assert()
        .success()
        .get_output()
        .clone();

    let page_size = setting(&shown(&output), "page_size").clone();
    assert_eq!(page_size["value"], 70);
    assert_eq!(page_size["origin"]["variable"], "ONETASKGRAPH_PAGE_SIZE");
}

#[test]
fn a_flag_beats_the_environment() {
    let sandbox = Sandbox::new();

    let output = sandbox
        .command()
        .env("ONETASKGRAPH_PAGE_SIZE", "70")
        .args(["config", "show", "--json", "--page-size", "9"])
        .assert()
        .success()
        .get_output()
        .clone();

    let page_size = setting(&shown(&output), "page_size").clone();
    assert_eq!(page_size["value"], 9);
    assert_eq!(page_size["origin"]["flag"], "--page-size");
}

#[test]
fn a_flag_beats_the_file() {
    let sandbox = Sandbox::new();
    sandbox.project_document("page_size: 25\n");

    let output = sandbox
        .command()
        .args(["config", "show", "--json", "--page-size", "9"])
        .assert()
        .success()
        .get_output()
        .clone();

    let page_size = setting(&shown(&output), "page_size").clone();
    assert_eq!(page_size["value"], 9);
    assert_eq!(page_size["origin"]["flag"], "--page-size");
}

#[test]
fn all_three_layers_at_once_leave_the_flag_on_top_and_each_other_setting_with_its_own_layer() {
    for boundary in SOURCE_BOUNDARIES {
        let sandbox = Sandbox::new();
        let document = sandbox.project_document(
            &serde_json::to_string(&serde_json::json!({
                "page_size": 25,
                "sources": {
                    "work": boundary.source("in-memory", serde_json::json!({})),
                    "notes": boundary.source("in-memory", serde_json::json!({})),
                }
            }))
            .expect("a document"),
        );

        let output = sandbox
            .command()
            .env("ONETASKGRAPH_PAGE_SIZE", "70")
            .env("ONETASKGRAPH_DEFAULT_SOURCES", "work,notes")
            .args(["config", "show", "--json", "--page-size", "9"])
            .assert()
            .success()
            .get_output()
            .clone();

        let shown = shown(&output);
        assert_eq!(setting(&shown, "page_size")["value"], 9);
        assert_eq!(
            setting(&shown, "page_size")["origin"]["flag"],
            "--page-size"
        );
        assert_eq!(
            setting(&shown, "default_sources")["origin"]["variable"],
            "ONETASKGRAPH_DEFAULT_SOURCES",
            "the layer that set a setting is the layer reported for it, per setting"
        );
        assert_eq!(
            setting(
                &shown,
                match boundary {
                    SourceBoundary::Direct => "sources.work.plugin",
                    SourceBoundary::Subprocess => "sources.work.config.settings.kind",
                },
            )["origin"]["path"],
            document.to_string_lossy().to_string(),
            "a setting no higher layer touched still comes from the document that set it"
        );
    }
}

#[test]
fn the_project_document_layers_over_the_user_level_one() {
    let sandbox = Sandbox::new();
    for boundary in SOURCE_BOUNDARIES {
        let user = sandbox.user_document(
            &serde_json::to_string(&serde_json::json!({
                "page_size": 11,
                "sources": {"work": boundary.source("in-memory", serde_json::json!({}))}
            }))
            .expect("a user document"),
        );
        let project = sandbox.project_document("page_size: 25\n");

        let output = sandbox
            .command()
            .args(["config", "show", "--json"])
            .assert()
            .success()
            .get_output()
            .clone();

        let shown = shown(&output);
        assert_eq!(setting(&shown, "page_size")["value"], 25);
        assert_eq!(
            setting(&shown, "page_size")["origin"]["path"],
            project.to_string_lossy().to_string()
        );
        assert_eq!(
            setting(
                &shown,
                match boundary {
                    SourceBoundary::Direct => "sources.work.plugin",
                    SourceBoundary::Subprocess => "sources.work.config.settings.kind",
                },
            )["origin"]["path"],
            user.to_string_lossy().to_string(),
            "a source the project document does not mention still comes from the user's"
        );
    }
}

#[test]
fn the_document_is_discovered_upward_from_the_working_directory() {
    let sandbox = Sandbox::new();
    let document = sandbox.project_document("page_size: 25\n");
    let deep = sandbox.subdirectory("crates/onetaskgraph/src");

    let output = sandbox
        .command_in(&deep)
        .args(["config", "show", "--json"])
        .assert()
        .success()
        .get_output()
        .clone();

    assert_eq!(
        setting(&shown(&output), "page_size")["origin"]["path"],
        document.to_string_lossy().to_string()
    );
}

#[test]
fn a_setting_nothing_sets_is_still_reported_with_the_value_the_run_will_use() {
    let sandbox = Sandbox::new();

    let output = sandbox
        .command()
        .args(["config", "show", "--json"])
        .assert()
        .success()
        .get_output()
        .clone();

    let page_size = setting(&shown(&output), "page_size").clone();
    assert_eq!(page_size["origin"]["layer"], "default");
    assert_eq!(page_size["value"], 50);
}

#[test]
fn the_text_rendering_names_the_layer_beside_every_setting() {
    let sandbox = Sandbox::new();
    sandbox.project_document("page_size: 25\n");

    let output = sandbox
        .command()
        .env("ONETASKGRAPH_OUTPUT", "text")
        .args(["config", "show"])
        .assert()
        .success()
        .get_output()
        .clone();

    let rendered = stdout(&output);
    assert!(
        rendered.contains("page_size") && rendered.contains("onetaskgraph.yaml"),
        "the file that set page_size is named: {rendered}"
    );
    assert!(
        rendered.contains("environment ONETASKGRAPH_OUTPUT"),
        "the variable that set output is named: {rendered}"
    );
    assert!(
        rendered.contains("default"),
        "a setting nothing set is named as a default: {rendered}"
    );
}

#[test]
fn the_named_flags_reach_the_settings_they_are_shorthand_for() {
    for boundary in SOURCE_BOUNDARIES {
        let sandbox = Sandbox::new();
        sandbox.project_document(
            &serde_json::to_string(&serde_json::json!({
                "sources": {
                    "work": boundary.source("in-memory", serde_json::json!({})),
                    "notes": boundary.source("in-memory", serde_json::json!({})),
                }
            }))
            .expect("a document"),
        );

        let output = sandbox
            .command()
            .args([
                "config",
                "show",
                "--output",
                "json",
                "--default-sources",
                "notes,work",
            ])
            .assert()
            .success()
            .get_output()
            .clone();

        let shown = shown(&output);
        assert_eq!(
            setting(&shown, "output")["origin"]["flag"],
            "--output",
            "--output json renders JSON and says the flag it came from"
        );
        let selected = setting(&shown, "default_sources").clone();
        assert_eq!(selected["value"], serde_json::json!(["notes", "work"]));
        assert_eq!(selected["origin"]["flag"], "--default-sources");
    }
}

#[test]
fn an_output_format_this_build_does_not_have_is_refused_with_the_ones_it_does() {
    let sandbox = Sandbox::new();

    let output = sandbox
        .command()
        .args(["config", "show", "--output", "yaml"])
        .assert()
        .failure()
        .get_output()
        .clone();

    // Clap's own code for a malformed invocation: `--output` takes one of a closed set,
    // so this never reaches the configuration layer at all.
    assert_eq!(output.status.code(), Some(2));
    let message = stderr(&output);
    assert!(
        message.contains("yaml"),
        "the refusal quotes what was asked for: {message}"
    );
    assert!(
        message.contains("text") && message.contains("json"),
        "the refusal lists the formats this build has: {message}"
    );
}

#[test]
fn a_spelled_out_set_beats_the_json_shorthand_within_the_flag_layer() {
    let sandbox = Sandbox::new();

    let output = sandbox
        .command()
        .args(["config", "show", "--json", "--set", "output=text"])
        .assert()
        .success()
        .get_output()
        .clone();

    let rendered = stdout(&output);
    assert!(
        rendered.contains("output") && !rendered.starts_with('{'),
        "the spelled-out setting wins and the table is rendered: {rendered}"
    );
    assert!(
        rendered.contains("flag --set output"),
        "the layer named is the flag that actually set it: {rendered}"
    );
}

#[test]
fn asking_for_a_format_and_the_json_shorthand_at_once_is_refused_as_a_bad_invocation() {
    let sandbox = Sandbox::new();

    let output = sandbox
        .command()
        .args(["config", "show", "--output", "text", "--json"])
        .assert()
        .failure()
        .get_output()
        .clone();

    // Clap's own code for a malformed invocation, distinct from the `1` a command
    // that ran and failed exits with — a caller can tell the two apart.
    assert_eq!(output.status.code(), Some(2));
    let message = stderr(&output);
    assert!(
        message.contains("--output") && message.contains("--json"),
        "the refusal names both flags: {message}"
    );
    assert!(
        stdout(&output).is_empty(),
        "a refused invocation writes nothing to stdout"
    );
}

fn source_document(boundary: SourceBoundary, max_page_size: u32) -> String {
    serde_json::to_string(&serde_json::json!({
        "sources": {"work": boundary.source("in-memory", serde_json::json!({
            "capabilities": {"max_page_size": max_page_size}
        }))}
    }))
    .expect("a source document")
}

#[test]
fn a_malformed_configuration_flag_is_refused_on_a_verb_that_does_not_use_it() {
    let sandbox = Sandbox::new();

    // `schema` renders a static bundle, but the flags are global and parse on it, so a
    // flag written there has to be refused rather than accepted and dropped.
    let message = mistyped(&sandbox, &["schema", "--set", "page_size"]);
    assert!(
        message.contains("--set page_size: that is not an assignment"),
        "the refusal names the flag: {message}"
    );
}

#[test]
fn a_page_size_of_zero_is_refused_wherever_it_is_written() {
    for boundary in SOURCE_BOUNDARIES {
        let sandbox = Sandbox::new();
        sandbox.project_document(&one_source(boundary));

        for verb in [["schema"], ["config"]] {
            let mut command = sandbox.command();
            command.args(verb);
            if verb == ["config"] {
                command.arg("show");
            }
            let output = command
                .args(["--page-size", "0"])
                .assert()
                .failure()
                .get_output()
                .clone();

            assert!(
                stderr(&output).contains("--page-size"),
                "`onetaskgraph {} --page-size 0` names the flag it refused: {}",
                verb[0],
                stderr(&output)
            );
        }
    }
}

#[test]
fn a_well_formed_configuration_flag_leaves_the_bundle_this_verb_emits_alone() {
    let sandbox = Sandbox::new();

    let output = sandbox
        .command()
        .args(["schema", "--set", "page_size=10", "--page-size", "7"])
        .assert()
        .success()
        .get_output()
        .clone();

    let bundle: Value =
        serde_json::from_str(&stdout(&output)).expect("the schema bundle is still JSON");
    assert!(
        bundle["roots"]["EffectiveConfig"].is_object(),
        "a flag this verb does not read does not change what it emits"
    );
}

/// Every layer is validated on every verb, including the verbs that do not use it.
///
/// `schema` renders a static bundle, so nothing it emits depends on the configuration —
/// which is exactly why a setting written for it could be accepted and dropped without
/// anything looking wrong. A setting that addresses nothing this build has is a mistake
/// wherever it was written, so it is refused here as it is on `config show`: at the
/// flag layer a user typed on this invocation, and at the environment layer around it.
#[test]
fn a_setting_that_addresses_nothing_is_refused_on_a_verb_that_does_not_use_it() {
    let sandbox = Sandbox::new();

    let from_flag = sandbox
        .command()
        .args(["schema", "--set", "unknown=value"])
        .assert()
        .failure()
        .get_output()
        .clone();
    assert!(
        stdout(&from_flag).is_empty(),
        "a refusal emits no bundle for a generator to consume"
    );
    assert!(
        stderr(&from_flag).contains("unknown"),
        "the refusal names the setting: {}",
        stderr(&from_flag)
    );

    let from_environment = sandbox
        .command()
        .env("ONETASKGRAPH_UNKNOWN", "value")
        .args(["schema"])
        .assert()
        .failure()
        .get_output()
        .clone();
    assert!(
        stderr(&from_environment).contains("unknown"),
        "the refusal names the setting: {}",
        stderr(&from_environment)
    );
}

/// The other three refusals of the same clause, on that same verb.
///
/// An unknown field is one of four ways a setting can be wrong; a source name that
/// breaks the pattern and a plugin this build does not have are two more, and each is
/// found by a different check. Proving only the first would leave the other two free to
/// pass a verb that does not use the configuration.
#[test]
fn a_source_this_build_cannot_build_is_refused_on_a_verb_that_does_not_use_it() {
    let sandbox = Sandbox::new();

    for (assignment, named) in [
        ("sources.WORK.plugin=in-memory", "sources.WORK"),
        ("sources.work.plugin=nope", "sources.work.plugin"),
    ] {
        let output = sandbox
            .command()
            .args(["schema", "--set", assignment])
            .assert()
            .failure()
            .get_output()
            .clone();
        assert!(
            stderr(&output).contains(named),
            "`--set {assignment}` is refused naming {named}: {}",
            stderr(&output)
        );
    }
}

#[test]
fn a_named_sources_own_field_is_set_by_the_file() {
    for boundary in SOURCE_BOUNDARIES {
        let sandbox = Sandbox::new();
        let document = sandbox.project_document(&source_document(boundary, 20));

        let output = sandbox
            .command()
            .args(["config", "show", "--json"])
            .assert()
            .success()
            .get_output()
            .clone();

        let field = setting(
            &shown(&output),
            &boundary.config_path("capabilities.max_page_size"),
        )
        .clone();
        assert_eq!(field["value"], 20);
        assert_eq!(
            field["origin"]["path"],
            document.to_string_lossy().to_string()
        );
    }
}

#[test]
fn a_named_sources_own_field_is_set_by_the_environment_over_the_file() {
    for boundary in SOURCE_BOUNDARIES {
        let sandbox = Sandbox::new();
        sandbox.project_document(&source_document(boundary, 20));

        let variable = boundary.config_variable("capabilities.max_page_size");

        let output = sandbox
            .command()
            .env(&variable, "7")
            .args(["config", "show", "--json"])
            .assert()
            .success()
            .get_output()
            .clone();

        let field = setting(
            &shown(&output),
            &boundary.config_path("capabilities.max_page_size"),
        )
        .clone();
        assert_eq!(field["value"], 7);
        assert_eq!(field["origin"]["variable"], variable);
    }
}

#[test]
fn a_named_sources_own_field_is_set_by_a_flag_over_the_environment_and_the_file() {
    for boundary in SOURCE_BOUNDARIES {
        let sandbox = Sandbox::new();
        sandbox.project_document(&source_document(boundary, 20));

        let variable = boundary.config_variable("capabilities.max_page_size");
        let field_path = boundary.config_path("capabilities.max_page_size");
        let assignment = format!("{field_path}=3");

        let output = sandbox
            .command()
            .env(&variable, "7")
            .args(["config", "show", "--json", "--set", &assignment])
            .assert()
            .success()
            .get_output()
            .clone();

        let field = setting(&shown(&output), &field_path).clone();
        assert_eq!(field["value"], 3);
        assert_eq!(field["origin"]["flag"], format!("--set {field_path}"));
    }
}

#[test]
fn a_whole_named_source_is_configured_from_the_environment_alone() {
    let sandbox = Sandbox::new();

    let output = sandbox
        .command()
        .env("ONETASKGRAPH_SOURCES__GH_MAIN__PLUGIN", "github-projects")
        .args(["config", "show", "--json"])
        .assert()
        .success()
        .get_output()
        .clone();

    let plugin = setting(&shown(&output), "sources.gh-main.plugin").clone();
    assert_eq!(
        plugin["value"], "github-projects",
        "an underscore in the variable is a hyphen in the source name"
    );
}

#[test]
fn each_worked_environment_example_sets_the_setting_it_claims_to_set() {
    for boundary in SOURCE_BOUNDARIES {
        let sandbox = Sandbox::new();
        sandbox.project_document(&one_source(boundary));
        let variable = boundary.config_variable("capabilities.max_page_size");

        let output = sandbox
            .command()
            .env("ONETASKGRAPH_PAGE_SIZE", "100")
            .env("ONETASKGRAPH_DEFAULT_SOURCES", "work,gh-main")
            .env(&variable, "42")
            .env("ONETASKGRAPH_SOURCES__GH_MAIN__PLUGIN", "github-projects")
            .args(["config", "show", "--json"])
            .assert()
            .success()
            .get_output()
            .clone();

        let shown = shown(&output);
        assert_eq!(setting(&shown, "page_size")["value"], 100);
        assert_eq!(
            setting(&shown, "default_sources")["value"],
            serde_json::json!(["work", "gh-main"]),
            "a list is comma-separated"
        );
        assert_eq!(
            setting(&shown, &boundary.config_path("capabilities.max_page_size"))["value"],
            42
        );
        assert_eq!(
            setting(&shown, "sources.gh-main.plugin")["value"],
            "github-projects"
        );
    }
}

/// The worked example above is written against a `root`, which belongs to `local-md` —
/// a plugin whose configuration block is still empty, so nothing here can accept that
/// field yet. What the example claims is not that `root` is valid but *where the variable
/// lands*, so that is what this asserts: the value reaches `sources.work.config.root` and
/// is refused under that name, rather than being dropped or landing on another setting.
#[test]
fn the_worked_example_for_a_named_sources_own_field_reaches_that_field() {
    // Subprocess settings are intentionally opaque to `config show`, so the protocol cannot repeat an inner-schema refusal.
    let boundary = SourceBoundary::Direct;
    let sandbox = Sandbox::new();
    sandbox.project_document(&one_source(boundary));
    let variable = boundary.config_variable("root");

    let output = sandbox
        .command()
        .env(&variable, "/tmp/tasks")
        .args(["config", "show"])
        .assert()
        .failure()
        .get_output()
        .clone();

    assert!(
        stdout(&output).is_empty(),
        "a refusal writes nothing to stdout"
    );
    let message = stderr(&output);
    assert!(
        message.contains(&boundary.config_path("root")),
        "the variable reaches the field it names: {message}"
    );
}

/// A value distinctive enough that finding it anywhere is unambiguous.
const PLANTED: &str = "lin_api_PLANTED-CANARY-8f21c0";

#[test]
fn the_credentials_file_supplies_a_variable_the_process_environment_does_not_define() {
    let sandbox = Sandbox::new();
    sandbox.secrets_file(&format!("# credentials\nLINEAR_API_KEY={PLANTED}\n"));

    let output = sandbox
        .command()
        .env_remove("LINEAR_API_KEY")
        .args(["config", "show", "--json"])
        .assert()
        .success()
        .get_output()
        .clone();

    let shown = shown(&output);
    assert_eq!(
        shown["secrets"]["variables"],
        serde_json::json!([
            { "variable": "LINEAR_API_KEY", "resolved_from": "secrets-file" }
        ])
    );
}

#[test]
fn the_credentials_file_defers_to_a_variable_the_process_environment_already_defines() {
    let sandbox = Sandbox::new();
    sandbox.secrets_file(&format!("LINEAR_API_KEY={PLANTED}\n"));

    let output = sandbox
        .command()
        .env("LINEAR_API_KEY", "exported-by-the-shell")
        .args(["config", "show", "--json"])
        .assert()
        .success()
        .get_output()
        .clone();

    let shown = shown(&output);
    assert_eq!(
        shown["secrets"]["variables"][0]["resolved_from"], "environment",
        "an explicitly exported variable wins: {shown:#}"
    );
}

/// A one-source document whose source is a program of its own, naming one credential.
///
/// The hosted plugin is `in-memory` over a single task, because what this journey is about
/// is the credential reaching a spawned child rather than the work behind it.
fn subprocess_document(secrets: &[&str]) -> String {
    serde_json::to_string(&serde_json::json!({
        "sources": {
            "work": {
                "plugin": "subprocess",
                "config": {
                    "command": env!("CARGO_BIN_EXE_onetaskgraph-source"),
                    "secrets": secrets,
                    "settings": {
                        "kind": "in-memory",
                        "config": {
                            "tasks": [{
                                "id": "T-1",
                                "title": "Alpha",
                                "status": {"category": "todo", "name": "Todo"},
                                "labels": []
                            }]
                        }
                    }
                }
            }
        }
    }))
    .expect("a document")
}

#[test]
fn the_credentials_file_supplies_a_variable_a_source_in_another_process_names() {
    let sandbox = Sandbox::new();
    sandbox.secrets_file(&format!("LINEAR_API_KEY={PLANTED}\n"));
    sandbox.project_document(&subprocess_document(&["LINEAR_API_KEY"]));

    let output = sandbox
        .command()
        .env_remove("LINEAR_API_KEY")
        .args(["task", "list"])
        .assert()
        .success()
        .get_output()
        .clone();

    // The run reached the spawned plugin and got its rows, which it could only do with the
    // credential resolved and forwarded: the source refuses to build without it, as the
    // next journey asserts.
    let listed = String::from_utf8(output.stdout).expect("UTF-8");
    assert!(listed.contains("work:T-1"), "{listed}");
    assert!(
        !listed.contains(PLANTED),
        "a credential is never printed: {listed}"
    );
}

#[test]
fn a_credential_a_source_in_another_process_names_and_nothing_defines_is_refused_by_name() {
    let sandbox = Sandbox::new();
    sandbox.project_document(&subprocess_document(&["LINEAR_API_KEY"]));

    let output = sandbox
        .command()
        .env_remove("LINEAR_API_KEY")
        .args(["task", "list"])
        .assert()
        .failure()
        .get_output()
        .clone();

    let complaint = String::from_utf8(output.stderr).expect("UTF-8");
    assert!(complaint.contains("LINEAR_API_KEY"), "{complaint}");
}

#[test]
fn a_variable_name_a_source_in_another_process_could_never_resolve_is_refused_at_the_field() {
    let sandbox = Sandbox::new();
    sandbox.project_document(&subprocess_document(&["not a variable name"]));

    let output = sandbox
        .command()
        .args(["task", "list"])
        .assert()
        .failure()
        .get_output()
        .clone();

    let complaint = String::from_utf8(output.stderr).expect("UTF-8");
    assert!(
        complaint.contains("secrets") || complaint.contains("not a variable name"),
        "the field that holds the mistake is named: {complaint}"
    );
}

#[test]
fn a_missing_credentials_file_is_not_an_error() {
    let sandbox = Sandbox::new();

    let output = sandbox
        .command()
        .args(["config", "show", "--json"])
        .assert()
        .success()
        .get_output()
        .clone();

    assert_eq!(
        shown(&output)["secrets"]["variables"],
        serde_json::json!([])
    );
}

#[test]
fn the_override_variable_moves_the_credentials_file() {
    let sandbox = Sandbox::new();
    sandbox.secrets_file("LINEAR_API_KEY=from-the-default-path\n");
    let elsewhere = sandbox.project().join("elsewhere.env");
    std::fs::write(&elsewhere, format!("GH_PROJECTS_TOKEN={PLANTED}\n")).expect("written");

    let output = sandbox
        .command()
        .env("ONETASKGRAPH_SECRETS_FILE", &elsewhere)
        .env_remove("GH_PROJECTS_TOKEN")
        .args(["config", "show", "--json"])
        .assert()
        .success()
        .get_output()
        .clone();

    let shown = shown(&output);
    assert_eq!(
        shown["secrets"]["path"],
        elsewhere.to_string_lossy().to_string()
    );
    assert_eq!(
        shown["secrets"]["variables"],
        serde_json::json!([
            { "variable": "GH_PROJECTS_TOKEN", "resolved_from": "secrets-file" }
        ]),
        "the override replaces the default path rather than adding to it"
    );
}

#[test]
fn the_override_variable_is_not_mistaken_for_a_setting() {
    let sandbox = Sandbox::new();
    let elsewhere = sandbox.project().join("elsewhere.env");
    std::fs::write(&elsewhere, "").expect("written");

    let output = sandbox
        .command()
        .env("ONETASKGRAPH_SECRETS_FILE", &elsewhere)
        .args(["config", "show", "--json"])
        .assert()
        .success()
        .get_output()
        .clone();

    assert!(
        !stdout(&output).contains("secrets_file\""),
        "ONETASKGRAPH_SECRETS_FILE is the credentials path, not a setting called \
         `secrets_file`"
    );
}

#[test]
fn a_planted_credential_reaches_no_output_of_any_verb() {
    for boundary in SOURCE_BOUNDARIES {
        let sandbox = Sandbox::new();
        sandbox.project_document(&one_source(boundary));
        sandbox.secrets_file(&format!(
            "LINEAR_API_KEY={PLANTED}\nGH_PROJECTS_TOKEN={PLANTED}\n"
        ));

        // Every verb this binary answers, and a failing invocation, because an error path
        // that renders more state than a success path is exactly how one of these leaks.
        let invocations: Vec<Vec<&str>> = vec![
            vec!["--help"],
            vec!["--version"],
            vec!["schema"],
            vec!["config", "show"],
            vec!["config", "show", "--json"],
            vec!["config", "show", "--set", "sources.work.plugin=nope"],
            vec!["config", "show", "--set", "sources.work.config.taks=1"],
        ];

        for arguments in invocations {
            let output = sandbox
                .command()
                .args(&arguments)
                .output()
                .expect("the binary runs");
            for (stream, text) in [("stdout", stdout(&output)), ("stderr", stderr(&output))] {
                assert!(
                    !text.contains(PLANTED),
                    "`onetaskgraph {}` put a credential on {stream}",
                    arguments.join(" ")
                );
            }
        }
    }
}

/// Expect a refusal over `sandbox` from **every** verb, and return what it said.
///
/// Both verbs, because a configuration that cannot be loaded is a refusal wherever it
/// was written: `config show` reads the configuration and `schema` renders a static
/// bundle without it, so `schema` is the one that would answer anyway if a layer were
/// only validated where it is used. Every refusal below is configuration-shaped rather
/// than verb-shaped, so checking both here holds that for all of them at once — the
/// malformed document, the configuration and credentials files that cannot be read,
/// and each setting a layer got wrong.
fn refusal(sandbox: &Sandbox, arguments: &[&str]) -> String {
    let mut messages = Vec::new();
    for verb in [&["config", "show"][..], &["schema"][..]] {
        let output = sandbox
            .command()
            .args(verb)
            .args(arguments)
            .assert()
            .failure()
            .get_output()
            .clone();
        assert!(
            stdout(&output).is_empty(),
            "`onetaskgraph {}` wrote to stdout while refusing",
            verb.join(" ")
        );
        let message = stderr(&output);
        assert!(
            message.contains("next:"),
            "`onetaskgraph {}` suggests a next action: {message}",
            verb.join(" ")
        );
        messages.push(message);
    }
    let [from_config_show, from_schema] = messages
        .try_into()
        .expect("one message from each of the two verbs");
    assert_eq!(
        from_config_show, from_schema,
        "both verbs refuse the same configuration with the same problem"
    );
    from_config_show
}

#[test]
fn an_unknown_field_is_refused_by_name() {
    let sandbox = Sandbox::new();
    sandbox.project_document("page_sise: 25\n");

    let message = refusal(&sandbox, &[]);
    assert!(message.contains("page_sise"), "{message}");
}

#[test]
fn a_bad_value_is_refused_by_name() {
    let sandbox = Sandbox::new();
    sandbox.project_document("page_size: nowhere-near-a-number\n");

    let message = refusal(&sandbox, &[]);
    assert!(message.contains("page_size"), "{message}");
}

#[test]
fn an_unknown_plugin_name_is_refused_by_name_and_the_known_ones_are_listed() {
    let sandbox = Sandbox::new();
    sandbox.project_document("sources:\n  work:\n    plugin: jira\n");

    let message = refusal(&sandbox, &[]);
    assert!(message.contains("sources.work.plugin"), "{message}");
    assert!(message.contains("in-memory"), "{message}");
    assert!(
        message.contains("linear") && message.contains("github-projects"),
        "a plugin whose source is not written yet is still a name this build knows: \
         {message}"
    );
}

#[test]
fn a_source_name_that_breaks_the_pattern_is_refused_by_name() {
    // Invalid source names are refused before source construction, so no protocol capability can repeat this case.
    let sandbox = Sandbox::new();
    sandbox.project_document("sources:\n  Work_1:\n    plugin: in-memory\n");

    let message = refusal(&sandbox, &[]);
    assert!(message.contains("sources.Work_1"), "{message}");
    assert!(message.contains("^[a-z0-9][a-z0-9-]*$"), "{message}");
}

#[test]
fn a_mistyped_field_of_a_named_source_is_refused_at_the_boundary_rather_than_ignored() {
    // Subprocess settings are intentionally opaque until host startup, so no config-show protocol capability can repeat this refusal.
    let boundary = SourceBoundary::Direct;
    let sandbox = Sandbox::new();
    sandbox.project_document(
        &serde_json::to_string(&serde_json::json!({
            "sources": {"work": boundary.source("in-memory", serde_json::json!({"taks": []}))}
        }))
        .expect("a document"),
    );

    let message = refusal(&sandbox, &[]);
    assert!(
        message.contains(&boundary.config_path("taks")),
        "the offending field is named, not just the block it sits in: {message}"
    );
    assert!(message.contains("in-memory"), "{message}");
}

#[test]
fn a_default_source_naming_nothing_configured_is_refused() {
    let sandbox = Sandbox::new();
    sandbox.project_document("default_sources: [nope]\n");

    let message = refusal(&sandbox, &[]);
    assert!(message.contains("default_sources"), "{message}");
    assert!(message.contains("nope"), "{message}");
}

#[test]
fn a_malformed_document_is_refused_with_the_position_of_the_problem() {
    let sandbox = Sandbox::new();
    let document = sandbox.project_document("page_size: [\n");

    let message = refusal(&sandbox, &[]);
    assert!(
        message.contains(&document.to_string_lossy().to_string()),
        "{message}"
    );
    assert!(message.contains("line"), "{message}");
}

#[test]
fn a_document_that_is_not_a_mapping_is_refused_rather_than_read_as_unset() {
    let sandbox = Sandbox::new();
    sandbox.project_document("- page_size: 25\n");

    let message = refusal(&sandbox, &[]);
    assert!(message.contains("mapping"), "{message}");
}

#[test]
fn a_set_argument_that_is_not_an_assignment_is_refused_as_a_bad_invocation() {
    let sandbox = Sandbox::new();

    let message = mistyped(&sandbox, &["config", "show", "--set", "page_size"]);
    assert!(message.contains("--set page_size"), "{message}");
}

#[test]
fn a_set_argument_addressing_no_setting_is_refused_as_a_bad_invocation() {
    let sandbox = Sandbox::new();

    let message = mistyped(
        &sandbox,
        &["config", "show", "--set", "sources..plugin=in-memory"],
    );
    assert!(message.contains("--set"), "{message}");
}

/// Run `arguments` over `sandbox`, expect the exit code for a mistyped invocation, and
/// return what it said.
///
/// `2` is what the command line documents for an invocation that was typed wrongly, and
/// what clap itself exits with for an unknown flag. A caller branching on the code cannot
/// be asked to know which of the two spotted the mistake, so a `--set` that is not
/// `PATH=VALUE` exits the same way an unknown flag does rather than as a run that broke.
fn mistyped(sandbox: &Sandbox, arguments: &[&str]) -> String {
    let output = sandbox
        .command()
        .args(arguments)
        .assert()
        .code(2)
        .get_output()
        .clone();
    assert!(
        stdout(&output).is_empty(),
        "a refusal writes nothing to stdout"
    );
    stderr(&output)
}

/// Unix only: this drives a shell into removing its own working directory before it
/// execs the binary, and Windows will not unlink a directory a process is sitting in.
/// The functional lanes still gate that platform through every other journey here.
#[cfg(unix)]
#[test]
fn a_working_directory_that_no_longer_exists_is_reported_rather_than_crashing() {
    let sandbox = Sandbox::new();
    let doomed = sandbox.subdirectory("doomed");
    let binary = env!("CARGO_BIN_EXE_onetaskgraph");

    // The shell removes the directory it is standing in and then execs the binary into
    // it, so the binary really starts life somewhere the kernel can no longer name —
    // which is the one way `current_dir` fails that a user can actually reach.
    let output = std::process::Command::new("sh")
        .current_dir(&doomed)
        .env("XDG_CONFIG_HOME", sandbox.config_home())
        .env_remove("HOME")
        .args(["-c", r#"rm -rf "$PWD" && exec "$0" config show"#, binary])
        .output()
        .expect("the shell runs");

    assert_eq!(output.status.code(), Some(1));
    let message = stderr(&output);
    assert!(
        message.contains("could not read the working directory"),
        "the failure names what could not be read: {message}"
    );
    assert!(message.contains("next:"), "{message}");
    assert!(
        stdout(&output).is_empty(),
        "a refusal writes nothing to stdout"
    );
}

/// Unix only: an environment block holding bytes that are not UTF-8 is something only
/// `OsString` can express, and Windows environment blocks are UTF-16 rather than bytes.
#[cfg(unix)]
#[test]
fn a_setting_variable_this_build_cannot_read_is_refused_by_name_rather_than_ignored() {
    use std::os::unix::ffi::OsStringExt;

    let sandbox = Sandbox::new();
    let not_utf8 = std::ffi::OsString::from_vec(vec![0x66, 0xff, 0x6f]);

    let output = sandbox
        .command()
        .env("ONETASKGRAPH_OUTPUT", &not_utf8)
        .args(["config", "show"])
        .assert()
        .failure()
        .get_output()
        .clone();

    let message = stderr(&output);
    assert!(
        message.contains("ONETASKGRAPH_OUTPUT"),
        "the refusal names the variable: {message}"
    );
    assert!(message.contains("next:"), "{message}");
    assert!(
        stdout(&output).is_empty(),
        "a refusal writes nothing to stdout"
    );
}

/// Unix only, for the same reason as the refusal above.
#[cfg(unix)]
#[test]
fn a_variable_this_build_cannot_read_and_never_asked_for_leaves_the_run_alone() {
    use std::os::unix::ffi::OsStringExt;

    let sandbox = Sandbox::new();
    let not_utf8 = std::ffi::OsString::from_vec(vec![0x66, 0xff, 0x6f]);

    let output = sandbox
        .command()
        .env("SOMETHING_ELSE", &not_utf8)
        .args(["config", "show", "--json"])
        .assert()
        .success()
        .get_output()
        .clone();

    let page_size = setting(&shown(&output), "page_size").clone();
    assert_eq!(page_size["value"], 50);
    assert_eq!(page_size["origin"]["layer"], "default");
}

#[test]
fn a_credentials_file_line_that_is_not_an_assignment_is_refused_without_quoting_it() {
    let sandbox = Sandbox::new();
    let path = sandbox.secrets_file(&format!("LINEAR_API_KEY={PLANTED}\nthis is not a line\n"));

    let message = refusal(&sandbox, &[]);
    assert!(
        message.contains(&format!("{}:2", path.display())),
        "{message}"
    );
    assert!(
        !message.contains(PLANTED),
        "a refusal about one line does not print another line's value"
    );
}

/// Check `document` against the bundle's `root`, saying which root refused it.
fn validates(bundle: &Value, root: &str, document: &Value) {
    let schema = &bundle["roots"][root];
    assert!(
        schema.is_object(),
        "the bundle this binary emits carries the {root} root: {bundle:#}"
    );
    let validator = jsonschema::validator_for(schema)
        .unwrap_or_else(|error| panic!("the {root} root compiles: {error}"));
    let problems: Vec<String> = validator
        .iter_errors(document)
        .map(|error| format!("{} at {}", error, error.instance_path()))
        .collect();
    assert!(
        problems.is_empty(),
        "what the binary emitted does not match the {root} root it also emits: {} \
         (the value was {document:#})",
        problems.join("; ")
    );
}

#[test]
fn the_machine_readable_output_matches_a_root_of_the_schema_this_binary_emits() {
    for boundary in SOURCE_BOUNDARIES {
        let sandbox = Sandbox::new();
        sandbox.project_document(&one_source(boundary));
        // Both credential layers at once, so the report carries one entry of each kind and
        // the enum of layers is exercised rather than only its first variant.
        sandbox.secrets_file(&format!(
            "LINEAR_API_KEY={PLANTED}\nGH_PROJECTS_TOKEN={PLANTED}\n"
        ));

        let bundle: Value = serde_json::from_str(&stdout(
            &sandbox
                .command()
                .arg("schema")
                .assert()
                .success()
                .get_output()
                .clone(),
        ))
        .expect("the schema bundle is JSON");

        let output = sandbox
            .command()
            .env_remove("LINEAR_API_KEY")
            .env("GH_PROJECTS_TOKEN", "exported-by-the-shell")
            .args(["config", "show", "--json"])
            .assert()
            .success()
            .get_output()
            .clone();
        let shown = shown(&output);

        // The whole document, against the root the verb claims to emit.
        validates(&bundle, "EffectiveConfig", &shown);

        // `Setting.value` is an unconstrained JSON value — a setting holds whatever its
        // own type is — so the roots for the settings that *do* have a shape are checked
        // against the values this run actually reported, not left implied by the parent.
        validates(&bundle, "OutputFormat", &setting(&shown, "output")["value"]);

        let credentials = shown["secrets"]["variables"]
            .as_array()
            .expect("the report lists the credentials the file supplied");
        assert_eq!(
            credentials.len(),
            2,
            "both planted names are reported: {shown:#}"
        );
        for credential in credentials {
            validates(&bundle, "ResolvedCredential", credential);
            validates(&bundle, "CredentialLayer", &credential["resolved_from"]);
        }
        let layers: Vec<&Value> = credentials
            .iter()
            .map(|credential| &credential["resolved_from"])
            .collect();
        assert_eq!(
            layers,
            vec!["environment", "secrets-file"],
            "the exported name resolves from the environment and the other from the file: \
         {shown:#}"
        );
    }
}

#[test]
fn a_configuration_document_that_cannot_be_read_stops_the_run_rather_than_being_skipped() {
    let sandbox = Sandbox::new();
    // A project document that would set `page_size` if the layer beneath it were the
    // one in trouble, so a run that skipped the unreadable file would look successful.
    sandbox.project_document("page_size: 25\n");
    let path = sandbox.unreadable("onetaskgraph/config.yaml");

    let message = refusal(&sandbox, &[]);
    assert!(
        message.contains(&path.display().to_string()),
        "the refusal names the file it could not read: {message}"
    );
    assert!(
        message.contains("could not read"),
        "the refusal says what went wrong: {message}"
    );
}

#[test]
fn an_obstructed_project_document_stops_the_run_rather_than_being_walked_past() {
    let sandbox = Sandbox::new();
    // The user-level document would set `page_size` if the walk simply skipped what it
    // could not read, so a run that walked past this one would look successful and read
    // a configuration the user does not believe is in force.
    sandbox.user_document("page_size: 11\n");
    let obstruction = sandbox.project().join("onetaskgraph.yaml");
    std::fs::create_dir_all(&obstruction).expect("a directory in the document's place");

    let message = refusal(&sandbox, &[]);
    assert!(
        message.contains(&obstruction.display().to_string()),
        "the refusal names the document it could not read: {message}"
    );
    assert!(
        message.contains("could not read"),
        "the refusal says what went wrong: {message}"
    );
}

#[test]
fn a_credentials_file_that_cannot_be_read_stops_the_run_rather_than_being_skipped() {
    for boundary in SOURCE_BOUNDARIES {
        let sandbox = Sandbox::new();
        sandbox.project_document(&one_source(boundary));
        let path = sandbox.unreadable("onetaskgraph/secrets.env");

        let message = refusal(&sandbox, &[]);
        assert!(
            message.contains(&path.display().to_string()),
            "the refusal names the credentials file it could not read: {message}"
        );
        assert!(
            message.contains("could not read"),
            "the refusal says what went wrong: {message}"
        );
    }
}