Skip to content

Commit

Permalink
added browser tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jsoverson committed Oct 10, 2023
1 parent f4d2382 commit b719a54
Show file tree
Hide file tree
Showing 16 changed files with 998 additions and 47 deletions.
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/
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

0 comments on commit b719a54

Please sign in to comment.