Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

threadSnoozed.test.ts

392 lines13.2 KB

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);
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 };
67​
68type QueuedTurnShell = Parameters<typeof hasQueuedTurnStart>[0];
69​
70function makeQueuedTurnShell(overrides: Partial<QueuedTurnShell> = {}): QueuedTurnShell {
71 return { latestUserMessageAt: null, latestTurn: null, session: null, ...overrides };
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: "fresh failure",
101 completedAt: "2026-04-10T10:00:00.000Z",
102 pending: undefined,
103 wokeAt: "2026-04-10T10:00:00.000Z",
104 },
105 {
106 reason: "approval after a stale failure",
107 completedAt: "2026-04-10T08:00:00.000Z",
108 pending: "approval",
109 wokeAt: "2026-04-10T11:00:00.000Z",
110 },
111 {
112 reason: "input after a stale failure",
113 completedAt: "2026-04-10T08:00:00.000Z",
114 pending: "user-input",
115 wokeAt: "2026-04-10T11:00:00.000Z",
116 },
117 ] as const)(
118 "uses the wake event time for $reason despite newer metadata",
119 ({ completedAt, pending, wokeAt }) => {
120 const shell: ThreadSnoozeShell = {
121 ...makeShell({ snoozedUntil: FUTURE_WAKE }),
122 hasPendingApprovals: pending === "approval",
123 hasPendingUserInput: pending === "user-input",
124 runtime: { status: "failed", updatedAt: "2026-04-10T11:00:00.000Z" },
125 latestRun: { status: "failed", requestedAt: "2026-04-10T07:00:00.000Z", completedAt },
126 };
127 expect(effectiveSnoozed(shell, { now: NOW })).toBe(wokeAt === null);
128 expect(threadWokeAt(shell, { now: NOW })).toBe(wokeAt);
129 },
130 );
131​
132 it("stays snoozed while the session keeps working — snooze never pauses the agent", () => {
133 expect(
134 effectiveSnoozed(makeShell({ snoozedUntil: FUTURE_WAKE, sessionStatus: "running" }), {
135 now: NOW,
136 }),
137 ).toBe(true);
138 });
139​
140 it("stays snoozed when a run requested after the snooze completes", () => {
141 const shell = {
142 ...makeShell({ snoozedUntil: FUTURE_WAKE }),
143 latestRun: {
144 status: "completed",
145 requestedAt: "2026-04-10T09:00:00.001Z",
146 startedAt: "2026-04-10T09:15:00.000Z",
147 completedAt: "2026-04-10T10:30:00.000Z",
148 },
149 };
150​
151 expect(effectiveSnoozed(shell, { now: NOW })).toBe(true);
152 expect(threadWokeAt(shell, { now: NOW })).toBe(null);
153 });
154});
155​
156describe("threadRaisedHandWhileSnoozed", () => {
157 it("is true for approvals, input, and failures", () => {
158 expect(
159 threadRaisedHandWhileSnoozed(makeShell({ snoozedUntil: FUTURE_WAKE, pending: "approval" })),
160 ).toBe(true);
161 expect(
162 threadRaisedHandWhileSnoozed(makeShell({ snoozedUntil: FUTURE_WAKE, pending: "user-input" })),
163 ).toBe(true);
164 expect(
165 threadRaisedHandWhileSnoozed(
166 makeShell({ snoozedUntil: FUTURE_WAKE, sessionStatus: "error" }),
167 ),
168 ).toBe(true);
169 });
170});
171​
172describe("canSnooze", () => {
173 it("refuses blocked-on-you work", () => {
174 expect(
175 canSnooze({ ...makeShell({ pending: "approval" }), latestUserMessageAt: null }, { now: NOW }),
176 ).toBe(false);
177 expect(
178 canSnooze(
179 { ...makeShell({ pending: "user-input" }), latestUserMessageAt: null },
180 { now: NOW },
181 ),
182 ).toBe(false);
183 expect(
184 canSnooze(
185 { ...makeShell({}), latestUserMessageAt: null, pendingSubagentRequest: "approval" },
186 { now: NOW },
187 ),
188 ).toBe(false);
189 });
190​
191 it("refuses a queued turn start — same invisible-pending-work rule as settle", () => {
192 expect(
193 canSnooze(
194 { ...makeShell({}), latestUserMessageAt: "2026-04-10T11:59:30.000Z" },
195 { now: NOW },
196 ),
197 ).toBe(false);
198 expect(
199 canSnooze(
200 { ...makeShell({}), latestUserMessageAt: "2026-04-10T11:00:00.000Z" },
201 { now: NOW },
202 ),
203 ).toBe(true);
204 });
205});
206​
207describe("hasQueuedTurnStart", () => {
208 it("clears queued state when a turn adopts the message or the session fails", () => {
209 const messageAt = "2026-04-10T11:59:00.000Z";
210 const adopted = makeQueuedTurnShell({
211 latestUserMessageAt: messageAt,
212 latestTurn: {
213 turnId: TurnId.make("turn-adopted"),
214 state: "running",
215 requestedAt: messageAt,
216 startedAt: null,
217 completedAt: null,
218 assistantMessageId: null,
219 },
220 });
221 const failed = makeQueuedTurnShell({
222 latestUserMessageAt: messageAt,
223 session: {
224 threadId: ThreadId.make("thread-failed"),
225 status: "error",
226 providerName: "Codex",
227 runtimeMode: "full-access",
228 activeTurnId: null,
229 lastError: "failed",
230 updatedAt: NOW,
231 },
232 });
233 expect(hasQueuedTurnStart(adopted, { now: NOW })).toBe(false);
234 expect(hasQueuedTurnStart(failed, { now: NOW })).toBe(false);
235 });
236});
237​
238describe("threadWokeAt", () => {
239 it("reports the timer wake even after an already-requested run completes", () => {
240 expect(threadWokeAt(makeShell({ snoozedUntil: PAST_WAKE }), { now: NOW })).toBe(PAST_WAKE);
241 const shell = makeShell({
242 snoozedUntil: PAST_WAKE,
243 turnCompletedAt: "2026-04-10T09:30:00.000Z",
244 });
245 expect(effectiveSnoozed(shell, { now: NOW })).toBe(false);
246 expect(threadWokeAt(shell, { now: NOW })).toBe(PAST_WAKE);
247 });
248​
249 it.each([
250 { reason: "failure", sessionStatus: "error" },
251 { reason: "approval", sessionStatus: "ready", pending: "approval" },
252 { reason: "user input", sessionStatus: "ready", pending: "user-input" },
253 ] as const)("reports $reason activity instead of an already-requested completion", (activity) => {
254 const shell = makeShell({
255 ...activity,
256 snoozedUntil: FUTURE_WAKE,
257 turnCompletedAt: "2026-04-10T10:30:00.000Z",
258 });
259 expect(effectiveSnoozed(shell, { now: NOW })).toBe(false);
260 expect(threadWokeAt(shell, { now: NOW })).toBe("2026-04-10T11:00:00.000Z");
261 });
262​
263 it("keeps the early wake authoritative after the scheduled time passes", () => {
264 const shell: ThreadSnoozeShell = {
265 ...makeShell({ snoozedUntil: PAST_WAKE }),
266 runtime: { status: "failed", updatedAt: "2026-04-10T11:00:00.000Z" },
267 latestRun: {
268 status: "failed",
269 requestedAt: "2026-04-10T09:15:00.000Z",
270 completedAt: "2026-04-10T09:30:00.000Z",
271 },
272 };
273 expect(threadWokeAt(shell, { now: NOW })).toBe("2026-04-10T09:30:00.000Z");
274 });
275});
276​
277describe("resolveSnoozePresets", () => {
278 const formatTime = (date: Date) =>
279 `${date.getHours()}:${String(date.getMinutes()).padStart(2, "0")}`;
280 const menu = (now: Date, presets?: ReadonlyArray<SnoozePresetSetting>) =>
281 resolveSnoozePresets(now, { formatTime, ...(presets ? { presets } : {}) }).map(
282 (preset) => `${preset.label} ${preset.whenLabel}`,
283 );
284​
285 it("lists custom presets soonest first and names the day when the label does not", () => {
286 // Wednesday 20:00.
287 expect(
288 menu(localDate(2026, 4, 8, 20), [
289 { kind: "at", day: "monday", time: "18:00" },
290 { kind: "at", day: "tomorrow", time: "21:00" },
291 { kind: "after", minutes: 360 },
292 { kind: "after", minutes: 180 },
293 ]),
294 ).toEqual(["In 3 hours 23:00", "In 6 hours Thu 2:00", "Tomorrow 21:00", "Next week Mon 18:00"]);
295 });
296​
297 it("drops a today preset once its time passes and moves a weekday on that day to next week", () => {
298 // Monday 18:30.
299 const now = localDate(2026, 4, 13, 18, 30);
300 const presets = resolveSnoozePresets(now, {
301 presets: [
302 { kind: "at", day: "today", time: "18:00" },
303 { kind: "at", day: "monday", time: "09:00" },
304 ],
305 });
306 expect(presets.map((preset) => preset.id)).toEqual(["at:monday:09:00"]);
307 expect(new Date(presets[0]!.snoozedUntil)).toEqual(localDate(2026, 4, 20, 9));
308 });
309​
310 it("names the real day gap: a Monday option on Sunday is Tomorrow, a weekday a week out is Next", () => {
311 const theo: ReadonlyArray<SnoozePresetSetting> = [
312 { kind: "at", day: "tomorrow", time: "21:00" },
313 { kind: "at", day: "monday", time: "18:00" },
314 { kind: "at", day: "friday", time: "17:00" },
315 ];
316 // Sunday 14:00.
317 expect(menu(localDate(2026, 8, 30, 14), theo)).toEqual([
318 "Tomorrow 18:00",
319 "Tomorrow 21:00",
320 "Friday 17:00",
321 ]);
322 // Friday 10:00: today's 17:00 is a week away.
323 expect(menu(localDate(2026, 9, 4, 10), theo)).toEqual([
324 "Tomorrow 21:00",
325 "Next week Mon 18:00",
326 "Next Friday 17:00",
327 ]);
328 });
329​
330 it("shows one row when two presets wake at the same time", () => {
331 // Sunday, so Tomorrow 9:00 and Next week 9:00 are the same Monday.
332 expect(menu(localDate(2026, 8, 30, 7, 1))).toEqual([
333 "In 1 hour 8:01",
334 "In 3 hours 10:01",
335 "This evening 18:00",
336 "Tomorrow 9:00",
337 ]);
338 });
339​
340 it("treats a duration and a set time in the same minute as one row", () => {
341 const now = localDate(2026, 4, 8, 15);
342 now.setSeconds(25);
343 expect(
344 menu(now, [
345 { kind: "after", minutes: 180 },
346 { kind: "at", day: "today", time: "18:00" },
347 ]),
348 ).toEqual(["In 3 hours 18:00"]);
349 });
350});
351​
352describe("pickSnoozePreset", () => {
353 it("starts a duration when it is picked, even when the menu merged it with a set time", () => {
354 const presets: ReadonlyArray<SnoozePresetSetting> = [
355 { kind: "at", day: "today", time: "18:30" },
356 { kind: "after", minutes: 180 },
357 ];
358 // At 15:00 the two differ; at 15:30 "In 3 hours" lands on 18:30 and the menu shows one row.
359 const shown = resolveSnoozePresets(localDate(2026, 4, 8, 15), { presets });
360 const picked = pickSnoozePreset({
361 shown,
362 id: "after:180",
363 now: localDate(2026, 4, 8, 15, 30),
364 presets,
365 });
366 expect(picked?.snoozedUntil).toBe(localDate(2026, 4, 8, 18, 30).toISOString());
367 });
368​
369 it("keeps a set time's shown wake when the day changes while the menu is open", () => {
370 // Sunday 23:59: "Tomorrow" means Monday 9:00, even if the pick lands after midnight.
371 const shown = resolveSnoozePresets(localDate(2026, 8, 30, 23, 59));
372 const picked = pickSnoozePreset({
373 shown,
374 id: "at:tomorrow:09:00",
375 now: localDate(2026, 8, 31, 0, 1),
376 });
377 expect(picked?.snoozedUntil).toBe(localDate(2026, 8, 31, 9).toISOString());
378 });
379});
380​
381it("dismisses a wake at its timestamp without hiding a later wake", () => {
382 const firstWake = "2026-04-10T10:00:00.000Z";
383 const state = {
384 wokeAt: firstWake,
385 settled: false,
386 lastVisitedAt: null,
387 latestRunCompletedAt: null,
388 };
389 expect(isThreadWokeUnseen(state)).toBe(true);
390 expect(isThreadWokeUnseen({ ...state, lastVisitedAt: firstWake })).toBe(false);
391 expect(isThreadWokeUnseen({ ...state, lastVisitedAt: firstWake, wokeAt: NOW })).toBe(true);
392});