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 CLI-origin 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 origin: {
38 surface: "cli",
39 },
40 },
41 payload: {
42 projectId: ProjectId.make("project-roundtrip"),
43 title: "Roundtrip Project",
44 workspaceRoot: "/tmp/project-roundtrip",
45 defaultModelSelection: null,
46 scripts: [],
47 createdAt: now,
48 updatedAt: now,
49 },
50 });
51
52 const storedRows = yield* sql<{
53 readonly payloadJson: string;
54 readonly metadataJson: string;
55 }>`
56 SELECT
57 payload_json AS "payloadJson",
58 metadata_json AS "metadataJson"
59 FROM orchestration_events
60 WHERE event_id = ${appended.eventId}
61 `;
62 assert.equal(storedRows.length, 1);
63 assert.equal(typeof storedRows[0]?.payloadJson, "string");
64 assert.equal(typeof storedRows[0]?.metadataJson, "string");
65
66 const replayed = yield* Stream.runCollect(eventStore.readFromSequence(0, 10)).pipe(
67 Effect.map((chunk) => Array.from(chunk)),
68 );
69 assert.equal(replayed.length, 1);
70 assert.equal(replayed[0]?.type, "project.created");
71 assert.equal(replayed[0]?.metadata.adapterKey, "codex");
72 assert.deepEqual(replayed[0]?.metadata.origin, { surface: "cli" });
73 }),
74 );
75
76 it.effect("fails with PersistenceDecodeError when stored json is invalid", () =>
77 Effect.gen(function* () {
78 const eventStore = yield* OrchestrationEventStore;
79 const sql = yield* SqlClient.SqlClient;
80 const now = "2026-01-01T00:00:00.000Z";
81
82 yield* sql`
83 INSERT INTO orchestration_events (
84 event_id,
85 aggregate_kind,
86 stream_id,
87 stream_version,
88 event_type,
89 occurred_at,
90 command_id,
91 causation_event_id,
92 correlation_id,
93 actor_kind,
94 payload_json,
95 metadata_json
96 )
97 VALUES (
98 ${EventId.make("evt-store-invalid-json")},
99 ${"project"},
100 ${ProjectId.make("project-invalid-json")},
101 ${0},
102 ${"project.created"},
103 ${now},
104 ${CommandId.make("cmd-store-invalid-json")},
105 ${null},
106 ${null},
107 ${"server"},
108 ${"{"},
109 ${"{}"}
110 )
111 `;
112
113 const replayResult = yield* Effect.result(
114 Stream.runCollect(eventStore.readFromSequence(0, 10)),
115 );
116 assert.equal(replayResult._tag, "Failure");
117 if (replayResult._tag === "Failure") {
118 assert.ok(isPersistenceDecodeError(replayResult.failure));
119 assert.ok(
120 replayResult.failure.operation.includes(
121 "OrchestrationEventStore.readFromSequence:decodeRows",
122 ),
123 );
124 }
125 }),
126 );
127});