use anyhow::Result;
use std::path::PathBuf;
use tempfile::TempDir;
use twin_cli::cli::commands::handle_init;
use twin_cli::cli::InitArgs;
#[tokio::test]
async fn test_init_command_creates_config_file() -> Result<()> {
let temp_dir = TempDir::new()?;
let config_path = temp_dir.path().join("twin.toml");
let args = InitArgs {
path: Some(config_path.clone()),
force: false,
};
handle_init(args).await?;
assert!(config_path.exists());
let content = tokio::fs::read_to_string(&config_path).await?;
assert!(content.contains("[files]"));
assert!(content.contains("[hooks]"));
Ok(())
}
#[tokio::test]
async fn test_init_command_with_force_flag() -> Result<()> {
let temp_dir = TempDir::new()?;
let config_path = temp_dir.path().join("twin.toml");
let args = InitArgs {
path: Some(config_path.clone()),
force: false,
};
handle_init(args).await?;
tokio::fs::write(&config_path, "# custom content\n").await?;
let args = InitArgs {
path: Some(config_path.clone()),
force: true,
};
handle_init(args).await?;
let content = tokio::fs::read_to_string(&config_path).await?;
assert!(!content.starts_with("# custom content"));
assert!(content.contains("[files]"));
Ok(())
}
#[tokio::test]
async fn test_init_command_error_when_file_exists() -> Result<()> {
let temp_dir = TempDir::new()?;
let config_path = temp_dir.path().join("twin.toml");
let args = InitArgs {
path: Some(config_path.clone()),
force: false,
};
handle_init(args).await?;
let args = InitArgs {
path: Some(config_path.clone()),
force: false,
};
let result = handle_init(args).await;
assert!(result.is_err());
assert!(result.unwrap_err().to_string().contains("already exists"));
Ok(())
}
#[tokio::test]
async fn test_init_command_default_filename() -> Result<()> {
use std::env;
let temp_dir = TempDir::new()?;
let original_dir = env::current_dir()?;
env::set_current_dir(temp_dir.path())?;
let args = InitArgs {
path: None,
force: false,
};
handle_init(args).await?;
let default_path = PathBuf::from("twin.toml");
assert!(default_path.exists());
env::set_current_dir(original_dir)?;
Ok(())
}