//! Tests for basic CLI commands that don't require daemon.

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

#[macro_use]
mod common;

#[test]
fn test_version() {
    require_ghidra!();

    Command::cargo_bin("ghidra")
        .unwrap()
        .arg("version")
        .assert()
        .success()
        .stdout(predicate::str::contains("ghidra-cli"));
}

#[test]
fn test_doctor() {
    require_ghidra!();

    Command::cargo_bin("ghidra")
        .unwrap()
        .arg("doctor")
        .assert()
        .success()
        .stdout(predicate::str::contains("Ghidra CLI Doctor"));
}

#[test]
fn test_config_list() {
    require_ghidra!();

    Command::cargo_bin("ghidra")
        .unwrap()
        .arg("config")
        .arg("list")
        .assert()
        .success()
        .stdout(predicate::str::contains("ghidra_install_dir"));
}

#[test]
fn test_config_get() {
    require_ghidra!();

    Command::cargo_bin("ghidra")
        .unwrap()
        .arg("config")
        .arg("get")
        .arg("ghidra_install_dir")
        .assert()
        .success();
}

#[test]
fn test_config_set() {
    require_ghidra!();

    let temp = tempfile::tempdir().unwrap();
    let config_path = temp.path().join("config.yaml");

    Command::cargo_bin("ghidra")
        .unwrap()
        .env("GHIDRA_CLI_CONFIG", &config_path)
        .arg("config")
        .arg("set")
        .arg("default_output_format")
        .arg("json")
        .assert()
        .success();
}

#[test]
fn test_config_reset() {
    require_ghidra!();

    let temp = tempfile::tempdir().unwrap();
    let config_path = temp.path().join("config.yaml");

    Command::cargo_bin("ghidra")
        .unwrap()
        .env("GHIDRA_CLI_CONFIG", &config_path)
        .arg("config")
        .arg("reset")
        .assert()
        .success();
}

#[test]
fn test_init() {
    require_ghidra!();

    let temp = tempfile::tempdir().unwrap();
    let config_path = temp.path().join("config.yaml");

    Command::cargo_bin("ghidra")
        .unwrap()
        .env("GHIDRA_CLI_CONFIG", &config_path)
        .arg("init")
        .assert()
        .success();

    assert!(config_path.exists());
}

#[test]
fn test_set_default_program() {
    require_ghidra!();

    let temp = tempfile::tempdir().unwrap();
    let config_path = temp.path().join("config.yaml");

    Command::cargo_bin("ghidra")
        .unwrap()
        .env("GHIDRA_CLI_CONFIG", &config_path)
        .arg("set-default")
        .arg("program")
        .arg("sample_binary")
        .assert()
        .success()
        .stdout(predicate::str::contains("Default program set"));
}

#[test]
fn test_set_default_project() {
    require_ghidra!();

    let temp = tempfile::tempdir().unwrap();
    let config_path = temp.path().join("config.yaml");

    Command::cargo_bin("ghidra")
        .unwrap()
        .env("GHIDRA_CLI_CONFIG", &config_path)
        .arg("set-default")
        .arg("project")
        .arg("test-project")
        .assert()
        .success()
        .stdout(predicate::str::contains("Default project set"));
}
