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

projector.pinned.test.ts

154 lines4.4 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 pin 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 expect(created.threads[0]?.pinnedAt ?? null).toBeNull();
56​
57 const pinned = yield* projectEvent(
58 created,
59 makeEvent({
60 sequence: 2,
61 type: "thread.pinned",
62 payload: { threadId: ThreadId.make("thread-1"), pinnedAt: now, updatedAt: now },
63 }),
64 );
65 expect(pinned.threads[0]?.pinnedAt).toBe(now);
66​
67 const unpinned = yield* projectEvent(
68 pinned,
69 makeEvent({
70 sequence: 3,
71 type: "thread.unpinned",
72 payload: { threadId: ThreadId.make("thread-1"), updatedAt: now },
73 }),
74 );
75 expect(unpinned.threads[0]?.pinnedAt).toBeNull();
76 }),
77);
78​
79it.effect("projects pin order key lifecycle", () =>
80 Effect.gen(function* () {
81 const now = "2026-01-01T00:00:00.000Z";
82 const created = yield* projectEvent(
83 createEmptyReadModel(now),
84 makeEvent({
85 sequence: 1,
86 type: "thread.created",
87 payload: {
88 threadId: ThreadId.make("thread-1"),
89 projectId: ProjectId.make("project-1"),
90 title: "Thread",
91 modelSelection: { provider: "codex", model: "gpt-5.4" },
92 runtimeMode: "full-access",
93 interactionMode: "default",
94 branch: null,
95 worktreePath: null,
96 createdAt: now,
97 updatedAt: now,
98 },
99 }),
100 );
101 expect(created.threads[0]?.pinOrderKey ?? null).toBeNull();
102​
103 // Fresh pin carries the client's slot in the arranged order.
104 const pinned = yield* projectEvent(
105 created,
106 makeEvent({
107 sequence: 2,
108 type: "thread.pinned",
109 payload: {
110 threadId: ThreadId.make("thread-1"),
111 pinnedAt: now,
112 pinOrderKey: "g",
113 updatedAt: now,
114 },
115 }),
116 );
117 expect(pinned.threads[0]?.pinOrderKey).toBe("g");
118​
119 // Re-pins and events from pre-reorder servers omit the field entirely;
120 // the existing key must survive rather than being nulled out.
121 const repinned = yield* projectEvent(
122 pinned,
123 makeEvent({
124 sequence: 3,
125 type: "thread.pinned",
126 payload: { threadId: ThreadId.make("thread-1"), pinnedAt: now, updatedAt: now },
127 }),
128 );
129 expect(repinned.threads[0]?.pinOrderKey).toBe("g");
130​
131 // A drag persists the new slot.
132 const reordered = yield* projectEvent(
133 repinned,
134 makeEvent({
135 sequence: 4,
136 type: "thread.pin-reordered",
137 payload: { threadId: ThreadId.make("thread-1"), orderKey: "m", updatedAt: now },
138 }),
139 );
140 expect(reordered.threads[0]?.pinOrderKey).toBe("m");
141​
142 // Unpin clears the slot: re-pinning is "pin again", not "restore an
143 // ancient position".
144 const unpinned = yield* projectEvent(
145 reordered,
146 makeEvent({
147 sequence: 5,
148 type: "thread.unpinned",
149 payload: { threadId: ThreadId.make("thread-1"), updatedAt: now },
150 }),
151 );
152 expect(unpinned.threads[0]?.pinOrderKey).toBeNull();
153 }),
154);