| 1 | use super::*; |
| 2 | use codewhale_engine::command_catalog::{BuiltinCommand, CommandCatalog, UserCommand}; |
| 3 | |
| 4 | struct Catalog( |
| 5 | Arc<std::sync::Mutex<Option<String>>>, |
| 6 | Arc<std::sync::Mutex<Vec<String>>>, |
| 7 | ); |
| 8 | |
| 9 | impl CommandCatalog for Catalog { |
| 10 | fn answers_to(&self, _name: &str) -> bool { |
| 11 | false |
| 12 | } |
| 13 | fn builtins(&self) -> Vec<BuiltinCommand> { |
| 14 | Vec::new() |
| 15 | } |
| 16 | fn user_commands(&self, _workspace: &Path) -> Vec<UserCommand> { |
| 17 | let plugin_id = self.0.lock().expect("plugin id").clone(); |
| 18 | let command = |name: &str, plugin: Option<String>| UserCommand { |
| 19 | name: name.to_string(), |
| 20 | aliases: Vec::new(), |
| 21 | description: Some("A command.".to_string()), |
| 22 | usage: None, |
| 23 | takes_arguments: false, |
| 24 | hidden: false, |
| 25 | extension: false, |
| 26 | plugin_id: plugin, |
| 27 | workspace_sourced: false, |
| 28 | builtin_bindings: Vec::new(), |
| 29 | }; |
| 30 | vec![command("mine", None), command("ship", plugin_id)] |
| 31 | } |
| 32 | fn user_commands_with_plugins( |
| 33 | &self, |
| 34 | workspace: &Path, |
| 35 | plugins: &codewhale_plugins::PluginRegistry, |
| 36 | ) -> Vec<UserCommand> { |
| 37 | *self.1.lock().expect("adopted") = plugins |
| 38 | .list() |
| 39 | .iter() |
| 40 | .map(|plugin| plugin.name().to_string()) |
| 41 | .collect(); |
| 42 | self.user_commands(workspace) |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | #[tokio::test] |
| 47 | async fn the_index_lists_every_kind_once_and_a_plugin_carries_what_it_adds() -> Result<()> { |
| 48 | let _env = lock_test_env(); |
| 49 | let temp = tempfile::tempdir()?; |
| 50 | let root = temp.path().join("index-route"); |
| 51 | let workspace = root.join("workspace"); |
| 52 | fs::create_dir_all(&workspace)?; |
| 53 | let plugin_id = Arc::new(std::sync::Mutex::new(None)); |
| 54 | let adopted = Arc::new(std::sync::Mutex::new(Vec::new())); |
| 55 | let _catalog = codewhale_engine::command_catalog::CatalogGuard::install(Arc::new(Catalog( |
| 56 | plugin_id.clone(), |
| 57 | adopted.clone(), |
| 58 | ))); |
| 59 | let sources = root.join("sources"); |
| 60 | fs::create_dir_all(&sources)?; |
| 61 | let bundle = write_plugin_source_bundle(&sources, "indexed")?; |
| 62 | fs::create_dir_all(bundle.join("commands"))?; |
| 63 | fs::write(bundle.join("commands/ship.md"), "Ship it.\n")?; |
| 64 | fs::create_dir_all(bundle.join("agents"))?; |
| 65 | fs::write(bundle.join("agents/reviewer.toml"), "name = \"reviewer\"\n")?; |
| 66 | fs::write( |
| 67 | bundle.join("plugin.toml"), |
| 68 | "schema_version = 1\n[plugin]\nname = \"indexed\"\nversion = \"1.0.0\"\n\ |
| 69 | description = \"test bundle\"\n\n[skills]\npath = \"skills\"\n\ |
| 70 | [commands]\npath = \"commands\"\n[agents]\npath = \"agents\"\n", |
| 71 | )?; |
| 72 | |
| 73 | let Some((addr, handle)) = spawn_plugin_api_server(root, workspace).await? else { |
| 74 | return Ok(()); |
| 75 | }; |
| 76 | let client = codewhale_release::tls::reqwest_client(); |
| 77 | plugin_ui::install_active( |
| 78 | &client, |
| 79 | &format!("http://{addr}/v1/apps/plugins"), |
| 80 | bundle.display().to_string(), |
| 81 | ) |
| 82 | .await?; |
| 83 | |
| 84 | let listed: Value = client |
| 85 | .get(format!("http://{addr}/v1/apps/plugins")) |
| 86 | .send() |
| 87 | .await? |
| 88 | .error_for_status()? |
| 89 | .json() |
| 90 | .await?; |
| 91 | let id = listed["plugins"] |
| 92 | .as_array() |
| 93 | .and_then(|plugins| plugins.iter().find(|plugin| plugin["name"] == "indexed")) |
| 94 | .and_then(|plugin| plugin["id"].as_str()) |
| 95 | .expect("installed plugin id") |
| 96 | .to_string(); |
| 97 | *plugin_id.lock().expect("plugin id") = Some(id.clone()); |
| 98 | |
| 99 | let commands: Value = client |
| 100 | .get(format!("http://{addr}/v1/commands")) |
| 101 | .send() |
| 102 | .await? |
| 103 | .error_for_status()? |
| 104 | .json() |
| 105 | .await?; |
| 106 | let ship = commands["commands"] |
| 107 | .as_array() |
| 108 | .and_then(|rows| rows.iter().find(|row| row["name"] == "ship")) |
| 109 | .expect("ship row"); |
| 110 | assert_eq!(ship["plugin_id"], id.as_str()); |
| 111 | assert!( |
| 112 | adopted |
| 113 | .lock() |
| 114 | .expect("adopted") |
| 115 | .contains(&"indexed".to_string()), |
| 116 | "/v1/commands hands the workspace's plugins to the catalog" |
| 117 | ); |
| 118 | |
| 119 | let detail: Value = client |
| 120 | .get(format!("http://{addr}/v1/apps/plugins/indexed")) |
| 121 | .send() |
| 122 | .await? |
| 123 | .error_for_status()? |
| 124 | .json() |
| 125 | .await?; |
| 126 | assert_eq!(detail["review"]["commands"], serde_json::json!(["ship"])); |
| 127 | assert_eq!(detail["review"]["agents"], serde_json::json!(["reviewer"])); |
| 128 | |
| 129 | let index: Value = client |
| 130 | .get(format!("http://{addr}/v1/apps/plugin-index")) |
| 131 | .send() |
| 132 | .await? |
| 133 | .error_for_status()? |
| 134 | .json() |
| 135 | .await?; |
| 136 | let items = index["items"].as_array().expect("items"); |
| 137 | let find = |id: &str| items.iter().find(|item| item["id"] == id); |
| 138 | |
| 139 | let plugin = items |
| 140 | .iter() |
| 141 | .find(|item| item["kind"] == "plugin" && item["row"]["name"] == "indexed") |
| 142 | .unwrap_or_else(|| panic!("plugin row in {index}")); |
| 143 | assert_eq!(plugin["kind"], "plugin"); |
| 144 | assert_eq!(plugin["state"], "enabled"); |
| 145 | assert_eq!(plugin["adds"]["skills"], serde_json::json!(["greet"])); |
| 146 | assert_eq!(plugin["adds"]["commands"], serde_json::json!(["ship"])); |
| 147 | assert_eq!(plugin["adds"]["profiles"], serde_json::json!(["reviewer"])); |
| 148 | assert_eq!( |
| 149 | plugin["labels"], |
| 150 | serde_json::json!(["skill", "command", "profile"]) |
| 151 | ); |
| 152 | assert_eq!(plugin["row"]["name"], "indexed"); |
| 153 | |
| 154 | assert!(find("skill:greet").is_none()); |
| 155 | assert!(find("command:ship").is_none()); |
| 156 | let mine = find("command:mine").expect("a hand-written command is a row"); |
| 157 | assert_eq!(mine["source"], "user"); |
| 158 | assert_eq!(mine["labels"], serde_json::json!(["command"])); |
| 159 | assert!( |
| 160 | items |
| 161 | .iter() |
| 162 | .any(|item| item["kind"] == "plugin" && item["tier"] == "builtin"), |
| 163 | "built-in switches are rows" |
| 164 | ); |
| 165 | assert_eq!( |
| 166 | index["unavailable"].as_array().map(Vec::len), |
| 167 | Some(0), |
| 168 | "{index}" |
| 169 | ); |
| 170 | |
| 171 | handle.abort(); |
| 172 | Ok(()) |
| 173 | } |
| 174 | |
| 175 | #[tokio::test] |
| 176 | async fn the_index_keeps_same_name_hooks_distinct() -> Result<()> { |
| 177 | let _env = lock_test_env(); |
| 178 | let temp = tempfile::tempdir()?; |
| 179 | let root = temp.path().join("hook-index-route"); |
| 180 | let workspace = root.join("workspace"); |
| 181 | let mut config = Config::default(); |
| 182 | config.hooks = Some(codewhale_config::hooks::HooksConfig { |
| 183 | hooks: vec![ |
| 184 | codewhale_config::hooks::Hook::new( |
| 185 | codewhale_config::hooks::HookEvent::SessionStart, |
| 186 | "echo first", |
| 187 | ) |
| 188 | .with_name("gate"), |
| 189 | codewhale_config::hooks::Hook::new( |
| 190 | codewhale_config::hooks::HookEvent::SessionStart, |
| 191 | "echo second", |
| 192 | ) |
| 193 | .with_name("gate"), |
| 194 | codewhale_config::hooks::Hook::new( |
| 195 | codewhale_config::hooks::HookEvent::SessionStart, |
| 196 | "echo third", |
| 197 | ), |
| 198 | codewhale_config::hooks::Hook::new( |
| 199 | codewhale_config::hooks::HookEvent::SessionStart, |
| 200 | "echo fourth", |
| 201 | ) |
| 202 | .with_name("session_start 3"), |
| 203 | ], |
| 204 | enabled: true, |
| 205 | ..Default::default() |
| 206 | }); |
| 207 | let (addr, _runtime_threads, handle) = |
| 208 | spawn_test_server_with_root_token_mobile_workspace_and_overrides( |
| 209 | root.clone(), |
| 210 | root.join("sessions"), |
| 211 | None, |
| 212 | false, |
| 213 | workspace.clone(), |
| 214 | TestServerOverrides { |
| 215 | config: Some(config), |
| 216 | plugin_discovery: Some(isolated_plugin_discovery(&root, &workspace)), |
| 217 | ..TestServerOverrides::default() |
| 218 | }, |
| 219 | ) |
| 220 | .await? |
| 221 | .expect("hook index test server"); |
| 222 | let client = codewhale_release::tls::reqwest_client(); |
| 223 | let index: Value = client |
| 224 | .get(format!("http://{addr}/v1/apps/plugin-index")) |
| 225 | .send() |
| 226 | .await? |
| 227 | .error_for_status()? |
| 228 | .json() |
| 229 | .await?; |
| 230 | let hooks: Vec<_> = index["items"] |
| 231 | .as_array() |
| 232 | .expect("items") |
| 233 | .iter() |
| 234 | .filter(|item| item["kind"] == "hook") |
| 235 | .collect(); |
| 236 | assert_eq!(hooks.len(), 4, "{index}"); |
| 237 | for (ordinal, hook) in hooks.iter().enumerate() { |
| 238 | assert_eq!( |
| 239 | hook["id"], |
| 240 | format!("hook:user:session_start:{}", ordinal + 1) |
| 241 | ); |
| 242 | assert_eq!(hook["state"], "enabled"); |
| 243 | } |
| 244 | assert_eq!(hooks[0]["name"], "gate"); |
| 245 | assert_eq!(hooks[1]["name"], "gate"); |
| 246 | assert_eq!(hooks[2]["name"], "session_start 3"); |
| 247 | assert_eq!(hooks[3]["name"], "session_start 3"); |
| 248 | assert_eq!(hooks[3]["row"]["command"], "echo fourth"); |
| 249 | assert_eq!(hooks[0]["row"]["command"], "echo first"); |
| 250 | assert_eq!(hooks[1]["row"]["command"], "echo second"); |
| 251 | handle.abort(); |
| 252 | Ok(()) |
| 253 | } |
| 254 |