// Feature lifecycle: the built-in Table formulas setting and the native Terminal feature turn off, persist, and turn on again across windows. // Terminal off asks first while a shell runs, and then ends the shells of each window. // An old Table Formulas package stays inactive, shows that the feature is built in, and uninstalls without restoration. import assert from 'node:assert/strict'; import path from 'node:path'; import { mockTauri, openPage, outputDir, runSmoke, smokeUrl } from '../_harness/index.mjs'; import { mockInstalledPlugins, oldTableFormulasRow } from '../_harness/pluginMocks.mjs'; await runSmoke('plugins/lifecycle', async ({ browser }) => { 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 openPage(context); const settingsPage = await openPage(context); page.on('console', message => { if (message.type() === 'error' || message.type() === 'warning') console.log(message.type(), message.text()); }); await mockTauri(context, { commands: test => { const ws = test.workspace({ root: 'C:\\smoke-workspace', files: { '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 files = ws.files; test.files = files; return { ...ws, workspace_write: ({ rel, contents, expected }) => { if (expected !== undefined && files.get(rel) !== expected) throw new Error('Save blocked: file changed on disk'); files.set(rel, contents); test.emit('workspace-event', { root: ws.root, kind: 'modify', paths: [rel] }); }, settings_open_window: () => null, // Terminal off asks first while a shell runs (src/lib/layout/SettingsDialog.svelte). 'plugin:dialog|message': args => { test.state.prompts = [...test.state.prompts ?? [], args.message]; return 'Ok'; }, }; }, }); await mockInstalledPlugins(context, [oldTableFormulasRow]); const output = await outputDir('plugins/lifecycle'); 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 }); const ptyCalled = command => window.__TEST__.calls.some(call => call.command === command); const count = command => page.evaluate(command => window.__TEST__.calls.filter(call => call.command === command).length, command); 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(); } async function settings(category = 'Plug-ins') { await page.getByRole('button', { name: 'Settings (Ctrl+,)', exact: true }).click(); await settingsPage.goto(smokeUrl + '/?settingsWindow=true'); await dialog.getByRole('button', { name: category, exact: true }).click(); } // The switch of an old package of a built-in feature stays disabled. const idle = () => settingsPage.waitForFunction(() => [...document.querySelectorAll('input[role="switch"]')].every(input => !input.disabled || input.id === 'external-runestone.table-formulas')); async function closeSettings() { await idle(); await settingsPage.goto(smokeUrl + '/__settings-closed'); await page.bringToFront(); } try { await page.goto(smokeUrl); 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'); assert(!await page.evaluate(() => window.__TEST__.calls.some(call => call.command === 'plugins_load' && call.args.id === 'runestone.table-formulas')), 'The old Table Formulas package does not load'); await settings('General'); await switchFor('Table formulas').uncheck(); await closeSettings(); await page.waitForFunction(() => !document.querySelector('.md-table-formula')); assert.equal(await page.evaluate(() => window.__TEST__.settings('general').tableFormulas), false, 'The settings file stores the choice'); 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('General'); await switchFor('Table formulas').check(); await closeSettings(); await page.waitForFunction(() => document.querySelector('.md-table-formula')?.textContent === '8'); await page.getByRole('radiogroup', { name: 'View', exact: true }).getByRole('radio', { checked: true }).press('End'); const preview = page.locator('.md-page table').first(); await preview.locator('.md-table-formula').waitFor(); await settings('General'); 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('General'); 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(ptyCalled, 'pty_open'); await settings(); const oldPackage = dialog.locator('.card').filter({ has: settingsPage.getByRole('switch', { name: 'Table Formulas', exact: true }) }); await oldPackage.getByText('Included with RuneStone', { exact: true }).waitFor(); await oldPackage.getByText(/Table Formulas is now built in\. You can remove this old package\./).waitFor(); assert(await oldPackage.getByRole('switch').isDisabled(), 'The old package cannot be enabled'); assert.equal(await switchFor('Terminal').count(), 0, 'Terminal is a feature, not a plugin'); await dialog.getByRole('button', { name: 'Features', exact: true }).click(); await switchFor('Terminal').uncheck(); await page.waitForFunction(ptyCalled, 'pty_close'); const prompts = await settingsPage.evaluate(() => window.__TEST__.state.prompts); assert.deepEqual(prompts, ['Terminal sessions will end.\nTurn off Terminal?'], 'Terminal off asks first while a shell runs'); assert.equal(await settingsPage.evaluate(() => window.__TEST__.settings('features').terminal), false); await settingsPage.screenshot({ path: path.join(output, 'features-terminal-off.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'); await page.keyboard.press('Control+Shift+Backquote'); 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('Features'); assert(!await switchFor('Terminal').isChecked()); assert.equal(await dialog.getByRole('button', { name: 'Terminal', exact: true }).count(), 0, 'Settings → Terminal shows only while Terminal is on'); const standalone = await openPage(context); await standalone.goto(smokeUrl + '?terminalWindow=C%3A%5Csmoke-workspace'); await standalone.getByText('Terminal is off. Turn it on in Settings → Features.', { exact: true }).waitFor(); await switchFor('Terminal').check(); await standalone.locator('.xterm').waitFor(); await standalone.waitForFunction(ptyCalled, 'pty_open'); await dialog.getByRole('button', { name: 'Terminal', exact: true }).waitFor(); await switchFor('Terminal').uncheck(); await standalone.getByText('Terminal is off. Turn it on in Settings → Features.', { exact: true }).waitFor(); await standalone.waitForFunction(ptyCalled, 'pty_close'); assert.equal((await settingsPage.evaluate(() => window.__TEST__.state.prompts)).length, 1, 'The shell of the terminal window gives a prompt too'); page.errors.push(...standalone.errors); 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); await idle(); await dialog.locator('.card').filter({ has: settingsPage.getByRole('switch', { name: 'Table Formulas', exact: true }) }).getByRole('button', { name: 'Uninstall', exact: true }).click(); const confirmation = settingsPage.getByRole('dialog', { name: 'Uninstall Table Formulas?', exact: true }); await confirmation.getByRole('button', { name: 'Uninstall and delete files', exact: true }).click(); await confirmation.waitFor({ state: 'hidden' }); await switchFor('Table Formulas').waitFor({ state: 'detached' }); await closeSettings(); assert.equal(await page.locator('.xterm').count(), 1, 'The package removal does not touch the Terminal feature'); assert.equal(await table.locator('.md-table-formula').innerText(), '8', 'Removing the old package keeps the built-in formulas'); await page.reload(); await page.getByText('smoke-workspace', { exact: true }).waitFor(); await settings(); await dialog.getByText('No plugins installed.', { exact: true }).waitFor(); assert.deepEqual([...page.errors, ...settingsPage.errors], []); } catch (error) { await settingsPage.screenshot({ path: path.join(output, 'plugins-settings-failure.png') }); console.log('Settings page:', settingsPage.url(), await settingsPage.locator('body').innerText()); await page.screenshot({ path: path.join(output, 'plugins-failure.png') }); console.log(await page.locator('.md-page').allTextContents()); console.log([...page.errors, ...settingsPage.errors]); throw error; } console.log('Plugin smoke passed: formula setting, source preservation, inactive old package, Terminal off with a prompt, persistence, shortcuts, the terminal window, removal, and no automatic restoration.'); });