Files
RuneStone/scripts/plugins.spec.ts
T
Knotty Wyvern 90d1940c73 Make the checks pass on a fresh clone
- build.rs creates the empty browser-engine folder that tauri-build
  requires, so cargo test runs before the browser runtime is staged.
- The Table Formulas tests skip with a reason when the package is not
  built. Before, the missing module failed two whole test files.
- The installer test accepts CRLF checkouts and skips with a reason
  when the NSIS compiler is missing.
- hostTodo.spec.mjs never ran because of its extension. Its two valid
  background tests move to appearance.spec.ts with current defaults.
  The third test repeated an existing export test.
- Remove tsconfig.plugins.json. It checked the plugins/ folder, which
  moved to separate repositories in 0.1.7.
2026-10-27 19:41:08 -07:00

90 lines
4.6 KiB
TypeScript

import test from "node:test";
import assert from "node:assert/strict";
import { ContributionRegistry, pluginHost } from "../src/lib/plugins/registry.ts";
import { ExternalSession } from "../src/lib/plugins/externalSession.ts";
import { existsSync } from "node:fs";
// The Table Formulas package comes from a sibling repository. Run `pnpm build:plugins` to build it.
const tableFormulasEntry = new URL("../plugin-packages/table-formulas/main.js", import.meta.url);
const tableFormulas = existsSync(tableFormulasEntry) ? await import(tableFormulasEntry.href) : undefined;
const activate = tableFormulas?.activate;
const needsTableFormulas = { skip: tableFormulas ? false : "the Table Formulas package is not built; run pnpm build:plugins" };
import { renderMarkdownHtml, renderMarkdownSections } from "../src/lib/workspace/noteHtml.ts";
import type { PluginApi } 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() },
};
test("optional workspace creation and reads stop when the plugin is disabled", async () => {
const files = new Map<string, string>();
let api!: PluginApi;
const session = new ExternalSession("workspace-test", {
...services,
workspace: {
...services.workspace,
readOptional: async path => files.get(path) ?? null,
create: async (path, contents) => { if (files.has(path)) throw new Error("exists"); files.set(path, contents); },
},
}, () => {}, () => {});
await session.start({ activate(value) { api = value; } });
assert.equal(await api.workspace.readOptional!("board.json"), null);
await api.workspace.create!("board.json", "{}");
assert.equal(await api.workspace.readOptional!("board.json"), "{}");
await assert.rejects(api.workspace.create!("board.json", "replacement"), /exists/);
await session.stop();
assert.throws(() => api.workspace.create!("other.json", "{}"), /disabled/);
assert.throws(() => api.workspace.readOptional!("board.json"), /disabled/);
assert.equal(files.size, 1);
});
test("core starts without optional features and formula packages preserve source when stopped", needsTableFormulas, async () => {
assert.equal(pluginHost.get("terminal"), undefined);
assert.equal(pluginHost.get("tableFormulas"), undefined);
const source = "| A | B |\n| --- | --- |\n| 2 | =A2*3 |\n";
assert.doesNotMatch(renderMarkdownHtml(source, new Map()), /md-table-formula/);
const session = new ExternalSession("runestone.table-formulas", services, () => {}, () => {});
await session.start({ activate });
const enabled = renderMarkdownHtml(source, new Map());
assert.match(enabled, />6<\/span>/);
await session.stop();
for (const html of [renderMarkdownHtml(source, new Map()), ...renderMarkdownSections(source, new Map()).map(section => section.html)]) {
assert.doesNotMatch(html, /md-table-formula/);
assert.match(html, /=A2\*3/);
assert.match(html, /<table>/);
}
const next = new ExternalSession("runestone.table-formulas", services, () => {}, () => {});
await next.start({ activate });
assert.equal(renderMarkdownHtml(source, new Map()), enabled);
await next.stop();
});
test("package contribution conflicts preserve the first owner and revoke access before cleanup", needsTableFormulas, async () => {
const registry = new ContributionRegistry();
const first = new ExternalSession("first", services, () => {}, () => {}, registry);
const second = new ExternalSession("second", services, () => {}, () => {}, registry);
let api!: PluginApi;
await first.start({ activate(value) { api = value; activate(api); } });
const contribution = registry.get("tableFormulas");
await assert.rejects(second.start({ activate }), /already registered/);
await second.stop();
assert.equal(registry.get("tableFormulas"), contribution);
api.onDispose(() => {
assert.equal(registry.get("tableFormulas"), undefined);
assert.throws(() => api.contribute("tableFormulas", contribution!), /disabled/);
});
await first.stop();
});
test("partial activation and failed cleanup cannot retain feature contributions", needsTableFormulas, async () => {
const registry = new ContributionRegistry();
const session = new ExternalSession("failed", services, () => {}, () => {}, registry);
await assert.rejects(session.start({ activate(api) {
activate(api);
api.onDispose(() => { throw new Error("Cleanup failed"); });
throw new Error("Activation failed");
} }), /Activation failed/);
await assert.rejects(session.stop(), /Cleanup failed/);
assert.equal(registry.get("tableFormulas"), undefined);
});