use clap::Command;
// Deliberately reached through clap_usage, proving a consumer needs no
// direct usage-lib dependency to annotate the spec it gets back.
use clap_usage::usage::SpecCommandEffect;

/// The reason `spec` exists: `generate` writes straight to a writer, so there
/// is no way to set something clap cannot express, such as `effect=`.
#[test]
fn spec_can_be_annotated_before_rendering() {
    let mut cmd = Command::new("mycli")
        .subcommand(Command::new("ls").about("List things"))
        .subcommand(Command::new("rm").about("Remove things"));

    let mut spec = clap_usage::spec(&mut cmd, "mycli");
    assert_eq!(spec.bin, "mycli");

    spec.cmd.subcommands.get_mut("ls").unwrap().effect = Some(SpecCommandEffect::Read);
    spec.cmd.subcommands.get_mut("rm").unwrap().effect = Some(SpecCommandEffect::Destructive);

    let rendered = spec.to_string();
    assert!(rendered.contains("cmd ls"), "{rendered}");
    assert!(rendered.contains("effect=read"), "{rendered}");
    assert!(rendered.contains("effect=destructive"), "{rendered}");
}

/// `generate` must keep producing exactly what it always did.
#[test]
fn generate_still_writes_the_header_and_spec() {
    let mut cmd = Command::new("mycli").subcommand(Command::new("ls"));
    let mut buf = vec![];
    clap_usage::generate(&mut cmd, "mycli", &mut buf);
    let out = String::from_utf8(buf).unwrap();
    assert!(out.starts_with("// @generated by usage-cli from clap metadata\n"));
    assert!(out.contains("bin mycli"));
}
