Files
stack/packages/business/tests/plugin-settings.test.mjs
T
jason.woltjeandClaude Opus 5.5 eaeb5ebf2b plugins: the plug-in layer, kind registry and loader (row 131, #1626)
packages/plugins holds the kind registry and the loader for in-repo
plug-in modules (REQ-PLUG-1). The first two kinds are usage-source and
limit-check (REQ-LIMIT-4). The business file's plugins key turns modules
on per kind, through plugin-settings.mjs and step 5's write path
(REQ-ROLE-3). A refused load is logged and keeps the kind's old modules.
Any filesystem error other than ENOENT or ENOTDIR refuses as unreadable.
The organization writer validates the bytes it writes as well as the
document. stack-copy ships packages/plugins. Nothing is wired into a
host process yet.

Candidate 466c835f7dce79d20c7ab74c197a52b70e7fdeb0efae0456222dc6d0a633f219,
Dewey packet 80797a539 (round 2), base 0bdd573ff. Filbert approved
round 2 in 28568 and Darkwing in 28570. Landing gate at f7d249634: 24 suites green, 0 failed.
14 package suites passed 1586 tests (plugins 21, business 92,
bus 171, cli 195, webui 112). The 10 script suites passed. Follow-up:
P19 task 293, coded writer refusals.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-15 11:57:54 -05:00

106 lines
5.5 KiB
JavaScript

// REQ-PLUG-1 / REQ-ROLE-3 (step 5 write path): the `plugins` key in the organization
// file, written only through the host setter and logged like a limit change.
import test, { after } from "node:test";
import assert from "node:assert/strict";
import { existsSync, readFileSync, rmSync } from "node:fs";
import { join } from "node:path";
import { businessDoc, rolesCopy, tmp as makeRoot, writeJson } from "./helpers.mjs";
import { validateBusinessDocument } from "../src/business.mjs";
import { pluginSettings } from "../src/plugin-settings.mjs";
import { OrganizationError } from "../src/organization.mjs";
import { BusinessError } from "../src/errors.mjs";
const roots = [];
after(() => roots.forEach(root => rmSync(root, { recursive: true, force: true })));
function fixture() {
const root = makeRoot();
roots.push(root);
const rolesDir = rolesCopy(root);
const doc = businessDoc(root, "acme");
const file = writeJson(join(root, "businesses", "acme.json"), doc);
return { root, rolesDir, doc, file, settings: pluginSettings({ dir: root, rolesDir }) };
}
const events = f => existsSync(f.settings.writer.logFile("acme"))
? readFileSync(f.settings.writer.logFile("acme"), "utf8").trim().split("\n").map(JSON.parse) : [];
test("REQ-PLUG-1 schema: plugins is optional, names known kinds and checked module names", () => {
const f = fixture();
const check = d => validateBusinessDocument(d, f.file, { rolesDir: f.rolesDir });
assert.deepEqual(check(f.doc).plugins, {});
const on = { ...structuredClone(f.doc), plugins: { "usage-source": ["meter"], "limit-check": [] } };
const business = check(on);
assert.deepEqual(business.plugins, on.plugins);
assert.ok(Object.isFrozen(business.plugins));
assert.ok(!Object.isFrozen(on.plugins), "the document itself stays mutable");
for (const plugins of [{ "made-up": [] }, { "usage-source": "meter" }, { "usage-source": ["Meter"] }, { "usage-source": ["a", "a"] }, []]) {
assert.throws(() => check({ ...structuredClone(f.doc), plugins }), BusinessError, JSON.stringify(plugins));
}
});
test("REQ-PLUG-1 REQ-ROLE-3 the setter writes plugins through the organization writer and logs old and new names", () => {
const f = fixture();
const view = f.settings.read("acme");
assert.deepEqual(view.plugins, {});
const done = f.settings.set("acme", { revision: view.revision, plugins: { "usage-source": ["meter"] } }, "jason");
assert.deepEqual(JSON.parse(readFileSync(f.file, "utf8")).plugins, { "usage-source": ["meter"] });
assert.deepEqual(done.plugins, { "usage-source": ["meter"] });
assert.notEqual(done.revision, view.revision);
let last = events(f).at(-1);
assert.equal(last.state, "committed");
assert.equal(last.actor, "jason");
assert.equal(last.transaction, done.transaction);
assert.deepEqual(last.changes, [{ path: "plugins", old: null, new: { "usage-source": ["meter"] }, oldPresent: false, newPresent: true }]);
f.settings.set("acme", { revision: done.revision, plugins: { "usage-source": ["meter", "other"], "limit-check": ["cap"] } }, "jason");
last = events(f).at(-1);
assert.deepEqual(last.changes.map(c => [c.path, c.old, c.new]), [
["plugins.usage-source", ["meter"], ["meter", "other"]],
["plugins.limit-check", null, ["cap"]],
]);
// A null-prototype object is still a plain JSON object.
const bare = Object.assign(Object.create(null), { "limit-check": ["cap"] });
f.settings.set("acme", { revision: f.settings.read("acme").revision, plugins: bare }, "jason");
assert.deepEqual(JSON.parse(readFileSync(f.file, "utf8")).plugins, { "limit-check": ["cap"] });
});
test("REQ-PLUG-1 REQ-ROLE-3 the writer checks the bytes it writes: a Date in plugins is refused there too", () => {
// Without the setter's own check, the organization writer still refuses:
// it validates JSON.parse of the bytes, where the Date is a string.
const f = fixture();
const { revision } = f.settings.read("acme");
const bytes = readFileSync(f.file, "utf8");
assert.throws(() => f.settings.writer.write("acme", { ...structuredClone(f.doc), plugins: new Date(0) }, { revision, actor: "jason" }),
BusinessError);
assert.equal(readFileSync(f.file, "utf8"), bytes);
assert.deepEqual(events(f), []);
});
test("REQ-PLUG-1 REQ-ROLE-3 a refused setter write throws a code, changes nothing and logs nothing", () => {
const f = fixture();
const { revision } = f.settings.read("acme");
const bytes = readFileSync(f.file, "utf8");
const refusals = [
[{ revision, plugins: { "made-up": ["x"] } }, "invalid-plugins"],
[{ revision, plugins: { "usage-source": ["Bad"] } }, "invalid-plugins"],
[{ revision, plugins: { "usage-source": "meter" } }, "invalid-plugins"],
[{ revision, plugins: { "limit-check": ["cap", "cap"] } }, "invalid-plugins"],
[{ revision }, "invalid-plugins"],
[{ revision, plugins: [] }, "invalid-plugins"],
// No own keys, so checkPlugins alone passes them; JSON would write something else.
[{ revision, plugins: new Date(0) }, "invalid-plugins"],
[{ revision, plugins: new Map([["usage-source", ["meter"]]]) }, "invalid-plugins"],
[{ revision, plugins: {}, limits: {} }, "invalid-request"],
[{ revision: "nope", plugins: {} }, "invalid-request"],
[{ plugins: {} }, "invalid-request"],
[null, "invalid-request"],
[["x"], "invalid-request"],
];
for (const [args, expected] of refusals) {
assert.throws(() => f.settings.set("acme", args, "jason"),
e => e instanceof OrganizationError && e.code === expected, JSON.stringify(args));
}
assert.equal(readFileSync(f.file, "utf8"), bytes);
assert.deepEqual(events(f), []);
});