feat(housekeeping): dispatch audited commands

This commit is contained in:
Simo committed 2026-08-27 18:30:44 +02:00
1 parent ca666d9f90
commit 6aed71a8b2
8 files changed
+996

No files matched your search

@@ -0,0 +1,50 @@
import { describe, expect, it } from "vitest";
import { z } from "zod";
import { anyCapability, ok } from "../contracts";
import {
getHousekeepingCommand,
type HousekeepingCommand,
registerHousekeepingCommand,
} from "./registry";
function command(
id: string,
owner: "people" | "system" = "people",
): HousekeepingCommand<{ id: number }, { id: number }> {
return {
id,
owner,
risk: "safe",
capability: anyCapability("admin.users.view"),
input: z.object({ id: z.number().int().positive() }),
requiresReason: false,
rateLimit: { attempts: 4, windowMs: 30_000 },
execute: async (context, input) => ok(input, context.correlationId),
};
}
describe("housekeeping command registry", () => {
it("returns the exact command registered under its global ID", () => {
const registered = command("people.registry.lookup");
registerHousekeepingCommand(registered);
expect(getHousekeepingCommand(registered.id)).toBe(registered);
});
it("rejects a duplicate global command ID before it can shadow its owner", () => {
registerHousekeepingCommand(command("people.registry.duplicate"));
expect(() =>
registerHousekeepingCommand(command("people.registry.duplicate")),
).toThrow("duplicate command id: people.registry.duplicate");
});
it("rejects a command whose namespace disagrees with its declared owner", () => {
expect(() =>
registerHousekeepingCommand(
command("people.registry.owner-mismatch", "system"),
),
).toThrow("command owner mismatch: people.registry.owner-mismatch");
});
});