| 1 | use codewhale_plugins::activation::PluginActivationCapability; |
| 2 | use codewhale_plugins::discovery::{DiscoveryConfig, discover_with_config}; |
| 3 | use codewhale_plugins::providers::*; |
| 4 | use codewhale_plugins::registry::verify_plugin_component_authority; |
| 5 | use codewhale_runtime::config::Config; |
| 6 | use std::sync::Arc; |
| 7 | |
| 8 | #[test] |
| 9 | fn reviewed_provider_registration_preserves_collisions_and_revokes_existing_routes() { |
| 10 | let temp = tempfile::tempdir().unwrap(); |
| 11 | let workspace = temp.path().join("workspace"); |
| 12 | let plugin = temp.path().join("plugins/provider-demo"); |
| 13 | std::fs::create_dir_all(&workspace).unwrap(); |
| 14 | std::fs::create_dir_all(&plugin).unwrap(); |
| 15 | let mut reviewed_declaration = declaration(); |
| 16 | reviewed_declaration |
| 17 | .http_headers |
| 18 | .insert("X-Route-Group".into(), "reviewed-group".into()); |
| 19 | let manifest = serde_json::json!({ |
| 20 | "$schema": codewhale_plugins::agent_plugin::PLUGIN_SCHEMA_URL, |
| 21 | "name": "provider-demo", "version": "1.0.0", |
| 22 | "extensions": {"net.codewhale": {"providers": {"gateway": reviewed_declaration}}} |
| 23 | }); |
| 24 | std::fs::write( |
| 25 | plugin.join("plugin.json"), |
| 26 | serde_json::to_vec(&manifest).unwrap(), |
| 27 | ) |
| 28 | .unwrap(); |
| 29 | let discovery = DiscoveryConfig { |
| 30 | workspace: workspace.clone(), |
| 31 | user_plugins_dir: temp.path().join("plugins"), |
| 32 | workspace_plugins_dir: workspace.join(".codewhale/plugins"), |
| 33 | builtin_plugin_dirs: vec![], |
| 34 | state_path: temp.path().join("state/plugins.json"), |
| 35 | }; |
| 36 | let mut registry = discover_with_config(&discovery); |
| 37 | let mut config = Config::default(); |
| 38 | apply_providers(&mut config, ®istry).unwrap(); |
| 39 | assert!( |
| 40 | config |
| 41 | .providers |
| 42 | .as_ref() |
| 43 | .is_none_or(|providers| !providers.custom.contains_key("gateway")) |
| 44 | ); |
| 45 | registry.trust("provider-demo").unwrap(); |
| 46 | registry.enable("provider-demo").unwrap(); |
| 47 | let registry = discover_with_config(&discovery); |
| 48 | apply_providers(&mut config, ®istry).unwrap(); |
| 49 | let entry = config |
| 50 | .providers |
| 51 | .as_ref() |
| 52 | .unwrap() |
| 53 | .custom |
| 54 | .get("gateway") |
| 55 | .unwrap(); |
| 56 | assert_eq!(entry.auth_mode.as_deref(), Some("oauth")); |
| 57 | let authority = entry.plugin_authority.clone().unwrap(); |
| 58 | let oauth = entry.oauth.as_ref().unwrap(); |
| 59 | let headers = entry.http_headers.as_ref().unwrap(); |
| 60 | verify_provider_binding( |
| 61 | &authority, |
| 62 | "gateway", |
| 63 | "https://gateway.example/api/", |
| 64 | oauth, |
| 65 | Some(headers), |
| 66 | ) |
| 67 | .unwrap(); |
| 68 | assert!( |
| 69 | verify_provider_binding( |
| 70 | &authority, |
| 71 | "gateway", |
| 72 | "https://other.example/api", |
| 73 | oauth, |
| 74 | Some(headers) |
| 75 | ) |
| 76 | .is_err() |
| 77 | ); |
| 78 | let mut altered_oauth = oauth.clone(); |
| 79 | altered_oauth.scopes.push("admin:write".into()); |
| 80 | assert!( |
| 81 | verify_provider_binding( |
| 82 | &authority, |
| 83 | "gateway", |
| 84 | "https://gateway.example/api", |
| 85 | &altered_oauth, |
| 86 | Some(headers) |
| 87 | ) |
| 88 | .is_err() |
| 89 | ); |
| 90 | let mut altered_headers = headers.clone(); |
| 91 | altered_headers.insert("X-Route-Group".into(), "unreviewed-group".into()); |
| 92 | assert!( |
| 93 | verify_provider_binding( |
| 94 | &authority, |
| 95 | "gateway", |
| 96 | "https://gateway.example/api", |
| 97 | oauth, |
| 98 | Some(&altered_headers) |
| 99 | ) |
| 100 | .is_err() |
| 101 | ); |
| 102 | assert!( |
| 103 | verify_provider_binding( |
| 104 | &authority, |
| 105 | "gateway", |
| 106 | "https://gateway.example/api", |
| 107 | oauth, |
| 108 | Some(&std::collections::HashMap::new()) |
| 109 | ) |
| 110 | .is_err() |
| 111 | ); |
| 112 | assert!( |
| 113 | verify_provider_binding( |
| 114 | &authority, |
| 115 | "other-provider", |
| 116 | "https://gateway.example/api", |
| 117 | oauth, |
| 118 | Some(headers) |
| 119 | ) |
| 120 | .is_err() |
| 121 | ); |
| 122 | |
| 123 | assert!( |
| 124 | config |
| 125 | .custom_models |
| 126 | .as_ref() |
| 127 | .unwrap() |
| 128 | .iter() |
| 129 | .any(|model| model.provider == "gateway" && model.id == "tiny-model") |
| 130 | ); |
| 131 | // Persist only the user's selection; the reviewed route stays in the |
| 132 | // plugin and is reconstructed after parsing the next startup document. |
| 133 | let selection_path = temp.path().join("selection.toml"); |
| 134 | let saved = with_test_registry(Arc::new(registry.clone()), || { |
| 135 | // First use starts from an empty document, with no provider table. |
| 136 | let identity = config.resolve_provider_pin_identity("gateway").unwrap(); |
| 137 | let mut doc = toml_edit::DocumentMut::new(); |
| 138 | codewhale_engine::route_preferences::set_provider_model_document( |
| 139 | &mut doc, |
| 140 | &identity, |
| 141 | "selected-model", |
| 142 | ) |
| 143 | .unwrap(); |
| 144 | let model_only = doc.to_string(); |
| 145 | assert!(!model_only.contains("base_url") && !model_only.contains("oauth")); |
| 146 | let mut parsed = codewhale_runtime::config::parse_config_base(&model_only).unwrap(); |
| 147 | apply_startup_providers(&mut parsed).unwrap(); |
| 148 | assert_eq!( |
| 149 | parsed.providers.as_ref().unwrap().custom["gateway"] |
| 150 | .model |
| 151 | .as_deref(), |
| 152 | Some("selected-model") |
| 153 | ); |
| 154 | codewhale_engine::route_preferences::persist_provider_selection( |
| 155 | Some(&selection_path), |
| 156 | &identity, |
| 157 | Some("selected-model"), |
| 158 | ) |
| 159 | .unwrap(); |
| 160 | std::fs::read_to_string(&selection_path).unwrap() |
| 161 | }); |
| 162 | // The scoped registry is gone; reconstruct using the explicit fixture. |
| 163 | assert!(TEST_REGISTRY.with(|slot| slot.borrow().is_none())); |
| 164 | let mut restored = codewhale_runtime::config::parse_config_base(&saved).unwrap(); |
| 165 | apply_providers(&mut restored, ®istry).unwrap(); |
| 166 | assert_eq!(restored.provider.as_deref(), Some("gateway")); |
| 167 | let restored_entry = restored |
| 168 | .providers |
| 169 | .as_ref() |
| 170 | .unwrap() |
| 171 | .custom |
| 172 | .get("gateway") |
| 173 | .unwrap(); |
| 174 | assert_eq!(restored_entry.model.as_deref(), Some("selected-model")); |
| 175 | assert_eq!( |
| 176 | restored_entry.base_url.as_deref(), |
| 177 | Some("https://gateway.example/api") |
| 178 | ); |
| 179 | assert_eq!(restored_entry.auth_mode.as_deref(), Some("oauth")); |
| 180 | assert_eq!(restored_entry.plugin_authority, Some(authority.clone())); |
| 181 | assert!(!saved.contains("base_url") && !saved.contains("oauth")); |
| 182 | for override_field in [ |
| 183 | "vendor = 'other'", |
| 184 | "api_key = 'other'", |
| 185 | "base_url = 'https://other.example'", |
| 186 | "context_window = 1", |
| 187 | "model_context_windows = { other = 1 }", |
| 188 | "mode = 'other'", |
| 189 | "wire = 'responses'", |
| 190 | "auth_mode = 'none'", |
| 191 | "oauth_credential_generation = 'other'", |
| 192 | "insecure_skip_tls_verify = false", |
| 193 | "allow_insecure_http = false", |
| 194 | "http_headers = {}", |
| 195 | "path_suffix = '/other'", |
| 196 | "reasoning_stream_style = 'other'", |
| 197 | "max_concurrency = 1", |
| 198 | "kind = 'openai-compatible'", |
| 199 | "api_key_env = 'OTHER_KEY'", |
| 200 | ] { |
| 201 | let mut collision = codewhale_runtime::config::parse_config_base(&format!( |
| 202 | "[providers.gateway]\nmodel = 'tiny-model'\n{override_field}\n" |
| 203 | )) |
| 204 | .unwrap(); |
| 205 | assert!( |
| 206 | apply_providers(&mut collision, ®istry).is_err(), |
| 207 | "accepted non-model route override {override_field}" |
| 208 | ); |
| 209 | } |
| 210 | let snapshot = config.providers.as_ref().unwrap().custom.len(); |
| 211 | assert!(apply_providers(&mut config, ®istry).is_err()); |
| 212 | assert_eq!(config.providers.as_ref().unwrap().custom.len(), snapshot); |
| 213 | let mut fresh = discover_with_config(&discovery); |
| 214 | fresh.disable("provider-demo").unwrap(); |
| 215 | assert!( |
| 216 | verify_plugin_component_authority(&authority, PluginActivationCapability::Providers) |
| 217 | .is_err() |
| 218 | ); |
| 219 | } |
| 220 | |
| 221 | fn declaration() -> PluginProviderDeclaration { |
| 222 | serde_json::from_value(serde_json::json!({ |
| 223 | "base_url": "https://gateway.example/api", |
| 224 | "model": "tiny-model", "models": ["tiny-model"], |
| 225 | "oauth": {"issuer":"https://gateway.example", "authorization_endpoint":"https://gateway.example/authorize", "token_endpoint":"https://gateway.example/token", "client_id":"custom-cli", "scopes":["models:invoke"]} |
| 226 | })).unwrap() |
| 227 | } |
| 228 |