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 threadRaisedHandWhileSnoozed,
12 threadWokeAt,
13 isThreadWokeUnseen,
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 turnRequestedAt?: string | null;
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: input.turnRequestedAt === undefined ? SNOOZED_AT : input.turnRequestedAt,
59 startedAt: null,
60 completedAt: input.turnCompletedAt,
61 assistantMessageId: null,
62 },
63 };
64}
65
66type QueuedTurnShell = Parameters<typeof hasQueuedTurnStart>[0];
67
68function makeQueuedTurnShell(overrides: Partial<QueuedTurnShell> = {}): QueuedTurnShell {
69 return { latestUserMessageAt: null, latestTurn: null, session: null, ...overrides };
70}
71
72describe("effectiveSnoozed", () => {
73 it("never hides on malformed wake data", () => {
74 expect(effectiveSnoozed(makeShell({ snoozedUntil: "not-a-date" }), { now: NOW })).toBe(false);
75 });
76
77 it("wakes early when the agent is blocked on the user", () => {
78 expect(
79 effectiveSnoozed(makeShell({ snoozedUntil: FUTURE_WAKE, pending: "approval" }), {
80 now: NOW,
81 }),
82 ).toBe(false);
83 expect(
84 effectiveSnoozed(makeShell({ snoozedUntil: FUTURE_WAKE, pending: "user-input" }), {
85 now: NOW,
86 }),
87 ).toBe(false);
88 });
89
90 it.each([
91 {
92 reason: "stale failure",
93 completedAt: "2026-04-10T08:00:00.000Z",
94 pending: undefined,
95 wokeAt: null,
96 },
97 {
98 reason: "fresh failure",
99 completedAt: "2026-04-10T10:00:00.000Z",
100 pending: undefined,
101 wokeAt: "2026-04-10T10:00:00.000Z",
102 },
103 {
104 reason: "approval after a stale failure",
105 completedAt: "2026-04-10T08:00:00.000Z",
106 pending: "approval",
107 wokeAt: "2026-04-10T11:00:00.000Z",
108 },
109 {
110 reason: "input after a stale failure",
111 completedAt: "2026-04-10T08:00:00.000Z",
112 pending: "user-input",
113 wokeAt: "2026-04-10T11:00:00.000Z",
114 },
115 ] as const)(
116 "uses the wake event time for $reason despite newer metadata",
117 ({ completedAt, pending, wokeAt }) => {
118 const shell: ThreadSnoozeShell = {
119 ...makeShell({ snoozedUntil: FUTURE_WAKE }),
120 hasPendingApprovals: pending === "approval",
121 hasPendingUserInput: pending === "user-input",
122 runtime: { status: "failed", updatedAt: "2026-04-10T11:00:00.000Z" },
123 latestRun: { status: "failed", requestedAt: "2026-04-10T07:00:00.000Z", completedAt },
124 };
125 expect(effectiveSnoozed(shell, { now: NOW })).toBe(wokeAt === null);
126 expect(threadWokeAt(shell, { now: NOW })).toBe(wokeAt);
127 },
128 );
129
130 it("stays snoozed while the session keeps working — snooze never pauses the agent", () => {
131 expect(
132 effectiveSnoozed(makeShell({ snoozedUntil: FUTURE_WAKE, sessionStatus: "running" }), {
133 now: NOW,
134 }),
135 ).toBe(true);
136 });
137
138 it("stays snoozed when a run requested after the snooze completes", () => {
139 const shell = {
140 ...makeShell({ snoozedUntil: FUTURE_WAKE }),
141 latestRun: {
142 status: "completed",
143 requestedAt: "2026-04-10T09:00:00.001Z",
144 startedAt: "2026-04-10T09:15:00.000Z",
145 completedAt: "2026-04-10T10:30:00.000Z",
146 },
147 };
148
149 expect(effectiveSnoozed(shell, { now: NOW })).toBe(true);
150 expect(threadWokeAt(shell, { now: NOW })).toBe(null);
151 });
152});
153
154describe("threadRaisedHandWhileSnoozed", () => {
155 it("is true for approvals, input, and failures", () => {
156 expect(
157 threadRaisedHandWhileSnoozed(makeShell({ snoozedUntil: FUTURE_WAKE, pending: "approval" })),
158 ).toBe(true);
159 expect(
160 threadRaisedHandWhileSnoozed(makeShell({ snoozedUntil: FUTURE_WAKE, pending: "user-input" })),
161 ).toBe(true);
162 expect(
163 threadRaisedHandWhileSnoozed(
164 makeShell({ snoozedUntil: FUTURE_WAKE, sessionStatus: "error" }),
165 ),
166 ).toBe(true);
167 });
168});
169
170describe("canSnooze", () => {
171 it("refuses blocked-on-you work", () => {
172 expect(
173 canSnooze({ ...makeShell({ pending: "approval" }), latestUserMessageAt: null }, { now: NOW }),
174 ).toBe(false);
175 expect(
176 canSnooze(
177 { ...makeShell({ pending: "user-input" }), latestUserMessageAt: null },
178 { now: NOW },
179 ),
180 ).toBe(false);
181 expect(
182 canSnooze(
183 { ...makeShell({}), latestUserMessageAt: null, pendingSubagentRequest: "approval" },
184 { now: NOW },
185 ),
186 ).toBe(false);
187 });
188
189 it("refuses a queued turn start — same invisible-pending-work rule as settle", () => {
190 expect(
191 canSnooze(
192 { ...makeShell({}), latestUserMessageAt: "2026-04-10T11:59:30.000Z" },
193 { now: NOW },
194 ),
195 ).toBe(false);
196 expect(
197 canSnooze(
198 { ...makeShell({}), latestUserMessageAt: "2026-04-10T11:00:00.000Z" },
199 { now: NOW },
200 ),
201 ).toBe(true);
202 });
203});
204
205describe("hasQueuedTurnStart", () => {
206 it("clears queued state when a turn adopts the message or the session fails", () => {
207 const messageAt = "2026-04-10T11:59:00.000Z";
208 const adopted = makeQueuedTurnShell({
209 latestUserMessageAt: messageAt,
210 latestTurn: {
211 turnId: TurnId.make("turn-adopted"),
212 state: "running",
213 requestedAt: messageAt,
214 startedAt: null,
215 completedAt: null,
216 assistantMessageId: null,
217 },
218 });
219 const failed = makeQueuedTurnShell({
220 latestUserMessageAt: messageAt,
221 session: {
222 threadId: ThreadId.make("thread-failed"),
223 status: "error",
224 providerName: "Codex",
225 runtimeMode: "full-access",
226 activeTurnId: null,
227 lastError: "failed",
228 updatedAt: NOW,
229 },
230 });
231 expect(hasQueuedTurnStart(adopted, { now: NOW })).toBe(false);
232 expect(hasQueuedTurnStart(failed, { now: NOW })).toBe(false);
233 });
234});
235
236describe("threadWokeAt", () => {
237 it("reports the timer wake even after an already-requested run completes", () => {
238 expect(threadWokeAt(makeShell({ snoozedUntil: PAST_WAKE }), { now: NOW })).toBe(PAST_WAKE);
239 const shell = makeShell({
240 snoozedUntil: PAST_WAKE,
241 turnCompletedAt: "2026-04-10T09:30:00.000Z",
242 });
243 expect(effectiveSnoozed(shell, { now: NOW })).toBe(false);
244 expect(threadWokeAt(shell, { now: NOW })).toBe(PAST_WAKE);
245 });
246
247 it.each([
248 { reason: "failure", sessionStatus: "error" },
249 { reason: "approval", sessionStatus: "ready", pending: "approval" },
250 { reason: "user input", sessionStatus: "ready", pending: "user-input" },
251 ] as const)("reports $reason activity instead of an already-requested completion", (activity) => {
252 const shell = makeShell({
253 ...activity,
254 snoozedUntil: FUTURE_WAKE,
255 turnCompletedAt: "2026-04-10T10:30:00.000Z",
256 });
257 expect(effectiveSnoozed(shell, { now: NOW })).toBe(false);
258 expect(threadWokeAt(shell, { now: NOW })).toBe("2026-04-10T11:00:00.000Z");
259 });
260
261 it("keeps the early wake authoritative after the scheduled time passes", () => {
262 const shell: ThreadSnoozeShell = {
263 ...makeShell({ snoozedUntil: PAST_WAKE }),
264 runtime: { status: "failed", updatedAt: "2026-04-10T11:00:00.000Z" },
265 latestRun: {
266 status: "failed",
267 requestedAt: "2026-04-10T09:15:00.000Z",
268 completedAt: "2026-04-10T09:30:00.000Z",
269 },
270 };
271 expect(threadWokeAt(shell, { now: NOW })).toBe("2026-04-10T09:30:00.000Z");
272 });
273});
274
275describe("resolveSnoozePresets", () => {
276 it("drops the evening choice once evening is near or past", () => {
277 expect(resolveSnoozePresets(localDate(2026, 4, 8, 17, 30)).map((preset) => preset.id)).toEqual([
278 "hour",
279 "three-hours",
280 "tomorrow",
281 "next-week",
282 ]);
283 });
284
285 it("drops next week on Sundays, when it lands on the same Monday as tomorrow", () => {
286 const presets = resolveSnoozePresets(localDate(2026, 8, 30, 7, 1));
287 expect(presets.map((preset) => preset.id)).toEqual([
288 "hour",
289 "three-hours",
290 "evening",
291 "tomorrow",
292 ]);
293 const tomorrow = new Date(presets.find((preset) => preset.id === "tomorrow")!.snoozedUntil);
294 expect(tomorrow.getDay()).toBe(1);
295 });
296});
297
298it("dismisses a wake at its timestamp without hiding a later wake", () => {
299 const firstWake = "2026-04-10T10:00:00.000Z";
300 const state = {
301 wokeAt: firstWake,
302 settled: false,
303 lastVisitedAt: null,
304 latestRunCompletedAt: null,
305 };
306 expect(isThreadWokeUnseen(state)).toBe(true);
307 expect(isThreadWokeUnseen({ ...state, lastVisitedAt: firstWake })).toBe(false);
308 expect(isThreadWokeUnseen({ ...state, lastVisitedAt: firstWake, wokeAt: NOW })).toBe(true);
309});