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

EventSink.test.ts

115 lines3.5 KB

1import { assert, it } from "@effect/vitest";
2import {
3 CommandId,
4 EventId,
5 ProjectId,
6 ProviderInstanceId,
7 RunId,
8 ThreadId,
9 type OrchestrationV2AppThread,
10} from "@t3tools/contracts";
11import * as DateTime from "effect/DateTime";
12import * as Effect from "effect/Effect";
13import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
14import * as Layer from "effect/Layer";
15​
16import * as Sqlite from "../persistence/Sqlite.ts";
17import * as EventSink from "./EventSink.ts";
18import * as EventStore from "./EventStore.ts";
19import * as ProjectionStore from "./ProjectionStore.ts";
20​
21const layer = EventSink.layer.pipe(
22 Layer.provideMerge(
23 Layer.merge(
24 EventStore.layer,
25 ProjectionStore.layer.pipe(Layer.provide(NodeCrypto.layerDigest)),
26 ),
27 ),
28 Layer.provide(Sqlite.layerMemory),
29);
30​
31it.effect("rejects a planned fork whose source was deleted before it committed", () =>
32 Effect.gen(function* () {
33 const sink = yield* EventSink.EventSinkV2;
34 const projections = yield* ProjectionStore.ProjectionStoreV2;
35 const now = yield* DateTime.now;
36 const sourceId = ThreadId.make("fork-source");
37 const targetId = ThreadId.make("fork-target");
38 const providerInstanceId = ProviderInstanceId.make("codex");
39 const source: OrchestrationV2AppThread = {
40 id: sourceId,
41 projectId: ProjectId.make("project"),
42 title: "Source",
43 createdBy: "user",
44 creationSource: "web",
45 providerInstanceId,
46 modelSelection: { instanceId: providerInstanceId, model: "gpt-5" },
47 runtimeMode: "full-access",
48 interactionMode: "default",
49 branch: null,
50 worktreePath: null,
51 activeProviderThreadId: null,
52 lineage: { rootThreadId: sourceId, parentThreadId: null, relationshipToParent: null },
53 forkedFrom: null,
54 createdAt: now,
55 updatedAt: now,
56 archivedAt: null,
57 deletedAt: null,
58 settledOverride: null,
59 settledAt: null,
60 snoozedUntil: null,
61 snoozedAt: null,
62 lastVisitedAt: null,
63 };
64 yield* sink.write({
65 events: [
66 {
67 id: EventId.make("source-created"),
68 type: "thread.created",
69 threadId: sourceId,
70 occurredAt: now,
71 payload: source,
72 },
73 ],
74 });
75 const plannedFork = {
76 commandId: CommandId.make("fork"),
77 commandType: "thread.fork",
78 threadId: targetId,
79 acceptedAt: now,
80 effects: [],
81 events: [
82 {
83 id: EventId.make("fork-created"),
84 type: "thread.created" as const,
85 threadId: targetId,
86 occurredAt: now,
87 payload: {
88 ...source,
89 id: targetId,
90 forkedFrom: { type: "run" as const, threadId: sourceId, runId: RunId.make("run") },
91 },
92 },
93 ],
94 };
95 yield* sink.write({
96 events: [
97 {
98 id: EventId.make("source-deleted"),
99 type: "thread.deleted",
100 threadId: sourceId,
101 occurredAt: now,
102 payload: { ...source, deletedAt: now },
103 },
104 ],
105 });
106 const rejected = yield* sink.commitCommand(plannedFork);
107 assert.equal(rejected.receipt.status, "rejected");
108 assert.isFalse(rejected.committed);
109 const replayed = yield* sink.commitCommand(plannedFork);
110 assert.deepEqual(replayed.receipt, rejected.receipt);
111 assert.isFalse(replayed.committed);
112 const missing = yield* projections.getThread(targetId).pipe(Effect.flip);
113 assert.instanceOf(missing, ProjectionStore.ProjectionStoreThreadNotFoundError);
114 }).pipe(Effect.provide(layer)),
115);