Files
suaveplan/packages/networking/events/event-sourcing/src/schemas/event-sourcing.schemas.test.ts
2026-05-27 23:47:53 +01:00

131 lines
3.7 KiB
TypeScript

import { describe, expect, it } from "@suaveplan/testing/runner";
import {
DomainEventSchema,
EventMetadataSchema,
MemoryEventStoreOptionsSchema,
SnapshotSchema,
UpcasterOptionsSchema,
} from "./event-sourcing.schemas.js";
describe("DomainEventSchema", () => {
const valid = {
id: "e1",
aggregateId: "a1",
aggregateType: "Order",
type: "OrderPlaced",
version: 1,
payload: { amount: 100 },
metadata: { actor: "user-1" },
occurredAt: new Date(),
};
it("parses valid event", { timeout: 5000 }, () => {
expect(() => DomainEventSchema.parse(valid)).not.toThrow();
});
it("rejects zero version", { timeout: 5000 }, () => {
expect(() =>
DomainEventSchema.parse({ ...valid, version: 0 })
).toThrow();
});
it("rejects missing id", { timeout: 5000 }, () => {
const { id: _id, ...rest } = valid;
expect(() => DomainEventSchema.parse(rest)).toThrow();
});
it("rejects string occurredAt", { timeout: 5000 }, () => {
expect(() =>
DomainEventSchema.parse({ ...valid, occurredAt: "2024-01-01" })
).toThrow();
});
});
describe("SnapshotSchema", () => {
const valid = {
aggregateId: "a1",
aggregateType: "Order",
version: 5,
state: { balance: 100 },
takenAt: new Date(),
};
it("parses valid snapshot", { timeout: 5000 }, () => {
expect(() => SnapshotSchema.parse(valid)).not.toThrow();
});
it("accepts version 0", { timeout: 5000 }, () => {
expect(() =>
SnapshotSchema.parse({ ...valid, version: 0 })
).not.toThrow();
});
it("rejects negative version", { timeout: 5000 }, () => {
expect(() => SnapshotSchema.parse({ ...valid, version: -1 })).toThrow();
});
});
describe("EventMetadataSchema", () => {
it("parses empty metadata", { timeout: 5000 }, () => {
expect(() => EventMetadataSchema.parse({})).not.toThrow();
});
it("passes through unknown keys", { timeout: 5000 }, () => {
const result = EventMetadataSchema.parse({ custom: "value" });
expect(result).toMatchObject({ custom: "value" });
});
it("accepts all optional fields", { timeout: 5000 }, () => {
const result = EventMetadataSchema.parse({
causationId: "c1",
correlationId: "r1",
actor: "user-1",
});
expect(result.actor).toBe("user-1");
});
});
describe("MemoryEventStoreOptionsSchema", () => {
it("accepts undefined", { timeout: 5000 }, () => {
expect(() =>
MemoryEventStoreOptionsSchema.parse(undefined)
).not.toThrow();
});
it("accepts maxStreamLength", { timeout: 5000 }, () => {
expect(() =>
MemoryEventStoreOptionsSchema.parse({ maxStreamLength: 1000 })
).not.toThrow();
});
it("rejects zero maxStreamLength", { timeout: 5000 }, () => {
expect(() =>
MemoryEventStoreOptionsSchema.parse({ maxStreamLength: 0 })
).toThrow();
});
});
describe("UpcasterOptionsSchema", () => {
it("parses valid upcaster", { timeout: 5000 }, () => {
expect(() =>
UpcasterOptionsSchema.parse({
eventType: "OrderPlaced",
fromVersion: 1,
toVersion: 2,
upcast: (p: unknown) => p,
})
).not.toThrow();
});
it("rejects negative fromVersion", { timeout: 5000 }, () => {
expect(() =>
UpcasterOptionsSchema.parse({
eventType: "E",
fromVersion: -1,
toVersion: 1,
upcast: (p: unknown) => p,
})
).toThrow();
});
});