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 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 };
63}
64
65type QueuedTurnShell = Parameters<typeof hasQueuedTurnStart>[0];
66
67function makeQueuedTurnShell(overrides: Partial<QueuedTurnShell> = {}): QueuedTurnShell {
68 return { latestUserMessageAt: null, latestTurn: null, session: null, ...overrides };
69}
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 // makeShell stamps session.updatedAt at 11:00, after SNOOZED_AT (9:00).
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 // Snoozed AFTER the error's status edge.
117 snoozedAt: "2026-04-10T11:30:00.000Z",
118 }),
119 { now: NOW },
120 ),
121 ).toBe(true);
122 });
123
124 it("stays snoozed while the session keeps working — snooze never pauses the agent", () => {
125 expect(
126 effectiveSnoozed(makeShell({ snoozedUntil: FUTURE_WAKE, sessionStatus: "running" }), {
127 now: NOW,
128 }),
129 ).toBe(true);
130 });
131
132 it("wakes early when a run completes after the snooze was set", () => {
133 expect(
134 effectiveSnoozed(
135 makeShell({ snoozedUntil: FUTURE_WAKE, turnCompletedAt: "2026-04-10T10:30:00.000Z" }),
136 { now: NOW },
137 ),
138 ).toBe(false);
139 });
140
141 it("ignores runs that completed before the snooze — the user saw that result", () => {
142 expect(
143 effectiveSnoozed(
144 makeShell({ snoozedUntil: FUTURE_WAKE, turnCompletedAt: "2026-04-10T08:00:00.000Z" }),
145 { now: NOW },
146 ),
147 ).toBe(true);
148 });
149});
150
151describe("threadRaisedHandWhileSnoozed", () => {
152 it("is false for a quiet snoozed thread", () => {
153 expect(threadRaisedHandWhileSnoozed(makeShell({ snoozedUntil: FUTURE_WAKE }))).toBe(false);
154 });
155
156 it("is true for approvals, input, and failures", () => {
157 expect(
158 threadRaisedHandWhileSnoozed(makeShell({ snoozedUntil: FUTURE_WAKE, pending: "approval" })),
159 ).toBe(true);
160 expect(
161 threadRaisedHandWhileSnoozed(makeShell({ snoozedUntil: FUTURE_WAKE, pending: "user-input" })),
162 ).toBe(true);
163 expect(
164 threadRaisedHandWhileSnoozed(
165 makeShell({ snoozedUntil: FUTURE_WAKE, sessionStatus: "error" }),
166 ),
167 ).toBe(true);
168 });
169});
170
171describe("canSnooze", () => {
172 it("allows snoozing quiet and working threads alike", () => {
173 expect(canSnooze({ ...makeShell({}), latestUserMessageAt: null }, { now: NOW })).toBe(true);
174 expect(
175 canSnooze(
176 { ...makeShell({ sessionStatus: "running" }), latestUserMessageAt: null },
177 { now: NOW },
178 ),
179 ).toBe(true);
180 });
181
182 it("refuses blocked-on-you work", () => {
183 expect(
184 canSnooze({ ...makeShell({ pending: "approval" }), latestUserMessageAt: null }, { now: NOW }),
185 ).toBe(false);
186 expect(
187 canSnooze(
188 { ...makeShell({ pending: "user-input" }), latestUserMessageAt: null },
189 { now: NOW },
190 ),
191 ).toBe(false);
192 });
193
194 it("refuses a queued turn start — same invisible-pending-work rule as settle", () => {
195 // Fresh user message, no turn has adopted it, within the grace window.
196 expect(
197 canSnooze(
198 { ...makeShell({}), latestUserMessageAt: "2026-04-10T11:59:30.000Z" },
199 { now: NOW },
200 ),
201 ).toBe(false);
202 // Outside the grace window the message is stale data, not queued work.
203 expect(
204 canSnooze(
205 { ...makeShell({}), latestUserMessageAt: "2026-04-10T11:00:00.000Z" },
206 { now: NOW },
207 ),
208 ).toBe(true);
209 });
210});
211
212describe("hasQueuedTurnStart", () => {
213 it("expires queued state after two minutes", () => {
214 const thread = makeQueuedTurnShell({
215 latestUserMessageAt: "2026-04-10T11:57:59.000Z",
216 });
217 expect(hasQueuedTurnStart(thread, { now: NOW })).toBe(false);
218 });
219
220 it("clears queued state when a turn adopts the message or the session fails", () => {
221 const messageAt = "2026-04-10T11:59:00.000Z";
222 const adopted = makeQueuedTurnShell({
223 latestUserMessageAt: messageAt,
224 latestTurn: {
225 turnId: TurnId.make("turn-adopted"),
226 state: "running",
227 requestedAt: messageAt,
228 startedAt: null,
229 completedAt: null,
230 assistantMessageId: null,
231 },
232 });
233 const failed = makeQueuedTurnShell({
234 latestUserMessageAt: messageAt,
235 session: {
236 threadId: ThreadId.make("thread-failed"),
237 status: "error",
238 providerName: "Codex",
239 runtimeMode: "full-access",
240 activeTurnId: null,
241 lastError: "failed",
242 updatedAt: NOW,
243 },
244 });
245 expect(hasQueuedTurnStart(adopted, { now: NOW })).toBe(false);
246 expect(hasQueuedTurnStart(failed, { now: NOW })).toBe(false);
247 });
248
249 it("bounds future client clock skew", () => {
250 const farAhead = makeQueuedTurnShell({
251 latestUserMessageAt: "2026-04-10T12:03:00.000Z",
252 });
253 const slightlyAhead = makeQueuedTurnShell({
254 latestUserMessageAt: "2026-04-10T12:01:00.000Z",
255 });
256 expect(hasQueuedTurnStart(farAhead, { now: NOW })).toBe(false);
257 expect(hasQueuedTurnStart(slightlyAhead, { now: NOW })).toBe(true);
258 });
259});
260
261describe("threadWokeAt", () => {
262 it("is null for never-snoozed and still-snoozed threads", () => {
263 expect(threadWokeAt(makeShell({}), { now: NOW })).toBe(null);
264 expect(threadWokeAt(makeShell({ snoozedUntil: FUTURE_WAKE }), { now: NOW })).toBe(null);
265 });
266
267 it("reports the wake time for a timer wake", () => {
268 expect(threadWokeAt(makeShell({ snoozedUntil: PAST_WAKE }), { now: NOW })).toBe(PAST_WAKE);
269 });
270
271 it("reports the completion time for an early run-completed wake", () => {
272 expect(
273 threadWokeAt(
274 makeShell({ snoozedUntil: FUTURE_WAKE, turnCompletedAt: "2026-04-10T10:30:00.000Z" }),
275 { now: NOW },
276 ),
277 ).toBe("2026-04-10T10:30:00.000Z");
278 });
279
280 it("falls back to session activity for blocked/failed early wakes", () => {
281 expect(
282 threadWokeAt(makeShell({ snoozedUntil: FUTURE_WAKE, sessionStatus: "error" }), {
283 now: NOW,
284 }),
285 ).toBe("2026-04-10T11:00:00.000Z");
286 });
287
288 it("keeps the early wake authoritative after the scheduled time passes", () => {
289 // Woke early at 10:30 via run-completed; the scheduled wake (PAST_WAKE
290 // 10:00 relative to a later now) has ALSO passed. Reporting the
291 // scheduled time would resurface a Woke pill the user already cleared
292 // by visiting between the early wake and now.
293 expect(
294 threadWokeAt(
295 makeShell({ snoozedUntil: PAST_WAKE, turnCompletedAt: "2026-04-10T09:30:00.000Z" }),
296 { now: NOW },
297 ),
298 ).toBe("2026-04-10T09:30:00.000Z");
299 });
300});
301
302describe("snoozeWakeLabel", () => {
303 const now = "2026-06-02T00:00:00.000Z";
304
305 it("formats remaining time coarsely, rounding up", () => {
306 expect(snoozeWakeLabel("2026-06-02T00:30:00.000Z", { now })).toBe("30m");
307 expect(snoozeWakeLabel("2026-06-02T01:30:00.000Z", { now })).toBe("2h");
308 expect(snoozeWakeLabel("2026-06-03T02:00:00.000Z", { now })).toBe("2d");
309 });
310
311 it("never reads zero or negative while still snoozed", () => {
312 expect(snoozeWakeLabel("2026-06-02T00:00:30.000Z", { now })).toBe("1m");
313 expect(snoozeWakeLabel("2026-06-01T23:59:59.000Z", { now })).toBe("now");
314 expect(snoozeWakeLabel("not-a-date", { now })).toBe("now");
315 expect(snoozeWakeLabel("2026-06-02T09:00:00.000Z", { now: "bad" })).toBe("now");
316 });
317});
318
319describe("resolveSnoozePresets", () => {
320 it("offers the shared desktop and mobile choices", () => {
321 const presets = resolveSnoozePresets(localDate(2026, 4, 8, 10));
322 expect(presets.map((preset) => preset.id)).toEqual([
323 "hour",
324 "three-hours",
325 "evening",
326 "tomorrow",
327 "next-week",
328 ]);
329 expect(presets.find((preset) => preset.id === "three-hours")?.snoozedUntil).toBe(
330 localDate(2026, 4, 8, 13).toISOString(),
331 );
332 expect(presets.find((preset) => preset.id === "three-hours")?.label).toBe("In 3 hours");
333 expect(presets.find((preset) => preset.id === "evening")?.label).toBe("This evening");
334 expect(
335 new Date(presets.find((preset) => preset.id === "tomorrow")!.snoozedUntil).getHours(),
336 ).toBe(9);
337 });
338
339 it("drops the evening choice once evening is near or past", () => {
340 expect(resolveSnoozePresets(localDate(2026, 4, 8, 17, 30)).map((preset) => preset.id)).toEqual([
341 "hour",
342 "three-hours",
343 "tomorrow",
344 "next-week",
345 ]);
346 });
347
348 it("puts next week on the following Monday", () => {
349 const nextWeek = new Date(
350 resolveSnoozePresets(localDate(2026, 4, 6, 10)).find((preset) => preset.id === "next-week")!
351 .snoozedUntil,
352 );
353 expect(nextWeek.getDay()).toBe(1);
354 expect(nextWeek.getDate()).toBe(13);
355 });
356
357 it("drops next week on Sundays, when it lands on the same Monday as tomorrow", () => {
358 // Sunday 2026-08-30 07:01: "Tomorrow" and "Next week" are both Monday 9:00.
359 const presets = resolveSnoozePresets(localDate(2026, 8, 30, 7, 1));
360 expect(presets.map((preset) => preset.id)).toEqual([
361 "hour",
362 "three-hours",
363 "evening",
364 "tomorrow",
365 ]);
366 const tomorrow = new Date(presets.find((preset) => preset.id === "tomorrow")!.snoozedUntil);
367 expect(tomorrow.getDay()).toBe(1);
368 });
369});