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

threadSnoozed.test.ts

313 lines10.5 KB

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