import test from "node:test"; import assert from "node:assert/strict"; import { ExternalSession, type SessionServices } from "../../../src/lib/plugins/externalSession.ts"; import { ContributionRegistry } from "../../../src/lib/plugins/registry.ts"; import { ServiceHub } from "../../../src/lib/plugins/services.ts"; import type { FilesTransport, PluginApi } from "../../../src/lib/plugins/sdk.ts"; const base: SessionServices = { workspace: { list: async () => [], read: async () => "", write: async () => {} }, settings: { read: async () => null, write: async () => {} }, assets: { read: async () => new Uint8Array() }, }; async function start(id: string, services: Partial) { let api!: PluginApi; const session = new ExternalSession(id, { ...base, ...services }, () => {}, () => {}, new ContributionRegistry()); await session.start({ activate(value) { api = value; } }); return { api, session }; } test("folder grant calls check shutdown before and after the native request", async () => { const calls: unknown[][] = []; let finish: ((value: unknown) => void) | undefined; const grant = { id: "7f1b6f6e-3c39-4b55-9f3e-4a3a6d1d7c11", path: "C:/Pictures/RuneStone Backgrounds", name: "RuneStone Backgrounds" }; const files: FilesTransport = { chooseFolder: async options => { calls.push(["choose", options]); return grant; }, pictureFolder: async name => { calls.push(["picture", name]); return grant; }, grants: async () => [grant], revoke: async id => { calls.push(["revoke", id]); return true; }, list: () => new Promise(resolve => { finish = resolve as (value: unknown) => void; }), readBytes: async (id, name) => { calls.push(["read", id, name]); return new Uint8Array([1, 2]); }, readSidecar: async () => null, writeSidecar: async (id, name, contents, expected) => { calls.push(["write", id, name, contents, expected]); }, }; const { api, session } = await start("org.test.slides", { files }); assert.deepEqual(await api.files!.chooseFolder({ title: "Images" }), grant); assert.deepEqual(await api.files!.pictureFolder("Backgrounds"), grant); assert.deepEqual(await api.files!.readBytes(grant.id, "a.png"), new Uint8Array([1, 2])); await api.files!.writeSidecar(grant.id, ".runestone-backgrounds.json", "{}", null); assert.equal(await api.files!.revoke(grant.id), true); assert.deepEqual(calls, [["choose", { title: "Images" }], ["picture", "Backgrounds"], ["read", grant.id, "a.png"], ["write", grant.id, ".runestone-backgrounds.json", "{}", null], ["revoke", grant.id]]); assert.throws(() => api.files!.readBytes("", "a.png"), /grant ID/); assert.throws(() => api.files!.writeSidecar(grant.id, ".runestone-a.json", "{}", undefined as never), /expected/); const pending = api.files!.list(grant.id); await session.stop(); finish!([{ name: "a.png", size: 2 }]); await assert.rejects(pending, /disabled/); assert.throws(() => api.files!.grants(), /disabled/); }); test("plugin services copy data, stop with their provider, and stop for a stopped consumer", async () => { const serviceHub = new ServiceHub(); const boards = [{ id: "b1", name: "Roadmap" }]; const provider = await start("org.test.boards", { serviceHub }); const consumer = await start("org.test.slides", { serviceHub }); const changes: string[] = []; consumer.api.services!.subscribe((plugin, name) => changes.push(`${plugin}/${name}`)); assert.equal(consumer.api.services!.get("org.test.boards", "boards"), null); const handle = provider.api.services!.provide("boards", { list: () => boards }); assert.throws(() => provider.api.services!.provide("boards", { list: () => boards }), /already/); assert.throws(() => provider.api.services!.provide("Bad Name", { list: () => boards }), /service name/); const service = consumer.api.services!.get("org.test.boards", "boards")!; const result = await service.list() as typeof boards; assert.deepEqual(result, boards); result[0].name = "Changed"; assert.equal(boards[0].name, "Roadmap", "the consumer receives a copy"); handle.notify(); assert.deepEqual(changes, ["org.test.boards/boards", "org.test.boards/boards"]); await provider.session.stop(); assert.deepEqual(changes.at(-1), "org.test.boards/boards"); await assert.rejects(service.list(), /no longer available/); assert.equal(consumer.api.services!.get("org.test.boards", "boards"), null); const again = await start("org.test.boards", { serviceHub }); again.api.services!.provide("boards", { list: () => boards }); const fresh = consumer.api.services!.get("org.test.boards", "boards")!; await consumer.session.stop(); await assert.rejects(fresh.list(), /disabled/); const count = changes.length; await again.session.stop(); assert.equal(changes.length, count, "a stopped consumer receives no notifications"); });