| 1 | use std::path::Path; |
| 2 | |
| 3 | #[test] |
| 4 | fn no_host_imports() { |
| 5 | let wasm_path = "target/wasm32-unknown-unknown/release/slides_pdf.wasm"; |
| 6 | |
| 7 | // SLIDES_REQUIRE_WASM=1 is set by the CI gate, which always builds the |
| 8 | // wasm artifact before running this test. Left unset, a bare |
| 9 | // `cargo test` on a fresh clone skips rather than failing. |
| 10 | let require_wasm = std::env::var("SLIDES_REQUIRE_WASM").as_deref() == Ok("1"); |
| 11 | |
| 12 | if !Path::new(wasm_path).exists() { |
| 13 | if require_wasm { |
| 14 | panic!( |
| 15 | "FAILED (SLIDES_REQUIRE_WASM=1): wasm artifact not found at {}. \ |
| 16 | The gate must build it before running this test.", |
| 17 | wasm_path |
| 18 | ); |
| 19 | } |
| 20 | eprintln!( |
| 21 | "SKIPPED (SLIDES_REQUIRE_WASM unset): wasm artifact not found at {}. \ |
| 22 | Run ./scripts/build-pdf-wasm.sh first.", |
| 23 | wasm_path |
| 24 | ); |
| 25 | return; |
| 26 | } |
| 27 | |
| 28 | let wasm_bytes = std::fs::read(wasm_path).expect("Failed to read wasm file"); |
| 29 | let parser = wasmparser::Parser::new(0); |
| 30 | |
| 31 | let mut imports = Vec::new(); |
| 32 | |
| 33 | for payload in parser.parse_all(&wasm_bytes) { |
| 34 | let payload = payload.expect("Failed to parse wasm module"); |
| 35 | |
| 36 | if let wasmparser::Payload::ImportSection(reader) = payload { |
| 37 | for import_result in reader.into_imports() { |
| 38 | let import = import_result.expect("Failed to parse import"); |
| 39 | imports.push(format!("{}.{}", import.module, import.name)); |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | if !imports.is_empty() { |
| 45 | panic!( |
| 46 | "WASM module has host imports (breaks Go/wazero compatibility):\n {}", |
| 47 | imports.join("\n ") |
| 48 | ); |
| 49 | } |
| 50 | |
| 51 | eprintln!("PASSED (checked {} bytes, 0 imports)", wasm_bytes.len()); |
| 52 | } |
| 53 | |