| 1 | use std::fs; |
| 2 | use std::path::PathBuf; |
| 3 | |
| 4 | use tempfile::TempDir; |
| 5 | |
| 6 | use super::PluginRegistry; |
| 7 | use super::discovery::{DiscoveryConfig, discover_with_config}; |
| 8 | |
| 9 | pub(crate) struct DeclarativePluginFixture { |
| 10 | _temp: TempDir, |
| 11 | pub workspace: PathBuf, |
| 12 | pub marker: PathBuf, |
| 13 | pub config: DiscoveryConfig, |
| 14 | pub registry: PluginRegistry, |
| 15 | } |
| 16 | |
| 17 | impl DeclarativePluginFixture { |
| 18 | pub fn new() -> Self { |
| 19 | let temp = TempDir::new().expect("plugin fixture tempdir"); |
| 20 | let workspace = temp.path().join("project"); |
| 21 | let user_plugins_dir = temp.path().join("user"); |
| 22 | let plugin = user_plugins_dir.join("runtime-demo"); |
| 23 | let marker = workspace.join("plugin-hook-ran"); |
| 24 | fs::create_dir_all(&workspace).expect("workspace"); |
| 25 | fs::create_dir_all(plugin.join("commands")).expect("commands"); |
| 26 | fs::create_dir_all(plugin.join("agents")).expect("agents"); |
| 27 | fs::create_dir_all(plugin.join("hooks")).expect("hooks"); |
| 28 | fs::write( |
| 29 | plugin.join("plugin.toml"), |
| 30 | "schema_version = 1\n[plugin]\nname = \"runtime-demo\"\nversion = \"1.0.0\"\n[commands]\npath = \"commands\"\n[agents]\npath = \"agents\"\n[hooks]\npath = \"hooks\"\n", |
| 31 | ) |
| 32 | .expect("manifest"); |
| 33 | fs::write( |
| 34 | plugin.join("commands/plugin-hello.md"), |
| 35 | "---\ndescription: Send a reviewed plugin greeting\n---\nhello from plugin $ARGUMENTS", |
| 36 | ) |
| 37 | .expect("command"); |
| 38 | fs::write( |
| 39 | plugin.join("agents/plugin-scout.toml"), |
| 40 | "id = \"plugin-scout\"\ndisplay_name = \"Plugin Scout\"\ndescription = \"Reviewed staged scout\"\nrole_hint = \"scout\"\n", |
| 41 | ) |
| 42 | .expect("agent"); |
| 43 | let command = format!("printf plugin-hook-ran > {}", marker.display()); |
| 44 | fs::write( |
| 45 | plugin.join("hooks/hooks.toml"), |
| 46 | format!( |
| 47 | "enabled = true\n\n[[hooks]]\nname = \"plugin-start\"\nevent = \"session_start\"\ncommand = {}\ncontinue_on_error = false\n", |
| 48 | toml::Value::String(command) |
| 49 | ), |
| 50 | ) |
| 51 | .expect("hooks"); |
| 52 | |
| 53 | let config = DiscoveryConfig { |
| 54 | workspace: workspace.clone(), |
| 55 | user_plugins_dir, |
| 56 | workspace_plugins_dir: workspace.join(".codewhale/plugins"), |
| 57 | builtin_plugin_dirs: Vec::new(), |
| 58 | state_path: temp.path().join("state/plugin-state.json"), |
| 59 | }; |
| 60 | let mut registry = discover_with_config(&config); |
| 61 | registry.trust("runtime-demo").expect("trust plugin"); |
| 62 | registry.enable("runtime-demo").expect("enable plugin"); |
| 63 | let registry = discover_with_config(&config); |
| 64 | assert!( |
| 65 | registry.is_active("runtime-demo"), |
| 66 | "restart restores plugin" |
| 67 | ); |
| 68 | |
| 69 | Self { |
| 70 | _temp: temp, |
| 71 | workspace, |
| 72 | marker, |
| 73 | config, |
| 74 | registry, |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | pub fn disable_from_fresh_registry(&self) -> PluginRegistry { |
| 79 | let mut registry = discover_with_config(&self.config); |
| 80 | registry.disable("runtime-demo").expect("disable plugin"); |
| 81 | discover_with_config(&self.config) |
| 82 | } |
| 83 | |
| 84 | pub fn revoke_from_fresh_registry(&self) -> PluginRegistry { |
| 85 | let mut registry = discover_with_config(&self.config); |
| 86 | registry |
| 87 | .revoke_trust("runtime-demo") |
| 88 | .expect("revoke plugin"); |
| 89 | discover_with_config(&self.config) |
| 90 | } |
| 91 | } |
| 92 |