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