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

threadSnoozed.test.ts

406 lines13.1 KB

1// @effect-diagnostics globalDate:off -- Tests exercise local calendar snooze boundaries.
2import { ThreadId } from "@t3tools/contracts";
3import { TurnId } from "@t3tools/contracts";
4import { describe, expect, it } from "vite-plus/test";
5​
6import {
7 canSnooze,
8 effectiveSnoozed,
9 hasQueuedTurnStart,
10 resolveSnoozePresets,
11 snoozeWakeLabel,
12 threadRaisedHandWhileSnoozed,
13 threadWokeAt,
14 type ThreadSnoozeShell,
15} from "./threadSettled.ts";
16​
17const NOW = "2026-04-10T12:00:00.000Z";
18const SNOOZED_AT = "2026-04-10T09:00:00.000Z";
19const FUTURE_WAKE = "2026-04-11T09:00:00.000Z";
20const PAST_WAKE = "2026-04-10T10:00:00.000Z";
21​
22function localDate(year: number, month: number, day: number, hour: number, minute = 0): Date {
23 return new Date(year, month - 1, day, hour, minute, 0, 0);
25​
26function makeShell(input: {
27 readonly snoozedUntil?: string | null;
28 readonly snoozedAt?: string | null;
29 readonly sessionStatus?: "starting" | "running" | "ready" | "error";
30 readonly pending?: "approval" | "user-input";
31 readonly turnCompletedAt?: string | null;
32}): ThreadSnoozeShell {
33 const threadId = ThreadId.make("thread-1");
34 return {
35 snoozedUntil: input.snoozedUntil ?? null,
36 snoozedAt: input.snoozedAt ?? (input.snoozedUntil != null ? SNOOZED_AT : null),
37 hasPendingApprovals: input.pending === "approval",
38 hasPendingUserInput: input.pending === "user-input",
39 session:
40 input.sessionStatus === undefined
41 ? null
42 : {
43 threadId,
44 status: input.sessionStatus,
45 providerName: "Codex",
46 runtimeMode: "full-access",
47 activeTurnId: null,
48 lastError: input.sessionStatus === "error" ? "boom" : null,
49 updatedAt: "2026-04-10T11:00:00.000Z",
50 },
51 latestTurn:
52 input.turnCompletedAt === undefined
53 ? null
54 : {
55 turnId: TurnId.make("turn-1"),
56 state: "completed",
57 requestedAt: SNOOZED_AT,
58 startedAt: null,
59 completedAt: input.turnCompletedAt,
60 assistantMessageId: null,
61 },
62 };
64​
65type QueuedTurnShell = Parameters<typeof hasQueuedTurnStart>[0];
66​
67function makeQueuedTurnShell(overrides: Partial<QueuedTurnShell> = {}): QueuedTurnShell {
68 return { latestUserMessageAt: null, latestTurn: null, session: null, ...overrides };
70​
71describe("effectiveSnoozed", () => {
72 it("hides a thread whose wake time is in the future", () => {
73 expect(effectiveSnoozed(makeShell({ snoozedUntil: FUTURE_WAKE }), { now: NOW })).toBe(true);
74 });
75​
76 it("stops classifying as snoozed once the wake time passes (timer wake, no event)", () => {
77 expect(effectiveSnoozed(makeShell({ snoozedUntil: PAST_WAKE }), { now: NOW })).toBe(false);
78 });
79​
80 it("never snoozes a thread with no snooze state", () => {
81 expect(effectiveSnoozed(makeShell({}), { now: NOW })).toBe(false);
82 });
83​
84 it("never hides on malformed wake data", () => {
85 expect(effectiveSnoozed(makeShell({ snoozedUntil: "not-a-date" }), { now: NOW })).toBe(false);
86 });
87​
88 it("wakes early when the agent is blocked on the user", () => {
89 expect(
90 effectiveSnoozed(makeShell({ snoozedUntil: FUTURE_WAKE, pending: "approval" }), {
91 now: NOW,
92 }),
93 ).toBe(false);
94 expect(
95 effectiveSnoozed(makeShell({ snoozedUntil: FUTURE_WAKE, pending: "user-input" }), {
96 now: NOW,
97 }),
98 ).toBe(false);
99 });
100​
101 it("wakes early on a failure that happened after the snooze", () => {
102 expect(
103 effectiveSnoozed(makeShell({ snoozedUntil: FUTURE_WAKE, sessionStatus: "error" }), {
104 now: NOW,
105 }),
106 ).toBe(false);
107 });
108​
109 it("stays snoozed when the failure predates the snooze — the user saw it", () => {
110 expect(
111 effectiveSnoozed(
112 makeShell({
113 snoozedUntil: FUTURE_WAKE,
114 sessionStatus: "error",
115 snoozedAt: "2026-04-10T11:30:00.000Z",
116 }),
117 { now: NOW },
118 ),
119 ).toBe(true);
120 });
121​
122 it.each([
123 {
124 reason: "stale failure",
125 completedAt: "2026-04-10T08:00:00.000Z",
126 pending: undefined,
127 wokeAt: null,
128 },
129 {
130 reason: "fresh failure",
131 completedAt: "2026-04-10T10:00:00.000Z",
132 pending: undefined,
133 wokeAt: "2026-04-10T10:00:00.000Z",
134 },
135 {
136 reason: "approval after a stale failure",
137 completedAt: "2026-04-10T08:00:00.000Z",
138 pending: "approval",
139 wokeAt: "2026-04-10T11:00:00.000Z",
140 },
141 {
142 reason: "input after a stale failure",
143 completedAt: "2026-04-10T08:00:00.000Z",
144 pending: "user-input",
145 wokeAt: "2026-04-10T11:00:00.000Z",
146 },
147 ] as const)(
148 "uses the wake event time for $reason despite newer metadata",
149 ({ completedAt, pending, wokeAt }) => {
150 const shell: ThreadSnoozeShell = {
151 ...makeShell({ snoozedUntil: FUTURE_WAKE }),
152 hasPendingApprovals: pending === "approval",
153 hasPendingUserInput: pending === "user-input",
154 runtime: { status: "failed", updatedAt: "2026-04-10T11:00:00.000Z" },
155 latestRun: { status: "failed", requestedAt: "2026-04-10T07:00:00.000Z", completedAt },
156 };
157 expect(effectiveSnoozed(shell, { now: NOW })).toBe(wokeAt === null);
158 expect(threadWokeAt(shell, { now: NOW })).toBe(wokeAt);
159 },
160 );
161​
162 it("stays snoozed while the session keeps working — snooze never pauses the agent", () => {
163 expect(
164 effectiveSnoozed(makeShell({ snoozedUntil: FUTURE_WAKE, sessionStatus: "running" }), {
165 now: NOW,
166 }),
167 ).toBe(true);
168 });
169​
170 it("wakes early when a run completes after the snooze was set", () => {
171 expect(
172 effectiveSnoozed(
173 makeShell({ snoozedUntil: FUTURE_WAKE, turnCompletedAt: "2026-04-10T10:30:00.000Z" }),
174 { now: NOW },
175 ),
176 ).toBe(false);
177 });
178​
179 it("ignores runs that completed before the snooze — the user saw that result", () => {
180 expect(
181 effectiveSnoozed(
182 makeShell({ snoozedUntil: FUTURE_WAKE, turnCompletedAt: "2026-04-10T08:00:00.000Z" }),
183 { now: NOW },
184 ),
185 ).toBe(true);
186 });
187});
188​
189describe("threadRaisedHandWhileSnoozed", () => {
190 it("is false for a quiet snoozed thread", () => {
191 expect(threadRaisedHandWhileSnoozed(makeShell({ snoozedUntil: FUTURE_WAKE }))).toBe(false);
192 });
193​
194 it("is true for approvals, input, and failures", () => {
195 expect(
196 threadRaisedHandWhileSnoozed(makeShell({ snoozedUntil: FUTURE_WAKE, pending: "approval" })),
197 ).toBe(true);
198 expect(
199 threadRaisedHandWhileSnoozed(makeShell({ snoozedUntil: FUTURE_WAKE, pending: "user-input" })),
200 ).toBe(true);
201 expect(
202 threadRaisedHandWhileSnoozed(
203 makeShell({ snoozedUntil: FUTURE_WAKE, sessionStatus: "error" }),
204 ),
205 ).toBe(true);
206 });
207});
208​
209describe("canSnooze", () => {
210 it("allows snoozing quiet and working threads alike", () => {
211 expect(canSnooze({ ...makeShell({}), latestUserMessageAt: null }, { now: NOW })).toBe(true);
212 expect(
213 canSnooze(
214 { ...makeShell({ sessionStatus: "running" }), latestUserMessageAt: null },
215 { now: NOW },
216 ),
217 ).toBe(true);
218 });
219​
220 it("refuses blocked-on-you work", () => {
221 expect(
222 canSnooze({ ...makeShell({ pending: "approval" }), latestUserMessageAt: null }, { now: NOW }),
223 ).toBe(false);
224 expect(
225 canSnooze(
226 { ...makeShell({ pending: "user-input" }), latestUserMessageAt: null },
227 { now: NOW },
228 ),
229 ).toBe(false);
230 expect(
231 canSnooze(
232 { ...makeShell({}), latestUserMessageAt: null, pendingSubagentRequest: "approval" },
233 { now: NOW },
234 ),
235 ).toBe(false);
236 });
237​
238 it("refuses a queued turn start — same invisible-pending-work rule as settle", () => {
239 expect(
240 canSnooze(
241 { ...makeShell({}), latestUserMessageAt: "2026-04-10T11:59:30.000Z" },
242 { now: NOW },
243 ),
244 ).toBe(false);
245 expect(
246 canSnooze(
247 { ...makeShell({}), latestUserMessageAt: "2026-04-10T11:00:00.000Z" },
248 { now: NOW },
249 ),
250 ).toBe(true);
251 });
252});
253​
254describe("hasQueuedTurnStart", () => {
255 it("expires queued state after two minutes", () => {
256 const thread = makeQueuedTurnShell({
257 latestUserMessageAt: "2026-04-10T11:57:59.000Z",
258 });
259 expect(hasQueuedTurnStart(thread, { now: NOW })).toBe(false);
260 });
261​
262 it("clears queued state when a turn adopts the message or the session fails", () => {
263 const messageAt = "2026-04-10T11:59:00.000Z";
264 const adopted = makeQueuedTurnShell({
265 latestUserMessageAt: messageAt,
266 latestTurn: {
267 turnId: TurnId.make("turn-adopted"),
268 state: "running",
269 requestedAt: messageAt,
270 startedAt: null,
271 completedAt: null,
272 assistantMessageId: null,
273 },
274 });
275 const failed = makeQueuedTurnShell({
276 latestUserMessageAt: messageAt,
277 session: {
278 threadId: ThreadId.make("thread-failed"),
279 status: "error",
280 providerName: "Codex",
281 runtimeMode: "full-access",
282 activeTurnId: null,
283 lastError: "failed",
284 updatedAt: NOW,
285 },
286 });
287 expect(hasQueuedTurnStart(adopted, { now: NOW })).toBe(false);
288 expect(hasQueuedTurnStart(failed, { now: NOW })).toBe(false);
289 });
290​
291 it("bounds future client clock skew", () => {
292 const farAhead = makeQueuedTurnShell({
293 latestUserMessageAt: "2026-04-10T12:03:00.000Z",
294 });
295 const slightlyAhead = makeQueuedTurnShell({
296 latestUserMessageAt: "2026-04-10T12:01:00.000Z",
297 });
298 expect(hasQueuedTurnStart(farAhead, { now: NOW })).toBe(false);
299 expect(hasQueuedTurnStart(slightlyAhead, { now: NOW })).toBe(true);
300 });
301});
302​
303describe("threadWokeAt", () => {
304 it("is null for never-snoozed and still-snoozed threads", () => {
305 expect(threadWokeAt(makeShell({}), { now: NOW })).toBe(null);
306 expect(threadWokeAt(makeShell({ snoozedUntil: FUTURE_WAKE }), { now: NOW })).toBe(null);
307 });
308​
309 it("reports the wake time for a timer wake", () => {
310 expect(threadWokeAt(makeShell({ snoozedUntil: PAST_WAKE }), { now: NOW })).toBe(PAST_WAKE);
311 });
312​
313 it("reports the completion time for an early run-completed wake", () => {
314 expect(
315 threadWokeAt(
316 makeShell({ snoozedUntil: FUTURE_WAKE, turnCompletedAt: "2026-04-10T10:30:00.000Z" }),
317 { now: NOW },
318 ),
319 ).toBe("2026-04-10T10:30:00.000Z");
320 });
321​
322 it("falls back to session activity for blocked/failed early wakes", () => {
323 expect(
324 threadWokeAt(makeShell({ snoozedUntil: FUTURE_WAKE, sessionStatus: "error" }), {
325 now: NOW,
326 }),
327 ).toBe("2026-04-10T11:00:00.000Z");
328 });
329​
330 it("keeps the early wake authoritative after the scheduled time passes", () => {
331 expect(
332 threadWokeAt(
333 makeShell({ snoozedUntil: PAST_WAKE, turnCompletedAt: "2026-04-10T09:30:00.000Z" }),
334 { now: NOW },
335 ),
336 ).toBe("2026-04-10T09:30:00.000Z");
337 });
338});
339​
340describe("snoozeWakeLabel", () => {
341 const now = "2026-06-02T00:00:00.000Z";
342​
343 it("formats remaining time coarsely, rounding up", () => {
344 expect(snoozeWakeLabel("2026-06-02T00:30:00.000Z", { now })).toBe("30m");
345 expect(snoozeWakeLabel("2026-06-02T01:30:00.000Z", { now })).toBe("2h");
346 expect(snoozeWakeLabel("2026-06-03T02:00:00.000Z", { now })).toBe("2d");
347 });
348​
349 it("never reads zero or negative while still snoozed", () => {
350 expect(snoozeWakeLabel("2026-06-02T00:00:30.000Z", { now })).toBe("1m");
351 expect(snoozeWakeLabel("2026-06-01T23:59:59.000Z", { now })).toBe("now");
352 expect(snoozeWakeLabel("not-a-date", { now })).toBe("now");
353 expect(snoozeWakeLabel("2026-06-02T09:00:00.000Z", { now: "bad" })).toBe("now");
354 });
355});
356​
357describe("resolveSnoozePresets", () => {
358 it("offers the shared desktop and mobile choices", () => {
359 const presets = resolveSnoozePresets(localDate(2026, 4, 8, 10));
360 expect(presets.map((preset) => preset.id)).toEqual([
361 "hour",
362 "three-hours",
363 "evening",
364 "tomorrow",
365 "next-week",
366 ]);
367 expect(presets.find((preset) => preset.id === "three-hours")?.snoozedUntil).toBe(
368 localDate(2026, 4, 8, 13).toISOString(),
369 );
370 expect(presets.find((preset) => preset.id === "three-hours")?.label).toBe("In 3 hours");
371 expect(presets.find((preset) => preset.id === "evening")?.label).toBe("This evening");
372 expect(
373 new Date(presets.find((preset) => preset.id === "tomorrow")!.snoozedUntil).getHours(),
374 ).toBe(9);
375 });
376​
377 it("drops the evening choice once evening is near or past", () => {
378 expect(resolveSnoozePresets(localDate(2026, 4, 8, 17, 30)).map((preset) => preset.id)).toEqual([
379 "hour",
380 "three-hours",
381 "tomorrow",
382 "next-week",
383 ]);
384 });
385​
386 it("puts next week on the following Monday", () => {
387 const nextWeek = new Date(
388 resolveSnoozePresets(localDate(2026, 4, 6, 10)).find((preset) => preset.id === "next-week")!
389 .snoozedUntil,
390 );
391 expect(nextWeek.getDay()).toBe(1);
392 expect(nextWeek.getDate()).toBe(13);
393 });
394​
395 it("drops next week on Sundays, when it lands on the same Monday as tomorrow", () => {
396 const presets = resolveSnoozePresets(localDate(2026, 8, 30, 7, 1));
397 expect(presets.map((preset) => preset.id)).toEqual([
398 "hour",
399 "three-hours",
400 "evening",
401 "tomorrow",
402 ]);
403 const tomorrow = new Date(presets.find((preset) => preset.id === "tomorrow")!.snoozedUntil);
404 expect(tomorrow.getDay()).toBe(1);
405 });
406});