import assert from "node:assert/strict"; import { existsSync, mkdtempSync, rmSync } from "node:fs"; import { resolve, delimiter } from "node:path"; import { tmpdir } from "node:os"; import { pathToFileURL } from "node:url"; // Start lakebed dev, then run via npm exec --package=@playwright/test -- node scripts/test-browser.mjs const root = process.env.PATH.split(delimiter).map((p) => resolve(p, "../playwright")) .find((p) => existsSync(resolve(p, "index.mjs"))); assert.ok(root, "Run via npm exec --package=@playwright/test"); const { chromium } = await import(pathToFileURL(resolve(root, "index.mjs")).href); const browser = await chromium.launch({ headless: true, args: ["--no-sandbox"] }); const profileDir = mkdtempSync(resolve(tmpdir(), "lunchtime-browser-")); let participantContext; const errors = []; async function rpc(page, name, args = [], query = false) { return page.evaluate(({ name, args, query }) => new Promise((resolve, reject) => { const socket = new WebSocket(`ws://${location.host}/__lakebed/ws`); const timeout = setTimeout(() => { socket.close(); reject(new Error("RPC timed out")); }, 10000); socket.onopen = () => socket.send(JSON.stringify({ id: "test", op: query ? "query.subscribe" : "mutation.run", name, args })); socket.onmessage = (event) => { const message = JSON.parse(event.data); if (message.id !== "test") return; clearTimeout(timeout); socket.close(); if (!message.ok) reject(new Error(message.error)); else resolve(query ? message.data : message.result); }; }), { name, args, query }); } try { const cookContext = await browser.newContext(); participantContext = await chromium.launchPersistentContext(profileDir, { channel: "chromium", headless: true, args: ["--no-sandbox"] }); await participantContext.grantPermissions(["notifications"], { origin: "http://localhost:3000" }); participantContext.on("serviceworker", (worker) => worker.on("console", (message) => console.log("Worker:", message.text()))); const cook = await cookContext.newPage(); let participant = await participantContext.newPage(); for (const page of [cook, participant]) page.on("pageerror", (error) => errors.push(error.message)); await cook.goto("http://localhost:3000"); await cook.getByRole("button", { name: "Check open payments" }).waitFor(); const { shareToken } = await rpc(cook, "createEvent", [{ title: "Notification browser test", description: "Lunch with the team", date: "2026-09-20", maxParticipants: 10 }]); const events = await rpc(cook, "myEvents", [], true); const event = events.find((event) => event.shareToken === shareToken); await participant.goto(`http://localhost:3000/#join/${shareToken}`); await participant.getByPlaceholder("Jane Smith").fill("Alice"); await participant.locator("select").selectOption("2"); const optIn = participant.getByRole("checkbox", { name: "Notify me when this event closes" }); assert.equal(await optIn.isChecked(), false); await optIn.check(); await participant.getByRole("button", { name: "Join Event", exact: true }).click(); await participant.getByRole("heading", { name: "You're in!" }).waitFor(); await participant.reload(); await participant.getByRole("heading", { name: "You're in!" }).waitFor(); await participant.getByRole("button", { name: "Open notifications & push settings" }).click(); await participant.getByRole("checkbox", { name: "Notify me when this event closes" }).uncheck(); await participant.getByText("Saving…", { exact: false }).waitFor({ state: "hidden" }); await participant.getByRole("checkbox", { name: "Notify me when this event closes" }).check(); await participant.getByText("Saving…", { exact: false }).waitFor({ state: "hidden" }); // A refused browser prompt must leave the in-app opt-in usable. await participant.evaluate(() => { Notification.requestPermission = async () => "denied"; }); await participant.getByRole("button", { name: "Enable push on this device" }).click(); await participant.getByRole("alert").filter({ hasText: "permission was not granted" }).waitFor(); await participant.close(); await cook.goto(`http://localhost:3000/#event/${event.id}`); await cook.getByRole("button", { name: "Finalize & Close", exact: true }).click(); await cook.locator('input[type="number"]').last().fill("6.50"); cook.once("dialog", (dialog) => dialog.accept()); await cook.getByRole("button", { name: "Confirm & Close", exact: true }).click(); await cook.getByRole("button", { name: "Closed ↺", exact: true }).waitFor(); participant = await participantContext.newPage(); participant.on("pageerror", (error) => errors.push(error.message)); await participant.goto("http://localhost:3000/#notifications"); await participant.getByRole("button", { name: "Notifications, 1 unread", exact: true }).waitFor(); await participant.getByText("Final price: 6,50 € per person.", { exact: true }).waitFor(); await participant.getByText("Total for your party of 2: 13,00 €.", { exact: true }).waitFor(); await participant.setViewportSize({ width: 390, height: 844 }); assert.equal(await participant.evaluate(() => document.documentElement.scrollWidth > innerWidth), false); await participant.screenshot({ path: "/tmp/lunchtime-inbox.png", fullPage: true }); await participant.getByRole("button", { name: "View event & payment details" }).click(); await participant.getByText("This event is closed", { exact: true }).waitFor(); assert.equal(await participant.getByRole("button", { name: "Notifications, 1 unread", exact: true }).count(), 0); // Exercise the real worker and native notification display, with no live push provider. console.log("Inbox browser flow passed; checking the service worker."); await participant.evaluate(async () => { await Promise.race([ (async () => { await navigator.serviceWorker.register("/push-worker.js", { scope: "/" }); await navigator.serviceWorker.ready; })(), new Promise((_, reject) => setTimeout(() => reject(new Error("Service worker activation timed out")), 10000)), ]); }); const worker = participantContext.serviceWorkers()[0]; await participant.close(); const title = await worker.evaluate(async () => { await new Promise((resolve, reject) => { setTimeout(() => reject(new Error("Notification display timed out")), 10000); const event = new PushEvent("push"); event.waitUntil = (promise) => promise.then(resolve, reject); self.dispatchEvent(event); }); const notifications = await self.registration.getNotifications(); const title = notifications[0]?.title; notifications.forEach((notification) => notification.close()); return title; }); assert.equal(title, "A lunch event has closed"); assert.deepEqual(errors, []); await rpc(cook, "deleteEvent", [event.id]); console.log("Browser checks passed: registration opt-in, preference changes, denied permission, closing while away, unread/read state, mobile layout, and native service-worker notification display."); } finally { await participantContext?.close(); await browser.close(); rmSync(profileDir, { recursive: true, force: true }); }