test(crud): finalize MVP1 gate CRUD contract coverage

This commit is contained in:
2026-02-10 21:26:49 +01:00
parent 70fc154f97
commit 36b09cd9d7
3 changed files with 55 additions and 5 deletions

View File

@@ -63,6 +63,32 @@ function createMemoryRepository() {
}
describe("createCrudService", () => {
it("supports list and detail lookups through the repository contract", async () => {
const service = createCrudService({
resource: "fake-entity",
repository: createMemoryRepository(),
schemas: {
create: z.object({
title: z.string().min(3),
}),
update: z.object({
title: z.string().min(3).optional(),
}),
},
})
const createdA = await service.create({ title: "First" })
const createdB = await service.create({ title: "Second" })
expect(await service.getById(createdA.id)).toEqual(createdA)
expect(await service.getById("missing")).toBeNull()
const listed = await service.list()
expect(listed).toHaveLength(2)
expect(listed).toContainEqual(createdA)
expect(listed).toContainEqual(createdB)
})
it("validates create and update payloads", async () => {
const service = createCrudService({
resource: "fake-entity",
@@ -106,8 +132,13 @@ describe("createCrudService", () => {
})
it("emits audit events for create, update and delete", async () => {
const events: Array<{ action: string; beforeTitle: string | null; afterTitle: string | null }> =
[]
const events: Array<{
action: string
beforeTitle: string | null
afterTitle: string | null
actorRole: string | null
requestId: string | null
}> = []
const service = createCrudService({
resource: "fake-entity",
repository: createMemoryRepository(),
@@ -125,6 +156,9 @@ describe("createCrudService", () => {
action: event.action,
beforeTitle: event.before?.title ?? null,
afterTitle: event.after?.title ?? null,
actorRole: event.actor?.role ?? null,
requestId:
typeof event.metadata?.requestId === "string" ? event.metadata.requestId : null,
})
},
],
@@ -134,6 +168,9 @@ describe("createCrudService", () => {
{ title: "Created" },
{
actor: { id: "u-1", role: "owner" },
metadata: {
requestId: "req-1",
},
},
)
@@ -145,16 +182,22 @@ describe("createCrudService", () => {
action: "create",
beforeTitle: null,
afterTitle: "Created",
actorRole: "owner",
requestId: "req-1",
},
{
action: "update",
beforeTitle: "Created",
afterTitle: "Updated",
actorRole: null,
requestId: null,
},
{
action: "delete",
beforeTitle: "Updated",
afterTitle: null,
actorRole: null,
requestId: null,
},
])
})