Files
RuneStone/tests/unit/plugins/externalPlugins.spec.ts
T
2026-11-03 00:23:07 -07:00

139 lines
7.7 KiB
TypeScript

import test from "node:test";
import assert from "node:assert/strict";
import { ExternalSession } from "../../../src/lib/plugins/externalSession.ts";
import type { PluginApi } from "../../../src/lib/plugins/sdk.ts";
function fixture() {
let writes = 0;
const session = new ExternalSession("org.test.demo", {
workspace: { list: async () => [], read: async () => "original", write: async () => { writes++; } },
settings: { read: async () => null, write: async () => {} },
assets: { read: async () => new Uint8Array() },
}, () => {}, () => {});
return { session, writes: () => writes };
}
test("native Markdown discovery remains optional and is revoked on disable", async () => {
let api!: PluginApi;
let scans = 0;
const session = new ExternalSession("org.test.scan", {
workspace: { list: async () => [], listMarkdown: async () => { scans++; return ["Notes/task.md"]; }, read: async () => "", write: async () => {} },
settings: { read: async () => null, write: async () => {} },
assets: { read: async () => new Uint8Array() },
}, () => {}, () => {});
await session.start({ activate(value) { api = value; } });
assert.deepEqual(await api.workspace.listMarkdown!(), ["Notes/task.md"]);
await session.stop();
assert.throws(() => api.workspace.listMarkdown!(), /disabled/);
assert.equal(scans, 1);
const old = fixture().session;
await old.start({ activate(value) { assert.equal(value.workspace.listMarkdown, undefined); } });
await old.stop();
});
test("external commands run and all host access stops before cleanup", async () => {
const { session, writes } = fixture(); let api!: PluginApi; let ran = 0;
await session.start({ activate(value) {
api = value;
api.registerCommand({ id: "run", title: "Run", run: () => { ran++; } });
api.registerView({ id: "view", title: "View", mount: () => {} });
api.onDispose(() => { assert(api.signal.aborted); assert.throws(() => api.workspace.read("x"), /disabled/); });
} });
await session.run("run"); assert.equal(ran, 1);
assert.throws(() => api.workspace.write("x", "new", undefined!), /original/);
await api.workspace.write("x", "new", "original"); assert.equal(writes(), 1);
await session.stop(); assert.equal(session.commands.size, 0); assert.equal(session.views.size, 0);
await assert.rejects(session.run("run"), /disabled/);
assert.throws(() => api.settings.read(), /disabled/);
});
test("async activation cleanup runs once even when disabled before activation finishes", async () => {
const { session } = fixture(); let resolve!: () => void; let cleaned = 0;
const gate = new Promise<void>(done => resolve = done);
const start = session.start({ async activate() { await gate; return () => { cleaned++; }; } });
await Promise.resolve();
const stopped = session.stop(); resolve();
await Promise.all([start, stopped]); await session.stop(); assert.equal(cleaned, 1);
});
test("failed cleanup does not skip other callbacks and blocks successful shutdown", async () => {
const { session } = fixture(); const calls: number[] = [];
await session.start({ activate(api) {
api.onDispose(() => { calls.push(1); });
api.onDispose(() => { calls.push(2); throw new Error("Cleanup failed"); });
api.onDispose(() => { calls.push(3); });
} });
await assert.rejects(session.stop(), /Cleanup failed/); assert.deepEqual(calls, [3, 2, 1]);
});
test("duplicate contribution IDs fail and partial activation can be cleaned", async () => {
const { session } = fixture(); let cleaned = false;
await assert.rejects(session.start({ activate(api) {
api.onDispose(() => { cleaned = true; });
api.registerCommand({ id: "run", title: "Run", run() {} });
api.registerCommand({ id: "run", title: "Again", run() {} });
} }), /duplicate/);
await session.stop(); assert(cleaned); assert.equal(session.commands.size, 0);
});
test("view disposal is idempotent and participates in plugin shutdown", async () => {
const { session } = fixture(); let cleaned = 0; let cleared = 0;
await session.start({ activate(api) { api.registerView({ id: "view", title: "View", mount: () => () => { cleaned++; } }); } });
const cleanup = await session.mount("view", { replaceChildren() { cleared++; } } as unknown as HTMLElement);
await session.stop(); await cleanup(); assert.equal(cleaned, 1); assert.equal(cleared, 1);
});
test("shutdown waits for view cleanup that already started when the view closed", async () => {
const { session } = fixture(); let done!: () => void; let cleaned = false;
const gate = new Promise<void>(resolve => done = resolve);
await session.start({ activate(api) { api.registerView({ id: "view", title: "View", mount: () => async () => { await gate; cleaned = true; } }); } });
const cleanup = await session.mount("view", { replaceChildren() {} } as unknown as HTMLElement);
const closing = cleanup(); await Promise.resolve();
let stopped = false; const stopping = session.stop().then(() => { stopped = true; });
await Promise.resolve(); await Promise.resolve(); assert.equal(stopped, false);
done(); await Promise.all([closing, stopping]); assert(cleaned);
});
test("cleanup returned by late activation still blocks shutdown when it fails", async () => {
const { session } = fixture(); let done!: () => void;
const gate = new Promise<void>(resolve => done = resolve);
const starting = session.start({ async activate() { await gate; return () => { throw new Error("Late cleanup failed"); }; } });
await Promise.resolve(); const stopping = session.stop(); done();
await assert.rejects(starting, /Late cleanup failed/); await assert.rejects(stopping, /Late cleanup failed/);
});
test("batch workspace reads remain optional and stop when the plugin is disabled", async () => {
let api!: PluginApi;
let calls = 0;
const rows = [{ path: "Card.md", source: "Card source" }, { path: "Missing.md", error: "File not found" }];
const session = new ExternalSession("org.test.batch", {
workspace: { list: async () => [], read: async () => "", write: async () => {},
readMany: async paths => { calls++; assert.deepEqual(paths, ["Card.md", "Missing.md"]); return rows; } },
settings: { read: async () => null, write: async () => {} },
assets: { read: async () => new Uint8Array() },
}, () => {}, () => {});
await session.start({ activate(value) { api = value; } });
assert.deepEqual(await api.workspace.readMany!(["Card.md", "Missing.md"]), rows);
await session.stop();
assert.throws(() => api.workspace.readMany!(["Card.md"]), /disabled/);
assert.equal(calls, 1);
const old = fixture().session;
await old.start({ activate(value) { assert.equal(value.workspace.readMany, undefined); } });
await old.stop();
});
test("view launch selects a default while explicit opens preserve their target", async () => {
const opened: string[] = [];
let api!: PluginApi;
const session = new ExternalSession("org.test.launch", {
workspace: { list: async () => [], read: async () => "", write: async () => {} },
settings: { read: async () => null, write: async () => {} },
assets: { read: async () => new Uint8Array() },
}, () => {}, id => { opened.push(id); });
await session.start({ activate(value) {
api = value;
api.registerView({ id: "all", title: "All", launch: async () => { api.openView("board"); }, mount() {} });
api.registerView({ id: "board", title: "Board", mount() {} });
assert.throws(() => api.registerView({ id: "invalid", title: "Invalid", launch: true as never, mount() {} }), /launch handler/);
} });
await session.launch("all");
api.openView("all");
await session.launch("board");
assert.deepEqual(opened, ["board", "all", "board"]);
await assert.rejects(session.launch("missing"), /Unknown plugin view/);
await session.stop();
await assert.rejects(session.launch("all"), /disabled/);
});