The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { describe, expect, it, vi } from "vite-plus/test"; |
| 2 | |
| 3 | import { |
| 4 | createThreadNotificationGate, |
| 5 | isSnoozedForNotification, |
| 6 | RELEASED_COMPLETION_GRACE_MS, |
| 7 | type ThreadNotificationObservation, |
| 8 | } from "./ThreadNotificationCoordinator.logic"; |
| 9 | |
| 10 | const working: ThreadNotificationObservation = { |
| 11 | attention: null, |
| 12 | requestId: null, |
| 13 | settled: false, |
| 14 | completedAt: null, |
| 15 | backgroundWake: false, |
| 16 | }; |
| 17 | const completed = (at: number): ThreadNotificationObservation => ({ |
| 18 | attention: null, |
| 19 | requestId: null, |
| 20 | settled: true, |
| 21 | completedAt: at, |
| 22 | backgroundWake: false, |
| 23 | }); |
| 24 | const waking: ThreadNotificationObservation = { ...working, backgroundWake: true }; |
| 25 | const woke = (at: number): ThreadNotificationObservation => ({ |
| 26 | ...completed(at), |
| 27 | backgroundWake: true, |
| 28 | }); |
| 29 | const visible = { archived: false, quiet: false, snoozed: false }; |
| 30 | const snoozed = { ...visible, snoozed: true }; |
| 31 | |
| 32 | function fakeTimers(): Disposable { |
| 33 | vi.useFakeTimers(); |
| 34 | return { [Symbol.dispose]: () => void vi.useRealTimers() }; |
| 35 | } |
| 36 | |
| 37 | function harness() { |
| 38 | const notify = vi.fn(); |
| 39 | const gate = createThreadNotificationGate<string>(notify); |
| 40 | let prior: ThreadNotificationObservation | undefined; |
| 41 | const observe = (next: ThreadNotificationObservation, options = visible) => { |
| 42 | gate.observe("thread", prior, next, options); |
| 43 | prior = next; |
| 44 | }; |
| 45 | return { gate, notify, observe }; |
| 46 | } |
| 47 | |
| 48 | describe("createThreadNotificationGate", () => { |
| 49 | it("skips a completion seen while snoozed, but still notifies attention", () => { |
| 50 | const { notify, observe } = harness(); |
| 51 | observe(working); |
| 52 | observe(completed(1), snoozed); |
| 53 | observe({ ...completed(1) }); |
| 54 | expect(notify).not.toHaveBeenCalled(); |
| 55 | observe({ ...working, attention: "run-2:approval" }, snoozed); |
| 56 | expect(notify.mock.calls).toEqual([["thread", "input"]]); |
| 57 | }); |
| 58 | |
| 59 | it("notifies a completion immediately and only once", () => { |
| 60 | const { notify, observe } = harness(); |
| 61 | observe(working); |
| 62 | observe(completed(1)); |
| 63 | expect(notify.mock.calls).toEqual([["thread", "completion"]]); |
| 64 | observe({ ...completed(1) }); |
| 65 | observe(working); |
| 66 | observe(completed(1)); |
| 67 | expect(notify).toHaveBeenCalledTimes(1); |
| 68 | }); |
| 69 | |
| 70 | it("notifies each newer completion immediately", () => { |
| 71 | const { notify, observe } = harness(); |
| 72 | observe(working); |
| 73 | observe(completed(1)); |
| 74 | observe(working); |
| 75 | observe(completed(2)); |
| 76 | expect(notify.mock.calls).toEqual([ |
| 77 | ["thread", "completion"], |
| 78 | ["thread", "completion"], |
| 79 | ]); |
| 80 | observe(completed(1)); |
| 81 | expect(notify).toHaveBeenCalledTimes(2); |
| 82 | }); |
| 83 | |
| 84 | it("notifies each attention once per stretch and still notifies the subsequent completion", () => { |
| 85 | const { notify, observe } = harness(); |
| 86 | observe(working); |
| 87 | const attention = { ...working, attention: "run-1:approval:a", requestId: "a" }; |
| 88 | observe(attention); |
| 89 | expect(notify.mock.calls).toEqual([["thread", "input"]]); |
| 90 | observe({ ...attention }); |
| 91 | expect(notify).toHaveBeenCalledTimes(1); |
| 92 | observe({ ...attention, attention: "run-1:approval:b", requestId: "b" }); |
| 93 | observe(attention); |
| 94 | expect(notify).toHaveBeenCalledTimes(2); |
| 95 | observe(working); |
| 96 | observe(attention); |
| 97 | expect(notify).toHaveBeenCalledTimes(3); |
| 98 | const subagent = { ...attention, attention: "run-1:approval:", requestId: null }; |
| 99 | observe(subagent); |
| 100 | observe(attention); |
| 101 | observe(subagent); |
| 102 | expect(notify).toHaveBeenCalledTimes(5); |
| 103 | observe(working); |
| 104 | observe(completed(1)); |
| 105 | expect(notify.mock.calls).toEqual([ |
| 106 | ["thread", "input"], |
| 107 | ["thread", "input"], |
| 108 | ["thread", "input"], |
| 109 | ["thread", "input"], |
| 110 | ["thread", "input"], |
| 111 | ["thread", "completion"], |
| 112 | ]); |
| 113 | }); |
| 114 | |
| 115 | it("does not replay a completion after attention resolves", () => { |
| 116 | const { notify, observe } = harness(); |
| 117 | observe(working); |
| 118 | observe(completed(1)); |
| 119 | observe({ ...working, attention: "run-1:limited" }); |
| 120 | observe(completed(1)); |
| 121 | expect(notify.mock.calls).toEqual([ |
| 122 | ["thread", "completion"], |
| 123 | ["thread", "input"], |
| 124 | ]); |
| 125 | }); |
| 126 | |
| 127 | it("never notifies existing, archived or stopped completions, including when unarchived", () => { |
| 128 | const { notify, observe } = harness(); |
| 129 | observe(completed(1)); |
| 130 | observe(working); |
| 131 | observe(completed(2), { ...visible, quiet: true }); |
| 132 | observe(completed(2)); |
| 133 | observe(working); |
| 134 | observe(completed(3), { ...visible, archived: true }); |
| 135 | observe(completed(3)); |
| 136 | expect(notify).not.toHaveBeenCalled(); |
| 137 | observe(working); |
| 138 | observe(completed(4)); |
| 139 | expect(notify.mock.calls).toEqual([["thread", "completion"]]); |
| 140 | }); |
| 141 | |
| 142 | it("forgets handled completions when a thread is removed or the gate is cleared", () => { |
| 143 | for (const reset of [ |
| 144 | (gate: ReturnType<typeof createThreadNotificationGate<string>>) => gate.retain(new Set()), |
| 145 | (gate: ReturnType<typeof createThreadNotificationGate<string>>) => gate.clear(), |
| 146 | ]) { |
| 147 | const { gate, notify, observe } = harness(); |
| 148 | observe(completed(1)); |
| 149 | reset(gate); |
| 150 | gate.observe("thread", undefined, working, visible); |
| 151 | gate.observe("thread", working, completed(1), visible); |
| 152 | expect(notify.mock.calls).toEqual([["thread", "completion"]]); |
| 153 | } |
| 154 | }); |
| 155 | |
| 156 | it("waits out the grace before notifying a wake's completion, and a final wake replaces it", () => { |
| 157 | using _timers = fakeTimers(); |
| 158 | const { notify, observe } = harness(); |
| 159 | observe(working); |
| 160 | observe(completed(1)); |
| 161 | // Held until the monitor left, or completing after it already had: either way unheld here. |
| 162 | observe(waking); |
| 163 | observe(woke(2)); |
| 164 | expect(notify).toHaveBeenCalledTimes(1); |
| 165 | observe(working); |
| 166 | vi.advanceTimersByTime(RELEASED_COMPLETION_GRACE_MS); |
| 167 | expect(notify).toHaveBeenCalledTimes(1); |
| 168 | observe(completed(3)); |
| 169 | vi.advanceTimersByTime(RELEASED_COMPLETION_GRACE_MS); |
| 170 | expect(notify.mock.calls).toEqual([ |
| 171 | ["thread", "completion"], |
| 172 | ["thread", "completion"], |
| 173 | ]); |
| 174 | }); |
| 175 | |
| 176 | it("notifies a wake's completion once after the grace when no run follows, even across a pause", () => { |
| 177 | using _timers = fakeTimers(); |
| 178 | const { gate, notify, observe } = harness(); |
| 179 | observe(waking); |
| 180 | observe(woke(1)); |
| 181 | gate.pause(); |
| 182 | vi.advanceTimersByTime(RELEASED_COMPLETION_GRACE_MS); |
| 183 | expect(notify).not.toHaveBeenCalled(); |
| 184 | observe({ ...woke(1) }); |
| 185 | vi.advanceTimersByTime(RELEASED_COMPLETION_GRACE_MS - 1); |
| 186 | observe({ ...woke(1) }); |
| 187 | expect(notify).not.toHaveBeenCalled(); |
| 188 | vi.advanceTimersByTime(1); |
| 189 | expect(notify.mock.calls).toEqual([["thread", "completion"]]); |
| 190 | observe({ ...woke(1) }); |
| 191 | vi.advanceTimersByTime(RELEASED_COMPLETION_GRACE_MS); |
| 192 | expect(notify).toHaveBeenCalledTimes(1); |
| 193 | }); |
| 194 | |
| 195 | it("drops a pending wake completion when the thread is snoozed", () => { |
| 196 | using _timers = fakeTimers(); |
| 197 | const { notify, observe } = harness(); |
| 198 | observe(waking); |
| 199 | observe(woke(1)); |
| 200 | observe({ ...woke(1) }, snoozed); |
| 201 | vi.advanceTimersByTime(RELEASED_COMPLETION_GRACE_MS); |
| 202 | expect(notify).not.toHaveBeenCalled(); |
| 203 | }); |
| 204 | |
| 205 | it("drops a pending wake completion when the thread is removed or the gate is cleared", () => { |
| 206 | using _timers = fakeTimers(); |
| 207 | for (const reset of [ |
| 208 | (gate: ReturnType<typeof createThreadNotificationGate<string>>) => gate.retain(new Set()), |
| 209 | (gate: ReturnType<typeof createThreadNotificationGate<string>>) => gate.clear(), |
| 210 | ]) { |
| 211 | const { gate, notify, observe } = harness(); |
| 212 | observe(waking); |
| 213 | observe(woke(1)); |
| 214 | reset(gate); |
| 215 | vi.advanceTimersByTime(RELEASED_COMPLETION_GRACE_MS); |
| 216 | expect(notify).not.toHaveBeenCalled(); |
| 217 | } |
| 218 | }); |
| 219 | }); |
| 220 | |
| 221 | describe("isSnoozedForNotification", () => { |
| 222 | const snoozedAt = Date.parse("2026-01-01T11:00:00.000Z"); |
| 223 | const wakeAt = Date.parse("2026-01-01T12:00:00.000Z"); |
| 224 | const thread = { |
| 225 | snoozedAt: new Date(snoozedAt).toISOString(), |
| 226 | snoozedUntil: new Date(wakeAt).toISOString(), |
| 227 | hasPendingApprovals: false, |
| 228 | hasPendingUserInput: false, |
| 229 | }; |
| 230 | const later = wakeAt + 60_000; |
| 231 | |
| 232 | it.each([ |
| 233 | ["during the snooze, processed after the wake", thread, wakeAt - 1, later, true], |
| 234 | ["before the snooze, processed after the wake", thread, snoozedAt - 1, later, false], |
| 235 | ["after the wake", thread, wakeAt + 1, later, false], |
| 236 | [ |
| 237 | "after an early wake for attention", |
| 238 | { ...thread, hasPendingApprovals: true }, |
| 239 | wakeAt - 1, |
| 240 | wakeAt - 1, |
| 241 | false, |
| 242 | ], |
| 243 | ])("a completion %s is suppressed: %s", (_name, shell, completedAt, now, expected) => { |
| 244 | expect(isSnoozedForNotification(shell, completedAt, now)).toBe(expected); |
| 245 | }); |
| 246 | }); |