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 type { SnoozePresetSetting } from "@t3tools/contracts/settings";
5import { describe, expect, it } from "vite-plus/test";
6
7import {
8 canSnooze,
9 effectiveSnoozed,
10 hasQueuedTurnStart,
11 pickSnoozePreset,
12 resolveSnoozePresets,
13 threadRaisedHandWhileSnoozed,
14 threadWokeAt,
15 isThreadWokeUnseen,
16 type ThreadSnoozeShell,
17} from "./threadSettled.ts";
18
19const NOW = "2026-04-10T12:00:00.000Z";
20const SNOOZED_AT = "2026-04-10T09:00:00.000Z";
21const FUTURE_WAKE = "2026-04-11T09:00:00.000Z";
22const PAST_WAKE = "2026-04-10T10:00:00.000Z";
23
24function localDate(year: number, month: number, day: number, hour: number, minute = 0): Date {
25 return new Date(year, month - 1, day, hour, minute, 0, 0);
26}
27
28function makeShell(input: {
29 readonly snoozedUntil?: string | null;
30 readonly snoozedAt?: string | null;
31 readonly sessionStatus?: "starting" | "running" | "ready" | "error";
32 readonly pending?: "approval" | "user-input";
33 readonly turnRequestedAt?: string | null;
34 readonly turnCompletedAt?: string | null;
35}): ThreadSnoozeShell {
36 const threadId = ThreadId.make("thread-1");
37 return {
38 snoozedUntil: input.snoozedUntil ?? null,
39 snoozedAt: input.snoozedAt ?? (input.snoozedUntil != null ? SNOOZED_AT : null),
40 hasPendingApprovals: input.pending === "approval",
41 hasPendingUserInput: input.pending === "user-input",
42 session:
43 input.sessionStatus === undefined
44 ? null
45 : {
46 threadId,
47 status: input.sessionStatus,
48 providerName: "Codex",
49 runtimeMode: "full-access",
50 activeTurnId: null,
51 lastError: input.sessionStatus === "error" ? "boom" : null,
52 updatedAt: "2026-04-10T11:00:00.000Z",
53 },
54 latestTurn:
55 input.turnCompletedAt === undefined
56 ? null
57 : {
58 turnId: TurnId.make("turn-1"),
59 state: "completed",
60 requestedAt: input.turnRequestedAt === undefined ? SNOOZED_AT : input.turnRequestedAt,
61 startedAt: null,
62 completedAt: input.turnCompletedAt,
63 assistantMessageId: null,
64 },
65 };
66}
67
68type QueuedTurnShell = Parameters<typeof hasQueuedTurnStart>[0];
69
70function makeQueuedTurnShell(overrides: Partial<QueuedTurnShell> = {}): QueuedTurnShell {
71 return { latestUserMessageAt: null, latestTurn: null, session: null, ...overrides };
72}
73
74describe("effectiveSnoozed", () => {
75 it("never hides on malformed wake data", () => {
76 expect(effectiveSnoozed(makeShell({ snoozedUntil: "not-a-date" }), { now: NOW })).toBe(false);
77 });
78
79 it("wakes early when the agent is blocked on the user", () => {
80 expect(
81 effectiveSnoozed(makeShell({ snoozedUntil: FUTURE_WAKE, pending: "approval" }), {
82 now: NOW,
83 }),
84 ).toBe(false);
85 expect(
86 effectiveSnoozed(makeShell({ snoozedUntil: FUTURE_WAKE, pending: "user-input" }), {
87 now: NOW,
88 }),
89 ).toBe(false);
90 });
91
92 it.each([
93 {
94 reason: "stale failure",
95 completedAt: "2026-04-10T08:00:00.000Z",
96 pending: undefined,
97 wokeAt: null,
98 },
99 {
100 reason: "failure without a completion timestamp",
101 completedAt: null,
102 pending: undefined,
103 wokeAt: null,
104 },
105 {
106 reason: "approval without a failure completion timestamp",
107 completedAt: null,
108 pending: "approval",
109 wokeAt: "2026-04-10T11:00:00.000Z",
110 },
111 {
112 reason: "fresh failure",
113 completedAt: "2026-04-10T10:00:00.000Z",
114 pending: undefined,
115 wokeAt: "2026-04-10T10:00:00.000Z",
116 },
117 {
118 reason: "approval after a stale failure",
119 completedAt: "2026-04-10T08:00:00.000Z",
120 pending: "approval",
121 wokeAt: "2026-04-10T11:00:00.000Z",
122 },
123 {
124 reason: "input after a stale failure",
125 completedAt: "2026-04-10T08:00:00.000Z",
126 pending: "user-input",
127 wokeAt: "2026-04-10T11:00:00.000Z",
128 },
129 ] as const)(
130 "uses the wake event time for $reason despite newer metadata",
131 ({ completedAt, pending, wokeAt }) => {
132 const shell: ThreadSnoozeShell = {
133 ...makeShell({ snoozedUntil: FUTURE_WAKE }),
134 hasPendingApprovals: pending === "approval",
135 hasPendingUserInput: pending === "user-input",
136 runtime: { status: "failed", updatedAt: "2026-04-10T11:00:00.000Z" },
137 latestRun: { status: "failed", requestedAt: "2026-04-10T07:00:00.000Z", completedAt },
138 };
139 expect(effectiveSnoozed(shell, { now: NOW })).toBe(wokeAt === null);
140 expect(threadWokeAt(shell, { now: NOW })).toBe(wokeAt);
141 },
142 );
143
144 it("stays snoozed while the session keeps working — snooze never pauses the agent", () => {
145 expect(
146 effectiveSnoozed(makeShell({ snoozedUntil: FUTURE_WAKE, sessionStatus: "running" }), {
147 now: NOW,
148 }),
149 ).toBe(true);
150 });
151
152 it("stays snoozed when a run requested after the snooze completes", () => {
153 const shell = {
154 ...makeShell({ snoozedUntil: FUTURE_WAKE }),
155 latestRun: {
156 status: "completed",
157 requestedAt: "2026-04-10T09:00:00.001Z",
158 startedAt: "2026-04-10T09:15:00.000Z",
159 completedAt: "2026-04-10T10:30:00.000Z",
160 },
161 };
162
163 expect(effectiveSnoozed(shell, { now: NOW })).toBe(true);
164 expect(threadWokeAt(shell, { now: NOW })).toBe(null);
165 });
166});
167
168describe("threadRaisedHandWhileSnoozed", () => {
169 it("is true for approvals, input, and failures", () => {
170 expect(
171 threadRaisedHandWhileSnoozed(makeShell({ snoozedUntil: FUTURE_WAKE, pending: "approval" })),
172 ).toBe(true);
173 expect(
174 threadRaisedHandWhileSnoozed(makeShell({ snoozedUntil: FUTURE_WAKE, pending: "user-input" })),
175 ).toBe(true);
176 expect(
177 threadRaisedHandWhileSnoozed(
178 makeShell({ snoozedUntil: FUTURE_WAKE, sessionStatus: "error" }),
179 ),
180 ).toBe(true);
181 });
182});
183
184describe("canSnooze", () => {
185 it("refuses blocked-on-you work", () => {
186 expect(
187 canSnooze({ ...makeShell({ pending: "approval" }), latestUserMessageAt: null }, { now: NOW }),
188 ).toBe(false);
189 expect(
190 canSnooze(
191 { ...makeShell({ pending: "user-input" }), latestUserMessageAt: null },
192 { now: NOW },
193 ),
194 ).toBe(false);
195 expect(
196 canSnooze(
197 { ...makeShell({}), latestUserMessageAt: null, pendingSubagentRequest: "approval" },
198 { now: NOW },
199 ),
200 ).toBe(false);
201 });
202
203 it("refuses a queued turn start — same invisible-pending-work rule as settle", () => {
204 expect(
205 canSnooze(
206 { ...makeShell({}), latestUserMessageAt: "2026-04-10T11:59:30.000Z" },
207 { now: NOW },
208 ),
209 ).toBe(false);
210 expect(
211 canSnooze(
212 { ...makeShell({}), latestUserMessageAt: "2026-04-10T11:00:00.000Z" },
213 { now: NOW },
214 ),
215 ).toBe(true);
216 });
217});
218
219describe("hasQueuedTurnStart", () => {
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
250describe("threadWokeAt", () => {
251 it("reports the timer wake even after an already-requested run completes", () => {
252 expect(threadWokeAt(makeShell({ snoozedUntil: PAST_WAKE }), { now: NOW })).toBe(PAST_WAKE);
253 const shell = makeShell({
254 snoozedUntil: PAST_WAKE,
255 turnCompletedAt: "2026-04-10T09:30:00.000Z",
256 });
257 expect(effectiveSnoozed(shell, { now: NOW })).toBe(false);
258 expect(threadWokeAt(shell, { now: NOW })).toBe(PAST_WAKE);
259 });
260
261 it.each([
262 { reason: "failure", sessionStatus: "error" },
263 { reason: "approval", sessionStatus: "ready", pending: "approval" },
264 { reason: "user input", sessionStatus: "ready", pending: "user-input" },
265 ] as const)("reports $reason activity instead of an already-requested completion", (activity) => {
266 const shell = makeShell({
267 ...activity,
268 snoozedUntil: FUTURE_WAKE,
269 turnCompletedAt: "2026-04-10T10:30:00.000Z",
270 });
271 expect(effectiveSnoozed(shell, { now: NOW })).toBe(false);
272 expect(threadWokeAt(shell, { now: NOW })).toBe("2026-04-10T11:00:00.000Z");
273 });
274
275 it("keeps the early wake authoritative after the scheduled time passes", () => {
276 const shell: ThreadSnoozeShell = {
277 ...makeShell({ snoozedUntil: PAST_WAKE }),
278 runtime: { status: "failed", updatedAt: "2026-04-10T11:00:00.000Z" },
279 latestRun: {
280 status: "failed",
281 requestedAt: "2026-04-10T09:15:00.000Z",
282 completedAt: "2026-04-10T09:30:00.000Z",
283 },
284 };
285 expect(threadWokeAt(shell, { now: NOW })).toBe("2026-04-10T09:30:00.000Z");
286 });
287});
288
289describe("resolveSnoozePresets", () => {
290 const formatTime = (date: Date) =>
291 `${date.getHours()}:${String(date.getMinutes()).padStart(2, "0")}`;
292 const menu = (now: Date, presets?: ReadonlyArray<SnoozePresetSetting>) =>
293 resolveSnoozePresets(now, { formatTime, ...(presets ? { presets } : {}) }).map(
294 (preset) => `${preset.label} ${preset.whenLabel}`,
295 );
296
297 it("lists custom presets soonest first and names the day when the label does not", () => {
298 // Wednesday 20:00.
299 expect(
300 menu(localDate(2026, 4, 8, 20), [
301 { kind: "at", day: "monday", time: "18:00" },
302 { kind: "at", day: "tomorrow", time: "21:00" },
303 { kind: "after", minutes: 360 },
304 { kind: "after", minutes: 180 },
305 ]),
306 ).toEqual(["In 3 hours 23:00", "In 6 hours Thu 2:00", "Tomorrow 21:00", "Next week Mon 18:00"]);
307 });
308
309 it("drops a today preset once its time passes and moves a weekday on that day to next week", () => {
310 // Monday 18:30.
311 const now = localDate(2026, 4, 13, 18, 30);
312 const presets = resolveSnoozePresets(now, {
313 presets: [
314 { kind: "at", day: "today", time: "18:00" },
315 { kind: "at", day: "monday", time: "09:00" },
316 ],
317 });
318 expect(presets.map((preset) => preset.id)).toEqual(["at:monday:09:00"]);
319 expect(new Date(presets[0]!.snoozedUntil)).toEqual(localDate(2026, 4, 20, 9));
320 });
321
322 it("names the real day gap: a Monday option on Sunday is Tomorrow, a weekday a week out is Next", () => {
323 const theo: ReadonlyArray<SnoozePresetSetting> = [
324 { kind: "at", day: "tomorrow", time: "21:00" },
325 { kind: "at", day: "monday", time: "18:00" },
326 { kind: "at", day: "friday", time: "17:00" },
327 ];
328 // Sunday 14:00.
329 expect(menu(localDate(2026, 8, 30, 14), theo)).toEqual([
330 "Tomorrow 18:00",
331 "Tomorrow 21:00",
332 "Friday 17:00",
333 ]);
334 // Friday 10:00: today's 17:00 is a week away.
335 expect(menu(localDate(2026, 9, 4, 10), theo)).toEqual([
336 "Tomorrow 21:00",
337 "Next week Mon 18:00",
338 "Next Friday 17:00",
339 ]);
340 });
341
342 it("shows one row when two presets wake at the same time", () => {
343 // Sunday, so Tomorrow 9:00 and Next week 9:00 are the same Monday.
344 expect(menu(localDate(2026, 8, 30, 7, 1))).toEqual([
345 "In 1 hour 8:01",
346 "In 3 hours 10:01",
347 "This evening 18:00",
348 "Tomorrow 9:00",
349 ]);
350 });
351
352 it("treats a duration and a set time in the same minute as one row", () => {
353 const now = localDate(2026, 4, 8, 15);
354 now.setSeconds(25);
355 expect(
356 menu(now, [
357 { kind: "after", minutes: 180 },
358 { kind: "at", day: "today", time: "18:00" },
359 ]),
360 ).toEqual(["In 3 hours 18:00"]);
361 });
362});
363
364describe("pickSnoozePreset", () => {
365 it("starts a duration when it is picked, even when the menu merged it with a set time", () => {
366 const presets: ReadonlyArray<SnoozePresetSetting> = [
367 { kind: "at", day: "today", time: "18:30" },
368 { kind: "after", minutes: 180 },
369 ];
370 // At 15:00 the two differ; at 15:30 "In 3 hours" lands on 18:30 and the menu shows one row.
371 const shown = resolveSnoozePresets(localDate(2026, 4, 8, 15), { presets });
372 const picked = pickSnoozePreset({
373 shown,
374 id: "after:180",
375 now: localDate(2026, 4, 8, 15, 30),
376 presets,
377 });
378 expect(picked?.snoozedUntil).toBe(localDate(2026, 4, 8, 18, 30).toISOString());
379 });
380
381 it("keeps a set time's shown wake when the day changes while the menu is open", () => {
382 // Sunday 23:59: "Tomorrow" means Monday 9:00, even if the pick lands after midnight.
383 const shown = resolveSnoozePresets(localDate(2026, 8, 30, 23, 59));
384 const picked = pickSnoozePreset({
385 shown,
386 id: "at:tomorrow:09:00",
387 now: localDate(2026, 8, 31, 0, 1),
388 });
389 expect(picked?.snoozedUntil).toBe(localDate(2026, 8, 31, 9).toISOString());
390 });
391});
392
393it("dismisses a wake at its timestamp without hiding a later wake", () => {
394 const firstWake = "2026-04-10T10:00:00.000Z";
395 const state = {
396 wokeAt: firstWake,
397 settled: false,
398 lastVisitedAt: null,
399 latestRunCompletedAt: null,
400 };
401 expect(isThreadWokeUnseen(state)).toBe(true);
402 expect(isThreadWokeUnseen({ ...state, lastVisitedAt: firstWake })).toBe(false);
403 expect(isThreadWokeUnseen({ ...state, lastVisitedAt: firstWake, wokeAt: NOW })).toBe(true);
404});