Files
Epicnabbo-Catalogus-Updated…/src/features/housekeeping/foundation/commands/bootstrap.test.ts
T

77 lines
2.6 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { z } from "zod";
import { SYSTEM_COMMAND_IDS } from "../../domains/system/commands/system-commands";
import { anyCapability, ok } from "../contracts";
import {
defineHousekeepingCommands,
housekeepingCommandRegistryReady,
registerHousekeepingCommands,
} from "./bootstrap";
import {
getHousekeepingCommand,
type HousekeepingCommand,
registerHousekeepingCommand,
} from "./registry";
describe("housekeeping command bootstrap", () => {
it("preserves heterogeneous concrete input and output types", () => {
const stringCommand: HousekeepingCommand<
{ value: string },
{ length: number }
> = {
id: "system.bootstrap.string-shape",
owner: "system",
risk: "safe",
capability: anyCapability("admin.settings.view"),
input: z.object({ value: z.string() }),
requiresReason: false,
rateLimit: { attempts: 1, windowMs: 1_000 },
execute: async (context, input) =>
ok({ length: input.value.length }, context.correlationId),
};
const numberCommand: HousekeepingCommand<
{ count: number },
{ doubled: number }
> = {
id: "system.bootstrap.number-shape",
owner: "system",
risk: "safe",
capability: anyCapability("admin.settings.view"),
input: z.object({ count: z.number().int() }),
requiresReason: false,
rateLimit: { attempts: 2, windowMs: 2_000 },
execute: async (context, input) =>
ok({ doubled: input.count * 2 }, context.correlationId),
};
const commands = defineHousekeepingCommands(stringCommand, numberCommand);
const stringInput: z.input<(typeof commands)[0]["input"]> = {
value: "typed",
};
const numberInput: z.input<(typeof commands)[1]["input"]> = { count: 4 };
expect(commands[0].input.parse(stringInput)).toEqual({ value: "typed" });
expect(commands[1].input.parse(numberInput)).toEqual({ count: 4 });
expect(() => registerHousekeepingCommands(commands)).toThrow(
"command registry is sealed",
);
});
it("registers the complete current list and seals during module initialization", () => {
expect(housekeepingCommandRegistryReady).toBe(true);
expect(
SYSTEM_COMMAND_IDS.map((id) => getHousekeepingCommand(id)?.id),
).toEqual(SYSTEM_COMMAND_IDS);
expect(() =>
registerHousekeepingCommand({
id: "system.bootstrap.too-late",
owner: "system",
risk: "safe",
capability: anyCapability("admin.settings.view"),
input: z.object({}),
requiresReason: false,
rateLimit: { attempts: 1, windowMs: 1_000 },
execute: async (context) => ok(null, context.correlationId),
}),
).toThrow("command registry is sealed");
});
});