| 1 | //! Actual Core ToolSpecs, committed pinned host and local file fixtures. |
| 2 | //! No provider traffic. Host selection is independent of third-party Native policy. |
| 3 | use super::*; |
| 4 | use crate::extension_host::test_support::node_for_tests; |
| 5 | use crate::extension_host::{ExtensionHostManager, ExtensionHostOptions, TestManagerGuard}; |
| 6 | use codewhale_plugins::activation::TestPolicyGuard; |
| 7 | use codewhale_runtime::features::{Feature, Features}; |
| 8 | use std::sync::Arc; |
| 9 | |
| 10 | fn manager(node: std::path::PathBuf, home: &std::path::Path) -> Arc<ExtensionHostManager> { |
| 11 | Arc::new(ExtensionHostManager::new(ExtensionHostOptions { |
| 12 | runtime: codewhale_config::extension_host::ExtensionHostRuntime::Node, |
| 13 | node_override: Some(node), |
| 14 | root: Some(home.join("host")), |
| 15 | ..ExtensionHostOptions::default() |
| 16 | })) |
| 17 | } |
| 18 | fn context(root: &std::path::Path, feature: Option<Feature>) -> ToolContext { |
| 19 | let mut features = Features::with_defaults(); |
| 20 | if let Some(feature) = feature { |
| 21 | features.enable(feature); |
| 22 | } |
| 23 | ToolContext::new(root).with_features(features) |
| 24 | } |
| 25 | fn equal_tool_results(rust: &ToolResult, host: &ToolResult) { |
| 26 | assert_eq!(host.success, rust.success); |
| 27 | assert_eq!(host.content, rust.content, "exact user/model result bytes"); |
| 28 | assert_eq!(host.metadata, rust.metadata); |
| 29 | } |
| 30 | |
| 31 | #[test] |
| 32 | fn data_host_flag_defaults_to_rust_and_retired_finance_host_still_loads() { |
| 33 | let defaults = Features::with_defaults(); |
| 34 | assert!(!defaults.enabled(Feature::DataHost)); |
| 35 | assert!(codewhale_runtime::features::is_known_feature_key( |
| 36 | "finance_host" |
| 37 | )); |
| 38 | assert_eq!( |
| 39 | codewhale_runtime::features::feature_from_key("finance_host"), |
| 40 | None |
| 41 | ); |
| 42 | assert_eq!( |
| 43 | codewhale_runtime::features::feature_from_key("data_host"), |
| 44 | Some(Feature::DataHost) |
| 45 | ); |
| 46 | } |
| 47 | |
| 48 | #[tokio::test(flavor = "current_thread")] |
| 49 | async fn real_host_data_recorded_results_match_rust_without_native_activation() { |
| 50 | let _home = crate::test_support::SealedHome::new(); |
| 51 | let _policy = TestPolicyGuard::extension_host(false); |
| 52 | let Some(node) = node_for_tests("real_host_data_recorded_results") else { |
| 53 | return; |
| 54 | }; |
| 55 | let home = tempfile::tempdir().unwrap(); |
| 56 | let manager = manager(node, home.path()); |
| 57 | let _manager = TestManagerGuard::install(Arc::clone(&manager)); |
| 58 | let rust = context(home.path(), None); |
| 59 | let host = context(home.path(), Some(Feature::DataHost)); |
| 60 | let cases = [ |
| 61 | json!({"content":"{\"😀\":1,\"\":2,\"__proto__\":3,\"a\":4}"}), |
| 62 | json!({"content":"[1,true,null]", "format":"json"}), |
| 63 | json!({"content":"42", "format":"json"}), |
| 64 | json!({"content":"-0.0", "format":"json"}), |
| 65 | json!({"content":"title = \"hello\"\ndate = 2026-10-02\n[a]\nvalue = 2\n"}), |
| 66 | json!({"content":"{bad 😀", "format":"json"}), |
| 67 | json!({"content":"key = [", "format":"toml"}), |
| 68 | json!({"content":"neither valid JSON nor TOML"}), |
| 69 | ]; |
| 70 | for input in cases { |
| 71 | let expected = ValidateDataTool |
| 72 | .execute(input.clone(), &rust) |
| 73 | .await |
| 74 | .unwrap(); |
| 75 | let actual = ValidateDataTool.execute(input, &host).await.unwrap(); |
| 76 | equal_tool_results(&expected, &actual); |
| 77 | } |
| 78 | // File extension must pin a failing JSON parser even if TOML could succeed. |
| 79 | std::fs::write(home.path().join("pinned.json"), "value = 1").unwrap(); |
| 80 | let input = json!({"path":"pinned.json"}); |
| 81 | equal_tool_results( |
| 82 | &ValidateDataTool |
| 83 | .execute(input.clone(), &rust) |
| 84 | .await |
| 85 | .unwrap(), |
| 86 | &ValidateDataTool.execute(input, &host).await.unwrap(), |
| 87 | ); |
| 88 | assert!(!codewhale_plugins::activation::extension_host_policy_enabled()); |
| 89 | manager.shutdown().await; |
| 90 | } |
| 91 | |
| 92 | #[tokio::test(flavor = "current_thread")] |
| 93 | async fn selected_host_missing_runtime_refuses_without_rust_fallback() { |
| 94 | let _home = crate::test_support::SealedHome::new(); |
| 95 | let _policy = TestPolicyGuard::extension_host(false); |
| 96 | let home = tempfile::tempdir().unwrap(); |
| 97 | let manager = manager(home.path().join("absent-node"), home.path()); |
| 98 | let _manager = TestManagerGuard::install(Arc::clone(&manager)); |
| 99 | let data = ValidateDataTool |
| 100 | .execute( |
| 101 | json!({"content":"{}"}), |
| 102 | &context(home.path(), Some(Feature::DataHost)), |
| 103 | ) |
| 104 | .await; |
| 105 | assert!(data.is_err(), "Rust validation would have succeeded"); |
| 106 | manager.shutdown().await; |
| 107 | } |
| 108 | |
| 109 | #[tokio::test(flavor = "current_thread")] |
| 110 | async fn host_input_bound_and_cancel_refuse_before_admitting_transform() { |
| 111 | let _home = crate::test_support::SealedHome::new(); |
| 112 | let _policy = TestPolicyGuard::extension_host(false); |
| 113 | let home = tempfile::tempdir().unwrap(); |
| 114 | let host = context(home.path(), Some(Feature::DataHost)); |
| 115 | assert!(matches!( |
| 116 | ValidateDataTool |
| 117 | .execute(json!({"content":"x".repeat(1024*1024+1)}), &host) |
| 118 | .await, |
| 119 | Err(ToolError::InvalidInput { .. }) |
| 120 | )); |
| 121 | let manager = manager(home.path().join("absent-node"), home.path()); |
| 122 | let _manager = TestManagerGuard::install(Arc::clone(&manager)); |
| 123 | let cancel = tokio_util::sync::CancellationToken::new(); |
| 124 | cancel.cancel(); |
| 125 | let host = host.with_cancel_token(cancel); |
| 126 | let error = ValidateDataTool |
| 127 | .execute(json!({"content":"{}"}), &host) |
| 128 | .await |
| 129 | .unwrap_err(); |
| 130 | assert!(error.to_string().contains("cancelled")); |
| 131 | manager.shutdown().await; |
| 132 | } |
| 133 |