Files
RuneStone/tests/smoke/plugins/lifecycle.smoke.mjs
T
Knotty Wyvern 4d09da30d9 Support the migrated plugins in the host tests
- The pty_open mocks in layout/workbench and plugins/lifecycle send
  output through the channel that api.pty passes. Without a channel they
  keep the old events, so released Terminal versions still pass.
- plugins/gitToolbar resolves the Git plugin's "@runestone/plugin-sdk"
  imports to this repository's SDK build. The smoke runner builds the
  SDK when it is missing.
- The menu audit no longer compares the Terminal plugin's copies of the
  host menu files. The plugin now uses host menus through the SDK.

Checked with the released plugins and with the migrated Terminal, Git,
and Project Management branches.
2026-10-28 03:11:07 -07:00

186 lines
11 KiB
JavaScript

// Feature lifecycle: the built-in Table formulas setting and the Terminal plug-in turn off, persist, reopen across windows, and uninstall without restoration.
// An old Table Formulas package stays inactive and shows that the feature is built in.
// @requires plugin:terminal
import assert from 'node:assert/strict';
import path from 'node:path';
import { mockTauri, openPage, outputDir, runSmoke, smokeUrl } from '../_harness/index.mjs';
import { mockFeaturePlugins, 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: '<!doctype html><title>Closed settings</title>' }));
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,
pty_profiles: () => [{ id: 'powershell', title: 'PowerShell', kind: 'powershell' }],
pty_open: async ({ id, channel }) => {
const data = new TextEncoder().encode('READY 🪨\r\n\x1b[6n');
// A Terminal plugin that uses api.pty passes a channel and gets raw bytes. Released versions listen for events.
if (channel) setTimeout(() => { channel.onmessage(data.slice(0, 8).buffer); channel.onmessage(data.slice(8).buffer); }, 0);
else {
test.emit('pty-data', { id, data: [...data.slice(0, 8)] });
test.emit('pty-data', { id, data: [...data.slice(8)] });
}
await new Promise(resolve => setTimeout(resolve, 50));
},
pty_write: () => null,
pty_resize: () => null,
pty_close: () => null,
};
},
});
await mockFeaturePlugins(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('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('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');
await switchFor('Terminal').uncheck();
await page.waitForFunction(ptyCalled, 'pty_close');
await settingsPage.screenshot({ path: path.join(output, '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 openPage(context);
await standalone.goto(smokeUrl + '?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(ptyCalled, 'pty_open');
await switchFor('Terminal').uncheck();
await standalone.getByText(/Install and enable the Terminal plugin/).waitFor();
await standalone.waitForFunction(ptyCalled, 'pty_close');
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);
for (const name of ['Terminal', 'Table Formulas']) {
await idle();
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('.xterm'));
assert.equal(await page.getByRole('button', { name: 'Terminal', exact: true }).count(), 0);
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 shutdown, persistence, shortcuts, cross-window enable controls, removal, and no automatic restoration.');
});