//! RED tests for Gitea #29: `io/print_str` must print exactly the
//! bytes of its argument with no implicit trailing newline.
//!
//! Today the codegen at `crates/ailang-codegen/src/lib.rs:2734-2754`
//! lowers `(do io/print_str s)` to a C `@puts(ptr)` call. `puts`
//! writes the C string PLUS a trailing newline, so `io/print_str` is
//! de-facto `println_str`. That is undocumented in the design ledger
//! and was empirically caught in the cross-model-authoring naming-A/B
//! run on 2026-05-21: Qwen3-Coder repeatedly wrote
//! `(do io/print_str "text\n")` with explicit `\n`, expecting the
//! bytes to be printed literally — and got doubled newlines, failing
//! every t3_main_prints stdout match across all three cohorts.
//!
//! These tests will turn GREEN once the codegen swaps the `@puts`
//! call for `fputs(s, stdout)` (Option 1 from Gitea #29). Until
//! then they pin the symptom: two single-character prints emit
//! `"ab"`, not `"a\nb\n"`; one print of `"x\n"` emits `"x\n"`, not
//! `"x\n\n"`.
//!
//! NOTE: Unlike `show_print_e2e.rs` these assertions compare RAW
//! stdout — no `.trim()`. The whole point of #29 is the byte-level
//! property; trimming would mask both bugs.

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

fn fixture_path(name: &str) -> PathBuf {
    PathBuf::from(env!("CARGO_MANIFEST_DIR"))
        .join("../../examples")
        .join(name)
}

fn build_and_run_raw(fixture: &str) -> String {
    let src = fixture_path(fixture);
    let out = std::env::temp_dir().join(format!("ail_{}.bin", fixture.replace('.', "_")));

    let build = Command::new(env!("CARGO_BIN_EXE_ail"))
        .args(["build", src.to_str().unwrap(), "-o", out.to_str().unwrap()])
        .output()
        .expect("ail build");
    assert!(
        build.status.success(),
        "ail build failed for {fixture}:\nstdout: {}\nstderr: {}",
        String::from_utf8_lossy(&build.stdout),
        String::from_utf8_lossy(&build.stderr),
    );

    let run = Command::new(&out).output().expect("run binary");
    assert!(
        run.status.success(),
        "binary exited non-zero:\nstdout: {}\nstderr: {}",
        String::from_utf8_lossy(&run.stdout),
        String::from_utf8_lossy(&run.stderr),
    );
    // Raw stdout — no trimming. The bug is at the byte level.
    String::from_utf8(run.stdout).expect("stdout is valid UTF-8")
}

/// Property: two consecutive `(do io/print_str ...)` calls with
/// single-character no-newline arguments produce exactly the
/// concatenation of those characters on stdout — no separator, no
/// trailing newline.
///
/// Fixture body: `(seq (do io/print_str "a") (do io/print_str "b"))`.
///
/// Expected (post-fix): `"ab"`.
/// Today (pre-fix):     `"a\nb\n"` — `@puts` appends a newline per call.
#[test]
fn io_print_str_does_not_append_auto_newline() {
    let stdout = build_and_run_raw("print_str_no_auto_newline.ail");
    assert_eq!(stdout, "ab", "got: {stdout:?}");
}

/// Property: a single `(do io/print_str "x\n")` emits exactly one
/// newline — the one the author embedded. The codegen must not
/// double it.
///
/// Fixture body: `(do io/print_str "x\n")`.
///
/// Expected (post-fix): `"x\n"`.
/// Today (pre-fix):     `"x\n\n"` — embedded `\n` plus auto-newline
/// from `@puts`.
#[test]
fn io_print_str_does_not_double_embedded_newline() {
    let stdout = build_and_run_raw("print_str_embedded_newline_no_doubling.ail");
    assert_eq!(stdout, "x\n", "got: {stdout:?}");
}
