vpf-cli 0.2.0

The `vpf` command: check plans in the Verifiable Plan Format and show them in words.
//! The `vpf` command, run as a person runs it.

use std::path::{Path, PathBuf};
use std::process::{Command, Output};

fn root() -> PathBuf {
    Path::new(env!("CARGO_MANIFEST_DIR")).join("../..")
}

fn vpf(args: &[&str]) -> Output {
    Command::new(env!("CARGO_BIN_EXE_vpf"))
        .args(args)
        .current_dir(root())
        .output()
        .expect("vpf runs")
}

fn stdout(output: &Output) -> String {
    String::from_utf8_lossy(&output.stdout).into_owned()
}

#[test]
fn check_passes_a_plan_that_may_run() {
    let output = vpf(&["check", "examples/monthly-sales-report.vpf.json"]);
    assert_eq!(output.status.code(), Some(0));
    assert_eq!(
        stdout(&output),
        "examples/monthly-sales-report.vpf.json: no problems, it may run\n"
    );
}

#[test]
fn check_tells_every_problem_and_fails() {
    let output = vpf(&["check", "conformance/plans/several-problems.vpf.json"]);
    assert_eq!(output.status.code(), Some(1));
    let text = stdout(&output);
    assert!(
        text.starts_with("conformance/plans/several-problems.vpf.json: 5 problems\n"),
        "{text}"
    );
    assert!(
        text.contains(
            "  step.id: Count: An id is not 1 to 24 small letters, digits or -, or is asked or item.\n"
        ),
        "{text}"
    );
    assert!(text.contains("  proof.agent: compare: "), "{text}");
}

#[test]
fn check_takes_several_files_and_fails_if_one_does() {
    let output = vpf(&[
        "check",
        "examples/blog-post.vpf.json",
        "conformance/plans/plan-wish-blank.vpf.json",
    ]);
    assert_eq!(output.status.code(), Some(1));
    assert!(stdout(&output).contains("examples/blog-post.vpf.json: no problems"));
}

#[test]
fn check_answers_in_json() {
    let output = vpf(&[
        "check",
        "--json",
        "conformance/plans/step-do-blank.vpf.json",
    ]);
    assert_eq!(output.status.code(), Some(1));
    let answer: serde_json::Value = serde_json::from_slice(&output.stdout).expect("JSON");
    assert_eq!(
        answer[0]["file"],
        "conformance/plans/step-do-blank.vpf.json"
    );
    assert_eq!(answer[0]["may_run"], false);
    assert_eq!(answer[0]["problems"][0]["reason"], "step.do");
    assert_eq!(answer[0]["problems"][0]["step"], "report");
}

#[test]
fn show_says_the_plan_in_words() {
    let output = vpf(&["show", "examples/monthly-sales-report.vpf.json"]);
    assert_eq!(output.status.code(), Some(0));
    let text = stdout(&output);
    assert!(
        text.starts_with("The wish: The monthly sales report, sent to the team\n"),
        "{text}"
    );
    assert!(text.contains("Step 3 ยท send\n"), "{text}");
}

#[test]
fn show_refuses_a_plan_with_problems() {
    let output = vpf(&["show", "conformance/plans/file-not-json.vpf.json"]);
    assert_eq!(output.status.code(), Some(1));
    assert!(stdout(&output).contains("file.invalid"));
}

#[test]
fn a_missing_file_is_told() {
    let output = vpf(&["check", "no-such-plan.vpf.json"]);
    assert_eq!(output.status.code(), Some(2));
    assert!(String::from_utf8_lossy(&output.stderr).contains("no-such-plan.vpf.json"));
}