Skip to content

Commit 6bd17e5

Browse files
saphidclaude
andcommitted
test(server): prove the shell snapshot reads one reader snapshot while a write commits
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 33b4795 commit 6bd17e5

1 file changed

Lines changed: 73 additions & 0 deletions

File tree

‎apps/server/src/orchestration-v2/ShellStream.test.ts‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
import { describe, expect, it } from "@effect/vitest";
22
import type {
33
ApplicationStoredEvent,
4+
OrchestrationProjectShell,
45
OrchestrationV2ShellSnapshot,
56
OrchestrationV2ShellStreamItem,
67
OrchestrationV2StoredEvent,
78
OrchestrationV2ThreadShell,
89
} from "@t3tools/contracts";
910
import { ProjectId, ProviderInstanceId, ThreadId } from "@t3tools/contracts";
11+
import * as NodeServices from "@effect/platform-node/NodeServices";
1012
import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
1113
import * as DateTime from "effect/DateTime";
1214
import * as Duration from "effect/Duration";
1315
import * as Effect from "effect/Effect";
16+
import * as Fiber from "effect/Fiber";
17+
import * as FileSystem from "effect/FileSystem";
18+
import * as Deferred from "effect/Deferred";
1419
import * as Option from "effect/Option";
20+
import * as Path from "effect/Path";
1521
import * as SqlClient from "effect/sql/SqlClient";
1622
import * as Stream from "effect/Stream";
1723
import * as TestClock from "effect/testing/TestClock";
@@ -144,6 +150,73 @@ describe("loadShellSnapshotParts", () => {
144150
expect(snapshot.threads.threads).toEqual([thread]);
145151
}).pipe(Effect.provide(NodeSqliteClient.layer({ filename: ":memory:" }))),
146152
);
153+
154+
it.effect(
155+
"reads threads, projects and sequence from one reader snapshot while a write commits",
156+
() =>
157+
Effect.gen(function* () {
158+
const fs = yield* FileSystem.FileSystem;
159+
const path = yield* Path.Path;
160+
const directory = yield* fs.makeTempDirectoryScoped({ prefix: "t3-shell-snapshot-" });
161+
const filename = path.join(directory, "state.sqlite");
162+
yield* Effect.gen(function* () {
163+
const sql = yield* SqlClient.SqlClient;
164+
yield* sql`PRAGMA journal_mode = WAL`;
165+
yield* sql`CREATE TABLE threads(id TEXT NOT NULL)`;
166+
yield* sql`CREATE TABLE projects(id TEXT NOT NULL)`;
167+
yield* sql`CREATE TABLE events(sequence INTEGER NOT NULL)`;
168+
yield* sql`INSERT INTO threads VALUES ('thread-a')`;
169+
yield* sql`INSERT INTO projects VALUES ('project-a')`;
170+
yield* sql`INSERT INTO events VALUES (1)`;
171+
172+
const threadsRead = yield* Deferred.make<void>();
173+
const write = yield* Effect.forkChild(
174+
Deferred.await(threadsRead).pipe(
175+
Effect.andThen(
176+
sql.withTransaction(
177+
Effect.all([
178+
sql`INSERT INTO threads VALUES ('thread-b')`,
179+
sql`INSERT INTO projects VALUES ('project-b')`,
180+
sql`INSERT INTO events VALUES (2)`,
181+
]),
182+
),
183+
),
184+
),
185+
);
186+
187+
const snapshot = yield* loadShellSnapshotParts({
188+
sql,
189+
readThreads: Effect.gen(function* () {
190+
const rows = yield* sql<{ id: string }>`SELECT id FROM threads ORDER BY id`;
191+
yield* Deferred.succeed(threadsRead, undefined);
192+
// The write commits before the rest of the snapshot is read. Holding
193+
// the writer here would deadlock this join.
194+
yield* Fiber.join(write);
195+
return Effect.succeed({
196+
schemaVersion: 1 as const,
197+
snapshotSequence: 0,
198+
threads: rows.map((row) => shellFixture({ id: ThreadId.make(row.id) })),
199+
archivedThreads: [],
200+
});
201+
}),
202+
listProjects: Effect.map(
203+
sql<{ id: string }>`SELECT id FROM projects ORDER BY id`,
204+
(rows) => rows.map((row) => ({ id: row.id }) as OrchestrationProjectShell),
205+
),
206+
latestSequence: Effect.map(
207+
sql<{ sequence: number }>`SELECT MAX(sequence) AS sequence FROM events`,
208+
(rows) => rows[0]?.sequence ?? 0,
209+
),
210+
});
211+
212+
expect({
213+
threads: snapshot.threads.threads.map((thread) => thread.id),
214+
projects: snapshot.projects.map((project) => project.id),
215+
snapshotSequence: snapshot.snapshotSequence,
216+
}).toEqual({ threads: ["thread-a"], projects: ["project-a"], snapshotSequence: 1 });
217+
}).pipe(Effect.provide(NodeSqliteClient.layer({ filename, readerWorker: true })));
218+
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
219+
);
147220
});
148221

149222
function shellFixture(overrides: Partial<OrchestrationV2ThreadShell>): OrchestrationV2ThreadShell {

0 commit comments

Comments
 (0)