Ekklesia had no notion of a module being on or off: the accepted module set was exactly "whatever discovery found on disk", and `ModuleLifecycleStage` named nine states nothing produced or consumed. This replaces that vocabulary with two axes that real code produces — persistent, administrator-owned configuration (`enabled`/`disabled`) and per-boot runtime observation (`running`/`disabled`/`blocked`) — and wires them through startup. - One Core table (`ekklesia_core_module_state`, migration 0004) holding only `module_id` and `enabled`. A module with no record is enabled by default, so deploying a module is still sufficient to run it and discovery never needs an initialization write. A record for an undeployed module is retained and ignored, so redeploying restores the decision. - `resolveModuleEligibility` is pure: a disabled module transitively blocks every module that *requires* it (module and capability edges alike) with a structured reason naming the provider; optional dependencies never block; and a missing, incompatible, or cyclic dependency stays discovery's concern. - `startApi` starts only the eligible set and builds the UI publication registry from that same list, so routes, artifacts, browser inventory, and Dashboard navigation all follow one authoritative set. A disabled module is never imported, so there is no per-request enablement check anywhere. - Startup stays all-or-nothing, deliberately; there is consequently no `failed` runtime state. `pnpm modules:enable`/`modules:disable` is the out-of-band recovery path for a module that breaks startup. - Changes take effect at the next start, and the read model reports configuration, runtime, and `restartRequired` separately rather than claiming a module stopped when it has not. - Migration participation and permission composition still span the deployed and discovered inventories respectively, unchanged. - New restricted platform permission `core.modules.lifecycle.manage` gates both reading and changing enablement, at a sibling prefix (`/api/v1/module-lifecycle`) that cannot collide with the module route namespace. - `pnpm doctor` gains a `modules.lifecycle` check so "why is this module unavailable?" is answerable without reading logs. See ADR 0028. Co-Authored-By: Claude Opus 5 <[email protected]>
168 lines
6.8 KiB
TypeScript
168 lines
6.8 KiB
TypeScript
import { parsePermissionId } from "@ekklesia/contracts";
|
|
import { describe, expect, it } from "vitest";
|
|
import {
|
|
CORE_AUTHORIZATION_GRANTS_MANAGE,
|
|
CORE_DASHBOARD_NAVIGATION_MANAGE,
|
|
CORE_MODULES_LIFECYCLE_MANAGE,
|
|
CORE_MODULES_UI_READ,
|
|
CORE_PERMISSION_DECLARATIONS,
|
|
CORE_PERMISSION_REGISTRY,
|
|
createPermissionRegistry,
|
|
PermissionRegistryError,
|
|
} from "../src/authorization/index.js";
|
|
|
|
describe("createPermissionRegistry", () => {
|
|
it("builds an immutable registry from a fixed declaration list", () => {
|
|
const id = parsePermissionId("test.example.read");
|
|
const registry = createPermissionRegistry([
|
|
{ baseline: "public", description: "An example permission.", id },
|
|
]);
|
|
|
|
expect(registry.has(id)).toBe(true);
|
|
expect(registry.get(id)).toEqual({
|
|
baseline: "public",
|
|
description: "An example permission.",
|
|
id,
|
|
});
|
|
expect(Object.isFrozen(registry)).toBe(true);
|
|
expect(Object.isFrozen(registry.declarations)).toBe(true);
|
|
expect(Object.isFrozen(registry.get(id))).toBe(true);
|
|
});
|
|
|
|
it("rejects a duplicate permission id at composition time, not per request", () => {
|
|
const id = parsePermissionId("test.example.read");
|
|
expect(() =>
|
|
createPermissionRegistry([
|
|
{ baseline: "public", description: "First.", id },
|
|
{ baseline: "restricted", description: "Second.", id },
|
|
]),
|
|
).toThrow(PermissionRegistryError);
|
|
});
|
|
|
|
it("carries the offending permission id on the duplicate-declaration error (a composition-time, developer-facing detail, not a runtime response)", () => {
|
|
const id = parsePermissionId("test.example.read");
|
|
let captured: unknown;
|
|
try {
|
|
createPermissionRegistry([
|
|
{ baseline: "public", description: "First.", id },
|
|
{ baseline: "restricted", description: "Second.", id },
|
|
]);
|
|
} catch (error) {
|
|
captured = error;
|
|
}
|
|
expect(captured).toBeInstanceOf(PermissionRegistryError);
|
|
expect(captured).toMatchObject({
|
|
code: "PERMISSION_REGISTRY_DUPLICATE_DECLARATION",
|
|
permissionId: id,
|
|
});
|
|
});
|
|
|
|
it("treats an unregistered permission the same as a malformed one: get/has both fail closed", () => {
|
|
const registered = parsePermissionId("test.example.read");
|
|
const unregistered = parsePermissionId("test.example.unregistered");
|
|
const registry = createPermissionRegistry([
|
|
{ baseline: "public", description: "Registered.", id: registered },
|
|
]);
|
|
|
|
expect(registry.has(unregistered)).toBe(false);
|
|
expect(registry.get(unregistered)).toBeUndefined();
|
|
});
|
|
|
|
it("fails closed for a malformed permission id passed to get/has, without throwing", () => {
|
|
const registry = createPermissionRegistry([]);
|
|
|
|
expect(registry.has("not a valid permission")).toBe(false);
|
|
expect(registry.get(null)).toBeUndefined();
|
|
expect(registry.get(undefined)).toBeUndefined();
|
|
expect(registry.get(42)).toBeUndefined();
|
|
expect(registry.get("media.*")).toBeUndefined();
|
|
});
|
|
|
|
it("builds an empty registry from an empty list without error", () => {
|
|
const registry = createPermissionRegistry([]);
|
|
expect(registry.declarations).toEqual([]);
|
|
});
|
|
});
|
|
|
|
describe("CORE_PERMISSION_REGISTRY", () => {
|
|
it("registers exactly core.authorization.grants.manage as restricted", () => {
|
|
const declaration = CORE_PERMISSION_REGISTRY.get(CORE_AUTHORIZATION_GRANTS_MANAGE);
|
|
expect(declaration).toBeDefined();
|
|
expect(declaration?.baseline).toBe("restricted");
|
|
expect(declaration?.id).toBe("core.authorization.grants.manage");
|
|
});
|
|
|
|
it("registers core.modules.ui.read as authenticated, not restricted", () => {
|
|
const declaration = CORE_PERMISSION_REGISTRY.get(CORE_MODULES_UI_READ);
|
|
expect(declaration).toBeDefined();
|
|
// Any signed-in user may learn which module UIs the browser host can mount; a shell that
|
|
// rendered nothing until an operator granted each user a permission would be unusable, and the
|
|
// module UIs gate their own content on their own permissions regardless (ADR 0026).
|
|
expect(declaration?.baseline).toBe("authenticated");
|
|
expect(declaration?.id).toBe("core.modules.ui.read");
|
|
});
|
|
|
|
it("registers core.dashboard.navigation.manage as restricted", () => {
|
|
const declaration = CORE_PERMISSION_REGISTRY.get(CORE_DASHBOARD_NAVIGATION_MANAGE);
|
|
expect(declaration).toBeDefined();
|
|
// Reorganizing the Dashboard is administrative, not something every signed-in user may do —
|
|
// unlike merely reading which module UIs exist (ADR 0027).
|
|
expect(declaration?.baseline).toBe("restricted");
|
|
expect(declaration?.id).toBe("core.dashboard.navigation.manage");
|
|
});
|
|
|
|
it("registers core.modules.lifecycle.manage as restricted", () => {
|
|
const declaration = CORE_PERMISSION_REGISTRY.get(CORE_MODULES_LIFECYCLE_MANAGE);
|
|
expect(declaration).toBeDefined();
|
|
// Deciding which installed modules run is administrative, and so is *reading* that: the
|
|
// lifecycle view is the only place a disabled or blocked module is visible at all, unlike the
|
|
// merely-authenticated mountable inventory (ADR 0028).
|
|
expect(declaration?.baseline).toBe("restricted");
|
|
expect(declaration?.id).toBe("core.modules.lifecycle.manage");
|
|
});
|
|
|
|
it("contains exactly these four permissions and no others", () => {
|
|
expect(CORE_PERMISSION_REGISTRY.declarations.map((declaration) => declaration.id)).toEqual([
|
|
CORE_AUTHORIZATION_GRANTS_MANAGE,
|
|
CORE_MODULES_UI_READ,
|
|
CORE_DASHBOARD_NAVIGATION_MANAGE,
|
|
CORE_MODULES_LIFECYCLE_MANAGE,
|
|
]);
|
|
});
|
|
});
|
|
|
|
describe("CORE_PERMISSION_DECLARATIONS", () => {
|
|
it("is Core's own declaration list, exported separately from the registry in deeply immutable form", () => {
|
|
expect(CORE_PERMISSION_DECLARATIONS).toEqual([
|
|
{
|
|
baseline: "restricted",
|
|
description: "Create, list, and revoke another user's explicit authorization grants.",
|
|
id: CORE_AUTHORIZATION_GRANTS_MANAGE,
|
|
},
|
|
{
|
|
baseline: "authenticated",
|
|
description: "List the installed module UIs a browser host may mount.",
|
|
id: CORE_MODULES_UI_READ,
|
|
},
|
|
{
|
|
baseline: "restricted",
|
|
description: "Organize the Dashboard: create, rename, order, and delete navigation groups.",
|
|
id: CORE_DASHBOARD_NAVIGATION_MANAGE,
|
|
},
|
|
{
|
|
baseline: "restricted",
|
|
description: "Read and change which installed modules are enabled.",
|
|
id: CORE_MODULES_LIFECYCLE_MANAGE,
|
|
},
|
|
]);
|
|
expect(Object.isFrozen(CORE_PERMISSION_DECLARATIONS)).toBe(true);
|
|
for (const declaration of CORE_PERMISSION_DECLARATIONS) {
|
|
expect(Object.isFrozen(declaration)).toBe(true);
|
|
}
|
|
});
|
|
|
|
it("matches exactly what CORE_PERMISSION_REGISTRY exposes as its own declarations, so a composer building on top of it never diverges from what Core itself registers", () => {
|
|
expect(CORE_PERMISSION_DECLARATIONS).toEqual(CORE_PERMISSION_REGISTRY.declarations);
|
|
});
|
|
});
|