| 1 | use super::*; |
| 2 | use codewhale_config::catalog::{ |
| 3 | CatalogOffering, CatalogSource, CatalogStatus, ProviderCatalogDelta, base_url_fingerprint, |
| 4 | now_unix, |
| 5 | }; |
| 6 | use codewhale_config::models_dev::ModelsDevModalities; |
| 7 | use codewhale_config::route::CapabilityState; |
| 8 | |
| 9 | const MODEL: &str = "fixture/vision"; |
| 10 | const FIRST_ENDPOINT: &str = "http://127.0.0.1:9/first/v1"; |
| 11 | const SECOND_ENDPOINT: &str = "http://127.0.0.1:9/second/v1"; |
| 12 | |
| 13 | struct CatalogReset; |
| 14 | |
| 15 | impl Drop for CatalogReset { |
| 16 | fn drop(&mut self) { |
| 17 | cold_process(); |
| 18 | codewhale_runtime::active_workshop::install_active_workshop(None); |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | fn cold_process() { |
| 23 | codewhale_engine::provider_catalog_live::reset_cache_for_test(); |
| 24 | codewhale_engine::provider_lake::clear_live_snapshot(); |
| 25 | } |
| 26 | |
| 27 | fn persist_catalog(config: &Config, images: bool) -> Result<()> { |
| 28 | let identity = config |
| 29 | .active_provider_identity() |
| 30 | .map_err(anyhow::Error::msg)?; |
| 31 | let endpoint = config.base_url_for_route(&identity); |
| 32 | let fingerprint = base_url_fingerprint(&endpoint); |
| 33 | let fetched_at = now_unix(); |
| 34 | let ticket = |
| 35 | codewhale_engine::provider_catalog_live::begin_scoped_refresh(config, &identity, &endpoint) |
| 36 | .map_err(anyhow::Error::msg)?; |
| 37 | let provider = identity.key.as_str(); |
| 38 | assert_eq!( |
| 39 | codewhale_engine::provider_catalog_live::record_success_if_current( |
| 40 | &ticket, |
| 41 | ProviderCatalogDelta { |
| 42 | provider: provider.into(), |
| 43 | base_url_fingerprint: fingerprint.clone(), |
| 44 | fetched_at, |
| 45 | offerings: vec![CatalogOffering { |
| 46 | provider: provider.into(), |
| 47 | wire_model_id: MODEL.into(), |
| 48 | endpoint_key: "chat".into(), |
| 49 | modalities: Some(ModelsDevModalities { |
| 50 | input: if images { |
| 51 | vec!["text".into(), "image".into()] |
| 52 | } else { |
| 53 | vec!["text".into()] |
| 54 | }, |
| 55 | output: vec!["text".into()], |
| 56 | }), |
| 57 | source: CatalogSource::Live { |
| 58 | base_url_fingerprint: fingerprint, |
| 59 | fetched_at, |
| 60 | }, |
| 61 | ..Default::default() |
| 62 | }], |
| 63 | }, |
| 64 | ), |
| 65 | Some(CatalogStatus::Fresh) |
| 66 | ); |
| 67 | Ok(()) |
| 68 | } |
| 69 | |
| 70 | fn config_for(identity: &str, endpoint: &str) -> Config { |
| 71 | let mut config = { |
| 72 | let __cw_config_init_0_0 = Some(identity.into()); |
| 73 | let mut __cw_config_init_0_value: Config = Default::default(); |
| 74 | __cw_config_init_0_value.provider = __cw_config_init_0_0; |
| 75 | __cw_config_init_0_value |
| 76 | }; |
| 77 | let route = if identity == "openrouter" { |
| 78 | &mut config |
| 79 | .providers |
| 80 | .get_or_insert_with(Default::default) |
| 81 | .openrouter |
| 82 | } else { |
| 83 | let route = config |
| 84 | .providers |
| 85 | .get_or_insert_with(Default::default) |
| 86 | .custom |
| 87 | .entry(identity.into()) |
| 88 | .or_default(); |
| 89 | route.kind = Some("openai-compatible".into()); |
| 90 | route |
| 91 | }; |
| 92 | route.base_url = Some(endpoint.into()); |
| 93 | route.auth_mode = Some("none".into()); |
| 94 | route.model = Some(MODEL.into()); |
| 95 | config |
| 96 | } |
| 97 | |
| 98 | fn image_capability(config: &Config) -> CapabilityState { |
| 99 | provider_model_image_input_for_api(config, &config.active_provider_identity().unwrap(), MODEL) |
| 100 | } |
| 101 | |
| 102 | fn open_server_manager(config: &Config, root: &Path) -> Result<SharedRuntimeThreadManager> { |
| 103 | let workspace = root.join("workspace"); |
| 104 | fs::create_dir_all(&workspace)?; |
| 105 | let (manager, _) = open_runtime_threads_for_server( |
| 106 | config, |
| 107 | workspace.clone(), |
| 108 | RuntimeThreadManagerConfig { |
| 109 | mcp_auth_context: Default::default(), |
| 110 | data_dir: root.join("runtime"), |
| 111 | task_data_dir: root.join("tasks"), |
| 112 | sessions_dir: None, |
| 113 | max_active_threads: 2, |
| 114 | }, |
| 115 | Arc::new(codewhale_plugins::PluginRegistry::empty(&workspace)), |
| 116 | )?; |
| 117 | Ok(manager) |
| 118 | } |
| 119 | |
| 120 | #[test] |
| 121 | fn headless_startup_publishes_cold_endpoint_catalog_capabilities() -> Result<()> { |
| 122 | // `active_workshop_test_guard()` takes the env barrier before the workshop |
| 123 | // gate and holds both (#6306), so it is the whole lock acquisition. Taking |
| 124 | // `lock_test_env()` again here parks this thread on a non-reentrant mutex |
| 125 | // it already owns; the ordering that #6049 hand-rolled now lives in the guard. |
| 126 | let _workshop = codewhale_runtime::active_workshop::active_workshop_test_guard(); |
| 127 | let _offline = EnvVarGuard::set("CODEWHALE_DISABLE_CLOUD_FACTS", "1"); |
| 128 | let _live = codewhale_engine::provider_lake::lock_live_snapshot(); |
| 129 | let home = tempfile::tempdir()?; |
| 130 | let _home = EnvVarGuard::set("CODEWHALE_HOME", home.path()); |
| 131 | let _reset = CatalogReset; |
| 132 | cold_process(); |
| 133 | let config = config_for("openrouter", FIRST_ENDPOINT); |
| 134 | persist_catalog(&config, true)?; |
| 135 | cold_process(); |
| 136 | |
| 137 | assert!( |
| 138 | provider_models_for_api( |
| 139 | &config, |
| 140 | &(config).test_identity_for_kind(ProviderKind::Openrouter) |
| 141 | ) |
| 142 | .contains(&MODEL.to_string()) |
| 143 | ); |
| 144 | assert_eq!(image_capability(&config), CapabilityState::Unknown); |
| 145 | let _manager = open_server_manager(&config, home.path())?; |
| 146 | assert_eq!(image_capability(&config), CapabilityState::Supported); |
| 147 | assert_eq!( |
| 148 | image_capability(&config_for("openrouter", SECOND_ENDPOINT)), |
| 149 | CapabilityState::Unknown, |
| 150 | "another endpoint must not inherit the saved capabilities" |
| 151 | ); |
| 152 | Ok(()) |
| 153 | } |
| 154 | |
| 155 | #[tokio::test(flavor = "current_thread")] |
| 156 | async fn headless_reload_publishes_only_the_accepted_identity_and_endpoint() -> Result<()> { |
| 157 | // The guard owns the env barrier and the workshop gate — see the |
| 158 | // headless_startup twin. |
| 159 | let _workshop = codewhale_runtime::active_workshop::active_workshop_test_guard(); |
| 160 | let _offline = EnvVarGuard::set("CODEWHALE_DISABLE_CLOUD_FACTS", "1"); |
| 161 | let _live = codewhale_engine::provider_lake::lock_live_snapshot(); |
| 162 | let home = tempfile::tempdir()?; |
| 163 | let _home = EnvVarGuard::set("CODEWHALE_HOME", home.path()); |
| 164 | let _reset = CatalogReset; |
| 165 | cold_process(); |
| 166 | let first = config_for("vision-one", FIRST_ENDPOINT); |
| 167 | let second = config_for("vision-two", FIRST_ENDPOINT); |
| 168 | let moved = config_for("vision-one", SECOND_ENDPOINT); |
| 169 | persist_catalog(&first, true)?; |
| 170 | persist_catalog(&second, false)?; |
| 171 | persist_catalog(&moved, false)?; |
| 172 | cold_process(); |
| 173 | |
| 174 | let manager = open_server_manager(&first, home.path())?; |
| 175 | assert_eq!(image_capability(&first), CapabilityState::Supported); |
| 176 | assert_eq!(image_capability(&second), CapabilityState::Unknown); |
| 177 | |
| 178 | manager.reload_config(second).await?; |
| 179 | assert_eq!( |
| 180 | image_capability(&manager.read_config()), |
| 181 | CapabilityState::Unsupported |
| 182 | ); |
| 183 | manager |
| 184 | .reload_config(config_for("vision-one", "http://127.0.0.1:9/uncached/v1")) |
| 185 | .await?; |
| 186 | assert_eq!( |
| 187 | image_capability(&manager.read_config()), |
| 188 | CapabilityState::Unknown |
| 189 | ); |
| 190 | manager.reload_config(moved).await?; |
| 191 | assert_eq!( |
| 192 | image_capability(&manager.read_config()), |
| 193 | CapabilityState::Unsupported |
| 194 | ); |
| 195 | manager.reload_config(first).await?; |
| 196 | assert_eq!( |
| 197 | image_capability(&manager.read_config()), |
| 198 | CapabilityState::Supported |
| 199 | ); |
| 200 | Ok(()) |
| 201 | } |
| 202 |