| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321 |
- import { describe, expect } from "bun:test"
- import { Cause, DateTime, Deferred, Effect, Exit, Fiber, Layer, Option, Ref, Schema, Stream } from "effect"
- import { Bus } from "@opencode-ai/core/bus"
- import { Event } from "@opencode-ai/schema/event"
- import { Session } from "@opencode-ai/schema/session"
- import { SessionEvent } from "@opencode-ai/schema/session-event"
- import { SessionV1 } from "@opencode-ai/schema/session-v1"
- import { Database } from "@opencode-ai/core/database/database"
- import { AppNodeBuilder } from "@opencode-ai/core/effect/app-node-builder"
- import { LayerNode } from "@opencode-ai/util/effect/layer-node"
- import { EventSequenceTable, EventTable } from "@opencode-ai/core/event/sql"
- import { Location } from "@opencode-ai/core/location"
- import { AbsolutePath } from "@opencode-ai/core/schema"
- import { Workspace } from "@opencode-ai/core/workspace"
- import { eq } from "drizzle-orm"
- import { location } from "./fixture/location"
- import { testEffect } from "./lib/effect"
- const locationLayer = Layer.succeed(
- Location.Service,
- Location.Service.of(
- location({ directory: AbsolutePath.make("project"), workspaceID: Workspace.ID.make("wrk_test") }),
- ),
- )
- const Message = Bus.ephemeral({
- type: "test.message",
- schema: {
- text: Schema.String,
- },
- })
- const SyncMessage = Bus.durable({
- type: "test.sync",
- durable: {
- version: 1,
- aggregate: "id",
- },
- schema: {
- id: Schema.String,
- text: Schema.String,
- },
- })
- const SyncSent = Bus.durable({
- type: "test.sent",
- durable: {
- version: 1,
- aggregate: "messageID",
- },
- schema: {
- messageID: Schema.String,
- text: Schema.String,
- },
- })
- const VersionedMessageV1 = Bus.durable({
- type: "test.versioned",
- durable: { version: 1, aggregate: "id" },
- schema: { id: Schema.String },
- })
- const VersionedMessageV2 = Bus.durable({
- type: "test.versioned",
- durable: { version: 2, aggregate: "id" },
- schema: { id: Schema.String },
- })
- const GlobalMessage = Bus.ephemeral({
- type: "test.global",
- schema: {
- text: Schema.String,
- },
- })
- const CountMessage = Bus.ephemeral({
- type: "test.count",
- schema: {
- count: Schema.Number,
- },
- })
- const VersionedMessage = Bus.durable({
- type: "test.versioned",
- durable: {
- version: 2,
- aggregate: "id",
- },
- schema: {
- id: Schema.String,
- text: Schema.String,
- },
- })
- const DurableMessage = SessionV1.Event.MessageRemoved
- const durableData = (sessionID: Session.ID, text: string) => ({
- sessionID,
- messageID: SessionV1.MessageID.ascending(`msg_${text}`),
- })
- /** Followed log read without markers: the old `durable` stream shape. */
- const tail = (bus: Bus.Interface, input: { aggregateID: string; after?: number }) =>
- bus.log({ ...input, follow: true }).pipe(Stream.filter((item): item is Event.Payload => !Bus.isSynced(item)))
- const it = testEffect(
- AppNodeBuilder.build(LayerNode.group([Database.node, Bus.node, Location.node]), [[Location.node, locationLayer]]),
- )
- const itWithoutLocation = testEffect(AppNodeBuilder.build(LayerNode.group([Database.node, Bus.node])))
- describe("Bus", () => {
- it.effect("subscribes to multiple event definitions with a discriminated payload union", () =>
- Effect.gen(function* () {
- const bus = yield* Bus.Service
- // @ts-expect-error multi-definition subscriptions require at least one definition
- bus.subscribe([])
- const fiber = yield* bus
- .subscribe([Message, CountMessage])
- .pipe(Stream.take(2), Stream.runCollect, Effect.forkScoped)
- yield* Effect.yieldNow
- yield* bus.publish(Message, { text: "hello" })
- yield* bus.publish(CountMessage, { count: 2 })
- const received = Array.from(yield* Fiber.join(fiber)).map((event) =>
- event.type === "test.message" ? event.data.text : event.data.count,
- )
- expect(received).toEqual(["hello", 2])
- }),
- )
- it.effect("publishes events with the current location", () =>
- Effect.gen(function* () {
- const bus = yield* Bus.Service
- const fiber = yield* bus.subscribe(Message).pipe(Stream.take(1), Stream.runCollect, Effect.forkScoped)
- yield* Effect.yieldNow
- const event = yield* bus.publish(Message, { text: "hello" })
- const received = Array.from(yield* Fiber.join(fiber))
- expect(received).toEqual([event])
- expect(event.type).toBe("test.message")
- expect(event).not.toHaveProperty("version")
- expect(event.data).toEqual({ text: "hello" })
- expect(event.location).toEqual({
- directory: AbsolutePath.make("project"),
- workspaceID: Workspace.ID.make("wrk_test"),
- })
- }),
- )
- itWithoutLocation.effect("omits location when no location is available", () =>
- Effect.gen(function* () {
- const bus = yield* Bus.Service
- const event = yield* bus.publish(GlobalMessage, { text: "hello" })
- expect(event).not.toHaveProperty("location")
- expect(event.type).toBe("test.global")
- }),
- )
- it.effect("publishes definition version", () =>
- Effect.gen(function* () {
- const bus = yield* Bus.Service
- const event = yield* bus.publish(VersionedMessage, { id: "one", text: "hello" })
- expect(event.type).toBe("test.versioned")
- expect(event.durable?.version).toBe(Event.Version.make(2))
- }),
- )
- it.effect("selects the latest durable definition independent of declaration order", () =>
- Effect.sync(() => {
- const latest = Bus.durable({
- type: "test.out-of-order",
- durable: { version: 2, aggregate: "id" },
- schema: { id: Schema.String },
- })
- const historical = Bus.durable({
- type: "test.out-of-order",
- durable: { version: 1, aggregate: "id" },
- schema: { id: Schema.String },
- })
- expect(Event.latest([latest, historical]).get("test.out-of-order")).toBe(latest)
- expect(Event.latest([historical, latest]).get("test.out-of-order")).toBe(latest)
- }),
- )
- it.effect("publishes to typed and wildcard subscriptions", () =>
- Effect.gen(function* () {
- const bus = yield* Bus.Service
- const typed = yield* bus.subscribe(Message).pipe(Stream.take(1), Stream.runCollect, Effect.forkScoped)
- const wildcard = yield* bus.subscribe().pipe(Stream.take(1), Stream.runCollect, Effect.forkScoped)
- yield* Effect.yieldNow
- const event = yield* bus.publish(Message, { text: "hello" })
- expect(Array.from(yield* Fiber.join(typed))).toEqual([event])
- expect(Array.from(yield* Fiber.join(wildcard))).toEqual([event])
- }),
- )
- it.effect("runs projectors inline", () =>
- Effect.gen(function* () {
- const bus = yield* Bus.Service
- const received = new Array<Event.Payload>()
- yield* bus.project(SyncMessage, (event) =>
- Effect.sync(() => {
- received.push(event)
- }),
- )
- const event = yield* bus.publish(SyncMessage, { id: "one", text: "hello" })
- yield* bus.publish(SyncMessage, { id: "one", text: "after unsubscribe" })
- expect(received[0]).toEqual(event)
- expect(received[1]?.data).toEqual({ id: "one", text: "after unsubscribe" })
- }),
- )
- it.effect("commits local operational state inside a new durable event transaction", () =>
- Effect.gen(function* () {
- const bus = yield* Bus.Service
- const received = new Array<string>()
- const aggregateID = Event.ID.create()
- yield* bus.project(SyncMessage, () => Effect.sync(() => received.push("projector")))
- yield* bus.publish(
- SyncMessage,
- { id: aggregateID, text: "hello" },
- { commit: (seq) => Effect.sync(() => received.push(`commit:${seq}`)) },
- )
- expect(received).toEqual(["projector", "commit:0"])
- }),
- )
- it.effect("rolls back the durable event and projector when the local commit fails", () =>
- Effect.gen(function* () {
- const bus = yield* Bus.Service
- const { db } = yield* Database.Service
- const aggregateID = Event.ID.create()
- yield* db.run("CREATE TABLE IF NOT EXISTS event_commit_probe (value text NOT NULL)")
- yield* db.run("DELETE FROM event_commit_probe")
- yield* bus.project(SyncMessage, () =>
- db.run("INSERT INTO event_commit_probe (value) VALUES ('projected')").pipe(Effect.orDie, Effect.asVoid),
- )
- const exit = yield* bus
- .publish(SyncMessage, { id: aggregateID, text: "hello" }, { commit: () => Effect.die("commit failed") })
- .pipe(Effect.exit)
- expect(String(exit)).toContain("commit failed")
- expect(yield* db.all("SELECT value FROM event_commit_probe")).toEqual([])
- expect(yield* db.select().from(EventTable).where(eq(EventTable.aggregate_id, aggregateID)).all()).toEqual([])
- expect(
- yield* db.select().from(EventSequenceTable).where(eq(EventSequenceTable.aggregate_id, aggregateID)).all(),
- ).toEqual([])
- }),
- )
- it.effect("rejects local commit hooks on live-only events", () =>
- Effect.gen(function* () {
- const bus = yield* Bus.Service
- const exit = yield* bus.publish(Message, { text: "hello" }, { commit: () => Effect.void }).pipe(Effect.exit)
- expect(String(exit)).toContain("Local commit hooks require a durable event")
- }),
- )
- it.effect("runs projectors before publishing to streams", () =>
- Effect.gen(function* () {
- const bus = yield* Bus.Service
- const received = new Array<string>()
- const fiber = yield* bus.subscribe().pipe(
- Stream.take(1),
- Stream.runForEach(() => Effect.sync(() => received.push("stream"))),
- Effect.forkScoped,
- )
- yield* bus.project(SyncMessage, (event) =>
- Effect.sync(() => {
- received.push(event.type)
- }),
- )
- yield* Effect.yieldNow
- yield* bus.publish(SyncMessage, { id: "one", text: "hello" })
- yield* Fiber.join(fiber)
- expect(received).toEqual([SyncMessage.type, "stream"])
- }),
- )
- it.effect("runs listeners inline after projectors", () =>
- Effect.gen(function* () {
- const bus = yield* Bus.Service
- const received = new Array<string>()
- yield* bus.project(SyncMessage, () =>
- Effect.sync(() => {
- received.push("projector")
- }),
- )
- const unsubscribe = yield* bus.listen(() =>
- Effect.sync(() => {
- received.push("listener")
- }),
- )
- yield* bus.publish(SyncMessage, { id: "one", text: "hello" })
- yield* unsubscribe
- yield* bus.publish(SyncMessage, { id: "one", text: "after unsubscribe" })
- expect(received).toEqual(["projector", "listener", "projector"])
- }),
- )
- it.effect("isolates observer defects after durable events commit", () =>
- Effect.gen(function* () {
- const bus = yield* Bus.Service
- const received = new Array<string>()
- yield* bus.listen(() => {
- throw new Error("listener defect")
- })
- yield* bus.listen((event) =>
- Effect.sync(() => {
- received.push(event.type)
- }),
- )
- const event = yield* bus.publish(SyncMessage, { id: "one", text: "hello" })
- expect(received).toEqual([SyncMessage.type])
- expect(event.durable?.seq).toBeNumber()
- }),
- )
- it.effect("notifies global listeners only after a durable event is committed", () =>
- Effect.gen(function* () {
- const bus = yield* Bus.Service
- const { db } = yield* Database.Service
- const aggregateID = Event.ID.create()
- const observed = new Array<{ id: string; seq: number }>()
- yield* bus.listen((event) =>
- event.type !== SyncMessage.type
- ? Effect.void
- : db
- .select({ id: EventTable.id, seq: EventTable.seq })
- .from(EventTable)
- .where(eq(EventTable.id, event.id))
- .get()
- .pipe(
- Effect.orDie,
- Effect.tap((row) =>
- Effect.sync(() => {
- if (row) observed.push(row)
- }),
- ),
- Effect.asVoid,
- ),
- )
- const event = yield* bus.publish(SyncMessage, { id: aggregateID, text: "committed" })
- if (!event.durable) throw new Error("Expected durable event metadata")
- expect(observed).toEqual([{ id: event.id, seq: event.durable.seq }])
- }),
- )
- it.effect("preserves observer interruption", () =>
- Effect.gen(function* () {
- const bus = yield* Bus.Service
- const { db } = yield* Database.Service
- yield* bus.listen(() => Effect.interrupt)
- const exit = yield* bus.publish(SyncMessage, { id: "interrupted", text: "hello" }).pipe(Effect.exit)
- const committed = yield* db
- .select({ id: EventTable.id })
- .from(EventTable)
- .where(eq(EventTable.aggregate_id, "interrupted"))
- .get()
- .pipe(Effect.orDie)
- expect(Exit.isFailure(exit) && Cause.hasInterrupts(exit.cause)).toBeTrue()
- expect(committed).toBeDefined()
- }),
- )
- it.effect("keeps live-only listener defects fail-fast", () =>
- Effect.gen(function* () {
- const bus = yield* Bus.Service
- const defect = new Error("listener defect")
- yield* bus.listen(() => Effect.die(defect))
- expect(yield* bus.publish(Message, { text: "hello" }).pipe(Effect.catchDefect(Effect.succeed))).toBe(defect)
- }),
- )
- it.effect("inserts durable event rows on publish", () =>
- Effect.gen(function* () {
- const bus = yield* Bus.Service
- const { db } = yield* Database.Service
- const aggregateID = Event.ID.create()
- yield* bus.publish(SyncMessage, { id: aggregateID, text: "first" })
- const rows = yield* db
- .select()
- .from(EventTable)
- .where(eq(EventTable.aggregate_id, aggregateID))
- .all()
- .pipe(Effect.orDie)
- expect(rows).toHaveLength(1)
- expect(rows[0]?.type).toBe(Bus.versionedType(SyncMessage.type, 1))
- expect(rows[0]?.aggregate_id).toBe(aggregateID)
- }),
- )
- it.effect("increments durable event seq per aggregate", () =>
- Effect.gen(function* () {
- const bus = yield* Bus.Service
- const { db } = yield* Database.Service
- const aggregateID = Event.ID.create()
- yield* bus.publish(SyncMessage, { id: aggregateID, text: "first" })
- yield* bus.publish(SyncMessage, { id: aggregateID, text: "second" })
- const rows = yield* db
- .select()
- .from(EventTable)
- .where(eq(EventTable.aggregate_id, aggregateID))
- .all()
- .pipe(Effect.orDie)
- expect(rows.map((row) => row.seq)).toEqual([0, 1])
- }),
- )
- it.effect("replays durable aggregate events after a sequence and tails new events", () =>
- Effect.gen(function* () {
- const bus = yield* Bus.Service
- const aggregateID = Session.ID.create()
- yield* bus.publish(DurableMessage, durableData(aggregateID, "zero"))
- yield* bus.publish(DurableMessage, durableData(aggregateID, "one"))
- const fiber = yield* tail(bus, { aggregateID, after: 0 }).pipe(
- Stream.take(2),
- Stream.runCollect,
- Effect.forkScoped,
- )
- yield* Effect.yieldNow
- yield* bus.publish(DurableMessage, durableData(aggregateID, "two"))
- expect(Array.from(yield* Fiber.join(fiber)).map((event) => [event.durable?.seq, event.data])).toEqual([
- [1, durableData(aggregateID, "one")],
- [2, durableData(aggregateID, "two")],
- ])
- }),
- )
- it.effect("catches durable aggregate events published during replay handoff", () =>
- Effect.gen(function* () {
- const bus = yield* Bus.Service
- const aggregateID = Session.ID.create()
- yield* bus.publish(DurableMessage, durableData(aggregateID, "zero"))
- const fiber = yield* tail(bus, { aggregateID }).pipe(Stream.take(2), Stream.runCollect, Effect.forkScoped)
- yield* bus.publish(DurableMessage, durableData(aggregateID, "one"))
- expect(Array.from(yield* Fiber.join(fiber)).map((event) => [event.durable?.seq, event.data])).toEqual([
- [0, durableData(aggregateID, "zero")],
- [1, durableData(aggregateID, "one")],
- ])
- }),
- )
- it.effect("retains a durable wake committed while historical replay is paused", () =>
- Effect.gen(function* () {
- const readStarted = yield* Deferred.make<void>()
- const continueRead = yield* Deferred.make<void>()
- let pause = true
- const eventLayer = Bus.layerWith({
- beforeAggregateRead: () =>
- pause
- ? Deferred.succeed(readStarted, undefined).pipe(Effect.andThen(Deferred.await(continueRead)))
- : Effect.void,
- }).pipe(Layer.provide(LayerNode.compile(Database.node)))
- yield* Effect.gen(function* () {
- const bus = yield* Bus.Service
- const aggregateID = Session.ID.create()
- const fiber = yield* tail(bus, { aggregateID }).pipe(Stream.take(1), Stream.runCollect, Effect.forkScoped)
- yield* Deferred.await(readStarted)
- pause = false
- yield* bus.publish(DurableMessage, durableData(aggregateID, "during handoff"))
- yield* Deferred.succeed(continueRead, undefined)
- expect(Array.from(yield* Fiber.join(fiber)).map((event) => [event.durable?.seq, event.data])).toEqual([
- [0, durableData(aggregateID, "during handoff")],
- ])
- }).pipe(Effect.provide(Layer.merge(LayerNode.compile(Database.node), eventLayer)))
- }),
- )
- it.effect("coalesces durable aggregate wakes while draining every committed event", () =>
- Effect.gen(function* () {
- const bus = yield* Bus.Service
- const aggregateID = Session.ID.create()
- const count = 64
- const fiber = yield* tail(bus, { aggregateID }).pipe(Stream.take(count), Stream.runCollect, Effect.forkScoped)
- yield* Effect.yieldNow
- for (let index = 0; index < count; index++) {
- yield* bus.publish(DurableMessage, durableData(aggregateID, String(index)))
- }
- expect(Array.from(yield* Fiber.join(fiber)).map((event) => [event.durable?.seq, event.data])).toEqual(
- Array.from({ length: count }, (_, index) => [index, durableData(aggregateID, String(index))]),
- )
- }),
- )
- it.effect("omits live-only events from durable aggregate streams", () =>
- Effect.gen(function* () {
- const bus = yield* Bus.Service
- const aggregateID = Session.ID.create()
- const fiber = yield* tail(bus, { aggregateID }).pipe(Stream.take(1), Stream.runCollect, Effect.forkScoped)
- yield* Effect.yieldNow
- yield* bus.publish(Message, { text: "live only" })
- yield* bus.publish(DurableMessage, durableData(aggregateID, "durable"))
- expect(Array.from(yield* Fiber.join(fiber)).map((event) => event.type)).toEqual([DurableMessage.type])
- }),
- )
- it.effect("uses custom sync aggregate field", () =>
- Effect.gen(function* () {
- const bus = yield* Bus.Service
- const { db } = yield* Database.Service
- const aggregateID = Event.ID.create()
- yield* bus.publish(SyncSent, { messageID: aggregateID, text: "sent" })
- const rows = yield* db
- .select()
- .from(EventTable)
- .where(eq(EventTable.aggregate_id, aggregateID))
- .all()
- .pipe(Effect.orDie)
- expect(rows).toHaveLength(1)
- expect(rows[0]?.aggregate_id).toBe(aggregateID)
- }),
- )
- it.effect("replays durable events through projectors", () =>
- Effect.gen(function* () {
- const bus = yield* Bus.Service
- const received = new Array<Event.Payload>()
- yield* bus.project(DurableMessage, (event) =>
- Effect.sync(() => {
- received.push(event)
- }),
- )
- const aggregateID = Session.ID.create()
- yield* bus.replay({
- id: Event.ID.create(),
- created: DateTime.makeUnsafe(0),
- type: Bus.versionedType(DurableMessage.type, 1),
- seq: 0,
- aggregateID,
- data: durableData(aggregateID, "hello"),
- })
- expect(received[0]?.type).toBe(DurableMessage.type)
- expect(received[0]?.data).toEqual(durableData(aggregateID, "hello"))
- }),
- )
- it.effect("replay inserts external event rows", () =>
- Effect.gen(function* () {
- const bus = yield* Bus.Service
- const { db } = yield* Database.Service
- const aggregateID = Session.ID.create()
- yield* bus.replay({
- id: Event.ID.create(),
- created: DateTime.makeUnsafe(0),
- type: Bus.versionedType(DurableMessage.type, 1),
- seq: 0,
- aggregateID,
- data: durableData(aggregateID, "replayed"),
- })
- const rows = yield* db
- .select()
- .from(EventTable)
- .where(eq(EventTable.aggregate_id, aggregateID))
- .all()
- .pipe(Effect.orDie)
- expect(rows).toHaveLength(1)
- expect(rows[0]?.aggregate_id).toBe(aggregateID)
- }),
- )
- it.effect(
- "replay rejects an envelope aggregate that differs from its payload without mutating the payload aggregate",
- () =>
- Effect.gen(function* () {
- const bus = yield* Bus.Service
- const { db } = yield* Database.Service
- const envelopeAggregateID = Session.ID.create()
- const payloadAggregateID = Session.ID.create()
- const received = new Array<Event.Payload>()
- yield* bus.publish(DurableMessage, durableData(payloadAggregateID, "seed"))
- yield* bus.project(DurableMessage, (event) =>
- Effect.sync(() => {
- received.push(event)
- }),
- )
- const exit = yield* bus
- .replay({
- id: Event.ID.create(),
- created: DateTime.makeUnsafe(0),
- type: Bus.versionedType(DurableMessage.type, 1),
- seq: 1,
- aggregateID: envelopeAggregateID,
- data: durableData(payloadAggregateID, "replayed"),
- })
- .pipe(Effect.exit)
- const rows = yield* db
- .select()
- .from(EventTable)
- .where(eq(EventTable.aggregate_id, payloadAggregateID))
- .all()
- .pipe(Effect.orDie)
- const sequence = yield* db
- .select({ seq: EventSequenceTable.seq })
- .from(EventSequenceTable)
- .where(eq(EventSequenceTable.aggregate_id, payloadAggregateID))
- .get()
- .pipe(Effect.orDie)
- expect(String(exit)).toContain("Aggregate mismatch")
- expect(received).toHaveLength(0)
- expect(rows).toHaveLength(1)
- expect(sequence).toEqual({ seq: 0 })
- }),
- )
- it.effect("replay defects on sequence mismatch", () =>
- Effect.gen(function* () {
- const bus = yield* Bus.Service
- const aggregateID = Session.ID.create()
- yield* bus.replay({
- id: Event.ID.create(),
- created: DateTime.makeUnsafe(0),
- type: Bus.versionedType(DurableMessage.type, 1),
- seq: 0,
- aggregateID,
- data: durableData(aggregateID, "first"),
- })
- const exit = yield* bus
- .replay({
- id: Event.ID.create(),
- created: DateTime.makeUnsafe(0),
- type: Bus.versionedType(DurableMessage.type, 1),
- seq: 5,
- aggregateID,
- data: durableData(aggregateID, "bad"),
- })
- .pipe(Effect.exit)
- expect(String(exit)).toContain("Sequence mismatch")
- }),
- )
- it.effect("replay decodes synchronized transformed values before projection", () =>
- Effect.gen(function* () {
- const bus = yield* Bus.Service
- const aggregateID = Session.ID.create()
- const received = new Array<typeof SessionEvent.InstructionsUpdated.Type>()
- yield* bus.project(SessionEvent.InstructionsUpdated, (event) =>
- Effect.sync(() => {
- received.push(event)
- }),
- )
- yield* bus.replay({
- id: Event.ID.create(),
- created: DateTime.makeUnsafe(0),
- type: Bus.versionedType(SessionEvent.InstructionsUpdated.type, 2),
- seq: 0,
- aggregateID,
- data: { sessionID: aggregateID, delta: { "core/context": "0".repeat(64) } },
- })
- expect(received[0]?.created).toEqual(DateTime.makeUnsafe(0))
- }),
- )
- it.effect("dispatches durable projectors by exact event version", () =>
- Effect.gen(function* () {
- const bus = yield* Bus.Service
- const aggregateID = Session.ID.create()
- const received = new Array<typeof VersionedMessageV2.Type>()
- yield* bus.project(VersionedMessageV2, (event) =>
- Effect.sync(() => {
- received.push(event)
- }),
- )
- yield* bus.publish(VersionedMessageV1, { id: aggregateID })
- yield* bus.publish(VersionedMessageV2, { id: aggregateID })
- expect(received).toHaveLength(1)
- expect(received[0]?.durable.version).toBe(Event.Version.make(2))
- }),
- )
- it.effect("replay defects on unknown event type", () =>
- Effect.gen(function* () {
- const bus = yield* Bus.Service
- const exit = yield* bus
- .replay({
- id: Event.ID.create(),
- created: DateTime.makeUnsafe(0),
- type: "unknown.event.1",
- seq: 0,
- aggregateID: Event.ID.create(),
- data: {},
- })
- .pipe(Effect.exit)
- expect(String(exit)).toContain("Unknown durable event type")
- }),
- )
- it.effect("replayAll validates contiguous aggregate events", () =>
- Effect.gen(function* () {
- const bus = yield* Bus.Service
- const aggregateID = Session.ID.create()
- const source = yield* bus.replayAll([
- {
- id: Event.ID.create(),
- created: DateTime.makeUnsafe(0),
- type: Bus.versionedType(DurableMessage.type, 1),
- seq: 0,
- aggregateID,
- data: durableData(aggregateID, "one"),
- },
- {
- id: Event.ID.create(),
- created: DateTime.makeUnsafe(0),
- type: Bus.versionedType(DurableMessage.type, 1),
- seq: 1,
- aggregateID,
- data: durableData(aggregateID, "two"),
- },
- ])
- expect(source).toBe(aggregateID)
- }),
- )
- it.effect("replayAll accepts later chunks after the first batch", () =>
- Effect.gen(function* () {
- const bus = yield* Bus.Service
- const { db } = yield* Database.Service
- const aggregateID = Session.ID.create()
- const one = yield* bus.replayAll([
- {
- id: Event.ID.create(),
- created: DateTime.makeUnsafe(0),
- type: Bus.versionedType(DurableMessage.type, 1),
- seq: 0,
- aggregateID,
- data: durableData(aggregateID, "one"),
- },
- {
- id: Event.ID.create(),
- created: DateTime.makeUnsafe(0),
- type: Bus.versionedType(DurableMessage.type, 1),
- seq: 1,
- aggregateID,
- data: durableData(aggregateID, "two"),
- },
- ])
- const two = yield* bus.replayAll([
- {
- id: Event.ID.create(),
- created: DateTime.makeUnsafe(0),
- type: Bus.versionedType(DurableMessage.type, 1),
- seq: 2,
- aggregateID,
- data: durableData(aggregateID, "three"),
- },
- {
- id: Event.ID.create(),
- created: DateTime.makeUnsafe(0),
- type: Bus.versionedType(DurableMessage.type, 1),
- seq: 3,
- aggregateID,
- data: durableData(aggregateID, "four"),
- },
- ])
- const rows = yield* db
- .select()
- .from(EventTable)
- .where(eq(EventTable.aggregate_id, aggregateID))
- .all()
- .pipe(Effect.orDie)
- expect(one).toBe(aggregateID)
- expect(two).toBe(aggregateID)
- expect(rows.map((row) => row.seq)).toEqual([0, 1, 2, 3])
- }),
- )
- it.effect("claim fences replay owners", () =>
- Effect.gen(function* () {
- const bus = yield* Bus.Service
- const received = new Array<Event.Payload>()
- const aggregateID = Session.ID.create()
- yield* bus.publish(DurableMessage, durableData(aggregateID, "seed"))
- yield* bus.claim(aggregateID, "owner-a")
- yield* bus.project(DurableMessage, (event) =>
- Effect.sync(() => {
- received.push(event)
- }),
- )
- yield* bus.replay(
- {
- id: Event.ID.create(),
- created: DateTime.makeUnsafe(0),
- type: Bus.versionedType(DurableMessage.type, 1),
- seq: 1,
- aggregateID,
- data: durableData(aggregateID, "ignored"),
- },
- { ownerID: "owner-b" },
- )
- expect(received).toHaveLength(0)
- }),
- )
- it.effect("strict owner fences exact replay", () =>
- Effect.gen(function* () {
- const bus = yield* Bus.Service
- const aggregateID = Session.ID.create()
- const id = Event.ID.create()
- const replayed = {
- id,
- created: DateTime.makeUnsafe(0),
- type: Bus.versionedType(DurableMessage.type, 1),
- seq: 0,
- aggregateID,
- data: durableData(aggregateID, "owned"),
- }
- yield* bus.replay(replayed, { ownerID: "owner-a" })
- const exit = yield* bus.replay(replayed, { ownerID: "owner-b", strictOwner: true }).pipe(Effect.exit)
- expect(String(exit)).toContain("Replay owner mismatch")
- }),
- )
- it.effect("exact replay claims an unowned aggregate", () =>
- Effect.gen(function* () {
- const bus = yield* Bus.Service
- const { db } = yield* Database.Service
- const aggregateID = Session.ID.create()
- const published = yield* bus.publish(DurableMessage, durableData(aggregateID, "owned"))
- const replayed = {
- id: published.id,
- created: published.created,
- type: Bus.versionedType(DurableMessage.type, 1),
- seq: published.durable!.seq,
- aggregateID,
- data: published.data,
- }
- yield* bus.replay(replayed, { ownerID: "owner-a", strictOwner: true })
- const row = yield* db
- .select({ ownerID: EventSequenceTable.owner_id })
- .from(EventSequenceTable)
- .where(eq(EventSequenceTable.aggregate_id, aggregateID))
- .get()
- .pipe(Effect.orDie)
- expect(row?.ownerID).toBe("owner-a")
- const exit = yield* bus
- .replay(
- { ...replayed, id: Event.ID.create(), seq: 1, data: durableData(aggregateID, "conflict") },
- { ownerID: "owner-b", strictOwner: true },
- )
- .pipe(Effect.exit)
- expect(String(exit)).toContain("Replay owner mismatch")
- }),
- )
- it.effect("replay with owner claims an unowned sequence", () =>
- Effect.gen(function* () {
- const bus = yield* Bus.Service
- const { db } = yield* Database.Service
- const aggregateID = Session.ID.create()
- yield* bus.replay(
- {
- id: Event.ID.create(),
- created: DateTime.makeUnsafe(0),
- type: Bus.versionedType(DurableMessage.type, 1),
- seq: 0,
- aggregateID,
- data: durableData(aggregateID, "owned"),
- },
- { ownerID: "owner-1" },
- )
- const row = yield* db
- .select({ seq: EventSequenceTable.seq, ownerID: EventSequenceTable.owner_id })
- .from(EventSequenceTable)
- .where(eq(EventSequenceTable.aggregate_id, aggregateID))
- .get()
- .pipe(Effect.orDie)
- expect(row).toEqual({ seq: 0, ownerID: "owner-1" })
- }),
- )
- it.effect("replay claims an existing unowned sequence before fencing a different owner", () =>
- Effect.gen(function* () {
- const bus = yield* Bus.Service
- const { db } = yield* Database.Service
- const aggregateID = Session.ID.create()
- yield* bus.publish(DurableMessage, durableData(aggregateID, "local"))
- yield* bus.replay(
- {
- id: Event.ID.create(),
- created: DateTime.makeUnsafe(0),
- type: Bus.versionedType(DurableMessage.type, 1),
- seq: 1,
- aggregateID,
- data: durableData(aggregateID, "claimed"),
- },
- { ownerID: "owner-1" },
- )
- yield* bus.replay(
- {
- id: Event.ID.create(),
- created: DateTime.makeUnsafe(0),
- type: Bus.versionedType(DurableMessage.type, 1),
- seq: 2,
- aggregateID,
- data: durableData(aggregateID, "fenced"),
- },
- { ownerID: "owner-2" },
- )
- const rows = yield* db
- .select()
- .from(EventTable)
- .where(eq(EventTable.aggregate_id, aggregateID))
- .all()
- .pipe(Effect.orDie)
- const sequence = yield* db
- .select({ seq: EventSequenceTable.seq, ownerID: EventSequenceTable.owner_id })
- .from(EventSequenceTable)
- .where(eq(EventSequenceTable.aggregate_id, aggregateID))
- .get()
- .pipe(Effect.orDie)
- expect(rows.map((row) => row.seq)).toEqual([0, 1])
- expect(sequence).toEqual({ seq: 1, ownerID: "owner-1" })
- }),
- )
- it.effect("strict replay rejects an owner conflict instead of silently skipping it", () =>
- Effect.gen(function* () {
- const bus = yield* Bus.Service
- const aggregateID = Session.ID.create()
- yield* bus.replay(
- {
- id: Event.ID.create(),
- created: DateTime.makeUnsafe(0),
- type: Bus.versionedType(DurableMessage.type, 1),
- seq: 0,
- aggregateID,
- data: durableData(aggregateID, "claimed"),
- },
- { ownerID: "owner-1" },
- )
- const exit = yield* bus
- .replay(
- {
- id: Event.ID.create(),
- created: DateTime.makeUnsafe(0),
- type: Bus.versionedType(DurableMessage.type, 1),
- seq: 1,
- aggregateID,
- data: durableData(aggregateID, "conflict"),
- },
- { ownerID: "owner-2", strictOwner: true },
- )
- .pipe(Effect.exit)
- expect(String(exit)).toContain("Replay owner mismatch")
- }),
- )
- it.effect("publishes accepted replay with its durable sequence and suppresses stale replay", () =>
- Effect.gen(function* () {
- const bus = yield* Bus.Service
- const received = new Array<Event.Payload>()
- const aggregateID = Session.ID.create()
- yield* bus.listen((event) => Effect.sync(() => received.push(event)))
- const replayed = {
- id: Event.ID.create(),
- created: DateTime.makeUnsafe(0),
- type: Bus.versionedType(DurableMessage.type, 1),
- seq: 0,
- aggregateID,
- data: durableData(aggregateID, "replayed"),
- }
- yield* bus.replay(replayed, { publish: true })
- yield* bus.replay(replayed, { publish: true })
- expect(received).toMatchObject([{ id: replayed.id, durable: { seq: 0, version: 1 }, data: replayed.data }])
- }),
- )
- it.effect("rejects divergent stale replay without publishing it", () =>
- Effect.gen(function* () {
- const bus = yield* Bus.Service
- const received = new Array<Event.Payload>()
- const aggregateID = Session.ID.create()
- const replayed = {
- id: Event.ID.create(),
- created: DateTime.makeUnsafe(0),
- type: Bus.versionedType(DurableMessage.type, 1),
- seq: 0,
- aggregateID,
- data: durableData(aggregateID, "original"),
- }
- yield* bus.listen((event) => Effect.sync(() => received.push(event)))
- yield* bus.replay(replayed, { publish: true })
- const exit = yield* bus
- .replay({ ...replayed, data: durableData(aggregateID, "divergent") }, { publish: true })
- .pipe(Effect.exit)
- expect(String(exit)).toContain("Replay diverged")
- expect(received).toHaveLength(1)
- }),
- )
- it.effect("rejects an event ID reused at another aggregate position", () =>
- Effect.gen(function* () {
- const bus = yield* Bus.Service
- const aggregateID = Session.ID.create()
- const id = Event.ID.create()
- yield* bus.replay({
- id,
- created: DateTime.makeUnsafe(0),
- type: Bus.versionedType(DurableMessage.type, 1),
- seq: 0,
- aggregateID,
- data: durableData(aggregateID, "first"),
- })
- const exit = yield* bus
- .replay({
- id,
- created: DateTime.makeUnsafe(0),
- type: Bus.versionedType(DurableMessage.type, 1),
- seq: 1,
- aggregateID,
- data: durableData(aggregateID, "second"),
- })
- .pipe(Effect.exit)
- expect(String(exit)).toContain(`Event ${id} already exists`)
- }),
- )
- it.effect("replay from a different owner leaves claimed sequence unchanged", () =>
- Effect.gen(function* () {
- const bus = yield* Bus.Service
- const { db } = yield* Database.Service
- const aggregateID = Session.ID.create()
- const received = new Array<Event.Payload>()
- yield* bus.listen((event) => Effect.sync(() => received.push(event)))
- yield* bus.replay(
- {
- id: Event.ID.create(),
- created: DateTime.makeUnsafe(0),
- type: Bus.versionedType(DurableMessage.type, 1),
- seq: 0,
- aggregateID,
- data: durableData(aggregateID, "first"),
- },
- { ownerID: "owner-1" },
- )
- yield* bus.replay(
- {
- id: Event.ID.create(),
- created: DateTime.makeUnsafe(0),
- type: Bus.versionedType(DurableMessage.type, 1),
- seq: 1,
- aggregateID,
- data: durableData(aggregateID, "ignored"),
- },
- { ownerID: "owner-2", publish: true },
- )
- const rows = yield* db
- .select()
- .from(EventTable)
- .where(eq(EventTable.aggregate_id, aggregateID))
- .all()
- .pipe(Effect.orDie)
- const sequence = yield* db
- .select({ seq: EventSequenceTable.seq, ownerID: EventSequenceTable.owner_id })
- .from(EventSequenceTable)
- .where(eq(EventSequenceTable.aggregate_id, aggregateID))
- .get()
- .pipe(Effect.orDie)
- expect(rows).toHaveLength(1)
- expect(sequence).toEqual({ seq: 0, ownerID: "owner-1" })
- expect(received).toHaveLength(0)
- }),
- )
- it.effect("claim updates the event sequence owner", () =>
- Effect.gen(function* () {
- const bus = yield* Bus.Service
- const { db } = yield* Database.Service
- const aggregateID = Event.ID.create()
- yield* bus.publish(SyncMessage, { id: aggregateID, text: "claimed" })
- yield* bus.claim(aggregateID, "owner-1")
- yield* bus.claim(aggregateID, "owner-2")
- const row = yield* db
- .select({ seq: EventSequenceTable.seq, ownerID: EventSequenceTable.owner_id })
- .from(EventSequenceTable)
- .where(eq(EventSequenceTable.aggregate_id, aggregateID))
- .get()
- .pipe(Effect.orDie)
- expect(row).toEqual({ seq: 0, ownerID: "owner-2" })
- }),
- )
- it.effect("remove clears durable event sequence", () =>
- Effect.gen(function* () {
- const bus = yield* Bus.Service
- const received = new Array<Event.Payload>()
- const aggregateID = Session.ID.create()
- yield* bus.publish(DurableMessage, durableData(aggregateID, "seed"))
- yield* bus.remove(aggregateID)
- yield* bus.project(DurableMessage, (event) =>
- Effect.sync(() => {
- received.push(event)
- }),
- )
- yield* bus.replay({
- id: Event.ID.create(),
- created: DateTime.makeUnsafe(0),
- type: Bus.versionedType(DurableMessage.type, 1),
- seq: 0,
- aggregateID,
- data: durableData(aggregateID, "replayed"),
- })
- expect(received[0]?.data).toEqual(durableData(aggregateID, "replayed"))
- }),
- )
- it.effect("log without follow replays events and completes with a synced marker", () =>
- Effect.gen(function* () {
- const bus = yield* Bus.Service
- const aggregateID = Session.ID.create()
- yield* bus.publish(DurableMessage, durableData(aggregateID, "zero"))
- yield* bus.publish(DurableMessage, durableData(aggregateID, "one"))
- const items = Array.from(yield* Stream.runCollect(bus.log({ aggregateID })))
- expect(items.map((item) => (Bus.isSynced(item) ? item.type : item.durable?.seq))).toEqual([
- Event.Seq.make(0),
- Event.Seq.make(1),
- "log.synced",
- ])
- expect(items.at(-1)).toEqual({ type: "log.synced", aggregateID, seq: Event.Seq.make(1) })
- }),
- )
- it.effect("log synced marker omits seq for an empty log and keeps the cursor otherwise", () =>
- Effect.gen(function* () {
- const bus = yield* Bus.Service
- const aggregateID = Session.ID.create()
- const empty = Array.from(yield* Stream.runCollect(bus.log({ aggregateID })))
- yield* bus.publish(DurableMessage, durableData(aggregateID, "zero"))
- const drained = Array.from(yield* Stream.runCollect(bus.log({ aggregateID, after: 0 })))
- expect(empty).toEqual([{ type: "log.synced", aggregateID }])
- expect(empty[0]).not.toHaveProperty("seq")
- expect(drained).toEqual([{ type: "log.synced", aggregateID, seq: Event.Seq.make(0) }])
- }),
- )
- it.effect("log with follow emits the synced marker at the replay-to-live boundary", () =>
- Effect.gen(function* () {
- const bus = yield* Bus.Service
- const aggregateID = Session.ID.create()
- yield* bus.publish(DurableMessage, durableData(aggregateID, "zero"))
- const fiber = yield* bus
- .log({ aggregateID, follow: true })
- .pipe(Stream.take(3), Stream.runCollect, Effect.forkScoped)
- yield* Effect.yieldNow
- yield* bus.publish(DurableMessage, durableData(aggregateID, "one"))
- const items = Array.from(yield* Fiber.join(fiber))
- expect(items.map((item) => (Bus.isSynced(item) ? item : item.durable?.seq))).toEqual([
- Event.Seq.make(0),
- { type: "log.synced", aggregateID, seq: Event.Seq.make(0) },
- Event.Seq.make(1),
- ])
- }),
- )
- it.effect("log replays across configured read pages", () =>
- Effect.gen(function* () {
- const eventLayer = Bus.layerWith({ logReadPageSize: 2 }).pipe(Layer.provide(LayerNode.compile(Database.node)))
- yield* Effect.gen(function* () {
- const bus = yield* Bus.Service
- const aggregateID = Session.ID.create()
- yield* bus.publish(DurableMessage, durableData(aggregateID, "zero"))
- yield* bus.publish(DurableMessage, durableData(aggregateID, "one"))
- yield* bus.publish(DurableMessage, durableData(aggregateID, "two"))
- yield* bus.publish(DurableMessage, durableData(aggregateID, "three"))
- yield* bus.publish(DurableMessage, durableData(aggregateID, "four"))
- const items = Array.from(yield* Stream.runCollect(bus.log({ aggregateID })))
- expect(items.map((item) => (Bus.isSynced(item) ? item.type : item.durable?.seq))).toEqual([
- Event.Seq.make(0),
- Event.Seq.make(1),
- Event.Seq.make(2),
- Event.Seq.make(3),
- Event.Seq.make(4),
- "log.synced",
- ])
- expect(items.at(-1)).toEqual({ type: "log.synced", aggregateID, seq: Event.Seq.make(4) })
- }).pipe(Effect.provide(Layer.merge(LayerNode.compile(Database.node), eventLayer)))
- }),
- )
- it.effect("log with follow emits events committed during replay after the synced marker", () =>
- Effect.gen(function* () {
- const readStarted = yield* Deferred.make<void>()
- const releaseRead = yield* Deferred.make<void>()
- const firstRead = yield* Ref.make(true)
- const eventLayer = Bus.layerWith({
- beforeAggregateRead: () =>
- Ref.getAndSet(firstRead, false).pipe(
- Effect.flatMap((shouldBlock) => {
- if (!shouldBlock) return Effect.void
- return Deferred.succeed(readStarted, undefined).pipe(Effect.andThen(Deferred.await(releaseRead)))
- }),
- ),
- }).pipe(Layer.provide(LayerNode.compile(Database.node)))
- yield* Effect.gen(function* () {
- const bus = yield* Bus.Service
- const aggregateID = Session.ID.create()
- yield* bus.publish(DurableMessage, durableData(aggregateID, "zero"))
- const fiber = yield* bus
- .log({ aggregateID, follow: true })
- .pipe(Stream.take(3), Stream.runCollect, Effect.forkScoped)
- yield* Deferred.await(readStarted)
- yield* bus.publish(DurableMessage, durableData(aggregateID, "one"))
- yield* Deferred.succeed(releaseRead, undefined)
- const items = Array.from(yield* Fiber.join(fiber))
- expect(items.map((item) => (Bus.isSynced(item) ? item : item.durable?.seq))).toEqual([
- Event.Seq.make(0),
- { type: "log.synced", aggregateID, seq: Event.Seq.make(0) },
- Event.Seq.make(1),
- ])
- }).pipe(Effect.provide(Layer.merge(LayerNode.compile(Database.node), eventLayer)))
- }),
- )
- it.effect("sequences returns the latest committed seq per aggregate and omits unknown aggregates", () =>
- Effect.gen(function* () {
- const bus = yield* Bus.Service
- const first = Session.ID.create()
- const second = Session.ID.create()
- yield* bus.publish(DurableMessage, durableData(first, "zero"))
- yield* bus.publish(DurableMessage, durableData(first, "one"))
- yield* bus.publish(DurableMessage, durableData(second, "zero"))
- const sequences = yield* bus.sequences([first, second, Session.ID.create()])
- expect(sequences).toEqual(
- new Map([
- [first, Event.Seq.make(1)],
- [second, Event.Seq.make(0)],
- ]),
- )
- expect(yield* bus.sequences([])).toEqual(new Map())
- }),
- )
- })
|