Skip to main content

alef_e2e/codegen/
wasm.rs

1//! WebAssembly e2e test generator using vitest.
2//!
3//! Reuses the TypeScript test renderer for both HTTP and non-HTTP fixtures,
4//! configured with the `@kreuzberg/wasm` (or equivalent) package as the import
5//! path and `wasm` as the language key for skip/override resolution. Adds
6//! wasm-specific scaffolding: a `setup.ts` chdir to `test_documents/` so
7//! file_path fixtures resolve, and a `globalSetup.ts` that spawns the
8//! mock-server for HTTP fixtures. The wasm-pack `--target nodejs` CJS bundle
9//! initializes synchronously and does not require vite-plugin-wasm.
10
11use crate::config::E2eConfig;
12use crate::escape::sanitize_filename;
13
14use crate::fixture::{Fixture, FixtureGroup};
15use alef_core::backend::GeneratedFile;
16use alef_core::config::ResolvedCrateConfig;
17use alef_core::hash::{self, CommentStyle};
18use alef_core::template_versions as tv;
19use anyhow::Result;
20use std::fmt::Write as FmtWrite;
21use std::path::PathBuf;
22
23use super::E2eCodegen;
24
25/// WebAssembly e2e code generator.
26pub struct WasmCodegen;
27
28impl E2eCodegen for WasmCodegen {
29    fn generate(
30        &self,
31        groups: &[FixtureGroup],
32        e2e_config: &E2eConfig,
33        config: &ResolvedCrateConfig,
34        type_defs: &[alef_core::ir::TypeDef],
35        enums: &[alef_core::ir::EnumDef],
36    ) -> Result<Vec<GeneratedFile>> {
37        let lang = self.language_name();
38        let output_base = PathBuf::from(e2e_config.effective_output()).join(lang);
39        let tests_base = output_base.join("tests");
40
41        let mut files = Vec::new();
42
43        // Resolve call config with wasm-specific overrides.
44        let call = &e2e_config.call;
45        let overrides = call.overrides.get(lang);
46        let module_path = overrides
47            .and_then(|o| o.module.as_ref())
48            .cloned()
49            .unwrap_or_else(|| call.module.clone());
50        let function_name = overrides
51            .and_then(|o| o.function.as_ref())
52            .cloned()
53            .unwrap_or_else(|| snake_to_camel(&call.function));
54        let client_factory = overrides.and_then(|o| o.client_factory.as_deref());
55
56        // Resolve package config — defaults to a co-located pkg/ directory shipped
57        // by `wasm-pack build` next to the wasm crate.
58        // When `[crates.output] wasm` is set explicitly, derive the pkg path from
59        // that value so that renamed WASM crates resolve correctly without any
60        // hardcoded special cases.
61        let wasm_pkg = e2e_config.resolve_package("wasm");
62        // `pkg_path_is_explicit` distinguishes "user told us exactly where the
63        // npm-consumable package lives" from "fall back to the default
64        // wasm-pack output directory". The render below appends `/nodejs` only
65        // for the fallback case (`wasm_crate_path()` returns the crate's
66        // `pkg/` dir, whose npm-consumable subpackage is at `pkg/nodejs/`).
67        // For an explicit path the user is responsible for pointing at a
68        // directory that already has a valid `package.json`.
69        let (pkg_path, pkg_path_is_explicit) = match wasm_pkg.as_ref().and_then(|p| p.path.as_ref()) {
70            Some(p) => (p.clone(), true),
71            None => (config.wasm_crate_path(), false),
72        };
73        let pkg_name = wasm_pkg
74            .as_ref()
75            .and_then(|p| p.name.as_ref())
76            .cloned()
77            .unwrap_or_else(|| {
78                // Default: derive from WASM crate name (config.name + "-wasm")
79                // wasm-pack transforms the crate name to the package name by replacing
80                // dashes with the crate separator in Cargo (e.g., kreuzberg-wasm -> kreuzberg_wasm).
81                // However, the published npm package might use the module name, which is typically
82                // the crate name without "-wasm". Fall back to the module path.
83                module_path.clone()
84            });
85        let pkg_version = wasm_pkg
86            .as_ref()
87            .and_then(|p| p.version.as_ref())
88            .cloned()
89            .or_else(|| config.resolved_version())
90            .unwrap_or_else(|| "0.1.0".to_string());
91
92        // Determine which auxiliary scaffolding files we need based on the active
93        // fixture set. Doing this once up front lets us emit a self-contained vitest
94        // config that wires only the setup files we'll actually generate.
95        //
96        // WASM language filtering: when `[crates.wasm].languages` is set, auto-skip
97        // fixtures for languages not in that static-compiled list. This bridges the gap
98        // between the full language pack and WASM's 8-language static build.
99        let wasm_languages = config.wasm.as_ref().and_then(|w| {
100            if w.languages.is_empty() {
101                None
102            } else {
103                Some(w.languages.clone())
104            }
105        });
106
107        // Build active fixtures per group. For WASM, when the backend declares a static
108        // language set via `[crates.wasm].languages`, include fixtures for languages
109        // not in that set but mark them with auto-skip directives so they render as
110        // `it.skip()` tests instead of being omitted entirely.
111        let active_per_group: Vec<Vec<Fixture>> = groups
112            .iter()
113            .map(|group| {
114                let mut result = Vec::new();
115                for fixture in &group.fixtures {
116                    // Determine if this fixture should be included.
117                    // Start with the base should_include_fixture check.
118                    let base_include = super::should_include_fixture(fixture, lang, e2e_config);
119
120                    // Check per-call skip_languages (fixture is completely unsupported)
121                    let cc = e2e_config.resolve_call_for_fixture(
122                        fixture.call.as_deref(),
123                        &fixture.id,
124                        &fixture.resolved_category(),
125                        &fixture.tags,
126                        &fixture.input,
127                    );
128                    if cc.skip_languages.iter().any(|l| l == lang) {
129                        // Per-call skip — drop entirely, never include
130                        continue;
131                    }
132
133                    if base_include {
134                        // Check node fetch compatibility
135                        if let Some(http) = &fixture.http {
136                            if http
137                                .request
138                                .headers
139                                .iter()
140                                .any(|(k, _)| k.eq_ignore_ascii_case("content-length"))
141                            {
142                                // Node fetch rejects mismatched Content-Length — skip fixture
143                                continue;
144                            }
145                            let m = http.request.method.to_ascii_uppercase();
146                            if m == "TRACE" || m == "CONNECT" {
147                                // Node fetch doesn't support these methods — skip fixture
148                                continue;
149                            }
150                        }
151
152                        // Include the fixture normally
153                        result.push(fixture.clone());
154                    } else if let Some(ref wasm_langs) = wasm_languages {
155                        // Fixture failed should_include_fixture. If WASM has a static
156                        // language set, auto-add a skip directive so it renders as it.skip()
157                        // instead of being omitted entirely.
158                        let mut auto_skipped = fixture.clone();
159                        if auto_skipped.skip.is_none() {
160                            // Only auto-skip if the fixture doesn't already have a skip directive
161                            auto_skipped.skip = Some(crate::fixture::SkipDirective {
162                                languages: vec!["wasm".to_string()],
163                                reason: Some(format!(
164                                    "language not in WASM's static-compiled set: [{}]",
165                                    wasm_langs.join(", ")
166                                )),
167                            });
168                            result.push(auto_skipped);
169                        }
170                    }
171                }
172                result
173            })
174            .collect();
175
176        let any_fixtures = active_per_group.iter().flat_map(|g| g.iter());
177        // The wasm globalSetup spawns the mock server. It must run for any fixture
178        // that interpolates `${process.env.MOCK_SERVER_URL}` into a base URL —
179        // i.e. anything with `mock_response` (liter-llm shape) or `http`
180        // (kreuzberg/kreuzcrawl shape), not just raw `is_http_test`. The
181        // comment block below this line states the same intent; the previous
182        // condition (`f.is_http_test()`) only detected the consumer-style
183        // `http: { ... }` shape and missed the entire liter-llm fixture set.
184        let has_http_fixtures = any_fixtures.clone().any(|f| f.needs_mock_server());
185        // file_path / bytes args are read off disk by the generated code at runtime;
186        // we add a setup.ts chdir to test_documents so relative paths resolve.
187        let has_file_fixtures = active_per_group.iter().flatten().any(|f| {
188            let cc = e2e_config.resolve_call_for_fixture(
189                f.call.as_deref(),
190                &f.id,
191                &f.resolved_category(),
192                &f.tags,
193                &f.input,
194            );
195            cc.args
196                .iter()
197                .any(|a| a.arg_type == "file_path" || a.arg_type == "bytes")
198        });
199
200        // Generate package.json — adds vitest + rollup dev deps so that the test
201        // suite can import the wasm-pack nodejs CJS bundle by package name.
202        files.push(GeneratedFile {
203            path: output_base.join("package.json"),
204            content: render_package_json(
205                &pkg_name,
206                &pkg_path,
207                pkg_path_is_explicit,
208                &pkg_version,
209                e2e_config.dep_mode,
210                e2e_config.harness_extras.get("wasm"),
211            ),
212            generated_header: false,
213        });
214
215        // Generate vitest.config.ts — optional globalSetup (for HTTP fixtures and
216        // any function-call test that hits the mock server via MOCK_SERVER_URL)
217        // and setupFiles (for chdir). Function-call e2e tests construct request URLs via
218        // `${process.env.MOCK_SERVER_URL}/fixtures/<id>`, so the mock server must
219        // be running and the env var set even when no raw HTTP fixtures exist.
220        let needs_global_setup = has_http_fixtures;
221        files.push(GeneratedFile {
222            path: output_base.join("vitest.config.ts"),
223            content: render_vitest_config(needs_global_setup, has_file_fixtures),
224            generated_header: true,
225        });
226
227        // Generate globalSetup.ts when any fixture requires the mock server —
228        // either an HTTP fixture (the original consumer) or any function-call
229        // fixture that interpolates `${process.env.MOCK_SERVER_URL}` into a
230        // base URL. It spawns the rust mock-server binary.
231        if needs_global_setup {
232            files.push(GeneratedFile {
233                path: output_base.join("globalSetup.ts"),
234                content: render_global_setup(),
235                generated_header: true,
236            });
237        }
238
239        // Generate setup.ts when any active fixture takes a file_path / bytes arg.
240        // This chdir's to test_documents/ so relative fixture paths resolve.
241        if has_file_fixtures {
242            files.push(GeneratedFile {
243                path: output_base.join("setup.ts"),
244                content: render_file_setup(&e2e_config.test_documents_dir),
245                generated_header: true,
246            });
247        }
248
249        // Generate tsconfig.json — prevents Vite from walking up to a project-level
250        // tsconfig and pulling in unrelated compiler options.
251        files.push(GeneratedFile {
252            path: output_base.join("tsconfig.json"),
253            content: render_tsconfig(),
254            generated_header: false,
255        });
256
257        // Emit a local `pnpm-workspace.yaml` declaring `e2e/wasm/` as its own
258        // pnpm workspace root. Without this, `pnpm install` walks up to the
259        // repo-root `pnpm-workspace.yaml`, where polyglot repos commonly
260        // exclude `e2e/wasm` (it depends on a `wasm-pack build` artifact that
261        // is absent on fresh checkouts). The CLI flag `--ignore-workspace`
262        // would also work, but it forces every caller (Taskfile, CI step) to
263        // pass it; making `e2e/wasm/` self-rooted keeps the generated suite
264        // self-contained.
265        files.push(GeneratedFile {
266            path: output_base.join("pnpm-workspace.yaml"),
267            content: "packages:\n  - \".\"\n".to_string(),
268            generated_header: false,
269        });
270
271        // Resolve options_type from override (e.g. `WasmExtractionConfig`).
272        let options_type = overrides.and_then(|o| o.options_type.clone());
273
274        // Generate test files per category. We delegate the per-fixture rendering
275        // to the typescript codegen (`render_test_file`), which already handles
276        // both HTTP and function-call fixtures correctly. Passing `lang = "wasm"`
277        // routes per-fixture override resolution and skip checks through the wasm
278        // language key. We then inject Node.js WASM initialization code to load
279        // the WASM binary from the pkg directory using fs.readFileSync.
280        let wasm_type_prefix = config.wasm_type_prefix();
281        for (group, active) in groups.iter().zip(active_per_group.iter()) {
282            if active.is_empty() {
283                continue;
284            }
285            let filename = format!("{}.test.ts", sanitize_filename(&group.category));
286            // Convert Vec<Fixture> to Vec<&Fixture> for render_test_file
287            let active_refs: Vec<&Fixture> = active.iter().collect();
288            let content = super::typescript::render_test_file(
289                lang,
290                &group.category,
291                &active_refs,
292                &module_path,
293                &pkg_name,
294                &function_name,
295                &e2e_config.call.args,
296                options_type.as_deref(),
297                client_factory,
298                e2e_config,
299                type_defs,
300                enums,
301                &wasm_type_prefix,
302            );
303
304            // The local `pkg/` directory produced by `wasm-pack build --target nodejs`
305            // is already a Node-friendly self-initializing CJS module — `pkg/package.json`
306            // sets `"main"` to the JS entry, so test files can import the package by name
307            // (`from "<pkg_name>"`) with no subpath. The historical `dist-node` rewrite
308            // assumed a multi-distribution layout (`dist/`, `dist-node/`, `dist-web/`)
309            // that the alef-managed `wasm-pack build` does not produce; it is therefore
310            // intentionally absent here.
311            let _ = (&pkg_path, &config.name); // keep variables alive for future use
312
313            files.push(GeneratedFile {
314                path: tests_base.join(filename),
315                content,
316                generated_header: true,
317            });
318        }
319
320        Ok(files)
321    }
322
323    fn language_name(&self) -> &'static str {
324        "wasm"
325    }
326}
327
328fn snake_to_camel(s: &str) -> String {
329    let mut out = String::with_capacity(s.len());
330    let mut upper_next = false;
331    for ch in s.chars() {
332        if ch == '_' {
333            upper_next = true;
334        } else if upper_next {
335            out.push(ch.to_ascii_uppercase());
336            upper_next = false;
337        } else {
338            out.push(ch);
339        }
340    }
341    out
342}
343
344fn render_package_json(
345    pkg_name: &str,
346    pkg_path: &str,
347    pkg_path_is_explicit: bool,
348    pkg_version: &str,
349    dep_mode: crate::config::DependencyMode,
350    extras: Option<&alef_core::config::manifest_extras::ManifestExtras>,
351) -> String {
352    let dep_value = match dep_mode {
353        crate::config::DependencyMode::Registry => pkg_version.to_string(),
354        // Fallback path: `wasm-pack build --target nodejs --out-dir pkg/nodejs` writes
355        // the npm-consumable package (its own package.json with `main`/`types` etc.)
356        // to `pkg/nodejs/`, not to `pkg/` directly. The fallback `wasm_crate_path()`
357        // points at `pkg/`, so we descend into `nodejs/` to find a valid
358        // package.json. When the user has set `[e2e.packages.wasm].path` explicitly,
359        // we trust they have already pointed at a directory with a valid package.json
360        // (the crate root, the wasm-pack out-dir, or another distribution layout) and
361        // do not mutate it.
362        crate::config::DependencyMode::Local => {
363            if pkg_path_is_explicit {
364                format!("file:{pkg_path}")
365            } else {
366                format!("file:{pkg_path}/nodejs")
367            }
368        }
369    };
370    let rendered = crate::template_env::render(
371        "wasm/package.json.jinja",
372        minijinja::context! {
373            pkg_name => pkg_name,
374            dep_value => dep_value,
375            rollup => tv::npm::ROLLUP,
376            vitest => tv::npm::VITEST,
377        },
378    );
379    match extras {
380        Some(e) if !e.is_empty() => crate::codegen::typescript::config::inject_package_json_extras(&rendered, e),
381        _ => rendered,
382    }
383}
384
385fn render_vitest_config(with_global_setup: bool, with_file_setup: bool) -> String {
386    let header = hash::header(CommentStyle::DoubleSlash);
387    crate::template_env::render(
388        "wasm/vitest.config.ts.jinja",
389        minijinja::context! {
390            header => header,
391            with_global_setup => with_global_setup,
392            with_file_setup => with_file_setup,
393        },
394    )
395}
396
397fn render_file_setup(test_documents_dir: &str) -> String {
398    let header = hash::header(CommentStyle::DoubleSlash);
399    let mut out = header;
400    out.push_str("import { createRequire } from 'module';\n");
401    out.push_str("import { fileURLToPath } from 'url';\n");
402    out.push_str("import { dirname, join } from 'path';\n\n");
403    out.push_str("// Patch CommonJS `require('env')` and `require('wasi_snapshot_preview1')` to\n");
404    out.push_str("// return shim objects. wasm-pack `--target nodejs` emits bare `require()`\n");
405    out.push_str("// calls for these from getrandom/wasi transitives, but they are not real\n");
406    out.push_str("// Node modules — the WASM module imports them by name and the host is\n");
407    out.push_str("// expected to satisfy them. Patch Module._load BEFORE the wasm bundle is\n");
408    out.push_str("// imported by any test file.\n");
409    out.push_str("// Note: setupFiles run per-test-worker; vitest imports the test files\n");
410    out.push_str("// AFTER setupFiles complete, so this hook installs in time.\n");
411    out.push_str("{\n");
412    out.push_str("  const _require = createRequire(import.meta.url);\n");
413    out.push_str("  const Module = _require('module');\n");
414    out.push_str("  // env.system / env.mkstemp come from C-runtime calls embedded in some\n");
415    out.push_str("  // WASM-compiled deps (e.g. tesseract-wasm). Tests that don't exercise\n");
416    out.push_str("  // those paths only need the imports to be callable for module instantiation.\n");
417    out.push_str("  const env = {\n");
418    out.push_str("    system: (_cmd: number) => -1,\n");
419    out.push_str("    mkstemp: (_template: number) => -1,\n");
420    out.push_str("  };\n");
421    out.push_str("  // WASI shims. Critical: clock_time_get and random_get must produce realistic\n");
422    out.push_str("  // values — returning 0 for all clock calls causes WASM-side timing loops to\n");
423    out.push_str("  // spin forever (e.g. getrandom's spin-until-elapsed retry), and zero-filled\n");
424    out.push_str("  // random buffers can cause init loops in deps expecting non-zero entropy.\n");
425    out.push_str("  const _wasiMemoryView = (): DataView | null => {\n");
426    out.push_str("    // Imports are wired before the WASM is instantiated; the bundle stashes\n");
427    out.push_str("    // its instance on a runtime-known global once available. We try to grab\n");
428    out.push_str("    // it lazily so writes to wasm memory go to the right place.\n");
429    out.push_str("    const g = globalThis as unknown as { __alef_wasm_memory__?: WebAssembly.Memory };\n");
430    out.push_str("    return g.__alef_wasm_memory__ ? new DataView(g.__alef_wasm_memory__.buffer) : null;\n");
431    out.push_str("  };\n");
432    out.push_str("  const _cryptoFill = (buf: Uint8Array) => {\n");
433    out.push_str("    const c = globalThis.crypto;\n");
434    out.push_str("    if (c && typeof c.getRandomValues === 'function') c.getRandomValues(buf);\n");
435    out.push_str("    else for (let i = 0; i < buf.length; i++) buf[i] = Math.floor(Math.random() * 256);\n");
436    out.push_str("  };\n");
437    out.push_str("  const wasi_snapshot_preview1 = {\n");
438    out.push_str("    proc_exit: () => {},\n");
439    out.push_str("    environ_get: () => 0,\n");
440    out.push_str("    environ_sizes_get: (countOut: number, _sizeOut: number) => {\n");
441    out.push_str("      const v = _wasiMemoryView();\n");
442    out.push_str("      if (v) v.setUint32(countOut, 0, true);\n");
443    out.push_str("      return 0;\n");
444    out.push_str("    },\n");
445    out.push_str("    // WASI fd_write must update `nwritten_ptr` with the total bytes consumed,\n");
446    out.push_str("    // otherwise libc-style callers (e.g. tesseract-compiled-to-wasm fputs)\n");
447    out.push_str("    // see 0 of N bytes written and retry forever, hanging the host.\n");
448    out.push_str("    fd_write: (_fd: number, iovsPtr: number, iovsLen: number, nwrittenPtr: number) => {\n");
449    out.push_str("      const v = _wasiMemoryView();\n");
450    out.push_str("      if (!v) return 0;\n");
451    out.push_str("      let total = 0;\n");
452    out.push_str("      for (let i = 0; i < iovsLen; i++) {\n");
453    out.push_str("        const off = iovsPtr + i * 8;\n");
454    out.push_str("        total += v.getUint32(off + 4, true);\n");
455    out.push_str("      }\n");
456    out.push_str("      v.setUint32(nwrittenPtr, total, true);\n");
457    out.push_str("      return 0;\n");
458    out.push_str("    },\n");
459    out.push_str("    // Mirror fd_write: callers retry on partial reads. Reporting 0 bytes\n");
460    out.push_str("    // read (EOF) is fine; just make sure `nread_ptr` is written.\n");
461    out.push_str("    fd_read: (_fd: number, _iovsPtr: number, _iovsLen: number, nreadPtr: number) => {\n");
462    out.push_str("      const v = _wasiMemoryView();\n");
463    out.push_str("      if (v) v.setUint32(nreadPtr, 0, true);\n");
464    out.push_str("      return 0;\n");
465    out.push_str("    },\n");
466    out.push_str("    fd_seek: () => 0,\n");
467    out.push_str("    fd_close: () => 0,\n");
468    out.push_str("    fd_prestat_get: () => 8, // EBADF — no preopens.\n");
469    out.push_str("    fd_prestat_dir_name: () => 0,\n");
470    out.push_str("    fd_fdstat_get: () => 0,\n");
471    out.push_str("    fd_fdstat_set_flags: () => 0,\n");
472    out.push_str("    path_open: () => 44, // ENOENT.\n");
473    out.push_str("    path_create_directory: () => 0,\n");
474    out.push_str("    path_remove_directory: () => 0,\n");
475    out.push_str("    path_unlink_file: () => 0,\n");
476    out.push_str("    path_filestat_get: () => 44, // ENOENT.\n");
477    out.push_str("    path_rename: () => 0,\n");
478    out.push_str("    clock_time_get: (_clockId: number, _precision: bigint, timeOut: number) => {\n");
479    out.push_str("      const ns = BigInt(Date.now()) * 1_000_000n + BigInt(performance.now() | 0) % 1_000_000n;\n");
480    out.push_str("      const v = _wasiMemoryView();\n");
481    out.push_str("      if (v) v.setBigUint64(timeOut, ns, true);\n");
482    out.push_str("      return 0;\n");
483    out.push_str("    },\n");
484    out.push_str("    clock_res_get: (_clockId: number, resOut: number) => {\n");
485    out.push_str("      const v = _wasiMemoryView();\n");
486    out.push_str("      if (v) v.setBigUint64(resOut, 1_000n, true);\n");
487    out.push_str("      return 0;\n");
488    out.push_str("    },\n");
489    out.push_str("    random_get: (bufPtr: number, bufLen: number) => {\n");
490    out.push_str("      const g = globalThis as unknown as { __alef_wasm_memory__?: WebAssembly.Memory };\n");
491    out.push_str("      if (!g.__alef_wasm_memory__) return 0;\n");
492    out.push_str("      _cryptoFill(new Uint8Array(g.__alef_wasm_memory__.buffer, bufPtr, bufLen));\n");
493    out.push_str("      return 0;\n");
494    out.push_str("    },\n");
495    out.push_str("    args_get: () => 0,\n");
496    out.push_str("    args_sizes_get: (countOut: number, _sizeOut: number) => {\n");
497    out.push_str("      const v = _wasiMemoryView();\n");
498    out.push_str("      if (v) v.setUint32(countOut, 0, true);\n");
499    out.push_str("      return 0;\n");
500    out.push_str("    },\n");
501    out.push_str("    poll_oneoff: () => 0,\n");
502    out.push_str("    sched_yield: () => 0,\n");
503    out.push_str("  };\n");
504    out.push_str("  const _origResolve = Module._resolveFilename;\n");
505    out.push_str("  Module._resolveFilename = function(request: string, parent: unknown, ...rest: unknown[]) {\n");
506    out.push_str("    if (request === 'env' || request === 'wasi_snapshot_preview1') return request;\n");
507    out.push_str("    return _origResolve.call(this, request, parent, ...rest);\n");
508    out.push_str("  };\n");
509    out.push_str("  const _origLoad = Module._load;\n");
510    out.push_str("  Module._load = function(request: string, parent: unknown, ...rest: unknown[]) {\n");
511    out.push_str("    if (request === 'env') return env;\n");
512    out.push_str("    if (request === 'wasi_snapshot_preview1') return wasi_snapshot_preview1;\n");
513    out.push_str("    return _origLoad.call(this, request, parent, ...rest);\n");
514    out.push_str("  };\n");
515    out.push_str("  // Capture the WASM linear memory at instantiation time so the WASI shims\n");
516    out.push_str("  // can read/write into it. Without this, every shim that needs memory\n");
517    out.push_str("  // (fd_write nwritten, clock_time_get, random_get, etc.) silently no-ops\n");
518    out.push_str("  // and the host-side C runtime hangs in a retry loop.\n");
519    out.push_str("  const _OrigInstance = WebAssembly.Instance;\n");
520    out.push_str("  const PatchedInstance = function(this: WebAssembly.Instance, mod: WebAssembly.Module, imports?: WebAssembly.Imports) {\n");
521    out.push_str("    const inst = new _OrigInstance(mod, imports);\n");
522    out.push_str("    const exportsMem = (inst.exports as Record<string, unknown>).memory;\n");
523    out.push_str("    if (exportsMem instanceof WebAssembly.Memory) {\n");
524    out.push_str("      (globalThis as unknown as { __alef_wasm_memory__?: WebAssembly.Memory }).__alef_wasm_memory__ = exportsMem;\n");
525    out.push_str("    }\n");
526    out.push_str("    return inst;\n");
527    out.push_str("  } as unknown as typeof WebAssembly.Instance;\n");
528    out.push_str("  PatchedInstance.prototype = _OrigInstance.prototype;\n");
529    out.push_str(
530        "  (WebAssembly as unknown as { Instance: typeof WebAssembly.Instance }).Instance = PatchedInstance;\n",
531    );
532    out.push_str("}\n\n");
533    out.push_str("// Change to the configured test-documents directory so that fixture file paths like\n");
534    out.push_str("// \"pdf/fake_memo.pdf\" resolve correctly when vitest runs from e2e/wasm/.\n");
535    out.push_str("// setup.ts lives in e2e/wasm/; the fixtures dir lives at the repository root,\n");
536    out.push_str("// two directories up: e2e/wasm/ -> e2e/ -> repo root.\n");
537    out.push_str("const __filename = fileURLToPath(import.meta.url);\n");
538    out.push_str("const __dirname = dirname(__filename);\n");
539    let _ = writeln!(
540        out,
541        "const testDocumentsDir = join(__dirname, '..', '..', '{test_documents_dir}');"
542    );
543    out.push_str("process.chdir(testDocumentsDir);\n");
544    out
545}
546
547fn render_global_setup() -> String {
548    let header = hash::header(CommentStyle::DoubleSlash);
549    crate::template_env::render(
550        "wasm/globalSetup.ts.jinja",
551        minijinja::context! {
552            header => header,
553        },
554    )
555}
556
557fn render_tsconfig() -> String {
558    crate::template_env::render("wasm/tsconfig.jinja", minijinja::context! {})
559}
560
561// The historical `inject_wasm_init` post-processor rewrote test imports to a
562// `<pkg>/dist-node` subpath. It was removed because the alef-managed
563// `wasm-pack build --target nodejs` artifact is a flat self-initializing CJS
564// module — its `package.json` already sets `"main"` to the JS entry, so the
565// emitted `import … from "<pkg>"` resolves directly.