|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/* |
| 4 | + * Run a WASI-compatible test program in the browser. |
| 5 | + * |
| 6 | + * This script behaves like `wasmtime` but runs given WASI-compatible test |
| 7 | + * program in the browser. |
| 8 | + * |
| 9 | + * Example: |
| 10 | + * $ ./harness.mjs check.wasm |
| 11 | + */ |
| 12 | + |
| 13 | +import { parseArgs } from 'node:util'; |
| 14 | +import { createServer } from 'node:http'; |
| 15 | +import { fileURLToPath } from 'node:url'; |
| 16 | +import { dirname, join } from 'node:path'; |
| 17 | +import { readFileSync } from 'node:fs'; |
| 18 | +import url from "node:url"; |
| 19 | +import { chromium } from 'playwright'; |
| 20 | + |
| 21 | +const SKIP_TESTS = [ |
| 22 | + // "poll_oneoff" can't be implemented in the browser |
| 23 | + "libc-test/functional/pthread_cond", |
| 24 | + // atomic.wait32 can't be executed on the main thread |
| 25 | + "libc-test/functional/pthread_mutex", |
| 26 | + "libc-test/functional/pthread_tsd", |
| 27 | + // XFAIL: @bjorn3/browser_wasi_shim doesn't support symlinks for now |
| 28 | + "misc/fts", |
| 29 | +]; |
| 30 | + |
| 31 | +/** |
| 32 | + * @param {{wasmPath: string, port: number}} |
| 33 | + * @returns {Promise<{server: import('node:http').Server, port: number}>} |
| 34 | + */ |
| 35 | +async function startServer({ wasmPath, port }) { |
| 36 | + const server = createServer((req, res) => { |
| 37 | + // Set required headers for SharedArrayBuffer |
| 38 | + res.setHeader('Cross-Origin-Opener-Policy', 'same-origin'); |
| 39 | + res.setHeader('Cross-Origin-Embedder-Policy', 'require-corp'); |
| 40 | + |
| 41 | + let filePath; |
| 42 | + const parsed = url.parse(req.url, true); |
| 43 | + const pathname = parsed.pathname; |
| 44 | + if (pathname === "/target.wasm") { |
| 45 | + // Serve the test target Wasm file |
| 46 | + filePath = wasmPath; |
| 47 | + res.setHeader('Content-Type', 'application/wasm'); |
| 48 | + } else { |
| 49 | + // Serve other resources |
| 50 | + const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 51 | + filePath = join(__dirname, pathname); |
| 52 | + const contentTypes = { |
| 53 | + "mjs": "text/javascript", |
| 54 | + "js": "text/javascript", |
| 55 | + "html": "text/html", |
| 56 | + } |
| 57 | + res.setHeader('Content-Type', contentTypes[pathname.split('.').pop()] || 'text/plain'); |
| 58 | + } |
| 59 | + |
| 60 | + try { |
| 61 | + const content = readFileSync(filePath); |
| 62 | + res.end(content); |
| 63 | + } catch (error) { |
| 64 | + res.statusCode = 404; |
| 65 | + res.end('Not found'); |
| 66 | + } |
| 67 | + }); |
| 68 | + |
| 69 | + return new Promise((resolve) => { |
| 70 | + server.listen(port, () => { |
| 71 | + const port = server.address().port; |
| 72 | + resolve({ server, port }); |
| 73 | + }); |
| 74 | + }); |
| 75 | +} |
| 76 | + |
| 77 | +/** @param {number} port */ |
| 78 | +function buildUrl(port) { |
| 79 | + return `http://localhost:${port}/run-test.html`; |
| 80 | +} |
| 81 | + |
| 82 | +/** @param {import('playwright').Page} page */ |
| 83 | +/** @param {number} port */ |
| 84 | +/** @returns {Promise<{passed: boolean, error?: string}>} */ |
| 85 | +async function runTest(page, port) { |
| 86 | + const url = buildUrl(port); |
| 87 | + const onExit = new Promise((resolve) => { |
| 88 | + page.exposeFunction("exitTest", resolve); |
| 89 | + }); |
| 90 | + await page.goto(url); |
| 91 | + return onExit; |
| 92 | +} |
| 93 | + |
| 94 | +async function main() { |
| 95 | + // Parse and interpret a subset of the wasmtime CLI options used by the tests |
| 96 | + const args = parseArgs({ |
| 97 | + args: process.argv.slice(2), |
| 98 | + allowPositionals: true, |
| 99 | + options: { |
| 100 | + // MARK: wasmtime CLI options |
| 101 | + wasi: { |
| 102 | + type: "string", |
| 103 | + multiple: true, |
| 104 | + }, |
| 105 | + dir: { |
| 106 | + type: "string", |
| 107 | + multiple: true, |
| 108 | + }, |
| 109 | + // MARK: For debugging purposes |
| 110 | + headful: { |
| 111 | + type: "boolean", |
| 112 | + default: false, |
| 113 | + }, |
| 114 | + port: { |
| 115 | + type: "string", |
| 116 | + default: "0", |
| 117 | + } |
| 118 | + } |
| 119 | + }); |
| 120 | + |
| 121 | + const wasmPath = args.positionals[0]; |
| 122 | + if (!wasmPath) { |
| 123 | + console.error('Test path not specified'); |
| 124 | + return 1; |
| 125 | + } |
| 126 | + |
| 127 | + if (SKIP_TESTS.some(test => wasmPath.includes(test + "."))) { |
| 128 | + // Silently skip tests that are known to fail in the browser |
| 129 | + return 0; |
| 130 | + } |
| 131 | + |
| 132 | + if (args.values.dir && args.values.dir.length > 0) { |
| 133 | + // Silently skip tests that require preopened directories for now |
| 134 | + // as it adds more complexity to the harness and file system emulation |
| 135 | + // is not our primary testing target. |
| 136 | + return 0; |
| 137 | + } |
| 138 | + |
| 139 | + // Start a HTTP server to serve the test files |
| 140 | + const { server, port } = await startServer({ wasmPath, port: parseInt(args.values.port) }); |
| 141 | + |
| 142 | + const browser = await chromium.launch(); |
| 143 | + const page = await browser.newPage(); |
| 144 | + |
| 145 | + try { |
| 146 | + if (args.values.headful) { |
| 147 | + // Run in headful mode to allow manual testing |
| 148 | + console.log(`Please visit ${buildUrl(port)}`); |
| 149 | + console.log('Press Ctrl+C to stop'); |
| 150 | + await new Promise(resolve => process.on('SIGINT', resolve)); |
| 151 | + return 0; |
| 152 | + } |
| 153 | + |
| 154 | + // Run in headless mode |
| 155 | + const result = await runTest(page, port); |
| 156 | + if (!result.passed) { |
| 157 | + console.error('Test failed:', result.error); |
| 158 | + console.error(`Hint: You can debug the test by running it in headful mode by passing --headful |
| 159 | +$ ${process.argv.join(' ')} --headful`); |
| 160 | + return 1; |
| 161 | + } |
| 162 | + return 0; |
| 163 | + } catch (error) { |
| 164 | + console.error('Test failed:', error); |
| 165 | + return 1; |
| 166 | + } finally { |
| 167 | + await browser.close(); |
| 168 | + server.close(); |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +process.exit(await main()); |
0 commit comments