Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

041_ApplicationEventSource.test.ts

135 lines3.7 KB

1import { assert, it } from "@effect/vitest";
2import * as Effect from "effect/Effect";
3import * as Layer from "effect/Layer";
4import * as SqlClient from "effect/unstable/sql/SqlClient";
5​
6import { runMigrations } from "../Migrations.ts";
7import * as NodeSqliteClient from "../NodeSqliteClient.ts";
8​
9const layer = it.layer(Layer.mergeAll(NodeSqliteClient.layerMemory()));
10​
11layer("040_ApplicationEventSource", (it) => {
12 it.effect("moves V2 events and current project state behind one global sequence", () =>
13 Effect.gen(function* () {
14 const sql = yield* SqlClient.SqlClient;
15 yield* runMigrations({ toMigrationInclusive: 40 });
16​
17 yield* sql`
18 INSERT INTO orchestration_v2_events (
19 event_id,
20 command_id,
21 thread_id,
22 run_id,
23 node_id,
24 provider,
25 driver,
26 provider_instance_id,
27 raw_event_id,
28 event_type,
29 occurred_at,
30 payload_json
31 ) VALUES (
32 'event:v2:existing',
33 'command:v2:existing',
34 'thread:v2:existing',
35 NULL,
36 NULL,
37 'codex',
38 'codex',
39 'codex',
40 NULL,
41 'thread.created',
42 '2026-06-20T00:00:00.000Z',
43 '{}'
44 )
45 `;
46 yield* sql`
47 INSERT INTO orchestration_v2_command_receipts (
48 command_id,
49 thread_id,
50 command_type,
51 accepted_at,
52 result_sequence,
53 status,
54 error
55 ) VALUES (
56 'command:v2:existing',
57 'thread:v2:existing',
58 'thread.create',
59 '2026-06-20T00:00:00.000Z',
60 1,
61 'accepted',
62 NULL
63 )
64 `;
65 yield* sql`
66 INSERT INTO projection_projects (
67 project_id,
68 title,
69 workspace_root,
70 default_model_selection_json,
71 scripts_json,
72 created_at,
73 updated_at,
74 deleted_at
75 ) VALUES (
76 'project:existing',
77 'Existing project',
78 '/work/existing',
79 '{"instanceId":"codex","model":"gpt-5.4"}',
80 '[]',
81 '2026-06-19T00:00:00.000Z',
82 '2026-06-20T00:00:00.000Z',
83 NULL
84 )
85 `;
86​
87 yield* runMigrations({ toMigrationInclusive: 41 });
88​
89 const events = yield* sql<{
90 readonly sequence: number;
91 readonly aggregate_kind: string;
92 readonly stream_id: string;
93 readonly event_type: string;
94 readonly application_event_version: number;
95 }>`
96 SELECT
97 sequence,
98 aggregate_kind,
99 stream_id,
100 event_type,
101 application_event_version
102 FROM orchestration_events
103 WHERE application_event_version = 2
104 ORDER BY sequence ASC
105 `;
106 assert.deepStrictEqual(
107 events.map((event) => [event.aggregate_kind, event.stream_id, event.event_type]),
108 [
109 ["thread", "thread:v2:existing", "thread.created"],
110 ["project", "project:existing", "project.created"],
111 ],
112 );
113 assert.ok(events[0]!.sequence < events[1]!.sequence);
114​
115 const receipts = yield* sql<{
116 readonly aggregate_kind: string;
117 readonly aggregate_id: string;
118 readonly command_type: string;
119 readonly result_sequence: number;
120 }>`
121 SELECT aggregate_kind, aggregate_id, command_type, result_sequence
122 FROM orchestration_command_receipts
123 WHERE command_id = 'command:v2:existing'
124 `;
125 assert.deepStrictEqual(receipts, [
126 {
127 aggregate_kind: "thread",
128 aggregate_id: "thread:v2:existing",
129 command_type: "thread.create",
130 result_sequence: events[0]!.sequence,
131 },
132 ]);
133 }),
134 );
135});