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

246 lines
11 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).
// The smoke test of every ROADMAP §4 flow with its clicks counted (handoff §4, §15): the real
// app against a stand-in for control's API. Typing, focus and waiting are not clicks.
import { cleanup, render, screen, waitFor, within } from "@testing-library/svelte";
import userEvent from "@testing-library/user-event";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import App from "../src/App.svelte";
import { app } from "../src/lib/app.svelte";
import { installMock, type Mock } from "./mockApi";
let mock: Mock;
let clicks = 0;
let user: ReturnType<typeof userEvent.setup>;
async function click(el: Element) {
clicks++;
await user.click(el);
}
function start(path = "/") {
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 = [];
mock = installMock();
clicks = 0;
user = userEvent.setup();
render(App);
}
const called = (method: string, path: string) => mock.calls.some((c) => c.method === method && c.path.startsWith(path));
const T = { timeout: 5000 };
beforeEach(() => {
try {
sessionStorage.clear();
} catch {
/* none */
}
});
afterEach(() => cleanup());
describe("ROADMAP §4 flows, clicks counted", () => {
it("Add host: + Host → fill → Apply = 2 clicks, auto-kept", async () => {
start();
await click(await screen.findByRole("button", { name: "Add a host" }));
await user.type(await screen.findByLabelText("Hostname"), "recipes.home.test");
await user.type(screen.getByLabelText("Send traffic to"), "http://10.0.0.5:8080");
await click(screen.getByRole("button", { name: "Apply" }));
// Kept: the panel closes by itself and the new host is in the list.
await waitFor(() => expect(called("GET", "/applies/ch_new")).toBe(true), T);
await waitFor(() => expect(document.querySelector("dialog.ia-panel")).toBeNull(), T);
await screen.findByRole("button", { name: "More for recipes.home.test" }, T);
expect(called("POST", "/hosts")).toBe(true);
const sent = mock.calls.find((c) => c.method === "POST" && c.path === "/hosts")?.body as { host: Record<string, unknown> };
// Every field is sent explicitly, with the design's defaults.
expect(sent.host).toMatchObject({ hostname: "recipes.home.test", tls: "domi", auth: "forward-auth", websockets: true });
expect(clicks).toBe(2);
});
it("Undo last change = 1 click", async () => {
start();
await click(await screen.findByRole("button", { name: /^Undo: Added recipes\.home\.test/ }));
await waitFor(() => expect(called("POST", "/undo")).toBe(true));
const body = mock.calls.find((c) => c.path === "/undo" && c.method === "POST")?.body as Record<string, string>;
expect(body.target_change_id).toBe("ch_1");
expect(clicks).toBe(1);
});
it("Restore: History → pick → Restore = 3 clicks", async () => {
start();
await screen.findByRole("button", { name: "Add a host" });
const nav = screen.getAllByRole("navigation", { name: "Main" })[0];
await click(within(nav).getByRole("link", { name: "History" }));
await click(await screen.findByRole("button", { name: /Changed wiki\.home\.test/ }));
await screen.findByText(/sends traffic to changes from/);
await click(await screen.findByRole("button", { name: "Restore this version" }));
await waitFor(() => expect(called("POST", "/restore")).toBe(true));
expect(clicks).toBe(3);
});
it("Share link: ⋯ → Share link → Copy = 3 clicks (default expiry 7 days)", async () => {
start();
await click(await screen.findByRole("button", { name: "More for wiki.home.test" }));
await click(await screen.findByRole("menuitem", { name: /Share link/ }));
const copy = await screen.findByRole("button", { name: "Copy link" });
await waitFor(() => expect(copy.getAttribute("aria-disabled")).toBeNull());
await click(copy);
await screen.findByText("Copied");
const mint = mock.calls.find((c) => c.method === "POST" && c.path.endsWith("/share-links"))?.body as Record<string, unknown>;
expect(mint.expires_in_s).toBe(604800);
expect(await navigator.clipboard.readText()).toContain("?share=ish1.");
expect(clicks).toBe(3);
});
it("Share link on a host with links off: 4 clicks (Mark, Q1: the accepted 4th)", async () => {
start();
await click(await screen.findByRole("button", { name: "More for photos.home.test" }));
await click(await screen.findByRole("menuitem", { name: /Share link/ }));
await click(await screen.findByRole("button", { name: "Turn on share links" }));
const copy = await screen.findByRole("button", { name: "Copy link" }, T);
await waitFor(() => expect(copy.getAttribute("aria-disabled")).toBeNull());
await click(copy);
await screen.findByText("Copied");
expect(called("PATCH", "/hosts/photos.home.test")).toBe(true);
expect(clicks).toBe(4);
});
it("Switch engine: engine chip → Switch to Caddy = 2 clicks", async () => {
start();
await click(await screen.findByRole("button", { name: /^Engine: Pingora/ }));
await click(await screen.findByRole("button", { name: "Switch to Caddy" }));
await waitFor(() => expect(called("POST", "/engine")).toBe(true));
const body = mock.calls.find((c) => c.method === "POST" && c.path === "/engine")?.body as Record<string, string>;
expect(body.target).toBe("caddy");
await screen.findByText("Now serving with Caddy.", {}, T);
expect(clicks).toBe(2);
});
it("Device certificate: Certificates → + Device → Issue = 3 clicks; install is the accepted 4th", async () => {
start();
await screen.findByRole("button", { name: "Add a host" });
const nav = screen.getAllByRole("navigation", { name: "Main" })[0];
await click(within(nav).getByRole("link", { name: "Certificates" }));
await click(await screen.findByRole("button", { name: "Add a device certificate" }));
await user.type(await screen.findByLabelText("Device name"), "Mark's phone");
await click(screen.getByRole("button", { name: "Issue" }));
await screen.findByLabelText("Match code 47 19 83");
expect(clicks).toBe(3);
// Approved on the server (the stand-in approves on the first poll).
await click(await screen.findByRole("button", { name: "Show QR code" }, T));
await screen.findByText("ABCD-EFGH-JKLM-NPQR");
expect(clicks).toBe(4);
});
it("Renew intermediate: banner Renew = 1 click", async () => {
start();
await click(await screen.findByRole("button", { name: "Renew" }));
await waitFor(() => expect(called("POST", "/certs/intermediate/renewal")).toBe(true));
// The renewal is waiting: the banner says where to sign it (no code to compare with openssl).
await screen.findByRole("button", { name: "Sign it by hand" });
expect(clicks).toBe(1);
});
it("API token: ⚙ → API → New token = 3 clicks; Copy token is the accepted 4th", async () => {
start();
await screen.findByRole("button", { name: "Add a host" });
const nav = screen.getAllByRole("navigation", { name: "Main" })[0];
await click(within(nav).getByRole("link", { name: "Settings" }));
await click(await screen.findByRole("tab", { name: "API" }));
await click(await screen.findByRole("button", { name: "New token" }));
const box = (await screen.findByLabelText("New API token")) as HTMLInputElement;
expect(box.value).toBe("ian_tk1_secretvalue");
expect(clicks).toBe(3);
await click(screen.getByRole("button", { name: "Copy token" }));
expect(await navigator.clipboard.readText()).toBe("ian_tk1_secretvalue");
expect(clicks).toBe(4);
// The token is never stored by the page.
expect(JSON.stringify({ ...sessionStorage })).not.toContain("ian_tk1");
expect(JSON.stringify({ ...localStorage })).not.toContain("ian_tk1");
});
});
describe("focus", () => {
it("+ Host puts the cursor in Hostname, so typing right after the click lands there", async () => {
start();
await click(await screen.findByRole("button", { name: "Add a host" }));
await user.keyboard("recipes.home.test");
expect((await screen.findByLabelText("Hostname") as HTMLInputElement).value).toBe("recipes.home.test");
});
it("the share dialog opens on Copy link and gives focus back to the row's More button", async () => {
start();
const more = await screen.findByRole("button", { name: "More for wiki.home.test" });
await click(more);
await click(await screen.findByRole("menuitem", { name: /Share link/ }));
const copy = await screen.findByRole("button", { name: "Copy link" });
await waitFor(() => expect(document.activeElement).toBe(copy));
// jsdom fires no cancel event on Esc for a modal dialog: close it with its button.
await click(screen.getByRole("button", { name: "Close" }));
await waitFor(() => expect(screen.queryByRole("button", { name: "Copy link" })).toBeNull());
await waitFor(() => expect(document.activeElement).toBe(more));
});
it("the engine popover closes with Esc and focus goes back to the chip", async () => {
start();
const chip = await screen.findByRole("button", { name: /^Engine: Pingora/ });
await click(chip);
await screen.findByRole("button", { name: "Switch to Caddy" });
await user.keyboard("{Escape}");
await waitFor(() => expect(screen.queryByRole("button", { name: "Switch to Caddy" })).toBeNull());
expect(document.activeElement).toBe(screen.getByRole("button", { name: /^Engine: Pingora/ }));
});
it("a click on the engine chip while its popover is open closes it", async () => {
start();
const chip = await screen.findByRole("button", { name: /^Engine: Pingora/ });
await click(chip);
await screen.findByRole("button", { name: "Switch to Caddy" });
await new Promise((r) => setTimeout(r, 10));
await click(chip);
await waitFor(() => expect(screen.queryByRole("button", { name: "Switch to Caddy" })).toBeNull());
});
});
describe("sign-in and contact", () => {
it("a 401 without Ianitor's envelope shows the signed-out dialog", async () => {
start();
await screen.findByRole("button", { name: "Add a host" });
globalThis.fetch = (async () => new Response("", { status: 401 })) as typeof fetch;
app.etag = null;
await click(screen.getByRole("button", { name: "Add a host" }));
await user.type(await screen.findByLabelText("Hostname"), "x.home.test");
await user.type(screen.getByLabelText("Send traffic to"), "http://10.0.0.5:8080");
await click(screen.getByRole("button", { name: "Apply" }));
await screen.findByRole("alertdialog", { name: "Your sign-in has ended" });
// What was typed is still there.
expect((screen.getByLabelText("Hostname") as HTMLInputElement).value).toBe("x.home.test");
});
it("every request carries the UI header and no token", async () => {
const seen: Headers[] = [];
start();
const inner = globalThis.fetch;
globalThis.fetch = (async (i: RequestInfo | URL, init?: RequestInit) => {
seen.push(new Headers(init?.headers));
return inner(i, init);
}) as typeof fetch;
await click(await screen.findByRole("button", { name: /^Undo:/ }));
await waitFor(() => expect(seen.length).toBeGreaterThan(0));
for (const h of seen) {
expect(h.get("X-Ianitor-UI")).toBe("1");
expect(h.get("Authorization")).toBeNull();
}
});
});