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

projector.settled.test.ts

123 lines3.9 KB

1import {
2 CommandId,
3 EventId,
4 ProjectId,
5 ThreadId,
6 type OrchestrationEvent,
7} from "@t3tools/contracts";
8import { expect, it } from "@effect/vitest";
9import * as Effect from "effect/Effect";
10​
11import { createEmptyReadModel, projectEvent } from "./projector.ts";
12​
13function makeEvent(input: {
14 readonly sequence: number;
15 readonly type: OrchestrationEvent["type"];
16 readonly payload: unknown;
17}): OrchestrationEvent {
18 return {
19 sequence: input.sequence,
20 eventId: EventId.make(`event-${input.sequence}`),
21 type: input.type,
22 aggregateKind: "thread",
23 aggregateId: ThreadId.make("thread-1"),
24 occurredAt: "2026-01-01T00:00:00.000Z",
25 commandId: CommandId.make(`command-${input.sequence}`),
26 causationEventId: null,
27 correlationId: null,
28 metadata: {},
29 payload: input.payload as never,
30 } as OrchestrationEvent;
32​
33it.effect("projects settled lifecycle events", () =>
34 Effect.gen(function* () {
35 const now = "2026-01-01T00:00:00.000Z";
36 const created = yield* projectEvent(
37 createEmptyReadModel(now),
38 makeEvent({
39 sequence: 1,
40 type: "thread.created",
41 payload: {
42 threadId: ThreadId.make("thread-1"),
43 projectId: ProjectId.make("project-1"),
44 title: "Thread",
45 modelSelection: { provider: "codex", model: "gpt-5.4" },
46 runtimeMode: "full-access",
47 interactionMode: "default",
48 branch: null,
49 worktreePath: null,
50 createdAt: now,
51 updatedAt: now,
52 },
53 }),
54 );
55 const settled = yield* projectEvent(
56 created,
57 makeEvent({
58 sequence: 2,
59 type: "thread.settled",
60 payload: { threadId: ThreadId.make("thread-1"), settledAt: now, updatedAt: now },
61 }),
62 );
63 expect(settled.threads[0]?.settledOverride).toBe("settled");
64 expect(settled.threads[0]?.settledAt).toBe(now);
65 expect(settled.threads[0]?.unsettledAt).toBeNull();
66​
67 const unsettleAt = "2026-01-02T00:00:00.000Z";
68 const userUnsettled = yield* projectEvent(
69 settled,
70 makeEvent({
71 sequence: 3,
72 type: "thread.unsettled",
73 payload: { threadId: ThreadId.make("thread-1"), reason: "user", updatedAt: unsettleAt },
74 }),
75 );
76 expect(userUnsettled.threads[0]?.settledOverride).toBe("active");
77 expect(userUnsettled.threads[0]?.settledAt).toBeNull();
78 expect(userUnsettled.threads[0]?.unsettledAt).toBe(unsettleAt);
79​
80 // Clearing the keep-active pin on activity is not a re-entry: the thread
81 // is already in the active list, so the stamp must not move it.
82 const activityAt = "2026-01-03T00:00:00.000Z";
83 const activityUnsettled = yield* projectEvent(
84 userUnsettled,
85 makeEvent({
86 sequence: 4,
87 type: "thread.unsettled",
88 payload: { threadId: ThreadId.make("thread-1"), reason: "activity", updatedAt: activityAt },
89 }),
90 );
91 expect(activityUnsettled.threads[0]?.settledOverride).toBeNull();
92 expect(activityUnsettled.threads[0]?.settledAt).toBeNull();
93 expect(activityUnsettled.threads[0]?.unsettledAt).toBe(unsettleAt);
94​
95 const resettledAt = "2026-01-04T00:00:00.000Z";
96 const resettled = yield* projectEvent(
97 activityUnsettled,
98 makeEvent({
99 sequence: 5,
100 type: "thread.settled",
101 payload: {
102 threadId: ThreadId.make("thread-1"),
103 settledAt: resettledAt,
104 updatedAt: resettledAt,
105 },
106 }),
107 );
108 expect(resettled.threads[0]?.unsettledAt).toBeNull();
109​
110 // Waking a settled thread on activity IS a re-entry and stamps.
111 const wakeAt = "2026-01-05T00:00:00.000Z";
112 const woke = yield* projectEvent(
113 resettled,
114 makeEvent({
115 sequence: 6,
116 type: "thread.unsettled",
117 payload: { threadId: ThreadId.make("thread-1"), reason: "activity", updatedAt: wakeAt },
118 }),
119 );
120 expect(woke.threads[0]?.settledOverride).toBeNull();
121 expect(woke.threads[0]?.unsettledAt).toBe(wakeAt);
122 }),
123);