| 1 | use super::{ExtensionHostManager, ExtensionHostOptions, HostAttachment}; |
| 2 | use crate::command_catalog::{BuiltinCommand, CatalogGuard, CommandCatalog, UserCommand}; |
| 3 | use crate::tools::spec::{ToolContext, ToolSpec}; |
| 4 | use codewhale_plugins::PluginRegistry; |
| 5 | use codewhale_plugins::discovery::{DiscoveryConfig, discover_with_config}; |
| 6 | use std::path::{Path, PathBuf}; |
| 7 | use std::sync::Arc; |
| 8 | |
| 9 | /// What the registry is told is a built-in command in the tests that are not |
| 10 | /// about the command table. Those that are (`commands::extension_host_tests`) |
| 11 | /// install the real one: this module may not depend on `crate::commands`. |
| 12 | #[derive(Debug)] |
| 13 | struct StubBuiltinCommands; |
| 14 | |
| 15 | impl CommandCatalog for StubBuiltinCommands { |
| 16 | fn answers_to(&self, name: &str) -> bool { |
| 17 | matches!(name, "help" | "trust" | "model" | "jihua" | "stub-alias") |
| 18 | } |
| 19 | fn builtins(&self) -> Vec<BuiltinCommand> { |
| 20 | Vec::new() |
| 21 | } |
| 22 | fn user_commands(&self, _: &Path) -> Vec<UserCommand> { |
| 23 | Vec::new() |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | /// The stub catalog, for this thread until the guard drops. |
| 28 | pub(super) fn stub_builtin_commands() -> CatalogGuard { |
| 29 | CatalogGuard::install(Arc::new(StubBuiltinCommands)) |
| 30 | } |
| 31 | |
| 32 | pub fn fixtures_dir() -> PathBuf { |
| 33 | Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/extension_host") |
| 34 | } |
| 35 | |
| 36 | pub fn fake_authority(plugin_id: &str) -> codewhale_config::PluginAuthority { |
| 37 | codewhale_config::PluginAuthority { |
| 38 | plugin_id: codewhale_config::PluginId(plugin_id.to_string()), |
| 39 | plugin_name: plugin_id.to_string(), |
| 40 | workspace: PathBuf::from("/w"), |
| 41 | state_path: PathBuf::from("/s"), |
| 42 | managed_policy_path: Some(PathBuf::from("/managed-policy.json")), |
| 43 | managed_policy_source: Some(PathBuf::from("/managed-policy.json")), |
| 44 | source_manifest: PathBuf::from("/m"), |
| 45 | staged_manifest: PathBuf::from("/sm"), |
| 46 | content_hash: format!("hash-{plugin_id}"), |
| 47 | capability_hash: "cap".to_string(), |
| 48 | state_generation: 1, |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | /// A Node for the integration tests, or `None` (skip) when there is none and |
| 53 | /// the tests were not explicitly required. |
| 54 | pub fn node_for_tests(test: &str) -> Option<PathBuf> { |
| 55 | let resolution = codewhale_dependencies::resolve_extension_host_runtime( |
| 56 | codewhale_config::extension_host::ExtensionHostRuntime::Node, |
| 57 | None, |
| 58 | None, |
| 59 | crate::extension_host::bundle_sha256(), |
| 60 | ); |
| 61 | match resolution.selected.as_ref() { |
| 62 | Some(runtime) => Some(runtime.path.clone()), |
| 63 | None if std::env::var_os("CODEWHALE_EXT_HOST_TESTS").is_some() => panic!( |
| 64 | "{test}: CODEWHALE_EXT_HOST_TESTS is set but no Node ^22.19 || >=24 was found: {}", |
| 65 | resolution.failure() |
| 66 | ), |
| 67 | None => { |
| 68 | eprintln!( |
| 69 | "skipping {test}: no Node ^22.19 || >=24 ({})", |
| 70 | resolution.failure() |
| 71 | ); |
| 72 | None |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | /// Fixture plugins installed into a private user plugin dir through the |
| 78 | /// reviewed installer (`plugins::install`, local path), then reviewed |
| 79 | /// (trusted) and enabled through the real registry. Callers must hold a |
| 80 | /// `TestPolicyGuard::extension_host(true)` on this thread. |
| 81 | pub struct FixturePlugins { |
| 82 | _temp: codewhale_plugins::test_fixture::PluginTempDir, |
| 83 | pub config: DiscoveryConfig, |
| 84 | pub root: PathBuf, |
| 85 | /// The stub built-in command catalog, so a fixture plugin's commands can |
| 86 | /// register; a test about the real table installs its own after this. |
| 87 | _commands: CatalogGuard, |
| 88 | } |
| 89 | |
| 90 | impl FixturePlugins { |
| 91 | pub async fn new(names: &[&str]) -> Self { |
| 92 | use codewhale_plugins::install::{ |
| 93 | DEFAULT_MAX_SIZE_BYTES, PluginInstallOutcome, PluginInstallSource, install, |
| 94 | }; |
| 95 | let temp = codewhale_plugins::test_fixture::PluginTempDir::new().unwrap(); |
| 96 | let workspace = temp.path().join("project"); |
| 97 | let user = temp.path().join("user"); |
| 98 | std::fs::create_dir_all(&workspace).unwrap(); |
| 99 | for name in names { |
| 100 | let outcome = install( |
| 101 | PluginInstallSource::LocalPath(fixtures_dir().join(name)), |
| 102 | &user, |
| 103 | DEFAULT_MAX_SIZE_BYTES, |
| 104 | &codewhale_net::network_policy::NetworkPolicy::default(), |
| 105 | false, |
| 106 | &|_| None, |
| 107 | ) |
| 108 | .await |
| 109 | .unwrap_or_else(|error| panic!("install {name}: {error:#}")); |
| 110 | assert!( |
| 111 | matches!(outcome, PluginInstallOutcome::Installed(ref installed) if installed.name == *name), |
| 112 | "install {name}: {outcome:?}" |
| 113 | ); |
| 114 | } |
| 115 | let config = DiscoveryConfig { |
| 116 | workspace: workspace.clone(), |
| 117 | user_plugins_dir: user, |
| 118 | workspace_plugins_dir: workspace.join(".codewhale/plugins"), |
| 119 | builtin_plugin_dirs: Vec::new(), |
| 120 | state_path: temp.path().join("state/plugin-state.json"), |
| 121 | }; |
| 122 | let mut registry = discover_with_config(&config); |
| 123 | for name in names { |
| 124 | registry |
| 125 | .trust(name) |
| 126 | .unwrap_or_else(|e| panic!("trust {name}: {e}")); |
| 127 | registry |
| 128 | .enable(name) |
| 129 | .unwrap_or_else(|e| panic!("enable {name}: {e}")); |
| 130 | } |
| 131 | let root = temp.path().join("home"); |
| 132 | let fixture = Self { |
| 133 | _temp: temp, |
| 134 | config, |
| 135 | root, |
| 136 | _commands: stub_builtin_commands(), |
| 137 | }; |
| 138 | let registry = fixture.registry(); |
| 139 | for name in names { |
| 140 | assert!( |
| 141 | registry.is_active(name), |
| 142 | "{name} must be active under policy v4" |
| 143 | ); |
| 144 | } |
| 145 | fixture |
| 146 | } |
| 147 | |
| 148 | pub fn registry(&self) -> Arc<PluginRegistry> { |
| 149 | Arc::new(discover_with_config(&self.config)) |
| 150 | } |
| 151 | |
| 152 | pub fn disable(&self, name: &str) -> Arc<PluginRegistry> { |
| 153 | let mut registry = discover_with_config(&self.config); |
| 154 | registry.disable(name).unwrap(); |
| 155 | self.registry() |
| 156 | } |
| 157 | |
| 158 | pub fn workspace(&self) -> &Path { |
| 159 | &self.config.workspace |
| 160 | } |
| 161 | |
| 162 | pub fn manager(&self, node: PathBuf) -> Arc<ExtensionHostManager> { |
| 163 | self.manager_with_supervision(node, Default::default()) |
| 164 | } |
| 165 | |
| 166 | pub fn manager_with_supervision( |
| 167 | &self, |
| 168 | node: PathBuf, |
| 169 | supervision: super::SupervisionOptions, |
| 170 | ) -> Arc<ExtensionHostManager> { |
| 171 | Arc::new(ExtensionHostManager::new(ExtensionHostOptions { |
| 172 | runtime: codewhale_config::extension_host::ExtensionHostRuntime::Node, |
| 173 | node_override: Some(node), |
| 174 | root: Some(self.root.clone()), |
| 175 | supervision, |
| 176 | ..Default::default() |
| 177 | })) |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | pub fn host_tool(engine: &HostAttachment, workspace: &Path, name: &str) -> Arc<dyn ToolSpec> { |
| 182 | let mut registry = crate::tools::registry::ToolRegistryBuilder::new() |
| 183 | .build(ToolContext::new(workspace).with_plugin_registry(engine.plugin_view())); |
| 184 | let installed = engine.install_tools(&mut registry); |
| 185 | assert!( |
| 186 | installed.contains(&name.to_string()), |
| 187 | "{name} not installed: {installed:?}" |
| 188 | ); |
| 189 | registry.get(name).unwrap() |
| 190 | } |
| 191 | |
| 192 | pub fn installed(engine: &HostAttachment, workspace: &Path) -> Vec<String> { |
| 193 | let mut registry = crate::tools::registry::ToolRegistryBuilder::new() |
| 194 | .build(ToolContext::new(workspace).with_plugin_registry(engine.plugin_view())); |
| 195 | engine.install_tools(&mut registry) |
| 196 | } |
| 197 | |
| 198 | pub fn plugin_id(fixture: &FixturePlugins, name: &str) -> String { |
| 199 | fixture |
| 200 | .registry() |
| 201 | .get(name) |
| 202 | .unwrap() |
| 203 | .id |
| 204 | .as_str() |
| 205 | .to_string() |
| 206 | } |
| 207 |