Skip to content

Commit 65b4e9e

Browse files
committed
tests(web-host): all e2e tests with playwright
1 parent 1ed16f3 commit 65b4e9e

File tree

4 files changed

+196
-19
lines changed

4 files changed

+196
-19
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { expect, test } from "@playwright/test";
2+
import { fillAndSubmitCommand, getLastStd } from "./utils";
3+
4+
test("echo $0", async ({ page }) => {
5+
await page.goto("/#repl");
6+
await fillAndSubmitCommand(page, "echo foo", { expectStdout: "foo" });
7+
await fillAndSubmitCommand(page, "echo bar", { expectStdout: "bar" });
8+
await fillAndSubmitCommand(page, "echo $0", { expectStdout: "bar" });
9+
});
10+
11+
test("echo $ROOT/$USER", async ({ page }) => {
12+
await page.goto("/#repl");
13+
await fillAndSubmitCommand(page, "echo $ROOT/$USER", {
14+
expectStdout: "/Users/Tophe",
15+
});
16+
});
17+
18+
test("export USER=WebAssembly", async ({ page }) => {
19+
await page.goto("/#repl");
20+
await fillAndSubmitCommand(page, "export USER=WebAssembly");
21+
await fillAndSubmitCommand(page, "echo $ROOT/$USER", {
22+
expectStdout: "/Users/WebAssembly",
23+
});
24+
});
25+
26+
test("echo $?", async ({ page }) => {
27+
await page.goto("/#repl");
28+
await fillAndSubmitCommand(page, "echo $?", { expectStdout: "0" });
29+
await fillAndSubmitCommand(page, "azertyuiop", {
30+
expectStderr:
31+
"Unknown command: azertyuiop. Try `help` to see available commands.",
32+
});
33+
await fillAndSubmitCommand(page, "echo $?", { expectStdout: "1" });
34+
await fillAndSubmitCommand(page, "echo $?", { expectStdout: "0" });
35+
});
36+
37+
test("help", async ({ page }) => {
38+
await page.goto("/#repl");
39+
await fillAndSubmitCommand(page, "help");
40+
const stdout = await getLastStd(page, "stdout");
41+
await expect(stdout).toContainText("help - Show the manual for a command");
42+
});
43+
44+
test("man help -> should show the help", async ({ page }) => {
45+
await page.goto("/#repl");
46+
await fillAndSubmitCommand(page, "help");
47+
const stdout = await getLastStd(page, "stdout");
48+
await expect(stdout).toContainText("help - Show the manual for a command");
49+
});
50+
51+
test("man for reserved commands", async ({ page }) => {
52+
const reservedCommands = [
53+
["help", "help - Show the manual for a command"],
54+
["export", "export - Export a variable to the environment"],
55+
[
56+
"list-commands",
57+
"list-commands - List the plugins loaded in the host and the reserved commands (not overridable by plugins) included in the REPL logic.",
58+
],
59+
];
60+
await page.goto("/#repl");
61+
for (const [command, partialManpage] of reservedCommands) {
62+
await fillAndSubmitCommand(page, `man ${command}`);
63+
const stdout = await getLastStd(page, "stdout");
64+
await expect(stdout).toContainText(partialManpage);
65+
}
66+
});
Lines changed: 74 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,82 @@
1-
import { expect, type Page, test } from "@playwright/test";
1+
import { expect, test } from "@playwright/test";
2+
import { fillAndSubmitCommand, getLastStd } from "./utils";
23

3-
async function getLastStd(page: Page, type: "stdin" | "stdout" | "stderr") {
4-
return await page.locator(`[data-stdtype='${type}']`).last();
5-
}
4+
test("echo foo", async ({ page }) => {
5+
await page.goto("/#repl");
6+
await fillAndSubmitCommand(page, "echo foo", { expectStdout: "foo" });
7+
});
68

7-
test("echo foo - enter", async ({ page }) => {
9+
test("greet World", async ({ page }) => {
810
await page.goto("/#repl");
9-
const input = await page.getByPlaceholder("Type a command...");
10-
await input.fill("echo foo");
11-
await input.press("Enter");
12-
const stdin = await getLastStd(page, "stdin");
13-
await expect(stdin).toHaveText("echo foo");
14-
const stdout = await getLastStd(page, "stdout");
15-
await expect(stdout).toHaveText("foo");
11+
await fillAndSubmitCommand(page, "greet World", {
12+
expectStdout: "Hello, World!",
13+
});
14+
});
15+
16+
test("weather Paris", async ({ page }) => {
17+
await page.route("https://wttr.in/Paris?format=j1", (route) => {
18+
route.fulfill({
19+
status: 200,
20+
body: JSON.stringify({
21+
current_condition: [
22+
{
23+
weatherDesc: [
24+
{
25+
value: "Sunny",
26+
},
27+
],
28+
},
29+
],
30+
}),
31+
});
32+
});
33+
await page.goto("/#repl");
34+
await fillAndSubmitCommand(page, "weather Paris", {
35+
expectStdout: "Sunny",
36+
});
1637
});
1738

18-
test("echo foo - run", async ({ page }) => {
39+
test("ls", async ({ page }) => {
1940
await page.goto("/#repl");
20-
const input = await page.getByPlaceholder("Type a command...");
21-
await input.fill("echo foo");
22-
await page.getByRole("button", { name: "Run", exact: true }).click();
23-
const stdin = await getLastStd(page, "stdin");
24-
await expect(stdin).toHaveText("echo foo");
41+
await fillAndSubmitCommand(page, "ls", {
42+
expectStdout: `D data
43+
D documents
44+
D logs
45+
F .config
46+
F .hidden_file
47+
F README.md`,
48+
});
49+
});
50+
51+
test("ls data", async ({ page }) => {
52+
await page.goto("/#repl");
53+
await fillAndSubmitCommand(page, "ls data", {
54+
expectStdout: `D data/processed
55+
D data/raw
56+
F data/sample.csv
57+
F data/users.yaml`,
58+
});
59+
});
60+
61+
test("ls data/users.yaml - should handle files", async ({ page }) => {
62+
await page.goto("/#repl");
63+
await fillAndSubmitCommand(page, "ls data/users.yaml", {
64+
expectStdout: "F data/users.yaml",
65+
});
66+
});
67+
68+
test("cat data", async ({ page }) => {
69+
await page.goto("/#repl");
70+
await fillAndSubmitCommand(page, "cat data", {
71+
expectStderr: "cat: data: Is a directory",
72+
});
73+
});
74+
75+
test("cat README.md", async ({ page }) => {
76+
await page.goto("/#repl");
77+
await fillAndSubmitCommand(page, "cat README.md");
2578
const stdout = await getLastStd(page, "stdout");
26-
await expect(stdout).toHaveText("foo");
79+
await expect(stdout).toContainText(`# filesystem
80+
81+
You are interacting with a virtual filesystem, in your browser!`);
2782
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { expect, test } from "@playwright/test";
2+
import { fillAndSubmitCommand, getLastStd } from "./utils";
3+
4+
test("echo foo - enter", async ({ page }) => {
5+
await page.goto("/#repl");
6+
await fillAndSubmitCommand(page, "echo foo", { expectStdout: "foo" });
7+
});
8+
9+
test("echo foo - run", async ({ page }) => {
10+
await page.goto("/#repl");
11+
const input = await page.getByPlaceholder("Type a command...");
12+
await input.fill("echo foo");
13+
await page.getByRole("button", { name: "Run", exact: true }).click();
14+
const stdin = await getLastStd(page, "stdin");
15+
await expect(stdin).toHaveText("echo foo");
16+
const stdout = await getLastStd(page, "stdout");
17+
await expect(stdout).toHaveText("foo");
18+
});
19+
20+
// todo: test wand button

packages/web-host/tests/utils.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { expect, type Page } from "@playwright/test";
2+
3+
export async function getLastStd(
4+
page: Page,
5+
type: "stdin" | "stdout" | "stderr",
6+
) {
7+
return await page.locator(`[data-stdtype='${type}']`).last();
8+
}
9+
10+
export async function fillAndSubmitCommand(
11+
page: Page,
12+
command: string,
13+
{
14+
expectStdin = command,
15+
expectStdout,
16+
expectStderr,
17+
}: {
18+
expectStdin?: string;
19+
expectStdout?: string;
20+
expectStderr?: string;
21+
} = {},
22+
) {
23+
const input = await page.getByPlaceholder("Type a command...");
24+
await input.fill(command);
25+
await input.press("Enter");
26+
const stdin = await getLastStd(page, "stdin");
27+
await expect(stdin).toHaveText(expectStdin);
28+
if (expectStdout) {
29+
const stdout = await getLastStd(page, "stdout");
30+
await expect(stdout).toHaveText(expectStdout);
31+
}
32+
if (expectStderr) {
33+
const stderr = await getLastStd(page, "stderr");
34+
await expect(stderr).toHaveText(expectStderr);
35+
}
36+
}

0 commit comments

Comments
 (0)