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