118 lines
8.8 KiB
JavaScript
118 lines
8.8 KiB
JavaScript
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 origin = process.env.SMOKE_URL || "http://127.0.0.1:1420";
|
|
const context = await browser.newContext({ viewport: { width: 1440, height: 900 } });
|
|
const errors = [];
|
|
await mkdir(".audit", { recursive: true });
|
|
await context.addInitScript(() => {
|
|
window.isTauri = true;
|
|
const callbacks = new Map(), listeners = new Map();
|
|
let sequence = 0;
|
|
const emit = (event, payload) => { for (const id of listeners.get(event) || []) callbacks.get(id)?.({ event, id, payload }); };
|
|
const manifest = (id, name) => ({ schemaVersion: 1, id, name, author: "Test", description: "Access test", version: "1.0.0", apiVersion: 1, entry: "main.js", access: "full-app" });
|
|
const plugins = [manifest("runestone.local-autocomplete", "Local Autocomplete"), manifest("runestone.browser", "Browser"), manifest("org.example.tasks", "Tasks")];
|
|
const modules = {
|
|
"runestone.local-autocomplete": `export function activate(api) {
|
|
window.accessTest = { mounts: 0, disposed: 0, settings: 0 };
|
|
api.settings.subscribe?.(() => { window.accessTest.settings++; });
|
|
api.registerView({ id: 'settings', title: 'Local Autocomplete', placement: 'editor', mount(container, context) {
|
|
window.accessTest.mounts++;
|
|
container.innerHTML = '<h2>Local Autocomplete</h2><label>Model<input aria-label="Model"></label><button>Save model</button><p class="mount-context"></p>';
|
|
container.querySelector('p').textContent = context?.surface;
|
|
container.querySelector('button').onclick = () => api.settings.write({ model: container.querySelector('input').value });
|
|
return () => { window.accessTest.disposed++; };
|
|
}});
|
|
api.registerCommand({ id: 'configure', title: 'Configure local autocomplete', run: () => api.openView('settings') });
|
|
api.registerCommand({ id: 'fail', title: 'Failed command', run: () => { throw Error('Command failed for test'); } });
|
|
}`,
|
|
"runestone.browser": `export function activate(api) {
|
|
api.registerCommand({ id: 'new-tab', title: 'New browser tab', run() {
|
|
const id = 'tab-' + Date.now(); api.registerView({ id, title: 'Browser instance', placement: 'editor', mount(container) { container.textContent = 'Browser content'; } }); api.openView(id);
|
|
}});
|
|
}`,
|
|
"org.example.tasks": `export function activate(api) {
|
|
api.registerView({ id: 'tasks', title: 'All tasks', icon: 'list', navigation: true, placement: 'editor', mount(container) { container.textContent = 'Task content'; } });
|
|
api.registerView({ id: 'dialog', title: 'Task summary', mount(container) { container.textContent = 'Task summary content'; } });
|
|
}`,
|
|
};
|
|
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 (command === 'plugin:event|listen') { const ids = listeners.get(args.event) || []; ids.push(args.handler); listeners.set(args.event, ids); return args.handler; }
|
|
if (command === 'plugin:event|unlisten') { callbacks.delete(args.eventId); return; }
|
|
if (command === 'workspace_last_path') return null;
|
|
if (command === 'plugins_snapshot') return { root: 'C:/test/plugins', plugins: plugins.map(manifest => ({ manifest, hash: 'test', enabled: !(manifest.id === 'runestone.local-autocomplete' && localStorage.getItem('disabled')), installedAt: 1, path: 'C:/test/plugins/' + manifest.id, error: null, removal: false })) };
|
|
if (command === 'plugins_load') return modules[args.id];
|
|
if (command === 'plugins_data') {
|
|
if (args.value !== null) { localStorage.setItem('settings:' + args.id, args.value); emit('plugin-settings-changed', args.id); }
|
|
return localStorage.getItem('settings:' + args.id) || 'null';
|
|
}
|
|
if (command === 'plugins_set_enabled') { if (!args.enabled) localStorage.setItem('disabled', 'true'); emit('plugins-changed', {}); return; }
|
|
if (command === 'plugins_release' || command === 'settings_open_window') return;
|
|
if (command.startsWith('plugin:window|')) return false;
|
|
throw new Error('Unhandled IPC: ' + command);
|
|
},
|
|
};
|
|
window.__TAURI_EVENT_PLUGIN_INTERNALS__ = { unregisterListener: (_event, id) => callbacks.delete(id) };
|
|
window.addEventListener('storage', event => { if (event.key?.startsWith('settings:')) emit('plugin-settings-changed', event.key.slice(9)); if (event.key === 'disabled') emit('plugins-changed', {}); });
|
|
});
|
|
const main = await context.newPage(), settings = await context.newPage();
|
|
for (const page of [main, settings]) { page.setDefaultTimeout(10000); page.on('pageerror', error => errors.push(error.message)); }
|
|
try {
|
|
await main.goto(origin);
|
|
const open = main.getByRole('button', { name: 'Open tab or view', exact: true });
|
|
await open.click();
|
|
const menu = main.getByRole('menu', { name: 'Open tab or view', exact: true });
|
|
await menu.waitFor();
|
|
assert.equal(await menu.getByRole('menuitem').count(), 3);
|
|
assert.equal(await menu.getByText('Local Autocomplete').count(), 0);
|
|
await open.click(); await menu.waitFor({ state: 'detached' });
|
|
await open.focus(); await main.keyboard.press('Enter'); await main.keyboard.press('End'); await main.keyboard.press('Escape');
|
|
assert(await open.evaluate(element => element === document.activeElement));
|
|
await open.click(); await menu.getByRole('menuitem', { name: /New browser tab/ }).click();
|
|
await main.getByText('Browser content', { exact: true }).waitFor();
|
|
await open.click(); assert.equal(await menu.getByRole('menuitem').count(), 3);
|
|
await menu.getByRole('menuitem', { name: /Task summary/ }).click();
|
|
await main.getByRole('dialog', { name: 'Task summary' }).waitFor();
|
|
await main.keyboard.press('Escape');
|
|
await open.click(); await main.screenshot({ path: '.audit/plugin-access-menu.png' }); await main.keyboard.press('Escape');
|
|
await main.evaluate(() => document.documentElement.dataset.theme = 'light');
|
|
await open.click(); await main.screenshot({ path: '.audit/plugin-access-menu-light.png' }); await main.keyboard.press('Escape');
|
|
await main.evaluate(() => document.documentElement.dataset.theme = 'dark');
|
|
await main.getByRole('button', { name: 'Commands', exact: true }).click();
|
|
await main.getByRole('menuitem', { name: /Failed command/ }).click();
|
|
await main.getByRole('alert').filter({ hasText: 'Command failed for test' }).waitFor();
|
|
await main.getByRole('button', { name: 'Dismiss', exact: true }).click();
|
|
await settings.goto(origin + '/?settingsWindow=true');
|
|
await settings.getByRole('button', { name: 'Local Autocomplete', exact: true }).click();
|
|
await settings.getByRole('textbox', { name: 'Model', exact: true }).fill('unsaved-model');
|
|
assert.equal(await settings.locator('.mount-context').innerText(), 'settings');
|
|
await settings.waitForTimeout(5500);
|
|
assert.equal(await settings.getByRole('textbox', { name: 'Model', exact: true }).inputValue(), 'unsaved-model');
|
|
assert.equal(await settings.evaluate(() => window.accessTest.mounts), 1);
|
|
await settings.getByRole('button', { name: 'Save model', exact: true }).click();
|
|
await main.waitForFunction(() => window.accessTest.settings === 1);
|
|
await settings.screenshot({ path: '.audit/plugin-access-settings.png' });
|
|
await settings.getByRole('button', { name: 'General', exact: true }).click();
|
|
await settings.waitForFunction(() => window.accessTest.disposed === 1);
|
|
await settings.getByRole('textbox', { name: 'Search settings' }).fill('Local');
|
|
await settings.getByRole('textbox', { name: 'Model', exact: true }).waitFor();
|
|
await settings.getByRole('textbox', { name: 'Search settings' }).fill('');
|
|
await settings.getByRole('button', { name: 'Plug-ins', exact: true }).click();
|
|
await settings.getByRole('switch', { name: 'Local Autocomplete', exact: true }).uncheck();
|
|
await settings.getByRole('button', { name: 'Local Autocomplete', exact: true }).waitFor({ state: 'detached' });
|
|
await main.getByRole('button', { name: 'Commands', exact: true }).waitFor({ state: 'detached' });
|
|
assert.deepEqual(errors, []);
|
|
console.log('Plugin access smoke passed: launchers, dynamic tabs, keyboard focus, dialog, settings search, stable forms, cross-window settings, disable cleanup.');
|
|
} catch (error) {
|
|
await settings.screenshot({ path: '.audit/plugin-access-failure.png' });
|
|
console.log(await settings.locator('body').innerText(), await settings.evaluate(() => window.accessTest), errors);
|
|
throw error;
|
|
} finally { await browser.close(); }
|