Open History to see the last commit
1import { ThreadId } from "@t3tools/contracts";
2import { TurnId } from "@t3tools/contracts";
3import { describe, expect, it } from "vite-plus/test";
4
5import {
6 canSnooze,
7 effectiveSnoozed,
8 threadRaisedHandWhileSnoozed,
9 threadWokeAt,
10 type ThreadSnoozeShell,
11} from "./threadSettled.ts";
12
13const NOW = "2026-04-10T12:00:00.000Z";
14const SNOOZED_AT = "2026-04-10T09:00:00.000Z";
15const FUTURE_WAKE = "2026-04-11T09:00:00.000Z";
16const PAST_WAKE = "2026-04-10T10:00:00.000Z";
17
18function makeShell(input: {
19 readonly snoozedUntil?: string | null;
20 readonly snoozedAt?: string | null;
21 readonly sessionStatus?: "starting" | "running" | "ready" | "error";
22 readonly pending?: "approval" | "user-input";
23 readonly turnCompletedAt?: string | null;
24}): ThreadSnoozeShell {
25 const threadId = ThreadId.make("thread-1");
26 return {
27 snoozedUntil: input.snoozedUntil ?? null,
28 snoozedAt: input.snoozedAt ?? (input.snoozedUntil != null ? SNOOZED_AT : null),
29 hasPendingApprovals: input.pending === "approval",
30 hasPendingUserInput: input.pending === "user-input",
31 session:
32 input.sessionStatus === undefined
33 ? null
34 : {
35 threadId,
36 status: input.sessionStatus,
37 providerName: "Codex",
38 runtimeMode: "full-access",
39 activeTurnId: null,
40 lastError: input.sessionStatus === "error" ? "boom" : null,
41 updatedAt: "2026-04-10T11:00:00.000Z",
42 },
43 latestTurn:
44 input.turnCompletedAt === undefined
45 ? null
46 : {
47 turnId: TurnId.make("turn-1"),
48 state: "completed",
49 requestedAt: SNOOZED_AT,
50 startedAt: null,
51 completedAt: input.turnCompletedAt,
52 assistantMessageId: null,
53 },
54 };
55}
56
57describe("effectiveSnoozed", () => {
58 it("hides a thread whose wake time is in the future", () => {
59 expect(effectiveSnoozed(makeShell({ snoozedUntil: FUTURE_WAKE }), { now: NOW })).toBe(true);
60 });
61
62 it("stops classifying as snoozed once the wake time passes (timer wake, no event)", () => {
63 expect(effectiveSnoozed(makeShell({ snoozedUntil: PAST_WAKE }), { now: NOW })).toBe(false);
64 });
65
66 it("never snoozes a thread with no snooze state", () => {
67 expect(effectiveSnoozed(makeShell({}), { now: NOW })).toBe(false);
68 });
69
70 it("never hides on malformed wake data", () => {
71 expect(effectiveSnoozed(makeShell({ snoozedUntil: "not-a-date" }), { now: NOW })).toBe(false);
72 });
73
74 it("wakes early when the agent is blocked on the user", () => {
75 expect(
76 effectiveSnoozed(makeShell({ snoozedUntil: FUTURE_WAKE, pending: "approval" }), {
77 now: NOW,
78 }),
79 ).toBe(false);
80 expect(
81 effectiveSnoozed(makeShell({ snoozedUntil: FUTURE_WAKE, pending: "user-input" }), {
82 now: NOW,
83 }),
84 ).toBe(false);
85 });
86
87 it("wakes early on a failure that happened after the snooze", () => {
88 // makeShell stamps session.updatedAt at 11:00, after SNOOZED_AT (9:00).
89 expect(
90 effectiveSnoozed(makeShell({ snoozedUntil: FUTURE_WAKE, sessionStatus: "error" }), {
91 now: NOW,
92 }),
93 ).toBe(false);
94 });
95
96 it("stays snoozed when the failure predates the snooze — the user saw it", () => {
97 expect(
98 effectiveSnoozed(
99 makeShell({
100 snoozedUntil: FUTURE_WAKE,
101 sessionStatus: "error",
102 // Snoozed AFTER the error's status edge.
103 snoozedAt: "2026-04-10T11:30:00.000Z",
104 }),
105 { now: NOW },
106 ),
107 ).toBe(true);
108 });
109
110 it("stays snoozed while the session keeps working — snooze never pauses the agent", () => {
111 expect(
112 effectiveSnoozed(makeShell({ snoozedUntil: FUTURE_WAKE, sessionStatus: "running" }), {
113 now: NOW,
114 }),
115 ).toBe(true);
116 });
117
118 it("wakes early when a run completes after the snooze was set", () => {
119 expect(
120 effectiveSnoozed(
121 makeShell({ snoozedUntil: FUTURE_WAKE, turnCompletedAt: "2026-04-10T10:30:00.000Z" }),
122 { now: NOW },
123 ),
124 ).toBe(false);
125 });
126
127 it("ignores runs that completed before the snooze — the user saw that result", () => {
128 expect(
129 effectiveSnoozed(
130 makeShell({ snoozedUntil: FUTURE_WAKE, turnCompletedAt: "2026-04-10T08:00:00.000Z" }),
131 { now: NOW },
132 ),
133 ).toBe(true);
134 });
135});
136
137describe("threadRaisedHandWhileSnoozed", () => {
138 it("is false for a quiet snoozed thread", () => {
139 expect(threadRaisedHandWhileSnoozed(makeShell({ snoozedUntil: FUTURE_WAKE }))).toBe(false);
140 });
141
142 it("is true for approvals, input, and failures", () => {
143 expect(
144 threadRaisedHandWhileSnoozed(makeShell({ snoozedUntil: FUTURE_WAKE, pending: "approval" })),
145 ).toBe(true);
146 expect(
147 threadRaisedHandWhileSnoozed(makeShell({ snoozedUntil: FUTURE_WAKE, pending: "user-input" })),
148 ).toBe(true);
149 expect(
150 threadRaisedHandWhileSnoozed(
151 makeShell({ snoozedUntil: FUTURE_WAKE, sessionStatus: "error" }),
152 ),
153 ).toBe(true);
154 });
155});
156
157describe("canSnooze", () => {
158 it("allows snoozing quiet and working threads alike", () => {
159 expect(canSnooze({ ...makeShell({}), latestUserMessageAt: null }, { now: NOW })).toBe(true);
160 expect(
161 canSnooze(
162 { ...makeShell({ sessionStatus: "running" }), latestUserMessageAt: null },
163 { now: NOW },
164 ),
165 ).toBe(true);
166 });
167
168 it("refuses blocked-on-you work", () => {
169 expect(
170 canSnooze({ ...makeShell({ pending: "approval" }), latestUserMessageAt: null }, { now: NOW }),
171 ).toBe(false);
172 expect(
173 canSnooze(
174 { ...makeShell({ pending: "user-input" }), latestUserMessageAt: null },
175 { now: NOW },
176 ),
177 ).toBe(false);
178 });
179
180 it("refuses a queued turn start — same invisible-pending-work rule as settle", () => {
181 // Fresh user message, no turn has adopted it, within the grace window.
182 expect(
183 canSnooze(
184 { ...makeShell({}), latestUserMessageAt: "2026-04-10T11:59:30.000Z" },
185 { now: NOW },
186 ),
187 ).toBe(false);
188 // Outside the grace window the message is stale data, not queued work.
189 expect(
190 canSnooze(
191 { ...makeShell({}), latestUserMessageAt: "2026-04-10T11:00:00.000Z" },
192 { now: NOW },
193 ),
194 ).toBe(true);
195 });
196});
197
198describe("threadWokeAt", () => {
199 it("is null for never-snoozed and still-snoozed threads", () => {
200 expect(threadWokeAt(makeShell({}), { now: NOW })).toBe(null);
201 expect(threadWokeAt(makeShell({ snoozedUntil: FUTURE_WAKE }), { now: NOW })).toBe(null);
202 });
203
204 it("reports the wake time for a timer wake", () => {
205 expect(threadWokeAt(makeShell({ snoozedUntil: PAST_WAKE }), { now: NOW })).toBe(PAST_WAKE);
206 });
207
208 it("reports the completion time for an early run-completed wake", () => {
209 expect(
210 threadWokeAt(
211 makeShell({ snoozedUntil: FUTURE_WAKE, turnCompletedAt: "2026-04-10T10:30:00.000Z" }),
212 { now: NOW },
213 ),
214 ).toBe("2026-04-10T10:30:00.000Z");
215 });
216
217 it("falls back to session activity for blocked/failed early wakes", () => {
218 expect(
219 threadWokeAt(makeShell({ snoozedUntil: FUTURE_WAKE, sessionStatus: "error" }), {
220 now: NOW,
221 }),
222 ).toBe("2026-04-10T11:00:00.000Z");
223 });
224
225 it("keeps the early wake authoritative after the scheduled time passes", () => {
226 // Woke early at 10:30 via run-completed; the scheduled wake (PAST_WAKE
227 // 10:00 relative to a later now) has ALSO passed. Reporting the
228 // scheduled time would resurface a Woke pill the user already cleared
229 // by visiting between the early wake and now.
230 expect(
231 threadWokeAt(
232 makeShell({ snoozedUntil: PAST_WAKE, turnCompletedAt: "2026-04-10T09:30:00.000Z" }),
233 { now: NOW },
234 ),
235 ).toBe("2026-04-10T09:30:00.000Z");
236 });
237});