Files
AniCraft/crates/engine/tests/wasm.rs
T
Tanish Manku 7dcd01041e feat(shell): web build of the editor: browser entry, file picker and downloads, autosave to browser storage (M8)
src/web.rs starts the editor on a page canvas with eframe's WebRunner (WebGL2 via glow) and
provides the browser's services; platform.rs decides when: picked files go to the engine's
in-memory store and the command runs, saves and exports leave as downloads, preferences and
recovery copies live in localStorage, Web Locks tell open tabs from crashed ones, and leaving
with unsaved work asks first. The GPU stage and sound output become build cfgs that are off
on wasm. Video export and plug-in installs are dimmed in the browser with the reason.

scripts/build-web.sh and .ps1 build web/ with the wasm-bindgen CLI pinned to Cargo.lock's
version, from packaging/web/index.html. The web profile (opt-level s, stripped, renderer and
geometry at opt-level 3) gives an 8973 KB module, 3278 KB gzipped, in 41 s.
crates/engine/tests/wasm.rs runs the engine inside WebAssembly in Node (3 tests, 0.6 s).
2026-11-09 22:36:09 -07:00

81 lines
3.6 KiB
Rust

//! The engine inside WebAssembly (ADR 0036), as the web editor runs it: one thread, no file
//! system, no `std::time` clock. Run in Node by wasm-bindgen's test runner (docs/web.md):
//!
//! ```sh
//! CARGO_TARGET_WASM32_UNKNOWN_UNKNOWN_RUNNER=wasm-bindgen-test-runner \
//! cargo test -p anicraft-engine --target wasm32-unknown-unknown --test wasm
//! ```
//!
//! A build can't catch what traps at run time (a clock or thread call that compiles but isn't
//! there); these do. Natively this file is empty.
#![cfg(target_arch = "wasm32")]
use anicraft_engine::plugins::{Host, Limits, Plugin};
use anicraft_engine::render_service::RenderService;
use anicraft_engine::{Session, vfs};
use serde_json::{Map, Value, json};
use std::path::Path;
use wasm_bindgen_test::wasm_bindgen_test;
fn names() -> Vec<String> {
vfs::take_written().into_iter().map(|(p, _)| p.display().to_string()).collect()
}
#[wasm_bindgen_test]
fn saves_opens_and_exports_through_memory() {
assert!(vfs::in_memory(), "the browser has no disk: files live in memory");
let mut s = Session::new(anicraft_engine::doc::sample::demo());
s.execute("file.save", &json!({"path": "walk.anicraft"})).unwrap();
let saved = vfs::take_written();
assert_eq!(saved.len(), 1);
vfs::put(Path::new("walk.anicraft"), saved[0].1.clone()).unwrap();
let mut t = Session::default();
t.execute("file.open", &json!({"path": "walk.anicraft"})).unwrap();
assert_eq!(t.frame_count(), s.frame_count());
// Parallel renders (rayon on the one thread), timing (web clock), encoders.
s.execute("file.export_png", &json!({"path": "f.png"})).unwrap();
s.execute("file.export_gif", &json!({"path": "m.gif", "to": 4})).unwrap();
s.execute("file.export_html", &json!({"path": "m.html"})).unwrap();
s.execute("file.export_png_sequence", &json!({"dir": "seq", "to": 2})).unwrap();
assert_eq!(names().len(), 6);
assert!(s.execute("file.export_video", &json!({"path": "m.webm"})).is_err());
}
#[wasm_bindgen_test]
fn the_stage_renders_without_a_thread() {
let s = Session::new(anicraft_engine::doc::sample::demo());
let svc = RenderService::new(64 << 20, None);
let view = anicraft_engine::render::View::stage(&s.doc, 0.5);
// No worker thread can start, so a request renders right away.
let img = svc.request(svc.job(s.state, &s.doc, 3, view, false)).expect("rendered on this thread");
assert!(img.error.is_none() && img.width > 0 && img.rgba_premul.iter().any(|&b| b != 0));
}
struct Quiet;
impl Host for Quiet {
fn request(&mut self, _: &str, _: &Value) -> Result<Value, String> {
Ok(Value::Null)
}
}
#[wasm_bindgen_test]
fn plugins_run_in_the_browser_too() {
let manifest = r#"{"id":"org.test.web","name":"Web","version":"1.0.0","kind":"command","author":"Tests"}"#;
let text = format!(
r#"(module
(import "anicraft" "call" (func (param i32 i32) (result i32)))
(import "anicraft" "response" (func (param i32 i32) (result i32)))
(memory (export "memory") 1)
(data (i32.const 1024) "{manifest}")
(func (export "ac_abi_version") (result i32) i32.const 1)
(func (export "ac_manifest") (result i64) (i64.or (i64.shl (i64.const {len}) (i64.const 32)) (i64.const 1024)))
(func (export "ac_alloc") (param i32) (result i32) i32.const 4096)
(func (export "ac_run") (param i32 i32) (result i32) i32.const 0))"#,
manifest = manifest.replace('"', "\\22"),
len = manifest.len()
);
let plugin = Plugin::load(&wat::parse_str(&text).unwrap(), Limits::default()).unwrap();
assert_eq!(plugin.id(), "org.test.web");
plugin.run(&mut Quiet, &Map::new()).unwrap();
}