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