import test from "node:test"; import assert from "node:assert/strict"; import { ExternalSession } from "../src/lib/plugins/externalSession.ts"; import { ContributionRegistry } from "../src/lib/plugins/registry.ts"; import type { PluginApi } from "../src/lib/plugins/sdk.ts"; test("Git and file access stop before plugin cleanup", async () => { const calls: unknown[] = []; let api!: PluginApi; const registry = new ContributionRegistry(); const session = new ExternalSession("test.git", { workspace: { list: async () => [], read: async () => "", write: async () => {} }, settings: { read: async () => null, write: async () => {} }, assets: { read: async () => new Uint8Array() }, openFile: async path => { calls.push(path); }, git: { status: async () => ({ available: true, repository: true, branch: "main", changes: [] }), diff: async (path, staged) => { calls.push([path, staged]); return "diff"; }, stage: async paths => { calls.push(paths); }, unstage: async paths => { calls.push(paths); }, commit: async message => { calls.push(message); }, blame: async () => null, push: async () => { calls.push('push'); }, pull: async () => { calls.push('pull'); }, graph: async () => ({ commits: [] }), }, }, () => {}, () => {}, registry); await session.start({ activate(value) { api = value; api.contribute("git", { blame: (path, line) => api.git!.blame(path, line) }); } }); await api.openFile!("notes.md"); assert.equal(await api.git!.diff("notes.md", true), "diff"); await api.git!.stage(["notes.md"]); assert.deepEqual(calls, ["notes.md", ["notes.md", true], ["notes.md"]]); await api.git!.push(); await api.git!.pull(); assert.deepEqual(await api.git!.graph(), { commits: [] }); assert.deepEqual(calls.slice(-2), ['push', 'pull']); await session.stop(); assert.equal(registry.get("git"), undefined); assert.throws(() => api.openFile!("notes.md"), /disabled/); for (const action of [() => api.git!.push(), () => api.git!.pull(), () => api.git!.graph()]) assert.throws(action, /disabled/); for (const action of [() => api.git!.status(), () => api.git!.diff("a", false), () => api.git!.stage(["a"]), () => api.git!.unstage(["a"]), () => api.git!.commit("a"), () => api.git!.blame("a", 1)]) assert.throws(action, /disabled/); }); test("view replacement rejects unknown destinations and stops after disposal", async () => { let api!: PluginApi; const calls: unknown[] = []; const session = new ExternalSession("test.boards", { workspace: { list: async () => [], read: async () => "", write: async () => {} }, settings: { read: async () => null, write: async () => {} }, assets: { read: async () => new Uint8Array() }, }, () => {}, () => {}, new ContributionRegistry(), undefined, undefined, undefined, (current, next) => calls.push([current, next]), (document, next) => calls.push([document, next])); await session.start({ activate(value) { api = value; for (const id of ["a", "b"]) api.registerView({ id, title: id, placement: "editor", mount() {} }); api.registerView({ id: "sidebar", title: "Sidebar", placement: "sidebar", navigation: true, icon: "git", mount() {} }); } }); api.replaceView!("a", "b"); api.replaceDocumentWithView!({ id: "legacy", path: "Board.md" }, "b"); assert.deepEqual(calls, [["a", "b"], [{ id: "legacy", path: "Board.md" }, "b"]]); assert.throws(() => api.replaceView!("missing", "b"), /Unknown/); assert.throws(() => api.replaceDocumentWithView!({ id: "legacy", path: "Board.md" }, "missing"), /Unknown/); await session.stop(); assert.throws(() => api.replaceView!("a", "b"), /disabled/); });