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