//! Floats milestone iter 4.6 — end-to-end fixture.
//!
//! `examples/floats.ail.json` is built via the public `ail build`
//! CLI and run; stdout is matched against the expected three
//! lines (`4`, `42`, `-1.5`). This test exercises the full
//! pipeline — Float literal lowering, polymorphic `+` Float arm,
//! `int_to_float` (sitofp), `neg` Float (fneg), `io/print_float`
//! (printf `%g\n`).

use std::path::PathBuf;
use std::process::Command;

fn workspace_root() -> PathBuf {
    PathBuf::from(env!("CARGO_MANIFEST_DIR"))
        .parent().unwrap()
        .parent().unwrap()
        .to_path_buf()
}

#[test]
fn floats_example_prints_expected_stdout() {
    let root = workspace_root();
    let example = root.join("examples").join("floats.ail.json");
    assert!(example.exists(), "expected fixture at {:?}", example);

    // Build via `cargo run -p ail -- build` to use the in-tree CLI.
    let target_bin = std::env::temp_dir().join(format!(
        "ailang-floats-e2e-{}",
        std::process::id()
    ));

    let build = Command::new("cargo")
        .current_dir(&root)
        .args([
            "run", "--quiet", "-p", "ail", "--",
            "build",
            example.to_str().unwrap(),
            "-o", target_bin.to_str().unwrap(),
        ])
        .output()
        .expect("cargo run -p ail -- build");
    assert!(
        build.status.success(),
        "ail build failed:\nstdout: {}\nstderr: {}",
        String::from_utf8_lossy(&build.stdout),
        String::from_utf8_lossy(&build.stderr),
    );

    let run = Command::new(&target_bin)
        .output()
        .expect("run floats binary");
    assert!(
        run.status.success(),
        "floats binary exited non-zero: status {:?}\nstdout: {}\nstderr: {}",
        run.status,
        String::from_utf8_lossy(&run.stdout),
        String::from_utf8_lossy(&run.stderr),
    );

    let stdout = String::from_utf8_lossy(&run.stdout);
    let expected = "4\n42\n-1.5\n";
    assert_eq!(
        stdout, expected,
        "stdout mismatch.\nGot:\n{}\nExpected:\n{}",
        stdout, expected,
    );

    let _ = std::fs::remove_file(&target_bin);
}
