diff --git a/src-tauri/src/app/claude_plugin.rs b/src-tauri/src/app/claude_plugin.rs index da892a6..cc67457 100644 --- a/src-tauri/src/app/claude_plugin.rs +++ b/src-tauri/src/app/claude_plugin.rs @@ -378,6 +378,70 @@ mod tests { assert!(plugin_enabled_from(b"{}").is_err()); } + #[test] + fn older_complete_plugin_is_upgraded_before_success() { + for enabled in [true, false] { + let dir = tempfile::tempdir().unwrap(); + let source = Path::new(env!("CARGO_MANIFEST_DIR")).join("../plugins/adhd-ranch-hooks"); + for relative in [ + ".claude-plugin/plugin.json", + "hooks/hooks.json", + "scripts/send-hook", + ] { + let destination = dir.path().join(relative); + std::fs::create_dir_all(destination.parent().unwrap()).unwrap(); + std::fs::copy(source.join(relative), destination).unwrap(); + } + let manifest_path = dir.path().join(".claude-plugin/plugin.json"); + let current_manifest = std::fs::read(&manifest_path).unwrap(); + let mut old_manifest: serde_json::Value = + serde_json::from_slice(¤t_manifest).unwrap(); + old_manifest["version"] = serde_json::json!("0.1.0"); + std::fs::write(&manifest_path, serde_json::to_vec(&old_manifest).unwrap()).unwrap(); + let marketplace = source.parent().unwrap().parent().unwrap(); + let mut plugin_enabled = enabled; + let mut updated = false; + + install_user_with( + |args| { + match args { + ["plugin", "list", "--json"] => { + return Ok(serde_json::to_vec(&serde_json::json!([{ + "id": PLUGIN_ID, + "scope": "user", + "enabled": plugin_enabled, + "installPath": dir.path() + }])) + .unwrap()) + } + ["plugin", "marketplace", "list", "--json"] => { + return Ok(serde_json::to_vec(&serde_json::json!([{ + "name": MARKETPLACE_NAME, + "repo": MARKETPLACE_SOURCE, + "installLocation": marketplace + }])) + .unwrap()) + } + ["plugin", "marketplace", "update", MARKETPLACE_NAME] => {} + ["plugin", "update", PLUGIN_ID, "--scope", "user"] => { + std::fs::write(&manifest_path, ¤t_manifest).unwrap(); + updated = true; + } + ["plugin", "enable", PLUGIN_ID, "--scope", "user"] => plugin_enabled = true, + _ => panic!("unexpected Claude command: {args:?}"), + } + Ok(Vec::new()) + }, + contract::validate, + ) + .unwrap(); + + assert!(updated); + assert!(plugin_enabled); + assert_eq!(std::fs::read(&manifest_path).unwrap(), current_manifest); + } + } + #[test] fn already_enabled_and_valid_does_not_repeat_installation() { let mut calls = Vec::new(); diff --git a/src-tauri/src/app/claude_plugin/contract.rs b/src-tauri/src/app/claude_plugin/contract.rs index e800faa..ea4e5af 100644 --- a/src-tauri/src/app/claude_plugin/contract.rs +++ b/src-tauri/src/app/claude_plugin/contract.rs @@ -10,6 +10,10 @@ use adhd_ranch_domain::agents::claude_code::hooks::SESSION_HOOKS; use serde_json::{json, Value}; const HOOK_COMMAND: &str = "${CLAUDE_PLUGIN_ROOT}/scripts/send-hook"; +const MANIFEST: &[u8] = include_bytes!(concat!( + env!("CARGO_MANIFEST_DIR"), + "/../plugins/adhd-ranch-hooks/.claude-plugin/plugin.json" +)); const WRAPPER: &[u8] = include_bytes!(concat!( env!("CARGO_MANIFEST_DIR"), "/../plugins/adhd-ranch-hooks/scripts/send-hook" @@ -34,6 +38,12 @@ pub(super) fn validate(root: &Path) -> io::Result<()> { if manifest.get("name").and_then(Value::as_str) != Some("adhd-ranch-hooks") { return Err(invalid("Ranch plugin manifest has the wrong name")); } + let expected: Value = serde_json::from_slice(MANIFEST)?; + if manifest.get("version").and_then(Value::as_str) + != expected.get("version").and_then(Value::as_str) + { + return Err(invalid("Ranch plugin version differs from this app")); + } let document: Value = serde_json::from_slice(&read_required(root, "hooks/hooks.json")?)?; if let Some(unexpected) = document @@ -166,6 +176,25 @@ mod tests { validate(dir.path()).unwrap(); } + #[test] + fn outdated_or_missing_plugin_version_is_rejected() { + for version in [Some("0.1.0"), None] { + let dir = fixture(); + let file = dir.path().join(".claude-plugin/plugin.json"); + let mut manifest: Value = serde_json::from_slice(&fs::read(&file).unwrap()).unwrap(); + if let Some(version) = version { + manifest["version"] = json!(version); + } else { + manifest.as_object_mut().unwrap().remove("version"); + } + fs::write(file, serde_json::to_vec(&manifest).unwrap()).unwrap(); + + let error = validate(dir.path()).unwrap_err(); + + assert_eq!(error.kind(), io::ErrorKind::InvalidData); + } + } + #[test] fn missing_hook_is_rejected() { let dir = fixture();