The platform has had a complete story for one module using another's
*backend* since ADR 0006 — capabilities, versioned ranges, provider
registration. It has had none at all for one module using another's *browser*
code, and every workaround is wrong in a way that matters: a consumer cannot
import a peer module, it cannot re-implement a renderer for a domain it does
not own, and a second `entrypoints` role would put the platform in charge of
deciding when a slot inside someone else's screen should be filled.
Add a generic browser contribution. `manifestVersion: 7` declares one:
namespaced id, its own version, a target capability, a compatible range, and
one artifact. That is the complete list, and the omissions are the decision —
no payload schema, slot, ordering, or rendering hint exists anywhere, so Core
cannot learn what a slide is because the declaration cannot express it.
Discovery resolves a contribution artifact through the *identical* loop every
other compiled entrypoint uses, inheriting the path, containment, symlink,
extension, and file-kind checks rather than getting a weaker copy of them. The
registry publishes one only when another eligible module provides the target
capability within the declared range; otherwise it is absent rather than
mounted and broken. `apps/api` serves it as a sibling of the artifact and
display routes, with the same policy: three lookup keys, one exact match, one
already-validated path, and one indistinguishable 404 for unknown, undeclared,
stale, and incompatible alike.
A consumer discovers through the host, never through a URL. Its descriptor
carries identity, version, and capability; the URL stays server-side and is
resolved exactly as `api.request("records")` is. A module can only ever see
contributions aimed at a capability it itself provides, and cannot name its
way to anything else — a mount for a withheld id fails without fetching.
The consumer owns the domain contract entirely. Input is opaque JSON, checked
structurally so a Date, a function, a cycle, or `undefined` fails at the
boundary rather than vanishing somewhere far from the mistake, and checked for
nothing else. A contributed renderer receives its own id and that input: no
API client, realtime, principal, configuration, database, storage, or
credential. Loading one grants its module nothing.
The contribution ABI is versioned separately from the module UI ABI, because
the two have different shapes and lifecycles and coupling them would force
every module to rebuild whenever either moved. It supports asynchronous
readiness, update-without-remount (a screen that remounted per tick would
restart animations and reload media), deterministic cleanup in a `finally`,
and sanitized failure that never carries the consumer's domain data. Each
renderer mounts in its own isolated root with its own stylesheet.
The template now declares a contribution, so `pnpm build` exercises the whole
authoring path, and the shell suite drives the full relationship — one module
discovering, mounting, updating, and unmounting another's React renderer with
neither importing the other.
Co-Authored-By: Claude Opus 5 <[email protected]>
240 lines
9.7 KiB
TypeScript
240 lines
9.7 KiB
TypeScript
import { type PermissionBaseline, parseModuleManifest } from "@ekklesia/contracts";
|
|
import { describe, expect, it } from "vitest";
|
|
import {
|
|
CORE_AUTHORIZATION_GRANTS_MANAGE,
|
|
CORE_DASHBOARD_NAVIGATION_MANAGE,
|
|
CORE_DEVICES_MANAGE,
|
|
CORE_MODULES_CONFIGURATION_MANAGE,
|
|
CORE_MODULES_LIFECYCLE_MANAGE,
|
|
CORE_MODULES_UI_READ,
|
|
CORE_PERMISSION_DECLARATIONS,
|
|
PermissionRegistryError,
|
|
} from "../src/authorization/index.js";
|
|
import type { DiscoveredModule } from "../src/modules/discovery.js";
|
|
import {
|
|
composePlatformPermissionRegistry,
|
|
ModulePermissionCompositionError,
|
|
} from "../src/modules/permissions.js";
|
|
|
|
interface PermissionInput {
|
|
readonly id: string;
|
|
readonly baseline: PermissionBaseline;
|
|
readonly description: string;
|
|
}
|
|
|
|
/** A minimal, in-memory `DiscoveredModule` double — no filesystem, no real discovery. The manifest
|
|
* is still built through `parseModuleManifest`, so a fixture that would not actually pass manifest
|
|
* validation cannot silently misrepresent what real discovery would ever produce. Only `manifest`
|
|
* is read by `composePlatformPermissionRegistry`; the rest exist only to satisfy the type. */
|
|
function fakeModule(id: string, permissions: readonly PermissionInput[] = []): DiscoveredModule {
|
|
const manifest = parseModuleManifest({
|
|
manifestVersion: 7,
|
|
id,
|
|
name: `${id} module`,
|
|
version: "0.1.0",
|
|
compatibility: { platform: "^0.1.0", moduleSdk: "^0.1.0" },
|
|
permissions,
|
|
entrypoints: {
|
|
// Deliberately a path nothing will ever resolve, proving composition never imports or
|
|
// otherwise touches a module's server entrypoint.
|
|
server: "./dist/does-not-exist/index.js",
|
|
},
|
|
});
|
|
return {
|
|
contributions: [],
|
|
directory: `/modules/${id}`,
|
|
entrypoints: { server: `/modules/${id}/dist/does-not-exist/index.js` },
|
|
manifest,
|
|
manifestPath: `/modules/${id}/ekklesia.module.json`,
|
|
packageName: `@test/${id}`,
|
|
};
|
|
}
|
|
|
|
describe("composePlatformPermissionRegistry", () => {
|
|
it("composes a Core-only registry when no modules are supplied", () => {
|
|
const registry = composePlatformPermissionRegistry([]);
|
|
|
|
expect(registry.declarations).toEqual(CORE_PERMISSION_DECLARATIONS);
|
|
expect(registry.has(CORE_AUTHORIZATION_GRANTS_MANAGE)).toBe(true);
|
|
});
|
|
|
|
it("composes Core and multiple discovered modules' declarations into one registry", () => {
|
|
const calendar = fakeModule("calendar", [
|
|
{ id: "calendar.events.read", baseline: "restricted", description: "Read events." },
|
|
]);
|
|
const people = fakeModule("people", [
|
|
{ id: "people.directory.read", baseline: "authenticated", description: "Read directory." },
|
|
]);
|
|
|
|
const registry = composePlatformPermissionRegistry([calendar, people]);
|
|
|
|
expect(registry.has(CORE_AUTHORIZATION_GRANTS_MANAGE)).toBe(true);
|
|
expect(registry.has(CORE_MODULES_UI_READ)).toBe(true);
|
|
expect(registry.has("calendar.events.read")).toBe(true);
|
|
expect(registry.has("people.directory.read")).toBe(true);
|
|
expect(registry.has(CORE_DASHBOARD_NAVIGATION_MANAGE)).toBe(true);
|
|
expect(registry.has(CORE_MODULES_LIFECYCLE_MANAGE)).toBe(true);
|
|
expect(registry.has(CORE_MODULES_CONFIGURATION_MANAGE)).toBe(true);
|
|
// Core's own five declarations plus one from each module.
|
|
expect(registry.declarations).toHaveLength(8);
|
|
});
|
|
|
|
it("preserves deterministic order: Core first, then modules in the supplied order, then declarations in manifest order", () => {
|
|
const calendar = fakeModule("calendar", [
|
|
{ id: "calendar.events.read", baseline: "restricted", description: "Read events." },
|
|
{ id: "calendar.events.write", baseline: "restricted", description: "Write events." },
|
|
]);
|
|
const people = fakeModule("people", [
|
|
{ id: "people.directory.read", baseline: "authenticated", description: "Read directory." },
|
|
]);
|
|
|
|
const registry = composePlatformPermissionRegistry([calendar, people]);
|
|
|
|
expect(registry.declarations.map((declaration) => declaration.id)).toEqual([
|
|
CORE_AUTHORIZATION_GRANTS_MANAGE,
|
|
CORE_MODULES_UI_READ,
|
|
CORE_DASHBOARD_NAVIGATION_MANAGE,
|
|
CORE_MODULES_LIFECYCLE_MANAGE,
|
|
CORE_MODULES_CONFIGURATION_MANAGE,
|
|
CORE_DEVICES_MANAGE,
|
|
"calendar.events.read",
|
|
"calendar.events.write",
|
|
"people.directory.read",
|
|
]);
|
|
});
|
|
|
|
it.each([
|
|
"public",
|
|
"authenticated",
|
|
"restricted",
|
|
] as const)("preserves the exact %s baseline and description of a module declaration", (baseline) => {
|
|
const calendar = fakeModule("calendar", [
|
|
{ id: "calendar.events.read", baseline, description: "Read calendar events." },
|
|
]);
|
|
|
|
const registry = composePlatformPermissionRegistry([calendar]);
|
|
|
|
expect(registry.get("calendar.events.read")).toEqual({
|
|
id: "calendar.events.read",
|
|
baseline,
|
|
description: "Read calendar events.",
|
|
});
|
|
});
|
|
|
|
it("still fails closed for a permission no module or Core ever declared", () => {
|
|
const registry = composePlatformPermissionRegistry([]);
|
|
|
|
expect(registry.has("calendar.events.read")).toBe(false);
|
|
expect(registry.get("calendar.events.read")).toBeUndefined();
|
|
});
|
|
|
|
it("fails composition on an exact duplicate permission id supplied by two module entries", () => {
|
|
// A permission id must start with its own module's id, so a genuine cross-module collision is
|
|
// only reachable if the supplied list itself contains the same module id twice — real
|
|
// discovery already guarantees module-id uniqueness (`MODULE_ID_DUPLICATE`), but composition
|
|
// re-derives duplicate detection from the flat declaration list regardless, rather than
|
|
// trusting that upstream guarantee alone.
|
|
const first = fakeModule("calendar", [
|
|
{ id: "calendar.events.read", baseline: "restricted", description: "Read events." },
|
|
]);
|
|
const second = fakeModule("calendar", [
|
|
{ id: "calendar.events.read", baseline: "public", description: "Also read events." },
|
|
]);
|
|
|
|
expect(() => composePlatformPermissionRegistry([first, second])).toThrow(
|
|
PermissionRegistryError,
|
|
);
|
|
});
|
|
|
|
it("rejects a module contributing a permission in the reserved core.* namespace, without leaking a module path", () => {
|
|
// Only a module whose own id is literally "core" can produce a "core."-prefixed permission
|
|
// that still passes the manifest's own module-namespace check — this proves composition
|
|
// catches that case rather than trusting the manifest's self-consistency alone.
|
|
const impostor = fakeModule("core", [
|
|
{ id: "core.rogue.permission", baseline: "restricted", description: "Not really Core's." },
|
|
]);
|
|
|
|
let captured: unknown;
|
|
try {
|
|
composePlatformPermissionRegistry([impostor]);
|
|
} catch (error) {
|
|
captured = error;
|
|
}
|
|
|
|
expect(captured).toBeInstanceOf(ModulePermissionCompositionError);
|
|
expect(captured).toMatchObject({
|
|
code: "MODULE_PERMISSION_RESERVED_NAMESPACE",
|
|
moduleId: "core",
|
|
permissionId: "core.rogue.permission",
|
|
});
|
|
const publicText = String(captured);
|
|
expect(publicText).not.toContain("/modules/core");
|
|
expect(publicText).not.toContain("ekklesia.module.json");
|
|
});
|
|
|
|
it("rejects a reserved-namespace collision even when it would not otherwise duplicate an existing id", () => {
|
|
const impostor = fakeModule("core", [
|
|
{
|
|
id: "core.authorization.grants.manage",
|
|
baseline: "restricted",
|
|
description: "Attempting to shadow Core's own permission.",
|
|
},
|
|
]);
|
|
|
|
expect(() => composePlatformPermissionRegistry([impostor])).toThrow(
|
|
ModulePermissionCompositionError,
|
|
);
|
|
});
|
|
|
|
it("removes a module's permissions from the resulting registry once that module is absent from the supplied list", () => {
|
|
const calendar = fakeModule("calendar", [
|
|
{ id: "calendar.events.read", baseline: "restricted", description: "Read events." },
|
|
]);
|
|
|
|
const withCalendar = composePlatformPermissionRegistry([calendar]);
|
|
expect(withCalendar.has("calendar.events.read")).toBe(true);
|
|
|
|
const withoutCalendar = composePlatformPermissionRegistry([]);
|
|
expect(withoutCalendar.has("calendar.events.read")).toBe(false);
|
|
});
|
|
|
|
it("performs no I/O: composition is synchronous and never imports a module's entrypoint", () => {
|
|
const calendar = fakeModule("calendar", [
|
|
{ id: "calendar.events.read", baseline: "restricted", description: "Read events." },
|
|
]);
|
|
|
|
// A synchronous return (not a Promise) from a fixture whose entrypoint deliberately resolves
|
|
// to nothing on disk is direct evidence no filesystem or module-code operation occurred.
|
|
const result = composePlatformPermissionRegistry([calendar]);
|
|
expect(result).not.toBeInstanceOf(Promise);
|
|
expect(result.has("calendar.events.read")).toBe(true);
|
|
});
|
|
|
|
it("does not mutate the supplied module list or Core's own declaration list", () => {
|
|
const calendar = fakeModule("calendar", [
|
|
{ id: "calendar.events.read", baseline: "restricted", description: "Read events." },
|
|
]);
|
|
const modules = Object.freeze([calendar]);
|
|
const coreDeclarationsBefore = [...CORE_PERMISSION_DECLARATIONS];
|
|
|
|
composePlatformPermissionRegistry(modules);
|
|
|
|
expect(modules).toEqual([calendar]);
|
|
expect(CORE_PERMISSION_DECLARATIONS).toEqual(coreDeclarationsBefore);
|
|
});
|
|
|
|
it("returns a registry and declarations that remain immutable, including module-contributed ones", () => {
|
|
const calendar = fakeModule("calendar", [
|
|
{ id: "calendar.events.read", baseline: "restricted", description: "Read events." },
|
|
]);
|
|
|
|
const registry = composePlatformPermissionRegistry([calendar]);
|
|
|
|
expect(Object.isFrozen(registry)).toBe(true);
|
|
expect(Object.isFrozen(registry.declarations)).toBe(true);
|
|
for (const declaration of registry.declarations) {
|
|
expect(Object.isFrozen(declaration)).toBe(true);
|
|
}
|
|
});
|
|
});
|