event.test.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import { describe, expect, test } from "bun:test"
  2. import { Schema } from "effect"
  3. import { Event } from "../src/event.js"
  4. import { EventLog } from "../src/event-log.js"
  5. describe("public event schemas", () => {
  6. test("definition is pure", () => {
  7. const definitions = Event.inventory()
  8. Event.ephemeral({ type: "test.pure", schema: { value: Schema.String } })
  9. expect(definitions).toEqual([])
  10. })
  11. test("latest selection is independent of declaration order", () => {
  12. const historical = Event.durable({
  13. type: "test.versioned",
  14. durable: { aggregate: "id", version: 1 },
  15. schema: { id: Schema.String },
  16. })
  17. const current = Event.durable({
  18. type: "test.versioned",
  19. durable: { aggregate: "id", version: 2 },
  20. schema: { id: Schema.String, value: Schema.String },
  21. })
  22. expect(Event.latest([historical, current]).get(current.type)).toBe(current)
  23. expect(Event.latest([current, historical]).get(current.type)).toBe(current)
  24. })
  25. test("durable definitions are indexed by type and version", () => {
  26. const definition = Event.durable({
  27. type: "test.durable",
  28. durable: { aggregate: "id", version: 1 },
  29. schema: { id: Schema.String },
  30. })
  31. expect(Event.durableMap([definition]).get("test.durable.1")).toBe(definition)
  32. })
  33. test("synced marker encodes the captured watermark", () => {
  34. expect(
  35. Schema.encodeSync(EventLog.Synced)({
  36. type: "log.synced",
  37. aggregateID: "ses_test",
  38. seq: Event.Seq.make(1),
  39. }),
  40. ).toEqual({ type: "log.synced", aggregateID: "ses_test", seq: 1 })
  41. expect(Schema.encodeSync(EventLog.Synced)({ type: "log.synced", aggregateID: "ses_test" })).toEqual({
  42. type: "log.synced",
  43. aggregateID: "ses_test",
  44. })
  45. })
  46. })