oauth-db-cli 0.1.0

Command-line tool for managing OAuth-DB platform
Documentation
use assert_cmd::Command;
use predicates::prelude::*;

mod helpers;
use helpers::{TestContext, setup_test_cmd};

#[test]
fn test_config_list_default() {
    let ctx = TestContext::new("http://localhost:38080".to_string());
    ctx.write_config();

    let mut cmd = setup_test_cmd(&ctx);
    cmd.arg("config")
        .arg("list")
        .assert()
        .success()
        .stdout(predicates::str::contains("server.url = http://localhost:38080"))
        .stdout(predicates::str::contains("output.format = table"))
        .stdout(predicates::str::contains("output.color = true"))
        .stdout(predicates::str::contains("log.level = info"));
}

#[test]
fn test_config_get_server_url() {
    let ctx = TestContext::new("http://localhost:38080".to_string());
    ctx.write_config();

    let mut cmd = setup_test_cmd(&ctx);
    cmd.arg("config")
        .arg("get")
        .arg("server.url")
        .assert()
        .success()
        .stdout(predicates::str::contains("http://localhost:38080"));
}

#[test]
fn test_config_set_output_format() {
    let ctx = TestContext::new("http://localhost:38080".to_string());
    ctx.write_config();

    // 设置输出格式为 json
    let mut cmd = setup_test_cmd(&ctx);
    cmd.arg("config")
        .arg("set")
        .arg("output.format")
        .arg("json")
        .assert()
        .success()
        .stdout(predicates::str::contains("Set output.format"));

    // 验证设置成功
    let mut cmd = setup_test_cmd(&ctx);
    cmd.arg("config")
        .arg("get")
        .arg("output.format")
        .assert()
        .success()
        .stdout(predicates::str::contains("json"));
}

#[test]
fn test_config_reset() {
    let ctx = TestContext::new("http://localhost:38080".to_string());
    ctx.write_config();

    // 重置配置
    let mut cmd = setup_test_cmd(&ctx);
    cmd.arg("config")
        .arg("reset")
        .assert()
        .success()
        .stdout(predicates::str::contains("Configuration reset"));

    // 验证配置已重置为默认值
    let mut cmd = setup_test_cmd(&ctx);
    cmd.arg("config")
        .arg("list")
        .assert()
        .success();
}

#[test]
fn test_config_get_nonexistent_key() {
    let ctx = TestContext::new("http://localhost:38080".to_string());
    ctx.write_config();

    let mut cmd = setup_test_cmd(&ctx);
    cmd.arg("config")
        .arg("get")
        .arg("nonexistent.key")
        .assert()
        .failure()
        .stderr(predicates::str::contains("Unknown config key"));
}