| 1 | //! Conformance corpus for native DSH bundle import. The cases mirror the |
| 2 | //! bundle-mode contracts of `scripts/dsh-plugin-acceptance/test_convert_plugin.py` (converter |
| 3 | //! 0.10.1); the pinned upstream package is shared with that suite. |
| 4 | |
| 5 | use codewhale_plugins::install::dsh::presets; |
| 6 | use codewhale_plugins::install::dsh::*; |
| 7 | use codewhale_skill_install::install::sha256_hex; |
| 8 | use serde_json::Value as Json; |
| 9 | use serde_json::json; |
| 10 | use std::fs; |
| 11 | use std::path::Path; |
| 12 | use std::path::PathBuf; |
| 13 | |
| 14 | const MCP: &str = "@deepseek-ai/dsh-mcp-client"; |
| 15 | const SKILLS: &str = "@deepseek-ai/dsh-skill-filesystem"; |
| 16 | |
| 17 | struct Fixture { |
| 18 | _host: codewhale_engine::extension_host::TestManagerGuard, |
| 19 | _root: tempfile::TempDir, |
| 20 | bundle: PathBuf, |
| 21 | output: PathBuf, |
| 22 | } |
| 23 | |
| 24 | fn isolated_host(root: &Path) -> codewhale_engine::extension_host::TestManagerGuard { |
| 25 | codewhale_engine::extension_host::TestManagerGuard::install(std::sync::Arc::new( |
| 26 | codewhale_engine::extension_host::ExtensionHostManager::new( |
| 27 | codewhale_engine::extension_host::ExtensionHostOptions { |
| 28 | root: Some(root.join("host")), |
| 29 | ..Default::default() |
| 30 | }, |
| 31 | ), |
| 32 | )) |
| 33 | } |
| 34 | |
| 35 | fn fixture(patch: Option<&str>, manifest: Option<Json>) -> Fixture { |
| 36 | let root = tempfile::tempdir().unwrap(); |
| 37 | let bundle = root.path().join("dsh-bundle"); |
| 38 | fs::create_dir(&bundle).unwrap(); |
| 39 | let mut package = json!({ |
| 40 | "name": "@demo/tools-dsh", |
| 41 | "version": "1.2.3", |
| 42 | "dsh": {"bundle": {"patch": "./cordis.patch.yml"}}, |
| 43 | }); |
| 44 | if let Some(Json::Object(extra)) = manifest { |
| 45 | for (key, value) in extra { |
| 46 | package[key] = value; |
| 47 | } |
| 48 | } |
| 49 | fs::write(bundle.join("package.json"), package.to_string()).unwrap(); |
| 50 | if let Some(patch) = patch { |
| 51 | fs::write(bundle.join("cordis.patch.yml"), patch).unwrap(); |
| 52 | } |
| 53 | let output = root.path().join("out"); |
| 54 | Fixture { |
| 55 | _host: isolated_host(root.path()), |
| 56 | _root: root, |
| 57 | bundle, |
| 58 | output, |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | fn docs_row(extra: &str) -> String { |
| 63 | format!( |
| 64 | " - id: docs-entry\n name: '{MCP}'\n{extra} config:\n serverName: docs\n transport: streamable-http\n url: https://docs.example.invalid/mcp\n" |
| 65 | ) |
| 66 | } |
| 67 | |
| 68 | fn servers(output: &Path) -> Json { |
| 69 | let text = fs::read_to_string(output.join("mcp.json")).unwrap(); |
| 70 | serde_json::from_str::<Json>(&text).unwrap()["mcpServers"].clone() |
| 71 | } |
| 72 | |
| 73 | fn receipt(output: &Path) -> String { |
| 74 | fs::read_to_string(output.join("CONVERSION.md")).unwrap() |
| 75 | } |
| 76 | |
| 77 | fn refused(fixture: &Fixture, needle: &str) { |
| 78 | let error = convert_package(&fixture.bundle, &fixture.output) |
| 79 | .unwrap_err() |
| 80 | .to_string(); |
| 81 | assert!(error.contains(needle), "expected `{needle}` in: {error}"); |
| 82 | assert!( |
| 83 | !fixture.output.exists(), |
| 84 | "a refusal leaves no partial output" |
| 85 | ); |
| 86 | } |
| 87 | |
| 88 | #[test] |
| 89 | fn evaluates_patches_and_skips_foreign_rows() { |
| 90 | let f = fixture( |
| 91 | Some(&format!( |
| 92 | "- insert:\n - id: docs-entry\n name: '{MCP}'\n config:\n serverName: docs\n transport: streamable-http\n url: https://docs.example.invalid/mcp\n toolCallTimeoutMs: 19000\n - id: skin\n name: '@deepseek-ai/dsh-client-ui-theme'\n config: {{hue: 4}}\n- id: docs-entry\n disabled: true\n- id: ghost\n disabled: true\n" |
| 93 | )), |
| 94 | None, |
| 95 | ); |
| 96 | let conversion = convert_package(&f.bundle, &f.output).unwrap(); |
| 97 | assert_eq!(conversion.plugin_name, "tools-dsh"); |
| 98 | assert_eq!( |
| 99 | servers(&f.output)["docs"], |
| 100 | json!({"type": "streamable-http", "url": "https://docs.example.invalid/mcp", |
| 101 | "extensions": {"net.codewhale": {"execute_timeout": 19, "disabled": true}}}) |
| 102 | ); |
| 103 | let text = receipt(&f.output); |
| 104 | for fact in ["@demo/tools-dsh@1.2.3", "skin", "ghost"] { |
| 105 | assert!(text.contains(fact), "{fact}: {text}"); |
| 106 | } |
| 107 | let manifest: Json = |
| 108 | serde_json::from_str(&fs::read_to_string(f.output.join("plugin.json")).unwrap()).unwrap(); |
| 109 | assert_eq!(manifest["name"], "tools-dsh"); |
| 110 | assert_eq!(manifest["version"], "1.2.3"); |
| 111 | assert_eq!( |
| 112 | manifest["extensions"]["net.codewhale"]["capabilities"]["network_hosts"], |
| 113 | json!(["docs.example.invalid"]) |
| 114 | ); |
| 115 | } |
| 116 | |
| 117 | #[test] |
| 118 | fn environment_expressions_are_never_evaluated_and_host_paths_never_copied() { |
| 119 | let f = fixture( |
| 120 | Some(&format!( |
| 121 | "- insert:\n - id: local-entry\n name: '{MCP}'\n config:\n serverName: localdocs\n transport: stdio\n command: !!js process.execPath\n args:\n - !!js process.env.DSH_UNSET_7391 || '/tmp/server.mjs'\n - id: env-url\n name: '{MCP}'\n config:\n serverName: envdocs\n transport: streamable-http\n url: !!js '`https://example.invalid/${{process.env.DSH_CANARY}}/mcp`'\n{}", |
| 122 | docs_row("") |
| 123 | )), |
| 124 | None, |
| 125 | ); |
| 126 | convert_package(&f.bundle, &f.output).unwrap(); |
| 127 | let names: Vec<_> = servers(&f.output) |
| 128 | .as_object() |
| 129 | .unwrap() |
| 130 | .keys() |
| 131 | .cloned() |
| 132 | .collect(); |
| 133 | assert_eq!(names, ["docs"]); |
| 134 | assert!(!f.output.join("mcp").exists()); |
| 135 | let text = receipt(&f.output); |
| 136 | assert!( |
| 137 | text.contains("local-entry") && text.contains("reads an environment value"), |
| 138 | "{text}" |
| 139 | ); |
| 140 | assert!(text.contains("No environment variable values were resolved")); |
| 141 | } |
| 142 | |
| 143 | #[test] |
| 144 | fn a_relative_entry_missing_from_the_package_is_skipped_not_searched_for() { |
| 145 | let f = fixture( |
| 146 | Some(&format!( |
| 147 | "- insert:\n - id: local-entry\n name: '{MCP}'\n config:\n serverName: localdocs\n transport: stdio\n command: !!js process.execPath\n args: ['./server.mjs']\n" |
| 148 | )), |
| 149 | None, |
| 150 | ); |
| 151 | refused(&f, "No portable components"); |
| 152 | } |
| 153 | |
| 154 | #[test] |
| 155 | fn a_package_with_nothing_portable_is_refused_naming_its_skipped_rows() { |
| 156 | let extra: String = (1..=6) |
| 157 | .map(|n| format!(" - {{id: extra-{n}, name: '@demo/extra-{n}'}}\n")) |
| 158 | .collect(); |
| 159 | let f = fixture( |
| 160 | Some(&format!( |
| 161 | "- insert:\n - {{id: skin, name: '@deepseek-ai/dsh-client-ui-theme'}}\n{extra}" |
| 162 | )), |
| 163 | None, |
| 164 | ); |
| 165 | refused(&f, "No portable components in this package"); |
| 166 | let error = convert_package(&f.bundle, &f.output) |
| 167 | .unwrap_err() |
| 168 | .to_string(); |
| 169 | for fact in [ |
| 170 | "skin (@deepseek-ai/dsh-client-ui-theme): ", |
| 171 | "has no portable representation", |
| 172 | "extra-4 (@demo/extra-4)", |
| 173 | "; and 2 more", |
| 174 | ] { |
| 175 | assert!(error.contains(fact), "{fact}: {error}"); |
| 176 | } |
| 177 | assert!(!error.contains("extra-5"), "{error}"); |
| 178 | assert!(!error.contains('\n'), "{error}"); |
| 179 | } |
| 180 | |
| 181 | #[test] |
| 182 | fn refusal_text_is_bounded_and_shows_control_and_bidi_characters_as_escapes() { |
| 183 | assert_eq!( |
| 184 | refusal_text("a\u{202e}b\nc\u{1b}[31m it's"), |
| 185 | "a\\u{202e}b\\nc\\u{1b}[31m it's" |
| 186 | ); |
| 187 | let long = refusal_text(&"x".repeat(MAX_REFUSAL_TEXT + 40)); |
| 188 | assert_eq!(long.chars().count(), MAX_REFUSAL_TEXT + 1); |
| 189 | assert!(long.ends_with('…')); |
| 190 | assert_eq!( |
| 191 | refusal_list((1..=7).map(|n| n.to_string())), |
| 192 | "1; 2; 3; 4; 5; and 2 more" |
| 193 | ); |
| 194 | } |
| 195 | |
| 196 | #[test] |
| 197 | fn disabled_ancestry_disables_children_and_is_receipted() { |
| 198 | let f = fixture( |
| 199 | Some(&format!( |
| 200 | "- insert:\n - id: outer\n group: true\n disabled: true\n config:\n - id: inner\n group: true\n config:\n - id: docs-entry\n name: '{MCP}'\n config: {{serverName: docs, transport: streamable-http, url: 'https://docs.example.invalid/mcp'}}\n - id: skills-row\n name: '{SKILLS}'\n config: {{customSkillDirs: [pack-skills]}}\n - id: live-entry\n name: '{MCP}'\n config: {{serverName: live, transport: streamable-http, url: 'https://live.example.invalid/mcp'}}\n" |
| 201 | )), |
| 202 | None, |
| 203 | ); |
| 204 | let skill = f.bundle.join("pack-skills/guide"); |
| 205 | fs::create_dir_all(&skill).unwrap(); |
| 206 | fs::write( |
| 207 | skill.join("SKILL.md"), |
| 208 | "---\nname: guide\ndescription: Bundled skill\n---\nBody.\n", |
| 209 | ) |
| 210 | .unwrap(); |
| 211 | convert_package(&f.bundle, &f.output).unwrap(); |
| 212 | let servers = servers(&f.output); |
| 213 | assert_eq!( |
| 214 | servers["docs"]["extensions"]["net.codewhale"], |
| 215 | json!({"disabled": true}) |
| 216 | ); |
| 217 | assert_eq!(servers["live"]["extensions"]["net.codewhale"], json!({})); |
| 218 | assert!(!f.output.join("skills").exists()); |
| 219 | let text = receipt(&f.output); |
| 220 | assert!(text.contains("disabled by ancestor `outer`"), "{text}"); |
| 221 | assert!(text.contains("skipped-disabled"), "{text}"); |
| 222 | } |
| 223 | |
| 224 | #[test] |
| 225 | fn unresolved_conditional_gates_refuse_but_foreign_gates_are_skipped() { |
| 226 | let gated_group = format!( |
| 227 | "- insert:\n - id: gate\n name: '@deepseek-ai/cordis-plugin-group'\n group: true\n disabled: !!js process.env.DISABLE_THIS\n config:\n - id: docs-entry\n name: '{MCP}'\n config: {{serverName: docs, transport: streamable-http, url: 'https://docs.example.invalid/mcp'}}\n" |
| 228 | ); |
| 229 | let gated_row = format!( |
| 230 | "- insert:\n - id: docs-entry\n name: '{MCP}'\n disabled: !!js process.env.DISABLE_THIS\n config: {{serverName: docs, transport: streamable-http, url: 'https://docs.example.invalid/mcp'}}\n" |
| 231 | ); |
| 232 | let string_gate = format!( |
| 233 | "- insert:\n - id: docs-entry\n name: '{MCP}'\n disabled: 'yes'\n config: {{serverName: docs, transport: streamable-http, url: 'https://docs.example.invalid/mcp'}}\n" |
| 234 | ); |
| 235 | for patch in [gated_group, gated_row, string_gate] { |
| 236 | refused(&fixture(Some(&patch), None), "conditional or non-boolean"); |
| 237 | } |
| 238 | let control = fixture( |
| 239 | Some(&format!( |
| 240 | "- insert:\n - id: tool-bash\n name: '@deepseek-ai/dsh-tool-bash'\n disabled: !!js process.platform === 'win32'\n{}", |
| 241 | docs_row("") |
| 242 | )), |
| 243 | None, |
| 244 | ); |
| 245 | convert_package(&control.bundle, &control.output).unwrap(); |
| 246 | let text = receipt(&control.output); |
| 247 | assert!( |
| 248 | text.contains("tool-bash") |
| 249 | && text.contains("conditional `disabled` gate was not evaluated"), |
| 250 | "{text}" |
| 251 | ); |
| 252 | } |
| 253 | |
| 254 | #[test] |
| 255 | fn ordered_layers_replace_config_and_record_provenance() { |
| 256 | let f = fixture( |
| 257 | Some(&format!( |
| 258 | "- insert:\n - id: docs-entry\n name: '{MCP}'\n config: {{serverName: docs, transport: streamable-http, url: 'https://first.example.invalid/mcp', toolCallTimeoutMs: 19000}}\n" |
| 259 | )), |
| 260 | Some( |
| 261 | json!({"dsh": {"bundle": {"patch": ["./cordis.patch.yml", "./presets/overlay.patch.yml"]}}}), |
| 262 | ), |
| 263 | ); |
| 264 | fs::create_dir(f.bundle.join("presets")).unwrap(); |
| 265 | fs::write( |
| 266 | f.bundle.join("presets/overlay.patch.yml"), |
| 267 | "- id: docs-entry\n disabled: true\n config: {serverName: docs, transport: streamable-http, url: 'https://second.example.invalid/mcp'}\n", |
| 268 | ) |
| 269 | .unwrap(); |
| 270 | convert_package(&f.bundle, &f.output).unwrap(); |
| 271 | assert_eq!( |
| 272 | servers(&f.output)["docs"], |
| 273 | json!({"type": "streamable-http", "url": "https://second.example.invalid/mcp", |
| 274 | "extensions": {"net.codewhale": {"disabled": true}}}) |
| 275 | ); |
| 276 | let text = receipt(&f.output); |
| 277 | assert!(text.contains("applied in declaration order")); |
| 278 | assert!(text.contains(&format!("Converter version {CONVERTER_VERSION}"))); |
| 279 | assert!(text.contains("Source package: @demo/tools-dsh@1.2.3")); |
| 280 | assert!(text.contains(&sha256_hex( |
| 281 | &fs::read(f.bundle.join("package.json")).unwrap() |
| 282 | ))); |
| 283 | for relative in ["./cordis.patch.yml", "./presets/overlay.patch.yml"] { |
| 284 | let content = fs::read(f.bundle.join(relative.trim_start_matches("./"))).unwrap(); |
| 285 | assert!( |
| 286 | text.contains(&format!( |
| 287 | "{relative} (sha256 {}, {} bytes)", |
| 288 | sha256_hex(&content), |
| 289 | content.len() |
| 290 | )), |
| 291 | "{relative}: {text}" |
| 292 | ); |
| 293 | } |
| 294 | let structured: Json = |
| 295 | serde_json::from_str(&fs::read_to_string(f.output.join("CONVERSION.json")).unwrap()) |
| 296 | .unwrap(); |
| 297 | assert_eq!(structured["schema"], "codewhale.plugin-conversion.v1"); |
| 298 | } |
| 299 | |
| 300 | #[test] |
| 301 | fn skipped_patch_operations_are_structured_manual_ports() { |
| 302 | let f = fixture( |
| 303 | Some(&format!("- insert:\n{}", docs_row(""))), |
| 304 | Some(json!({"dsh": {"bundle": {"patch": ["./cordis.patch.yml", "./overlay.yml"]}}})), |
| 305 | ); |
| 306 | fs::write( |
| 307 | f.bundle.join("overlay.yml"), |
| 308 | "- {id: missing-group, insert: []}\n- {disabled: true}\n- {id: missing-row, disabled: true}\n- {id: docs-entry, name: wrong-package, disabled: true}\n", |
| 309 | ) |
| 310 | .unwrap(); |
| 311 | convert_package(&f.bundle, &f.output).unwrap(); |
| 312 | let structured: Json = |
| 313 | serde_json::from_str(&fs::read_to_string(f.output.join("CONVERSION.json")).unwrap()) |
| 314 | .unwrap(); |
| 315 | let skipped = structured["required_manual_ports"].as_array().unwrap(); |
| 316 | assert_eq!( |
| 317 | skipped |
| 318 | .iter() |
| 319 | .map(|row| row["row"].clone()) |
| 320 | .collect::<Vec<_>>(), |
| 321 | [ |
| 322 | json!("missing-group"), |
| 323 | Json::Null, |
| 324 | json!("missing-row"), |
| 325 | json!("docs-entry") |
| 326 | ] |
| 327 | ); |
| 328 | assert_eq!( |
| 329 | skipped |
| 330 | .iter() |
| 331 | .map(|row| row["patch"].clone()) |
| 332 | .collect::<Vec<_>>(), |
| 333 | [json!(1), json!(2), json!(3), json!(4)] |
| 334 | ); |
| 335 | for row in skipped { |
| 336 | assert_eq!( |
| 337 | (row["kind"].as_str(), row["layer"].as_str()), |
| 338 | (Some("patch"), Some("./overlay.yml")) |
| 339 | ); |
| 340 | assert!(receipt(&f.output).contains(row["reason"].as_str().unwrap())); |
| 341 | } |
| 342 | assert!( |
| 343 | servers(&f.output)["docs"]["extensions"]["net.codewhale"] |
| 344 | .get("disabled") |
| 345 | .is_none() |
| 346 | ); |
| 347 | } |
| 348 | |
| 349 | #[test] |
| 350 | fn patch_layer_list_is_bounded_and_contained() { |
| 351 | let portable = format!("- insert:\n{}", docs_row("")); |
| 352 | let attempt = |declared: Json, files: &[(&str, String)]| { |
| 353 | let f = fixture(None, Some(json!({"dsh": {"bundle": {"patch": declared}}}))); |
| 354 | for (name, text) in files { |
| 355 | let destination = f.bundle.join(name); |
| 356 | fs::create_dir_all(destination.parent().unwrap()).unwrap(); |
| 357 | fs::write(destination, text).unwrap(); |
| 358 | } |
| 359 | f |
| 360 | }; |
| 361 | let ok = attempt(json!(["./a.yml"]), &[("a.yml", portable.clone())]); |
| 362 | convert_package(&ok.bundle, &ok.output).unwrap(); |
| 363 | refused( |
| 364 | &attempt( |
| 365 | json!(["./a.yml", "./a.yml"]), |
| 366 | &[("a.yml", portable.clone())], |
| 367 | ), |
| 368 | "listed once", |
| 369 | ); |
| 370 | refused( |
| 371 | &attempt(json!(["a.yml", "./a.yml"]), &[("a.yml", portable.clone())]), |
| 372 | "listed once", |
| 373 | ); |
| 374 | refused(&attempt(json!(["/tmp/a.yml"]), &[]), "relative path"); |
| 375 | refused(&attempt(json!(["C:\\a.yml"]), &[]), "relative path"); |
| 376 | refused( |
| 377 | &attempt( |
| 378 | json!(["./a.yml", "../outside.yml"]), |
| 379 | &[("a.yml", portable.clone())], |
| 380 | ), |
| 381 | "relative path", |
| 382 | ); |
| 383 | refused(&attempt(json!(["./missing.yml"]), &[]), "inside the bundle"); |
| 384 | refused(&attempt(json!([]), &[]), "non-empty ordered list"); |
| 385 | let many: Vec<String> = (0..65).map(|i| format!("./layer-{i}.yml")).collect(); |
| 386 | refused(&attempt(json!(many), &[]), "At most 64"); |
| 387 | refused( |
| 388 | &attempt( |
| 389 | json!(["./a.yml", "./b.yml"]), |
| 390 | &[ |
| 391 | ("a.yml", format!("[]\n{}", " ".repeat(1024 * 1024 - 3))), |
| 392 | ("b.yml", "[]\n".into()), |
| 393 | ], |
| 394 | ), |
| 395 | "aggregate patch limit", |
| 396 | ); |
| 397 | } |
| 398 | |
| 399 | #[test] |
| 400 | fn pinned_upstream_multifile_package_parses_without_promoting_rows() { |
| 401 | codewhale_engine::extension_host::install_ports(); |
| 402 | let bundle = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../scripts/fixtures/dsh-web-app"); |
| 403 | let upstream: Json = |
| 404 | serde_json::from_str(&fs::read_to_string(bundle.join("UPSTREAM.json")).unwrap()).unwrap(); |
| 405 | assert_eq!( |
| 406 | upstream["commit"], |
| 407 | "00102833dfaee1da9f48a3a8eae9d34005a75218" |
| 408 | ); |
| 409 | for (relative, digest) in upstream["files"].as_object().unwrap() { |
| 410 | assert_eq!( |
| 411 | &json!(sha256_hex(&fs::read(bundle.join(relative)).unwrap())), |
| 412 | digest, |
| 413 | "{relative}" |
| 414 | ); |
| 415 | } |
| 416 | let package = Package::open(&bundle).unwrap(); |
| 417 | let loaded = load_bundle(&package).unwrap(); |
| 418 | assert_eq!(loaded.layers.len(), 5); |
| 419 | assert!(!loaded.entries.is_empty()); |
| 420 | let mut components = Components::default(); |
| 421 | components.walk(&package, &loaded.entries, None).unwrap(); |
| 422 | assert!( |
| 423 | components.servers.is_empty() |
| 424 | && components.hosts.is_empty() |
| 425 | && components.skill_dirs.is_empty() |
| 426 | ); |
| 427 | assert!( |
| 428 | components |
| 429 | .outcomes |
| 430 | .iter() |
| 431 | .all(|o| !o.outcome.starts_with("converted")) |
| 432 | ); |
| 433 | assert!( |
| 434 | components |
| 435 | .notes |
| 436 | .iter() |
| 437 | .any(|note| note.contains("conditional")) |
| 438 | ); |
| 439 | } |
| 440 | |
| 441 | #[test] |
| 442 | fn group_children_and_relative_stdio_entries_convert_from_the_declared_cwd() { |
| 443 | let f = fixture( |
| 444 | Some(&format!( |
| 445 | "- insert:\n - {{id: grouped, group: true, config: []}}\n- id: grouped\n insert:\n - id: in-group\n name: '{MCP}'\n config: {{serverName: inner, transport: stdio, command: node, args: [server.mjs], cwd: packaged}}\n" |
| 446 | )), |
| 447 | None, |
| 448 | ); |
| 449 | fs::write(f.bundle.join("server.mjs"), "wrong source").unwrap(); |
| 450 | fs::create_dir(f.bundle.join("packaged")).unwrap(); |
| 451 | fs::write(f.bundle.join("packaged/server.mjs"), "correct source").unwrap(); |
| 452 | let conversion = convert_package(&f.bundle, &f.output).unwrap(); |
| 453 | assert!(conversion.requires_node); |
| 454 | assert_eq!(conversion.local_servers, ["inner"]); |
| 455 | assert_eq!(servers(&f.output)["inner"]["cwd"], "mcp/inner"); |
| 456 | assert_eq!( |
| 457 | fs::read_to_string(f.output.join("mcp/inner/server.mjs")).unwrap(), |
| 458 | "correct source" |
| 459 | ); |
| 460 | } |
| 461 | |
| 462 | #[test] |
| 463 | fn custom_skill_dirs_import_and_disabled_skills_are_omitted() { |
| 464 | let build = |disabled: &str| { |
| 465 | let f = fixture( |
| 466 | Some(&format!( |
| 467 | "- insert:\n - id: skills-row\n name: '{SKILLS}'\n{disabled} config: {{customSkillDirs: [pack-skills]}}\n{}", |
| 468 | docs_row("") |
| 469 | )), |
| 470 | None, |
| 471 | ); |
| 472 | let skill = f.bundle.join("pack-skills/guide"); |
| 473 | fs::create_dir_all(&skill).unwrap(); |
| 474 | fs::write( |
| 475 | skill.join("SKILL.md"), |
| 476 | "---\nname: guide\ndescription: Bundled skill\n---\nBody.\n", |
| 477 | ) |
| 478 | .unwrap(); |
| 479 | fs::write(skill.join("notes.txt"), "companion").unwrap(); |
| 480 | f |
| 481 | }; |
| 482 | let enabled = build(""); |
| 483 | let conversion = convert_package(&enabled.bundle, &enabled.output).unwrap(); |
| 484 | assert_eq!(conversion.skills, ["guide"]); |
| 485 | let skill = fs::read_to_string(enabled.output.join("skills/guide/SKILL.md")).unwrap(); |
| 486 | assert!( |
| 487 | skill.starts_with("---\nname: guide\ndescription: |-\n Bundled skill\n---"), |
| 488 | "{skill}" |
| 489 | ); |
| 490 | assert_eq!( |
| 491 | fs::read_to_string(enabled.output.join("skills/guide/notes.txt")).unwrap(), |
| 492 | "companion" |
| 493 | ); |
| 494 | let disabled = build(" disabled: true\n"); |
| 495 | assert!( |
| 496 | convert_package(&disabled.bundle, &disabled.output) |
| 497 | .unwrap() |
| 498 | .skills |
| 499 | .is_empty() |
| 500 | ); |
| 501 | let text = receipt(&disabled.output); |
| 502 | assert!( |
| 503 | text.contains("skipped-disabled") && text.contains("preserved by omission"), |
| 504 | "{text}" |
| 505 | ); |
| 506 | } |
| 507 | |
| 508 | #[test] |
| 509 | fn js_is_never_executed_and_unlowerable_rows_are_recorded() { |
| 510 | let sentinel = tempfile::tempdir().unwrap().path().join("expression-ran"); |
| 511 | let f = fixture( |
| 512 | Some(&format!( |
| 513 | "- insert:\n - id: bad\n name: '{MCP}'\n config:\n serverName: bad\n transport: streamable-http\n url: !!js require('node:fs').writeFileSync('{}', 'ran')\n - id: ok\n name: '{MCP}'\n config: {{serverName: ok, transport: streamable-http, url: 'https://ok.example.invalid/mcp'}}\n", |
| 514 | sentinel.display() |
| 515 | )), |
| 516 | None, |
| 517 | ); |
| 518 | convert_package(&f.bundle, &f.output).unwrap(); |
| 519 | assert!(!sentinel.exists()); |
| 520 | let names: Vec<_> = servers(&f.output) |
| 521 | .as_object() |
| 522 | .unwrap() |
| 523 | .keys() |
| 524 | .cloned() |
| 525 | .collect(); |
| 526 | assert_eq!(names, ["ok"]); |
| 527 | assert!(receipt(&f.output).contains("bad")); |
| 528 | } |
| 529 | |
| 530 | #[test] |
| 531 | fn manifests_must_declare_contained_patches() { |
| 532 | refused(&fixture(None, None), "inside the bundle"); |
| 533 | refused( |
| 534 | &fixture( |
| 535 | None, |
| 536 | Some(json!({"dsh": {"bundle": {"patch": "../outside.yml"}}})), |
| 537 | ), |
| 538 | "relative path", |
| 539 | ); |
| 540 | let plain = fixture(None, None); |
| 541 | fs::write( |
| 542 | plain.bundle.join("package.json"), |
| 543 | json!({"name": "plain"}).to_string(), |
| 544 | ) |
| 545 | .unwrap(); |
| 546 | refused(&plain, "dsh.bundle.patch"); |
| 547 | assert!(!is_dsh_package(&plain.bundle)); |
| 548 | assert!(is_dsh_package(&fixture(None, None).bundle)); |
| 549 | } |
| 550 | |
| 551 | #[test] |
| 552 | fn policy_and_dependency_fields_never_widen_activation() { |
| 553 | let _home = codewhale_runtime::test_support::SealedHome::new(); |
| 554 | for field in [ |
| 555 | "inject: [approvals]", |
| 556 | "intercept: {tools: true}", |
| 557 | "isolate: {tools: private}", |
| 558 | "unknownGate: true", |
| 559 | ] { |
| 560 | for disabled in ["false", "true"] { |
| 561 | let rows = [ |
| 562 | format!( |
| 563 | " - {{id: docs-entry, name: '{MCP}', {field}, disabled: {disabled}, config: {{serverName: docs, transport: streamable-http, url: 'https://docs.example.invalid/mcp'}}}}\n" |
| 564 | ), |
| 565 | format!( |
| 566 | " - {{id: skills, name: '{SKILLS}', {field}, disabled: {disabled}, config: {{customSkillDirs: [skills]}}}}\n" |
| 567 | ), |
| 568 | format!( |
| 569 | " - {{id: group, group: true, {field}, disabled: {disabled}, config: []}}\n" |
| 570 | ), |
| 571 | ]; |
| 572 | for row in rows { |
| 573 | refused( |
| 574 | &fixture(Some(&format!("- insert:\n{row}")), None), |
| 575 | "unsupported entry policy or dependency", |
| 576 | ); |
| 577 | } |
| 578 | } |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | #[test] |
| 583 | fn skipped_stdio_rows_never_copy_their_source() { |
| 584 | let f = fixture( |
| 585 | Some(&format!( |
| 586 | "- insert:\n{} - id: unportable\n name: '{MCP}'\n config: {{serverName: local, transport: stdio, command: node, args: [server.mjs], cwd: packaged, env: {{TOKEN: canary-9f2}}}}\n", |
| 587 | docs_row("") |
| 588 | )), |
| 589 | None, |
| 590 | ); |
| 591 | fs::create_dir(f.bundle.join("packaged")).unwrap(); |
| 592 | fs::write( |
| 593 | f.bundle.join("packaged/server.mjs"), |
| 594 | "throw new Error('never run');\n", |
| 595 | ) |
| 596 | .unwrap(); |
| 597 | convert_package(&f.bundle, &f.output).unwrap(); |
| 598 | assert!(!f.output.join("mcp").exists()); |
| 599 | let structured = fs::read_to_string(f.output.join("CONVERSION.json")).unwrap(); |
| 600 | let parsed: Json = serde_json::from_str(&structured).unwrap(); |
| 601 | assert_eq!(parsed["required_manual_ports"][0]["row"], "unportable"); |
| 602 | assert!(!structured.contains("canary-9f2")); |
| 603 | } |
| 604 | |
| 605 | #[test] |
| 606 | fn a_duplicate_server_cannot_discard_a_disabled_row() { |
| 607 | let f = fixture( |
| 608 | Some(&format!( |
| 609 | "- insert:\n{}{}", |
| 610 | docs_row(""), |
| 611 | docs_row(" disabled: true\n").replace("id: docs-entry", "id: disabled-copy") |
| 612 | )), |
| 613 | None, |
| 614 | ); |
| 615 | refused(&f, "Duplicate MCP server"); |
| 616 | } |
| 617 | |
| 618 | #[test] |
| 619 | fn literal_lowering_never_evaluates_or_changes_escaped_strings() { |
| 620 | for expression in [ |
| 621 | "'https://example.invalid/mcp'", |
| 622 | "`https://example.invalid/mcp`", |
| 623 | ] { |
| 624 | assert_eq!( |
| 625 | lower_js(expression, "url").unwrap(), |
| 626 | "https://example.invalid/mcp" |
| 627 | ); |
| 628 | } |
| 629 | assert_eq!(lower_js("process.execPath", "command").unwrap(), "node"); |
| 630 | for expression in [ |
| 631 | r"'https://example.invalid/\\x41'", |
| 632 | "`x` + `y`", |
| 633 | "process.env.TOKEN", |
| 634 | "process.env.TOKEN || 'literal'", |
| 635 | "`${process.env.TOKEN}`", |
| 636 | "process.env['TOKEN']", |
| 637 | ] { |
| 638 | assert!(lower_js(expression, "url").is_err(), "{expression}"); |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | #[test] |
| 643 | fn data_parsing_is_closed() { |
| 644 | assert!( |
| 645 | parse_json(r#"{"a": 1, "a": 2}"#).is_err(), |
| 646 | "duplicate JSON keys" |
| 647 | ); |
| 648 | assert!( |
| 649 | parse_yaml("a: 1\na: 2\n", false).is_err(), |
| 650 | "duplicate YAML keys" |
| 651 | ); |
| 652 | assert!(parse_yaml("a: &x 1\nb: *x\n", false).is_err(), "aliases"); |
| 653 | assert!(parse_yaml("a: !!str 1\n", false).is_err(), "explicit tags"); |
| 654 | assert!( |
| 655 | parse_yaml("a: !!js process.execPath\n", false).is_err(), |
| 656 | "js without permission" |
| 657 | ); |
| 658 | assert_eq!( |
| 659 | parse_yaml("a: !!js process.execPath\n", true) |
| 660 | .unwrap() |
| 661 | .get("a"), |
| 662 | Some(&Value::Js("process.execPath".into())) |
| 663 | ); |
| 664 | assert!(parse_yaml("- !!js x\n", true).unwrap() == Value::Seq(vec![Value::Js("x".into())])); |
| 665 | assert!( |
| 666 | parse_yaml("!!js [a]\n", true).is_err(), |
| 667 | "tagged collections" |
| 668 | ); |
| 669 | assert!( |
| 670 | parse_yaml("a: 1\n---\nb: 2\n", false).is_err(), |
| 671 | "multiple documents" |
| 672 | ); |
| 673 | assert!(parse_yaml("{__jsExpr: x}\n", false).is_err()); |
| 674 | assert!( |
| 675 | parse_yaml(&format!("{}1{}", "[".repeat(33), "]".repeat(33)), false).is_err(), |
| 676 | "depth" |
| 677 | ); |
| 678 | assert_eq!( |
| 679 | parse_yaml("{a: yes, b: true, c: 0x10, d: 1.5, e: ~, f: '1'}\n", false).unwrap(), |
| 680 | Value::Map(vec![ |
| 681 | ("a".into(), Value::Str("yes".into())), |
| 682 | ("b".into(), Value::Bool(true)), |
| 683 | ("c".into(), Value::Int(16)), |
| 684 | ("d".into(), Value::Float(1.5)), |
| 685 | ("e".into(), Value::Null), |
| 686 | ("f".into(), Value::Str("1".into())), |
| 687 | ]) |
| 688 | ); |
| 689 | } |
| 690 | |
| 691 | #[test] |
| 692 | fn endpoints_are_literal_https_or_loopback_with_canonical_hosts() { |
| 693 | for (url, host) in [ |
| 694 | ("https://docs.example.invalid/mcp", "docs.example.invalid"), |
| 695 | ( |
| 696 | "https://Docs.Example.Invalid:8443/mcp", |
| 697 | "docs.example.invalid", |
| 698 | ), |
| 699 | ("http://127.0.0.1:9000/mcp", "127.0.0.1"), |
| 700 | ("http://[::1]:9000/mcp", "[::1]"), |
| 701 | ("https://10.0.0.1/mcp", "10.0.0.1"), |
| 702 | ] { |
| 703 | assert_eq!(endpoint_host(url).unwrap(), host, "{url}"); |
| 704 | } |
| 705 | for url in [ |
| 706 | "http://docs.example.invalid/mcp", |
| 707 | "https://user:secret@docs.example.invalid/mcp", |
| 708 | "https://docs.example.invalid/mcp?token=x", |
| 709 | "https://docs.example.invalid/mcp#x", |
| 710 | "https://127.1/mcp", |
| 711 | "https://0x7f.0.0.1/mcp", |
| 712 | "https://docs.example.invalid:0/mcp", |
| 713 | "https://docs.example.invalid/${x}", |
| 714 | "ftp://docs.example.invalid/mcp", |
| 715 | ] { |
| 716 | assert!(endpoint_host(url).is_err(), "{url}"); |
| 717 | } |
| 718 | } |
| 719 | |
| 720 | #[test] |
| 721 | fn links_inside_the_package_are_refused() { |
| 722 | #[cfg(unix)] |
| 723 | { |
| 724 | let f = fixture(Some(&format!("- insert:\n{}", docs_row(""))), None); |
| 725 | let outside = tempfile::tempdir().unwrap(); |
| 726 | fs::write(outside.path().join("layer.yml"), "[]\n").unwrap(); |
| 727 | std::os::unix::fs::symlink( |
| 728 | outside.path().join("layer.yml"), |
| 729 | f.bundle.join("linked.yml"), |
| 730 | ) |
| 731 | .unwrap(); |
| 732 | fs::write( |
| 733 | f.bundle.join("package.json"), |
| 734 | json!({"name": "@demo/tools-dsh", "dsh": {"bundle": {"patch": ["./linked.yml"]}}}) |
| 735 | .to_string(), |
| 736 | ) |
| 737 | .unwrap(); |
| 738 | refused(&f, "links or reparse points"); |
| 739 | } |
| 740 | } |
| 741 | |
| 742 | #[test] |
| 743 | fn the_converted_bundle_is_a_valid_native_plugin() { |
| 744 | let f = fixture(Some(&format!("- insert:\n{}", docs_row(""))), None); |
| 745 | convert_package(&f.bundle, &f.output).unwrap(); |
| 746 | let manifest = |
| 747 | codewhale_plugins::agent_plugin::resolve_manifest_path(&f.output).expect("manifest"); |
| 748 | let validated = |
| 749 | codewhale_plugins::manifest::PluginManifest::validate_from_path(&manifest).unwrap(); |
| 750 | assert_eq!(validated.manifest.plugin.name, "tools-dsh"); |
| 751 | } |
| 752 | |
| 753 | #[test] |
| 754 | fn derived_names_are_native_plugin_names() { |
| 755 | assert_eq!( |
| 756 | derived_plugin_name("@deepseek-ai/bundle-web-app").as_deref(), |
| 757 | Some("bundle-web-app") |
| 758 | ); |
| 759 | assert_eq!( |
| 760 | derived_plugin_name("Tools_DSH").as_deref(), |
| 761 | Some("tools-dsh") |
| 762 | ); |
| 763 | assert_eq!(derived_plugin_name("@x/--"), None); |
| 764 | } |
| 765 | |
| 766 | #[test] |
| 767 | fn raw_agent_presets_import_emits_exact_native_entries_and_catalog() { |
| 768 | let source = codewhale_engine::extension_host::test_support::fixtures_dir() |
| 769 | .join("raw-agent-presets/source"); |
| 770 | let root = tempfile::tempdir().unwrap(); |
| 771 | let _host = isolated_host(root.path()); |
| 772 | let output = root.path().join("raw-preset-output"); |
| 773 | let converted = convert_package(&source, &output).unwrap(); |
| 774 | assert!(converted.requires_native); |
| 775 | assert_eq!(converted.native_rows, ["@deepseek-ai/dsh-agent-presets"]); |
| 776 | let manifest: Json = |
| 777 | serde_json::from_slice(&fs::read(output.join("plugin.json")).unwrap()).unwrap(); |
| 778 | assert_eq!( |
| 779 | manifest["extensions"]["net.codewhale"]["native"]["paths"], |
| 780 | json!(["native/presets/a.mjs", "native/presets/b.mjs"]) |
| 781 | ); |
| 782 | assert!( |
| 783 | !output.join("native/index.mjs").exists(), |
| 784 | "no unselected global composition consumer remains" |
| 785 | ); |
| 786 | let catalog: Json = |
| 787 | serde_json::from_slice(&fs::read(output.join("native/presets.json")).unwrap()).unwrap(); |
| 788 | assert_eq!(catalog["default"], "a"); |
| 789 | for row in catalog["presets"].as_array().unwrap() { |
| 790 | let entry = row["entry"]["path"].as_str().unwrap(); |
| 791 | let bytes = fs::read(output.join(entry)).unwrap(); |
| 792 | assert_eq!(row["entry"]["sha256"], sha256_hex(&bytes)); |
| 793 | let data = codewhale_plugins::native_presets::metadata_from_bytes(&bytes).unwrap(); |
| 794 | assert_eq!(data.id, row["id"].as_str().unwrap()); |
| 795 | } |
| 796 | assert!( |
| 797 | codewhale_plugins::manifest::PluginManifest::validate_from_path( |
| 798 | &output.join("plugin.json") |
| 799 | ) |
| 800 | .is_ok() |
| 801 | ); |
| 802 | } |
| 803 | |
| 804 | #[test] |
| 805 | fn contained_bare_modules_never_walk_ambient_packages_or_escape_exports() { |
| 806 | let f = fixture(None, None); |
| 807 | let package = Package::open(&f.bundle).unwrap(); |
| 808 | assert!( |
| 809 | presets::contained_module(&package, "js-yaml").is_err(), |
| 810 | "installed host dependency is not a package receipt" |
| 811 | ); |
| 812 | let dir = f.bundle.join("node_modules/@demo/profile"); |
| 813 | fs::create_dir_all(&dir).unwrap(); |
| 814 | fs::write(dir.join("package.json"), r#"{"name":"@demo/profile","type":"module","exports":{".":{"require":"./not.cjs","import":"./index.mjs"},"./escape":"../../../outside.mjs"}}"#).unwrap(); |
| 815 | fs::write(dir.join("index.mjs"), "export function apply() {}\n").unwrap(); |
| 816 | assert_eq!( |
| 817 | presets::contained_module(&package, "@demo/profile").unwrap(), |
| 818 | dir.join("index.mjs").canonicalize().unwrap() |
| 819 | ); |
| 820 | assert!(presets::contained_module(&package, "@demo/profile/escape").is_err()); |
| 821 | assert!(presets::contained_module(&package, "@demo/profile/../../outside").is_err()); |
| 822 | fs::write(dir.join("package.json"),r#"{"name":"@demo/profile","type":"module","exports":{"node":null,"import":"./index.mjs"}}"#).unwrap(); |
| 823 | assert!( |
| 824 | presets::contained_module(&package, "@demo/profile").is_err(), |
| 825 | "matched null export must not fall through" |
| 826 | ); |
| 827 | } |
| 828 | |
| 829 | #[test] |
| 830 | fn contained_bare_native_row_imports_but_an_unresolved_sibling_never_installs_a_partial_graph() { |
| 831 | let build = |mixed: bool| { |
| 832 | let f = fixture( |
| 833 | Some(&format!( |
| 834 | "- insert:\n - {{id: native-row, name: '@demo/tools-dsh'}}\n{}", |
| 835 | if mixed { |
| 836 | " - {id: missing, name: absent-package}\n" |
| 837 | } else { |
| 838 | "" |
| 839 | } |
| 840 | )), |
| 841 | Some(json!({ |
| 842 | "type": "module", |
| 843 | "exports": "./index.mjs", |
| 844 | "engines": {"dsh": ">=0.2.0-rc.1 <0.3.0-0"} |
| 845 | })), |
| 846 | ); |
| 847 | fs::write(f.bundle.join("index.mjs"),"export const inject=['prompt'];export function apply(ctx){ctx.prompt.registerSection({id:'profile',text:'actual closed bare module'})}\n").unwrap(); |
| 848 | f |
| 849 | }; |
| 850 | let valid = build(false); |
| 851 | let converted = convert_package(&valid.bundle, &valid.output).unwrap(); |
| 852 | assert!(converted.requires_native); |
| 853 | assert_eq!(converted.native_rows, ["@demo/tools-dsh"]); |
| 854 | let spec: Json = |
| 855 | serde_json::from_slice(&fs::read(valid.output.join("native/composition.json")).unwrap()) |
| 856 | .unwrap(); |
| 857 | assert_eq!(spec["modules"][0]["name"], "@demo/tools-dsh"); |
| 858 | assert_eq!(spec["modules"][0]["path"], "index.mjs"); |
| 859 | assert!( |
| 860 | codewhale_plugins::manifest::PluginManifest::validate_from_path( |
| 861 | &valid.output.join("plugin.json") |
| 862 | ) |
| 863 | .is_ok() |
| 864 | ); |
| 865 | refused(&build(true), "module (absent-package); no partial graph"); |
| 866 | } |
| 867 | |
| 868 | #[test] |
| 869 | fn raw_catalog_broken_rows_remain_structured_manual_ports_beside_healthy_entries() { |
| 870 | let source = codewhale_engine::extension_host::test_support::fixtures_dir() |
| 871 | .join("raw-agent-presets/source"); |
| 872 | let root = tempfile::tempdir().unwrap(); |
| 873 | let _host = isolated_host(root.path()); |
| 874 | let bundle = root.path().join("source"); |
| 875 | fs::create_dir(&bundle).unwrap(); |
| 876 | walk_files(&source, |path, is_dir| { |
| 877 | let target = bundle.join(path.strip_prefix(&source).unwrap()); |
| 878 | if is_dir { |
| 879 | fs::create_dir_all(target)?; |
| 880 | } else { |
| 881 | fs::create_dir_all(target.parent().unwrap())?; |
| 882 | fs::copy(path, target)?; |
| 883 | } |
| 884 | Ok(()) |
| 885 | }) |
| 886 | .unwrap(); |
| 887 | fs::create_dir_all(bundle.join("presets/broken")).unwrap(); |
| 888 | fs::write( |
| 889 | bundle.join("presets/broken/agent.cordis.yml"), |
| 890 | "[invalid yaml\n", |
| 891 | ) |
| 892 | .unwrap(); |
| 893 | let output = root.path().join("converted"); |
| 894 | let converted = convert_package(&bundle, &output).unwrap(); |
| 895 | assert!(converted.requires_native); |
| 896 | let manual: Vec<_> = converted |
| 897 | .outcomes |
| 898 | .iter() |
| 899 | .filter(|row| row.kind == "native-preset") |
| 900 | .collect(); |
| 901 | assert_eq!(manual.len(), 1); |
| 902 | assert_eq!(manual[0].row.as_deref(), Some("broken")); |
| 903 | assert!(manual[0].needs_manual_port()); |
| 904 | let catalog: Json = |
| 905 | serde_json::from_slice(&fs::read(output.join("native/presets.json")).unwrap()).unwrap(); |
| 906 | let broken = catalog["presets"] |
| 907 | .as_array() |
| 908 | .unwrap() |
| 909 | .iter() |
| 910 | .find(|row| row["id"] == "broken") |
| 911 | .unwrap(); |
| 912 | assert!(broken.get("broken").is_some()); |
| 913 | assert!(broken.get("entry").is_none()); |
| 914 | assert_eq!( |
| 915 | catalog["presets"] |
| 916 | .as_array() |
| 917 | .unwrap() |
| 918 | .iter() |
| 919 | .filter(|row| row.get("entry").is_some()) |
| 920 | .count(), |
| 921 | 2 |
| 922 | ); |
| 923 | let receipt: Json = |
| 924 | serde_json::from_slice(&fs::read(output.join("CONVERSION.json")).unwrap()).unwrap(); |
| 925 | assert!( |
| 926 | receipt["required_manual_ports"] |
| 927 | .as_array() |
| 928 | .unwrap() |
| 929 | .iter() |
| 930 | .any(|row| row["row"] == "broken") |
| 931 | ); |
| 932 | } |
| 933 | |
| 934 | mod shell_hook_import_tests { |
| 935 | use super::*; |
| 936 | #[test] |
| 937 | fn raw_mixed_preset_import_keeps_mcp_and_skills_selected_without_global_duplicates() { |
| 938 | codewhale_engine::extension_host::install_ports(); |
| 939 | let _home = codewhale_runtime::test_support::SealedHome::new(); |
| 940 | let temp = tempfile::tempdir().unwrap(); |
| 941 | let source = temp.path().join("source"); |
| 942 | fs::create_dir_all(source.join("presets/a")).unwrap(); |
| 943 | fs::create_dir_all(source.join("skills/check")).unwrap(); |
| 944 | fs::write(source.join("package.json"), r#"{"name":"@demo/mixed-preset","version":"1.0.0","dsh":{"bundle":{"patch":"./cordis.patch.json"}}}"#).unwrap(); |
| 945 | fs::write(source.join("cordis.patch.json"), json!([{"insert":[{"id":"roster","name":"@deepseek-ai/dsh-agent-presets","config":{"default":"a","roots":[{"path":"presets","trust":"user"}],"includeShippedRoot":false,"includeUserRoot":false}}]}]).to_string()).unwrap(); |
| 946 | fs::write( |
| 947 | source.join("presets/a/preset.yml"), |
| 948 | "name: Mixed\ndescription: Five selected component categories\n", |
| 949 | ) |
| 950 | .unwrap(); |
| 951 | fs::write(source.join("presets/a/agent.cordis.yml"), "- name: '@deepseek-ai/dsh-mcp-client'\n config: {serverName: scoped, transport: stdio, command: node, args: [peer.mjs]}\n- name: '@deepseek-ai/dsh-skill-filesystem'\n config: {includeDefaultRoots: false, watch: false, customSkillDirs: [skills]}\n- name: ../../authored.mjs\n").unwrap(); |
| 952 | fs::write(source.join("authored.mjs"), "export const inject=['tools','commands'];export function apply(ctx){ctx.tools.register({name:'echo',description:'mixed',parameters:{type:'object'},execute:()=>''});ctx.commands.register({name:'echo-mod',description:'mixed',handler:()=>''});ctx.on('tools/pre-execute',()=>undefined)}").unwrap(); |
| 953 | fs::write( |
| 954 | source.join("peer.mjs"), |
| 955 | "throw new Error('import must not execute this MCP process')", |
| 956 | ) |
| 957 | .unwrap(); |
| 958 | fs::write( |
| 959 | source.join("skills/check/SKILL.md"), |
| 960 | "---\nname: check\ndescription: mixed skill\n---\nInspect the selected source.\n", |
| 961 | ) |
| 962 | .unwrap(); |
| 963 | let output = temp.path().join("out"); |
| 964 | let converted = convert_package(&source, &output).unwrap(); |
| 965 | assert!(converted.requires_native); |
| 966 | assert!( |
| 967 | !converted.requires_node, |
| 968 | "Native composition uses the selected host runtime" |
| 969 | ); |
| 970 | assert!( |
| 971 | !output.join("mcp.json").exists(), |
| 972 | "no global duplicate MCP adapter" |
| 973 | ); |
| 974 | assert!( |
| 975 | !output.join("skills").exists(), |
| 976 | "no global duplicate Skill adapter" |
| 977 | ); |
| 978 | assert!(output.join("source/skills/check/SKILL.md").is_file()); |
| 979 | assert!(output.join("source/peer.mjs").is_file()); |
| 980 | assert!(output.join("native/presets/a.mjs").is_file()); |
| 981 | let manifest: Json = |
| 982 | serde_json::from_slice(&fs::read(output.join("plugin.json")).unwrap()).unwrap(); |
| 983 | assert_eq!( |
| 984 | manifest["extensions"]["net.codewhale"]["native"]["paths"], |
| 985 | json!(["native/presets/a.mjs"]) |
| 986 | ); |
| 987 | assert!(manifest.get("skills").is_none()); |
| 988 | let admitted = |
| 989 | codewhale_plugins::manifest::PluginManifest::from_path(&output.join("plugin.json")) |
| 990 | .unwrap(); |
| 991 | assert!( |
| 992 | admitted.when.is_none(), |
| 993 | "no fabricated Node binary condition" |
| 994 | ); |
| 995 | assert!( |
| 996 | admitted.check_when(), |
| 997 | "Native-only applicability is runtime-neutral" |
| 998 | ); |
| 999 | } |
| 1000 | |
| 1001 | #[test] |
| 1002 | fn native_shell_bridge_import_seals_assets_without_executing_commands() { |
| 1003 | codewhale_engine::extension_host::install_ports(); |
| 1004 | let _home = codewhale_runtime::test_support::SealedHome::new(); |
| 1005 | let temp = tempfile::tempdir().unwrap(); |
| 1006 | let bundle = temp.path().join("bundle"); |
| 1007 | fs::create_dir(&bundle).unwrap(); |
| 1008 | fs::write(bundle.join("package.json"), r#"{"name":"@demo/hook-bundle","version":"1.0.0","dsh":{"bundle":{"patch":"./cordis.patch.yml"}}}"#).unwrap(); |
| 1009 | fs::write(bundle.join("cordis.patch.yml"), "- insert:\n - id: hooks\n name: '@deepseek-ai/dsh-hooks-claude-code'\n config: {configPath: hooks.json}\n").unwrap(); |
| 1010 | let sentinel = temp.path().join("must-not-execute"); |
| 1011 | let config = json!({"hooks":{"PreToolUse":[{"hooks":[{"type":"command","command":format!("touch {}",sentinel.display())}]}]}}).to_string(); |
| 1012 | fs::write(bundle.join("hooks.json"), &config).unwrap(); |
| 1013 | let output = temp.path().join("output"); |
| 1014 | let converted = convert_package(&bundle, &output).unwrap(); |
| 1015 | assert!(converted.requires_native); |
| 1016 | assert!( |
| 1017 | !converted.requires_node, |
| 1018 | "Native composition uses the selected host runtime" |
| 1019 | ); |
| 1020 | assert_eq!( |
| 1021 | converted.native_rows, |
| 1022 | ["@deepseek-ai/dsh-hooks-claude-code"] |
| 1023 | ); |
| 1024 | let spec: Json = |
| 1025 | serde_json::from_slice(&fs::read(output.join("native/composition.json")).unwrap()) |
| 1026 | .unwrap(); |
| 1027 | assert_eq!(spec["modules"], json!([])); |
| 1028 | assert_eq!(spec["files"]["hooks.json"], sha256_hex(config.as_bytes())); |
| 1029 | assert_eq!( |
| 1030 | fs::read(output.join("source/hooks.json")).unwrap(), |
| 1031 | config.as_bytes() |
| 1032 | ); |
| 1033 | let manifest: Json = |
| 1034 | serde_json::from_slice(&fs::read(output.join("plugin.json")).unwrap()).unwrap(); |
| 1035 | assert_eq!( |
| 1036 | manifest["extensions"]["net.codewhale"]["native"]["path"], |
| 1037 | "native/index.mjs" |
| 1038 | ); |
| 1039 | let admitted = |
| 1040 | codewhale_plugins::manifest::PluginManifest::from_path(&output.join("plugin.json")) |
| 1041 | .unwrap(); |
| 1042 | assert!( |
| 1043 | admitted.when.is_none(), |
| 1044 | "no fabricated Node binary condition" |
| 1045 | ); |
| 1046 | assert!( |
| 1047 | admitted.check_when(), |
| 1048 | "Native-only applicability is runtime-neutral" |
| 1049 | ); |
| 1050 | assert!( |
| 1051 | !sentinel.exists(), |
| 1052 | "import and preview must never run a shell command" |
| 1053 | ); |
| 1054 | } |
| 1055 | } |
| 1056 | |
| 1057 | #[test] |
| 1058 | fn a_plugin_json_beside_a_dsh_package_merges_through_the_real_reviewer() { |
| 1059 | let pkg = fixture( |
| 1060 | Some("- insert:\n - id: native-row\n name: '@demo/tools-dsh'\n"), |
| 1061 | Some(json!({"main": "index.js"})), |
| 1062 | ); |
| 1063 | fs::write(pkg.bundle.join("index.js"), "export function apply() {}\n").unwrap(); |
| 1064 | fs::create_dir_all(pkg.bundle.join("commands")).unwrap(); |
| 1065 | fs::write(pkg.bundle.join("commands/hello.md"), "# Hello\n").unwrap(); |
| 1066 | fs::create_dir_all(pkg.bundle.join("agents")).unwrap(); |
| 1067 | fs::write( |
| 1068 | pkg.bundle.join("agents/reviewer.toml"), |
| 1069 | "id = \"reviewer\"\n[role]\nname = \"reviewer\"\ndescription = \"Reviews\"\nprompt = \"Review.\"\n", |
| 1070 | ) |
| 1071 | .unwrap(); |
| 1072 | fs::create_dir_all(pkg.bundle.join("hooks")).unwrap(); |
| 1073 | fs::write( |
| 1074 | pkg.bundle.join("hooks/hooks.toml"), |
| 1075 | "enabled = true\n[[hooks]]\nname = \"check\"\nevent = \"turn_end\"\ncommand = \"true\"\n", |
| 1076 | ) |
| 1077 | .unwrap(); |
| 1078 | fs::write( |
| 1079 | pkg.bundle.join("plugin.json"), |
| 1080 | json!({ |
| 1081 | "$schema": "https://agent-plugins.org/schemas/plugin.json", |
| 1082 | "name": "tools-dsh", |
| 1083 | "version": "1.2.3", |
| 1084 | "description": "ignored plugin.json description", |
| 1085 | "extensions": { |
| 1086 | "net.codewhale": { |
| 1087 | "commands": {"path": "commands"}, |
| 1088 | "agents": {"path": "agents"}, |
| 1089 | "hooks": {"path": "hooks"} |
| 1090 | } |
| 1091 | } |
| 1092 | }) |
| 1093 | .to_string(), |
| 1094 | ) |
| 1095 | .unwrap(); |
| 1096 | |
| 1097 | let (previewed, preview_hash) = codewhale_plugins::install::preview_dsh(&pkg.bundle).unwrap(); |
| 1098 | assert_eq!(previewed.plugin_name, "tools-dsh"); |
| 1099 | assert_eq!(previewed.commands, ["commands/hello.md"]); |
| 1100 | assert_eq!(previewed.agents, ["agents/reviewer.toml"]); |
| 1101 | assert_eq!(previewed.hooks, ["hooks/hooks.toml"]); |
| 1102 | |
| 1103 | let converted = convert_package(&pkg.bundle, &pkg.output).unwrap(); |
| 1104 | assert_eq!(converted.commands, ["commands/hello.md"]); |
| 1105 | assert_eq!(converted.agents, ["agents/reviewer.toml"]); |
| 1106 | assert_eq!(converted.hooks, ["hooks/hooks.toml"]); |
| 1107 | let validated = codewhale_plugins::manifest::PluginManifest::validate_from_path( |
| 1108 | &pkg.output.join("plugin.json"), |
| 1109 | ) |
| 1110 | .unwrap(); |
| 1111 | assert_eq!(validated.content_hash, preview_hash); |
| 1112 | assert_eq!(validated.inventory.native, 1); |
| 1113 | assert_eq!(validated.inventory.commands, 1); |
| 1114 | assert_eq!(validated.inventory.agents, 1); |
| 1115 | assert_eq!(validated.inventory.hooks, 1); |
| 1116 | assert_eq!( |
| 1117 | fs::read(pkg.output.join("source/commands/hello.md")).unwrap(), |
| 1118 | fs::read(pkg.output.join("commands/hello.md")).unwrap() |
| 1119 | ); |
| 1120 | |
| 1121 | let clash = fixture( |
| 1122 | Some( |
| 1123 | "- insert:\n - id: claude-hooks\n name: '@deepseek-ai/dsh-hooks-claude-code'\n config:\n configPath: bridge.json\n", |
| 1124 | ), |
| 1125 | None, |
| 1126 | ); |
| 1127 | fs::write(clash.bundle.join("bridge.json"), "{}").unwrap(); |
| 1128 | fs::create_dir_all(clash.bundle.join("hooks")).unwrap(); |
| 1129 | fs::write( |
| 1130 | clash.bundle.join("hooks/hooks.toml"), |
| 1131 | "enabled = true\n[[hooks]]\nname = \"check\"\nevent = \"turn_end\"\ncommand = \"true\"\n", |
| 1132 | ) |
| 1133 | .unwrap(); |
| 1134 | fs::write( |
| 1135 | clash.bundle.join("plugin.json"), |
| 1136 | json!({ |
| 1137 | "$schema": "https://agent-plugins.org/schemas/plugin.json", |
| 1138 | "name": "tools-dsh", |
| 1139 | "version": "1.2.3", |
| 1140 | "extensions": {"net.codewhale": {"hooks": {"path": "hooks"}}} |
| 1141 | }) |
| 1142 | .to_string(), |
| 1143 | ) |
| 1144 | .unwrap(); |
| 1145 | let err = convert_package(&clash.bundle, &clash.output) |
| 1146 | .unwrap_err() |
| 1147 | .to_string(); |
| 1148 | assert!(err.contains("package.json"), "{err}"); |
| 1149 | assert!(err.contains("plugin.json"), "{err}"); |
| 1150 | assert!(err.contains("hooks"), "{err}"); |
| 1151 | } |
| 1152 |