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 context = await browser.newContext({ viewport: { width: 1200, height: 850 } }); const page = await context.newPage(); const errors = []; page.on('pageerror', error => errors.push(error.message)); await context.addInitScript(() => { window.isTauri = true; const root = 'C:\\host-todo-test'; const files = new Map([['a.md', '# Note\n\nOriginal text.'], ['.runestone/templates/Daily.md', '# Daily\n\n## Tasks'], ['.runestone/marks.json', JSON.stringify({ pins: ['a.md'], bookmarks: ['a.md'], order: { '': ['a.md'] } })]]); const state = { files, calls: [], exportResult: 'C:\\exports\\my-theme.json', exportError: '' }; window.__HOST_TEST__ = state; let sequence = 0; window.__TAURI_INTERNALS__ = { metadata: { currentWindow: { label: 'main' }, currentWebview: { label: 'main' } }, transformCallback: () => ++sequence, unregisterCallback: () => {}, invoke: async (command, args = {}) => { state.calls.push({ command, args }); if (command === 'plugins_snapshot') return { root: 'C:/plugins', plugins: [] }; if (command === 'workspace_last_path') return root; if (command === 'workspace_open') return { root, name: 'host-todo-test', watch_mode: 'native' }; if (command === 'workspace_read') { if (!files.has(args.rel)) throw new Error('Missing file'); return files.get(args.rel); } if (command === 'workspace_list') { const prefix = args.rel ? args.rel + '/' : ''; const entries = new Map(); for (const path of files.keys()) { if (!path.startsWith(prefix) || (!args.rel && path.startsWith('.runestone/'))) continue; const tail = path.slice(prefix.length), 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('Destination exists'); files.set(args.rel, ''); return; } if (command === 'workspace_write') { if (args.expected !== undefined && files.get(args.rel) !== args.expected) throw new Error('Changed on disk'); files.set(args.rel, args.contents); return; } if (command === 'workspace_rename') { if (files.has(args.to)) throw new Error('Destination exists'); if (!files.has(args.from)) throw new Error('Missing file'); files.set(args.to, files.get(args.from)); files.delete(args.from); return; } if (command === 'workspace_delete') { files.delete(args.rel); return; } if (command === 'workspace_search') return []; if (command === 'plugin:event|listen') return ++sequence; if (command === 'plugin:window|is_maximized') return false; if (command === 'plugin:dialog|message') return 'Ok'; if (command === 'theme_export') { if (state.exportError) throw new Error(state.exportError); return state.exportResult; } return null; }, }; window.__TAURI_EVENT_PLUGIN_INTERNALS__ = { unregisterListener: () => {} }; }); const origin = process.env.SMOKE_URL || 'http://127.0.0.1:5173'; const row = path => page.locator(`[data-tree-rel="${path}"]`); const item = name => page.getByRole('menuitem', { name, exact: true }); async function paneMenu() { await page.getByRole('navigation', { name: 'Workspace files' }).click({ button: 'right', position: { x: 90, y: 360 } }); } await mkdir('output/host-todo', { recursive: true }); try { await page.goto(origin); await row('a.md').waitFor(); await page.getByRole('button', { name: 'New file Ctrl N' }).click(); await page.getByRole('textbox', { name: 'New file path relative to the workspace' }).waitFor(); await page.mouse.click(5, 700); assert.equal(await page.locator('dialog').count(), 0, 'Outside click dismisses new-file dialog'); await page.getByRole('button', { name: 'Find a file Ctrl P' }).click(); await page.getByRole('textbox', { name: 'Find a file' }).click(); assert.equal(await page.locator('dialog').count(), 1, 'Inside click keeps file search open'); await page.mouse.click(5, 700); assert.equal(await page.locator('dialog').count(), 0, 'Outside click dismisses file search'); await row('a.md').click({ button: 'right' }); assert.equal(await item('Copy path').count(), 0); assert.equal(await item('Copy system path').count(), 1); await item('Rename').click(); await page.locator('.tree input').fill('renamed.md'); await page.locator('.tree input').press('Enter'); await row('renamed.md').waitFor(); await row('renamed.md').click({ button: 'right' }); assert.equal(await item('Undo rename').count(), 1); await page.keyboard.press('Escape'); await row('renamed.md').focus(); await page.keyboard.press('Control+z'); await row('a.md').waitFor(); assert.deepEqual(await page.evaluate(() => JSON.parse(window.__HOST_TEST__.files.get('.runestone/marks.json')).pins), ['a.md']); await row('a.md').focus(); await page.keyboard.press('Control+y'); await row('renamed.md').waitFor(); await paneMenu(); await item('New note').hover(); await item('Daily').click(); await row('Daily.md').waitFor(); assert.equal(await page.evaluate(() => window.__HOST_TEST__.files.get('Daily.md')), '# Daily\n\n## Tasks'); await paneMenu(); await item('New note').hover(); await item('Manage templates…').click(); await page.getByRole('button', { name: 'Rename Daily', exact: true }).click(); await page.getByRole('textbox', { name: 'Template name', exact: true }).fill('Weekly'); await page.getByRole('button', { name: 'Continue', exact: true }).click(); await page.getByRole('button', { name: 'Edit Weekly', exact: true }).waitFor(); await page.evaluate(() => window.__HOST_TEST__.files.set('.runestone/templates/Existing.md', '# Keep this template')); await page.getByRole('button', { name: 'Rename Weekly', exact: true }).click(); await page.getByRole('textbox', { name: 'Template name', exact: true }).fill('Existing'); await page.getByRole('button', { name: 'Continue', exact: true }).click(); await page.getByRole('alert').filter({ hasText: 'Destination exists' }).waitFor(); assert.equal(await page.evaluate(() => window.__HOST_TEST__.files.get('.runestone/templates/Existing.md')), '# Keep this template'); for (const mode of ['light', 'dark']) { await page.evaluate(async mode => { const { saveAppearance } = await import('/src/lib/theme/appearance.ts'); saveAppearance({ theme: 'default', mode }); }, mode); await page.screenshot({ path: `output/host-todo/templates-${mode}.png` }); } await page.getByRole('button', { name: 'Edit Weekly', exact: true }).click(); await page.getByRole('tab', { name: 'Weekly.md', exact: true }).waitFor(); await paneMenu(); await item('New note').hover(); await item('Manage templates…').click(); await page.getByRole('button', { name: 'New template', exact: true }).click(); await page.getByRole('textbox', { name: 'Template name', exact: true }).fill('Meeting'); await page.getByRole('button', { name: 'Continue', exact: true }).click(); await page.getByRole('tab', { name: 'Meeting.md', exact: true }).waitFor(); assert.equal(await page.evaluate(() => window.__HOST_TEST__.files.get('.runestone/templates/Meeting.md')), ''); await paneMenu(); await item('New note').hover(); await item('Manage templates…').click(); await page.getByRole('button', { name: 'Delete Weekly', exact: true }).click(); await page.getByRole('button', { name: 'Edit Weekly', exact: true }).waitFor({ state: 'detached' }); assert.equal(await page.evaluate(() => window.__HOST_TEST__.files.get('Daily.md')), '# Daily\n\n## Tasks', 'Deleting template preserves notes'); await page.getByRole('button', { name: 'Close templates', exact: true }).click(); await page.evaluate(() => { for (let i = 0; i < 40; i++) window.__HOST_TEST__.files.set(`.runestone/templates/Long template name ${i}.md`, '# Template'); }); await page.setViewportSize({ width: 600, height: 600 }); await paneMenu(); await item('New note').hover(); await item('Manage templates…').click(); assert.ok(await page.locator('dialog .list').evaluate(node => node.scrollHeight > node.clientHeight), 'Long template list scrolls'); await page.screenshot({ path: 'output/host-todo/templates-narrow-long.png' }); await page.getByRole('button', { name: 'Close templates', exact: true }).click(); await page.setViewportSize({ width: 1200, height: 850 }); const settings = await context.newPage(); settings.on('pageerror', error => errors.push(error.message)); await settings.goto(origin + '/?settingsWindow=true'); await settings.getByRole('button', { name: 'Appearance', exact: true }).click(); await settings.getByRole('button', { name: 'Export theme template' }).click(); await settings.getByRole('status').filter({ hasText: 'Theme saved to' }).waitFor(); assert.equal(await settings.evaluate(() => JSON.parse(window.__HOST_TEST__.calls.find(call => call.command === 'theme_export').args.contents).schema), 1); await settings.evaluate(() => window.__HOST_TEST__.exportResult = null); await settings.getByRole('button', { name: 'Export theme template' }).click(); await settings.getByText('Export canceled.', { exact: true }).waitFor(); await settings.evaluate(() => window.__HOST_TEST__.exportError = 'Disk is full'); await settings.getByRole('button', { name: 'Export theme template' }).click(); await settings.getByRole('alert').filter({ hasText: 'Disk is full' }).waitFor(); await settings.getByRole('button', { name: 'Color theme', exact: true }).click(); await settings.getByRole('menuitemradio', { name: 'RuneStone', exact: true }).focus(); await settings.keyboard.press('ArrowDown'); await settings.keyboard.press('Enter'); assert.equal(await settings.evaluate(() => document.documentElement.dataset.palette), 'claude'); assert.equal(await settings.locator('input[type=color]').count(), 0); await page.getByRole('button', { name: 'New tab (Ctrl T)', exact: true }).click(); await page.locator('.empty').waitFor(); await page.waitForFunction(() => document.documentElement.dataset.palette === 'claude'); assert.equal(await page.locator('.empty').evaluate(node => { const reference = document.createElement('span'); reference.style.color = 'var(--rs-bg)'; node.append(reference); const matches = getComputedStyle(node).backgroundColor === getComputedStyle(reference).color; reference.remove(); return matches; }), true); await settings.getByLabel('Background image file', { exact: true }).setInputFiles({ name: 'test.png', mimeType: 'image/png', buffer: Buffer.from('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+jM1sAAAAASUVORK5CYII=', 'base64') }); await page.waitForFunction(() => getComputedStyle(document.querySelector('.empty')).backgroundImage.includes('blob:')); await page.reload(); await page.locator('.empty').waitFor(); assert.ok(await page.locator('.empty').evaluate(node => getComputedStyle(node).backgroundImage.includes('blob:')), 'Background survives restart'); await settings.getByRole('button', { name: 'Remove image', exact: true }).click(); await page.waitForFunction(() => getComputedStyle(document.querySelector('.empty')).backgroundImage === 'none'); for (const mode of ['light', 'dark']) { await settings.evaluate(async mode => { const { saveAppearance, parseAppearance, APPEARANCE_KEY } = await import('/src/lib/theme/appearance.ts'); saveAppearance({ ...parseAppearance(localStorage.getItem(APPEARANCE_KEY)), theme: 'default', mode }); window.dispatchEvent(new Event('focus')); }, mode); await settings.setViewportSize({ width: 650, height: 760 }); await settings.screenshot({ path: `output/host-todo/appearance-${mode}-narrow.png` }); } assert.equal(await settings.getByRole('button', { name: 'Reset background', exact: true }).isDisabled(), true); await page.waitForFunction(() => !JSON.parse(localStorage.getItem('runestone.appearance')).newTabBackground); assert.deepEqual(errors, []); console.log('Host To Do: dialog dismissal, rename undo/redo with marks, template creation/rename/edit/delete, theme export success/cancel/error, shared theme menu, background persistence/reset passed.'); } finally { await browser.close(); }