Unit tests go to tests/unit/<area>/, browser smoke tests to tests/smoke/<area>/, and tests against a running desktop app to tests/native/. Files named after dates, work passes, or Kanban cards get feature names, for example editorOctober12 -> editor/footnotes. scripts/ keeps the build, release, and package tools. Plugin repositories import scripts/packagePlugin.mjs, and the Terminal smoke test imports scripts/pluginMocks.mjs, which now re-exports the harness copy. The unit tests get their own type check (tests/tsconfig.json) with Node types. pnpm check runs it after the app check. Add @types/node and fix the three test mocks that it reported.
97 lines
5.0 KiB
TypeScript
97 lines
5.0 KiB
TypeScript
import test from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import { ExternalSession } from "../../../src/lib/plugins/externalSession.ts";
|
|
import type { PluginApi, PluginDocumentType } from "../../../src/lib/plugins/sdk.ts";
|
|
|
|
const services = {
|
|
workspace: { list: async () => [], read: async () => "", write: async () => {} },
|
|
settings: { read: async () => null, write: async () => {} },
|
|
assets: { read: async () => new Uint8Array() },
|
|
};
|
|
const type: PluginDocumentType = {
|
|
id: "board", title: "Board", icon: "kanban", extensions: ["md"],
|
|
identify: (_path, source) => source === "board" ? { id: "stable" } : null,
|
|
create: { label: "New board", defaultName: "Untitled board", extension: "md", contents: () => "board" },
|
|
mount: () => {},
|
|
};
|
|
|
|
test("document renames pass the stable identity and stop after shutdown", async () => {
|
|
let api!: PluginApi;
|
|
const calls: unknown[] = [];
|
|
const session = new ExternalSession("fixture", services, () => {}, () => {}, undefined, undefined,
|
|
async (document, name) => { calls.push({ document, name }); return "Renamed.md"; });
|
|
await session.start({ activate(value) { api = value; } });
|
|
assert.equal(await api.renameDocument!({ id: "stable", path: "Board.md" }, "Renamed"), "Renamed.md");
|
|
assert.deepEqual(calls, [{ document: { id: "stable", path: "Board.md" }, name: "Renamed" }]);
|
|
await session.stop();
|
|
assert.throws(() => api.renameDocument!({ id: "stable", path: "Board.md" }, "Other"), /disabled/);
|
|
assert.equal(calls.length, 1);
|
|
});
|
|
|
|
test("binary document registration preserves bytes and revokes reads on shutdown", async () => {
|
|
let api!: PluginApi;
|
|
const bytes = new Uint8Array([0x25, 0x50, 0x44, 0x46, 0xff, 0x00]);
|
|
const paths: string[] = [];
|
|
const session = new ExternalSession("fixture", {
|
|
...services, workspace: { ...services.workspace, readBytes: async path => { paths.push(path); return bytes; } },
|
|
}, () => {}, () => {});
|
|
await session.start({ activate(value) {
|
|
api = value;
|
|
api.registerDocumentType({ ...type, id: "pdf", extensions: ["pdf"], content: "binary", create: undefined });
|
|
} });
|
|
assert.equal(session.documentTypes.get("pdf")?.content, "binary");
|
|
assert.deepEqual(await api.workspace.readBytes!("report.pdf"), bytes);
|
|
assert.deepEqual(paths, ["report.pdf"]);
|
|
assert.throws(() => api.registerDocumentType({ ...type, content: "invalid" as "binary" }), /content type/);
|
|
await session.stop();
|
|
assert.throws(() => api.workspace.readBytes!("report.pdf"), /disabled/);
|
|
});
|
|
|
|
test("document types have independent registrations and reject invalid creation actions", async () => {
|
|
let api!: PluginApi;
|
|
const session = new ExternalSession("fixture", services, () => {}, () => {});
|
|
await session.start({ activate(value) { api = value; api.registerDocumentType(type); } });
|
|
assert.equal(session.documentTypes.get("board")?.identify("Board.md", "board")?.id, "stable");
|
|
assert.equal(session.documentTypes.get("board")?.identify("Note.md", "plain"), null);
|
|
assert.throws(() => api.registerDocumentType(type), /duplicate/);
|
|
assert.throws(() => api.registerDocumentType({ ...type, id: "other", create: { ...type.create!, extension: "exe" } }), /Invalid document creation/);
|
|
await session.stop();
|
|
assert.equal(session.documentTypes.size, 0);
|
|
assert.throws(() => api.registerDocumentType(type), /disabled/);
|
|
});
|
|
|
|
test("two document mounts retain independent context and clean up once", async () => {
|
|
const mounted: string[] = [], cleaned: string[] = [];
|
|
const session = new ExternalSession("fixture", services, () => {}, () => {});
|
|
await session.start({ activate(api) {
|
|
api.registerDocumentType({ ...type, mount(_node, context) { mounted.push(context.path); return () => { cleaned.push(context.id); }; } });
|
|
} });
|
|
const element = () => ({ replaceChildren() {} }) as unknown as HTMLElement;
|
|
const first = await session.mount("board", element(), { id: "first", path: "One.md" });
|
|
await session.mount("board", element(), { id: "second", path: "Two.md" });
|
|
assert.deepEqual(mounted, ["One.md", "Two.md"]);
|
|
await first(); await first();
|
|
assert.deepEqual(cleaned, ["first"]);
|
|
await session.stop();
|
|
assert.deepEqual(cleaned, ["first", "second"]);
|
|
});
|
|
|
|
test("document opening and workspace subscriptions stop with their plugin", async () => {
|
|
let api!: PluginApi;
|
|
let listener: (() => void) | undefined;
|
|
let notifications = 0, removed = 0;
|
|
const opened: string[] = [];
|
|
const session = new ExternalSession("fixture", {
|
|
...services,
|
|
workspace: { ...services.workspace, subscribe(callback) { listener = callback; return () => { removed++; }; } },
|
|
}, () => {}, () => {}, undefined, async path => { opened.push(path); });
|
|
await session.start({ activate(value) { api = value; api.workspace.subscribe!(() => { notifications++; }); } });
|
|
await api.openDocument("Board.md"); listener!();
|
|
assert.deepEqual(opened, ["Board.md"]);
|
|
assert.equal(notifications, 1);
|
|
await session.stop(); listener!();
|
|
assert.equal(notifications, 1);
|
|
assert.equal(removed, 1);
|
|
assert.throws(() => api.openDocument("Board.md"), /disabled/);
|
|
});
|