vc-cli 0.1.0

Command-line interface for Visual Compiler
//! Smoke tests for the CLI.

use anyhow::Result;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;
use tempfile::tempdir;

/// Returns the path to the `vc` binary.
fn vc_binary() -> PathBuf {
    // CARGO_BIN_EXE_vc is set by Cargo when building the binary.
    // This is more reliable than constructing the path manually.
    env!("CARGO_BIN_EXE_vc").into()
}

/// Generates a valid compiler_output.json using `vc demo`.
fn generate_compiler_output(build_dir: &Path) -> Result<()> {
    let output = Command::new(vc_binary()).arg("demo").arg("--out").arg(build_dir).output()?;

    assert!(output.status.success());
    Ok(())
}

/// Writes a minimal valid config TOML for the stub backend.
fn write_stub_config(config_path: &Path) -> Result<()> {
    let content = r#"
[global]

[profiles.default]
backend_name = "stub"

[backends.stub]
enabled = true

[backends.stub.options]
kind = "dummy"

[backends.stub.options.options]
"#;
    fs::write(config_path, content)?;
    Ok(())
}

#[test]
fn cli_demo_and_run() -> Result<()> {
    let temp = tempdir()?;
    let build_dir = temp.path().join("build");
    let run_dir = temp.path().join("run");

    generate_compiler_output(&build_dir)?;

    let config_path = temp.path().join("config.toml");
    write_stub_config(&config_path)?;

    let run_output = Command::new(vc_binary())
        .arg("run")
        .arg("--input")
        .arg(&build_dir)
        .arg("--out")
        .arg(&run_dir)
        .arg("--config")
        .arg(&config_path)
        .arg("--profile")
        .arg("default")
        .output()?;

    assert!(run_output.status.success());

    let runtime_path = run_dir.join("runtime.json");
    assert!(runtime_path.exists());

    Ok(())
}

#[test]
fn cli_run_fails_with_missing_config() -> Result<()> {
    let temp = tempdir()?;
    let build_dir = temp.path().join("build");
    let run_dir = temp.path().join("run");

    generate_compiler_output(&build_dir)?;

    let missing_config = temp.path().join("missing.toml");
    let run_output = Command::new(vc_binary())
        .arg("run")
        .arg("--input")
        .arg(&build_dir)
        .arg("--out")
        .arg(&run_dir)
        .arg("--config")
        .arg(&missing_config)
        .arg("--profile")
        .arg("default")
        .output()?;

    assert!(!run_output.status.success());
    Ok(())
}