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

decider.pinned.test.ts

325 lines11.1 KB

1import {
2 CommandId,
3 ProjectId,
4 ProviderInstanceId,
5 ThreadId,
6 type OrchestrationReadModel,
7} from "@t3tools/contracts";
8import * as NodeServices from "@effect/platform-node/NodeServices";
9import { expect, it } from "@effect/vitest";
10import * as Effect from "effect/Effect";
11​
12import { decideOrchestrationCommand } from "./decider.ts";
13​
14const NOW = "2026-01-01T00:00:00.000Z";
15const PINNED_AT = "1969-12-30T00:00:00.000Z";
16​
17function makeReadModel(input: {
18 readonly pinnedAt?: string | null;
19 readonly pinOrderKey?: string | null;
20 readonly archivedAt?: string | null;
21 readonly settledOverride?: "settled" | "active" | null;
22 readonly settledAt?: string | null;
23 readonly snoozedUntil?: string | null;
24 readonly snoozedAt?: string | null;
25}): OrchestrationReadModel {
26 return {
27 snapshotSequence: 0,
28 projects: [],
29 threads: [
30 {
31 id: ThreadId.make("thread-1"),
32 projectId: ProjectId.make("project-1"),
33 title: "Thread",
34 modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.4" },
35 runtimeMode: "full-access",
36 interactionMode: "default",
37 branch: null,
38 worktreePath: null,
39 pullRequests: [],
40 latestTurn: null,
41 createdAt: NOW,
42 updatedAt: NOW,
43 archivedAt: input.archivedAt ?? null,
44 settledOverride: input.settledOverride ?? null,
45 settledAt: input.settledAt ?? (input.settledOverride === "settled" ? NOW : null),
46 snoozedUntil: input.snoozedUntil ?? null,
47 snoozedAt: input.snoozedAt ?? (input.snoozedUntil != null ? PINNED_AT : null),
48 pinnedAt: input.pinnedAt ?? null,
49 pinOrderKey: input.pinOrderKey ?? null,
50 deletedAt: null,
51 messages: [],
52 proposedPlans: [],
53 activities: [],
54 checkpoints: [],
55 session: null,
56 },
57 ],
58 updatedAt: NOW,
59 };
61​
62it.layer(NodeServices.layer)("pinned thread decider", (it) => {
63 it.effect("pins a thread, stamping pinnedAt and updatedAt together", () =>
64 Effect.gen(function* () {
65 const event = yield* decideOrchestrationCommand({
66 command: {
67 type: "thread.pin",
68 commandId: CommandId.make("cmd-pin"),
69 threadId: ThreadId.make("thread-1"),
70 },
71 readModel: makeReadModel({}),
72 });
73 const events = Array.isArray(event) ? event : [event];
74 expect(events).toHaveLength(1);
75 expect(events[0]?.type).toBe("thread.pinned");
76 if (events[0]?.type === "thread.pinned") {
77 expect(events[0].payload.pinnedAt).toBe(events[0].payload.updatedAt);
78 }
79 }),
80 );
81​
82 it.effect("re-pinning preserves the original pinnedAt and updatedAt", () =>
83 Effect.gen(function* () {
84 const event = yield* decideOrchestrationCommand({
85 command: {
86 type: "thread.pin",
87 commandId: CommandId.make("cmd-pin-again"),
88 threadId: ThreadId.make("thread-1"),
89 },
90 readModel: makeReadModel({ pinnedAt: PINNED_AT }),
91 });
92 const events = Array.isArray(event) ? event : [event];
93 expect(events[0]?.type).toBe("thread.pinned");
94 if (events[0]?.type === "thread.pinned") {
95 expect(events[0].payload.pinnedAt).toBe(PINNED_AT);
96 expect(events[0].payload.updatedAt).toBe(NOW);
97 }
98 }),
99 );
100​
101 it.effect("unpins a pinned thread", () =>
102 Effect.gen(function* () {
103 const event = yield* decideOrchestrationCommand({
104 command: {
105 type: "thread.unpin",
106 commandId: CommandId.make("cmd-unpin"),
107 threadId: ThreadId.make("thread-1"),
108 },
109 readModel: makeReadModel({ pinnedAt: PINNED_AT }),
110 });
111 const events = Array.isArray(event) ? event : [event];
112 expect(events[0]?.type).toBe("thread.unpinned");
113 if (events[0]?.type === "thread.unpinned") {
114 expect(events[0].payload.updatedAt).not.toBe(NOW);
115 }
116 }),
117 );
118​
119 it.effect("unpinning an unpinned thread preserves updatedAt", () =>
120 Effect.gen(function* () {
121 const event = yield* decideOrchestrationCommand({
122 command: {
123 type: "thread.unpin",
124 commandId: CommandId.make("cmd-unpin-noop"),
125 threadId: ThreadId.make("thread-1"),
126 },
127 readModel: makeReadModel({}),
128 });
129 const events = Array.isArray(event) ? event : [event];
130 expect(events[0]?.type).toBe("thread.unpinned");
131 if (events[0]?.type === "thread.unpinned") {
132 expect(events[0].payload.updatedAt).toBe(NOW);
133 }
134 }),
135 );
136​
137 it.effect("pinning a settled thread also un-settles it", () =>
138 Effect.gen(function* () {
139 const event = yield* decideOrchestrationCommand({
140 command: {
141 type: "thread.pin",
142 commandId: CommandId.make("cmd-pin-settled"),
143 threadId: ThreadId.make("thread-1"),
144 },
145 readModel: makeReadModel({ settledOverride: "settled" }),
146 });
147 const events = Array.isArray(event) ? event : [event];
148 expect(events.map((entry) => entry.type)).toEqual(["thread.pinned", "thread.unsettled"]);
149 const unsettled = events.find((entry) => entry.type === "thread.unsettled");
150 if (unsettled?.type === "thread.unsettled") {
151 expect(unsettled.payload.reason).toBe("user");
152 }
153 }),
154 );
155​
156 it.effect("pinning a snoozed thread also wakes it", () =>
157 Effect.gen(function* () {
158 const event = yield* decideOrchestrationCommand({
159 command: {
160 type: "thread.pin",
161 commandId: CommandId.make("cmd-pin-snoozed"),
162 threadId: ThreadId.make("thread-1"),
163 },
164 readModel: makeReadModel({ snoozedUntil: "1970-01-02T09:00:00.000Z" }),
165 });
166 const events = Array.isArray(event) ? event : [event];
167 expect(events.map((entry) => entry.type)).toEqual(["thread.pinned", "thread.unsnoozed"]);
168 }),
169 );
170​
171 it.effect("pinning an unparked thread emits only thread.pinned", () =>
172 Effect.gen(function* () {
173 const event = yield* decideOrchestrationCommand({
174 command: {
175 type: "thread.pin",
176 commandId: CommandId.make("cmd-pin-plain"),
177 threadId: ThreadId.make("thread-1"),
178 },
179 readModel: makeReadModel({}),
180 });
181 const events = Array.isArray(event) ? event : [event];
182 expect(events.map((entry) => entry.type)).toEqual(["thread.pinned"]);
183 }),
184 );
185​
186 it.effect("settling a pinned thread also unpins it", () =>
187 Effect.gen(function* () {
188 const event = yield* decideOrchestrationCommand({
189 command: {
190 type: "thread.settle",
191 commandId: CommandId.make("cmd-settle-pinned"),
192 threadId: ThreadId.make("thread-1"),
193 },
194 readModel: makeReadModel({ pinnedAt: PINNED_AT }),
195 });
196 const events = Array.isArray(event) ? event : [event];
197 expect(events.map((entry) => entry.type)).toEqual(["thread.settled", "thread.unpinned"]);
198 }),
199 );
200​
201 it.effect("settling an unpinned thread emits no unpin event", () =>
202 Effect.gen(function* () {
203 const event = yield* decideOrchestrationCommand({
204 command: {
205 type: "thread.settle",
206 commandId: CommandId.make("cmd-settle-unpinned"),
207 threadId: ThreadId.make("thread-1"),
208 },
209 readModel: makeReadModel({}),
210 });
211 const events = Array.isArray(event) ? event : [event];
212 expect(events.map((entry) => entry.type)).toEqual(["thread.settled"]);
213 }),
214 );
215​
216 it.effect("rejects pinning an archived thread", () =>
217 Effect.gen(function* () {
218 const error = yield* decideOrchestrationCommand({
219 command: {
220 type: "thread.pin",
221 commandId: CommandId.make("cmd-pin-archived"),
222 threadId: ThreadId.make("thread-1"),
223 },
224 readModel: makeReadModel({ archivedAt: NOW }),
225 }).pipe(Effect.flip);
226 expect(error._tag).toBe("OrchestrationCommandInvariantError");
227 }),
228 );
229​
230 it.effect("a fresh pin carries the client's order key", () =>
231 Effect.gen(function* () {
232 const event = yield* decideOrchestrationCommand({
233 command: {
234 type: "thread.pin",
235 commandId: CommandId.make("cmd-pin-keyed"),
236 threadId: ThreadId.make("thread-1"),
237 orderKey: "g",
238 },
239 readModel: makeReadModel({}),
240 });
241 const events = Array.isArray(event) ? event : [event];
242 expect(events[0]?.type).toBe("thread.pinned");
243 if (events[0]?.type === "thread.pinned") {
244 expect(events[0].payload.pinOrderKey).toBe("g");
245 }
246 }),
247 );
248​
249 it.effect(
250 "re-pinning ignores the incoming order key so raced pins cannot move a placed thread",
251 () =>
252 Effect.gen(function* () {
253 const event = yield* decideOrchestrationCommand({
254 command: {
255 type: "thread.pin",
256 commandId: CommandId.make("cmd-pin-keyed-again"),
257 threadId: ThreadId.make("thread-1"),
258 orderKey: "t",
259 },
260 readModel: makeReadModel({ pinnedAt: PINNED_AT, pinOrderKey: "g" }),
261 });
262 const events = Array.isArray(event) ? event : [event];
263 expect(events[0]?.type).toBe("thread.pinned");
264 if (events[0]?.type === "thread.pinned") {
265 expect(events[0].payload.pinOrderKey).toBeUndefined();
266 }
267 }),
268 );
269​
270 it.effect("reorders a pinned thread, stamping the new key", () =>
271 Effect.gen(function* () {
272 const event = yield* decideOrchestrationCommand({
273 command: {
274 type: "thread.pin.reorder",
275 commandId: CommandId.make("cmd-reorder"),
276 threadId: ThreadId.make("thread-1"),
277 orderKey: "m",
278 },
279 readModel: makeReadModel({ pinnedAt: PINNED_AT, pinOrderKey: "g" }),
280 });
281 const events = Array.isArray(event) ? event : [event];
282 expect(events[0]?.type).toBe("thread.pin-reordered");
283 if (events[0]?.type === "thread.pin-reordered") {
284 expect(events[0].payload.orderKey).toBe("m");
285 // A real move stamps the command time (the test clock), not the
286 // thread's previous updatedAt.
287 expect(events[0].payload.updatedAt).not.toBe(NOW);
288 }
289 }),
290 );
291​
292 it.effect("reordering onto the same key preserves updatedAt", () =>
293 Effect.gen(function* () {
294 const event = yield* decideOrchestrationCommand({
295 command: {
296 type: "thread.pin.reorder",
297 commandId: CommandId.make("cmd-reorder-noop"),
298 threadId: ThreadId.make("thread-1"),
299 orderKey: "g",
300 },
301 readModel: makeReadModel({ pinnedAt: PINNED_AT, pinOrderKey: "g" }),
302 });
303 const events = Array.isArray(event) ? event : [event];
304 expect(events[0]?.type).toBe("thread.pin-reordered");
305 if (events[0]?.type === "thread.pin-reordered") {
306 expect(events[0].payload.updatedAt).toBe(NOW);
307 }
308 }),
309 );
310​
311 it.effect("rejects reordering an unpinned thread", () =>
312 Effect.gen(function* () {
313 const error = yield* decideOrchestrationCommand({
314 command: {
315 type: "thread.pin.reorder",
316 commandId: CommandId.make("cmd-reorder-unpinned"),
317 threadId: ThreadId.make("thread-1"),
318 orderKey: "m",
319 },
320 readModel: makeReadModel({}),
321 }).pipe(Effect.flip);
322 expect(error._tag).toBe("OrchestrationCommandInvariantError");
323 }),
324 );
325});