use predicates::prelude::predicate;
use std::time::Duration;
use tempfile::tempdir;

#[cfg(target_arch = "x86_64")] // Mojang only provides a x86_64 binary this test will fail on AArch64
#[test]
#[ignore = "downloads the version-locked Minecraft Bedrock server"]
fn bedrock_server_normal_stop() {
    let temp_dir = tempdir().unwrap();
    let data_home = temp_dir.path().join("data");
    let config_home = temp_dir.path().join("config");

    let envs = [
        ("XDG_DATA_HOME", data_home.as_os_str().to_str().unwrap()),
        ("XDG_CONFIG_HOME", config_home.as_os_str().to_str().unwrap()),
    ];

    let mut cmd = assert_cmd::cargo::cargo_bin_cmd!("minecraft-server-bedrock");
    cmd.envs(envs)
        .write_stdin("stop\n")
        .timeout(Duration::from_mins(3));

    cmd.assert()
        .success()
        .stdout(predicate::str::contains("INFO] Server started."))
        .stdout(predicate::str::contains(
            "Minecraft Bedrock server exited with exit status: 0",
        ));
}

/*
 *  TODO: add signal handling tests
 *  SIGTERM, SIGINT, ctrl+c (simulation only to the rust wrapper)
 *  for all 4 different processes (the 2 rust wrappers and the 2 minecraft servers)
 *  expected behavior
 *  1. when the rust wrappers recieve the signals the servers should shutdown just like when
 *      recieving the "stop\n" sequence. they should output that they recieved the signal
 *  2. when the minecraft servers recieve the signals they should quit and exit with high numbered
 *      codes. the rust wrapper should detect that and report the exit statuses correctly
 *
 *  the easiest way to impliment this would be:
 *  1. run the process
 *  2. manually send the signal
 *  3. inspect the captured output
 *
 *  however i don't know how to do this with assert-cmd :(
 *
 *  the nix package would make it straight forward:
 *      https://docs.rs/nix/latest/nix/sys/signal/fn.killpg.html (to simulate ctrl+c)
 *      https://docs.rs/nix/latest/nix/sys/signal/fn.kill.html
 *
 *  TODO: add tests for configuration options
 *      there are files and environment variables
 */
