termproof 0.4.1

Evidence-first verification for TUI and terminal applications: recipe model, schema, steps, assertions, orchestration, terminal sessions and the evidence pipeline
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
//! The command line that fills in a [`RunConfig`].
//!
//! [`super`] models a whole run and says how the three sources of an answer
//! rank — flag, then config file, then built-in default, via [`pick`]. It does
//! not say where the flags come from, so every consumer wrote that layer again:
//! the same twenty-odd `clap` builders, and [`pick`] re-derived by hand once per
//! flag. This module is that layer, once.
//!
//! # The whole outer loop
//!
//! ```no_run
//! use termproof::run_config::{self, Execution, RunConfig};
//!
//! let builtin = RunConfig {
//!     execution: Execution { transport: Some("pty".into()), ..Default::default() },
//!     ..Default::default()
//! };
//! let matches = run_config::clap_command().get_matches();
//! let config = run_config::resolve(&matches, &builtin)?;
//! # Ok::<(), String>(())
//! ```
//!
//! [`resolve`] reads `--run-config`, parses it, and applies the precedence to
//! every field. A consumer with no flags of its own needs nothing else.
//!
//! # Composing
//!
//! A consumer with flags of its own owns the [`Command`] and asks for the
//! standard ones to be added to it:
//!
//! ```
//! use clap::{Arg, ArgAction, Command};
//! use termproof::run_config;
//!
//! let command = run_config::augment_args(Command::new("validate"))
//!     .arg(Arg::new("watch").long("watch").action(ArgAction::SetTrue));
//! let matches = command.get_matches_from(["validate", "--renderer", "alternate", "--watch"]);
//!
//! assert!(matches.get_flag("watch"));
//! let flags = run_config::from_matches(&matches).unwrap();
//! assert_eq!(flags.execution.renderer.as_deref(), Some("alternate"));
//! ```
//!
//! [`clap_command`] is [`augment_args`] over a bare `termproof` command, so the
//! two agree by construction rather than by being kept in step.
//!
//! # No flag here has a `default_value`
//!
//! That is the load-bearing rule, and [`super`]'s precedence section is where
//! it comes from: a flag that merely *has* a default is indistinguishable from
//! one the caller *passed*, and if the two look alike a config file can never
//! override a defaulted flag — which would make most of the schema dead. So the
//! built-in default lives in the `builtin: &RunConfig` argument to [`resolve`]
//! and [`merge`], never in the `clap` argument, and "the caller passed this"
//! means exactly that `ArgMatches` has a value for it.
//!
//! The corollary is that **an empty value is still a value**. `--renderer ''`
//! beats a configured renderer and resolves to `Some("")`, because the caller
//! passed it. Reading it as "unset" would be this layer inventing a fourth rule
//! on top of the three [`pick`] states, and would put the caller back to
//! guessing which flags treat empty specially.
//!
//! # Repeatable flags, and what "unset" means for them
//!
//! `--root`, `--exclude`, `--recipe-name`, `--env`, `--publisher` and
//! `--publisher-setting` are repeatable, and land in fields that are a `Vec` or
//! a map rather than an `Option`. For those, empty is unset and the first
//! non-empty of flag / config / built-in wins as a whole — the same shape
//! [`pick`] has, applied to the whole list.
//!
//! Two consequences worth stating rather than discovering. There is no way to
//! say "explicitly no excludes" on the command line, which matches the config
//! file, where `exclude: []` is also indistinguishable from an absent key. And
//! a passed `--env` replaces a configured `env` wholesale rather than merging
//! key by key: per-key merging is a fourth precedence rule, and this layer
//! exists to stop those being invented one consumer at a time.
//!
//! # `uploaded_media` is the one field that does not obey the precedence
//!
//! [`Requirements::uploaded_media`](super::Requirements::uploaded_media) is a
//! `bool`, so it has no unset state for [`pick`] to rank and [`merge`] ORs the
//! three layers instead. Any of the three asking for it is asking for it.
//!
//! The consequence is that it can be raised but never lowered: `require:
//! {uploaded_media: false}` in a `--run-config` file is inert against a
//! built-in `true`, and there is no `--no-require-uploaded-media`. For a
//! *requirement* that is the safe direction to fail in — a config file quietly
//! switching off a "the evidence must be shareable" gate is the outcome worth
//! preventing — but it is a real exception to the rule above rather than an
//! instance of it, so a caller that needs the gate off has to leave it off in
//! the built-in.
//!
//! # Names this layer claims
//!
//! [`augment_args`] adds 22 long options, and several are generic enough that a
//! consumer may already own one: `--root`, `--model`, `--exclude`, `--env`,
//! `--publisher`, `--transport`, `--effort`. A duplicate long name is a
//! `clap` **panic**, not an error, and `clap`'s `debug_assert` machinery only
//! runs in debug builds, so a consumer that only builds release gets the
//! collision resolved by `clap`'s internal ordering with no diagnostic at all.
//! The full list, in the order they are added:
//!
//! `--run-config`, `--root`, `--repo-marker`, `--all`, `--priority`,
//! `--recipe-name`, `--changed-files`, `--exclude`, `--transport`,
//! `--renderer`, `--model`, `--effort`, `--binary-installed`,
//! `--binary-build`, `--env`, `--timeout-scale`, `--artifact-dir`,
//! `--report-path`, `--result-json-path`, `--publisher`,
//! `--publisher-setting`, `--require-uploaded-media`,
//! `--require-media-publisher`.
//!
//! The argument ids are the same strings without the leading `--`, and the two
//! [`ArgGroup`] names are `select` and `binary`.
//!
//! For the same reason, [`from_matches`] and [`resolve`] must be handed an
//! `ArgMatches` from a command this module augmented. `clap` panics on a lookup
//! of an id the command never declared, so despite the `Result` these two
//! cannot turn a foreign `ArgMatches` into an `Err` — the realistic way in is
//! augmenting a *subcommand* and then passing the top-level matches.
//!
//! # Divergence from `termproof.cli`
//!
//! Python's `termproof.cli` is an `argparse` program for the `termproof`
//! binary; this is a library layer for a config type Python has no counterpart
//! for. Where the two name the same thing they use the same flag —
//! `--priority`, `--recipe-name`, `--renderer` — and where they do not, this
//! module picks a different name rather than reusing one of Python's for a
//! different meaning. The full reconciliation is in the CHANGELOG entry for
//! #197.
//!
//! One divergence is not about a *name* and is the easiest to trip over.
//! `--priority` and `--recipe-name` **compose in Python and conflict here.**
//! `termproof.registry.select_recipes` applies priority and then names as
//! successive filters, so `--priority P0 --recipe-name smoke` is a legal
//! narrowing — "smoke, if it is P0". Here they are two arms of [`Selection`],
//! which holds one, so they sit in one [`ArgGroup`] and passing both is a usage
//! error. Same two flag names, same two meanings, different composition; a
//! command line ported from Python that pairs them stops working. Expressing
//! the intersection needs the caller to filter the result of a
//! `Selection::Priority` run themselves.

use std::collections::BTreeMap;
use std::path::Path;

use clap::{value_parser, Arg, ArgAction, ArgGroup, ArgMatches, Command};

use super::{
    pick, BinarySource, Discovery, Execution, Output, Publisher, Requirements, RunConfig, Selection,
};

/// The standard `RunConfig` flags on a bare `termproof` command.
///
/// Equal to [`augment_args`] over `Command::new("termproof")`; a consumer that
/// already owns a [`Command`] should call that instead.
pub fn clap_command() -> Command {
    augment_args(Command::new("termproof"))
}

/// Add the standard `RunConfig` flags to an existing command.
///
/// The consumer keeps its own `Command` — name, version, about, subcommands and
/// any flags of its own — and gets the ones this crate knows how to parse back
/// into a [`RunConfig`].
pub fn augment_args(command: Command) -> Command {
    command
        .arg(
            Arg::new("run-config")
                .long("run-config")
                .value_name("PATH")
                .help("Read a run config from this YAML or JSON file"),
        )
        // -- discovery ---------------------------------------------------
        .arg(
            Arg::new("root")
                .long("root")
                .value_name("DIR")
                .action(ArgAction::Append)
                .help("Directory a changed file must fall under to count as touching the framework (repeatable)"),
        )
        .arg(
            Arg::new("repo-marker")
                .long("repo-marker")
                .value_name("FRAGMENT")
                .help("Path fragment that marks the start of a repo-relative path"),
        )
        .arg(
            Arg::new("all")
                .long("all")
                .action(ArgAction::SetTrue)
                .help("Run every discovered recipe"),
        )
        .arg(
            Arg::new("priority")
                .long("priority")
                .value_name("PRIORITY")
                .help("Run every recipe at this priority, e.g. P0"),
        )
        .arg(
            Arg::new("recipe-name")
                .long("recipe-name")
                .value_name("NAME")
                .action(ArgAction::Append)
                .help("Run exactly this recipe (repeatable)"),
        )
        .arg(
            Arg::new("changed-files")
                .long("changed-files")
                .value_name("PATH")
                .help("Run recipes whose ci_paths match the changed files listed in this file"),
        )
        // The enum is mutually exclusive by construction, so the command line
        // is too — otherwise "all and a priority" becomes something this layer
        // has to rank, and the config file has no such ranking to mirror.
        .group(
            ArgGroup::new("select")
                .args(["all", "priority", "recipe-name", "changed-files"])
                .multiple(false),
        )
        .arg(
            Arg::new("exclude")
                .long("exclude")
                .value_name("GLOB")
                .action(ArgAction::Append)
                .help("Skip recipes whose name matches this glob (repeatable)"),
        )
        // -- execution ---------------------------------------------------
        .arg(
            Arg::new("transport")
                .long("transport")
                .value_name("NAME")
                .help("Terminal transport, for example pty or tmux"),
        )
        .arg(
            Arg::new("renderer")
                .long("renderer")
                .value_name("NAME")
                .help("Renderer or renderer set to validate"),
        )
        .arg(
            Arg::new("model")
                .long("model")
                .value_name("ID")
                .help("Model identifier to run the product under test with"),
        )
        .arg(
            Arg::new("effort")
                .long("effort")
                .value_name("LEVEL")
                .help("Reasoning-effort setting to pair with --model"),
        )
        .arg(
            Arg::new("binary-installed")
                .long("binary-installed")
                .action(ArgAction::SetTrue)
                .help("Test the already-installed binary"),
        )
        .arg(
            Arg::new("binary-build")
                .long("binary-build")
                .value_name("CHANGE")
                .help("Build from the current checkout, labelling results with this change id"),
        )
        .group(
            ArgGroup::new("binary")
                .args(["binary-installed", "binary-build"])
                .multiple(false),
        )
        .arg(
            Arg::new("env")
                .long("env")
                .value_name("KEY=VALUE")
                .action(ArgAction::Append)
                .help("Environment applied to every recipe (repeatable)"),
        )
        .arg(
            Arg::new("timeout-scale")
                .long("timeout-scale")
                .value_name("FACTOR")
                .value_parser(value_parser!(f64))
                .help("Multiply every recipe's declared timeout by this factor"),
        )
        // -- output ------------------------------------------------------
        .arg(
            Arg::new("artifact-dir")
                .long("artifact-dir")
                .value_name("DIR")
                .help("Directory for screenshots, casts and videos"),
        )
        .arg(
            Arg::new("report-path")
                .long("report-path")
                .value_name("PATH")
                .help("Where the human-readable report is written"),
        )
        .arg(
            Arg::new("result-json-path")
                .long("result-json-path")
                .value_name("PATH")
                .help("Where the machine-readable result JSON is written"),
        )
        .arg(
            Arg::new("publisher")
                .long("publisher")
                .value_name("NAME")
                .action(ArgAction::Append)
                .help("Publisher to run, in the order given (repeatable)"),
        )
        .arg(
            Arg::new("publisher-setting")
                .long("publisher-setting")
                .value_name("PUBLISHER:KEY=VALUE")
                .action(ArgAction::Append)
                .help("Setting for a publisher named by --publisher (repeatable)"),
        )
        .arg(
            Arg::new("require-uploaded-media")
                .long("require-uploaded-media")
                .action(ArgAction::SetTrue)
                .help("Fail if evidence was not uploaded anywhere shareable"),
        )
        .arg(
            Arg::new("require-media-publisher")
                .long("require-media-publisher")
                .value_name("NAME")
                .help("Fail if this specific publisher did not carry the evidence"),
        )
}

/// Exactly what the flags said, and nothing else.
///
/// Every field the caller did not pass is left unset, so the result is the
/// `flag` column of [`pick`] rather than a usable config. [`resolve`] is what
/// turns it into one.
pub fn from_matches(matches: &ArgMatches) -> Result<RunConfig, String> {
    Ok(RunConfig {
        discovery: Discovery {
            roots: strings(matches, "root"),
            repo_marker: string(matches, "repo-marker"),
            select: selection(matches),
            exclude: strings(matches, "exclude"),
        },
        execution: Execution {
            transport: string(matches, "transport"),
            renderer: string(matches, "renderer"),
            model: string(matches, "model"),
            effort: string(matches, "effort"),
            binary: binary(matches),
            env: env(matches)?,
            timeout_scale: matches.get_one::<f64>("timeout-scale").copied(),
        },
        output: Output {
            artifact_dir: string(matches, "artifact-dir"),
            report_path: string(matches, "report-path"),
            result_json_path: string(matches, "result-json-path"),
            publishers: publishers(matches)?,
            require: Requirements {
                uploaded_media: matches.get_flag("require-uploaded-media"),
                media_publisher: string(matches, "require-media-publisher"),
            },
        },
    })
}

/// The config file named by `--run-config`, or an empty config if there is none.
pub fn configured(matches: &ArgMatches) -> Result<RunConfig, String> {
    match matches.get_one::<String>("run-config") {
        Some(path) => RunConfig::from_path(Path::new(path)),
        None => Ok(RunConfig::default()),
    }
}

/// The command line and its `--run-config` file, resolved against `builtin`.
///
/// This is the whole outer loop: [`configured`] for the file, [`from_matches`]
/// for the flags, [`merge`] for the precedence.
pub fn resolve(matches: &ArgMatches, builtin: &RunConfig) -> Result<RunConfig, String> {
    Ok(merge(
        &from_matches(matches)?,
        &configured(matches)?,
        builtin,
    ))
}

/// [`pick`] over every field of a [`RunConfig`], in one place.
///
/// Separate from [`resolve`] because it is pure: a consumer that reads its
/// config from somewhere other than `--run-config` still wants the precedence
/// applied once rather than per flag.
pub fn merge(flags: &RunConfig, configured: &RunConfig, builtin: &RunConfig) -> RunConfig {
    let (f, c, b) = (flags, configured, builtin);
    RunConfig {
        discovery: Discovery {
            roots: pick_list(&f.discovery.roots, &c.discovery.roots, &b.discovery.roots),
            repo_marker: pick_opt(
                &f.discovery.repo_marker,
                &c.discovery.repo_marker,
                &b.discovery.repo_marker,
            ),
            select: pick_opt(
                &f.discovery.select,
                &c.discovery.select,
                &b.discovery.select,
            ),
            exclude: pick_list(
                &f.discovery.exclude,
                &c.discovery.exclude,
                &b.discovery.exclude,
            ),
        },
        execution: Execution {
            transport: pick_opt(
                &f.execution.transport,
                &c.execution.transport,
                &b.execution.transport,
            ),
            renderer: pick_opt(
                &f.execution.renderer,
                &c.execution.renderer,
                &b.execution.renderer,
            ),
            model: pick_opt(&f.execution.model, &c.execution.model, &b.execution.model),
            effort: pick_opt(
                &f.execution.effort,
                &c.execution.effort,
                &b.execution.effort,
            ),
            binary: pick_opt(
                &f.execution.binary,
                &c.execution.binary,
                &b.execution.binary,
            ),
            env: pick_map(&f.execution.env, &c.execution.env, &b.execution.env),
            timeout_scale: pick_opt(
                &f.execution.timeout_scale,
                &c.execution.timeout_scale,
                &b.execution.timeout_scale,
            ),
        },
        output: Output {
            artifact_dir: pick_opt(
                &f.output.artifact_dir,
                &c.output.artifact_dir,
                &b.output.artifact_dir,
            ),
            report_path: pick_opt(
                &f.output.report_path,
                &c.output.report_path,
                &b.output.report_path,
            ),
            result_json_path: pick_opt(
                &f.output.result_json_path,
                &c.output.result_json_path,
                &b.output.result_json_path,
            ),
            publishers: pick_list(
                &f.output.publishers,
                &c.output.publishers,
                &b.output.publishers,
            ),
            require: Requirements {
                // A `bool` has no unset state to rank, so this is `pick` where
                // `pick` can tell the difference and a disjunction where it
                // cannot: any of the three asking for it is asking for it.
                uploaded_media: f.output.require.uploaded_media
                    || c.output.require.uploaded_media
                    || b.output.require.uploaded_media,
                media_publisher: pick_opt(
                    &f.output.require.media_publisher,
                    &c.output.require.media_publisher,
                    &b.output.require.media_publisher,
                ),
            },
        },
    }
}

/// [`pick`] for a field whose built-in is itself optional.
///
/// [`pick`] takes the built-in as a value, because that is what a built-in
/// usually is. Here it arrives as a field of a [`RunConfig`], so it can be
/// absent too — and when it is, there is no third answer and the first two are
/// the whole ranking.
fn pick_opt<T: Clone>(flags: &Option<T>, configured: &Option<T>, builtin: &Option<T>) -> Option<T> {
    match builtin {
        Some(builtin) => Some(pick(flags.clone(), configured.clone(), builtin.clone())),
        None => flags.clone().or_else(|| configured.clone()),
    }
}

/// [`pick`] for a field whose "unset" is emptiness rather than `None`.
fn pick_list<T: Clone>(flags: &[T], configured: &[T], builtin: &[T]) -> Vec<T> {
    for candidate in [flags, configured, builtin] {
        if !candidate.is_empty() {
            return candidate.to_vec();
        }
    }
    Vec::new()
}

/// [`pick_list`], for the one field that is a map rather than a list.
fn pick_map(
    flags: &BTreeMap<String, String>,
    configured: &BTreeMap<String, String>,
    builtin: &BTreeMap<String, String>,
) -> BTreeMap<String, String> {
    for candidate in [flags, configured, builtin] {
        if !candidate.is_empty() {
            return candidate.clone();
        }
    }
    BTreeMap::new()
}

fn string(matches: &ArgMatches, id: &str) -> Option<String> {
    matches.get_one::<String>(id).cloned()
}

fn strings(matches: &ArgMatches, id: &str) -> Vec<String> {
    matches
        .get_many::<String>(id)
        .map(|values| values.cloned().collect())
        .unwrap_or_default()
}

fn selection(matches: &ArgMatches) -> Option<Selection> {
    if matches.get_flag("all") {
        return Some(Selection::All);
    }
    if let Some(priority) = string(matches, "priority") {
        return Some(Selection::Priority(priority));
    }
    let names = strings(matches, "recipe-name");
    if !names.is_empty() {
        return Some(Selection::Names(names));
    }
    string(matches, "changed-files").map(Selection::ChangedFiles)
}

fn binary(matches: &ArgMatches) -> Option<BinarySource> {
    if matches.get_flag("binary-installed") {
        return Some(BinarySource::Installed);
    }
    string(matches, "binary-build").map(BinarySource::Build)
}

fn env(matches: &ArgMatches) -> Result<BTreeMap<String, String>, String> {
    let mut env = BTreeMap::new();
    for entry in strings(matches, "env") {
        let (key, value) = entry
            .split_once('=')
            .ok_or_else(|| format!("--env {entry}: expected KEY=VALUE"))?;
        if key.is_empty() {
            return Err(format!("--env {entry}: the key is empty"));
        }
        env.insert(key.to_string(), value.to_string());
    }
    Ok(env)
}

fn publishers(matches: &ArgMatches) -> Result<Vec<Publisher>, String> {
    let mut publishers: Vec<Publisher> = strings(matches, "publisher")
        .into_iter()
        .map(|name| Publisher {
            name,
            settings: BTreeMap::new(),
        })
        .collect();
    for entry in strings(matches, "publisher-setting") {
        let (name, setting) = entry
            .split_once(':')
            .ok_or_else(|| format!("--publisher-setting {entry}: expected PUBLISHER:KEY=VALUE"))?;
        let (key, value) = setting
            .split_once('=')
            .ok_or_else(|| format!("--publisher-setting {entry}: expected PUBLISHER:KEY=VALUE"))?;
        // Not auto-created: a setting for a publisher nobody asked to run is
        // either a typo in the name or a forgotten `--publisher`, and both are
        // better as a startup failure than as a publisher that silently
        // appears, or settings that silently go nowhere.
        let publisher = publishers
            .iter_mut()
            .find(|p| p.name == name)
            .ok_or_else(|| format!("--publisher-setting {entry}: no --publisher named {name:?}"))?;
        publisher
            .settings
            .insert(key.to_string(), value.to_string());
    }
    Ok(publishers)
}

#[cfg(test)]
mod tests {
    use super::*;

    /// Parse a command line the way a consumer with no extra flags would.
    fn parse(argv: &[&str]) -> RunConfig {
        try_parse(argv).unwrap()
    }

    fn try_parse(argv: &[&str]) -> Result<RunConfig, String> {
        from_matches(&matches_of(argv))
    }

    fn matches_of(argv: &[&str]) -> ArgMatches {
        let mut full = vec!["termproof"];
        full.extend_from_slice(argv);
        clap_command().get_matches_from(full)
    }

    #[test]
    fn the_command_line_says_the_same_things_the_config_file_does() {
        // The counterpart of `the_ci_workflows_flags_are_all_expressible` in
        // the parent module: every field of the schema, said as flags.
        let c = parse(&[
            "--root",
            "some/framework/root/",
            "--repo-marker",
            "/repo/",
            "--recipe-name",
            "smoke",
            "--recipe-name",
            "plugins",
            "--exclude",
            "flaky-*",
            "--transport",
            "pty",
            "--renderer",
            "some-renderer",
            "--model",
            "some-model",
            "--effort",
            "high",
            "--binary-installed",
            "--env",
            "KEY=value",
            "--timeout-scale",
            "1.5",
            "--artifact-dir",
            "/tmp/evidence",
            "--report-path",
            "/tmp/report.md",
            "--result-json-path",
            "/tmp/results.json",
            "--publisher",
            "object-store",
            "--publisher-setting",
            "object-store:bucket=some-bucket",
            "--require-uploaded-media",
            "--require-media-publisher",
            "image-host",
        ]);
        assert_eq!(c.discovery.roots, ["some/framework/root/"]);
        assert_eq!(c.discovery.repo_marker.as_deref(), Some("/repo/"));
        assert_eq!(
            c.discovery.select,
            Some(Selection::Names(vec![
                "smoke".to_string(),
                "plugins".to_string()
            ]))
        );
        assert_eq!(c.discovery.exclude, ["flaky-*"]);
        assert_eq!(c.execution.transport.as_deref(), Some("pty"));
        assert_eq!(c.execution.renderer.as_deref(), Some("some-renderer"));
        assert_eq!(c.execution.model.as_deref(), Some("some-model"));
        assert_eq!(c.execution.effort.as_deref(), Some("high"));
        assert_eq!(c.execution.binary, Some(BinarySource::Installed));
        assert_eq!(c.execution.env["KEY"], "value");
        assert_eq!(c.execution.timeout_scale, Some(1.5));
        assert_eq!(c.output.artifact_dir.as_deref(), Some("/tmp/evidence"));
        assert_eq!(c.output.report_path.as_deref(), Some("/tmp/report.md"));
        assert_eq!(
            c.output.result_json_path.as_deref(),
            Some("/tmp/results.json")
        );
        assert_eq!(
            c.publisher("object-store").unwrap().settings["bucket"],
            "some-bucket"
        );
        assert!(c.output.require.uploaded_media);
        assert_eq!(
            c.output.require.media_publisher.as_deref(),
            Some("image-host")
        );
    }

    #[test]
    fn an_empty_command_line_asks_for_nothing() {
        // The precondition for the whole precedence: what the caller did not
        // pass has to arrive as unset, or the config file is dead.
        assert_eq!(parse(&[]), RunConfig::default());
    }

    #[test]
    fn every_way_of_selecting_recipes_has_a_flag() {
        assert_eq!(parse(&["--all"]).discovery.select, Some(Selection::All));
        assert_eq!(
            parse(&["--priority", "P0"]).discovery.select,
            Some(Selection::Priority("P0".into()))
        );
        assert_eq!(
            parse(&["--recipe-name", "a"]).discovery.select,
            Some(Selection::Names(vec!["a".to_string()]))
        );
        assert_eq!(
            parse(&["--changed-files", "/tmp/c.txt"]).discovery.select,
            Some(Selection::ChangedFiles("/tmp/c.txt".into()))
        );
    }

    #[test]
    fn a_second_way_of_selecting_is_a_usage_error() {
        // The enum cannot hold two, so neither can the command line, and clap
        // says so before this module has to rank them.
        let e = clap_command()
            .try_get_matches_from(["termproof", "--all", "--priority", "P0"])
            .unwrap_err();
        assert_eq!(e.kind(), clap::error::ErrorKind::ArgumentConflict);
    }

    #[test]
    fn priority_and_recipe_name_do_not_compose_here_though_they_do_in_python() {
        // Pinned because it is the one divergence from `termproof.cli` that is
        // not visible in a flag *name*. `select_recipes` there applies priority
        // and then names as successive filters, so this pair is a legal
        // narrowing; `Selection` holds one arm, so here it is a usage error.
        // If `Selection` ever grows a way to say both, this test failing is the
        // prompt to update the divergence table rather than a regression.
        let e = clap_command()
            .try_get_matches_from(["termproof", "--priority", "P0", "--recipe-name", "smoke"])
            .unwrap_err();
        assert_eq!(e.kind(), clap::error::ErrorKind::ArgumentConflict);
    }

    #[test]
    fn a_binary_cannot_be_both_installed_and_built() {
        let e = clap_command()
            .try_get_matches_from(["termproof", "--binary-installed", "--binary-build", "D123"])
            .unwrap_err();
        assert_eq!(e.kind(), clap::error::ErrorKind::ArgumentConflict);
    }

    #[test]
    fn building_from_a_checkout_carries_the_change_id() {
        assert_eq!(
            parse(&["--binary-build", "D123"]).execution.binary,
            Some(BinarySource::Build("D123".into()))
        );
    }

    #[test]
    fn a_flag_beats_the_config_which_beats_the_builtin() {
        let builtin = RunConfig {
            execution: Execution {
                transport: Some("builtin".into()),
                renderer: Some("builtin".into()),
                ..Default::default()
            },
            ..Default::default()
        };
        let configured = RunConfig {
            execution: Execution {
                renderer: Some("configured".into()),
                ..Default::default()
            },
            ..Default::default()
        };

        let c = merge(&parse(&["--renderer", "flag"]), &configured, &builtin);
        assert_eq!(c.execution.renderer.as_deref(), Some("flag"));

        let c = merge(&parse(&[]), &configured, &builtin);
        assert_eq!(c.execution.renderer.as_deref(), Some("configured"));

        // And the field no flag and no config named still gets the built-in,
        // which is the half a caller re-deriving `pick()` per flag drops.
        assert_eq!(c.execution.transport.as_deref(), Some("builtin"));
    }

    #[test]
    fn a_flag_passed_empty_is_still_a_flag_passed() {
        // The bug this layer exists to stop being rewritten: `--renderer ''`
        // is a value the caller chose, so it beats the config. Treating it as
        // unset would be a fourth state on top of `pick`'s three, and every
        // consumer would have to remember which flags have it.
        let configured = RunConfig {
            execution: Execution {
                renderer: Some("configured".into()),
                ..Default::default()
            },
            ..Default::default()
        };
        let c = merge(
            &parse(&["--renderer", ""]),
            &configured,
            &RunConfig::default(),
        );
        assert_eq!(c.execution.renderer.as_deref(), Some(""));
    }

    #[test]
    fn a_repeatable_flag_replaces_the_configured_list_rather_than_adding_to_it() {
        let configured = RunConfig {
            discovery: Discovery {
                exclude: vec!["configured-*".to_string()],
                ..Default::default()
            },
            ..Default::default()
        };
        let c = merge(
            &parse(&["--exclude", "flag-*"]),
            &configured,
            &RunConfig::default(),
        );
        assert_eq!(c.discovery.exclude, ["flag-*"]);

        // Unset for a list is empty, so the configured list survives.
        let c = merge(&parse(&[]), &configured, &RunConfig::default());
        assert_eq!(c.discovery.exclude, ["configured-*"]);
    }

    #[test]
    fn a_passed_env_replaces_the_configured_env_wholesale() {
        let configured = RunConfig {
            execution: Execution {
                env: BTreeMap::from([("FROM_CONFIG".to_string(), "1".to_string())]),
                ..Default::default()
            },
            ..Default::default()
        };
        let c = merge(
            &parse(&["--env", "FROM_FLAG=1"]),
            &configured,
            &RunConfig::default(),
        );
        assert_eq!(c.execution.env.keys().collect::<Vec<_>>(), ["FROM_FLAG"]);
    }

    #[test]
    fn any_of_the_three_can_require_uploaded_media() {
        let required = RunConfig {
            output: Output {
                require: Requirements {
                    uploaded_media: true,
                    ..Default::default()
                },
                ..Default::default()
            },
            ..Default::default()
        };
        let off = RunConfig::default();
        assert!(
            merge(&parse(&["--require-uploaded-media"]), &off, &off)
                .output
                .require
                .uploaded_media
        );
        assert!(
            merge(&parse(&[]), &required, &off)
                .output
                .require
                .uploaded_media
        );
        assert!(
            merge(&parse(&[]), &off, &required)
                .output
                .require
                .uploaded_media
        );
        assert!(!merge(&parse(&[]), &off, &off).output.require.uploaded_media);
    }

    #[test]
    fn a_required_upload_can_be_raised_but_never_lowered() {
        // The documented exception to the precedence, pinned so it stays a
        // decision rather than becoming a surprise. A `bool` has no unset
        // state, so a configured `false` is indistinguishable from an absent
        // key and cannot outrank a built-in `true` — and there is no flag for
        // it either, which is the half that makes this one-way.
        let builtin = RunConfig {
            output: Output {
                require: Requirements {
                    uploaded_media: true,
                    ..Default::default()
                },
                ..Default::default()
            },
            ..Default::default()
        };
        let says_false =
            RunConfig::parse("output:\n  require:\n    uploaded_media: false\n").unwrap();
        assert!(
            merge(&parse(&[]), &says_false, &builtin)
                .output
                .require
                .uploaded_media
        );
        assert!(
            !clap_command()
                .get_arguments()
                .any(|a| a.get_long() == Some("no-require-uploaded-media")),
            "a flag to lower it would make this test's premise false"
        );
    }

    #[test]
    fn an_env_entry_without_a_value_says_what_it_wanted() {
        let e = try_parse(&["--env", "NOEQUALS"]).unwrap_err();
        assert!(e.contains("KEY=VALUE"), "{e}");
        let e = try_parse(&["--env", "=value"]).unwrap_err();
        assert!(e.contains("empty"), "{e}");
        // An empty *value* is legitimate: setting a variable to nothing.
        assert_eq!(parse(&["--env", "KEY="]).execution.env["KEY"], "");
    }

    #[test]
    fn publishers_keep_the_order_they_were_given() {
        let c = parse(&["--publisher", "first", "--publisher", "second"]);
        let names: Vec<&str> = c
            .output
            .publishers
            .iter()
            .map(|p| p.name.as_str())
            .collect();
        assert_eq!(names, ["first", "second"]);
    }

    #[test]
    fn a_setting_for_a_publisher_nobody_asked_for_is_an_error() {
        let e = try_parse(&[
            "--publisher",
            "object-store",
            "--publisher-setting",
            "objectstore:bucket=b",
        ])
        .unwrap_err();
        assert!(e.contains("objectstore"), "{e}");
        let e = try_parse(&["--publisher-setting", "no-colon-or-equals"]).unwrap_err();
        assert!(e.contains("PUBLISHER:KEY=VALUE"), "{e}");
    }

    #[test]
    fn a_setting_value_may_contain_the_separators() {
        // Split at the *first* colon and the *first* equals, so a URL or a
        // key=value payload survives being a setting value.
        let c = parse(&[
            "--publisher",
            "object-store",
            "--publisher-setting",
            "object-store:endpoint=https://host:9000/?a=b",
        ]);
        assert_eq!(
            c.publisher("object-store").unwrap().settings["endpoint"],
            "https://host:9000/?a=b"
        );
    }

    #[test]
    fn a_timeout_scale_that_is_not_a_number_is_a_usage_error() {
        let e = clap_command()
            .try_get_matches_from(["termproof", "--timeout-scale", "slow"])
            .unwrap_err();
        assert_eq!(e.kind(), clap::error::ErrorKind::ValueValidation);
    }

    #[test]
    fn no_flag_carries_a_default_value() {
        // The rule the whole precedence rests on, asserted structurally rather
        // than trusted: a `default_value` here would make that flag
        // indistinguishable from one the caller passed, and the config file
        // could never override it.
        for arg in clap_command().get_arguments() {
            assert!(
                arg.get_default_values().is_empty(),
                "--{} has a default value; the built-in belongs in `builtin`, not in clap",
                arg.get_id()
            );
        }
    }

    #[test]
    fn a_consumer_can_add_its_own_flags() {
        let command = augment_args(Command::new("validator"))
            .arg(Arg::new("watch").long("watch").action(ArgAction::SetTrue));
        // It is the consumer's command that comes back, not one of ours with
        // the consumer's flags bolted on — the name, and with it the about,
        // version and subcommands, is still theirs.
        assert_eq!(command.get_name(), "validator");
        let matches = command.get_matches_from(["validator", "--watch", "--priority", "P0"]);
        assert!(matches.get_flag("watch"));
        assert_eq!(
            from_matches(&matches).unwrap().discovery.select,
            Some(Selection::Priority("P0".into()))
        );
    }

    #[test]
    fn the_run_config_flag_reads_the_file_it_names() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("run.yaml");
        std::fs::write(&path, "execution:\n  renderer: from-file\n").unwrap();
        let path = path.to_str().unwrap();

        let c = resolve(&matches_of(&["--run-config", path]), &RunConfig::default()).unwrap();
        assert_eq!(c.execution.renderer.as_deref(), Some("from-file"));

        // And the flag still wins over the file it was read from.
        let c = resolve(
            &matches_of(&["--run-config", path, "--renderer", "from-flag"]),
            &RunConfig::default(),
        )
        .unwrap();
        assert_eq!(c.execution.renderer.as_deref(), Some("from-flag"));
    }

    #[test]
    fn a_missing_run_config_says_which_file() {
        let e = resolve(
            &matches_of(&["--run-config", "/nonexistent/run.yaml"]),
            &RunConfig::default(),
        )
        .unwrap_err();
        assert!(e.contains("/nonexistent/run.yaml"), "{e}");
    }

    #[test]
    fn with_no_run_config_the_configured_layer_is_simply_empty() {
        assert_eq!(configured(&matches_of(&[])).unwrap(), RunConfig::default());
    }

    #[test]
    fn the_bare_command_and_an_augmented_one_carry_the_same_flags() {
        // `clap_command` is `augment_args` over a bare command, so this cannot
        // drift — which is the point of it not being a second builder.
        let bare: Vec<_> = clap_command()
            .get_arguments()
            .map(|a| a.get_id().to_string())
            .collect();
        let augmented: Vec<_> = augment_args(Command::new("other"))
            .get_arguments()
            .map(|a| a.get_id().to_string())
            .collect();
        assert_eq!(bare, augmented);
    }

    #[test]
    fn the_command_is_internally_consistent() {
        // clap's own audit of ids, groups and conflicts. It panics rather than
        // returning, and only in debug builds, which is where tests run.
        clap_command().debug_assert();
    }
}