- Add the native feature runtime in src/lib/features/: the catalog, the switches, the first-start migration, the loader, and the feature sessions. - Add Settings → Features and the features settings section. - Add features_legacy. It reads the old plugin packages and changes nothing. - Move Git from the plugin into src/lib/features/git/, on host internals. - Remove api.git, the git contribution key, and the SDK Git types. - Add pnpm check:features. It fails when a feature module loads at start. - Update the tests, the menu inventory, the docs, and UI-055.
87 lines
4.2 KiB
TypeScript
87 lines
4.2 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 type { PluginApi } from "../../../src/lib/plugins/sdk.ts";
|
|
import type { Contributions } from "../../../src/lib/plugins/contracts.ts";
|
|
|
|
// A small test-only package. It contributes a feature, as the optional plugins do.
|
|
const terminal: Contributions["terminal"] = { mount: () => ({ update() {}, command() {}, dispose() {} }), shortcut: () => null };
|
|
const activate = (api: PluginApi) => { api.contribute("terminal", terminal); };
|
|
|
|
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 package contributions end with their session", async () => {
|
|
assert.equal(pluginHost.get("terminal"), undefined);
|
|
const registry = new ContributionRegistry();
|
|
const session = new ExternalSession("org.test.feature", services, () => {}, () => {}, registry);
|
|
await session.start({ activate });
|
|
assert.equal(registry.get("terminal"), terminal);
|
|
await session.stop();
|
|
assert.equal(registry.get("terminal"), undefined);
|
|
const next = new ExternalSession("org.test.feature", services, () => {}, () => {}, registry);
|
|
await next.start({ activate });
|
|
assert.equal(registry.get("terminal"), terminal);
|
|
await next.stop();
|
|
assert.equal(registry.get("terminal"), undefined);
|
|
});
|
|
|
|
test("unknown contribution keys are rejected", () => {
|
|
const registry = new ContributionRegistry();
|
|
for (const key of ["tableFormulas", "git"]) assert.throws(() => registry.register("old", key as keyof Contributions, terminal as never), /Unknown plugin contribution/);
|
|
});
|
|
|
|
test("package contribution conflicts preserve the first owner and revoke access before cleanup", 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("terminal");
|
|
await assert.rejects(second.start({ activate }), /already registered/);
|
|
await second.stop();
|
|
assert.equal(registry.get("terminal"), contribution);
|
|
api.onDispose(() => {
|
|
assert.equal(registry.get("terminal"), undefined);
|
|
assert.throws(() => api.contribute("terminal", contribution!), /disabled/);
|
|
});
|
|
await first.stop();
|
|
});
|
|
|
|
test("partial activation and failed cleanup cannot retain feature contributions", 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("terminal"), undefined);
|
|
});
|