Files
Ekklesia/core/tests/core-migration-discovery.test.ts
T
ekklesiaandClaude Opus 5 baa689b2a7
CI / verify (pull_request) Successful in 35s
CI / infrastructure-smoke (pull_request) Successful in 21s
CI / migration-smoke (pull_request) Successful in 46s
feat: add the module configuration and secrets contract
Modules had no way to be told anything. Announcements — the first real
product Ekklesia will absorb — has zero environment variables and compiles
a LAN IP address into its source, so this is the contract standing between
it and the module system.

- A module declares what it needs in its manifest, using six bounded types
  (string, integer, boolean, enum, url, secret) with no nesting and no
  regular expressions. Static declaration is what lets the platform validate
  configuration *before* deciding whether a module may start: an enabled
  module missing a required value is reported `blocked ·
  configuration_missing · <key>` rather than taking startup down from inside
  its own `start` hook, and Doctor reports the same while still importing no
  module code. That forces `manifestVersion: 5`, a hard cutover.
- Ordinary values persist in one Core table (migration 0005), one row per
  (module_id, key), stored as text until validated against the declaring
  module's current manifest. Nothing is coerced; an invalid stored value
  blocks the module rather than falling back to the default. A key the
  manifest no longer declares is retained and ignored.
- Secrets are never persisted. There is no encryption at rest in this
  repository, so a `secret` key is deployment-supplied and mapped in by the
  host under a name the module never sees. A secret declaration has no
  `default` field, the write API refuses secret material, and the HTTP view
  has no `value` field for one — absent by construction, not filtered.
- Modules receive a frozen `context.config` on the registration, runtime,
  and HTTP handler contexts: the same object identity, per module, beside
  `services` rather than inside it (services is deliberately one shared
  instance). No environment access, no database reference, no way to reach
  another module's values.
- Configuration decides eligibility through ADR 0028's existing resolver:
  three new blocked reasons, each with a distinct operator fix, propagating
  to required dependents through the same mechanism as a disabled module.
- Changes take effect at the next restart; both resources report
  restartRequired. New restricted permission
  `core.modules.configuration.manage`, separate from lifecycle, at the
  sibling prefix `/api/v1/module-configuration`.
- Migration participation stays independent of configuration validity.
  Endpoints and credentials for shared infrastructure stay platform
  concerns; a module declares which bucket it needs, not an S3 endpoint.

See ADR 0029.

Co-Authored-By: Claude Opus 5 <[email protected]>
2026-08-22 21:44:43 -05:00

244 lines
8.3 KiB
TypeScript

import { createHash } from "node:crypto";
import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import path from "node:path";
import { afterEach, describe, expect, it } from "vitest";
import {
CORE_MIGRATION_SOURCE_ID,
discoverAllMigrations,
discoverCoreMigrations,
} from "../src/migrations/index.js";
import { ModuleMigrationDiscoveryError } from "../src/modules/index.js";
const originSnapshotId = "00000000-0000-0000-0000-000000000000";
const temporaryDirectories: string[] = [];
afterEach(async () => {
await Promise.all(
temporaryDirectories
.splice(0)
.map((directory) => rm(directory, { force: true, recursive: true })),
);
});
async function createTemporaryDirectory(prefix: string): Promise<string> {
const directory = await mkdtemp(path.join(tmpdir(), prefix));
temporaryDirectories.push(directory);
return directory;
}
/** Writes a single-migration Drizzle directory (journal + snapshot + policy + SQL). */
async function writeMigrationsDirectory(
migrationsDirectory: string,
moduleId: string,
tag: string,
sql: string,
): Promise<string> {
await mkdir(path.join(migrationsDirectory, "meta"), { recursive: true });
await Promise.all([
writeFile(
path.join(migrationsDirectory, "meta/_journal.json"),
JSON.stringify({
version: "7",
dialect: "postgresql",
entries: [{ idx: 0, version: "7", when: 1_700_000_000_000, tag, breakpoints: true }],
}),
),
writeFile(
path.join(migrationsDirectory, "meta/_ekklesia.json"),
JSON.stringify({
version: 1,
moduleId,
entries: [{ tag, destructive: { mode: "none" } }],
}),
),
writeFile(
path.join(migrationsDirectory, "meta/0000_snapshot.json"),
JSON.stringify({
version: "7",
dialect: "postgresql",
id: "10000000-0000-4000-8000-000000000001",
prevId: originSnapshotId,
}),
),
writeFile(path.join(migrationsDirectory, `${tag}.sql`), sql),
]);
return sql;
}
async function createModule(
modulesDirectory: string,
id: string,
options: { readonly dependsOn?: readonly string[] } = {},
): Promise<void> {
const moduleDirectory = path.join(modulesDirectory, id);
const migrationsDirectory = path.join(moduleDirectory, "drizzle");
await mkdir(path.join(moduleDirectory, "dist/server"), { recursive: true });
const requiredDependencies = Object.fromEntries(
(options.dependsOn ?? []).map((dependencyId) => [dependencyId, "^0.1.0"]),
);
await Promise.all([
writeFile(
path.join(moduleDirectory, "package.json"),
JSON.stringify({
name: `@test/module-${id}`,
version: "0.1.0",
type: "module",
ekklesia: { layer: "module" },
}),
),
writeFile(
path.join(moduleDirectory, "ekklesia.module.json"),
JSON.stringify({
manifestVersion: 5,
id,
name: `${id} module`,
version: "0.1.0",
compatibility: { platform: "^0.1.0", moduleSdk: "^0.1.0" },
dependencies: {
modules: { required: requiredDependencies, optional: {} },
capabilities: { required: {}, optional: {} },
},
capabilities: [],
permissions: [],
entrypoints: { server: "./dist/server/index.js", migrations: "./drizzle" },
}),
),
writeFile(
path.join(moduleDirectory, "dist/server/index.js"),
`export default { id: ${JSON.stringify(id)}, version: "0.1.0" };`,
),
]);
await writeMigrationsDirectory(
migrationsDirectory,
id,
"0000_initial",
`CREATE TABLE "${id.replaceAll("-", "_")}_records" ("id" integer);\n`,
);
}
function sha256(contents: string): string {
return createHash("sha256").update(contents).digest("hex");
}
describe("Core-owned migration discovery", () => {
it("discovers Core's own migrations with the reserved core identity and an exact checksum", async () => {
const coreMigrationsDirectory = await createTemporaryDirectory("ekklesia-core-migrations-");
const sql = await writeMigrationsDirectory(
coreMigrationsDirectory,
"core",
"0000_identity_core_tables",
'CREATE TABLE "ekklesia_core_user" ("id" text PRIMARY KEY);\n',
);
const migrations = await discoverCoreMigrations({ coreMigrationsDirectory });
expect(CORE_MIGRATION_SOURCE_ID).toBe("core");
expect(migrations).toHaveLength(1);
expect(migrations[0]).toMatchObject({
approvalRequired: false,
checksum: sha256(sql),
identity: "core:0000_identity_core_tables",
index: 0,
moduleId: "core",
tag: "0000_identity_core_tables",
});
});
it("is deterministic across repeated discovery calls", async () => {
const coreMigrationsDirectory = await createTemporaryDirectory("ekklesia-core-migrations-");
await writeMigrationsDirectory(
coreMigrationsDirectory,
"core",
"0000_identity_core_tables",
'CREATE TABLE "ekklesia_core_user" ("id" text PRIMARY KEY);\n',
);
const first = await discoverCoreMigrations({ coreMigrationsDirectory });
const second = await discoverCoreMigrations({ coreMigrationsDirectory });
expect(first).toEqual(second);
});
it("orders Core migrations before every module migration while preserving dependency-first module order", async () => {
const modulesDirectory = await createTemporaryDirectory("ekklesia-modules-");
const coreMigrationsDirectory = await createTemporaryDirectory("ekklesia-core-migrations-");
await writeMigrationsDirectory(
coreMigrationsDirectory,
"core",
"0000_identity_core_tables",
'CREATE TABLE "ekklesia_core_user" ("id" text PRIMARY KEY);\n',
);
// "calendar" depends on "people"; created on disk in the opposite order so only the
// dependency graph (not directory order) can explain the resulting sequence.
await createModule(modulesDirectory, "calendar", { dependsOn: ["people"] });
await createModule(modulesDirectory, "people");
const aggregate = await discoverAllMigrations({
coreMigrationsDirectory,
moduleDiscovery: { moduleSdkVersion: "0.1.0", modulesDirectory, platformVersion: "0.1.0" },
});
expect(aggregate.modules.map((module) => module.moduleId)).toEqual([
"core",
"people",
"calendar",
]);
expect(aggregate.migrations.map((migration) => migration.identity)).toEqual([
"core:0000_identity_core_tables",
"people:0000_initial",
"calendar:0000_initial",
]);
});
it("rejects an installed module that reuses the reserved core migration identity", async () => {
const modulesDirectory = await createTemporaryDirectory("ekklesia-modules-");
const coreMigrationsDirectory = await createTemporaryDirectory("ekklesia-core-migrations-");
await writeMigrationsDirectory(
coreMigrationsDirectory,
"core",
"0000_identity_core_tables",
'CREATE TABLE "ekklesia_core_user" ("id" text PRIMARY KEY);\n',
);
await createModule(modulesDirectory, "core");
let captured: unknown;
try {
await discoverAllMigrations({
coreMigrationsDirectory,
moduleDiscovery: { moduleSdkVersion: "0.1.0", modulesDirectory, platformVersion: "0.1.0" },
});
} catch (error) {
captured = error;
}
expect(captured).toBeInstanceOf(ModuleMigrationDiscoveryError);
expect((captured as ModuleMigrationDiscoveryError).issues).toEqual([
expect.objectContaining({ code: "MODULE_MIGRATION_ID_DUPLICATE", moduleId: "core" }),
]);
});
it("keeps a fresh, module-free repository honestly reporting only Core's migration", async () => {
const modulesDirectory = await createTemporaryDirectory("ekklesia-modules-");
const coreMigrationsDirectory = await createTemporaryDirectory("ekklesia-core-migrations-");
await writeMigrationsDirectory(
coreMigrationsDirectory,
"core",
"0000_identity_core_tables",
'CREATE TABLE "ekklesia_core_user" ("id" text PRIMARY KEY);\n',
);
const aggregate = await discoverAllMigrations({
coreMigrationsDirectory,
moduleDiscovery: { moduleSdkVersion: "0.1.0", modulesDirectory, platformVersion: "0.1.0" },
});
expect(aggregate.discoveredModules).toEqual([]);
expect(aggregate.modules).toHaveLength(1);
expect(aggregate.modules[0]?.moduleId).toBe("core");
expect(aggregate.migrations).toHaveLength(1);
});
});