[web] E2E: Playwright flows recorded and the Aula shots, on the demo control plane at a fixed instant
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
|
||||
/** The suite's instant: the demo's answers and the page's Date both read it (a Monday morning in Prague). */
|
||||
export const CLOCK = "2026-11-16T09:30:00+01:00";
|
||||
@@ -0,0 +1,64 @@
|
||||
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
|
||||
// What every e2e test gets: the demo started over, the page's clock at the suite's instant, `open`
|
||||
// for a page of the UI, and afterwards a check that nothing the page showed looks real
|
||||
// (tests/demo.ts realLooking: names outside .test, addresses outside the documentation ranges).
|
||||
// A test with a `video` annotation has its recording kept as target/e2e/videos/<name>.webm.
|
||||
|
||||
import { mkdirSync } from "node:fs";
|
||||
import { join, resolve } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { test as base, expect, type Page } from "@playwright/test";
|
||||
import { realLooking } from "../tests/demo";
|
||||
import { CLOCK } from "./clock";
|
||||
|
||||
export const REPO = resolve(fileURLToPath(new URL(".", import.meta.url)), "../../..");
|
||||
export const TARGET = join(REPO, "target", "e2e");
|
||||
|
||||
function url(): string {
|
||||
const u = process.env.IANITOR_E2E_URL;
|
||||
if (!u) throw new Error("IANITOR_E2E_URL is not set: run the suite through playwright.config.ts (its global setup serves the demo)");
|
||||
return u;
|
||||
}
|
||||
|
||||
/** Every text, field value and link the page shows (the body: the head's links are the build's own files). */
|
||||
async function shown(page: Page): Promise<string[]> {
|
||||
return page.evaluate(() => {
|
||||
const out: string[] = [];
|
||||
const walk = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT);
|
||||
for (let n = walk.nextNode(); n; n = walk.nextNode()) if (n.textContent?.trim()) out.push(n.textContent);
|
||||
document.body.querySelectorAll<HTMLInputElement | HTMLTextAreaElement>("input, textarea").forEach((i) => out.push(i.value));
|
||||
document.body.querySelectorAll("[href]").forEach((a) => out.push(a.getAttribute("href") ?? ""));
|
||||
return out;
|
||||
});
|
||||
}
|
||||
|
||||
export const test = base.extend<{ open: (path: string) => Promise<void> }>({
|
||||
open: async ({ page, request }, use) => {
|
||||
const res = await request.post(`${url()}/__demo/reset`);
|
||||
expect(res.status()).toBe(204);
|
||||
await page.clock.setFixedTime(new Date(CLOCK));
|
||||
await use(async (path: string) => {
|
||||
await page.goto(url() + path);
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
test.afterEach(async ({ page }, info) => {
|
||||
const video = info.annotations.find((a) => a.type === "video")?.description;
|
||||
if (!page.isClosed()) {
|
||||
const seen = (await shown(page)).flatMap(realLooking);
|
||||
expect(seen, "the page showed something that looks real").toEqual([]);
|
||||
}
|
||||
const rec = page.video();
|
||||
if (video && rec && info.status === info.expectedStatus) {
|
||||
await page.close();
|
||||
const dir = join(TARGET, "videos");
|
||||
mkdirSync(dir, { recursive: true });
|
||||
await rec.saveAs(join(dir, `${video}.webm`));
|
||||
}
|
||||
});
|
||||
|
||||
export { expect };
|
||||
@@ -0,0 +1,113 @@
|
||||
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
|
||||
// The e2e suite's server (Playwright's global setup): builds the web UI (Vite, production) into a
|
||||
// fresh temporary folder and serves it on 127.0.0.1 at a free port, with /api/v1 answered by the
|
||||
// demo control plane (tests/demo.ts) at the suite's fixed instant. Any other path falls back to
|
||||
// index.html, as in production. POST /__demo/reset starts the demo over (each test starts from the
|
||||
// same invented server). The address reaches the tests as IANITOR_E2E_URL. Nothing here listens on
|
||||
// anything but loopback, and the temporary folder is removed when the run ends.
|
||||
|
||||
import { existsSync, mkdtempSync, readFileSync, rmSync, statSync } from "node:fs";
|
||||
import { createServer, type IncomingMessage, type Server } from "node:http";
|
||||
import { tmpdir } from "node:os";
|
||||
import { extname, join, normalize, resolve, sep } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { build } from "vite";
|
||||
import { demoApi } from "../tests/demo";
|
||||
import { clock } from "../tests/mockApi";
|
||||
import { CLOCK } from "./clock";
|
||||
|
||||
const web = resolve(fileURLToPath(new URL(".", import.meta.url)), "..");
|
||||
|
||||
const TYPES: Record<string, string> = {
|
||||
".html": "text/html; charset=utf-8",
|
||||
".js": "text/javascript; charset=utf-8",
|
||||
".css": "text/css; charset=utf-8",
|
||||
".json": "application/json",
|
||||
".svg": "image/svg+xml",
|
||||
".png": "image/png",
|
||||
".ico": "image/x-icon",
|
||||
".woff": "font/woff",
|
||||
".woff2": "font/woff2",
|
||||
".txt": "text/plain; charset=utf-8",
|
||||
};
|
||||
|
||||
function body(req: IncomingMessage): Promise<Record<string, unknown>> {
|
||||
return new Promise((ok) => {
|
||||
const chunks: Buffer[] = [];
|
||||
req.on("data", (c: Buffer) => chunks.push(c));
|
||||
req.on("end", () => {
|
||||
try {
|
||||
ok(chunks.length ? (JSON.parse(Buffer.concat(chunks).toString("utf8")) as Record<string, unknown>) : {});
|
||||
} catch {
|
||||
ok({});
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function serve(dist: string): Promise<Server> {
|
||||
const index = readFileSync(join(dist, "index.html"));
|
||||
let api = demoApi();
|
||||
const server = createServer((req, res) => {
|
||||
const url = new URL(req.url ?? "/", "http://127.0.0.1");
|
||||
if (url.pathname === "/__demo/reset" && req.method === "POST") {
|
||||
api = demoApi();
|
||||
res.writeHead(204).end();
|
||||
return;
|
||||
}
|
||||
if (url.pathname === "/api/v1" || url.pathname.startsWith("/api/v1/")) {
|
||||
void body(req).then((b) => {
|
||||
const a = api(req.method ?? "GET", url.pathname.slice("/api/v1".length) + url.search, b);
|
||||
res.writeHead(a.status, { "Content-Type": "application/json", "Cache-Control": "no-store" });
|
||||
res.end(a.text ?? undefined);
|
||||
});
|
||||
return;
|
||||
}
|
||||
let path: string;
|
||||
try {
|
||||
path = decodeURIComponent(url.pathname);
|
||||
} catch {
|
||||
res.writeHead(400).end();
|
||||
return;
|
||||
}
|
||||
const file = normalize(join(dist, path));
|
||||
if (file.startsWith(dist + sep) && existsSync(file) && statSync(file).isFile()) {
|
||||
res.writeHead(200, { "Content-Type": TYPES[extname(file)] ?? "application/octet-stream" });
|
||||
res.end(readFileSync(file));
|
||||
return;
|
||||
}
|
||||
res.writeHead(200, { "Content-Type": TYPES[".html"], "Cache-Control": "no-store" });
|
||||
res.end(index);
|
||||
});
|
||||
return new Promise((ok, fail) => {
|
||||
server.once("error", fail);
|
||||
server.listen(0, "127.0.0.1", () => ok(server));
|
||||
});
|
||||
}
|
||||
|
||||
export default async function setup(): Promise<() => Promise<void>> {
|
||||
clock.fixed = Date.parse(CLOCK);
|
||||
const tmp = mkdtempSync(join(tmpdir(), "ianitor-e2e-"));
|
||||
const remove = () => rmSync(tmp, { recursive: true, force: true, maxRetries: 10, retryDelay: 200 });
|
||||
let server: Server;
|
||||
try {
|
||||
const dist = join(tmp, "dist");
|
||||
await build({ root: web, logLevel: "warn", build: { outDir: dist, emptyOutDir: true } });
|
||||
server = await serve(dist);
|
||||
} catch (e) {
|
||||
remove();
|
||||
throw e;
|
||||
}
|
||||
const addr = server.address();
|
||||
if (!addr || typeof addr === "string") throw new Error("the demo server has no port");
|
||||
process.env.IANITOR_E2E_URL = `http://127.0.0.1:${addr.port}`;
|
||||
console.log(`e2e: the built UI and the demo control plane on ${process.env.IANITOR_E2E_URL}`);
|
||||
return async () => {
|
||||
server.closeAllConnections();
|
||||
await new Promise<void>((r) => server.close(() => r()));
|
||||
remove();
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
|
||||
// The release's screenshots (docs/release.md): each shot of tests/demo.ts SHOTS, 1440 × 900 at
|
||||
// scale 1, saved as target/e2e/aula/<name>.png. .fabrica/aula.yaml lists the same files, which
|
||||
// `fabrica aula shots` checks and publishes as aula-<name>.png with aula.json.
|
||||
|
||||
import { mkdirSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import { SHOTS } from "../../tests/demo";
|
||||
import { TARGET, expect, test } from "../fixtures";
|
||||
|
||||
const dir = join(TARGET, "aula");
|
||||
|
||||
for (const shot of SHOTS) {
|
||||
test(`aula: ${shot.name}`, async ({ page, open }) => {
|
||||
await open(shot.path);
|
||||
for (const sel of shot.click ?? []) await page.locator(sel).first().click();
|
||||
await expect(page.locator(shot.wait).first()).toBeAttached();
|
||||
// Nothing still loading, the fonts in, two frames drawn and transitions over.
|
||||
await expect(page.locator("[aria-busy=true]")).toHaveCount(0);
|
||||
await page.evaluate(() => document.fonts.ready.then(() => new Promise((r) => requestAnimationFrame(() => requestAnimationFrame(r)))));
|
||||
await page.waitForTimeout(400);
|
||||
mkdirSync(dir, { recursive: true });
|
||||
await page.screenshot({ path: join(dir, `${shot.name}.png`), animations: "disabled", caret: "hide" });
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
|
||||
// The main flows of ROADMAP §4 in a real browser, on the built UI and the demo control plane, with
|
||||
// the clicks counted as in the smoke test (tests/flows.test.ts). Each is recorded: the showcase
|
||||
// videos (docs/fabrica.md part 5) are target/e2e/videos/<name>.webm.
|
||||
|
||||
import type { Locator, Page } from "@playwright/test";
|
||||
import { expect, test } from "../fixtures";
|
||||
|
||||
function counter() {
|
||||
let n = 0;
|
||||
return {
|
||||
click: async (l: Locator) => {
|
||||
n++;
|
||||
await l.click();
|
||||
},
|
||||
get count() {
|
||||
return n;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/** A beat for the viewer of the video, so a screen is seen before the next click. */
|
||||
const beat = (page: Page) => page.waitForTimeout(700);
|
||||
const nav = (page: Page) => page.getByRole("navigation", { name: "Main" }).first();
|
||||
|
||||
test("Add a host: + Host → fill → Apply = 2 clicks, kept", { annotation: { type: "video", description: "add-host" } }, async ({ page, open }) => {
|
||||
const c = counter();
|
||||
await open("/");
|
||||
await expect(page.getByRole("button", { name: "More for wiki.home.test" })).toBeVisible();
|
||||
await beat(page);
|
||||
await c.click(page.getByRole("button", { name: "Add a host" }));
|
||||
await page.getByLabel("Hostname").pressSequentially("notes.home.test");
|
||||
await page.getByLabel("Send traffic to").fill("http://192.0.2.60:8080");
|
||||
await beat(page);
|
||||
await c.click(page.getByRole("button", { name: "Apply" }));
|
||||
await expect(page.locator("dialog.ia-panel")).toHaveCount(0, { timeout: 10_000 });
|
||||
await expect(page.getByRole("button", { name: "More for notes.home.test" })).toBeVisible();
|
||||
await beat(page);
|
||||
expect(c.count).toBe(2);
|
||||
});
|
||||
|
||||
test("Undo the last change = 1 click", { annotation: { type: "video", description: "undo" } }, async ({ page, open }) => {
|
||||
const c = counter();
|
||||
await open("/");
|
||||
const undo = page.getByRole("button", { name: /^Undo: Added recipes\.home\.test/ });
|
||||
await expect(undo).toBeVisible();
|
||||
await beat(page);
|
||||
const sent = page.waitForRequest((r) => r.method() === "POST" && r.url().endsWith("/api/v1/undo"));
|
||||
await c.click(undo);
|
||||
expect((await sent).postDataJSON()).toMatchObject({ target_change_id: "ch_0008" });
|
||||
await beat(page);
|
||||
expect(c.count).toBe(1);
|
||||
});
|
||||
|
||||
test("Restore: History → pick → Restore = 3 clicks", { annotation: { type: "video", description: "restore" } }, async ({ page, open }) => {
|
||||
const c = counter();
|
||||
await open("/");
|
||||
await expect(page.getByRole("button", { name: "Add a host" })).toBeVisible();
|
||||
await c.click(nav(page).getByRole("link", { name: "History" }));
|
||||
await beat(page);
|
||||
await c.click(page.getByRole("button", { name: /Turned share links on for wiki\.home\.test/ }));
|
||||
await expect(page.getByRole("button", { name: "Restore this version" })).toBeVisible();
|
||||
await beat(page);
|
||||
const sent = page.waitForRequest((r) => r.method() === "POST" && r.url().endsWith("/api/v1/restore"));
|
||||
await c.click(page.getByRole("button", { name: "Restore this version" }));
|
||||
await sent;
|
||||
await beat(page);
|
||||
expect(c.count).toBe(3);
|
||||
});
|
||||
|
||||
test("Share link: ⋯ → Share link → Copy = 3 clicks, 7 days", { annotation: { type: "video", description: "share-link" } }, async ({ page, context, open }) => {
|
||||
await context.grantPermissions(["clipboard-read", "clipboard-write"]);
|
||||
const c = counter();
|
||||
await open("/");
|
||||
await c.click(page.getByRole("button", { name: "More for wiki.home.test" }));
|
||||
const minted = page.waitForRequest((r) => r.method() === "POST" && r.url().endsWith("/share-links"));
|
||||
await c.click(page.getByRole("menuitem", { name: /Share link/ }));
|
||||
expect((await minted).postDataJSON()).toMatchObject({ expires_in_s: 604800 });
|
||||
const copy = page.getByRole("button", { name: "Copy link" });
|
||||
await expect(copy).not.toHaveAttribute("aria-disabled", /.*/);
|
||||
await beat(page);
|
||||
await c.click(copy);
|
||||
await expect(page.getByText("Copied")).toBeVisible();
|
||||
await beat(page);
|
||||
expect(c.count).toBe(3);
|
||||
});
|
||||
|
||||
test("Switch engine: engine chip → Switch to Caddy = 2 clicks", { annotation: { type: "video", description: "engine" } }, async ({ page, open }) => {
|
||||
const c = counter();
|
||||
await open("/");
|
||||
await c.click(page.getByRole("button", { name: /^Engine: Pingora/ }));
|
||||
await expect(page.getByRole("button", { name: "Switch to Caddy" })).toBeVisible();
|
||||
await beat(page);
|
||||
await c.click(page.getByRole("button", { name: "Switch to Caddy" }));
|
||||
await expect(page.getByText("Now serving with Caddy.")).toBeVisible({ timeout: 10_000 });
|
||||
await beat(page);
|
||||
expect(c.count).toBe(2);
|
||||
});
|
||||
|
||||
test("Device certificate: Certificates → + Device → Issue = 3 clicks", { annotation: { type: "video", description: "device-certificate" } }, async ({ page, open }) => {
|
||||
const c = counter();
|
||||
await open("/");
|
||||
await expect(page.getByRole("button", { name: "Add a host" })).toBeVisible();
|
||||
await c.click(nav(page).getByRole("link", { name: "Certificates" }));
|
||||
await beat(page);
|
||||
await c.click(page.getByRole("button", { name: "Add a device certificate" }));
|
||||
await page.getByLabel("Device name").fill("felix's tablet");
|
||||
await c.click(page.getByRole("button", { name: "Issue" }));
|
||||
await expect(page.getByLabel("Match code 47 19 83")).toBeVisible();
|
||||
expect(c.count).toBe(3);
|
||||
await expect(page.getByRole("button", { name: "Show QR code" })).toBeVisible({ timeout: 10_000 });
|
||||
await beat(page);
|
||||
});
|
||||
Reference in New Issue
Block a user