use std::process::{Command, Output};

fn run(args: &[&str]) -> Output {
    Command::new(env!("CARGO_BIN_EXE_kcan"))
        .args(args)
        .output()
        .expect("kcan binary should run")
}

fn stdout(output: Output) -> String {
    assert!(output.status.success(), "status={:?}", output.status.code());
    String::from_utf8(output.stdout).expect("stdout should be UTF-8")
}

#[test]
fn cli_help_mentions_main_entry_points() {
    let output = stdout(run(&["help"]));

    assert!(output.contains("Knott Dynamics"));
    assert!(output.contains("Scrin"));
    assert!(output.contains("Aisling"));
    assert!(output.contains("kcan models"));
}

#[test]
fn cli_lists_supported_models() {
    let output = stdout(run(&["models"]));

    assert!(output.contains("AK100-33"));
    assert!(output.contains("RS-06"));
}

#[test]
fn cli_builds_cubemars_frames() {
    let mit = stdout(run(&["cubemars", "mit", "ak60-6", "0x03"]));
    assert!(mit.contains("extended 0x803 len=8"));

    let direct = stdout(run(&["cubemars", "direct", "0x03", "velocity", "1000"]));
    assert!(direct.contains("extended 0x303 len=4 data=00 00 03 E8"));
}

#[test]
fn cli_builds_robstride_frames() {
    let status = stdout(run(&["robstride", "status", "rs01", "1"]));
    assert!(status.contains("extended 0x501 len=8"));

    let speed = stdout(run(&["robstride", "speed", "rs03", "3", "1.5", "12.0"]));
    assert!(speed.contains("extended 0x403 len=8"));
}

#[test]
fn cli_rejects_bad_commands() {
    let output = run(&["cubemars", "direct", "0x03", "velocity"]);

    assert!(!output.status.success());
    let stderr = String::from_utf8(output.stderr).expect("stderr should be UTF-8");
    assert!(stderr.contains("error:"));
}
