Files

65 lines
2.7 KiB
TypeScript

// SPDX-License-Identifier: CPAL-1.0
// Copyright (c) 2026 Marek Novák. Part of Ianitor, under the Common Public Attribution
// License 1.0: see LICENSE (its Exhibit A is this file's notice).
// 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 };