// Run against `pnpm dev`. PLAYWRIGHT_MODULE may point to a shared installation. import { createRequire } from "node:module"; import assert from "node:assert/strict"; import { readFileSync } from "node:fs"; 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 }, locale: 'en-US', timezoneId: 'America/Vancouver' }); const page = await context.newPage(); const settingsPage = await context.newPage(); settingsPage.setDefaultTimeout(10000); await context.exposeBinding("settingsHostInvoke", (_source, command, args) => page.evaluate(({ command, args }) => window.__TAURI_INTERNALS__.invoke(command, args), { command, args })); 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)); settingsPage.on("pageerror", error => errors.push(error.message)); const manifest = JSON.parse(readFileSync('fixtures/plugins/workspace-overview/plugin.json', 'utf8')); const pluginCode = readFileSync('fixtures/plugins/workspace-overview/main.js', 'utf8') + '\n// Browser cleanup probe\n'; await page.context().addInitScript(({ manifest, pluginCode }) => { 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 = []; let installed = []; let removalPrepared = false; const data = { preserved: true }; const path = "C:\\AppData\\RuneStone\\plugins\\" + manifest.id; const snapshot = () => ({ root: "C:\\AppData\\RuneStone\\plugins", plugins: installed }); 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 = {}) => { if (location.search.includes("settingsWindow") && !command.startsWith("plugin:event|") && !command.startsWith("plugin:window|")) return window.settingsHostInvoke(command, args); calls.push({ command, args }); if (command === "settings_open_window") return; if (command === "plugin:window|is_maximized") return false; if (command.startsWith("plugin:window|")) return null; if (command === "plugins_snapshot") return snapshot(); if (command === "plugins_choose") return [{ token: 'candidate', source: 'C:/Downloads/overview.zip', manifest, error: null }]; if (command === "plugins_from_git") { if (args.tag === 'missing') throw new Error('Release not found. Publish a release with one plugin ZIP.'); return [{ token: 'git-candidate', source: args.url + ' @ ' + (args.tag || 'v1.0.0'), manifest, error: null }]; } if (command === "plugins_cancel") { removalPrepared = false; return; } if (command === "plugins_install") { installed = [{ manifest, hash: 'reviewed-hash', enabled: args.enabled, installedAt: Date.parse('2026-10-05T18:24:36Z'), path, error: null, removal: false }]; emit('plugins-changed', null); return [{ name: manifest.name, error: null }]; } if (command === "plugins_set_enabled") { installed[0].enabled = args.enabled; emit('plugins-changed', null); return; } if (command === "plugins_load") { window.__TEST__.cleaned = false; window.__TEST__.released = false; return pluginCode.replace('export function activate(api) {', 'export function activate(api) { api.onDispose(() => { window.__TEST__.cleaned = true; });'); } if (command === "plugins_release") { window.__TEST__.released = true; return; } if (command === "plugins_data") return JSON.stringify(data); if (command === "plugins_reveal") return; if (command === "plugins_prepare_remove") { removalPrepared = true; return { token: 'remove-plan', plugin: installed[0] }; } if (command === "plugins_remove") { if (!removalPrepared) throw Error('Missing removal plan'); removalPrepared = false; installed[0].enabled = false; emit('plugins-changed', null); for (let i=0; i<100 && !window.__TEST__.released; i++) await new Promise(resolve => setTimeout(resolve, 50)); if (!window.__TEST__.cleaned || !window.__TEST__.released) throw Error('Cleanup was not acknowledged'); installed = []; emit('plugins-changed', null); 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, data }; }, { manifest, pluginCode }); 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 settingsDialog = settingsPage.getByRole('dialog', { name: 'Settings', exact: true }); const count = command => page.evaluate(command => window.__TEST__.calls.filter(call => call.command === command).length, command); 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 settingsDialog.getByRole('button', { name: 'Plug-ins', exact: true }).click(); } async function uninstall() { await settingsDialog.locator('.card').filter({ hasText: 'Workspace overview' }).getByRole('button', { name: 'Uninstall', exact: true }).click(); } try { await page.goto(process.env.SMOKE_URL || 'http://127.0.0.1:1420'); await page.getByText('smoke-workspace', { exact: true }).waitFor(); await settings(); assert.equal(await settingsDialog.getByRole('button', { name: 'Install from folder', exact: true }).count(), 0); assert.equal(await settingsDialog.getByRole('button', { name: 'Install from file', exact: true }).count(), 0); await settingsDialog.getByRole('button', { name: 'Install plugin ZIP', exact: true }).click(); let review = settingsPage.getByRole('dialog', { name: 'Review plugin installation', exact: true }); await review.getByText('Source: C:/Downloads/overview.zip', { exact: true }).waitFor(); await review.getByRole('button', { name: 'Cancel', exact: true }).click(); assert.equal(await count('plugins_install'), 0); const gitButton = settingsDialog.getByRole('button', { name: 'Install from Git link', exact: true }); await gitButton.click(); const gitDialog = settingsPage.getByRole('dialog', { name: 'Install from Git link', exact: true }); await gitDialog.getByLabel('Repository link', { exact: true }).fill('https://git.example/owner/plugin.git'); await gitDialog.getByLabel('Release tag (optional)', { exact: true }).fill('missing'); await gitDialog.getByLabel('Access token (optional)', { exact: true }).fill('test-token'); await gitDialog.getByRole('button', { name: 'Review release', exact: true }).click(); await gitDialog.getByRole('alert').getByText(/Release not found/).waitFor(); assert.equal(await gitDialog.getByLabel('Access token (optional)', { exact: true }).inputValue(), ''); await gitDialog.getByLabel('Release tag (optional)', { exact: true }).fill('v1.0.0'); await gitDialog.getByRole('button', { name: 'Review release', exact: true }).click(); await review.getByText('Source: https://git.example/owner/plugin.git @ v1.0.0', { exact: true }).waitFor(); await review.getByRole('button', { name: 'Cancel', exact: true }).click(); assert.equal(await count('plugins_install'), 0); assert(await gitButton.evaluate(element => element === document.activeElement)); await settingsDialog.getByRole('button', { name: 'Install plugin ZIP', exact: true }).click(); await review.getByRole('button', { name: 'Install and enable', exact: true }).click(); await settingsDialog.getByRole('switch', { name: 'Workspace overview', exact: true }).waitFor(); assert(await settingsDialog.getByRole('switch', { name: 'Workspace overview', exact: true }).isChecked()); assert.equal(await settingsDialog.locator('time').getAttribute('datetime'), '2026-10-05T18:24:36.000Z'); assert.match(await settingsDialog.locator('time').innerText(), /Oct 5, 2026.*11:24:36 AM PDT/); await settingsPage.screenshot({ path: '.audit/plugin-manager-installed.png' }); await settingsPage.keyboard.press('Escape'); await page.evaluate(async pluginId => { const { runPluginCommand } = await import('/src/lib/plugins/external.ts'); await runPluginCommand(pluginId, 'show-overview'); }, manifest.id); const view = page.getByRole('dialog', { name: 'Workspace overview', exact: true }); await view.getByText(/files and folders in this workspace/).waitFor(); await view.getByRole('button', { name: 'Close plugin view', exact: true }).click(); await settings(); await uninstall(); const confirmation = settingsPage.getByRole('dialog', { name: 'Uninstall Workspace overview?', exact: true }); assert(await confirmation.getByRole('button', { name: 'Cancel', exact: true }).evaluate(el => el === document.activeElement)); assert.equal(await confirmation.getByRole('button', { name: 'Continue', exact: true }).count(), 0); await confirmation.getByRole('button', { name: 'Cancel', exact: true }).click(); assert.equal(await count('plugins_remove'), 0); assert(await settingsDialog.getByRole('switch', { name: 'Workspace overview', exact: true }).isChecked()); await uninstall(); await confirmation.waitFor(); await settingsPage.keyboard.press('Escape'); assert(await settingsDialog.isVisible()); assert(await settingsDialog.getByRole('switch', { name: 'Workspace overview', exact: true }).isChecked()); assert.equal(await count('plugins_remove'), 0); await uninstall(); await confirmation.waitFor(); await settingsPage.screenshot({ path: '.audit/plugin-uninstall-confirmation.png' }); await confirmation.getByRole('button', { name: 'Uninstall and delete files', exact: true }).click(); await settingsDialog.getByText('No plugins installed.', { exact: true }).waitFor(); assert.equal(await count('plugins_remove'), 1); assert.equal(await page.evaluate(() => window.__TEST__.cleaned), true); assert.equal(await page.evaluate(() => window.__TEST__.data.preserved), true); await settingsPage.keyboard.press('Escape'); assert.equal(await page.getByRole('button', { name: 'Plugin commands and views', exact: true }).count(), 0); await settingsPage.setViewportSize({ width: 590, height: 820 }); await settings(); await settingsPage.screenshot({ path: '.audit/plugin-manager-narrow.png' }); assert.equal(await settingsDialog.evaluate(element => element.scrollWidth > element.clientWidth), false); await gitButton.click(); await gitDialog.getByLabel('Repository link', { exact: true }).fill('https://git.example/owner/plugin.git'); await settingsPage.screenshot({ path: '.audit/plugin-git-narrow.png' }); assert.equal(await gitDialog.evaluate(element => element.scrollWidth > element.clientWidth), false); await gitDialog.getByRole('button', { name: 'Review release', exact: true }).click(); await review.getByRole('button', { name: 'Install', exact: true }).click(); await settingsDialog.getByRole('switch', { name: 'Workspace overview', exact: true }).waitFor(); assert.equal(await settingsDialog.getByRole('switch', { name: 'Workspace overview', exact: true }).isChecked(), false); assert.equal(await count('plugins_from_git'), 3); assert.deepEqual(errors, []); console.log('External plugin smoke passed: ZIP and Git-link review, download errors, token clearing, disabled installation, code loading, cancellation, focus, cleanup, and uninstall.'); } finally { await browser.close(); }