// Run against pnpm dev. Native IPC is mocked; this does not test the operating system title bar. import { createRequire } from "node:module"; import assert from "node:assert/strict"; import { mkdir } from "node:fs/promises"; const require = createRequire(import.meta.url); const { chromium } = require(process.env.PLAYWRIGHT_MODULE || "playwright"); const browser = await chromium.launch({ headless: true, ...(process.env.CHROME_PATH ? { executablePath: process.env.CHROME_PATH } : {}) }); const origin = process.env.SMOKE_URL || "http://127.0.0.1:1420"; const errors = []; const context = await browser.newContext({ viewport: { width: 1100, height: 760 } }); const watch = page => page.on("pageerror", error => errors.push(error.message)); try { const main = await context.newPage(); watch(main); await main.goto(origin); await main.getByRole("button", { name: "Settings (Ctrl+,)", exact: true }).click(); assert.equal(await main.locator("dialog:modal").count(), 1, "Browser preview retains the settings dialog"); const settings = await context.newPage(); watch(settings); await settings.goto(`${origin}/?settingsWindow=true`); await settings.getByRole("heading", { name: "General", exact: true }).waitFor(); assert.equal(await settings.locator(".workbench").count(), 0); const interval = settings.getByRole("spinbutton", { name: "Auto-save interval" }); await interval.fill("17"); await interval.press("Tab"); await main.waitForFunction(() => document.querySelector("#autosave-seconds")?.value === "17"); await main.getByRole("spinbutton", { name: "Auto-save interval" }).fill("29"); await main.getByRole("spinbutton", { name: "Auto-save interval" }).press("Tab"); await settings.waitForFunction(() => document.querySelector("#autosave-seconds")?.value === "29"); await settings.reload(); await settings.waitForFunction(() => document.querySelector("#autosave-seconds")?.value === "29"); await interval.fill("301"); await interval.press("Tab"); await settings.getByRole("alert").filter({ hasText: "Choose a whole number" }).waitFor(); assert.equal(await settings.evaluate(() => JSON.parse(localStorage.getItem("runestone.settings")).autosaveSeconds), 29); await settings.evaluate(() => { Storage.prototype.setItem = () => { throw new Error("Storage full"); }; }); await interval.fill("40"); await interval.press("Tab"); await settings.getByRole("alert").filter({ hasText: "Could not save these settings" }).waitFor(); assert.equal(await settings.evaluate(() => JSON.parse(localStorage.getItem("runestone.settings")).autosaveSeconds), 29); await settings.reload(); await settings.getByRole("button", { name: "Plug-ins", exact: true }).click(); await settings.getByRole("heading", { name: "Plug-ins", exact: true }).waitFor(); await settings.getByRole("button", { name: "General", exact: true }).click(); await mkdir(".audit", { recursive: true }); await settings.screenshot({ path: ".audit/settings-window.png" }); await settings.setViewportSize({ width: 540, height: 420 }); assert.equal(await settings.evaluate(() => document.documentElement.scrollWidth <= innerWidth), true); await settings.keyboard.press("Escape"); await settings.waitForURL(`${origin}/`); const desktop = await context.newPage(); watch(desktop); await desktop.addInitScript(() => { window.isTauri = true; const callbacks = new Map(); let sequence = 0; window.settingsTest = { opens: 0, fail: false, closes: 0 }; window.__TAURI_INTERNALS__ = { metadata: { currentWindow: { label: "main" }, currentWebview: { label: "main" } }, transformCallback: callback => { const id = ++sequence; callbacks.set(id, callback); return id; }, unregisterCallback: id => callbacks.delete(id), invoke: async (command, args = {}) => { if (command === "settings_open_window") { window.settingsTest.opens++; if (window.settingsTest.fail) throw new Error("Native window creation failed"); await new Promise(resolve => setTimeout(resolve, 150)); return; } if (command === "plugins_snapshot") return { root: "C:/plugins", plugins: [] }; if (command === "workspace_last_path") return null; if (command === "plugin:event|listen") return args.handler; if (command === "plugin:event|unlisten") return; if (command === "plugin:window|close") { window.settingsTest.closes++; return; } throw new Error(`Unexpected IPC: ${command}`); }, }; window.__TAURI_EVENT_PLUGIN_INTERNALS__ = { unregisterListener: (_event, id) => callbacks.delete(id) }; }); await desktop.goto(origin); await desktop.getByRole("button", { name: "Settings (Ctrl+,)", exact: true }).dblclick(); assert.equal(await desktop.evaluate(() => window.settingsTest.opens), 1, "Rapid requests share the pending native open"); assert.equal(await desktop.locator("dialog:modal").count(), 0, "Native open does not block the workbench"); await desktop.waitForTimeout(180); await desktop.keyboard.press("Control+,"); await desktop.waitForFunction(() => window.settingsTest.opens === 2); await desktop.waitForTimeout(180); await desktop.evaluate(() => { window.settingsTest.fail = true; }); await desktop.keyboard.press("Control+,"); await desktop.getByText(/Could not open settings: .*Native window creation failed/).waitFor(); await desktop.goto(`${origin}/?settingsWindow=true`); await desktop.getByRole("heading", { name: "General", exact: true }).waitFor(); await desktop.getByRole("button", { name: "Close settings", exact: true }).click(); assert.equal(await desktop.evaluate(() => window.settingsTest.closes), 1); assert.deepEqual(errors, []); console.log("Settings window smoke passed: preview fallback, standalone layout, two-way save sync, persistence, failed saves, native open requests, and close."); } finally { await browser.close(); }