//! Integration test for config-driven three-valued runs (WOR-1509).
//!
//! `run_options.inconclusive.on: true` opts a suite into the sequential
//! reliability test without passing `--repeat`: the run executes a fixed
//! `min_trials` samples and reports a PASS/FAIL/INCONCLUSIVE verdict at the
//! suite's `confidence_pct`. Before this fix those fields parsed but did
//! nothing. Offline and key-free: the server is the built-in mock.

use assert_cmd::Command;
use predicates::prelude::*;

fn mcptest() -> Command {
    Command::cargo_bin("mcptest").expect("binary builds")
}

fn mcptest_bin() -> &'static str {
    env!("CARGO_BIN_EXE_mcptest")
}

/// Write a clean mock manifest exposing one deterministic `echo` tool.
fn write_manifest(dir: &std::path::Path) -> std::path::PathBuf {
    let manifest = r#"mock_server:
  name: echo
  tools:
    - name: echo
      description: "Echo a fixed line."
      input_schema:
        type: object
      response:
        isError: false
        content:
          - type: text
            text: "hello"
"#;
    let path = dir.join("echo.yaml");
    std::fs::write(&path, manifest).expect("write mock manifest");
    path
}

/// A suite with `inconclusive.on: true` and no `--repeat` runs the sequential
/// reliability test for `min_trials` samples. The banner (always emitted to
/// stderr) is the proof that the config-driven path fired, where before the fix
/// the same suite ran exactly once.
#[test]
fn inconclusive_on_triggers_a_sequential_run_without_repeat() {
    let dir = tempfile::tempdir().unwrap();
    let manifest = write_manifest(dir.path());
    let suite = format!(
        r#"servers:
  mock:
    command: ['{bin}', 'mock', '--tools-from', '{manifest}']
run_options:
  inconclusive:
    on: true
    min_trials: 2
    confidence_pct: 90
tests:
  - name: echo returns hello
    server: mock
    tool: echo
    expect:
      - target: result.content[0].text
        matcher:
          exact: "hello"
"#,
        bin = mcptest_bin(),
        manifest = manifest.display(),
    );
    let path = dir.path().join("suite.yml");
    std::fs::write(&path, suite).expect("write suite");
    mcptest()
        .args(["run", "--config", path.to_str().unwrap()])
        .assert()
        .stderr(
            predicate::str::contains("sequential reliability test")
                .and(predicate::str::contains("confidence 90%")),
        );
}
