//! End-to-end mock-mode test (parent spec §Testing strategy).
//! Runs the harness binary against a canned response file that
//! makes (json, t3_main_prints) green on turn 2 and
//! (ail, t1_add_three) run to the turn limit. Asserts the
//! scores.csv is well-formed and the right per-(cohort,task)
//! artefacts land on disk.

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

fn harness_bin() -> PathBuf { PathBuf::from(env!("CARGO_BIN_EXE_xmodel-harness")) }

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

#[test]
fn mock_full_run_produces_scores_and_artefacts() {
    let out = tempfile::Builder::new().prefix("cma2-mock-").tempdir().unwrap();
    let status = Command::new(harness_bin())
        .arg("--rendered").arg(experiment_root().join("rendered"))
        .arg("--tasks").arg(experiment_root().join("master").join("tasks"))
        .arg("--out").arg(out.path())
        .arg("--model").arg("mock")
        .arg("--mock").arg(PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests").join("fixtures").join("mock_full_run.json"))
        .status()
        .expect("running xmodel-harness");
    assert!(status.success(), "harness exited non-zero");

    let mut entries = std::fs::read_dir(out.path()).unwrap();
    let run_dir = entries.next().unwrap().unwrap().path();
    let status_file = run_dir.join("RUN_STATUS");
    assert_eq!(std::fs::read_to_string(&status_file).unwrap(), "ok");
    let scores = std::fs::read_to_string(run_dir.join("scores.csv")).unwrap();
    assert!(scores.starts_with("cohort,task_id,first_attempt_green,"));
    assert_eq!(scores.lines().count(), 1 + 8, "header + 8 rows expected");
    assert!(scores.contains("json,t3_main_prints,false,2"));
    assert!(scores.contains("ail,t1_add_three,false,INF"));

    // Spot-check one per-cohort artefact tree.
    let pc = run_dir.join("per_cohort").join("ail").join("t1_add_three");
    assert!(pc.join("turn_1_program.ail").exists());
    assert!(pc.join("turn_5_program.ail").exists());
}
