use assert_cmd::Command;
use predicates::prelude::*;
use std::fs;
use tempfile::TempDir;
#[test]
fn test_update_progress_phases() {
let temp = TempDir::new().unwrap();
let project_dir = temp.path();
let manifest_content = r#"
[sources]
test_source = "../test_resources"
[agents]
test_agent = { path = "../test_resources/agent.md" }
"#;
fs::write(project_dir.join("ccpm.toml"), manifest_content).unwrap();
let resource_dir = project_dir.parent().unwrap().join("test_resources");
fs::create_dir_all(&resource_dir).unwrap();
fs::write(resource_dir.join("agent.md"), "# Test Agent").unwrap();
let mut cmd = Command::cargo_bin("ccpm").unwrap();
cmd.current_dir(project_dir)
.arg("install")
.assert()
.success();
assert!(project_dir.join("ccpm.lock").exists());
let mut cmd = Command::cargo_bin("ccpm").unwrap();
let output = cmd
.current_dir(project_dir)
.arg("update")
.assert()
.success();
let stdout = String::from_utf8_lossy(&output.get_output().stdout);
assert!(
stdout.contains("All dependencies are up to date")
|| stdout.contains("Syncing")
|| stdout.contains("Resolving")
|| stdout.contains("Installing"),
"Update output should show proper status messages: {}",
stdout
);
}
#[test]
fn test_update_empty_manifest() {
let temp = TempDir::new().unwrap();
let project_dir = temp.path();
fs::write(project_dir.join("ccpm.toml"), "[sources]\n[agents]\n").unwrap();
let mut cmd = Command::cargo_bin("ccpm").unwrap();
cmd.current_dir(project_dir)
.arg("install")
.assert()
.success();
let mut cmd = Command::cargo_bin("ccpm").unwrap();
cmd.current_dir(project_dir)
.arg("update")
.assert()
.success()
.stdout(predicate::str::contains("All dependencies are up to date"));
}
#[test]
fn test_update_no_lockfile_fresh_install() {
let temp = TempDir::new().unwrap();
let project_dir = temp.path();
fs::write(project_dir.join("ccpm.toml"), "[sources]\n[agents]\n").unwrap();
assert!(!project_dir.join("ccpm.lock").exists());
let mut cmd = Command::cargo_bin("ccpm").unwrap();
let output = cmd
.current_dir(project_dir)
.arg("update")
.assert()
.success();
let stdout = String::from_utf8_lossy(&output.get_output().stdout);
assert!(
stdout.contains("No lockfile found")
|| stdout.contains("fresh install")
|| stdout.contains("No dependencies"),
"Should indicate fresh install: {}",
stdout
);
assert!(project_dir.join("ccpm.lock").exists());
}
#[test]
fn test_update_dry_run_no_modifications() {
let temp = TempDir::new().unwrap();
let project_dir = temp.path();
fs::write(project_dir.join("ccpm.toml"), "[sources]\n[agents]\n").unwrap();
fs::write(
project_dir.join("ccpm.lock"),
"version = 1\nsources = []\nagents = []\n",
)
.unwrap();
let original_lock = fs::read_to_string(project_dir.join("ccpm.lock")).unwrap();
let mut cmd = Command::cargo_bin("ccpm").unwrap();
cmd.current_dir(project_dir)
.arg("update")
.arg("--dry-run")
.assert()
.success();
let current_lock = fs::read_to_string(project_dir.join("ccpm.lock")).unwrap();
assert_eq!(original_lock, current_lock);
}