// Run against `pnpm dev`. PLAYWRIGHT_MODULE may point to a shared installation. import { createRequire } from "node:module"; import assert from "node:assert/strict"; import { readFile } 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 page = await browser.newPage({ viewport: { width: 1440, height: 1000 } }); page.setDefaultTimeout(10000); const errors = []; const showcase = await readFile(new URL("../fixtures/reader-showcase.md", import.meta.url), "utf8"); page.on("console", message => { if (message.type() === "error" || message.type() === "warning") console.log(message.type(), message.text()); }); page.on("pageerror", error => errors.push(error.message)); await page.addInitScript(() => { window.isTauri = true; const root = "C:\\smoke-workspace"; const files = new Map([ ["notes/welcome.md", "---\ntitle: Welcome\n---\n\n# Working notes\n\nFirst paragraph.\n\n- [ ] A task\n\n[[second]]\n"], ["notes/second.md", "# Second note\n"], ["src/hello.py", "print('hello')\n"], ]); const callbacks = new Map(); const listeners = new Map(); let sequence = 0; const calls = []; const emit = (event, payload) => { for (const id of listeners.get(event) || []) callbacks.get(id)?.({ event, id, payload }); }; 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 = {}) => { calls.push({ command, args }); if (command === "plugin:event|listen") { const list = listeners.get(args.event) || []; list.push(args.handler); listeners.set(args.event, list); return args.handler; } if (command === "plugin:event|unlisten") { callbacks.delete(args.eventId); return; } if (command === "workspace_last_path") return root; if (command === "workspace_open") return { root, name: "smoke-workspace", watch_mode: "native" }; if (command === "workspace_read") { if (!files.has(args.rel)) throw new Error("missing"); return files.get(args.rel); } if (command === "workspace_write") { if (args.expected !== undefined && files.get(args.rel) !== args.expected) throw new Error("Save blocked: file changed on disk"); files.set(args.rel, args.contents); emit("workspace-event", { root, kind: "modify", paths: [args.rel] }); return; } if (command === "workspace_list") { const prefix = args.rel ? args.rel + "/" : ""; const entries = new Map(); for (const path of files.keys()) { if (!path.startsWith(prefix) || path.startsWith(".runestone/")) continue; const tail = path.slice(prefix.length); const name = tail.split("/")[0]; entries.set(name, { name, rel_path: prefix + name, kind: tail.includes("/") ? "folder" : "file" }); } return [...entries.values()]; } if (command === "workspace_create") { if (files.has(args.rel)) throw new Error("exists"); files.set(args.rel, ""); return; } if (command === "workspace_search") return []; if (command === "pty_open") { const data = [...new TextEncoder().encode("READY 🪨\r\n\x1b[6n")]; emit("pty-data", { id: args.id, data: data.slice(0, 8) }); emit("pty-data", { id: args.id, data: data.slice(8) }); await new Promise(resolve => setTimeout(resolve, 50)); return; } if (command.startsWith("pty_")) return; if (command === "plugin:dialog|message") return "Cancel"; throw new Error(`Unhandled IPC: ${command}`); }, }; window.__TAURI_EVENT_PLUGIN_INTERNALS__ = { unregisterListener: (_event, id) => callbacks.delete(id) }; window.__TEST__ = { files, calls, emit, root }; }); async function quickOpen(name) { await page.keyboard.press("Control+p"); await page.getByRole("textbox", { name: "Find a file" }).fill(name); await page.getByRole("dialog").getByRole("button").filter({ hasText: name }).first().click(); if (await page.getByRole("button", { name: "Switch to IDE layout (Ctrl E)", exact: true }).count()) { await page.getByRole("button", { name: "Switch to IDE layout (Ctrl E)", exact: true }).click(); } await page.locator(".monaco-editor textarea").waitFor(); } async function contents() { return page.evaluate(async () => { const { monaco } = await import("/src/lib/editor/monacoEnv.ts"); return monaco.editor.getEditors().find(editor => editor.getDomNode()?.isConnected)?.getValue(); }); } async function waitForText(text) { await page.evaluate(async () => { window.__MONACO__ = (await import("/src/lib/editor/monacoEnv.ts")).monaco; }); await page.waitForFunction(expected => window.__MONACO__.editor.getEditors().some(editor => editor.getDomNode()?.isConnected && editor.getValue() === expected), text); } async function focusEditor() { await page.locator(".monaco-host .view-lines").click({position:{x:120,y:12}}); } try { await page.goto(process.env.SMOKE_URL || "http://127.0.0.1:1420"); await page.getByText("smoke-workspace", { exact: true }).waitFor(); await quickOpen("welcome.md"); const source = "First paragraph.\n\nSecond paragraph.\n"; await page.evaluate(async value => { const { monaco } = await import("/src/lib/editor/monacoEnv.ts"); const editor = monaco.editor.getEditors().find(editor => editor.getDomNode()?.isConnected); editor.setValue(value); editor.setSelection(new monaco.Selection(1, 1, 1, 6)); editor.focus(); }, source); const menu = page.getByRole("menu", { name: "Markdown text actions", exact: true }); await page.keyboard.press("Shift+F10"); await menu.waitFor(); await menu.getByRole("menuitem", { name: "Format", exact: true }).hover(); await menu.getByRole("menuitem", { name: "Bold", exact: true }).click(); await waitForText("**First** paragraph.\n\nSecond paragraph.\n"); await page.keyboard.press("Control+z"); await waitForText(source); await page.getByRole("button", { name: "Switch to writing layout (Ctrl E)", exact: true }).click(); const writer = page.getByRole("textbox", { name: "Writing editor", exact: true }); await writer.click(); await page.keyboard.press("Control+Home"); await page.keyboard.press("Control+Shift+ArrowRight"); await page.keyboard.press("Shift+F10"); await menu.waitFor(); await menu.getByRole("menuitem", { name: "Format", exact: true }).hover(); await menu.getByRole("menuitem", { name: "Italic", exact: true }).click(); await page.keyboard.press("Control+s"); await page.waitForFunction(() => window.__TEST__.files.get("notes/welcome.md") === "*First* paragraph.\n\nSecond paragraph.\n"); await page.keyboard.press("Control+z"); await page.keyboard.press("Control+Home"); await page.keyboard.press("Shift+F10"); await menu.getByRole("menuitem", { name: "Paragraph", exact: true }).hover(); await menu.getByRole("menuitem", { name: "Heading 2", exact: true }).click(); await page.keyboard.press("Control+s"); await page.waitForFunction(() => window.__TEST__.files.get("notes/welcome.md").startsWith("## First paragraph.")); await page.keyboard.press("Control+z"); await page.keyboard.press("Control+End"); await page.keyboard.press("Shift+F10"); await menu.getByRole("menuitem", { name: "Insert", exact: true }).hover(); await menu.getByRole("menuitem", { name: "Table", exact: true }).click(); await page.locator(".live-writer table").waitFor(); await page.keyboard.press("Control+z"); await page.keyboard.press("Control+Home"); await page.keyboard.press("Control+Shift+ArrowRight"); // Mouse invocation must preserve the selection, too. await page.locator(".cm-line").filter({ hasText: "First paragraph." }).click({ button: "right", position: { x: 12, y: 10 } }); await menu.waitFor(); await menu.getByRole("menuitem", { name: "Format", exact: true }).hover(); await menu.getByRole("menuitem", { name: "Highlight", exact: true }).click(); await page.keyboard.press("Control+s"); await page.waitForFunction(() => window.__TEST__.files.get("notes/welcome.md").startsWith("==First== paragraph.")); await page.keyboard.press("Control+End"); await page.locator(".writer-highlight").waitFor(); assert.equal(await page.locator(".writer-highlight").textContent(), "First"); await page.keyboard.press("Control+z"); await page.keyboard.press("Control+End"); await page.keyboard.press("Shift+F10"); await menu.getByRole("menuitem", { name: "Insert", exact: true }).hover(); await page.screenshot({ path: ".audit/markdown-insert-menu.png" }); await page.keyboard.press("Escape"); await page.setViewportSize({ width: 760, height: 600 }); await page.evaluate(() => new Promise(resolve => requestAnimationFrame(() => requestAnimationFrame(resolve)))); await writer.focus(); await page.keyboard.press("Shift+F10"); await menu.getByRole("menuitem", { name: "Paragraph", exact: true }).focus(); await page.keyboard.press("ArrowRight"); assert.equal(await page.evaluate(() => document.activeElement.textContent), "Bullet list"); const rect = await menu.getByRole("menu", { name: "Paragraph", exact: true }).boundingBox(); assert.ok(rect.x >= 0 && rect.y >= 0 && rect.x + rect.width <= 760 && rect.y + rect.height <= 600); await page.screenshot({ path: ".audit/markdown-paragraph-menu.png" }); await page.keyboard.press("ArrowLeft"); assert.equal(await page.evaluate(() => document.activeElement.textContent), "Paragraph"); await page.keyboard.press("Escape"); await page.keyboard.press("Control+Home"); await page.keyboard.press("Control+Shift+ArrowRight"); await page.keyboard.press("Shift+F10"); await menu.getByRole("menuitem", { name: "Add external link", exact: true }).click(); await page.getByRole("textbox", { name: "External link URL" }).fill("https://example.com/a(b)"); await page.getByRole("button", { name: "Continue", exact: true }).click(); await page.keyboard.press("Control+s"); await page.waitForFunction(() => window.__TEST__.files.get("notes/welcome.md").startsWith("[First](https://example.com/a%28b%29) paragraph.")); await page.keyboard.press("Control+z"); await page.keyboard.press("Shift+F10"); await menu.getByRole("menuitem", { name: "Extract current selection…", exact: true }).click(); await page.getByRole("textbox", { name: "Extract selection to a new note (workspace relative path)" }).fill("notes/extracted.md"); await page.getByRole("button", { name: "Continue", exact: true }).click(); await page.keyboard.press("Control+s"); await page.waitForFunction(() => window.__TEST__.files.get("notes/welcome.md").startsWith("[[/notes/extracted.md|First]] paragraph.")); assert.equal(await page.evaluate(() => window.__TEST__.files.get("notes/extracted.md")), "First\n"); await page.keyboard.press("Control+z"); await page.context().grantPermissions(["clipboard-read", "clipboard-write"]); await page.evaluate(() => navigator.clipboard.writeText("Pasted")); await page.keyboard.press("Shift+F10"); await menu.getByRole("menuitem", { name: "Paste as plain text", exact: true }).click(); await page.waitForFunction(() => document.querySelector(".cm-content")?.textContent.includes("Pasted paragraph.")); await page.keyboard.press("Control+s"); await page.waitForFunction(() => window.__TEST__.files.get("notes/welcome.md").startsWith("Pasted paragraph.")); await page.evaluate(() => { navigator.clipboard.readText = () => new Promise(resolve => { window.__finishPaste = resolve; }); }); await page.keyboard.press("Shift+F10"); await menu.getByRole("menuitem", { name: "Paste as plain text", exact: true }).click(); await writer.focus(); await page.keyboard.press("End"); await page.keyboard.type(" kept"); await page.evaluate(async () => { window.__finishPaste("STALE"); await new Promise(resolve => requestAnimationFrame(() => requestAnimationFrame(resolve))); }); assert.ok(!(await writer.textContent()).includes("STALE")); assert.ok((await writer.textContent()).includes("kept")); assert.deepEqual(errors, []); console.log("PASS: Markdown menus in source/writing modes, selection retention, formatting, paragraph conversion, table insertion, undo, keyboard navigation, narrow viewport, links, extraction, clipboard and stale-selection guards."); } catch (error) { console.error("Browser errors:", errors); console.error("Visible text:", (await page.locator("body").innerText()).slice(0, 5000)); await page.screenshot({ path: ".audit/markdown-menu-failure.png" }); throw error; } finally { await browser.close(); }