// Web console page tests: the page as the macOS app's frame has it. WebKit // gives that frame no cookie, so the console signs it in by a path prefix // (`/f//`, routes.mjs) and the page runs under it. Every request the page // makes there must stay under the prefix: one that leaves it gets a 403, and // only in the macOS app. This file opens the page that way in a browser // context with no cookie, uses the parts that fetch, and fails on any request // that left the prefix or was refused. Run with: // node --test scripts/test-web-console-page-frame.mjs import crypto from "node:crypto"; import fs from "node:fs"; import path from "node:path"; import test from "node:test"; import assert from "node:assert/strict"; import { pageSuite, openSession, drawn } from "./page-test-helpers.mjs"; import { until, HOME } from "./web-console-test-helpers.mjs"; import { createTab, listTabs } from "../src/web-console/notepads.mjs"; const suite = pageSuite(test); const LOOKS_FILE = path.join(HOME, "looks.json"); // The server takes the app's frame mode only on macOS, with the app's page as // the embed ancestor. On any other OS this makes the server alone believe it // runs on macOS (the CLI that starts it is left as it is), so CI's Linux run // checks the same thing. The whole server sees darwin, not only frameKey(): // every macOS branch in it runs here too. const FAKE_MAC = "data:text/javascript," + encodeURIComponent('if (/server\\.mjs$/.test(process.argv[1] || "")) Object.defineProperty(process, "platform", { value: "darwin" });'); const FRAME_ENV = { PLAN2CODE_EMBED_ORIGIN: "tauri://localhost", NODE_OPTIONS: `--import=${FAKE_MAC}` }; // The frame's key, as routes.mjs makes it from the session token. const frameKey = (token) => crypto.createHash("sha256").update(token).digest("hex").slice(0, 32); test("under the frame's prefix, with no cookie, the dashboard draws, its stream connects, and nothing it asks for leaves the prefix", async (t) => { fs.writeFileSync(LOOKS_FILE, JSON.stringify({ looks: { welcomeSeen: true, hideActivities: false } })); t.after(() => fs.rmSync(LOOKS_FILE, { force: true })); const browser = await suite.browser(); const s = await openSession(browser, { t, workflow: "dashboard", env: FRAME_ENV }); if (!listTabs(s.project).tabs.length) createTab(s.project, "Notes"); const signIn = new URL(s.url); const token = /^\/s\/([^/]+)\/$/.exec(signIn.pathname)[1]; const prefix = `/f/${frameKey(token)}/`; const context = await browser.newContext({ viewport: { width: 1280, height: 800 } }); t.after(() => context.close().catch(() => {})); const page = await context.newPage(); page.setDefaultTimeout(60000); const left = []; const refused = []; const errors = []; let streamed = false; page.on("pageerror", (e) => errors.push(e.message)); page.on("request", (r) => { const u = new URL(r.url()); if (u.origin === signIn.origin && !u.pathname.startsWith(prefix)) left.push(`${r.method()} ${u.pathname}`); }); page.on("response", (r) => { const u = new URL(r.url()); if (u.origin !== signIn.origin) return; if (r.status() === 403 || r.status() === 404) refused.push(`${r.status()} ${u.pathname}`); if (u.pathname === `${prefix}events` && r.status() === 200) streamed = true; }); const res = await page.goto(signIn.origin + prefix, { waitUntil: "domcontentloaded" }); assert.equal(res.status(), 200, "the page itself, under the prefix"); await drawn(page); // Past Planny's wake-up, to the menu. const skip = page.getByRole("button", { name: "Skip the intro" }); await skip.click(); await skip.waitFor({ state: "hidden" }); await page.locator("#main section.dash-group").first().waitFor(); assert.deepEqual(await context.cookies(), [], "the frame signs in with no cookie"); await until(() => streamed, 10000); assert.ok(streamed, "the event stream connected under the prefix"); // A step that waits on a request the server refused times out; the // requests that left the prefix or were refused are the reason, so they // are reported first. try { // The Notebook: its tab list and note come from the server. await page.click("#btn-notepad"); await page.locator("#notepad-modal[open]").waitFor({ timeout: 15000 }); await page.waitForFunction( () => { const c = document.querySelector(".notepad-cm-host")?.shadowRoot?.querySelector(".cm-content"); return Boolean(c) && c.getAttribute("aria-readonly") !== "true"; }, null, { timeout: 15000 } ); await page.click("#notepad-close"); // User Preferences: a change of theme is saved to the server. await page.click("#btn-looks"); await page.locator("#looks-modal[open]").waitFor({ timeout: 15000 }); const saved = page.waitForResponse((r) => r.url().endsWith(`${prefix}looks`) && r.request().method() === "POST", { timeout: 15000 }); await page.locator('input[name="looks-theme"][value="dark"]').check(); assert.equal((await saved).status(), 200, "the looks were saved"); await page.keyboard.press("Escape"); // Activities: a game's module, loaded the first time it opens. await page.click("#btn-activities"); await page.locator("#activities-modal[open]").waitFor({ timeout: 15000 }); await page.locator('#activities-picker .activities-row[data-activity="snake"]').click(); await page.locator("#activities-board").waitFor({ timeout: 15000 }); } finally { await until(async () => { const n = refused.length + left.length; await new Promise((r) => setTimeout(r, 300)); return n === refused.length + left.length; }, 5000); assert.deepEqual(left, [], "every request stays under the prefix"); assert.deepEqual(refused, [], "no request is refused or missing"); } assert.deepEqual(errors, [], "no error on the page"); });