| 1 | use super::*; |
| 2 | |
| 3 | fn bundle(root: &Path, directory: &str, name: &str) -> PathBuf { |
| 4 | let path = root.join(directory); |
| 5 | fs::create_dir_all(&path).unwrap(); |
| 6 | fs::write( |
| 7 | path.join("plugin.toml"), |
| 8 | format!("schema_version = 1\n[plugin]\nname = {name:?}\nversion = \"1.0.0\"\n"), |
| 9 | ) |
| 10 | .unwrap(); |
| 11 | path |
| 12 | } |
| 13 | |
| 14 | fn config(root: &Path) -> DiscoveryConfig { |
| 15 | DiscoveryConfig { |
| 16 | workspace: root.join("project"), |
| 17 | user_plugins_dir: root.join("plugins"), |
| 18 | workspace_plugins_dir: root.join("project/.codewhale/plugins"), |
| 19 | builtin_plugin_dirs: vec![], |
| 20 | state_path: root.join("state.json"), |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | #[test] |
| 25 | fn private_staging_copies_cannot_shadow_the_installed_incumbent() { |
| 26 | let tmp = tempfile::tempdir().unwrap(); |
| 27 | let cfg = config(tmp.path()); |
| 28 | let installed = bundle(&cfg.user_plugins_dir, "demo", "demo"); |
| 29 | let operation = format!(".staging-{}", uuid::Uuid::new_v4().simple()); |
| 30 | let staged = bundle(&cfg.user_plugins_dir, &operation, "demo"); |
| 31 | fs::write(staged.join("canary.txt"), b"private staged payload").unwrap(); |
| 32 | let registry = discover_with_config(&cfg); |
| 33 | assert_eq!(registry.len(), 1); |
| 34 | assert_eq!( |
| 35 | registry.get("demo").unwrap().canonical_root, |
| 36 | installed.canonicalize().unwrap() |
| 37 | ); |
| 38 | assert!( |
| 39 | !registry |
| 40 | .diagnostics() |
| 41 | .iter() |
| 42 | .any(|d| d.code == "name-conflict") |
| 43 | ); |
| 44 | assert_eq!( |
| 45 | fs::read(staged.join("canary.txt")).unwrap(), |
| 46 | b"private staged payload" |
| 47 | ); |
| 48 | assert!(!cfg.state_path.exists(), "discovery remains read-only"); |
| 49 | } |
| 50 | |
| 51 | #[test] |
| 52 | fn reserved_backup_containers_are_hidden_but_human_bundle_paths_remain_visible() { |
| 53 | let tmp = tempfile::tempdir().unwrap(); |
| 54 | let cfg = config(tmp.path()); |
| 55 | fs::create_dir_all(&cfg.user_plugins_dir).unwrap(); |
| 56 | let backup = tempfile::Builder::new() |
| 57 | .prefix(".plugin-backup-") |
| 58 | .rand_bytes(16) |
| 59 | .tempdir_in(&cfg.user_plugins_dir) |
| 60 | .unwrap(); |
| 61 | // Even a manifest accidentally written directly in private recovery |
| 62 | // storage must not turn it into a discoverable public bundle. |
| 63 | bundle(backup.path(), "", "private-backup"); |
| 64 | bundle( |
| 65 | &cfg.user_plugins_dir, |
| 66 | ".staging-not-a-uuid", |
| 67 | "human-staging", |
| 68 | ); |
| 69 | bundle( |
| 70 | &cfg.user_plugins_dir, |
| 71 | ".plugin-backup-short", |
| 72 | "human-backup", |
| 73 | ); |
| 74 | let dotted = cfg.user_plugins_dir.join("demo.bak"); |
| 75 | fs::create_dir(&dotted).unwrap(); |
| 76 | fs::write( |
| 77 | dotted.join("plugin.json"), |
| 78 | serde_json::json!({ |
| 79 | "$schema": crate::agent_plugin::PLUGIN_SCHEMA_URL, |
| 80 | "name": "demo.bak", "version": "1.0.0" |
| 81 | }) |
| 82 | .to_string(), |
| 83 | ) |
| 84 | .unwrap(); |
| 85 | let registry = discover_with_config(&cfg); |
| 86 | assert!(registry.get("private-backup").is_none()); |
| 87 | assert!(registry.get("human-staging").is_some()); |
| 88 | assert!(registry.get("human-backup").is_some()); |
| 89 | assert_eq!( |
| 90 | registry.get("demo.bak").unwrap().canonical_root, |
| 91 | dotted.canonicalize().unwrap() |
| 92 | ); |
| 93 | assert_eq!(registry.len(), 3); |
| 94 | assert!(backup.path().join("plugin.toml").is_file()); |
| 95 | assert!(!cfg.state_path.exists()); |
| 96 | } |
| 97 |