Open History to see the last commit
1import { CommandId, EventId, ProjectId } from "@t3tools/contracts";
2import { assert, it } from "@effect/vitest";
3import * as Effect from "effect/Effect";
4import * as Layer from "effect/Layer";
5import * as Schema from "effect/Schema";
6import * as Stream from "effect/Stream";
7import * as SqlClient from "effect/unstable/sql/SqlClient";
8
9import { PersistenceDecodeError } from "../Errors.ts";
10import { OrchestrationEventStore } from "../Services/OrchestrationEventStore.ts";
11import { OrchestrationEventStoreLive } from "./OrchestrationEventStore.ts";
12import { SqlitePersistenceMemory } from "./Sqlite.ts";
13const isPersistenceDecodeError = Schema.is(PersistenceDecodeError);
14
15const layer = it.layer(
16 OrchestrationEventStoreLive.pipe(Layer.provideMerge(SqlitePersistenceMemory)),
17);
18
19layer("OrchestrationEventStore", (it) => {
20 it.effect("stores json columns as strings and replays decoded events", () =>
21 Effect.gen(function* () {
22 const eventStore = yield* OrchestrationEventStore;
23 const sql = yield* SqlClient.SqlClient;
24 const now = "2026-01-01T00:00:00.000Z";
25
26 const appended = yield* eventStore.append({
27 type: "project.created",
28 eventId: EventId.make("evt-store-roundtrip"),
29 aggregateKind: "project",
30 aggregateId: ProjectId.make("project-roundtrip"),
31 occurredAt: now,
32 commandId: CommandId.make("cmd-store-roundtrip"),
33 causationEventId: null,
34 correlationId: CommandId.make("cmd-store-roundtrip"),
35 metadata: {
36 adapterKey: "codex",
37 },
38 payload: {
39 projectId: ProjectId.make("project-roundtrip"),
40 title: "Roundtrip Project",
41 workspaceRoot: "/tmp/project-roundtrip",
42 defaultModelSelection: null,
43 scripts: [],
44 createdAt: now,
45 updatedAt: now,
46 },
47 });
48
49 const storedRows = yield* sql<{
50 readonly payloadJson: string;
51 readonly metadataJson: string;
52 }>`
53 SELECT
54 payload_json AS "payloadJson",
55 metadata_json AS "metadataJson"
56 FROM orchestration_events
57 WHERE event_id = ${appended.eventId}
58 `;
59 assert.equal(storedRows.length, 1);
60 assert.equal(typeof storedRows[0]?.payloadJson, "string");
61 assert.equal(typeof storedRows[0]?.metadataJson, "string");
62
63 const replayed = yield* Stream.runCollect(eventStore.readFromSequence(0, 10)).pipe(
64 Effect.map((chunk) => Array.from(chunk)),
65 );
66 assert.equal(replayed.length, 1);
67 assert.equal(replayed[0]?.type, "project.created");
68 assert.equal(replayed[0]?.metadata.adapterKey, "codex");
69 }),
70 );
71
72 it.effect("fails with PersistenceDecodeError when stored json is invalid", () =>
73 Effect.gen(function* () {
74 const eventStore = yield* OrchestrationEventStore;
75 const sql = yield* SqlClient.SqlClient;
76 const now = "2026-01-01T00:00:00.000Z";
77
78 yield* sql`
79 INSERT INTO orchestration_events (
80 event_id,
81 aggregate_kind,
82 stream_id,
83 stream_version,
84 event_type,
85 occurred_at,
86 command_id,
87 causation_event_id,
88 correlation_id,
89 actor_kind,
90 payload_json,
91 metadata_json
92 )
93 VALUES (
94 ${EventId.make("evt-store-invalid-json")},
95 ${"project"},
96 ${ProjectId.make("project-invalid-json")},
97 ${0},
98 ${"project.created"},
99 ${now},
100 ${CommandId.make("cmd-store-invalid-json")},
101 ${null},
102 ${null},
103 ${"server"},
104 ${"{"},
105 ${"{}"}
106 )
107 `;
108
109 const replayResult = yield* Effect.result(
110 Stream.runCollect(eventStore.readFromSequence(0, 10)),
111 );
112 assert.equal(replayResult._tag, "Failure");
113 if (replayResult._tag === "Failure") {
114 assert.ok(isPersistenceDecodeError(replayResult.failure));
115 assert.ok(
116 replayResult.failure.operation.includes(
117 "OrchestrationEventStore.readFromSequence:decodeRows",
118 ),
119 );
120 }
121 }),
122 );
123});