| 1 | use super::*; |
| 2 | use codewhale_engine::conversation_modes::CreatorAvailabilityGuard; |
| 3 | |
| 4 | #[tokio::test] |
| 5 | async fn modes_route_lists_the_table_and_threads_carry_and_refuse_modes() -> Result<()> { |
| 6 | let mut guard = CreatorAvailabilityGuard::set(None); |
| 7 | codewhale_engine::conversation_modes::clear_probe_cache_for_tests(); |
| 8 | let Some((addr, _threads, handle)) = spawn_test_server().await? else { |
| 9 | return Ok(()); |
| 10 | }; |
| 11 | let client = codewhale_release::tls::reqwest_client(); |
| 12 | let base = format!("http://{addr}"); |
| 13 | |
| 14 | let table: Value = client |
| 15 | .get(format!("{base}/v1/modes")) |
| 16 | .send() |
| 17 | .await? |
| 18 | .error_for_status()? |
| 19 | .json() |
| 20 | .await?; |
| 21 | assert_eq!(table["default"], "standard"); |
| 22 | assert_eq!(table["modes"][0]["id"], "standard"); |
| 23 | assert_eq!(table["modes"][0]["source"], "builtin"); |
| 24 | assert_eq!(table["modes"][0]["available"], true); |
| 25 | assert!(table["modes"][0].get("reason").is_none()); |
| 26 | assert_eq!(table["modes"][1]["id"], "creator"); |
| 27 | assert_eq!(table["modes"][1]["available"], true); |
| 28 | assert!(table["modes"][1].get("reason").is_none()); |
| 29 | |
| 30 | let creator_created: Value = client |
| 31 | .post(format!("{base}/v1/threads")) |
| 32 | .json(&json!({"agent_preset": "creator"})) |
| 33 | .send() |
| 34 | .await? |
| 35 | .error_for_status()? |
| 36 | .json() |
| 37 | .await?; |
| 38 | let creator_id = creator_created["id"] |
| 39 | .as_str() |
| 40 | .expect("creator thread id") |
| 41 | .to_string(); |
| 42 | assert_eq!(creator_created["agent_preset"], "creator"); |
| 43 | |
| 44 | let unknown = client |
| 45 | .post(format!("{base}/v1/threads")) |
| 46 | .json(&json!({"agent_preset": "minimal"})) |
| 47 | .send() |
| 48 | .await?; |
| 49 | assert_eq!(unknown.status(), reqwest::StatusCode::BAD_REQUEST); |
| 50 | let body: Value = unknown.json().await?; |
| 51 | assert_eq!(body["error"]["code"], "unknown_mode"); |
| 52 | |
| 53 | let created: Value = client |
| 54 | .post(format!("{base}/v1/threads")) |
| 55 | .json(&json!({"agent_preset": "standard"})) |
| 56 | .send() |
| 57 | .await? |
| 58 | .error_for_status()? |
| 59 | .json() |
| 60 | .await?; |
| 61 | let id = created["id"].as_str().expect("thread id").to_string(); |
| 62 | let second_standard: Value = client |
| 63 | .post(format!("{base}/v1/threads")) |
| 64 | .json(&json!({"agent_preset": "standard"})) |
| 65 | .send() |
| 66 | .await? |
| 67 | .error_for_status()? |
| 68 | .json() |
| 69 | .await?; |
| 70 | let second_id = second_standard["id"] |
| 71 | .as_str() |
| 72 | .expect("second standard thread id") |
| 73 | .to_string(); |
| 74 | let summary: Value = client |
| 75 | .get(format!("{base}/v1/threads/summary")) |
| 76 | .send() |
| 77 | .await? |
| 78 | .error_for_status()? |
| 79 | .json() |
| 80 | .await?; |
| 81 | let rows = summary.as_array().expect("summary rows"); |
| 82 | let creator_row = rows |
| 83 | .iter() |
| 84 | .find(|row| row["id"] == creator_id.as_str()) |
| 85 | .expect("creator summary row"); |
| 86 | assert_eq!(creator_row["agent_preset"], "creator"); |
| 87 | let row = rows |
| 88 | .iter() |
| 89 | .find(|row| row["id"] == id.as_str()) |
| 90 | .expect("standard summary row"); |
| 91 | assert_eq!(row["agent_preset"], "standard"); |
| 92 | |
| 93 | let patched_ok: Value = client |
| 94 | .patch(format!("{base}/v1/threads/{id}")) |
| 95 | .json(&json!({"agent_preset": "creator"})) |
| 96 | .send() |
| 97 | .await? |
| 98 | .error_for_status()? |
| 99 | .json() |
| 100 | .await?; |
| 101 | assert_eq!(patched_ok["agent_preset"], "creator"); |
| 102 | |
| 103 | drop(guard); |
| 104 | guard = CreatorAvailabilityGuard::set(Some("missing runtime")); |
| 105 | let unavailable = client |
| 106 | .post(format!("{base}/v1/threads")) |
| 107 | .json(&json!({"agent_preset": "creator"})) |
| 108 | .send() |
| 109 | .await?; |
| 110 | assert_eq!(unavailable.status(), reqwest::StatusCode::FAILED_DEPENDENCY); |
| 111 | let body: Value = unavailable.json().await?; |
| 112 | assert_eq!(body["error"]["code"], "mode_unavailable"); |
| 113 | |
| 114 | let patched = client |
| 115 | .patch(format!("{base}/v1/threads/{second_id}")) |
| 116 | .json(&json!({"agent_preset": "creator"})) |
| 117 | .send() |
| 118 | .await?; |
| 119 | assert_eq!(patched.status(), reqwest::StatusCode::FAILED_DEPENDENCY); |
| 120 | drop(guard); |
| 121 | |
| 122 | if codewhale_engine::extension_host::test_support::node_for_tests( |
| 123 | "modes_route_lists_the_table_and_threads_carry_and_refuse_modes", |
| 124 | ) |
| 125 | .is_some() |
| 126 | { |
| 127 | let _unforced = CreatorAvailabilityGuard::unforced(); |
| 128 | codewhale_engine::conversation_modes::clear_probe_cache_for_tests(); |
| 129 | let live_table: Value = client |
| 130 | .get(format!("{base}/v1/modes")) |
| 131 | .send() |
| 132 | .await? |
| 133 | .error_for_status()? |
| 134 | .json() |
| 135 | .await?; |
| 136 | assert_eq!(live_table["modes"][1]["id"], "creator"); |
| 137 | assert_eq!(live_table["modes"][1]["available"], true); |
| 138 | assert!(live_table["modes"][1].get("reason").is_none()); |
| 139 | |
| 140 | let live_created: Value = client |
| 141 | .post(format!("{base}/v1/threads")) |
| 142 | .json(&json!({"agent_preset": "creator"})) |
| 143 | .send() |
| 144 | .await? |
| 145 | .error_for_status()? |
| 146 | .json() |
| 147 | .await?; |
| 148 | assert_eq!(live_created["agent_preset"], "creator"); |
| 149 | } |
| 150 | |
| 151 | handle.abort(); |
| 152 | let _ = handle.await; |
| 153 | Ok(()) |
| 154 | } |
| 155 | |
| 156 | #[tokio::test] |
| 157 | async fn catalog_lists_plugin_creator_matching_modes_and_disable_updates_modes() -> Result<()> { |
| 158 | let guard = CreatorAvailabilityGuard::set(None); |
| 159 | codewhale_engine::conversation_modes::clear_probe_cache_for_tests(); |
| 160 | let dir = tempfile::TempDir::new()?; |
| 161 | let config_path = dir.path().join("config.toml"); |
| 162 | std::fs::write(&config_path, "# initial\n")?; |
| 163 | let Some((addr, _threads, handle)) = |
| 164 | spawn_test_server_with_config_path(config_path.clone()).await? |
| 165 | else { |
| 166 | return Ok(()); |
| 167 | }; |
| 168 | let client = codewhale_release::tls::reqwest_client(); |
| 169 | let base = format!("http://{addr}"); |
| 170 | |
| 171 | let catalog: Value = client |
| 172 | .get(format!("{base}/v1/apps/plugins/catalog")) |
| 173 | .send() |
| 174 | .await? |
| 175 | .error_for_status()? |
| 176 | .json() |
| 177 | .await?; |
| 178 | let modes: Value = client |
| 179 | .get(format!("{base}/v1/modes")) |
| 180 | .send() |
| 181 | .await? |
| 182 | .error_for_status()? |
| 183 | .json() |
| 184 | .await?; |
| 185 | let catalog_row = catalog["builtin"] |
| 186 | .as_array() |
| 187 | .and_then(|rows| rows.iter().find(|row| row["id"] == "plugin_creator")) |
| 188 | .expect("plugin_creator row in builtin catalog"); |
| 189 | assert_eq!(catalog_row["enabled"], true); |
| 190 | assert_eq!(catalog_row["available"], true); |
| 191 | assert!(catalog_row["unavailable_reason"].is_null()); |
| 192 | assert_eq!(catalog_row["available"], modes["modes"][1]["available"]); |
| 193 | assert_eq!( |
| 194 | catalog_row["unavailable_reason"], |
| 195 | modes["modes"][1]["reason"] |
| 196 | ); |
| 197 | |
| 198 | let disabled: Value = client |
| 199 | .post(format!( |
| 200 | "{base}/v1/apps/plugins/catalog/plugin_creator/disable" |
| 201 | )) |
| 202 | .send() |
| 203 | .await? |
| 204 | .error_for_status()? |
| 205 | .json() |
| 206 | .await?; |
| 207 | assert_eq!(disabled["row"]["id"], "plugin_creator"); |
| 208 | assert_eq!(disabled["row"]["enabled"], false); |
| 209 | assert_eq!(disabled["row"]["available"], false); |
| 210 | assert_eq!( |
| 211 | disabled["row"]["unavailable_reason"], |
| 212 | codewhale_engine::conversation_modes::CREATOR_KEY_OFF_REASON |
| 213 | ); |
| 214 | let saved = std::fs::read_to_string(&config_path)?; |
| 215 | assert!(saved.contains("plugin_creator = false"), "{saved}"); |
| 216 | |
| 217 | let modes_after: Value = client |
| 218 | .get(format!("{base}/v1/modes")) |
| 219 | .send() |
| 220 | .await? |
| 221 | .error_for_status()? |
| 222 | .json() |
| 223 | .await?; |
| 224 | assert_eq!(modes_after["modes"][1]["id"], "creator"); |
| 225 | assert_eq!(modes_after["modes"][1]["available"], false); |
| 226 | assert_eq!( |
| 227 | modes_after["modes"][1]["reason"], |
| 228 | codewhale_engine::conversation_modes::CREATOR_KEY_OFF_REASON |
| 229 | ); |
| 230 | |
| 231 | let enabled: Value = client |
| 232 | .post(format!( |
| 233 | "{base}/v1/apps/plugins/catalog/plugin_creator/enable" |
| 234 | )) |
| 235 | .send() |
| 236 | .await? |
| 237 | .error_for_status()? |
| 238 | .json() |
| 239 | .await?; |
| 240 | assert_eq!(enabled["row"]["enabled"], true); |
| 241 | assert_eq!(enabled["row"]["available"], true); |
| 242 | assert!(enabled["row"]["unavailable_reason"].is_null()); |
| 243 | |
| 244 | let modes_restored: Value = client |
| 245 | .get(format!("{base}/v1/modes")) |
| 246 | .send() |
| 247 | .await? |
| 248 | .error_for_status()? |
| 249 | .json() |
| 250 | .await?; |
| 251 | assert_eq!(modes_restored["modes"][1]["available"], true); |
| 252 | assert!(modes_restored["modes"][1].get("reason").is_none()); |
| 253 | drop(guard); |
| 254 | |
| 255 | if codewhale_engine::extension_host::test_support::node_for_tests( |
| 256 | "catalog_lists_plugin_creator_matching_modes_and_disable_updates_modes", |
| 257 | ) |
| 258 | .is_some() |
| 259 | { |
| 260 | let _unforced = CreatorAvailabilityGuard::unforced(); |
| 261 | codewhale_engine::conversation_modes::clear_probe_cache_for_tests(); |
| 262 | let live_catalog: Value = client |
| 263 | .get(format!("{base}/v1/apps/plugins/catalog")) |
| 264 | .send() |
| 265 | .await? |
| 266 | .error_for_status()? |
| 267 | .json() |
| 268 | .await?; |
| 269 | let live_modes: Value = client |
| 270 | .get(format!("{base}/v1/modes")) |
| 271 | .send() |
| 272 | .await? |
| 273 | .error_for_status()? |
| 274 | .json() |
| 275 | .await?; |
| 276 | let live_row = live_catalog["builtin"] |
| 277 | .as_array() |
| 278 | .and_then(|rows| rows.iter().find(|row| row["id"] == "plugin_creator")) |
| 279 | .expect("plugin_creator row in live builtin catalog"); |
| 280 | assert_eq!(live_row["available"], true); |
| 281 | assert!(live_row["unavailable_reason"].is_null()); |
| 282 | assert_eq!(live_modes["modes"][1]["available"], true); |
| 283 | assert!(live_modes["modes"][1].get("reason").is_none()); |
| 284 | } |
| 285 | |
| 286 | handle.abort(); |
| 287 | let _ = handle.await; |
| 288 | Ok(()) |
| 289 | } |
| 290 | |
| 291 | #[tokio::test] |
| 292 | async fn warmed_thread_sees_plugin_creator_disable_and_rejects_switch_to_creator() -> Result<()> { |
| 293 | let _guard = CreatorAvailabilityGuard::set(None); |
| 294 | codewhale_engine::conversation_modes::clear_probe_cache_for_tests(); |
| 295 | let dir = tempfile::TempDir::new()?; |
| 296 | let config_path = dir.path().join("config.toml"); |
| 297 | std::fs::write( |
| 298 | &config_path, |
| 299 | "provider = \"deepseek\"\n[providers.deepseek]\napi_key = \"fixture-key\"\nbase_url = \"http://127.0.0.1:1/v1\"\n", |
| 300 | )?; |
| 301 | let Some((addr, threads, handle)) = spawn_test_server_with_config_path(config_path).await? |
| 302 | else { |
| 303 | return Ok(()); |
| 304 | }; |
| 305 | let client = codewhale_release::tls::reqwest_client(); |
| 306 | let base = format!("http://{addr}"); |
| 307 | |
| 308 | let std_created: Value = client |
| 309 | .post(format!("{base}/v1/threads")) |
| 310 | .json(&json!({"agent_preset": "standard"})) |
| 311 | .send() |
| 312 | .await? |
| 313 | .error_for_status()? |
| 314 | .json() |
| 315 | .await?; |
| 316 | let std_id = std_created["id"] |
| 317 | .as_str() |
| 318 | .expect("standard thread id") |
| 319 | .to_string(); |
| 320 | |
| 321 | let creator_created: Value = client |
| 322 | .post(format!("{base}/v1/threads")) |
| 323 | .json(&json!({"agent_preset": "creator"})) |
| 324 | .send() |
| 325 | .await? |
| 326 | .error_for_status()? |
| 327 | .json() |
| 328 | .await?; |
| 329 | let creator_id = creator_created["id"] |
| 330 | .as_str() |
| 331 | .expect("creator thread id") |
| 332 | .to_string(); |
| 333 | |
| 334 | client |
| 335 | .post(format!("{base}/v1/threads/{std_id}/resume")) |
| 336 | .send() |
| 337 | .await? |
| 338 | .error_for_status()?; |
| 339 | client |
| 340 | .post(format!("{base}/v1/threads/{creator_id}/resume")) |
| 341 | .send() |
| 342 | .await? |
| 343 | .error_for_status()?; |
| 344 | assert!(threads.loaded_engine(&std_id).await.is_some()); |
| 345 | assert!(threads.loaded_engine(&creator_id).await.is_some()); |
| 346 | |
| 347 | let disabled: Value = client |
| 348 | .post(format!( |
| 349 | "{base}/v1/apps/plugins/catalog/plugin_creator/disable" |
| 350 | )) |
| 351 | .send() |
| 352 | .await? |
| 353 | .error_for_status()? |
| 354 | .json() |
| 355 | .await?; |
| 356 | assert_eq!(disabled["row"]["enabled"], false); |
| 357 | assert!(threads.loaded_engine(&std_id).await.is_none()); |
| 358 | assert!(threads.loaded_engine(&creator_id).await.is_none()); |
| 359 | |
| 360 | let switched = client |
| 361 | .patch(format!("{base}/v1/threads/{std_id}")) |
| 362 | .json(&json!({"agent_preset": "creator"})) |
| 363 | .send() |
| 364 | .await?; |
| 365 | assert_eq!(switched.status(), reqwest::StatusCode::FAILED_DEPENDENCY); |
| 366 | let body: Value = switched.json().await?; |
| 367 | assert_eq!(body["error"]["code"], "mode_unavailable"); |
| 368 | |
| 369 | client |
| 370 | .post(format!("{base}/v1/threads/{std_id}/resume")) |
| 371 | .send() |
| 372 | .await? |
| 373 | .error_for_status()?; |
| 374 | assert!(threads.loaded_engine(&std_id).await.is_some()); |
| 375 | |
| 376 | client |
| 377 | .post(format!( |
| 378 | "{base}/v1/apps/plugins/catalog/plugin_creator/enable" |
| 379 | )) |
| 380 | .send() |
| 381 | .await? |
| 382 | .error_for_status()?; |
| 383 | assert!(threads.loaded_engine(&std_id).await.is_none()); |
| 384 | |
| 385 | let enabled_switch: Value = client |
| 386 | .patch(format!("{base}/v1/threads/{std_id}")) |
| 387 | .json(&json!({"agent_preset": "creator"})) |
| 388 | .send() |
| 389 | .await? |
| 390 | .error_for_status()? |
| 391 | .json() |
| 392 | .await?; |
| 393 | assert_eq!(enabled_switch["agent_preset"], "creator"); |
| 394 | |
| 395 | handle.abort(); |
| 396 | let _ = handle.await; |
| 397 | Ok(()) |
| 398 | } |
| 399 |