1
0

bus.test.ts 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345
  1. import { describe, expect } from "bun:test"
  2. import { Cause, DateTime, Deferred, Effect, Exit, Fiber, Layer, Option, Ref, Schema, Stream } from "effect"
  3. import { Bus } from "@opencode-ai/core/bus"
  4. import { Event } from "@opencode-ai/schema/event"
  5. import { Session } from "@opencode-ai/schema/session"
  6. import { SessionEvent } from "@opencode-ai/schema/session-event"
  7. import { Database } from "@opencode-ai/core/database/database"
  8. import { AppNodeBuilder } from "@opencode-ai/core/effect/app-node-builder"
  9. import { LayerNode } from "@opencode-ai/util/effect/layer-node"
  10. import { EventSequenceTable, EventTable } from "@opencode-ai/core/event/sql"
  11. import { Location } from "@opencode-ai/core/location"
  12. import { AbsolutePath } from "@opencode-ai/core/schema"
  13. import { Workspace } from "@opencode-ai/core/workspace"
  14. import { eq } from "drizzle-orm"
  15. import { location } from "./fixture/location"
  16. import { testEffect } from "./lib/effect"
  17. const locationLayer = Layer.succeed(
  18. Location.Service,
  19. Location.Service.of(
  20. location({ directory: AbsolutePath.make("project"), workspaceID: Workspace.ID.make("wrk_test") }),
  21. ),
  22. )
  23. const Message = Bus.ephemeral({
  24. type: "test.message",
  25. schema: {
  26. text: Schema.String,
  27. },
  28. })
  29. const SyncMessage = Bus.durable({
  30. type: "test.sync",
  31. durable: {
  32. version: 1,
  33. aggregate: "id",
  34. },
  35. schema: {
  36. id: Schema.String,
  37. text: Schema.String,
  38. },
  39. })
  40. const SyncSent = Bus.durable({
  41. type: "test.sent",
  42. durable: {
  43. version: 1,
  44. aggregate: "messageID",
  45. },
  46. schema: {
  47. messageID: Schema.String,
  48. text: Schema.String,
  49. },
  50. })
  51. const VersionedMessageV1 = Bus.durable({
  52. type: "test.versioned",
  53. durable: { version: 1, aggregate: "id" },
  54. schema: { id: Schema.String },
  55. })
  56. const VersionedMessageV2 = Bus.durable({
  57. type: "test.versioned",
  58. durable: { version: 2, aggregate: "id" },
  59. schema: { id: Schema.String },
  60. })
  61. const GlobalMessage = Bus.ephemeral({
  62. type: "test.global",
  63. schema: {
  64. text: Schema.String,
  65. },
  66. })
  67. const CountMessage = Bus.ephemeral({
  68. type: "test.count",
  69. schema: {
  70. count: Schema.Number,
  71. },
  72. })
  73. const VersionedMessage = Bus.durable({
  74. type: "test.versioned",
  75. durable: {
  76. version: 2,
  77. aggregate: "id",
  78. },
  79. schema: {
  80. id: Schema.String,
  81. text: Schema.String,
  82. },
  83. })
  84. const DurableMessage = SessionEvent.Renamed
  85. const durableData = (sessionID: Session.ID, text: string) => ({
  86. sessionID,
  87. title: text,
  88. })
  89. /** Followed log read without markers: the old `durable` stream shape. */
  90. const tail = (bus: Bus.Interface, input: { aggregateID: string; after?: number }) =>
  91. bus.log({ ...input, follow: true }).pipe(Stream.filter((item): item is Event.Payload => !Bus.isSynced(item)))
  92. const it = testEffect(
  93. AppNodeBuilder.build(LayerNode.group([Database.node, Bus.node, Location.node]), [
  94. [Location.node, locationLayer],
  95. [Bus.node, Bus.configured({ persist: true })],
  96. ]),
  97. )
  98. const itWithoutLocation = testEffect(
  99. AppNodeBuilder.build(LayerNode.group([Database.node, Bus.node]), [
  100. [Bus.node, Bus.configured({ persist: true })],
  101. ]),
  102. )
  103. const itWithoutPersistence = testEffect(
  104. AppNodeBuilder.build(LayerNode.group([Database.node, Bus.node])),
  105. )
  106. describe("Bus", () => {
  107. it.effect("subscribes to multiple event definitions with a discriminated payload union", () =>
  108. Effect.gen(function* () {
  109. const bus = yield* Bus.Service
  110. // @ts-expect-error multi-definition subscriptions require at least one definition
  111. bus.subscribe([])
  112. const fiber = yield* bus
  113. .subscribe([Message, CountMessage])
  114. .pipe(Stream.take(2), Stream.runCollect, Effect.forkScoped)
  115. yield* Effect.yieldNow
  116. yield* bus.publish(Message, { text: "hello" })
  117. yield* bus.publish(CountMessage, { count: 2 })
  118. const received = Array.from(yield* Fiber.join(fiber)).map((event) =>
  119. event.type === "test.message" ? event.data.text : event.data.count,
  120. )
  121. expect(received).toEqual(["hello", 2])
  122. }),
  123. )
  124. it.effect("publishes events with the current location", () =>
  125. Effect.gen(function* () {
  126. const bus = yield* Bus.Service
  127. const fiber = yield* bus.subscribe(Message).pipe(Stream.take(1), Stream.runCollect, Effect.forkScoped)
  128. yield* Effect.yieldNow
  129. const event = yield* bus.publish(Message, { text: "hello" })
  130. const received = Array.from(yield* Fiber.join(fiber))
  131. expect(received).toEqual([event])
  132. expect(event.type).toBe("test.message")
  133. expect(event).not.toHaveProperty("version")
  134. expect(event.data).toEqual({ text: "hello" })
  135. expect(event.location).toEqual({
  136. directory: AbsolutePath.make("project"),
  137. workspaceID: Workspace.ID.make("wrk_test"),
  138. })
  139. }),
  140. )
  141. itWithoutLocation.effect("omits location when no location is available", () =>
  142. Effect.gen(function* () {
  143. const bus = yield* Bus.Service
  144. const event = yield* bus.publish(GlobalMessage, { text: "hello" })
  145. expect(event).not.toHaveProperty("location")
  146. expect(event.type).toBe("test.global")
  147. }),
  148. )
  149. it.effect("publishes definition version", () =>
  150. Effect.gen(function* () {
  151. const bus = yield* Bus.Service
  152. const event = yield* bus.publish(VersionedMessage, { id: "one", text: "hello" })
  153. expect(event.type).toBe("test.versioned")
  154. expect(event.durable?.version).toBe(Event.Version.make(2))
  155. }),
  156. )
  157. it.effect("selects the latest durable definition independent of declaration order", () =>
  158. Effect.sync(() => {
  159. const latest = Bus.durable({
  160. type: "test.out-of-order",
  161. durable: { version: 2, aggregate: "id" },
  162. schema: { id: Schema.String },
  163. })
  164. const historical = Bus.durable({
  165. type: "test.out-of-order",
  166. durable: { version: 1, aggregate: "id" },
  167. schema: { id: Schema.String },
  168. })
  169. expect(Event.latest([latest, historical]).get("test.out-of-order")).toBe(latest)
  170. expect(Event.latest([historical, latest]).get("test.out-of-order")).toBe(latest)
  171. }),
  172. )
  173. it.effect("publishes to typed and wildcard subscriptions", () =>
  174. Effect.gen(function* () {
  175. const bus = yield* Bus.Service
  176. const typed = yield* bus.subscribe(Message).pipe(Stream.take(1), Stream.runCollect, Effect.forkScoped)
  177. const wildcard = yield* bus.subscribe().pipe(Stream.take(1), Stream.runCollect, Effect.forkScoped)
  178. yield* Effect.yieldNow
  179. const event = yield* bus.publish(Message, { text: "hello" })
  180. expect(Array.from(yield* Fiber.join(typed))).toEqual([event])
  181. expect(Array.from(yield* Fiber.join(wildcard))).toEqual([event])
  182. }),
  183. )
  184. it.effect("runs projectors inline", () =>
  185. Effect.gen(function* () {
  186. const bus = yield* Bus.Service
  187. const received = new Array<Event.Payload>()
  188. yield* bus.project(SyncMessage, (event) =>
  189. Effect.sync(() => {
  190. received.push(event)
  191. }),
  192. )
  193. const event = yield* bus.publish(SyncMessage, { id: "one", text: "hello" })
  194. yield* bus.publish(SyncMessage, { id: "one", text: "after unsubscribe" })
  195. expect(received[0]).toEqual(event)
  196. expect(received[1]?.data).toEqual({ id: "one", text: "after unsubscribe" })
  197. }),
  198. )
  199. it.effect("commits local operational state inside a new durable event transaction", () =>
  200. Effect.gen(function* () {
  201. const bus = yield* Bus.Service
  202. const received = new Array<string>()
  203. const aggregateID = Event.ID.create()
  204. yield* bus.project(SyncMessage, () => Effect.sync(() => received.push("projector")))
  205. yield* bus.publish(
  206. SyncMessage,
  207. { id: aggregateID, text: "hello" },
  208. { commit: (seq) => Effect.sync(() => received.push(`commit:${seq}`)) },
  209. )
  210. expect(received).toEqual(["projector", "commit:0"])
  211. }),
  212. )
  213. it.effect("rolls back the durable event and projector when the local commit fails", () =>
  214. Effect.gen(function* () {
  215. const bus = yield* Bus.Service
  216. const { db } = yield* Database.Service
  217. const aggregateID = Event.ID.create()
  218. yield* db.run("CREATE TABLE IF NOT EXISTS event_commit_probe (value text NOT NULL)")
  219. yield* db.run("DELETE FROM event_commit_probe")
  220. yield* bus.project(SyncMessage, () =>
  221. db.run("INSERT INTO event_commit_probe (value) VALUES ('projected')").pipe(Effect.orDie, Effect.asVoid),
  222. )
  223. const exit = yield* bus
  224. .publish(SyncMessage, { id: aggregateID, text: "hello" }, { commit: () => Effect.die("commit failed") })
  225. .pipe(Effect.exit)
  226. expect(String(exit)).toContain("commit failed")
  227. expect(yield* db.all("SELECT value FROM event_commit_probe")).toEqual([])
  228. expect(yield* db.select().from(EventTable).where(eq(EventTable.aggregate_id, aggregateID)).all()).toEqual([])
  229. expect(
  230. yield* db.select().from(EventSequenceTable).where(eq(EventSequenceTable.aggregate_id, aggregateID)).all(),
  231. ).toEqual([])
  232. }),
  233. )
  234. itWithoutPersistence.effect("projects durable events without retaining their payloads", () =>
  235. Effect.gen(function* () {
  236. const bus = yield* Bus.Service
  237. const { db } = yield* Database.Service
  238. const aggregateID = Event.ID.create()
  239. yield* db.run("CREATE TABLE IF NOT EXISTS event_commit_probe (value text NOT NULL)")
  240. yield* bus.project(SyncMessage, () =>
  241. db.run("INSERT INTO event_commit_probe (value) VALUES ('projected')").pipe(Effect.orDie, Effect.asVoid),
  242. )
  243. const event = yield* bus.publish(SyncMessage, { id: aggregateID, text: "hello" })
  244. expect(event.durable?.seq).toBe(Event.Seq.make(0))
  245. expect(yield* db.all("SELECT value FROM event_commit_probe")).toEqual([{ value: "projected" }])
  246. expect(yield* db.select().from(EventTable).where(eq(EventTable.aggregate_id, aggregateID)).all()).toEqual([])
  247. expect(
  248. yield* db.select().from(EventSequenceTable).where(eq(EventSequenceTable.aggregate_id, aggregateID)).all(),
  249. ).toEqual([{ aggregate_id: aggregateID, seq: 0, owner_id: null }])
  250. }),
  251. )
  252. it.effect("rejects local commit hooks on live-only events", () =>
  253. Effect.gen(function* () {
  254. const bus = yield* Bus.Service
  255. const exit = yield* bus.publish(Message, { text: "hello" }, { commit: () => Effect.void }).pipe(Effect.exit)
  256. expect(String(exit)).toContain("Local commit hooks require a durable event")
  257. }),
  258. )
  259. it.effect("runs projectors before publishing to streams", () =>
  260. Effect.gen(function* () {
  261. const bus = yield* Bus.Service
  262. const received = new Array<string>()
  263. const fiber = yield* bus.subscribe().pipe(
  264. Stream.take(1),
  265. Stream.runForEach(() => Effect.sync(() => received.push("stream"))),
  266. Effect.forkScoped,
  267. )
  268. yield* bus.project(SyncMessage, (event) =>
  269. Effect.sync(() => {
  270. received.push(event.type)
  271. }),
  272. )
  273. yield* Effect.yieldNow
  274. yield* bus.publish(SyncMessage, { id: "one", text: "hello" })
  275. yield* Fiber.join(fiber)
  276. expect(received).toEqual([SyncMessage.type, "stream"])
  277. }),
  278. )
  279. it.effect("runs listeners inline after projectors", () =>
  280. Effect.gen(function* () {
  281. const bus = yield* Bus.Service
  282. const received = new Array<string>()
  283. yield* bus.project(SyncMessage, () =>
  284. Effect.sync(() => {
  285. received.push("projector")
  286. }),
  287. )
  288. const unsubscribe = yield* bus.listen(() =>
  289. Effect.sync(() => {
  290. received.push("listener")
  291. }),
  292. )
  293. yield* bus.publish(SyncMessage, { id: "one", text: "hello" })
  294. yield* unsubscribe
  295. yield* bus.publish(SyncMessage, { id: "one", text: "after unsubscribe" })
  296. expect(received).toEqual(["projector", "listener", "projector"])
  297. }),
  298. )
  299. it.effect("isolates observer defects after durable events commit", () =>
  300. Effect.gen(function* () {
  301. const bus = yield* Bus.Service
  302. const received = new Array<string>()
  303. yield* bus.listen(() => {
  304. throw new Error("listener defect")
  305. })
  306. yield* bus.listen((event) =>
  307. Effect.sync(() => {
  308. received.push(event.type)
  309. }),
  310. )
  311. const event = yield* bus.publish(SyncMessage, { id: "one", text: "hello" })
  312. expect(received).toEqual([SyncMessage.type])
  313. expect(event.durable?.seq).toBeNumber()
  314. }),
  315. )
  316. it.effect("notifies global listeners only after a durable event is committed", () =>
  317. Effect.gen(function* () {
  318. const bus = yield* Bus.Service
  319. const { db } = yield* Database.Service
  320. const aggregateID = Event.ID.create()
  321. const observed = new Array<{ id: string; seq: number }>()
  322. yield* bus.listen((event) =>
  323. event.type !== SyncMessage.type
  324. ? Effect.void
  325. : db
  326. .select({ id: EventTable.id, seq: EventTable.seq })
  327. .from(EventTable)
  328. .where(eq(EventTable.id, event.id))
  329. .get()
  330. .pipe(
  331. Effect.orDie,
  332. Effect.tap((row) =>
  333. Effect.sync(() => {
  334. if (row) observed.push(row)
  335. }),
  336. ),
  337. Effect.asVoid,
  338. ),
  339. )
  340. const event = yield* bus.publish(SyncMessage, { id: aggregateID, text: "committed" })
  341. if (!event.durable) throw new Error("Expected durable event metadata")
  342. expect(observed).toEqual([{ id: event.id, seq: event.durable.seq }])
  343. }),
  344. )
  345. it.effect("preserves observer interruption", () =>
  346. Effect.gen(function* () {
  347. const bus = yield* Bus.Service
  348. const { db } = yield* Database.Service
  349. yield* bus.listen(() => Effect.interrupt)
  350. const exit = yield* bus.publish(SyncMessage, { id: "interrupted", text: "hello" }).pipe(Effect.exit)
  351. const committed = yield* db
  352. .select({ id: EventTable.id })
  353. .from(EventTable)
  354. .where(eq(EventTable.aggregate_id, "interrupted"))
  355. .get()
  356. .pipe(Effect.orDie)
  357. expect(Exit.isFailure(exit) && Cause.hasInterrupts(exit.cause)).toBeTrue()
  358. expect(committed).toBeDefined()
  359. }),
  360. )
  361. it.effect("keeps live-only listener defects fail-fast", () =>
  362. Effect.gen(function* () {
  363. const bus = yield* Bus.Service
  364. const defect = new Error("listener defect")
  365. yield* bus.listen(() => Effect.die(defect))
  366. expect(yield* bus.publish(Message, { text: "hello" }).pipe(Effect.catchDefect(Effect.succeed))).toBe(defect)
  367. }),
  368. )
  369. it.effect("inserts durable event rows on publish", () =>
  370. Effect.gen(function* () {
  371. const bus = yield* Bus.Service
  372. const { db } = yield* Database.Service
  373. const aggregateID = Event.ID.create()
  374. yield* bus.publish(SyncMessage, { id: aggregateID, text: "first" })
  375. const rows = yield* db
  376. .select()
  377. .from(EventTable)
  378. .where(eq(EventTable.aggregate_id, aggregateID))
  379. .all()
  380. .pipe(Effect.orDie)
  381. expect(rows).toHaveLength(1)
  382. expect(rows[0]?.type).toBe(Bus.versionedType(SyncMessage.type, 1))
  383. expect(rows[0]?.aggregate_id).toBe(aggregateID)
  384. }),
  385. )
  386. it.effect("increments durable event seq per aggregate", () =>
  387. Effect.gen(function* () {
  388. const bus = yield* Bus.Service
  389. const { db } = yield* Database.Service
  390. const aggregateID = Event.ID.create()
  391. yield* bus.publish(SyncMessage, { id: aggregateID, text: "first" })
  392. yield* bus.publish(SyncMessage, { id: aggregateID, text: "second" })
  393. const rows = yield* db
  394. .select()
  395. .from(EventTable)
  396. .where(eq(EventTable.aggregate_id, aggregateID))
  397. .all()
  398. .pipe(Effect.orDie)
  399. expect(rows.map((row) => row.seq)).toEqual([0, 1])
  400. }),
  401. )
  402. it.effect("replays durable aggregate events after a sequence and tails new events", () =>
  403. Effect.gen(function* () {
  404. const bus = yield* Bus.Service
  405. const aggregateID = Session.ID.create()
  406. yield* bus.publish(DurableMessage, durableData(aggregateID, "zero"))
  407. yield* bus.publish(DurableMessage, durableData(aggregateID, "one"))
  408. const fiber = yield* tail(bus, { aggregateID, after: 0 }).pipe(
  409. Stream.take(2),
  410. Stream.runCollect,
  411. Effect.forkScoped,
  412. )
  413. yield* Effect.yieldNow
  414. yield* bus.publish(DurableMessage, durableData(aggregateID, "two"))
  415. expect(Array.from(yield* Fiber.join(fiber)).map((event) => [event.durable?.seq, event.data])).toEqual([
  416. [1, durableData(aggregateID, "one")],
  417. [2, durableData(aggregateID, "two")],
  418. ])
  419. }),
  420. )
  421. it.effect("catches durable aggregate events published during replay handoff", () =>
  422. Effect.gen(function* () {
  423. const bus = yield* Bus.Service
  424. const aggregateID = Session.ID.create()
  425. yield* bus.publish(DurableMessage, durableData(aggregateID, "zero"))
  426. const fiber = yield* tail(bus, { aggregateID }).pipe(Stream.take(2), Stream.runCollect, Effect.forkScoped)
  427. yield* bus.publish(DurableMessage, durableData(aggregateID, "one"))
  428. expect(Array.from(yield* Fiber.join(fiber)).map((event) => [event.durable?.seq, event.data])).toEqual([
  429. [0, durableData(aggregateID, "zero")],
  430. [1, durableData(aggregateID, "one")],
  431. ])
  432. }),
  433. )
  434. it.effect("retains a durable wake committed while historical replay is paused", () =>
  435. Effect.gen(function* () {
  436. const readStarted = yield* Deferred.make<void>()
  437. const continueRead = yield* Deferred.make<void>()
  438. let pause = true
  439. const eventLayer = AppNodeBuilder.build(LayerNode.group([Database.node, Bus.node]), [
  440. [
  441. Bus.node,
  442. Bus.configured({
  443. persist: true,
  444. beforeAggregateRead: () =>
  445. pause
  446. ? Deferred.succeed(readStarted, undefined).pipe(Effect.andThen(Deferred.await(continueRead)))
  447. : Effect.void,
  448. }),
  449. ],
  450. ])
  451. yield* Effect.gen(function* () {
  452. const bus = yield* Bus.Service
  453. const aggregateID = Session.ID.create()
  454. const fiber = yield* tail(bus, { aggregateID }).pipe(Stream.take(1), Stream.runCollect, Effect.forkScoped)
  455. yield* Deferred.await(readStarted)
  456. pause = false
  457. yield* bus.publish(DurableMessage, durableData(aggregateID, "during handoff"))
  458. yield* Deferred.succeed(continueRead, undefined)
  459. expect(Array.from(yield* Fiber.join(fiber)).map((event) => [event.durable?.seq, event.data])).toEqual([
  460. [0, durableData(aggregateID, "during handoff")],
  461. ])
  462. }).pipe(Effect.provide(eventLayer))
  463. }),
  464. )
  465. it.effect("coalesces durable aggregate wakes while draining every committed event", () =>
  466. Effect.gen(function* () {
  467. const bus = yield* Bus.Service
  468. const aggregateID = Session.ID.create()
  469. const count = 64
  470. const fiber = yield* tail(bus, { aggregateID }).pipe(Stream.take(count), Stream.runCollect, Effect.forkScoped)
  471. yield* Effect.yieldNow
  472. for (let index = 0; index < count; index++) {
  473. yield* bus.publish(DurableMessage, durableData(aggregateID, String(index)))
  474. }
  475. expect(Array.from(yield* Fiber.join(fiber)).map((event) => [event.durable?.seq, event.data])).toEqual(
  476. Array.from({ length: count }, (_, index) => [index, durableData(aggregateID, String(index))]),
  477. )
  478. }),
  479. )
  480. it.effect("omits live-only events from durable aggregate streams", () =>
  481. Effect.gen(function* () {
  482. const bus = yield* Bus.Service
  483. const aggregateID = Session.ID.create()
  484. const fiber = yield* tail(bus, { aggregateID }).pipe(Stream.take(1), Stream.runCollect, Effect.forkScoped)
  485. yield* Effect.yieldNow
  486. yield* bus.publish(Message, { text: "live only" })
  487. yield* bus.publish(DurableMessage, durableData(aggregateID, "durable"))
  488. expect(Array.from(yield* Fiber.join(fiber)).map((event) => event.type)).toEqual([DurableMessage.type])
  489. }),
  490. )
  491. it.effect("uses custom sync aggregate field", () =>
  492. Effect.gen(function* () {
  493. const bus = yield* Bus.Service
  494. const { db } = yield* Database.Service
  495. const aggregateID = Event.ID.create()
  496. yield* bus.publish(SyncSent, { messageID: aggregateID, text: "sent" })
  497. const rows = yield* db
  498. .select()
  499. .from(EventTable)
  500. .where(eq(EventTable.aggregate_id, aggregateID))
  501. .all()
  502. .pipe(Effect.orDie)
  503. expect(rows).toHaveLength(1)
  504. expect(rows[0]?.aggregate_id).toBe(aggregateID)
  505. }),
  506. )
  507. it.effect("replays durable events through projectors", () =>
  508. Effect.gen(function* () {
  509. const bus = yield* Bus.Service
  510. const received = new Array<Event.Payload>()
  511. yield* bus.project(DurableMessage, (event) =>
  512. Effect.sync(() => {
  513. received.push(event)
  514. }),
  515. )
  516. const aggregateID = Session.ID.create()
  517. yield* bus.replay({
  518. id: Event.ID.create(),
  519. created: DateTime.makeUnsafe(0),
  520. type: Bus.versionedType(DurableMessage.type, 1),
  521. seq: 0,
  522. aggregateID,
  523. data: durableData(aggregateID, "hello"),
  524. })
  525. expect(received[0]?.type).toBe(DurableMessage.type)
  526. expect(received[0]?.data).toEqual(durableData(aggregateID, "hello"))
  527. }),
  528. )
  529. it.effect("replay inserts external event rows", () =>
  530. Effect.gen(function* () {
  531. const bus = yield* Bus.Service
  532. const { db } = yield* Database.Service
  533. const aggregateID = Session.ID.create()
  534. yield* bus.replay({
  535. id: Event.ID.create(),
  536. created: DateTime.makeUnsafe(0),
  537. type: Bus.versionedType(DurableMessage.type, 1),
  538. seq: 0,
  539. aggregateID,
  540. data: durableData(aggregateID, "replayed"),
  541. })
  542. const rows = yield* db
  543. .select()
  544. .from(EventTable)
  545. .where(eq(EventTable.aggregate_id, aggregateID))
  546. .all()
  547. .pipe(Effect.orDie)
  548. expect(rows).toHaveLength(1)
  549. expect(rows[0]?.aggregate_id).toBe(aggregateID)
  550. }),
  551. )
  552. it.effect(
  553. "replay rejects an envelope aggregate that differs from its payload without mutating the payload aggregate",
  554. () =>
  555. Effect.gen(function* () {
  556. const bus = yield* Bus.Service
  557. const { db } = yield* Database.Service
  558. const envelopeAggregateID = Session.ID.create()
  559. const payloadAggregateID = Session.ID.create()
  560. const received = new Array<Event.Payload>()
  561. yield* bus.publish(DurableMessage, durableData(payloadAggregateID, "seed"))
  562. yield* bus.project(DurableMessage, (event) =>
  563. Effect.sync(() => {
  564. received.push(event)
  565. }),
  566. )
  567. const exit = yield* bus
  568. .replay({
  569. id: Event.ID.create(),
  570. created: DateTime.makeUnsafe(0),
  571. type: Bus.versionedType(DurableMessage.type, 1),
  572. seq: 1,
  573. aggregateID: envelopeAggregateID,
  574. data: durableData(payloadAggregateID, "replayed"),
  575. })
  576. .pipe(Effect.exit)
  577. const rows = yield* db
  578. .select()
  579. .from(EventTable)
  580. .where(eq(EventTable.aggregate_id, payloadAggregateID))
  581. .all()
  582. .pipe(Effect.orDie)
  583. const sequence = yield* db
  584. .select({ seq: EventSequenceTable.seq })
  585. .from(EventSequenceTable)
  586. .where(eq(EventSequenceTable.aggregate_id, payloadAggregateID))
  587. .get()
  588. .pipe(Effect.orDie)
  589. expect(String(exit)).toContain("Aggregate mismatch")
  590. expect(received).toHaveLength(0)
  591. expect(rows).toHaveLength(1)
  592. expect(sequence).toEqual({ seq: 0 })
  593. }),
  594. )
  595. it.effect("replay defects on sequence mismatch", () =>
  596. Effect.gen(function* () {
  597. const bus = yield* Bus.Service
  598. const aggregateID = Session.ID.create()
  599. yield* bus.replay({
  600. id: Event.ID.create(),
  601. created: DateTime.makeUnsafe(0),
  602. type: Bus.versionedType(DurableMessage.type, 1),
  603. seq: 0,
  604. aggregateID,
  605. data: durableData(aggregateID, "first"),
  606. })
  607. const exit = yield* bus
  608. .replay({
  609. id: Event.ID.create(),
  610. created: DateTime.makeUnsafe(0),
  611. type: Bus.versionedType(DurableMessage.type, 1),
  612. seq: 5,
  613. aggregateID,
  614. data: durableData(aggregateID, "bad"),
  615. })
  616. .pipe(Effect.exit)
  617. expect(String(exit)).toContain("Sequence mismatch")
  618. }),
  619. )
  620. it.effect("replay decodes synchronized transformed values before projection", () =>
  621. Effect.gen(function* () {
  622. const bus = yield* Bus.Service
  623. const aggregateID = Session.ID.create()
  624. const received = new Array<typeof SessionEvent.InstructionsUpdated.Type>()
  625. yield* bus.project(SessionEvent.InstructionsUpdated, (event) =>
  626. Effect.sync(() => {
  627. received.push(event)
  628. }),
  629. )
  630. yield* bus.replay({
  631. id: Event.ID.create(),
  632. created: DateTime.makeUnsafe(0),
  633. type: Bus.versionedType(SessionEvent.InstructionsUpdated.type, 2),
  634. seq: 0,
  635. aggregateID,
  636. data: { sessionID: aggregateID, delta: { "core/context": "0".repeat(64) } },
  637. })
  638. expect(received[0]?.created).toEqual(DateTime.makeUnsafe(0))
  639. }),
  640. )
  641. it.effect("dispatches durable projectors by exact event version", () =>
  642. Effect.gen(function* () {
  643. const bus = yield* Bus.Service
  644. const aggregateID = Session.ID.create()
  645. const received = new Array<typeof VersionedMessageV2.Type>()
  646. yield* bus.project(VersionedMessageV2, (event) =>
  647. Effect.sync(() => {
  648. received.push(event)
  649. }),
  650. )
  651. yield* bus.publish(VersionedMessageV1, { id: aggregateID })
  652. yield* bus.publish(VersionedMessageV2, { id: aggregateID })
  653. expect(received).toHaveLength(1)
  654. expect(received[0]?.durable.version).toBe(Event.Version.make(2))
  655. }),
  656. )
  657. it.effect("replay defects on unknown event type", () =>
  658. Effect.gen(function* () {
  659. const bus = yield* Bus.Service
  660. const exit = yield* bus
  661. .replay({
  662. id: Event.ID.create(),
  663. created: DateTime.makeUnsafe(0),
  664. type: "unknown.event.1",
  665. seq: 0,
  666. aggregateID: Event.ID.create(),
  667. data: {},
  668. })
  669. .pipe(Effect.exit)
  670. expect(String(exit)).toContain("Unknown durable event type")
  671. }),
  672. )
  673. it.effect("replayAll validates contiguous aggregate events", () =>
  674. Effect.gen(function* () {
  675. const bus = yield* Bus.Service
  676. const aggregateID = Session.ID.create()
  677. const source = yield* bus.replayAll([
  678. {
  679. id: Event.ID.create(),
  680. created: DateTime.makeUnsafe(0),
  681. type: Bus.versionedType(DurableMessage.type, 1),
  682. seq: 0,
  683. aggregateID,
  684. data: durableData(aggregateID, "one"),
  685. },
  686. {
  687. id: Event.ID.create(),
  688. created: DateTime.makeUnsafe(0),
  689. type: Bus.versionedType(DurableMessage.type, 1),
  690. seq: 1,
  691. aggregateID,
  692. data: durableData(aggregateID, "two"),
  693. },
  694. ])
  695. expect(source).toBe(aggregateID)
  696. }),
  697. )
  698. it.effect("replayAll accepts later chunks after the first batch", () =>
  699. Effect.gen(function* () {
  700. const bus = yield* Bus.Service
  701. const { db } = yield* Database.Service
  702. const aggregateID = Session.ID.create()
  703. const one = yield* bus.replayAll([
  704. {
  705. id: Event.ID.create(),
  706. created: DateTime.makeUnsafe(0),
  707. type: Bus.versionedType(DurableMessage.type, 1),
  708. seq: 0,
  709. aggregateID,
  710. data: durableData(aggregateID, "one"),
  711. },
  712. {
  713. id: Event.ID.create(),
  714. created: DateTime.makeUnsafe(0),
  715. type: Bus.versionedType(DurableMessage.type, 1),
  716. seq: 1,
  717. aggregateID,
  718. data: durableData(aggregateID, "two"),
  719. },
  720. ])
  721. const two = yield* bus.replayAll([
  722. {
  723. id: Event.ID.create(),
  724. created: DateTime.makeUnsafe(0),
  725. type: Bus.versionedType(DurableMessage.type, 1),
  726. seq: 2,
  727. aggregateID,
  728. data: durableData(aggregateID, "three"),
  729. },
  730. {
  731. id: Event.ID.create(),
  732. created: DateTime.makeUnsafe(0),
  733. type: Bus.versionedType(DurableMessage.type, 1),
  734. seq: 3,
  735. aggregateID,
  736. data: durableData(aggregateID, "four"),
  737. },
  738. ])
  739. const rows = yield* db
  740. .select()
  741. .from(EventTable)
  742. .where(eq(EventTable.aggregate_id, aggregateID))
  743. .all()
  744. .pipe(Effect.orDie)
  745. expect(one).toBe(aggregateID)
  746. expect(two).toBe(aggregateID)
  747. expect(rows.map((row) => row.seq)).toEqual([0, 1, 2, 3])
  748. }),
  749. )
  750. it.effect("claim fences replay owners", () =>
  751. Effect.gen(function* () {
  752. const bus = yield* Bus.Service
  753. const received = new Array<Event.Payload>()
  754. const aggregateID = Session.ID.create()
  755. yield* bus.publish(DurableMessage, durableData(aggregateID, "seed"))
  756. yield* bus.claim(aggregateID, "owner-a")
  757. yield* bus.project(DurableMessage, (event) =>
  758. Effect.sync(() => {
  759. received.push(event)
  760. }),
  761. )
  762. yield* bus.replay(
  763. {
  764. id: Event.ID.create(),
  765. created: DateTime.makeUnsafe(0),
  766. type: Bus.versionedType(DurableMessage.type, 1),
  767. seq: 1,
  768. aggregateID,
  769. data: durableData(aggregateID, "ignored"),
  770. },
  771. { ownerID: "owner-b" },
  772. )
  773. expect(received).toHaveLength(0)
  774. }),
  775. )
  776. it.effect("strict owner fences exact replay", () =>
  777. Effect.gen(function* () {
  778. const bus = yield* Bus.Service
  779. const aggregateID = Session.ID.create()
  780. const id = Event.ID.create()
  781. const replayed = {
  782. id,
  783. created: DateTime.makeUnsafe(0),
  784. type: Bus.versionedType(DurableMessage.type, 1),
  785. seq: 0,
  786. aggregateID,
  787. data: durableData(aggregateID, "owned"),
  788. }
  789. yield* bus.replay(replayed, { ownerID: "owner-a" })
  790. const exit = yield* bus.replay(replayed, { ownerID: "owner-b", strictOwner: true }).pipe(Effect.exit)
  791. expect(String(exit)).toContain("Replay owner mismatch")
  792. }),
  793. )
  794. it.effect("exact replay claims an unowned aggregate", () =>
  795. Effect.gen(function* () {
  796. const bus = yield* Bus.Service
  797. const { db } = yield* Database.Service
  798. const aggregateID = Session.ID.create()
  799. const published = yield* bus.publish(DurableMessage, durableData(aggregateID, "owned"))
  800. const replayed = {
  801. id: published.id,
  802. created: published.created,
  803. type: Bus.versionedType(DurableMessage.type, 1),
  804. seq: published.durable!.seq,
  805. aggregateID,
  806. data: published.data,
  807. }
  808. yield* bus.replay(replayed, { ownerID: "owner-a", strictOwner: true })
  809. const row = yield* db
  810. .select({ ownerID: EventSequenceTable.owner_id })
  811. .from(EventSequenceTable)
  812. .where(eq(EventSequenceTable.aggregate_id, aggregateID))
  813. .get()
  814. .pipe(Effect.orDie)
  815. expect(row?.ownerID).toBe("owner-a")
  816. const exit = yield* bus
  817. .replay(
  818. { ...replayed, id: Event.ID.create(), seq: 1, data: durableData(aggregateID, "conflict") },
  819. { ownerID: "owner-b", strictOwner: true },
  820. )
  821. .pipe(Effect.exit)
  822. expect(String(exit)).toContain("Replay owner mismatch")
  823. }),
  824. )
  825. it.effect("replay with owner claims an unowned sequence", () =>
  826. Effect.gen(function* () {
  827. const bus = yield* Bus.Service
  828. const { db } = yield* Database.Service
  829. const aggregateID = Session.ID.create()
  830. yield* bus.replay(
  831. {
  832. id: Event.ID.create(),
  833. created: DateTime.makeUnsafe(0),
  834. type: Bus.versionedType(DurableMessage.type, 1),
  835. seq: 0,
  836. aggregateID,
  837. data: durableData(aggregateID, "owned"),
  838. },
  839. { ownerID: "owner-1" },
  840. )
  841. const row = yield* db
  842. .select({ seq: EventSequenceTable.seq, ownerID: EventSequenceTable.owner_id })
  843. .from(EventSequenceTable)
  844. .where(eq(EventSequenceTable.aggregate_id, aggregateID))
  845. .get()
  846. .pipe(Effect.orDie)
  847. expect(row).toEqual({ seq: 0, ownerID: "owner-1" })
  848. }),
  849. )
  850. it.effect("replay claims an existing unowned sequence before fencing a different owner", () =>
  851. Effect.gen(function* () {
  852. const bus = yield* Bus.Service
  853. const { db } = yield* Database.Service
  854. const aggregateID = Session.ID.create()
  855. yield* bus.publish(DurableMessage, durableData(aggregateID, "local"))
  856. yield* bus.replay(
  857. {
  858. id: Event.ID.create(),
  859. created: DateTime.makeUnsafe(0),
  860. type: Bus.versionedType(DurableMessage.type, 1),
  861. seq: 1,
  862. aggregateID,
  863. data: durableData(aggregateID, "claimed"),
  864. },
  865. { ownerID: "owner-1" },
  866. )
  867. yield* bus.replay(
  868. {
  869. id: Event.ID.create(),
  870. created: DateTime.makeUnsafe(0),
  871. type: Bus.versionedType(DurableMessage.type, 1),
  872. seq: 2,
  873. aggregateID,
  874. data: durableData(aggregateID, "fenced"),
  875. },
  876. { ownerID: "owner-2" },
  877. )
  878. const rows = yield* db
  879. .select()
  880. .from(EventTable)
  881. .where(eq(EventTable.aggregate_id, aggregateID))
  882. .all()
  883. .pipe(Effect.orDie)
  884. const sequence = yield* db
  885. .select({ seq: EventSequenceTable.seq, ownerID: EventSequenceTable.owner_id })
  886. .from(EventSequenceTable)
  887. .where(eq(EventSequenceTable.aggregate_id, aggregateID))
  888. .get()
  889. .pipe(Effect.orDie)
  890. expect(rows.map((row) => row.seq)).toEqual([0, 1])
  891. expect(sequence).toEqual({ seq: 1, ownerID: "owner-1" })
  892. }),
  893. )
  894. it.effect("strict replay rejects an owner conflict instead of silently skipping it", () =>
  895. Effect.gen(function* () {
  896. const bus = yield* Bus.Service
  897. const aggregateID = Session.ID.create()
  898. yield* bus.replay(
  899. {
  900. id: Event.ID.create(),
  901. created: DateTime.makeUnsafe(0),
  902. type: Bus.versionedType(DurableMessage.type, 1),
  903. seq: 0,
  904. aggregateID,
  905. data: durableData(aggregateID, "claimed"),
  906. },
  907. { ownerID: "owner-1" },
  908. )
  909. const exit = yield* bus
  910. .replay(
  911. {
  912. id: Event.ID.create(),
  913. created: DateTime.makeUnsafe(0),
  914. type: Bus.versionedType(DurableMessage.type, 1),
  915. seq: 1,
  916. aggregateID,
  917. data: durableData(aggregateID, "conflict"),
  918. },
  919. { ownerID: "owner-2", strictOwner: true },
  920. )
  921. .pipe(Effect.exit)
  922. expect(String(exit)).toContain("Replay owner mismatch")
  923. }),
  924. )
  925. it.effect("publishes accepted replay with its durable sequence and suppresses stale replay", () =>
  926. Effect.gen(function* () {
  927. const bus = yield* Bus.Service
  928. const received = new Array<Event.Payload>()
  929. const aggregateID = Session.ID.create()
  930. yield* bus.listen((event) => Effect.sync(() => received.push(event)))
  931. const replayed = {
  932. id: Event.ID.create(),
  933. created: DateTime.makeUnsafe(0),
  934. type: Bus.versionedType(DurableMessage.type, 1),
  935. seq: 0,
  936. aggregateID,
  937. data: durableData(aggregateID, "replayed"),
  938. }
  939. yield* bus.replay(replayed, { publish: true })
  940. yield* bus.replay(replayed, { publish: true })
  941. expect(received).toMatchObject([{ id: replayed.id, durable: { seq: 0, version: 1 }, data: replayed.data }])
  942. }),
  943. )
  944. it.effect("rejects divergent stale replay without publishing it", () =>
  945. Effect.gen(function* () {
  946. const bus = yield* Bus.Service
  947. const received = new Array<Event.Payload>()
  948. const aggregateID = Session.ID.create()
  949. const replayed = {
  950. id: Event.ID.create(),
  951. created: DateTime.makeUnsafe(0),
  952. type: Bus.versionedType(DurableMessage.type, 1),
  953. seq: 0,
  954. aggregateID,
  955. data: durableData(aggregateID, "original"),
  956. }
  957. yield* bus.listen((event) => Effect.sync(() => received.push(event)))
  958. yield* bus.replay(replayed, { publish: true })
  959. const exit = yield* bus
  960. .replay({ ...replayed, data: durableData(aggregateID, "divergent") }, { publish: true })
  961. .pipe(Effect.exit)
  962. expect(String(exit)).toContain("Replay diverged")
  963. expect(received).toHaveLength(1)
  964. }),
  965. )
  966. it.effect("rejects an event ID reused at another aggregate position", () =>
  967. Effect.gen(function* () {
  968. const bus = yield* Bus.Service
  969. const aggregateID = Session.ID.create()
  970. const id = Event.ID.create()
  971. yield* bus.replay({
  972. id,
  973. created: DateTime.makeUnsafe(0),
  974. type: Bus.versionedType(DurableMessage.type, 1),
  975. seq: 0,
  976. aggregateID,
  977. data: durableData(aggregateID, "first"),
  978. })
  979. const exit = yield* bus
  980. .replay({
  981. id,
  982. created: DateTime.makeUnsafe(0),
  983. type: Bus.versionedType(DurableMessage.type, 1),
  984. seq: 1,
  985. aggregateID,
  986. data: durableData(aggregateID, "second"),
  987. })
  988. .pipe(Effect.exit)
  989. expect(String(exit)).toContain(`Event ${id} already exists`)
  990. }),
  991. )
  992. it.effect("replay from a different owner leaves claimed sequence unchanged", () =>
  993. Effect.gen(function* () {
  994. const bus = yield* Bus.Service
  995. const { db } = yield* Database.Service
  996. const aggregateID = Session.ID.create()
  997. const received = new Array<Event.Payload>()
  998. yield* bus.listen((event) => Effect.sync(() => received.push(event)))
  999. yield* bus.replay(
  1000. {
  1001. id: Event.ID.create(),
  1002. created: DateTime.makeUnsafe(0),
  1003. type: Bus.versionedType(DurableMessage.type, 1),
  1004. seq: 0,
  1005. aggregateID,
  1006. data: durableData(aggregateID, "first"),
  1007. },
  1008. { ownerID: "owner-1" },
  1009. )
  1010. yield* bus.replay(
  1011. {
  1012. id: Event.ID.create(),
  1013. created: DateTime.makeUnsafe(0),
  1014. type: Bus.versionedType(DurableMessage.type, 1),
  1015. seq: 1,
  1016. aggregateID,
  1017. data: durableData(aggregateID, "ignored"),
  1018. },
  1019. { ownerID: "owner-2", publish: true },
  1020. )
  1021. const rows = yield* db
  1022. .select()
  1023. .from(EventTable)
  1024. .where(eq(EventTable.aggregate_id, aggregateID))
  1025. .all()
  1026. .pipe(Effect.orDie)
  1027. const sequence = yield* db
  1028. .select({ seq: EventSequenceTable.seq, ownerID: EventSequenceTable.owner_id })
  1029. .from(EventSequenceTable)
  1030. .where(eq(EventSequenceTable.aggregate_id, aggregateID))
  1031. .get()
  1032. .pipe(Effect.orDie)
  1033. expect(rows).toHaveLength(1)
  1034. expect(sequence).toEqual({ seq: 0, ownerID: "owner-1" })
  1035. expect(received).toHaveLength(0)
  1036. }),
  1037. )
  1038. it.effect("claim updates the event sequence owner", () =>
  1039. Effect.gen(function* () {
  1040. const bus = yield* Bus.Service
  1041. const { db } = yield* Database.Service
  1042. const aggregateID = Event.ID.create()
  1043. yield* bus.publish(SyncMessage, { id: aggregateID, text: "claimed" })
  1044. yield* bus.claim(aggregateID, "owner-1")
  1045. yield* bus.claim(aggregateID, "owner-2")
  1046. const row = yield* db
  1047. .select({ seq: EventSequenceTable.seq, ownerID: EventSequenceTable.owner_id })
  1048. .from(EventSequenceTable)
  1049. .where(eq(EventSequenceTable.aggregate_id, aggregateID))
  1050. .get()
  1051. .pipe(Effect.orDie)
  1052. expect(row).toEqual({ seq: 0, ownerID: "owner-2" })
  1053. }),
  1054. )
  1055. it.effect("remove clears durable event sequence", () =>
  1056. Effect.gen(function* () {
  1057. const bus = yield* Bus.Service
  1058. const received = new Array<Event.Payload>()
  1059. const aggregateID = Session.ID.create()
  1060. yield* bus.publish(DurableMessage, durableData(aggregateID, "seed"))
  1061. yield* bus.remove(aggregateID)
  1062. yield* bus.project(DurableMessage, (event) =>
  1063. Effect.sync(() => {
  1064. received.push(event)
  1065. }),
  1066. )
  1067. yield* bus.replay({
  1068. id: Event.ID.create(),
  1069. created: DateTime.makeUnsafe(0),
  1070. type: Bus.versionedType(DurableMessage.type, 1),
  1071. seq: 0,
  1072. aggregateID,
  1073. data: durableData(aggregateID, "replayed"),
  1074. })
  1075. expect(received[0]?.data).toEqual(durableData(aggregateID, "replayed"))
  1076. }),
  1077. )
  1078. it.effect("log without follow replays events and completes with a synced marker", () =>
  1079. Effect.gen(function* () {
  1080. const bus = yield* Bus.Service
  1081. const aggregateID = Session.ID.create()
  1082. yield* bus.publish(DurableMessage, durableData(aggregateID, "zero"))
  1083. yield* bus.publish(DurableMessage, durableData(aggregateID, "one"))
  1084. const items = Array.from(yield* Stream.runCollect(bus.log({ aggregateID })))
  1085. expect(items.map((item) => (Bus.isSynced(item) ? item.type : item.durable?.seq))).toEqual([
  1086. Event.Seq.make(0),
  1087. Event.Seq.make(1),
  1088. "log.synced",
  1089. ])
  1090. expect(items.at(-1)).toEqual({ type: "log.synced", aggregateID, seq: Event.Seq.make(1) })
  1091. }),
  1092. )
  1093. it.effect("log synced marker omits seq for an empty log and keeps the cursor otherwise", () =>
  1094. Effect.gen(function* () {
  1095. const bus = yield* Bus.Service
  1096. const aggregateID = Session.ID.create()
  1097. const empty = Array.from(yield* Stream.runCollect(bus.log({ aggregateID })))
  1098. yield* bus.publish(DurableMessage, durableData(aggregateID, "zero"))
  1099. const drained = Array.from(yield* Stream.runCollect(bus.log({ aggregateID, after: 0 })))
  1100. expect(empty).toEqual([{ type: "log.synced", aggregateID }])
  1101. expect(empty[0]).not.toHaveProperty("seq")
  1102. expect(drained).toEqual([{ type: "log.synced", aggregateID, seq: Event.Seq.make(0) }])
  1103. }),
  1104. )
  1105. it.effect("log with follow emits the synced marker at the replay-to-live boundary", () =>
  1106. Effect.gen(function* () {
  1107. const bus = yield* Bus.Service
  1108. const aggregateID = Session.ID.create()
  1109. yield* bus.publish(DurableMessage, durableData(aggregateID, "zero"))
  1110. const fiber = yield* bus
  1111. .log({ aggregateID, follow: true })
  1112. .pipe(Stream.take(3), Stream.runCollect, Effect.forkScoped)
  1113. yield* Effect.yieldNow
  1114. yield* bus.publish(DurableMessage, durableData(aggregateID, "one"))
  1115. const items = Array.from(yield* Fiber.join(fiber))
  1116. expect(items.map((item) => (Bus.isSynced(item) ? item : item.durable?.seq))).toEqual([
  1117. Event.Seq.make(0),
  1118. { type: "log.synced", aggregateID, seq: Event.Seq.make(0) },
  1119. Event.Seq.make(1),
  1120. ])
  1121. }),
  1122. )
  1123. it.effect("log replays across configured read pages", () =>
  1124. Effect.gen(function* () {
  1125. const eventLayer = AppNodeBuilder.build(LayerNode.group([Database.node, Bus.node]), [
  1126. [Bus.node, Bus.configured({ persist: true, logReadPageSize: 2 })],
  1127. ])
  1128. yield* Effect.gen(function* () {
  1129. const bus = yield* Bus.Service
  1130. const aggregateID = Session.ID.create()
  1131. yield* bus.publish(DurableMessage, durableData(aggregateID, "zero"))
  1132. yield* bus.publish(DurableMessage, durableData(aggregateID, "one"))
  1133. yield* bus.publish(DurableMessage, durableData(aggregateID, "two"))
  1134. yield* bus.publish(DurableMessage, durableData(aggregateID, "three"))
  1135. yield* bus.publish(DurableMessage, durableData(aggregateID, "four"))
  1136. const items = Array.from(yield* Stream.runCollect(bus.log({ aggregateID })))
  1137. expect(items.map((item) => (Bus.isSynced(item) ? item.type : item.durable?.seq))).toEqual([
  1138. Event.Seq.make(0),
  1139. Event.Seq.make(1),
  1140. Event.Seq.make(2),
  1141. Event.Seq.make(3),
  1142. Event.Seq.make(4),
  1143. "log.synced",
  1144. ])
  1145. expect(items.at(-1)).toEqual({ type: "log.synced", aggregateID, seq: Event.Seq.make(4) })
  1146. }).pipe(Effect.provide(eventLayer))
  1147. }),
  1148. )
  1149. it.effect("log with follow emits events committed during replay after the synced marker", () =>
  1150. Effect.gen(function* () {
  1151. const readStarted = yield* Deferred.make<void>()
  1152. const releaseRead = yield* Deferred.make<void>()
  1153. const firstRead = yield* Ref.make(true)
  1154. const eventLayer = AppNodeBuilder.build(LayerNode.group([Database.node, Bus.node]), [
  1155. [
  1156. Bus.node,
  1157. Bus.configured({
  1158. persist: true,
  1159. beforeAggregateRead: () =>
  1160. Ref.getAndSet(firstRead, false).pipe(
  1161. Effect.flatMap((shouldBlock) => {
  1162. if (!shouldBlock) return Effect.void
  1163. return Deferred.succeed(readStarted, undefined).pipe(Effect.andThen(Deferred.await(releaseRead)))
  1164. }),
  1165. ),
  1166. }),
  1167. ],
  1168. ])
  1169. yield* Effect.gen(function* () {
  1170. const bus = yield* Bus.Service
  1171. const aggregateID = Session.ID.create()
  1172. yield* bus.publish(DurableMessage, durableData(aggregateID, "zero"))
  1173. const fiber = yield* bus
  1174. .log({ aggregateID, follow: true })
  1175. .pipe(Stream.take(3), Stream.runCollect, Effect.forkScoped)
  1176. yield* Deferred.await(readStarted)
  1177. yield* bus.publish(DurableMessage, durableData(aggregateID, "one"))
  1178. yield* Deferred.succeed(releaseRead, undefined)
  1179. const items = Array.from(yield* Fiber.join(fiber))
  1180. expect(items.map((item) => (Bus.isSynced(item) ? item : item.durable?.seq))).toEqual([
  1181. Event.Seq.make(0),
  1182. { type: "log.synced", aggregateID, seq: Event.Seq.make(0) },
  1183. Event.Seq.make(1),
  1184. ])
  1185. }).pipe(Effect.provide(eventLayer))
  1186. }),
  1187. )
  1188. })