// @requires plugin:terminal plugin:table-formulas import { mockFeaturePlugins } from "../_harness/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"; 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 context = await browser.newContext({ viewport: { width: 1440, height: 1000 } }); await context.route('**/__settings-closed', route => route.fulfill({ contentType: 'text/html', body: 'Closed settings' })); const page = await context.newPage(); const settingsPage = await context.newPage(); page.setDefaultTimeout(10000); settingsPage.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)); settingsPage.on("pageerror", error => errors.push(error.message)); await page.context().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 === "settings_open_window") return; 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 }; }); await mockFeaturePlugins(page.context()); 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(); await page.locator('.live-writer').waitFor(); } const source = '# Formulas\n\n| A | B |\n| --- | --- |\n| 2 | =A2*3 |\n\nAfter.\n'; const table = page.locator('.live-writer table'); const dialog = settingsPage.getByRole('dialog', { name: 'Settings', exact: true }); const switchFor = name => dialog.getByRole('switch', { name, exact: true }); async function settings() { await page.getByRole('button', { name: 'Settings (Ctrl+,)', exact: true }).click(); await settingsPage.goto((process.env.SMOKE_URL || 'http://127.0.0.1:1420') + '/?settingsWindow=true'); await dialog.getByRole('button', { name: 'Plug-ins', exact: true }).click(); } async function closeSettings() { await settingsPage.waitForFunction(() => [...document.querySelectorAll('input[role="switch"]')].every(input => !input.disabled)); await settingsPage.goto((process.env.SMOKE_URL || 'http://127.0.0.1:1420') + '/__settings-closed'); await page.bringToFront(); } const count = command => page.evaluate(command => window.__TEST__.calls.filter(call => call.command === command).length, command); try { await page.goto(process.env.SMOKE_URL || 'http://127.0.0.1:1420'); await page.getByText('smoke-workspace', { exact: true }).waitFor(); await page.evaluate(text => window.__TEST__.files.set('formulas.md', text), source); await quickOpen('formulas.md'); await table.waitFor(); assert.equal(await table.locator('.md-table-formula').innerText(), '6'); await settings(); await switchFor('Table formulas').uncheck(); await closeSettings(); await page.waitForFunction(() => !document.querySelector('.md-table-formula')); assert.match(await table.innerText(), /=A2\*3/); assert.equal(await page.locator('.table-formula-tools').count(), 0); await table.locator('td').last().click(); await page.locator('.writer-cell-editor').fill('=A2*4'); await page.keyboard.press('Enter'); await settings(); await switchFor('Table formulas').check(); await closeSettings(); await page.waitForFunction(() => document.querySelector('.md-table-formula')?.textContent === '8'); await page.getByRole('group', { name: 'Document view', exact: true }).getByRole('button', { pressed: true }).press('ArrowRight'); const preview = page.locator('.md-page table').first(); await preview.locator('.md-table-formula').waitFor(); await settings(); await switchFor('Table formulas').uncheck(); await closeSettings(); await page.waitForFunction(() => !document.querySelector('.md-page .md-table-formula')); assert.match(await preview.innerText(), /=A2\*4/); await settings(); await switchFor('Table formulas').check(); await closeSettings(); await preview.locator('.md-table-formula').waitFor(); assert.equal(await preview.locator('.md-table-formula').innerText(), '8'); await page.keyboard.press('Control+s'); await page.waitForFunction(() => window.__TEST__.files.get('formulas.md').includes('=A2*4')); assert(!await page.evaluate(() => window.__TEST__.files.get('formulas.md').includes('>8<'))); await page.getByRole('button', { name: 'Terminal', exact: true }).click(); await page.waitForFunction(() => window.__TEST__.calls.some(call => call.command === 'pty_open')); await settings(); await switchFor('Terminal').uncheck(); await page.waitForFunction(() => window.__TEST__.calls.some(call => call.command === 'pty_close')); await settingsPage.screenshot({ path: '.audit/plugins-settings.png' }); await closeSettings(); assert.equal(await page.getByRole('button', { name: 'Terminal', exact: true }).count(), 0); const opened = await count('pty_open'); await page.keyboard.press('Control+j'); assert.equal(await count('pty_open'), opened); await page.reload(); await page.getByText('smoke-workspace', { exact: true }).waitFor(); assert.equal(await page.getByRole('button', { name: 'Terminal', exact: true }).count(), 0); await settings(); assert(!await switchFor('Terminal').isChecked()); const standalone = await page.context().newPage(); await standalone.goto((process.env.SMOKE_URL || 'http://127.0.0.1:1420') + '?terminalWindow=C%3A%5Csmoke-workspace'); await standalone.getByText(/Install and enable the Terminal plugin/).waitFor(); await switchFor('Terminal').check(); await standalone.locator('.xterm').waitFor(); await standalone.waitForFunction(() => window.__TEST__.calls.some(call => call.command === 'pty_open')); await switchFor('Terminal').uncheck(); await standalone.getByText(/Install and enable the Terminal plugin/).waitFor(); await standalone.waitForFunction(() => window.__TEST__.calls.some(call => call.command === 'pty_close')); await standalone.close(); await switchFor('Terminal').check(); await closeSettings(); await page.getByRole('button', { name: 'Terminal', exact: true }).click(); await page.locator('.xterm').waitFor(); await page.evaluate(text => window.__TEST__.files.set('formulas.md', text.replace('=A2*3', '=A2*4')), source); await page.getByRole('button', { name: 'Terminal', exact: true }).focus(); await quickOpen('formulas.md'); await settings(); assert.equal(await dialog.getByRole('heading', { name: 'Bundled plugins' }).count(), 0); for (const name of ['Terminal', 'Table formulas']) { await settingsPage.waitForFunction(() => [...document.querySelectorAll('input[role="switch"]')].every(input => !input.disabled)); await dialog.locator('.card').filter({ has: settingsPage.getByRole('switch', { name, exact: true }) }).getByRole('button', { name: 'Uninstall', exact: true }).click(); const confirmation = settingsPage.getByRole('dialog', { name: `Uninstall ${name}?`, exact: true }); await confirmation.getByRole('button', { name: 'Uninstall and delete files', exact: true }).click(); await confirmation.waitFor({ state: 'hidden' }); await switchFor(name).waitFor({ state: 'detached' }); } await closeSettings(); await page.waitForFunction(() => !document.querySelector('.md-table-formula') && !document.querySelector('.xterm')); assert.equal(await page.getByRole('button', { name: 'Terminal', exact: true }).count(), 0); assert.match(await table.innerText(), /=A2\*4/); await page.reload(); await page.getByText('smoke-workspace', { exact: true }).waitFor(); await settings(); await dialog.getByText('No plugins installed.', { exact: true }).waitFor(); assert.deepEqual(errors, []); console.log('Plugin smoke passed: formulas, source preservation, terminal shutdown, persistence, shortcuts, cross-window enable controls, removal, and no automatic restoration.'); } catch (error) { await settingsPage.screenshot({ path: ".audit/plugins-settings-failure.png" }); console.log("Settings page:", settingsPage.url(), await settingsPage.locator('body').innerText()); await page.screenshot({ path: ".audit/plugins-failure.png" }); console.log(await page.locator('.md-page').allTextContents()); console.log(errors); throw error; } finally { await browser.close(); }