Files
ianitor/apps/web/tests/demo.test.ts
T

81 lines
3.5 KiB
TypeScript

// 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): every shot's screen is drawn from the demo control
// plane alone (each click and wait selector found, no call unanswered), and nothing the demo
// serves or the page shows names a real host, address or person.
import { cleanup, render, waitFor } from "@testing-library/svelte";
import { afterEach, describe, expect, it } from "vitest";
import App from "../src/App.svelte";
import { app } from "../src/lib/app.svelte";
import { SHOTS, demoApi, realLooking } from "./demo";
let served: { status: number; path: string; text: string | null }[] = [];
function start(path: string) {
history.replaceState({}, "", path);
app.path = path;
app.panel = null;
app.overview = null;
app.etag = null;
app.signedOut = false;
app.switchResult = null;
app.renewedUntil = null;
app.dismissed = [];
served = [];
const api = demoApi();
globalThis.fetch = (async (input: RequestInfo | URL, init?: RequestInit) => {
const url = typeof input === "string" ? input : input instanceof URL ? input.pathname + input.search : input.url;
const path = url.replace(/^.*\/api\/v1/, "");
const a = api(init?.method ?? "GET", path, init?.body ? JSON.parse(String(init.body)) : {});
served.push({ status: a.status, path, text: a.text });
return new Response(a.text, { status: a.status, headers: { "Content-Type": "application/json" } });
}) as typeof fetch;
render(App);
}
function pageText(): string[] {
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.querySelectorAll<HTMLInputElement | HTMLTextAreaElement>("input, textarea").forEach((i) => out.push(i.value));
document.querySelectorAll("[href]").forEach((a) => out.push(a.getAttribute("href") ?? ""));
return out;
}
afterEach(() => cleanup());
describe("realLooking", () => {
it("passes invented names and refuses real-looking ones", () => {
expect(realLooking("wiki.home.test http://192.0.2.10:3000 on 127.0.0.1 for ada")).toEqual([]);
expect(realLooking("http://10.0.0.2:3000")).toEqual(["10.0.0.2"]);
expect(realLooking("gitea.example.cz and Mark's phone")).toEqual(["gitea.example.cz", "mark"]);
expect(realLooking("flowers.stage")).toEqual(["flowers.stage", "flowers"]);
});
});
describe("the release's screenshots from the demo control plane", () => {
for (const shot of SHOTS) {
it(`${shot.name}: drawn from invented data only`, async () => {
start(shot.path);
for (const sel of shot.click ?? []) {
const el = await waitFor(() => {
const e = document.querySelector<HTMLElement>(sel);
expect(e, `click ${sel}`).not.toBeNull();
return e as HTMLElement;
}, { timeout: 5000 });
el.click();
}
await waitFor(() => expect(document.querySelector(shot.wait), `wait ${shot.wait}`).not.toBeNull(), { timeout: 5000 });
expect(served.filter((s) => s.status === 404).map((s) => s.path)).toEqual([]);
// Control's codes (apply.kept, pki.renew_due) are dotted words, not names.
const json = served.map((s) => (s.text ?? "").replace(/"(?:code|message_code|error_code)":"[^"]*"/g, ""));
const seen = [...json, ...pageText()];
expect(seen.flatMap(realLooking)).toEqual([]);
});
}
});