// Run against pnpm dev with PLAYWRIGHT_MODULE and CHROME_PATH if needed. import { createRequire } from "node:module"; import { mkdirSync, readFileSync } from "node:fs"; import assert from "node:assert/strict"; const require = createRequire(import.meta.url); const { version } = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8")); mkdirSync(".audit", { recursive: true }); 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 context = await browser.newContext({ viewport: { width: 1100, height: 800 } }); const page = await context.newPage(); const errors = []; page.on("pageerror", error => errors.push(error.message)); await context.addInitScript(() => { window.isTauri = true; const disk = { "note.txt": "Original note" }; const writes = []; window.__TEST__ = { disk, writes }; window.__TAURI_INTERNALS__ = { metadata: { currentWindow: { label: "main" }, currentWebview: { label: "main" } }, transformCallback: () => 1, unregisterCallback: () => {}, invoke: async (command, args = {}) => { if (command === "plugins_snapshot") return { root: "D:\\AppData\\RuneStone\\plugins", plugins: [] }; if (command === "workspace_last_path") return "D:\\Notes"; if (command === "workspace_open") return { root: "D:\\Notes", name: "Notes", watch_mode: "native" }; if (command === "workspace_list") return args.rel ? [] : [{ name: "note.txt", rel_path: "note.txt", kind: "file" }]; if (command === "workspace_read") { if (args.rel in disk) return disk[args.rel]; throw "Missing file"; } if (command === "workspace_write") { disk[args.rel] = args.contents; writes.push(args); return; } return null; }, }; window.__TAURI_EVENT_PLUGIN_INTERNALS__ = { unregisterListener: () => {} }; }); const url = process.env.SMOKE_URL || "http://127.0.0.1:1420"; const settings = page.getByRole("dialog", { name: "Settings", exact: true }); const trigger = page.getByRole("button", { name: "Settings (Ctrl+,)", exact: true }); const interval = settings.getByRole("spinbutton", { name: "Auto-save interval" }); const enabled = settings.getByRole("switch", { name: "Auto-save", exact: true }); async function changeSeconds(value) { await interval.fill(String(value)); await interval.press("Tab"); } async function edit(value) { await page.evaluate(async text => { const { monaco } = await import("/src/lib/editor/monacoEnv.ts"); monaco.editor.getEditors().find(editor => editor.getDomNode()?.isConnected).setValue(text); }, value); } const writes = () => page.evaluate(() => window.__TEST__.writes.length); try { await page.clock.install(); await page.goto(url); await page.locator(".ws").filter({ hasText: "Notes" }).waitFor(); await trigger.click(); assert.equal(await interval.inputValue(), "5"); assert(await enabled.isChecked()); await changeSeconds(2); await settings.getByRole("status").filter({ hasText: "Changes saved." }).waitFor(); await page.keyboard.press("Escape"); assert(await trigger.evaluate(element => element === document.activeElement), "Closing restores focus"); await page.reload(); await trigger.click(); assert.equal(await interval.inputValue(), "2", "Interval survives reload"); await changeSeconds(0); await settings.getByRole("alert").filter({ hasText: "whole number" }).waitFor(); assert.equal(await page.evaluate(() => JSON.parse(localStorage.getItem("runestone.settings")).autosaveSeconds), 2); await changeSeconds(2); await settings.getByRole("textbox", { name: "Search settings" }).fill("plugins"); await settings.getByText("No plugins installed.", { exact: true }).waitFor(); assert(await settings.getByRole("button", { name: "Install from folder", exact: true }).isEnabled()); await settings.getByRole("textbox", { name: "Search settings" }).fill("no such setting"); await settings.getByText("No settings found", { exact: true }).waitFor(); await settings.getByRole("button", { name: "Clear search" }).click(); await page.screenshot({ path: ".audit/settings-general.png" }); await page.keyboard.press("Escape"); await page.keyboard.press("Control+p"); await page.getByRole("textbox", { name: "Find a file" }).fill("note.txt"); await page.getByRole("dialog").getByRole("button").filter({ hasText: "note.txt" }).first().click(); await page.locator(".monaco-editor textarea").waitFor(); await page.clock.pauseAt(new Date()); await trigger.click(); await changeSeconds(3); await page.keyboard.press("Escape"); await edit("Saved at three seconds"); const before = await writes(); await page.clock.runFor(2100); assert.equal(await writes(), before, "Old two-second timer is removed"); await page.clock.runFor(1000); assert.equal(await writes(), before + 1, "New interval saves the dirty buffer"); await page.clock.runFor(3100); assert.equal(await writes(), before + 1, "Clean files are not rewritten"); await trigger.click(); await enabled.uncheck(); assert(await interval.isDisabled()); await page.keyboard.press("Escape"); await edit("Manual save while auto-save is disabled"); await page.clock.runFor(10000); assert.equal(await writes(), before + 1); await page.keyboard.press("Control+s"); await page.waitForFunction(count => window.__TEST__.writes.length === count, before + 2); await page.locator(".monaco-editor textarea").focus(); await page.keyboard.press("Control+,"); await settings.waitFor(); await page.keyboard.press("Escape"); await page.locator(".monaco-editor textarea").focus(); await page.keyboard.press("F1"); await page.getByRole("dialog", { name: "Help and about", exact: true }).waitFor(); await page.keyboard.press("Escape"); const other = await context.newPage(); await other.goto(url); await other.getByRole("button", { name: "Settings (Ctrl+,)" }).click(); assert.equal(await other.getByRole("spinbutton", { name: "Auto-save interval" }).inputValue(), "3"); assert.equal(await other.getByRole("switch", { name: "Auto-save", exact: true }).isChecked(), false); await other.getByRole("button", { name: "Restore defaults" }).click(); await page.waitForFunction(() => JSON.parse(localStorage.getItem("runestone.settings")).autosaveEnabled); await trigger.click(); await enabled.waitFor(); assert(await enabled.isChecked(), "Other windows receive changes"); assert.equal(await interval.inputValue(), "5"); await page.keyboard.press("Escape"); await edit("Auto-save enabled from another window"); await page.clock.runFor(5100); assert.equal(await writes(), before + 3); await other.close(); await trigger.click(); await page.evaluate(() => { window.__SET_ITEM__ = Storage.prototype.setItem; Storage.prototype.setItem = () => { throw new Error("Storage unavailable"); }; }); await changeSeconds(7); await settings.getByRole("alert").filter({ hasText: "Could not save" }).waitFor(); await page.evaluate(() => { Storage.prototype.setItem = window.__SET_ITEM__; }); await page.keyboard.press("Escape"); await trigger.click(); assert.equal(await interval.inputValue(), "5", "Failed writes preserve applied settings"); await page.keyboard.press("Escape"); await page.getByRole("button", { name: "Help and about (F1)" }).click(); const help = page.getByRole("dialog", { name: "Help and about", exact: true }); await help.getByRole("heading", { name: "RuneStone", exact: true }).waitFor(); await help.getByText(`Version ${version}`, { exact: false }).waitFor(); await page.screenshot({ path: ".audit/settings-help.png" }); await page.keyboard.press("Escape"); await page.keyboard.press("Control+b"); await page.keyboard.press("Control+,"); await settings.waitFor(); await page.keyboard.press("Escape"); await page.keyboard.press("F1"); await help.waitFor(); await page.keyboard.press("Escape"); await page.setViewportSize({ width: 420, height: 650 }); await page.keyboard.press("Control+,"); await settings.waitFor(); assert(await settings.evaluate(el => el.scrollWidth === el.clientWidth), "Narrow layout fits"); await page.screenshot({ path: ".audit/settings-narrow.png" }); for (let i = 0; i < 12; i++) { await page.keyboard.press("Tab"); assert(await settings.evaluate(el => el.contains(document.activeElement)), "Focus stays in the modal"); } assert.deepEqual(errors, []); console.log("Settings smoke passed: timer changes, disabled auto-save, manual saves, persistence, cross-window updates, validation, storage failure, help, search, focus, shortcuts, and narrow layout."); } finally { await browser.close(); }