import { mockFeaturePlugins } from "./pluginMocks.mjs"; // 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 === "workspace_abs_path") return root + "\\" + args.rel.replaceAll("/", "\\"); if (command === "pty_profiles") return [{ id: "powershell", title: "PowerShell", kind: "powershell" }, { id: "cmd", title: "Command Prompt", kind: "cmd" }, { id: "git-bash", title: "Git Bash", kind: "bash" }, { id: "node", title: "JavaScript Debug Terminal", kind: "node" }]; 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 }; }); await mockFeaturePlugins(page); const count = command => page.evaluate(command => window.__TEST__.calls.filter(c => c.command === command).length, command); const waitCount = (command, n) => page.waitForFunction(({ command, n }) => window.__TEST__.calls.filter(c => c.command === command).length === n, { command, n }); const click = name => page.getByRole("button", { name, exact: true }).click(); const menu = name => page.getByRole("menuitem", { name, exact: true }).click(); const writes = () => page.evaluate(() => window.__TEST__.calls.filter(c => c.command === "pty_write").map(c => c.args.data).join("")); try { await page.goto(process.env.SMOKE_URL || "http://127.0.0.1:1420"); await page.getByText("smoke-workspace", { exact: true }).waitFor(); await click("Terminal"); await waitCount("pty_open", 1); assert.equal(await page.getByRole("navigation", { name: "Terminal list" }).count(), 0); const adjacent = await page.getByRole("button", { name: "New terminal", exact: true }).evaluate(button => { const buttons = [...button.closest(".actions").querySelectorAll("button")]; return buttons[buttons.indexOf(button) + 1]?.getAttribute("aria-label"); }); assert.equal(adjacent, "Terminal profiles and tasks"); await click("Split terminal (Ctrl+Shift+5)"); await waitCount("pty_open", 2); assert.equal(await page.getByRole("navigation", { name: "Terminal list" }).count(), 1); assert.equal(await page.locator(".pane.shown").count(), 2); const original = await page.locator(".pane.shown").first().boundingBox(); await click("Maximize panel"); const enlarged = await page.locator(".pane.shown").first().boundingBox(); assert.ok(enlarged.height > original.height + 100); assert.equal(await count("pty_close"), 0); await page.screenshot({ path: ".audit/terminal-splits.png" }); await click("Restore panel"); await click("Hide terminal (sessions keep running)"); assert.equal(await count("pty_close"), 0); await page.keyboard.press("Control+j"); assert.equal(await count("pty_open"), 2); await click("Terminal profiles and tasks"); await page.screenshot({ path: ".audit/terminal-profiles.png" }); await menu("Command Prompt"); await waitCount("pty_open", 3); assert.equal(await page.locator(".pane.shown").count(), 1); assert.equal(await page.evaluate(() => window.__TEST__.calls.filter(c => c.command === "pty_open").at(-1).args.profile), "cmd"); await page.keyboard.press("Control+Shift+5"); await waitCount("pty_open", 4); assert.equal(await page.locator(".pane.shown").count(), 2); await click("Kill terminal"); await waitCount("pty_close", 1); assert.equal(await page.locator(".pane.shown").count(), 1); // OSC prompt integration reaches the history pickers. await page.evaluate(() => { const id = window.__TEST__.calls.filter(c => c.command === "pty_open")[2].args.id; const data = `\x1b]633;RSCommand;${btoa("echo history-check")}\x07\x1b]633;RSCwd;${btoa("C:\\two words")}\x07\x1b]133;A\x07C:\\two words>\x1b]133;B\x07`; window.__TEST__.emit("pty-data", { id, data: [...new TextEncoder().encode(data)] }); }); await click("More terminal actions"); await page.screenshot({ path: ".audit/terminal-actions.png" }); await menu("Run Recent Command… Ctrl+Alt+R"); await page.getByRole("dialog").getByRole("button", { name: "echo history-check" }).click(); await page.waitForFunction(() => window.__TEST__.calls.some(c => c.command === "pty_write" && c.args.data.includes("echo history-check"))); await page.keyboard.press("Control+g"); await page.getByRole("dialog").getByRole("button", { name: "C:\\two words" }).click(); assert.ok((await writes()).includes('cd /d "C:\\two words"')); await click("Terminal profiles and tasks"); await menu("Configure Terminal Settings"); await page.getByLabel("Font size", { exact: true }).fill("16"); await page.getByLabel("Default profile", { exact: true }).selectOption("git-bash"); await click("Save settings"); await click("New terminal"); await waitCount("pty_open", 5); assert.equal(await page.evaluate(() => window.__TEST__.calls.filter(c => c.command === "pty_open").at(-1).args.profile), "git-bash"); assert.equal(await page.evaluate(() => JSON.parse(localStorage.getItem("runestone.terminal")).fontSize), 16); await page.evaluate(() => window.__TEST__.files.set(".runestone/tasks.json", JSON.stringify({ tasks: [{ label: "Smoke task", command: "echo task-ok", profile: "powershell" }] }))); await click("Terminal profiles and tasks"); await menu("Run Task…"); await page.getByRole("dialog").getByRole("button", { name: "Smoke task echo task-ok" }).click(); await waitCount("pty_open", 6); await page.waitForFunction(() => window.__TEST__.calls.some(c => c.command === "pty_write" && c.args.data.includes("echo task-ok"))); // Capture selected source while the terminal menu takes focus. await page.getByRole("button", { name: /Find a file/ }).click(); await page.getByRole("textbox", { name: "Find a file" }).fill("hello.py"); await page.getByRole("dialog").getByRole("button").filter({ hasText: "hello.py" }).click(); await page.locator(".monaco-editor textarea").waitFor(); await page.evaluate(async () => { const { monaco } = await import("/src/lib/editor/monacoEnv.ts"); const editor = monaco.editor.getEditors().find(e => e.getDomNode()?.isConnected); editor.setSelection({ startLineNumber: 1, startColumn: 1, endLineNumber: 1, endColumn: 15 }); }); await click("More terminal actions"); await menu("Run Selected Text"); await page.waitForFunction(() => window.__TEST__.calls.some(c => c.command === "pty_write" && c.args.data.includes("print('hello')"))); await click("More terminal actions"); await menu("Run Active File"); await page.waitForFunction(() => window.__TEST__.calls.some(c => c.command === "pty_write" && c.args.data.includes("python 'C:\\smoke-workspace\\src\\hello.py'"))); await click("Terminal profiles and tasks"); await menu("New Terminal Window Ctrl+Shift+Alt+`"); await waitCount("pty_new_window", 1); await click("More terminal actions"); await menu("Start Dictation"); await waitCount("pty_dictation", 1); await click("More terminal actions"); await menu("Clear Terminal"); assert.equal(await count("pty_close"), 1); await page.setViewportSize({ width: 880, height: 600 }); await page.screenshot({ path: ".audit/terminal-narrow.png" }); for (let remaining = 5; remaining > 1; remaining--) await click("Kill terminal"); await waitCount("pty_close", 5); assert.equal(await page.getByRole("navigation", { name: "Terminal list" }).count(), 0); await page.screenshot({ path: ".audit/terminal-single.png" }); await click("Kill terminal"); await waitCount("pty_close", 6); await click("Start a terminal"); await waitCount("pty_open", 7); await page.locator(".pane.shown .xterm-helper-textarea").focus(); await page.keyboard.press("Control+Shift+Backquote"); await waitCount("pty_open", 8); await page.keyboard.press("Control+Shift+Alt+Backquote"); await waitCount("pty_new_window", 2); await page.goto((process.env.SMOKE_URL || "http://127.0.0.1:1420") + "/?terminalWindow=C%3A%5Csmoke-workspace"); await waitCount("pty_open", 1); assert.equal(await count("workspace_open"), 0); assert.equal(await page.getByRole("button", { name: "Close terminal window", exact: true }).count(), 1); assert.equal(await page.getByRole("navigation", { name: "Terminal list" }).count(), 0); await page.screenshot({ path: ".audit/terminal-window.png" }); assert.deepEqual(errors, []); console.log("PASS: split groups, profile selection, maximize/restore/hide, shell history, directory quoting, settings persistence, tasks, editor selection/file execution, window/dictation IPC, and narrow layout."); } catch (error) { console.log("errors", errors, "writes", await writes()); await page.screenshot({ path: ".audit/terminal-failure.png" }); throw error; } finally { await browser.close(); }