import { createRequire } from "node:module"; import assert from "node:assert/strict"; import { mkdir } 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(15000); const errors = []; page.on("pageerror", error => errors.push(error.message)); await mkdir(".audit", { recursive: true }); await page.addInitScript(() => { window.isTauri = true; const root = "C:\\tab-menu-workspace"; const files = new Map(["a.md", "b.md", "c.md", "d.md"].map(name => [name, `# ${name}\n\nOriginal text.\n\n\`\`\`js\nconst answer = 42;\n\`\`\`\n\n| Name | Value |\n| --- | --- |\n| Alpha | One |\n`])); files.set("a.md", "# Links\n\nSelected reading text.\n\n[Web label](https://example.com/path)\n\n[[b.md|Workspace note]]\n\n| Link | Note |\n| --- | --- |\n| [Cell web](https://example.com/cell) | [[b.md\\|Cell note]] |\n"); files.set("sample.txt", "const answer = 42;\n"); window.tabMenuTest = { prompts: [], answer: "Cancel", revealed: null, clipboard: null, opened: null, app: null, windows: [] }; Object.defineProperty(navigator, "clipboard", { value: { writeText: async text => { window.tabMenuTest.clipboard = text; } } }); window.tabMenuTest.files = files; let sequence = 0; const callbacks = new Map(), listeners = new Map(); window.spellingTest = { commands: [], emit: payload => callbacks.get(listeners.get("spelling-menu"))?.({ event: "spelling-menu", id: 1, payload }) }; window.__TAURI_INTERNALS__ = { metadata: { currentWindow: { label: "main" }, currentWebview: { label: "main" } }, transformCallback: callback => { callbacks.set(++sequence, callback); return sequence; }, unregisterCallback: id => callbacks.delete(id), invoke: async (command, args = {}) => { if (command === "plugin:event|listen") { listeners.set(args.event, args.handler); return ++sequence; } if (command === "plugin:event|unlisten") return; if (command.startsWith("spelling_menu_")) { window.spellingTest.commands.push({ method: command, ...args }); return; } if (command === "spelling_suggestions") return { language: "en-US", words: ["Corrected"], misspelled: args.word === "Original" }; if (command === "workspace_last_path") return root; if (command === "workspace_open") return { root, name: "tab-menu-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_list") return [...(!args.rel ? [{ name: "notes", rel_path: "notes", kind: "folder" }] : []), ...[...files.keys()].filter(path => path.split("/").slice(0, -1).join("/") === (args.rel || "")).map(path => ({ name: path.split("/").at(-1), rel_path: path, kind: "file" }))]; if (command === "workspace_search") { window.tabMenuTest.searched = args.query; return []; } if (command === "plugin:opener|open_url") { window.tabMenuTest.opened = args.url; return; } if (command === "workspace_open_default") { window.tabMenuTest.app = args.rel; return; } if (command === "workspace_open_window") { window.tabMenuTest.windows.push(args); return; } if (command === "workspace_write") { files.set(args.rel, args.contents); return; } if (command === "workspace_rename") { files.set(args.to, files.get(args.from)); files.delete(args.from); return; } if (command === "workspace_abs_path") return `${root}\\${args.rel}`; if (command === "workspace_reveal") { window.tabMenuTest.revealed = args.rel; return; } if (command === "plugin:dialog|message") { window.tabMenuTest.prompts.push(args.message); return window.tabMenuTest.answer; } throw new Error(`Unhandled IPC: ${command}`); }, }; window.__TAURI_EVENT_PLUGIN_INTERNALS__ = { unregisterListener: () => {} }; }); const menu = name => page.getByRole('menu', { name, exact: true }); const item = name => page.getByRole('menuitem', { name, exact: true }); const output = '.audit/editor-link-menus'; await mkdir(output, { recursive: true }); const dismiss = async () => { await page.keyboard.press('Escape'); await page.locator('.rs-menu').waitFor({ state: 'detached' }); }; const report = []; async function writerMenu(target='Web label') { await page.locator('.cm-content').getByText('Selected reading text.', { exact: true }).click(); await page.locator('.cm-content').getByText(target, { exact: true }).click({ button: 'right' }); await menu('Markdown text actions').waitFor(); } try { await page.goto(process.env.SMOKE_URL || 'http://127.0.0.1:1420'); await page.locator('[data-tree-rel="a.md"]').click(); await page.locator('.cm-content').waitFor(); await page.evaluate(async () => { const { pluginHost } = await import('/src/lib/plugins/registry.ts'); pluginHost.register('link-smoke', 'browser', { open: async target => { window.tabMenuTest.browser = target; return true; }, mountSettings: () => () => {}, mountBookmarks: () => () => {} }); }); for (const theme of ['dark', 'light']) { await page.evaluate(async mode => { const { saveAppearance } = await import('/src/lib/theme/appearance.ts'); saveAppearance({theme:'default', mode}); }, theme); await writerMenu(); for (const label of ['Open link in app browser', 'Open link in default browser', 'Copy URL', 'Edit link']) assert.equal(await item(label).count(), 1); await page.screenshot({ path: `${output}/writer-link-${theme}.png` }); await item('Copy URL').click(); await page.waitForFunction(() => window.tabMenuTest.clipboard === 'https://example.com/path'); await writerMenu(); await item('Open link in default browser').click(); await page.waitForFunction(() => window.tabMenuTest.opened === 'https://example.com/path'); await writerMenu(); await item('Open link in app browser').click(); await page.waitForFunction(() => window.tabMenuTest.browser === 'https://example.com/path'); await writerMenu(); await item('Edit link').click(); assert.equal(await page.evaluate(async () => { const { EditorView } = await import('/node_modules/@codemirror/view/dist/index.js'); const v = EditorView.findFromDOM(document.querySelector('.cm-content')); return v.state.sliceDoc(v.state.selection.main.from, v.state.selection.main.to); }), 'https://example.com/path'); await page.screenshot({ path: `${output}/edit-url-${theme}.png` }); await page.keyboard.type('https://changed.example'); await page.keyboard.press('Control+z'); await page.locator('.cm-content').getByText('Selected reading text.', {exact:true}).click(); await writerMenu('Workspace note'); for (const label of ['Open in new tab', 'Open in new window', 'Copy path', 'Bookmark', 'Show in system explorer', 'Reveal file in navigation', 'Edit link']) assert.equal(await item(label).count(), 1); for (const label of ['Rename…', 'Move file to…', 'Open in default app']) assert.equal(await item(label).count(), 0); await page.screenshot({ path: `${output}/workspace-link-${theme}.png` }); await dismiss(); await page.getByRole('button',{name:'Files',exact:true}).click(); await page.getByRole('button',{name:'Hide outline',exact:true}).click(); await page.setViewportSize({width:560,height:620}); await writerMenu('Workspace note'); await page.keyboard.press('End'); assert.match(await page.evaluate(() => document.activeElement.textContent), /Select all/); await page.screenshot({path:`${output}/workspace-narrow-${theme}.png`}); await dismiss(); await page.setViewportSize({width:1440,height:1000}); await page.getByRole('button',{name:'Files',exact:true}).click(); await page.getByRole('button',{name:'Show outline',exact:true}).click(); await writerMenu('Workspace note'); await item('Copy path').focus(); await page.keyboard.press('ArrowRight'); await item('Workspace path').click(); await page.waitForFunction(() => window.tabMenuTest.clipboard === 'b.md'); await writerMenu('Workspace note'); await item('Show in system explorer').click(); await page.waitForFunction(() => window.tabMenuTest.revealed === 'b.md'); await writerMenu('Workspace note'); await item('Reveal file in navigation').click(); await page.waitForFunction(() => document.activeElement?.getAttribute('data-tree-rel') === 'b.md'); await page.getByRole('group', { name: 'Document view', exact: true }).hover(); await page.getByRole('button', { name:'Read', exact:true }).click(); const reading = page.locator('.reading'); await reading.getByText('Selected reading text.',{exact:true}).evaluate(node => { const range = document.createRange(); range.selectNodeContents(node); const s = getSelection(); s.removeAllRanges(); s.addRange(range); node.closest('.reading').focus(); }); await page.keyboard.press('Shift+F10'); await menu('Reading actions').waitFor(); await page.keyboard.press('End'); assert.match(await page.evaluate(() => document.activeElement.textContent), /^Search for/); await page.screenshot({ path: `${output}/reading-selection-${theme}.png` }); await page.keyboard.press('Home'); await page.keyboard.press('Enter'); await page.waitForFunction(() => window.tabMenuTest.clipboard === 'Selected reading text.'); await page.keyboard.press('Shift+F10'); await page.getByRole('menuitem', {name:/^Search for/}).click(); await page.waitForFunction(() => window.tabMenuTest.searched === 'Selected reading text.'); await page.evaluate(() => getSelection().removeAllRanges()); await reading.getByRole('link',{name:'Web label'}).click({button:'right'}); await item('Copy URL').click(); await page.waitForFunction(() => window.tabMenuTest.clipboard === 'https://example.com/path'); await page.getByRole('button', { name: 'Search', exact: true }).click(); await page.getByRole('button', { name: 'Hide outline', exact: true }).click(); await page.setViewportSize({ width: 560, height: 620 }); await reading.getByRole('link',{name:'Web label'}).click({button:'right'}); const rect=await menu('Reading actions').boundingBox(); assert.ok(rect.x>=0 && rect.x+rect.width<=560 && rect.y+rect.height<=620); await page.screenshot({ path: `${output}/reading-narrow-${theme}.png` }); await dismiss(); assert.equal(await reading.evaluate(node => node.contains(document.activeElement)), true); await page.setViewportSize({width:1440,height:1000}); await page.getByRole('button', { name: 'Files', exact: true }).click(); await page.getByRole('button', { name: 'Show outline', exact: true }).click(); await page.getByRole('group', { name: 'Document view', exact: true }).hover(); await page.getByRole('button',{name:'Edit',exact:true}).click(); await page.getByRole('group', { name:'Editor layout',exact:true }).hover(); await page.getByRole('button',{name:'IDE',exact:true}).click(); await page.locator('.monaco-editor').waitFor(); await page.evaluate(async () => { const {monaco}=await import('/src/lib/editor/monacoEnv.ts'); const editor=monaco.editor.getEditors()[0]; const model=editor.getModel(); const pos=model.getPositionAt(model.getValue().indexOf('Web label')+2); editor.setPosition(pos); editor.focus(); }); await page.keyboard.press('Shift+F10'); await item('Edit link').click(); assert.equal(await page.evaluate(async () => { const {monaco}=await import('/src/lib/editor/monacoEnv.ts'); const editor=monaco.editor.getEditors()[0]; return editor.getModel().getValueInRange(editor.getSelection()); }), 'https://example.com/path'); await page.screenshot({ path: `${output}/ide-edit-url-${theme}.png` }); await page.getByRole('group', { name:'Editor layout',exact:true }).hover(); await page.getByRole('button',{name:'Writer',exact:true}).click(); await page.locator('.cm-content').getByText('Selected reading text.', {exact:true}).click(); const cellLink = page.locator('.writer-table a').filter({hasText:'Cell web'}); await cellLink.click({button:'right'}); await item('Edit link').waitFor(); await page.screenshot({path:`${output}/table-link-${theme}.png`}); await item('Copy URL').click(); await page.waitForFunction(() => window.tabMenuTest.clipboard === 'https://example.com/cell'); await cellLink.click({button:'right'}); await item('Edit link').click(); const sourceCell = page.locator('.writer-table .is-source-editing .writer-cell-editor'); await sourceCell.waitFor(); assert.equal(await page.getByLabel('Edit link target').count(), 0); assert.equal(await page.evaluate(() => getSelection().toString()), 'https://example.com/cell'); assert.equal(await sourceCell.locator('.writer-link-target').innerText(), 'https://example.com/cell'); await page.screenshot({path:`${output}/table-edit-${theme}.png`}); await page.keyboard.type('https://changed.example/cell'); await page.keyboard.press('Enter'); await page.waitForFunction(() => document.querySelector('.writer-table a')?.getAttribute('href') === 'https://changed.example/cell'); await page.locator('.cm-content').getByText('Selected reading text.', {exact:true}).click(); await page.keyboard.press('Control+z'); await page.waitForFunction(() => document.querySelector('.writer-table a')?.getAttribute('href') === 'https://example.com/cell'); const cellNote = page.locator('.writer-table a').filter({hasText:'Cell note'}); await cellNote.click({button:'right'}); await item('Edit link').click(); assert.equal(await page.evaluate(() => getSelection().toString()), 'b.md'); await page.keyboard.type('c.md'); await page.keyboard.press('Enter'); await page.waitForFunction(() => [...document.querySelectorAll('.writer-table a')].some(node => node.textContent === 'Cell note' && node.getAttribute('href') === 'c.md')); await page.locator('.cm-content').getByText('Selected reading text.', {exact:true}).click(); await page.keyboard.press('Control+z'); await page.waitForFunction(() => [...document.querySelectorAll('.writer-table a')].some(node => node.textContent === 'Cell note' && node.getAttribute('href') === 'b.md')); await cellLink.click({button:'right'}); await item('Edit link').click(); await page.keyboard.type('https://escape.example'); await page.keyboard.press('Escape'); assert.equal(await sourceCell.count(), 0); assert.equal(await cellLink.getAttribute('href'), 'https://escape.example'); await page.locator('.cm-content').getByText('Selected reading text.', {exact:true}).click(); await page.keyboard.press('Control+z'); await page.waitForFunction(() => document.querySelector('.writer-table a')?.getAttribute('href') === 'https://example.com/cell'); await page.getByRole('button',{name:'Files',exact:true}).click(); await page.getByRole('button',{name:'Hide outline',exact:true}).click(); await page.setViewportSize({width:560,height:620}); await cellLink.click(); await page.keyboard.press('Shift+F10'); await item('Edit link').waitFor(); await page.screenshot({path:`${output}/table-narrow-${theme}.png`}); await dismiss(); await page.setViewportSize({width:1440,height:1000}); await page.getByRole('button',{name:'Files',exact:true}).click(); await page.getByRole('button',{name:'Show outline',exact:true}).click(); await page.locator('.cm-content').getByText('Selected reading text.', {exact:true}).click(); report.push({theme,passed:['Writer and IDE edit URL selection','browser destinations','workspace file actions','Reader copy/search/link actions','inline table web/wiki link edits and undo','table source Escape and keyboard menu','keyboard focus return','narrow viewport'],nativeDesktop:'not tested'}); } await writerMenu('Workspace note'); await item('Open in new window').click(); await page.waitForFunction(() => window.tabMenuTest.windows.length === 1); await page.evaluate(() => { const {transfer}=window.tabMenuTest.windows[0]; dispatchEvent(new StorageEvent('storage',{key:`runestone.tab-transfer.${transfer}.accepted`,newValue:'true'})); }); assert.equal(await page.getByRole('tab',{name:/b.md/}).count(),1,'Opening a linked document in a new window keeps the source tab'); await page.locator('[data-tree-rel="a.md"]').click(); await writerMenu('Workspace note'); await item('Bookmark').click(); await page.waitForFunction(() => JSON.parse(window.tabMenuTest.files.get('.runestone/marks.json') || '{}').bookmarks?.includes('b.md')); await writerMenu('Workspace note'); await item('Copy path').focus(); await page.keyboard.press('ArrowRight'); await item('System path').click(); await page.waitForFunction(() => window.tabMenuTest.clipboard.endsWith('\\b.md')); await page.getByRole('group', { name: 'Document view', exact: true }).hover(); await page.getByRole('button', { name:'Read', exact:true }).click(); const readerWorkspaceMenu = async () => { await page.locator('.reading').getByRole('link',{name:'Workspace note'}).click({button:'right'}); }; await readerWorkspaceMenu(); await item('Open in default app').click(); await page.waitForFunction(() => window.tabMenuTest.app === 'b.md'); await readerWorkspaceMenu(); await item('Rename…').click(); await page.getByLabel('New file name').fill('renamed.md'); await page.getByRole('button',{name:'Continue',exact:true}).click(); await page.waitForFunction(() => window.tabMenuTest.files.has('renamed.md') && !window.tabMenuTest.files.has('b.md')); assert.equal(await page.evaluate(() => JSON.parse(window.tabMenuTest.files.get('.runestone/marks.json')).bookmarks.includes('renamed.md')),true); await page.getByRole('group', { name: 'Document view', exact: true }).hover(); await page.getByRole('button', { name:'Edit', exact:true }).click(); await page.locator('.cm-content').evaluate(async node => { const {EditorView}=await import('/node_modules/@codemirror/view/dist/index.js'); const view=EditorView.findFromDOM(node); const from=view.state.doc.toString().indexOf('b.md'); view.dispatch({changes:{from,to:from+4,insert:'renamed.md'}}); }); await page.getByRole('group', { name: 'Document view', exact: true }).hover(); await page.getByRole('button', { name:'Read', exact:true }).click(); await readerWorkspaceMenu(); await item('Move file to…').click(); await page.getByLabel('Move file to (workspace path, including filename)').fill('notes/moved.md'); await page.getByRole('button',{name:'Continue',exact:true}).click(); await page.waitForFunction(() => window.tabMenuTest.files.has('notes/moved.md') && !window.tabMenuTest.files.has('renamed.md')); assert.equal(await page.evaluate(() => JSON.parse(window.tabMenuTest.files.get('.runestone/marks.json')).bookmarks.includes('notes/moved.md')),true); assert.deepEqual(errors,[]); console.log(JSON.stringify(report,null,2)); } catch(error) { await page.screenshot({path:`${output}/failure.png`}); console.log(await page.locator('.rs-menu').allTextContents()); throw error; } finally { await browser.close(); }