Files
Ekklesia/core/tests/module-storage-lifecycle.test.ts
T
ekklesiaandClaude Opus 5 c444ff570b feat: let one module contribute a browser renderer to another's capability
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]>
2026-08-27 07:47:10 -05:00

311 lines
11 KiB
TypeScript

import { parseModuleManifest } from "@ekklesia/contracts";
import { describe, expect, it } from "vitest";
import type { DiscoveredModule } from "../src/modules/discovery.js";
import {
isStorageBlockedDetail,
resolveModuleEligibility,
resolveModuleLifecycleView,
} from "../src/modules/lifecycle.js";
/** A minimal in-memory `DiscoveredModule`, built through `parseModuleManifest` so a fixture real
* discovery would reject can never stand in for one it would accept. */
function fakeModule(
id: string,
options: {
readonly buckets?: readonly string[];
readonly dependencies?: Record<string, unknown>;
readonly configuration?: readonly Record<string, unknown>[];
} = {},
): DiscoveredModule {
const manifest = parseModuleManifest({
manifestVersion: 7,
id,
name: `${id} module`,
version: "1.0.0",
compatibility: { platform: "^0.1.0", moduleSdk: "^0.1.0" },
...(options.configuration ? { configuration: options.configuration } : {}),
...(options.dependencies ? { dependencies: options.dependencies } : {}),
...(options.buckets ? { storage: { buckets: options.buckets.map((id) => ({ id })) } } : {}),
entrypoints: { 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}`,
};
}
function enablement(...records: readonly (readonly [string, boolean])[]) {
return { modules: records.map(([moduleId, enabled]) => ({ moduleId, enabled })) };
}
const noConfiguration = { settings: { records: [] } };
const withStorage = { providerConfigured: true };
const withoutStorage = { providerConfigured: false };
function ids(modules: readonly DiscoveredModule[]): readonly string[] {
return modules.map((module) => module.manifest.id);
}
function entry(
resolution: ReturnType<typeof resolveModuleEligibility>,
moduleId: string,
): ReturnType<typeof resolveModuleEligibility>["entries"][number] {
const found = resolution.entries.find((candidate) => candidate.moduleId === moduleId);
if (!found) {
throw new Error(`No eligibility entry for ${moduleId}.`);
}
return found;
}
describe("declared storage decides eligibility", () => {
it("keeps a module requiring storage eligible when a provider is configured", () => {
const resolution = resolveModuleEligibility(
[fakeModule("announcements", { buckets: ["announcement-assets"] })],
enablement(),
noConfiguration,
withStorage,
);
expect(ids(resolution.eligibleModules)).toEqual(["announcements"]);
expect(entry(resolution, "announcements").blocked).toBeUndefined();
});
it("blocks a module requiring storage when the host has no provider, naming the logical bucket", () => {
const resolution = resolveModuleEligibility(
[fakeModule("announcements", { buckets: ["announcement-assets"] })],
enablement(),
noConfiguration,
withoutStorage,
);
expect(resolution.eligibleModules).toEqual([]);
expect(entry(resolution, "announcements").blocked).toEqual({
reason: "storage_unavailable",
storageBucketId: "announcement-assets",
});
});
it("leaves a module declaring no buckets entirely unaffected by storage", () => {
const resolution = resolveModuleEligibility(
[fakeModule("calendar")],
enablement(),
noConfiguration,
withoutStorage,
);
// An installation with no object storage is a complete, valid deployment.
expect(ids(resolution.eligibleModules)).toEqual(["calendar"]);
expect(entry(resolution, "calendar").blocked).toBeUndefined();
});
it("does not evaluate a disabled module's storage requirement", () => {
const resolution = resolveModuleEligibility(
[fakeModule("announcements", { buckets: ["announcement-assets"] })],
enablement(["announcements", false]),
noConfiguration,
withoutStorage,
);
// Disabled is not blocked: reporting a storage problem for something nobody asked to run would
// be noise.
expect(entry(resolution, "announcements").blocked).toBeUndefined();
expect(entry(resolution, "announcements").configured.enabled).toBe(false);
});
it("defaults to no provider when the caller supplies no storage input at all", () => {
// Every pre-existing caller keeps its exact behaviour: the parameter is optional, and a module
// that declares no bucket is unaffected either way.
const withoutArgument = resolveModuleEligibility([fakeModule("calendar")], enablement());
expect(ids(withoutArgument.eligibleModules)).toEqual(["calendar"]);
const requiring = resolveModuleEligibility(
[fakeModule("announcements", { buckets: ["assets"] })],
enablement(),
);
expect(requiring.eligibleModules).toEqual([]);
});
it("names only the logical bucket — never an endpoint, credential, or physical bucket", () => {
const resolution = resolveModuleEligibility(
[fakeModule("announcements", { buckets: ["announcement-assets"] })],
enablement(),
noConfiguration,
withoutStorage,
);
const serialized = JSON.stringify(resolution.entries);
expect(serialized).toContain("announcement-assets");
expect(serialized).not.toContain("ekklesia-announcements--");
expect(serialized).not.toContain("http");
expect(serialized).not.toMatch(/accessKey|secretKey|endpoint/iu);
});
});
describe("storage blocking propagates through the dependency graph", () => {
const graph = [
fakeModule("media-storage", { buckets: ["objects"] }),
fakeModule("media", { dependencies: { modules: { required: { "media-storage": "^1.0.0" } } } }),
fakeModule("announcements", { dependencies: { modules: { required: { media: "^1.0.0" } } } }),
];
it("blocks required dependents of a storage-blocked module, transitively", () => {
const resolution = resolveModuleEligibility(
graph,
enablement(),
noConfiguration,
withoutStorage,
);
expect(resolution.eligibleModules).toEqual([]);
// One mechanism, not two: the chain terminates at the module whose own requirement is
// unsatisfiable, exactly as it terminates at a disabled or misconfigured module.
expect(entry(resolution, "media-storage").blocked).toEqual({
reason: "storage_unavailable",
storageBucketId: "objects",
});
expect(entry(resolution, "media").blocked).toEqual({
dependency: { kind: "module", moduleId: "media-storage" },
reason: "required_dependency_blocked",
});
expect(entry(resolution, "announcements").blocked).toEqual({
dependency: { kind: "module", moduleId: "media" },
reason: "required_dependency_blocked",
});
});
it("unblocks the whole chain once a provider is configured", () => {
const resolution = resolveModuleEligibility(graph, enablement(), noConfiguration, withStorage);
expect(ids(resolution.eligibleModules)).toEqual(["media-storage", "media", "announcements"]);
});
it("never blocks an unrelated module because another module needs storage", () => {
const resolution = resolveModuleEligibility(
[...graph, fakeModule("calendar")],
enablement(),
noConfiguration,
withoutStorage,
);
expect(ids(resolution.eligibleModules)).toEqual(["calendar"]);
});
});
describe("storage and configuration blocking coexist without a second mechanism", () => {
it("reports the storage requirement first when both are unsatisfiable", () => {
const resolution = resolveModuleEligibility(
[
fakeModule("announcements", {
buckets: ["assets"],
configuration: [{ key: "bucketLabel", type: "string", required: true }],
}),
],
enablement(),
noConfiguration,
withoutStorage,
);
// Deterministic ordering — storage is evaluated before configuration — so an operator always
// sees the same reason for the same state rather than one that depends on evaluation order.
const blocked = entry(resolution, "announcements").blocked;
expect(blocked && isStorageBlockedDetail(blocked)).toBe(true);
});
it("falls through to the configuration reason once storage is available", () => {
const resolution = resolveModuleEligibility(
[
fakeModule("announcements", {
buckets: ["assets"],
configuration: [{ key: "bucketLabel", type: "string", required: true }],
}),
],
enablement(),
noConfiguration,
withStorage,
);
expect(entry(resolution, "announcements").blocked).toEqual({
configurationKey: "bucketLabel",
reason: "configuration_missing",
});
});
});
describe("the lifecycle view reports storage state honestly", () => {
const discoveredModules = [fakeModule("announcements", { buckets: ["announcement-assets"] })];
function bootSnapshot(storage: { providerConfigured: boolean }) {
const resolution = resolveModuleEligibility(
discoveredModules,
enablement(),
noConfiguration,
storage,
);
return {
discoveredModules,
eligibility: resolution.entries,
startedModuleIds: resolution.eligibleModules.map((module) => module.manifest.id),
};
}
it("reports a storage-blocked module with the logical bucket and no pending restart", () => {
const view = resolveModuleLifecycleView(
bootSnapshot(withoutStorage),
enablement(),
noConfiguration,
withoutStorage,
);
expect(view[0]?.runtime).toEqual({
reason: "storage_unavailable",
state: "blocked",
storageBucketId: "announcement-assets",
});
expect(view[0]?.restartRequired).toBe(false);
});
it("reports a pending restart once a provider is configured for a blocked module", () => {
const view = resolveModuleLifecycleView(
bootSnapshot(withoutStorage),
enablement(),
noConfiguration,
withStorage,
);
// The next boot really will start it, so the honest answer is that a restart is pending — while
// the runtime line still describes what *this* process did.
expect(view[0]?.runtime.state).toBe("blocked");
expect(view[0]?.restartRequired).toBe(true);
});
it("reports a running module with storage as ordinary and unremarkable", () => {
const view = resolveModuleLifecycleView(
bootSnapshot(withStorage),
enablement(),
noConfiguration,
withStorage,
);
expect(view[0]?.runtime).toEqual({ state: "running" });
expect(view[0]?.restartRequired).toBe(false);
});
it("puts no infrastructure detail into the view", () => {
const view = resolveModuleLifecycleView(
bootSnapshot(withoutStorage),
enablement(),
noConfiguration,
withoutStorage,
);
const serialized = JSON.stringify(view);
expect(serialized).toContain("announcement-assets");
expect(serialized).not.toContain("ekklesia-announcements--");
expect(serialized).not.toMatch(/http|accessKey|secretKey|seaweed/iu);
});
});