use super::super::{OutputFormat, ValidateCommand};
use crate::manifest::{Manifest, ResourceDependency};
use crate::utils::normalize_path_for_storage;
use anyhow::Result;
use tempfile::TempDir;
#[tokio::test]
async fn test_validate_json_format() -> Result<()> {
let temp = TempDir::new()?;
let manifest_path = temp.path().join("agpm.toml");
let mut manifest = crate::manifest::Manifest::new();
manifest.add_source("test".to_string(), "https://github.com/test/repo.git".to_string());
manifest.save(&manifest_path)?;
let cmd = ValidateCommand {
file: None,
resolve: false,
check_lock: false,
sources: false,
paths: false,
format: OutputFormat::Json,
verbose: false,
quiet: true,
strict: false,
render: false,
};
let result = cmd.execute_from_path(manifest_path).await;
result?;
Ok(())
}
#[tokio::test]
async fn test_validate_strict_mode() -> Result<()> {
let temp = TempDir::new()?;
let manifest_path = temp.path().join("agpm.toml");
let manifest = crate::manifest::Manifest::new();
manifest.save(&manifest_path)?;
let cmd = ValidateCommand {
file: None,
resolve: false,
check_lock: false,
sources: false,
paths: false,
format: OutputFormat::Text,
verbose: false,
quiet: true,
strict: true, render: false,
};
let result = cmd.execute_from_path(manifest_path).await;
assert!(result.is_err());
Ok(())
}
#[tokio::test]
async fn test_validate_verbose_mode() -> Result<()> {
let temp = TempDir::new()?;
let manifest_path = temp.path().join("agpm.toml");
let mut manifest = crate::manifest::Manifest::new();
manifest.add_source("test".to_string(), "https://github.com/test/repo.git".to_string());
manifest.save(&manifest_path)?;
let cmd = ValidateCommand {
file: None,
resolve: false,
check_lock: false,
sources: false,
paths: false,
format: OutputFormat::Text,
verbose: true, quiet: false,
strict: false,
render: false,
};
let result = cmd.execute_from_path(manifest_path).await;
result?;
Ok(())
}
#[tokio::test]
async fn test_validate_json_error_format() -> Result<()> {
let temp = TempDir::new()?;
let manifest_path = temp.path().join("agpm.toml");
let mut manifest = crate::manifest::Manifest::new();
manifest.add_dependency(
"test".to_string(),
crate::manifest::ResourceDependency::Detailed(Box::new(
crate::manifest::DetailedDependency {
source: Some("nonexistent".to_string()),
path: "test.md".to_string(),
version: None,
command: None,
branch: None,
rev: None,
args: None,
target: None,
filename: None,
dependencies: None,
tool: Some("claude-code".to_string()),
flatten: None,
install: None,
template_vars: Some(serde_json::Value::Object(serde_json::Map::new())),
},
)),
true,
);
manifest.save(&manifest_path)?;
let cmd = ValidateCommand {
file: None,
resolve: false,
check_lock: false,
sources: false,
paths: false,
format: OutputFormat::Json, verbose: false,
quiet: true,
strict: false,
render: false,
};
let result = cmd.execute_from_path(manifest_path).await;
assert!(result.is_err());
Ok(())
}
#[tokio::test]
async fn test_validate_verbose_output() -> Result<()> {
let temp = TempDir::new()?;
let manifest_path = temp.path().join("agpm.toml");
let manifest = crate::manifest::Manifest::new();
manifest.save(&manifest_path)?;
let cmd = ValidateCommand {
file: None,
resolve: false,
check_lock: false,
sources: false,
paths: false,
format: OutputFormat::Text,
verbose: true,
quiet: false,
strict: false,
render: false,
};
let result = cmd.execute_from_path(manifest_path).await;
result?;
Ok(())
}
#[tokio::test]
async fn test_validate_strict_mode_with_warnings() -> Result<()> {
let temp = TempDir::new()?;
let manifest_path = temp.path().join("agpm.toml");
let manifest = crate::manifest::Manifest::new();
manifest.save(&manifest_path)?;
let cmd = ValidateCommand {
file: None,
resolve: false,
check_lock: true,
sources: false,
paths: false,
format: OutputFormat::Text,
verbose: false,
quiet: false,
strict: true, render: false,
};
let result = cmd.execute_from_path(manifest_path).await;
assert!(result.is_err()); Ok(())
}
#[tokio::test]
async fn test_validate_quiet_mode() -> Result<()> {
let temp = TempDir::new()?;
let manifest_path = temp.path().join("agpm.toml");
let manifest = crate::manifest::Manifest::new();
manifest.save(&manifest_path)?;
let cmd = ValidateCommand {
file: None,
resolve: false,
check_lock: false,
sources: false,
paths: false,
format: OutputFormat::Text,
verbose: false,
quiet: true, strict: false,
render: false,
};
let result = cmd.execute_from_path(manifest_path).await;
result?;
Ok(())
}
#[tokio::test]
async fn test_validate_json_output_success() -> Result<()> {
let temp = TempDir::new()?;
let manifest_path = temp.path().join("agpm.toml");
let mut manifest = crate::manifest::Manifest::new();
use crate::manifest::{DetailedDependency, ResourceDependency};
manifest.agents.insert(
"test".to_string(),
ResourceDependency::Detailed(Box::new(DetailedDependency {
source: None,
path: "test.md".to_string(),
version: None,
command: None,
branch: None,
rev: None,
args: None,
target: None,
filename: None,
dependencies: None,
tool: Some("claude-code".to_string()),
flatten: None,
install: None,
template_vars: Some(serde_json::Value::Object(serde_json::Map::new())),
})),
);
manifest.save(&manifest_path)?;
let cmd = ValidateCommand {
file: None,
resolve: false,
check_lock: false,
sources: false,
paths: false,
format: OutputFormat::Json, verbose: false,
quiet: false,
strict: false,
render: false,
};
let result = cmd.execute_from_path(manifest_path).await;
result?;
Ok(())
}
#[tokio::test]
async fn test_validate_manifest_toml_syntax_error_json() -> Result<()> {
let temp = TempDir::new()?;
let manifest_path = temp.path().join("agpm.toml");
std::fs::write(&manifest_path, "invalid toml syntax [[[")?;
let cmd = ValidateCommand {
file: None,
resolve: false,
check_lock: false,
sources: false,
paths: false,
format: OutputFormat::Json,
verbose: false,
quiet: true,
strict: false,
render: false,
};
let result = cmd.execute_from_path(manifest_path).await;
assert!(result.is_err());
Ok(())
}
#[tokio::test]
async fn test_validate_resolve_with_error_json_output() -> Result<()> {
let temp = TempDir::new()?;
let manifest_path = temp.path().join("agpm.toml");
let mut manifest = crate::manifest::Manifest::new();
manifest.add_source("test".to_string(), "https://github.com/nonexistent/repo.git".to_string());
manifest.add_dependency(
"failing-agent".to_string(),
crate::manifest::ResourceDependency::Detailed(Box::new(
crate::manifest::DetailedDependency {
source: Some("test".to_string()),
path: "test.md".to_string(),
version: None,
command: None,
branch: None,
rev: None,
args: None,
target: None,
filename: None,
dependencies: None,
tool: Some("claude-code".to_string()),
flatten: None,
install: None,
template_vars: Some(serde_json::Value::Object(serde_json::Map::new())),
},
)),
true,
);
manifest.save(&manifest_path)?;
let cmd = ValidateCommand {
file: None,
resolve: true,
check_lock: false,
sources: false,
paths: false,
format: OutputFormat::Json,
verbose: false,
quiet: true,
strict: false,
render: false,
};
let result = cmd.execute_from_path(manifest_path).await;
let _ = result; Ok(())
}
#[tokio::test]
async fn test_validate_sources_accessibility_error_json() -> Result<()> {
let temp = TempDir::new()?;
let manifest_path = temp.path().join("agpm.toml");
let nonexistent_path1 = temp.path().join("nonexistent1");
let nonexistent_path2 = temp.path().join("nonexistent2");
let url1 = format!("file://{}", normalize_path_for_storage(&nonexistent_path1));
let url2 = format!("file://{}", normalize_path_for_storage(&nonexistent_path2));
let mut manifest = crate::manifest::Manifest::new();
manifest.add_source("official".to_string(), url1);
manifest.add_source("community".to_string(), url2);
manifest.save(&manifest_path)?;
let cmd = ValidateCommand {
file: None,
resolve: false,
check_lock: false,
sources: true,
paths: false,
format: OutputFormat::Json,
verbose: false,
quiet: true,
strict: false,
render: false,
};
let result = cmd.execute_from_path(manifest_path).await;
let _ = result;
Ok(())
}
#[tokio::test]
async fn test_validate_check_paths_missing_snippets_json() -> Result<()> {
let temp = TempDir::new()?;
let manifest_path = temp.path().join("agpm.toml");
let mut manifest = crate::manifest::Manifest::new();
manifest.snippets.insert(
"missing-snippet".to_string(),
crate::manifest::ResourceDependency::Detailed(Box::new(
crate::manifest::DetailedDependency {
source: None,
path: "./missing/snippet.md".to_string(),
version: None,
command: None,
branch: None,
rev: None,
args: None,
target: None,
filename: None,
dependencies: None,
tool: Some("claude-code".to_string()),
flatten: None,
install: None,
template_vars: Some(serde_json::Value::Object(serde_json::Map::new())),
},
)),
);
manifest.save(&manifest_path)?;
let cmd = ValidateCommand {
file: None,
resolve: false,
check_lock: false,
sources: false,
paths: true,
format: OutputFormat::Json, verbose: false,
quiet: true,
strict: false,
render: false,
};
let result = cmd.execute_from_path(manifest_path).await;
assert!(result.is_err());
Ok(())
}
#[tokio::test]
async fn test_validate_lockfile_syntax_error_json() -> Result<()> {
let temp = TempDir::new()?;
let manifest_path = temp.path().join("agpm.toml");
let lockfile_path = temp.path().join("agpm.lock");
let manifest = crate::manifest::Manifest::new();
manifest.save(&manifest_path)?;
std::fs::write(&lockfile_path, "invalid toml [[[")?;
let cmd = ValidateCommand {
file: None,
resolve: false,
check_lock: true,
sources: false,
paths: false,
format: OutputFormat::Json,
verbose: false,
quiet: true,
strict: false,
render: false,
};
let result = cmd.execute_from_path(manifest_path).await;
assert!(result.is_err());
Ok(())
}
#[tokio::test]
async fn test_validate_strict_mode_with_json_output() -> Result<()> {
let temp = TempDir::new()?;
let manifest_path = temp.path().join("agpm.toml");
let manifest = crate::manifest::Manifest::new(); manifest.save(&manifest_path)?;
let cmd = ValidateCommand {
file: None,
resolve: false,
check_lock: false,
sources: false,
paths: false,
format: OutputFormat::Json,
verbose: false,
quiet: true,
strict: true, render: false,
};
let result = cmd.execute_from_path(manifest_path).await;
assert!(result.is_err()); Ok(())
}
#[tokio::test]
async fn test_validate_strict_mode_text_output() -> Result<()> {
let temp = TempDir::new()?;
let manifest_path = temp.path().join("agpm.toml");
let manifest = crate::manifest::Manifest::new();
manifest.save(&manifest_path)?;
let cmd = ValidateCommand {
file: None,
resolve: false,
check_lock: false,
sources: false,
paths: false,
format: OutputFormat::Text,
verbose: false,
quiet: false, strict: true,
render: false,
};
let result = cmd.execute_from_path(manifest_path).await;
assert!(result.is_err());
Ok(())
}
#[tokio::test]
async fn test_validate_verbose_mode_with_summary() -> Result<()> {
let temp = TempDir::new()?;
let manifest_path = temp.path().join("agpm.toml");
let mut manifest = crate::manifest::Manifest::new();
manifest.add_source("test".to_string(), "https://github.com/test/repo.git".to_string());
manifest.add_dependency(
"test-agent".to_string(),
crate::manifest::ResourceDependency::Simple("test.md".to_string()),
true,
);
manifest.add_dependency(
"test-snippet".to_string(),
crate::manifest::ResourceDependency::Simple("snippet.md".to_string()),
false,
);
manifest.save(&manifest_path)?;
let cmd = ValidateCommand {
file: None,
resolve: false,
check_lock: false,
sources: false,
paths: false,
format: OutputFormat::Text,
verbose: true, quiet: false,
strict: false,
render: false,
};
let result = cmd.execute_from_path(manifest_path).await;
result?;
Ok(())
}
#[tokio::test]
async fn test_output_format_equality() -> Result<()> {
assert_eq!(OutputFormat::Text, OutputFormat::Text);
assert_eq!(OutputFormat::Json, OutputFormat::Json);
assert_ne!(OutputFormat::Text, OutputFormat::Json);
Ok(())
}
#[tokio::test]
async fn test_json_output_format() -> Result<()> {
let temp = TempDir::new()?;
let manifest_path = temp.path().join("agpm.toml");
let manifest = Manifest::new();
manifest.save(&manifest_path)?;
let cmd = ValidateCommand {
file: None,
resolve: false,
check_lock: false,
sources: false,
paths: false,
format: OutputFormat::Json,
verbose: false,
quiet: false,
strict: false,
render: false,
};
let result = cmd.execute_from_path(manifest_path).await;
result?;
Ok(())
}
#[tokio::test]
async fn test_validation_with_verbose_mode() -> Result<()> {
let temp = TempDir::new()?;
let manifest_path = temp.path().join("agpm.toml");
let manifest = Manifest::new();
manifest.save(&manifest_path)?;
let cmd = ValidateCommand {
file: None,
resolve: false,
check_lock: false,
sources: false,
paths: false,
format: OutputFormat::Text,
verbose: true,
quiet: false,
strict: false,
render: false,
};
let result = cmd.execute_from_path(manifest_path).await;
result?;
Ok(())
}
#[tokio::test]
async fn test_validation_with_quiet_mode() -> Result<()> {
let temp = TempDir::new()?;
let manifest_path = temp.path().join("agpm.toml");
let manifest = Manifest::new();
manifest.save(&manifest_path)?;
let cmd = ValidateCommand {
file: None,
resolve: false,
check_lock: false,
sources: false,
paths: false,
format: OutputFormat::Text,
verbose: false,
quiet: true,
strict: false,
render: false,
};
let result = cmd.execute_from_path(manifest_path).await;
result?;
Ok(())
}
#[tokio::test]
async fn test_validation_with_strict_mode_and_warnings() -> Result<()> {
let temp = TempDir::new()?;
let manifest_path = temp.path().join("agpm.toml");
let manifest = Manifest::new();
manifest.save(&manifest_path)?;
let cmd = ValidateCommand {
file: None,
resolve: false,
check_lock: false,
sources: false,
paths: false,
format: OutputFormat::Text,
verbose: false,
quiet: false,
strict: true, render: false,
};
let result = cmd.execute_from_path(manifest_path).await;
assert!(result.is_err()); Ok(())
}
#[tokio::test]
async fn test_validation_json_output_with_errors() -> Result<()> {
let temp = TempDir::new()?;
let manifest_path = temp.path().join("agpm.toml");
std::fs::write(&manifest_path, "invalid toml [[[ syntax")?;
let cmd = ValidateCommand {
file: None,
resolve: false,
check_lock: false,
sources: false,
paths: false,
format: OutputFormat::Json,
verbose: false,
quiet: false,
strict: false,
render: false,
};
let result = cmd.execute_from_path(manifest_path).await;
assert!(result.is_err());
Ok(())
}
#[tokio::test]
async fn test_validation_with_manifest_not_found_json() -> Result<()> {
let temp = TempDir::new()?;
let manifest_path = temp.path().join("nonexistent.toml");
let cmd = ValidateCommand {
file: None,
resolve: false,
check_lock: false,
sources: false,
paths: false,
format: OutputFormat::Json,
verbose: false,
quiet: false,
strict: false,
render: false,
};
let result = cmd.execute_from_path(manifest_path).await;
assert!(result.is_err());
Ok(())
}
#[tokio::test]
async fn test_validation_with_manifest_not_found_text() -> Result<()> {
let temp = TempDir::new()?;
let manifest_path = temp.path().join("nonexistent.toml");
let cmd = ValidateCommand {
file: None,
resolve: false,
check_lock: false,
sources: false,
paths: false,
format: OutputFormat::Text,
verbose: false,
quiet: false,
strict: false,
render: false,
};
let result = cmd.execute_from_path(manifest_path).await;
assert!(result.is_err());
Ok(())
}
#[tokio::test]
async fn test_validation_with_verbose_and_text_format() -> Result<()> {
let temp = TempDir::new()?;
let manifest_path = temp.path().join("agpm.toml");
let mut manifest = Manifest::new();
manifest.sources.insert("test".to_string(), "https://github.com/test/repo.git".to_string());
manifest
.agents
.insert("agent1".to_string(), ResourceDependency::Simple("agent.md".to_string()));
manifest
.snippets
.insert("snippet1".to_string(), ResourceDependency::Simple("snippet.md".to_string()));
manifest.save(&manifest_path)?;
let cmd = ValidateCommand {
file: None,
resolve: false,
check_lock: false,
sources: false,
paths: false,
format: OutputFormat::Text,
verbose: true,
quiet: false,
strict: false,
render: false,
};
let result = cmd.execute_from_path(manifest_path).await;
result?;
Ok(())
}