// Run against pnpm dev; use PLAYWRIGHT_MODULE for a shared Playwright installation. import { createRequire } from "node:module"; import assert from "node:assert/strict"; 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 page = await browser.newPage({ viewport: { width: 1100, height: 800 } }); const errors = []; page.on("pageerror", err => errors.push(err.message)); await page.addInitScript(() => { window.isTauri = true; let current = "D:\\Notes"; let recent = [current, "D:\\Writing\\Notes", "D:\\Missing"]; let picked = null; let contents = "Original note"; const calls = []; const windows = []; window.__TAURI_INTERNALS__ = { metadata: { currentWindow: { label: "main" }, currentWebview: { label: "main" } }, transformCallback: () => 1, unregisterCallback: () => {}, invoke: async (command, args = {}) => { calls.push({ command, args }); if (command === "workspace_last_path") return current; if (command === "workspace_recent") return recent; if (command === "workspace_list") return args.rel ? [] : [{ name: "note.txt", rel_path: "note.txt", kind: "file" }]; if (command === "workspace_read") { if (args.rel === "note.txt") return contents; throw "File not found"; } if (command === "workspace_write") { contents = args.contents; return; } if (command === "pty_profiles") return [{ id: "powershell", title: "PowerShell", kind: "powershell" }]; if (command === "plugin:dialog|open") return picked; if (command === "plugin:dialog|message") return "Cancel"; if (command === "workspace_new") { if (args.name === "Existing") throw "A file or folder with that name already exists."; const root = args.parent + "\\" + args.name; recent = [root, ...recent]; windows.push(root); return; } if (command === "workspace_open_window") { if (args.path.endsWith("Missing")) throw "folder does not exist"; windows.push(args.path); recent = [args.path, ...recent.filter(p => p !== args.path)]; return; } if (command === "workspace_open") { if (args.path.endsWith("Missing")) throw "folder does not exist"; current = args.path; recent = [current, ...recent.filter(p => p !== current)]; return { root: current, name: current.split("\\").pop(), watch_mode: "native" }; } return null; }, }; window.__TAURI_EVENT_PLUGIN_INTERNALS__ = { unregisterListener: () => {} }; window.__TEST__ = { calls, windows, pick: path => picked = path, clear: () => recent = [] }; }); try { await page.goto(process.env.SMOKE_URL || "http://127.0.0.1:1420"); await page.locator(".ws").filter({ hasText: "Notes" }).waitFor(); 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.evaluate(async () => { window.__MONACO__ = (await import("/src/lib/editor/monacoEnv.ts")).monaco; window.__MONACO__.editor.getEditors().find(e => e.getDomNode()?.isConnected).setValue("My pending edit"); }); const trigger = page.getByRole("button", { name: "Open folder", exact: true }); const menu = page.getByRole("dialog", { name: "Workspaces", exact: true }); await trigger.click(); await menu.getByText("D:\\Writing\\Notes", { exact: true }).waitFor(); assert.equal(await page.evaluate(() => window.__TEST__.calls.filter(c => c.command === "plugin:dialog|open").length), 0); await page.screenshot({ path: ".audit/workspaces-menu.png" }); const opened = await page.evaluate(() => window.__TEST__.calls.filter(c => c.command === "workspace_open").length); await menu.getByRole("button").filter({ hasText: "Current" }).click(); assert.equal(await page.evaluate(() => window.__TEST__.calls.filter(c => c.command === "workspace_open").length), opened); await page.keyboard.press("Control+o"); await menu.getByRole("button").filter({ hasText: "Missing" }).click(); await menu.getByRole("alert").filter({ hasText: "folder does not exist" }).waitFor(); await menu.getByRole("button", { name: "Open another folder…" }).click(); assert(await menu.isVisible(), "Cancelling folder picker keeps history available"); await menu.getByRole("button").filter({ hasText: "D:\\Writing\\Notes" }).click(); await menu.waitFor({ state: "hidden" }); assert.equal(await page.locator(".ws").textContent(), "Notes"); assert.deepEqual(await page.evaluate(() => window.__TEST__.windows), ["D:\\Writing\\Notes"]); await trigger.click(); await page.evaluate(() => window.__TEST__.pick("D:\\Other")); await menu.getByRole("button", { name: "Open another folder…" }).click(); await menu.waitFor({ state: "hidden" }); assert.equal(await page.locator(".ws").textContent(), "Notes"); await trigger.click(); await menu.getByRole("button", { name: "New workspace…" }).click(); const dialog = page.getByRole("dialog", { name: "New workspace", exact: true }); assert(await dialog.getByRole("button", { name: "Create workspace", exact: true }).isDisabled()); await dialog.getByLabel("Workspace name").fill("Existing"); await page.evaluate(() => window.__TEST__.pick("D:\\Projects")); await dialog.getByRole("button", { name: "Browse…" }).click(); await dialog.getByRole("button", { name: "Create workspace", exact: true }).click(); await dialog.getByRole("alert").filter({ hasText: "already exists" }).waitFor(); await dialog.getByLabel("Workspace name").fill("Fresh notes"); await page.screenshot({ path: ".audit/workspaces-create.png" }); await dialog.getByRole("button", { name: "Create workspace", exact: true }).click(); await dialog.waitFor({ state: "hidden" }); assert.equal(await page.locator(".ws").textContent(), "Notes"); assert.deepEqual(await page.evaluate(() => window.__TEST__.windows), ["D:\\Writing\\Notes", "D:\\Other", "D:\\Projects\\Fresh notes"]); assert.equal(await page.evaluate(() => window.__TEST__.calls.filter(c => c.command === "workspace_open").length), opened); assert.equal(await page.evaluate(() => window.__TEST__.calls.filter(c => c.command === "plugin:dialog|message").length), 0); assert.equal(await page.evaluate(() => window.__MONACO__.editor.getEditors().find(e => e.getDomNode()?.isConnected)?.getValue()), "My pending edit"); assert(await page.getByRole("tab", { name: /note\.txt/ }).isVisible()); await trigger.click(); await page.keyboard.press("End"); assert.equal(await page.locator(":focus").textContent(), " Open another folder…"); await page.keyboard.press("Escape"); await menu.waitFor({ state: "hidden" }); await page.evaluate(() => window.__TEST__.clear()); await trigger.click(); await menu.getByText("No recent workspaces yet.").waitFor(); await page.getByRole("button", { name: "New tab (Ctrl T)", exact: true }).click(); await menu.waitFor({ state: "hidden" }); assert.deepEqual(errors, []); console.log("Workspace smoke passed: history, opening and creating in new windows, original workspace preserved, cancellation, errors, keyboard and empty state."); } finally { await browser.close(); }