Ekklesia's module model has been backend-only since ADR 0001: a module owns domain behavior and data, and every presentation concern belongs to a surface. That prevents a module author — the person who knows the domain — from shipping the screen that makes their module useful, and it guarantees a module and its own UI drift apart. It also pushes toward two codebases for one domain the moment a module needs to run standalone. ADR 0021 replaces that rule. A module is an independently versioned product that owns its domain behavior, data, permissions, routes, migrations, and its own UI, hostable inside Ekklesia and — from the same implementation — by a later standalone host. Core keeps the containing application shell and every platform policy: authentication, authorization enforcement, platform context, top-level routing, navigation, global theme, error boundaries, and trusted-origin/CORS. The module graph stays flat: modules are peers, genuine module and capability dependencies remain the only relationships, and Dashboard folders are presentation configuration an administrator can reorganize, never module architecture. This pass implements the static contract only. `manifestVersion` moves to 4 and `entrypoints` gains one optional field, `ui`. Version 3's `entrypoints` is strict and rejects `ui`, so this is an incompatible shape change and version 3 is now rejected outright rather than reinterpreted, exactly as version 1 was by version 2 and version 2 by version 3. Every field version 4 adds is optional, so upgrading a backend-only manifest is a one-character edit. Discovery resolves and validates `ui` under the same rules as `server` and `schema` — package-relative, inside the package before and after symlink resolution, a real file, compiled ESM `.js`/`.mjs`, so source TypeScript is rejected rather than assumed to be compiled later. Discovery stays inert: nothing imports, evaluates, mounts, or renders a UI entrypoint, and a test proves a throwing artifact still discovers successfully. This follows `entrypoints.jobs`, declared and resolved since ADR 0006 with no runtime consumer. The name is `ui`, not `dashboard`, because a module's UI must be hostable by Ekklesia's shell and by a standalone host, so the contract cannot be named after one of them; and not `surface`, because `surfaces/*` is a separate ownership layer. No widget, route, icon, folder, menu, layout, or standalone-metadata field is added, and tests assert the manifest still rejects every unknown entrypoint key and offers no hierarchy field. Deliberately unchanged: `scripts/check-boundaries.ts` still enforces `modules-do-not-own-ui`. Relaxing that guard depends entirely on the UI authoring model a later pass chooses, and weakening a working protection before that decision exists would trade real coverage for nothing. A module may therefore declare `ui` today only for a framework-neutral compiled artifact, and the docs say so rather than implying otherwise. Restart-based discovery, explicit operator-invoked migrations, and the trusted compiled-module model are untouched — a module that ships UI is exactly as trusted as one that does not. No Dashboard shell, UI runtime, renderer, framework choice, navigation registry, grouping system, standalone host, installer, hot-loading, or feature module is added by this pass. Co-Authored-By: Claude Opus 5 <[email protected]>
224 lines
9.0 KiB
TypeScript
224 lines
9.0 KiB
TypeScript
import { type PermissionBaseline, parseModuleManifest } from "@ekklesia/contracts";
|
|
import { describe, expect, it } from "vitest";
|
|
import {
|
|
CORE_AUTHORIZATION_GRANTS_MANAGE,
|
|
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: 4,
|
|
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 {
|
|
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("calendar.events.read")).toBe(true);
|
|
expect(registry.has("people.directory.read")).toBe(true);
|
|
expect(registry.declarations).toHaveLength(3);
|
|
});
|
|
|
|
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,
|
|
"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);
|
|
}
|
|
});
|
|
});
|