usage-dynamic 7.1.2

Runtime command catalogs for usage-rs applications
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
use std::ffi::{OsStr, OsString};

use futures::executor::block_on;
use usage_dynamic::{Catalog, Error, Outcome, Spec};
use usage_rs::{Cli, Subcommands};

#[derive(Cli)]
#[usage(bin = "host")]
struct Host {
    #[usage(subcommand)]
    command: HostCommand,
}

#[derive(Subcommands)]
enum HostCommand {
    /// Built into the host.
    Builtin,
    /// Manage plugins.
    #[usage(alias = "p")]
    Plugins {
        #[usage(subcommand)]
        command: PluginCommand,
    },
}

#[derive(Subcommands)]
enum PluginCommand {
    /// A static plugin operation.
    #[usage(alias = "ls")]
    List,
    #[usage(external_subcommand)]
    External(Vec<OsString>),
}

/// A host with a value flag a runtime completer answers for.
#[derive(Cli)]
#[usage(bin = "themehost")]
#[allow(dead_code)]
struct ThemeHost {
    /// Which theme to use.
    #[usage(long)]
    theme: Option<String>,
    #[usage(subcommand)]
    command: ThemeCommand,
}

#[derive(Subcommands)]
#[allow(dead_code)]
enum ThemeCommand {
    Plugins {
        #[usage(subcommand)]
        command: ThemePluginCommand,
    },
}

#[derive(Subcommands)]
#[allow(dead_code)]
enum ThemePluginCommand {
    List,
    #[usage(external_subcommand)]
    External(Vec<OsString>),
}

/// A host flag, declared where runtime commands also appear.
#[derive(Cli)]
#[usage(bin = "flaghost")]
#[allow(dead_code)]
struct FlagHost {
    #[usage(subcommand)]
    command: FlagHostCommand,
}

#[derive(Subcommands)]
#[allow(dead_code)]
enum FlagHostCommand {
    Plugins {
        /// Say less.
        #[usage(long)]
        quiet: bool,
        #[usage(subcommand)]
        command: FlagPluginCommand,
    },
}

#[derive(Subcommands)]
#[allow(dead_code)]
enum FlagPluginCommand {
    /// A static plugin operation.
    List,
    #[usage(external_subcommand)]
    External(Vec<OsString>),
}

#[derive(Cli)]
#[usage(bin = "root-host")]
#[allow(dead_code)]
struct RootHost {
    #[usage(subcommand)]
    command: RootCommand,
}

#[derive(Subcommands)]
#[allow(dead_code)]
enum RootCommand {
    Known,
    #[usage(external_subcommand)]
    External(Vec<String>),
}

#[derive(Cli)]
#[usage(bin = "closed")]
#[allow(dead_code)]
struct Closed {
    #[usage(subcommand)]
    command: ClosedCommand,
}

#[derive(Subcommands)]
enum ClosedCommand {
    Child,
}

fn plugin(name: &str, extra: &str) -> Spec {
    format!("name \"{name}\"\nbin \"{name}\"\n{extra}")
        .parse()
        .unwrap()
}

fn catalog() -> usage_dynamic::Catalog<'static> {
    let mut formatter = plugin(
        "formatter",
        r#"
version "2.4"
unknown_flags "error"
flag "--color <WHEN>" env="USAGE_DYNAMIC_TEST_COLOR" default="always" {
    choices "always" "never"
}
arg "[path]"
cmd "check" help="Check formatting" {
    flag "--fix" help="Apply fixes"
}
"#,
    );
    formatter.about = Some("Format a project".into());
    formatter.about_long = Some("Format a project using its configured style.".into());
    formatter.cmd.help_heading = Some("Installed plugins".into());
    formatter.cmd.display_order = Some(2);
    formatter.cmd.aliases = vec!["fmt".into()];
    formatter.cmd.hidden_aliases = vec!["oldfmt".into()];
    let mut audit = plugin("audit", "");
    audit.about = Some("Audit a project".into());
    audit.cmd.help_heading = Some("Installed plugins".into());
    audit.cmd.display_order = Some(1);
    Catalog::builder(Host::app())
        .under("p", formatter)
        .under("plugins", audit)
        .build()
        .unwrap()
}

#[test]
fn root_summaries_appear_in_help_and_completion() {
    let mut spec = plugin("doctor", "");
    spec.about = Some("Inspect plugin health".into());
    let static_kdl = RootHost::app().to_kdl();
    let catalog = Catalog::builder(RootHost::app())
        .root(spec)
        .build()
        .unwrap();
    assert_eq!(static_kdl, RootHost::app().to_kdl());
    let help = catalog.app().unwrap().help("", false).unwrap();
    assert!(help.contains("doctor  Inspect plugin health"), "{help}");
    assert!(
        catalog
            .app()
            .unwrap()
            .help("doctor", false)
            .unwrap()
            .contains("Inspect plugin health")
    );

    let argv = [
        OsString::from("__complete_word__"),
        OsString::from("--words"),
        OsString::from("root-host"),
        OsString::new(),
    ];
    let answer = block_on(catalog.app().unwrap().completion_request(&argv)).unwrap();
    assert!(
        answer.lines().any(|line| line.starts_with("doctor")),
        "{answer}"
    );
    let RootCommand::External(argv) = RootHost::parse_from(&[OsStr::new("doctor")])
        .unwrap()
        .command
    else {
        panic!("expected top-level external command")
    };
    let argv: Vec<OsString> = argv.into_iter().map(OsString::from).collect();
    assert!(matches!(
        catalog.parse_external("", &argv).unwrap(),
        Some(Outcome::Parsed(_))
    ));
}

#[test]
fn nested_help_merges_summaries_aliases_headings_and_ordering() {
    let catalog = catalog();
    let app = catalog.app().unwrap();
    for long in [false, true] {
        let help = app.help("plugins", long).unwrap();
        assert!(help.contains("Installed plugins:"), "{help}");
        assert!(help.contains("  formatter  Format a project"), "{help}");
        assert!(help.contains("[aliases: fmt]"), "{help}");
        assert!(!help.contains("oldfmt"), "{help}");
        assert!(
            help.find("  audit").unwrap() < help.find("  formatter").unwrap(),
            "{help}"
        );
    }
    let plugin_help = app.help("plugins formatter", false).unwrap();
    assert!(plugin_help.contains("--color"), "{plugin_help}");
    assert!(plugin_help.contains("\n  check"), "{plugin_help}");
    let nested_help = app.help("plugins fmt check", false).unwrap();
    assert!(nested_help.contains("--fix"), "{nested_help}");
}

#[test]
fn command_completion_offers_visible_names_and_delegates_after_selection() {
    let catalog = catalog();
    let app = catalog.app().unwrap();
    let parsed = usage_parser::parse::parse_partial(
        app.spec(),
        &["host".into(), "plugins".into(), "formatter".into()],
    )
    .unwrap();
    assert_eq!(parsed.cmd.name, "formatter", "{:?}", parsed.cmds);
    let request = |words: &[&str]| {
        let mut argv = vec![
            OsString::from("__complete_word__"),
            OsString::from("--words"),
        ];
        argv.extend(words.iter().map(OsString::from));
        block_on(app.completion_request(&argv)).unwrap()
    };
    let answer = request(&["host", "plugins", ""]);
    assert!(
        answer.lines().any(|line| line.starts_with("formatter")),
        "{answer}"
    );
    assert!(
        answer.lines().any(|line| line.starts_with("fmt")),
        "{answer}"
    );
    assert!(!answer.contains("oldfmt"), "{answer}");
    let flags = request(&["host", "plugins", "formatter", "--"]);
    assert!(
        flags.lines().any(|line| line.starts_with("--color")),
        "{flags}"
    );
    let nested = request(&["host", "plugins", "formatter", "ch"]);
    assert!(
        nested.lines().any(|line| line.starts_with("check")),
        "{nested}"
    );
}

#[test]
fn canonical_alias_unknown_help_version_and_invalid_argv_are_typed() {
    let catalog = catalog();
    let parsed = catalog
        .parse_external("p", &[OsString::from("fmt"), OsString::from("project")])
        .unwrap()
        .unwrap();
    let Outcome::Parsed(parsed) = parsed else {
        panic!("expected parsed")
    };
    assert_eq!(parsed.parent, "plugins");
    assert_eq!(parsed.name, "formatter");
    assert_eq!(parsed.invoked_as, "fmt");
    assert!(!parsed.output.tokens.is_empty());
    assert!(!parsed.output.flags.is_empty(), "default should be applied");

    // FIXME: Audit that the environment access only happens in single-threaded code.
    unsafe { std::env::set_var("USAGE_DYNAMIC_TEST_COLOR", "never") };
    let from_env = catalog
        .parse_external("plugins", &[OsString::from("formatter")])
        .unwrap()
        .unwrap();
    // FIXME: Audit that the environment access only happens in single-threaded code.
    unsafe { std::env::remove_var("USAGE_DYNAMIC_TEST_COLOR") };
    let Outcome::Parsed(from_env) = from_env else {
        panic!("expected parsed")
    };
    assert!(
        from_env
            .output
            .flag_origins
            .values()
            .flatten()
            .any(|origin| format!("{origin:?}").contains("Env"))
    );

    assert!(
        catalog
            .parse_external("plugins", &[OsString::from("missing")])
            .unwrap()
            .is_none()
    );
    // A hidden alias dispatches like any other spelling; hiding is a help/completion property.
    let hidden = catalog
        .parse_external("plugins", &[OsString::from("oldfmt")])
        .unwrap()
        .unwrap();
    let Outcome::Parsed(hidden) = hidden else {
        panic!("expected parsed")
    };
    assert_eq!(hidden.name, "formatter");
    assert_eq!(hidden.invoked_as, "oldfmt");
    let help = catalog
        .parse_external(
            "plugins",
            &[OsString::from("formatter"), OsString::from("--help")],
        )
        .unwrap();
    assert!(matches!(help, Some(Outcome::Help(_))), "{help:?}");
    assert!(matches!(
        catalog
            .parse_external(
                "plugins",
                &[OsString::from("formatter"), OsString::from("--version")]
            )
            .unwrap(),
        Some(Outcome::Version(_))
    ));
    assert!(matches!(
        catalog.parse_external(
            "plugins",
            &[OsString::from("formatter"), OsString::from("--bogus")]
        ),
        Err(Error::Parse(_)) | Err(Error::InvalidArgv(_))
    ));
}

#[test]
fn derived_external_variant_receives_the_same_argv_the_catalog_parses() {
    let host =
        Host::parse_from(&[OsStr::new("plugins"), OsStr::new("fmt"), OsStr::new("src")]).unwrap();
    let HostCommand::Plugins {
        command: PluginCommand::External(argv),
    } = host.command
    else {
        panic!("expected nested external command")
    };
    assert!(matches!(
        catalog().parse_external("plugins", &argv).unwrap(),
        Some(Outcome::Parsed(_))
    ));
}

#[test]
fn validation_rejects_bad_parents_namespaces_and_mounts() {
    assert!(matches!(
        Catalog::builder(Host::app())
            .under("absent", plugin("x", ""))
            .build(),
        Err(Error::MissingParent(_))
    ));
    assert!(matches!(
        Catalog::builder(Closed::app())
            .under("child", plugin("x", ""))
            .build(),
        Err(Error::ParentNotExternal(_))
    ));
    assert!(matches!(
        Catalog::builder(Host::app())
            .under("plugins", plugin("", ""))
            .build(),
        Err(Error::EmptyName)
    ));
    for name in ["list", "ls", "help"] {
        assert!(matches!(
            Catalog::builder(Host::app())
                .under("plugins", plugin(name, ""))
                .build(),
            Err(Error::Collision { .. })
        ));
    }
    let mut alias_collision = plugin("one", "");
    alias_collision.cmd.aliases.push("same".into());
    assert!(matches!(
        Catalog::builder(Host::app())
            .under("plugins", alias_collision)
            .under("plugins", plugin("same", ""))
            .build(),
        Err(Error::Collision { .. })
    ));
    assert!(matches!(
        Catalog::builder(Host::app())
            .under("plugins", plugin("mounted", "mount run=\"discover\"\n"))
            .build(),
        Err(Error::UnresolvedMount(_))
    ));
}

#[cfg(unix)]
#[test]
fn non_utf8_reports_the_external_token_position() {
    use std::os::unix::ffi::OsStringExt;
    let error = catalog()
        .parse_external(
            "plugins",
            &[OsString::from("formatter"), OsString::from_vec(vec![0xff])],
        )
        .unwrap_err();
    assert!(matches!(error, Error::NonUtf8 { index: 1 }));
}

/// The answer to a completion request, as the lines a shell would read.
fn answer(catalog: &Catalog<'_>, words: &[&str]) -> Vec<String> {
    let mut argv = vec![
        OsString::from("__complete_word__"),
        OsString::from("--words"),
    ];
    argv.extend(words.iter().map(OsString::from));
    block_on(catalog.app().unwrap().completion_request(&argv))
        .unwrap()
        .lines()
        .map(|line| line.split('\t').next().unwrap_or_default().to_string())
        .collect()
}

#[test]
fn a_catalogued_name_is_offered_where_a_subcommand_belongs() {
    let catalog = catalog();
    let offered = answer(&catalog, &["host", "plugins", ""]);
    // Static commands and runtime ones, in one list: a user typing here cannot tell which is
    // which, and should not have to.
    for name in ["list", "ls", "audit", "formatter", "fmt"] {
        assert!(offered.contains(&name.to_string()), "{name}: {offered:?}");
    }
    // A hidden alias answers but is never advertised — the same rule static ones follow.
    assert!(!offered.contains(&"oldfmt".to_string()), "{offered:?}");
    // One list, sorted and deduplicated, rather than two concatenated.
    let mut sorted = offered.clone();
    sorted.sort();
    sorted.dedup();
    assert_eq!(offered, sorted, "{offered:?}");

    // A prefix narrows runtime names as it narrows static ones.
    assert_eq!(answer(&catalog, &["host", "plugins", "fo"]), ["formatter"]);
}

#[test]
fn a_flag_position_is_the_hosts_alone() {
    // Runtime commands are commands. Where a flag is what could be typed, the host's tables are
    // the whole answer and a plugin name would be a word that cannot go there.
    let catalog = Catalog::builder(FlagHost::app())
        .under("plugins", plugin("formatter", ""))
        .build()
        .unwrap();
    let offered = answer(&catalog, &["flaghost", "plugins", "--"]);
    assert!(offered.iter().any(|line| line == "--quiet"), "{offered:?}");
    assert!(!offered.contains(&"formatter".to_string()), "{offered:?}");
}

#[test]
fn completion_descends_into_a_plugins_own_spec() {
    let catalog = catalog();
    // Its subcommands, and not the sibling static ones it is being completed alongside.
    let offered = answer(&catalog, &["host", "plugins", "formatter", ""]);
    assert!(offered.contains(&"check".to_string()), "{offered:?}");
    assert!(!offered.contains(&"list".to_string()), "{offered:?}");

    // Its flags.
    let flags = answer(&catalog, &["host", "plugins", "formatter", "--"]);
    assert!(flags.contains(&"--color".to_string()), "{flags:?}");

    // Its nested command's flags, reached through an alias of its own.
    let nested = answer(&catalog, &["host", "plugins", "fmt", "check", "--f"]);
    assert!(nested.contains(&"--fix".to_string()), "{nested:?}");

    // And its declared choices, which are the whole answer: a mistyped one is no matches, not
    // an invitation to complete a path.
    let choices = answer(&catalog, &["host", "plugins", "formatter", "--color", ""]);
    assert_eq!(choices, ["always", "never"], "{choices:?}");
    let mistyped = answer(&catalog, &["host", "plugins", "formatter", "--color", "zz"]);
    assert!(mistyped.is_empty(), "{mistyped:?}");
}

#[test]
fn a_hidden_alias_descends_without_being_advertised() {
    let catalog = catalog();
    let offered = answer(&catalog, &["host", "plugins", "oldfmt", ""]);
    assert!(offered.contains(&"check".to_string()), "{offered:?}");
}

#[test]
fn an_uncatalogued_name_is_answered_with_nothing() {
    // The host has no idea what `unknownthing` is, and neither does this catalog. Offering the
    // parent's static subcommands there would complete a line that runs something else, and
    // offering the working directory would claim a path belongs to a program nobody can ask.
    let catalog = catalog();
    let offered = answer(&catalog, &["host", "plugins", "unknownthing", ""]);
    assert!(offered.is_empty(), "{offered:?}");
    let raw = {
        let argv = [
            OsString::from("__complete_word__"),
            OsString::from("--words"),
            OsString::from("host"),
            OsString::from("plugins"),
            OsString::from("unknownthing"),
            OsString::new(),
        ];
        block_on(catalog.app().unwrap().completion_request(&argv)).unwrap()
    };
    assert!(!raw.contains('\u{1}'), "no path fallback either: {raw:?}");
}

#[test]
fn a_host_flag_before_the_name_does_not_move_the_boundary() {
    let catalog = Catalog::builder(FlagHost::app())
        .under("plugins", plugin("formatter", "cmd \"check\" {}\n"))
        .build()
        .unwrap();
    let offered = answer(
        &catalog,
        &["flaghost", "plugins", "--quiet", "formatter", ""],
    );
    assert!(offered.contains(&"check".to_string()), "{offered:?}");
}

#[test]
fn a_line_and_a_word_list_are_the_same_question() {
    // Two ways in to the same protocol: elvish hands over pre-split words, the others a line
    // and a cursor. A host answering half the line itself must not make them disagree.
    let catalog = catalog();
    let by_words = answer(&catalog, &["host", "plugins", "formatter", ""]);
    let by_line = {
        let argv = [
            OsString::from("__complete_word__"),
            OsString::from("--line"),
            OsString::from("host plugins formatter "),
        ];
        block_on(catalog.app().unwrap().completion_request(&argv))
            .unwrap()
            .lines()
            .map(|line| line.split('\t').next().unwrap_or_default().to_string())
            .collect::<Vec<_>>()
    };
    assert_eq!(by_words, by_line);

    // A cursor before the end completes what is under it, and the words after it are not part
    // of the question.
    let mid = {
        let line = "host plugins formatter check";
        let argv = [
            OsString::from("__complete_word__"),
            OsString::from("--line"),
            OsString::from(line),
            OsString::from("--cursor"),
            OsString::from("21"),
        ];
        block_on(catalog.app().unwrap().completion_request(&argv))
            .unwrap()
            .lines()
            .map(|line| line.split('\t').next().unwrap_or_default().to_string())
            .collect::<Vec<_>>()
    };
    assert_eq!(mid, ["formatter"], "completing `form⌶` mid-line");
}

#[test]
fn an_open_position_still_defers_to_the_shells_paths() {
    // The tightening is about positions that state their own answers. One that has nothing to
    // say still hands over to the shell, or completing a plugin's file argument would offer
    // nothing at all. `check` declares a flag and no words of its own, so a bare word there is
    // the filesystem's to answer.
    let catalog = catalog();
    let argv = [
        OsString::from("__complete_word__"),
        OsString::from("--words"),
        OsString::from("host"),
        OsString::from("plugins"),
        OsString::from("formatter"),
        OsString::from("check"),
        OsString::new(),
    ];
    let raw = block_on(catalog.app().unwrap().completion_request(&argv)).unwrap();
    assert!(raw.contains("\u{1}files"), "{raw:?}");
}

#[test]
fn a_plugins_complete_inside_an_arg_wins_over_a_named_one() {
    // The named completer says directories; the one inside the arg says nothing belongs there.
    // The one inside the arg is attached to it, so the position closes with no path fallback.
    let catalog = Catalog::builder(Host::app())
        .under(
            "plugins",
            plugin(
                "formatter",
                r#"
complete "target" type="dir"
arg "<target>" {
    complete type="none"
}
cmd "named" {
    arg "<target>"
}
"#,
            ),
        )
        .build()
        .unwrap();
    let request = |words: &[&str]| {
        let mut argv = vec![
            OsString::from("__complete_word__"),
            OsString::from("--words"),
        ];
        argv.extend(words.iter().map(OsString::from));
        block_on(catalog.app().unwrap().completion_request(&argv)).unwrap()
    };
    let inline = request(&["host", "plugins", "formatter", "zz"]);
    assert!(!inline.contains('\u{1}'), "{inline:?}");
    // Where the arg has none of its own, the named one still answers.
    let named = request(&["host", "plugins", "formatter", "named", ""]);
    assert!(named.contains('\u{1}'), "{named:?}");
}

#[test]
fn dispatch_never_builds_the_merged_tree() {
    // The point of the split: running a plugin command is the hot path, and the merged tree is
    // a KDL round trip of the whole host spec. A catalog that is only ever dispatched through
    // must never assemble one — which is observable, because `app()` is what assembles it.
    let catalog = catalog();
    assert!(matches!(
        catalog
            .parse_external("plugins", &[OsString::from("fmt")])
            .unwrap(),
        Some(Outcome::Parsed(_))
    ));
    assert!(!catalog.is_assembled());
    catalog.app().unwrap();
    assert!(catalog.is_assembled());
}

/// A runtime completer of the host's own, of the kind `completions` registers.
fn theme_values(
    _: &usage_rs::complete::CompleteCtx<'_>,
) -> Vec<usage_rs::complete::Candidate<'static>> {
    vec![usage_rs::complete::Candidate::new("solarized")]
}

fn theme_values_async(
    ctx: usage_rs::complete::CompleteCtx<'_>,
) -> usage_rs::complete::CompletionFuture<'_> {
    let _ = ctx;
    Box::pin(async { vec![usage_rs::complete::Candidate::new("midnight")] })
}

static OVERLAYS: [usage_rs::complete::CompletionOverlay<'static>; 1] =
    [usage_rs::complete::CompletionOverlay::sync_any(
        "theme",
        theme_values,
    )];

static ASYNC_OVERLAYS: [usage_rs::complete::CompletionOverlay<'static>; 1] =
    [usage_rs::complete::CompletionOverlay::async_any(
        "theme",
        theme_values_async,
    )];

#[test]
fn the_hosts_own_completers_still_answer_for_the_hosts_words() {
    // The catalog answers part of the line, not all of it. Everything the host registered has
    // to keep working, or adopting runtime commands would quietly cost a CLI its completions.
    for (overlays, expected) in [
        (&OVERLAYS[..], "solarized"),
        (&ASYNC_OVERLAYS[..], "midnight"),
    ] {
        let catalog = Catalog::builder(ThemeHost::app())
            .completions(overlays)
            .under("plugins", plugin("formatter", ""))
            .build()
            .unwrap();
        let argv = [
            OsString::from("__complete_word__"),
            OsString::from("--line"),
            OsString::from("themehost --theme "),
        ];
        let rendered = block_on(catalog.app().unwrap().completion_request(&argv)).unwrap();
        assert!(rendered.contains(expected), "{rendered:?}");
    }
}

#[test]
fn a_named_completer_request_is_answered_by_the_host() {
    // What a spec's `run=` line asks for: one named completer's values rather than everything
    // the cursor could take. The request has its own flag, and a host that dropped it would
    // answer a different question than the KDL it emitted promised.
    let catalog = Catalog::builder(ThemeHost::app())
        .completions(&OVERLAYS)
        .under("plugins", plugin("formatter", ""))
        .build()
        .unwrap();
    let argv = [
        OsString::from("__complete_word__"),
        OsString::from("--candidates"),
        OsString::from("theme"),
        OsString::from("--line"),
        OsString::from("themehost "),
    ];
    let rendered = block_on(catalog.app().unwrap().completion_request(&argv)).unwrap();
    assert!(rendered.contains("solarized"), "{rendered:?}");

    // Past a runtime command there is nothing to run it with: a `run=` completer is a
    // subprocess, and this crate spawns none.
    let argv = [
        OsString::from("__complete_word__"),
        OsString::from("--candidates"),
        OsString::from("theme"),
        OsString::from("--line"),
        OsString::from("themehost plugins formatter "),
    ];
    let rendered = block_on(catalog.app().unwrap().completion_request(&argv)).unwrap();
    assert!(rendered.trim().is_empty(), "{rendered:?}");
}

#[test]
fn a_builtin_never_needs_a_catalog_and_a_fallback_needs_one_spec() {
    // The design's cost promise: parse first, against the static tables alone. A built-in
    // command dispatches before any catalog exists, so an application pays for discovery
    // only when the catch-all actually fired — and then only for the specs it chose to load.
    let host = Host::parse_from(&[OsStr::new("builtin")]).unwrap();
    assert!(matches!(host.command, HostCommand::Builtin));
    // No catalog was constructed on that path at all.

    let host = Host::parse_from(&[
        OsStr::new("plugins"),
        OsStr::new("formatter"),
        OsStr::new("src"),
    ])
    .unwrap();
    let HostCommand::Plugins {
        command: PluginCommand::External(captured),
    } = host.command
    else {
        panic!("expected the catch-all")
    };
    // Load exactly one plugin's spec, after the parse decided one is needed.
    let catalog = Catalog::builder(Host::app())
        .under("plugins", plugin("formatter", "arg \"[path]\"\n"))
        .build()
        .unwrap();
    assert!(matches!(
        catalog.parse_external("plugins", &captured).unwrap(),
        Some(Outcome::Parsed(_))
    ));
    assert!(!catalog.is_assembled(), "dispatch built no merged tree");

    // A name the loaded specs do not answer to falls through, same as always.
    let catalog = Catalog::builder(Host::app())
        .under("plugins", plugin("audit", ""))
        .build()
        .unwrap();
    assert!(
        catalog
            .parse_external("plugins", &captured)
            .unwrap()
            .is_none()
    );
}

#[test]
fn a_spec_built_in_rust_works_like_one_parsed_from_kdl() {
    // KDL is the contract with out-of-process plugins. Commands the application itself defines
    // at runtime — tasks from a config file, say — should not have to render KDL text just so
    // the catalog can parse it back. A `Spec` assembled with the builders must behave
    // identically.
    use usage_dynamic::{SpecArgBuilder, SpecCommandBuilder, SpecFlagBuilder};

    let task: Spec = SpecCommandBuilder::new()
        .name("deploy")
        .help("Deploy the project")
        .flag(
            SpecFlagBuilder::new()
                .name("env")
                .long("env")
                .arg(SpecArgBuilder::new().name("ENV").build())
                .help("Target environment")
                .build(),
        )
        .subcommand(
            SpecCommandBuilder::new()
                .name("status")
                .help("Show deploy status")
                .build(),
        )
        .build()
        .into();
    assert_eq!(task.name, "deploy");
    assert_eq!(task.about.as_deref(), Some("Deploy the project"));

    let catalog = Catalog::builder(Host::app())
        .under("plugins", task)
        .build()
        .unwrap();

    // Dispatch parses against the built spec, help/version included.
    let parsed = catalog
        .parse_external(
            "plugins",
            &[
                OsString::from("deploy"),
                OsString::from("--env"),
                OsString::from("prod"),
            ],
        )
        .unwrap()
        .unwrap();
    let Outcome::Parsed(parsed) = parsed else {
        panic!("expected parsed")
    };
    assert!(!parsed.output.flags.is_empty());
    let help = catalog
        .parse_external(
            "plugins",
            &[OsString::from("deploy"), OsString::from("--help")],
        )
        .unwrap()
        .unwrap();
    let Outcome::Help(help) = help else {
        panic!("expected help")
    };
    // The page's usage line reflects where the command sits, same as a KDL-parsed spec's would.
    assert!(help.page.contains("deploy"), "{}", help.page);
    assert!(help.page.contains("status"), "{}", help.page);
    assert!(help.page.contains("--env"), "{}", help.page);

    // Merged help and completion see it too.
    let app = catalog.app().unwrap();
    assert!(app.help("plugins deploy", false).unwrap().contains("--env"));
    let offered = answer(&catalog, &["host", "plugins", "deploy", ""]);
    assert!(offered.contains(&"status".to_string()), "{offered:?}");
}