use assert_cmd::Command;
use predicates::prelude::*;
use std::fs;
use tempfile::TempDir;
fn get_bin() -> Command {
Command::new(env!("CARGO_BIN_EXE_wasm-slim"))
}
#[test]
fn test_init_with_default_template_creates_config_file() {
let temp_dir = TempDir::new().expect("Failed to create temp directory for test");
let mut cmd = get_bin();
cmd.arg("init")
.current_dir(temp_dir.path())
.assert()
.success()
.stdout(predicate::str::contains("balanced"))
.stdout(predicate::str::contains("Created .wasm-slim.toml"));
let config_path = temp_dir.path().join(".wasm-slim.toml");
assert!(config_path.exists());
let config_content = fs::read_to_string(&config_path).expect("Failed to read file contents");
assert!(config_content.contains("template = \"balanced\""));
assert!(config_content.contains("opt-level"));
}
#[test]
fn test_init_with_minimal_template_creates_minimal_config() {
let temp_dir = TempDir::new().expect("Failed to create temp directory for test");
let mut cmd = get_bin();
cmd.arg("init")
.arg("--template")
.arg("minimal")
.current_dir(temp_dir.path())
.assert()
.success()
.stdout(predicate::str::contains("minimal"))
.stdout(predicate::str::contains("Maximum size reduction"));
let config_path = temp_dir.path().join(".wasm-slim.toml");
assert!(config_path.exists());
let config_content = fs::read_to_string(&config_path).expect("Failed to read file contents");
assert!(config_content.contains("template = \"minimal\""));
assert!(config_content.contains("opt-level = \"z\""));
}
#[test]
fn test_init_with_framework_template_creates_framework_config() {
let temp_dir = TempDir::new().expect("Failed to create temp directory for test");
let mut cmd = get_bin();
cmd.arg("init")
.arg("--template")
.arg("yew")
.current_dir(temp_dir.path())
.assert()
.success()
.stdout(predicate::str::contains("yew"))
.stdout(predicate::str::contains("Yew framework"));
let config_path = temp_dir.path().join(".wasm-slim.toml");
assert!(config_path.exists());
let config_content = fs::read_to_string(&config_path).expect("Failed to read file contents");
assert!(config_content.contains("template = \"yew\""));
}
#[test]
fn test_init_when_config_exists_returns_error() {
let temp_dir = TempDir::new().expect("Failed to create temp directory for test");
let config_path = temp_dir.path().join(".wasm-slim.toml");
fs::write(&config_path, "template = \"custom\"").expect("Failed to write test file");
let mut cmd = get_bin();
cmd.arg("init")
.current_dir(temp_dir.path())
.assert()
.success()
.stdout(predicate::str::contains("already exists"));
}
#[test]
fn test_init_with_invalid_template_returns_error() {
let temp_dir = TempDir::new().expect("Failed to create temp directory for test");
let mut cmd = get_bin();
cmd.arg("init")
.arg("--template")
.arg("invalid-template")
.current_dir(temp_dir.path())
.assert()
.failure()
.stderr(predicate::str::contains("not found"));
}