//! Integration test for `--server-url`.
//!
//! Confirms that the global override flag prints the runner banner and,
//!, actually rewrites the sole `servers:` entry so the
//! resolved run target reflects the flag rather than the YAML.

use assert_cmd::Command;
use predicates::prelude::*;
use std::io::Write;

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

#[test]
fn run_with_server_url_prints_override_note_and_exits_zero() {
    // Tempdir keeps the no-config path: the bare default would otherwise
    // resolve the repo's checked-in mcptest.yml (WOR-1350).
    let dir = tempfile::tempdir().unwrap();
    mcptest()
        .current_dir(dir.path())
        .args(["run", "--server-url", "https://example.com"])
        .assert()
        .success()
        .stdout(predicate::str::contains(
            "Server-target override applied: --server-url",
        ))
        .stdout(predicate::str::contains("no config at"));
}

#[test]
fn server_url_and_server_command_together_are_rejected() {
    let dir = tempfile::tempdir().unwrap();
    mcptest()
        .current_dir(dir.path())
        .args([
            "run",
            "--server-url",
            "https://example.com",
            "--server-command",
            "./dev",
        ])
        .assert()
        .failure()
        .stderr(predicate::str::contains("mutually exclusive"));
}

#[test]
fn server_url_redirects_sole_servers_entry_in_resolved_config() {
    // A config whose only server is a stdio command. `--print-config`
    // renders the server kind label, so a successful redirect flips the
    // sole entry from `stdio` to `http`. Before the fix the
    // override targeted a singular `server:` key the parser ignored, so
    // the label stayed `stdio` and the flag was a no-op.
    let mut cfg = tempfile::NamedTempFile::with_suffix(".yaml").expect("temp config");
    writeln!(
        cfg,
        "servers:\n  local:\n    command: [\"./dev-server\"]\ntools: []\n"
    )
    .expect("write config");

    mcptest()
        .args([
            "--config",
            cfg.path().to_str().expect("utf8 path"),
            "--server-url",
            "https://preview.example.com",
            "run",
            "--print-config",
        ])
        .assert()
        .success()
        .stdout(predicate::str::contains("- local (http)"))
        .stdout(predicate::str::contains("local (stdio)").not());
}
