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

decider.snoozed.test.ts

287 lines10.0 KB

1import {
2 CommandId,
3 EventId,
4 MessageId,
5 ProjectId,
6 ProviderInstanceId,
7 ThreadId,
8 type OrchestrationReadModel,
9 type OrchestrationThread,
10} from "@t3tools/contracts";
11import * as NodeServices from "@effect/platform-node/NodeServices";
12import { expect, it } from "@effect/vitest";
13import * as Effect from "effect/Effect";
14​
15import { decideOrchestrationCommand } from "./decider.ts";
16​
17const NOW = "2026-01-01T00:00:00.000Z";
18// The decider's clock is the Effect test clock, pinned to the epoch, so
19// "future" wake times are relative to 1970-01-01T00:00:00.000Z.
20const FUTURE_WAKE = "1970-01-02T09:00:00.000Z";
21const PAST_WAKE = "1969-12-31T09:00:00.000Z";
22const SNOOZED_AT = "1969-12-30T00:00:00.000Z";
23​
24function makeReadModel(input: {
25 readonly snoozedUntil?: string | null;
26 readonly snoozedAt?: string | null;
27 readonly archivedAt?: string | null;
28 readonly activities?: OrchestrationThread["activities"];
29 readonly messages?: OrchestrationThread["messages"];
30}): OrchestrationReadModel {
31 return {
32 snapshotSequence: 0,
33 projects: [],
34 threads: [
35 {
36 id: ThreadId.make("thread-1"),
37 projectId: ProjectId.make("project-1"),
38 title: "Thread",
39 modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.4" },
40 runtimeMode: "full-access",
41 interactionMode: "default",
42 branch: null,
43 worktreePath: null,
44 pullRequests: [],
45 latestTurn: null,
46 createdAt: NOW,
47 updatedAt: NOW,
48 archivedAt: input.archivedAt ?? null,
49 settledOverride: null,
50 settledAt: null,
51 snoozedUntil: input.snoozedUntil ?? null,
52 snoozedAt: input.snoozedAt ?? (input.snoozedUntil != null ? SNOOZED_AT : null),
53 deletedAt: null,
54 messages: input.messages ?? [],
55 proposedPlans: [],
56 activities: input.activities ?? [],
57 checkpoints: [],
58 session: null,
59 },
60 ],
61 updatedAt: NOW,
62 };
64​
65it.layer(NodeServices.layer)("snoozed thread decider", (it) => {
66 it.effect("snoozes a thread to a future wake time", () =>
67 Effect.gen(function* () {
68 const event = yield* decideOrchestrationCommand({
69 command: {
70 type: "thread.snooze",
71 commandId: CommandId.make("cmd-snooze"),
72 threadId: ThreadId.make("thread-1"),
73 snoozedUntil: FUTURE_WAKE,
74 },
75 readModel: makeReadModel({}),
76 });
77 const events = Array.isArray(event) ? event : [event];
78 expect(events).toHaveLength(1);
79 expect(events[0]?.type).toBe("thread.snoozed");
80 if (events[0]?.type === "thread.snoozed") {
81 expect(events[0].payload.snoozedUntil).toBe(FUTURE_WAKE);
82 expect(events[0].payload.snoozedAt).toBe(events[0].payload.updatedAt);
83 }
84 }),
85 );
86​
87 it.effect("rejects a wake time that is not in the future", () =>
88 Effect.gen(function* () {
89 const error = yield* decideOrchestrationCommand({
90 command: {
91 type: "thread.snooze",
92 commandId: CommandId.make("cmd-snooze-past"),
93 threadId: ThreadId.make("thread-1"),
94 snoozedUntil: PAST_WAKE,
95 },
96 readModel: makeReadModel({}),
97 }).pipe(Effect.flip);
98 expect(error._tag).toBe("OrchestrationCommandInvariantError");
99 }),
100 );
101​
102 it.effect("rejects an unparseable wake time", () =>
103 Effect.gen(function* () {
104 // IsoDateTime is structurally a string, so garbage can reach the
105 // decider; a NaN wake time must never persist as snooze state.
106 const error = yield* decideOrchestrationCommand({
107 command: {
108 type: "thread.snooze",
109 commandId: CommandId.make("cmd-snooze-garbage"),
110 threadId: ThreadId.make("thread-1"),
111 snoozedUntil: "not-a-date",
112 },
113 readModel: makeReadModel({}),
114 }).pipe(Effect.flip);
115 expect(error._tag).toBe("OrchestrationCommandInvariantError");
116 }),
117 );
118​
119 it.effect("rejects snoozing blocked-on-you work", () =>
120 Effect.gen(function* () {
121 const requestActivity = {
122 id: EventId.make("activity-req-1"),
123 tone: "approval" as const,
124 kind: "approval.requested",
125 summary: "approval.requested",
126 payload: { requestId: "req-1" },
127 turnId: null,
128 createdAt: NOW,
129 } as OrchestrationThread["activities"][number];
130 const error = yield* decideOrchestrationCommand({
131 command: {
132 type: "thread.snooze",
133 commandId: CommandId.make("cmd-snooze-blocked"),
134 threadId: ThreadId.make("thread-1"),
135 snoozedUntil: FUTURE_WAKE,
136 },
137 readModel: makeReadModel({ activities: [requestActivity] }),
138 }).pipe(Effect.flip);
139 expect(error._tag).toBe("OrchestrationCommandInvariantError");
140 }),
141 );
142​
143 it.effect("re-emits idempotently for a duplicate snooze to the same wake time", () =>
144 Effect.gen(function* () {
145 const reEmit = yield* decideOrchestrationCommand({
146 command: {
147 type: "thread.snooze",
148 commandId: CommandId.make("cmd-snooze-again"),
149 threadId: ThreadId.make("thread-1"),
150 snoozedUntil: FUTURE_WAKE,
151 },
152 readModel: makeReadModel({ snoozedUntil: FUTURE_WAKE }),
153 });
154 const events = Array.isArray(reEmit) ? reEmit : [reEmit];
155 expect(events).toHaveLength(1);
156 if (events[0]?.type === "thread.snoozed") {
157 // Original snoozedAt preserved; updatedAt must not churn.
158 expect(events[0].payload.snoozedAt).toBe(SNOOZED_AT);
159 expect(events[0].payload.updatedAt).toBe(NOW);
160 }
161 }),
162 );
163​
164 it.effect("re-snoozing to a DIFFERENT wake time stamps fresh", () =>
165 Effect.gen(function* () {
166 const event = yield* decideOrchestrationCommand({
167 command: {
168 type: "thread.snooze",
169 commandId: CommandId.make("cmd-snooze-extend"),
170 threadId: ThreadId.make("thread-1"),
171 snoozedUntil: "1970-01-03T09:00:00.000Z",
172 },
173 readModel: makeReadModel({ snoozedUntil: FUTURE_WAKE }),
174 });
175 const events = Array.isArray(event) ? event : [event];
176 if (events[0]?.type === "thread.snoozed") {
177 expect(events[0].payload.snoozedUntil).toBe("1970-01-03T09:00:00.000Z");
178 expect(events[0].payload.updatedAt).not.toBe(NOW);
179 }
180 }),
181 );
182​
183 it.effect("unsnoozes with reason user and re-emits idempotently when awake", () =>
184 Effect.gen(function* () {
185 const event = yield* decideOrchestrationCommand({
186 command: {
187 type: "thread.unsnooze",
188 commandId: CommandId.make("cmd-unsnooze"),
189 threadId: ThreadId.make("thread-1"),
190 reason: "user",
191 },
192 readModel: makeReadModel({ snoozedUntil: FUTURE_WAKE }),
193 });
194 const events = Array.isArray(event) ? event : [event];
195 expect(events[0]?.type).toBe("thread.unsnoozed");
196 if (events[0]?.type === "thread.unsnoozed") {
197 expect(events[0].payload.reason).toBe("user");
198 expect(events[0].payload.updatedAt).not.toBe(NOW);
199 }
200​
201 const awake = yield* decideOrchestrationCommand({
202 command: {
203 type: "thread.unsnooze",
204 commandId: CommandId.make("cmd-unsnooze-awake"),
205 threadId: ThreadId.make("thread-1"),
206 reason: "user",
207 },
208 readModel: makeReadModel({}),
209 });
210 const awakeEvents = Array.isArray(awake) ? awake : [awake];
211 expect(awakeEvents[0]?.type).toBe("thread.unsnoozed");
212 if (awakeEvents[0]?.type === "thread.unsnoozed") {
213 // No state change — keep the existing updatedAt.
214 expect(awakeEvents[0].payload.updatedAt).toBe(NOW);
215 }
216 }),
217 );
218​
219 it.effect("rejects snoozing a thread with a queued turn start", () =>
220 Effect.gen(function* () {
221 // The decider clock is the Effect test clock pinned to the epoch: a
222 // user message 30s before it with no adopting turn is queued work.
223 const queuedMessage = {
224 id: MessageId.make("message-queued"),
225 role: "user",
226 text: "Continue",
227 turnId: null,
228 streaming: false,
229 createdAt: "1969-12-31T23:59:30.000Z",
230 updatedAt: "1969-12-31T23:59:30.000Z",
231 } as OrchestrationThread["messages"][number];
232 const error = yield* decideOrchestrationCommand({
233 command: {
234 type: "thread.snooze",
235 commandId: CommandId.make("cmd-snooze-queued"),
236 threadId: ThreadId.make("thread-1"),
237 snoozedUntil: FUTURE_WAKE,
238 },
239 readModel: makeReadModel({ messages: [queuedMessage] }),
240 }).pipe(Effect.flip);
241 expect(error._tag).toBe("OrchestrationCommandInvariantError");
242 }),
243 );
244​
245 it.effect("rejects snoozing an archived thread", () =>
246 Effect.gen(function* () {
247 const error = yield* decideOrchestrationCommand({
248 command: {
249 type: "thread.snooze",
250 commandId: CommandId.make("cmd-snooze-archived"),
251 threadId: ThreadId.make("thread-1"),
252 snoozedUntil: FUTURE_WAKE,
253 },
254 readModel: makeReadModel({ archivedAt: NOW }),
255 }).pipe(Effect.flip);
256 expect(error._tag).toBe("OrchestrationCommandInvariantError");
257 }),
258 );
259​
260 it.effect("a user message spends the snooze return ticket (activity wake)", () =>
261 Effect.gen(function* () {
262 const result = yield* decideOrchestrationCommand({
263 command: {
264 type: "thread.turn.start",
265 commandId: CommandId.make("cmd-turn-start"),
266 threadId: ThreadId.make("thread-1"),
267 message: {
268 messageId: MessageId.make("message-1"),
269 role: "user",
270 text: "Continue",
271 attachments: [],
272 },
273 runtimeMode: "full-access",
274 interactionMode: "default",
275 createdAt: NOW,
276 },
277 readModel: makeReadModel({ snoozedUntil: FUTURE_WAKE }),
278 });
279 const events = Array.isArray(result) ? result : [result];
280 const unsnoozed = events.find((entry) => entry.type === "thread.unsnoozed");
281 expect(unsnoozed).toBeDefined();
282 if (unsnoozed?.type === "thread.unsnoozed") {
283 expect(unsnoozed.payload.reason).toBe("activity");
284 }
285 }),
286 );
287});