Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added browser tests using wasmtime test artifacts #42

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ module.exports = {
extends: ["eslint:recommended", "plugin:@typescript-eslint/recommended"],
parser: "@typescript-eslint/parser",
plugins: ["@typescript-eslint"],
env: {
browser: true,
node: true,
},
rules: {
"@typescript-eslint/no-this-alias": "off",
},
Expand Down
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
node_modules/
dist/
typings/
/test-results/
/playwright-report/
/playwright/.cache/

wasmtime/
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: missing trailing newline

16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,22 @@ $ npm install
$ npm run build
```

## Running tests

This project uses playwright to run tests in a browser using WebAssembly built from the Wasmtime project.

To clone wasmtime and build the test WebAssembly, run:

```
$ npm test:build
```

Once the WebAssembly is built, you can run the tests with:

```
$ npm test
```

## Running the demo

The demo requires the wasm rustc artifacts and the xterm js package. To get them run:
Expand Down
8 changes: 8 additions & 0 deletions e2e/harness.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<html>
<head>
<title>Test Harness</title>
</head>
<h1>Status: <span id=status data-testid=status>Not started</span></h1>
<h2>Check console for details</h2>
<script src="harness.js" type="module"></script>
</html>
71 changes: 71 additions & 0 deletions e2e/harness.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { WASI, File, PreopenDirectory } from "../dist/index.js";

const stdio = {
stdin: undefined,
stdout: new File(),
stderr: new File(),
};

const WASI_ENVS = {
DEFAULT: [
stdio.stdin,
stdio.stdout,
stdio.stderr,
new PreopenDirectory("/", {}),
],
};

async function main(options) {
const wasmReq = await fetch(options.file);

const args = [options.file];
args.push(...(options.args || []));

const wasi = new WASI(
args,
(options.env || "").split(","),
WASI_ENVS[options.wasi_env] || WASI_ENVS.DEFAULT,
{
debug: true,
},
);
const module = await WebAssembly.compileStreaming(wasmReq);

const instance = await WebAssembly.instantiate(module, {
wasi_snapshot_preview1: wasi.wasiImport,
});

const status = document.getElementById("status");
try {
if (options.reactor) {
wasi.initialize(instance);
}
if (options.command) {
wasi.start(instance);
}
printBuffer(stdio, "stdout", "log");
printBuffer(stdio, "stderr", "log");
status.innerText = "success";
} catch (e) {
printBuffer(stdio, "stdout", "log");
printBuffer(stdio, "stderr", "error");
status.innerText = "failure";
throw e;
}
}

function printBuffer(stdio, type, logger) {
const output = new TextDecoder().decode(stdio[type].data);
if (output.trim().length > 0) {
console[logger](`${type}`, output);
}
}

main(JSON.parse(decodeURI(location.search.slice(1))))
.then(() => {
console.log("done");
})
.catch((e) => {
console.error(e);
throw e;
});
24 changes: 24 additions & 0 deletions e2e/wasmtime.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { test, expect } from "@playwright/test";

const URL = "http://127.0.0.1:3000/e2e/harness.html";
const WASMDIR = "../wasmtime/artifacts";

const tests = [
{
file: `${WASMDIR}/preview1_path_open_read_write.wasm`,
command: true,
args: ["/"],
},
];

for (const testDef of tests) {
test(`first ${testDef.file}`, async ({ page }) => {
await page.goto(`${URL}?${JSON.stringify(testDef)}`);
await page.waitForFunction(
() => document.getElementById("status")?.textContent !== "Not started",
);
expect(await await page.getByTestId("status").textContent()).toBe(
"success",
);
});
}
Loading