import test from "node:test"; import assert from "node:assert/strict"; import { readFileSync } from "node:fs"; import { pluginAccess } from "../../../src/lib/plugins/access.ts"; import { ExternalSession } from "../../../src/lib/plugins/externalSession.ts"; import type { PluginApi, PluginCommand, PluginView } from "../../../src/lib/plugins/sdk.ts"; const view = (id: string, extra: Partial = {}): PluginView => ({ id, title: id, mount() {}, ...extra }); const command = (id: string, extra: Partial = {}): PluginCommand => ({ id, title: id, run() {}, ...extra }); test("plugin access separates settings, launchers, and commands without reading titles", () => { const tools = pluginAccess("org.example.test", [command("open", { category: "open" }), command("configure")], [view("preferences", { settings: true }), view("tasks"), view("instance", { launcher: false })]); assert.deepEqual(tools.map(({ id, surface }) => [id, surface]), [["open", "open"], ["configure", "commands"], ["preferences", "settings"], ["tasks", "open"]]); }); test("installed RuneStone packages retain access before their next package update", () => { assert.equal(pluginAccess("runestone.local-autocomplete", [], [view("settings")])[0].surface, "settings"); assert.deepEqual(pluginAccess("runestone.browser", [command("new-tab")], [view("tab-123")]).map(({ id, surface }) => [id, surface]), [["new-tab", "open"]]); assert.equal(pluginAccess("org.example.test", [], [view("settings")])[0].surface, "open"); assert.equal(pluginAccess("runestone.local-autocomplete", [], [view("settings", { settings: false })])[0].surface, "open"); assert.equal(pluginAccess("runestone.browser", [command("new-tab", { category: "action" })], [])[0].surface, "commands"); }); test("command launchers retain the plugin action icon", () => { const tools = pluginAccess("runestone.git", [command("graph", { category: "open", icon: "git" })], []); assert.equal(tools[0].icon, "git"); assert.equal(tools[0].surface, "open"); }); test("settings listeners are removable and revoked before plugin cleanup", async () => { let api!: PluginApi, notify!: () => void; let received = 0, disposed = 0; const session = new ExternalSession("org.example.settings", { workspace: { list: async () => [], read: async () => "", write: async () => {} }, assets: { read: async () => new Uint8Array() }, settings: { read: async () => null, write: async () => {}, subscribe(listener) { notify = listener; return () => { disposed++; }; } }, }, () => {}, () => {}); await session.start({ activate(value) { api = value; } }); const off = api.settings.subscribe!(() => { received++; }); notify(); assert.equal(received, 1); await off(); await off(); assert.equal(disposed, 1); api.settings.subscribe!(() => { received++; }); await session.stop(); notify(); assert.equal(received, 1); assert.equal(disposed, 2); assert.throws(() => api.settings.subscribe!(() => {}), /disabled/); }); test("invalid access metadata fails registration", async () => { for (const activate of [ (api: PluginApi) => api.registerCommand(command("invalid", { category: "unknown" as "open" })), (api: PluginApi) => api.registerView(view("invalid", { settings: "yes" as unknown as boolean })), (api: PluginApi) => api.registerView(view("invalid", { launcher: "yes" as unknown as boolean })), ]) { const session = new ExternalSession("org.example.invalid", { workspace: { list: async () => [], read: async () => "", write: async () => {} }, assets: { read: async () => new Uint8Array() }, settings: { read: async () => null, write: async () => {} }, }, () => {}, () => {}); await assert.rejects(session.start({ activate }), /category|boolean/); await session.stop(); } }); test("settings windows permit model checks and cancellation", () => { const capability = JSON.parse(readFileSync(new URL("../../../src-tauri/capabilities/settings.json", import.meta.url), "utf8")); for (const permission of ["allow-completion-request", "allow-completion-cancel", "allow-plugins-data"]) assert(capability.permissions.includes(permission)); }); test("view mount context distinguishes Settings from editor and dialog views", async () => { const surfaces: string[] = []; const session = new ExternalSession("org.example.context", { workspace: { list: async () => [], read: async () => "", write: async () => {} }, assets: { read: async () => new Uint8Array() }, settings: { read: async () => null, write: async () => {} }, }, () => {}, () => {}); await session.start({ activate(api) { api.registerView(view("settings", { mount(_element, context) { surfaces.push(context!.surface); } })); } }); const target = { replaceChildren() {} } as unknown as HTMLElement; const disposeView = await session.mount("settings", target); await disposeView(); const disposeSettings = await session.mount("settings", target, undefined, "settings"); await disposeSettings(); await session.stop(); assert.deepEqual(surfaces, ["view", "settings"]); });