// Run against `pnpm dev`. PLAYWRIGHT_MODULE may point to a shared 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: 1440, height: 1000 } }); page.setDefaultTimeout(10000); const errors = []; 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_profiles") return [{ id: "powershell", title: "PowerShell", kind: "powershell" }]; 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 open(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(); } async function writer() { await page.locator('.cm-content').waitFor(); return page.evaluate(async () => { const { EditorView } = await import('/node_modules/@codemirror/view/dist/index.js'); window.__WRITER__ = EditorView.findFromDOM(document.querySelector('.cm-content')); return window.__WRITER__.state.sliceDoc(); }); } async function expectWriter(expected) { await writer(); try { await page.waitForFunction(text => window.__WRITER__.state.sliceDoc() === text, expected); } catch (error) { console.log({ expected, actual: await writer(), errors }); throw error; } } try { await page.goto(process.env.SMOKE_URL || 'http://127.0.0.1:1420'); await page.getByText('smoke-workspace', { exact: true }).waitFor(); await open('welcome.md'); const original = await writer(); await page.locator('.cm-content').click(); await page.keyboard.press('Control+End'); await page.keyboard.type(' first edit'); await expectWriter(original + ' first edit'); await page.keyboard.press('Control+s'); await open('second.md'); await writer(); await page.getByRole('tab', { name: 'welcome.md', exact: true }).click(); await expectWriter(original + ' first edit'); await page.keyboard.press('Control+z'); await expectWriter(original); await page.keyboard.press('Control+y'); await expectWriter(original + ' first edit'); console.log('PASS: saved Writer edit survives tab switch with undo and redo'); await page.evaluate(() => window.dispatchEvent(new Event('focus'))); await page.keyboard.press('Control+z'); await expectWriter(original); await page.keyboard.press('Control+y'); await expectWriter(original + ' first edit'); assert.equal(await page.evaluate(() => window.__WRITER__.state.selection.main.head), (original + ' first edit').length); console.log('PASS: Writer undo and redo survive window focus refresh and restore the caret'); await page.keyboard.press('Control+e'); 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(view => view.getDomNode()?.isConnected); editor.setPosition({ lineNumber: editor.getModel().getLineCount(), column: editor.getModel().getLineMaxColumn(editor.getModel().getLineCount()) }); editor.focus(); window.__IDE__ = editor; }); await page.keyboard.type(' second edit'); assert.equal(await page.evaluate(() => window.__IDE__.getDomNode()?.isConnected), true, 'Typing keeps the same IDE editor mounted'); assert.equal(await page.evaluate(() => window.__IDE__.getValue()), original + ' first edit second edit'); await page.keyboard.press('Control+e'); await expectWriter(original + ' first edit second edit'); await page.locator('.cm-content').click(); await page.keyboard.press('Control+z'); await expectWriter(original + ' first edit second'); await page.keyboard.press('Control+z'); await expectWriter(original + ' first edit'); await page.keyboard.press('Control+z'); await expectWriter(original); await page.keyboard.press('Control+y'); await expectWriter(original + ' first edit'); await page.keyboard.press('Control+y'); await expectWriter(original + ' first edit second'); await page.keyboard.press('Control+y'); await expectWriter(original + ' first edit second edit'); console.log('PASS: Writer undo includes edits from IDE mode'); const tableSource = '# Table\n\n| A | B |\n| --- | --- |\n| One | Two |\n\nTail.\n'; await page.evaluate(source => { window.__TEST__.files.set('table.md', source); window.__TEST__.emit('workspace-event', { root: window.__TEST__.root, kind: 'create', paths: ['table.md'] }); }, tableSource); await open('table.md'); await writer(); const cells = page.locator('.writer-table tbody td'); await cells.nth(0).click(); await page.keyboard.press('Control+a'); await page.keyboard.type('Alpha'); await cells.nth(1).click(); await page.keyboard.press('Control+a'); await page.keyboard.type('Beta'); await page.keyboard.press('Escape'); const editedTable = await writer(); assert.ok(editedTable.includes('Alpha') && editedTable.includes('Beta')); await page.getByRole('tab', { name: 'welcome.md', exact: true }).click(); await writer(); await page.getByRole('tab', { name: 'table.md', exact: true }).click(); await writer(); await page.keyboard.press('Control+z'); await expectWriter(editedTable.replace('Beta', 'Two')); await page.keyboard.press('Control+z'); await expectWriter(tableSource); await page.keyboard.press('Control+y'); await expectWriter(editedTable.replace('Beta', 'Two')); await page.keyboard.press('Control+y'); await expectWriter(editedTable); console.log('PASS: table cell typing stays grouped per cell across tab switches'); assert.deepEqual(errors, []); } finally { await browser.close(); }