Files
2026-11-16 04:21:21 -07:00

116 lines
8.5 KiB
JavaScript

// Full-app plug-ins: compact header, navigation, dynamic and restored tabs, dialogs, settings forms across windows, and disable cleanup.
import assert from "node:assert/strict";
import path from "node:path";
import { mockTauri, openPage, outputDir, runSmoke, smokeUrl } from "../_harness/index.mjs";
await runSmoke("plugins/access", async ({ browser }) => {
const context = await browser.newContext({ viewport: { width: 1440, height: 900 } });
await mockTauri(context, {
commands: test => {
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("org.example.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'); } });
}`,
"org.example.browser": `export function activate(api) {
api.registerCommand({ id: 'new-tab', title: 'New browser tab', category: 'open', 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', launch() { api.openView('default-board'); }, mount(container) { container.textContent = 'Task content'; } });
api.registerView({ id: 'default-board', title: 'Default board', launcher: false, placement: 'editor', mount(container) { container.textContent = 'Default board content'; } });
window.taskTestApi = api;
api.registerView({ id: 'dialog', title: 'Task summary', mount(container) { container.textContent = 'Task summary content'; } });
}`,
};
// Settings and the enabled state live in localStorage, so both windows of the context share them.
window.addEventListener("storage", event => {
if (event.key?.startsWith("settings:")) test.emit("plugin-settings-changed", event.key.slice(9));
if (event.key === "disabled") test.emit("plugins-changed", {});
});
return {
browser_connect: () => "test-session",
workspace_last_path: () => null,
plugins_snapshot: () => ({ 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 })) }),
plugins_load: ({ id }) => modules[id],
plugins_data: ({ id, value }) => {
if (value !== null) { localStorage.setItem("settings:" + id, value); test.emit("plugin-settings-changed", id); }
return localStorage.getItem("settings:" + id) || "null";
},
plugins_set_enabled: ({ enabled }) => { if (!enabled) localStorage.setItem("disabled", "true"); test.emit("plugins-changed", {}); },
plugins_release: () => null,
"plugin:window|is_maximized": () => false,
};
},
});
const output = await outputDir("plugins/access");
const main = await openPage(context), settings = await openPage(context);
const external = page => page.evaluate(() => performance.getEntriesByType("resource").find(entry => new URL(entry.name).pathname === "/src/lib/plugins/external.ts").name);
try {
await main.goto(smokeUrl);
await main.getByRole("navigation", { name: "Plugin shortcuts" }).getByRole("button", { name: "All tasks", exact: true }).waitFor();
assert.equal(await main.getByRole("button", { name: "Open tab or view", exact: true }).count(), 0);
assert.equal(await main.getByRole("button", { name: "Commands", exact: true }).count(), 1);
await main.getByRole("navigation", { name: "Plugin shortcuts" }).getByRole("button", { name: "All tasks", exact: true }).click();
await main.getByText("Default board content", { exact: true }).waitFor();
await main.evaluate(() => window.taskTestApi.openView("tasks"));
await main.getByText("Task content", { exact: true }).waitFor();
const module = await external(main);
await main.evaluate(async module => {
const { closePluginEditor, pluginTabState, pluginView, restorePluginTab } = await import(module);
let view;
const off = pluginView.subscribe(value => { view = value; }); off();
const transfer = pluginTabState(view);
closePluginEditor(view.key);
if (!await restorePluginTab(transfer)) throw Error("Tab restore failed");
}, module);
await main.getByText("Task content", { exact: true }).waitFor();
await main.evaluate(async module => { await (await import(module)).runPluginCommand("org.example.browser", "new-tab"); }, module);
await main.getByText("Browser content", { exact: true }).waitFor();
await main.evaluate(async module => { (await import(module)).openPluginView("org.example.tasks", "dialog"); }, module);
await main.getByRole("dialog", { name: "Task summary" }).waitFor();
await main.keyboard.press("Escape");
for (const theme of ["dark", "light"]) {
await main.evaluate(theme => document.documentElement.dataset.theme = theme, theme);
await main.screenshot({ path: path.join(output, `plugin-access-${theme}.png`) });
}
await settings.goto(smokeUrl + "/?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: path.join(output, "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([...main.errors, ...settings.errors], []);
} catch (error) {
await settings.screenshot({ path: path.join(output, "plugin-access-failure.png") });
console.log(await settings.locator("body").innerText(), await settings.evaluate(() => window.accessTest), [...main.errors, ...settings.errors]);
throw error;
}
console.log("Plugin access smoke passed: compact header, navigation, dynamic tabs, dialog, settings search, stable forms, cross-window settings, disable cleanup.");
});