|
1 | | -import { WORKING_EXIT_GRACE_MS } from "@t3tools/client-runtime/state/thread-inbox"; |
2 | | -import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test"; |
| 1 | +import { describe, expect, it, vi } from "vite-plus/test"; |
3 | 2 |
|
4 | 3 | import { |
5 | 4 | createThreadNotificationGate, |
@@ -30,164 +29,85 @@ function harness() { |
30 | 29 | } |
31 | 30 |
|
32 | 31 | describe("createThreadNotificationGate", () => { |
33 | | - beforeEach(() => { |
34 | | - vi.useFakeTimers(); |
35 | | - }); |
36 | | - afterEach(() => { |
37 | | - vi.useRealTimers(); |
38 | | - }); |
39 | | - |
40 | | - it("drops a completion when the thread resumes within the grace", () => { |
41 | | - const { notify, observe } = harness(); |
42 | | - observe(working); |
43 | | - observe(completed(1)); |
44 | | - vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS - 1); |
45 | | - observe(working); |
46 | | - vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS * 2); |
47 | | - expect(notify).not.toHaveBeenCalled(); |
48 | | - }); |
49 | | - |
50 | | - it("notifies a real completion once after the grace", () => { |
| 32 | + it("notifies a completion immediately and only once", () => { |
51 | 33 | const { notify, observe } = harness(); |
52 | 34 | observe(working); |
53 | 35 | observe(completed(1)); |
54 | | - vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS - 1); |
55 | | - expect(notify).not.toHaveBeenCalled(); |
56 | | - vi.advanceTimersByTime(1); |
57 | 36 | expect(notify.mock.calls).toEqual([["thread", "completion"]]); |
58 | 37 | observe({ ...completed(1) }); |
59 | | - vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS * 2); |
60 | | - expect(notify).toHaveBeenCalledTimes(1); |
61 | | - }); |
62 | | - |
63 | | - it("notifies attention at once and replaces a pending completion", () => { |
64 | | - const { notify, observe } = harness(); |
65 | 38 | observe(working); |
66 | 39 | observe(completed(1)); |
67 | | - vi.advanceTimersByTime(500); |
68 | | - observe({ attention: "run-1:approval", settled: false, completedAt: null }); |
69 | | - expect(notify.mock.calls).toEqual([["thread", "input"]]); |
70 | | - vi.advanceTimersByTime(60_000); |
71 | | - // The request resolves and the same run reads as completed again. |
72 | | - observe(completed(1)); |
73 | | - vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS * 2); |
74 | | - expect(notify.mock.calls).toEqual([["thread", "input"]]); |
| 40 | + expect(notify).toHaveBeenCalledTimes(1); |
75 | 41 | }); |
76 | 42 |
|
77 | | - it("notifies one completion across continuation gaps", () => { |
| 43 | + it("notifies each newer completion immediately", () => { |
78 | 44 | const { notify, observe } = harness(); |
79 | 45 | observe(working); |
80 | 46 | observe(completed(1)); |
81 | | - vi.advanceTimersByTime(300); |
82 | 47 | observe(working); |
83 | | - vi.advanceTimersByTime(5_000); |
84 | 48 | observe(completed(2)); |
85 | | - vi.advanceTimersByTime(300); |
86 | | - observe(working); |
87 | | - vi.advanceTimersByTime(5_000); |
88 | | - observe(completed(3)); |
89 | | - vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS); |
90 | | - expect(notify.mock.calls).toEqual([["thread", "completion"]]); |
| 49 | + expect(notify.mock.calls).toEqual([ |
| 50 | + ["thread", "completion"], |
| 51 | + ["thread", "completion"], |
| 52 | + ]); |
| 53 | + observe(completed(1)); |
| 54 | + expect(notify).toHaveBeenCalledTimes(2); |
91 | 55 | }); |
92 | 56 |
|
93 | | - it("keeps a completion interrupted by attention after a working gap from firing", () => { |
| 57 | + it("notifies attention once and still notifies the subsequent completion", () => { |
94 | 58 | const { notify, observe } = harness(); |
95 | 59 | observe(working); |
96 | | - observe(completed(1)); |
97 | | - vi.advanceTimersByTime(500); |
| 60 | + const attention = { attention: "run-1:approval", settled: false, completedAt: null }; |
| 61 | + observe(attention); |
| 62 | + expect(notify.mock.calls).toEqual([["thread", "input"]]); |
| 63 | + observe({ ...attention }); |
| 64 | + expect(notify).toHaveBeenCalledTimes(1); |
98 | 65 | observe(working); |
99 | | - observe({ attention: "run-1:approval", settled: false, completedAt: null }); |
100 | | - vi.advanceTimersByTime(60_000); |
101 | 66 | observe(completed(1)); |
102 | | - vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS * 2); |
103 | | - expect(notify.mock.calls).toEqual([["thread", "input"]]); |
| 67 | + expect(notify.mock.calls).toEqual([ |
| 68 | + ["thread", "input"], |
| 69 | + ["thread", "completion"], |
| 70 | + ]); |
104 | 71 | }); |
105 | 72 |
|
106 | | - it("adds nothing when attention follows a completion that already fired", () => { |
| 73 | + it("does not replay a completion after attention resolves", () => { |
107 | 74 | const { notify, observe } = harness(); |
108 | 75 | observe(working); |
109 | 76 | observe(completed(1)); |
110 | | - vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS); |
111 | 77 | observe({ attention: "run-1:limited", settled: false, completedAt: null }); |
112 | 78 | observe(completed(1)); |
113 | | - vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS * 2); |
114 | 79 | expect(notify.mock.calls).toEqual([ |
115 | 80 | ["thread", "completion"], |
116 | 81 | ["thread", "input"], |
117 | 82 | ]); |
118 | 83 | }); |
119 | 84 |
|
120 | | - it("still notifies a newer completion after attention", () => { |
| 85 | + it("never notifies existing, archived or stopped completions, including when unarchived", () => { |
121 | 86 | const { notify, observe } = harness(); |
122 | | - observe(working); |
123 | 87 | observe(completed(1)); |
124 | | - vi.advanceTimersByTime(500); |
125 | | - observe({ attention: "run-2:approval", settled: false, completedAt: null }); |
126 | 88 | observe(working); |
| 89 | + observe(completed(2), { ...visible, quiet: true }); |
127 | 90 | observe(completed(2)); |
128 | | - vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS); |
129 | | - expect(notify.mock.calls).toEqual([ |
130 | | - ["thread", "input"], |
131 | | - ["thread", "completion"], |
132 | | - ]); |
133 | | - }); |
134 | | - |
135 | | - it("ignores unrelated updates while a completion waits", () => { |
136 | | - const { notify, observe } = harness(); |
137 | 91 | observe(working); |
138 | | - observe(completed(1)); |
139 | | - vi.advanceTimersByTime(1_500); |
140 | | - observe({ ...completed(1) }); |
141 | | - vi.advanceTimersByTime(500); |
142 | | - expect(notify).toHaveBeenCalledTimes(1); |
143 | | - }); |
144 | | - |
145 | | - it("notifies a run that settles again after a brief runtime blip", () => { |
146 | | - const { notify, observe } = harness(); |
147 | | - observe(working); |
148 | | - observe(completed(1)); |
149 | | - vi.advanceTimersByTime(500); |
| 92 | + observe(completed(3), { ...visible, archived: true }); |
| 93 | + observe(completed(3)); |
| 94 | + expect(notify).not.toHaveBeenCalled(); |
150 | 95 | observe(working); |
151 | | - observe(completed(1)); |
152 | | - vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS); |
| 96 | + observe(completed(4)); |
153 | 97 | expect(notify.mock.calls).toEqual([["thread", "completion"]]); |
154 | 98 | }); |
155 | 99 |
|
156 | | - it("restarts the grace when a newer completion replaces a pending one", () => { |
157 | | - const { notify, observe } = harness(); |
158 | | - observe(working); |
159 | | - observe(completed(1)); |
160 | | - vi.advanceTimersByTime(1_500); |
161 | | - observe(completed(2)); |
162 | | - vi.advanceTimersByTime(1_500); |
163 | | - expect(notify).not.toHaveBeenCalled(); |
164 | | - vi.advanceTimersByTime(500); |
165 | | - expect(notify).toHaveBeenCalledTimes(1); |
166 | | - }); |
167 | | - |
168 | | - it("drops a pending completion on archive, stop request, removal or clear", () => { |
169 | | - for (const end of [ |
170 | | - (h: ReturnType<typeof harness>) => h.observe(completed(1), { ...visible, archived: true }), |
171 | | - (h: ReturnType<typeof harness>) => |
172 | | - h.observe({ ...completed(1) }, { ...visible, quiet: true }), |
173 | | - (h: ReturnType<typeof harness>) => h.gate.retain(new Set()), |
174 | | - (h: ReturnType<typeof harness>) => h.gate.clear(), |
| 100 | + it("forgets handled completions when a thread is removed or the gate is cleared", () => { |
| 101 | + for (const reset of [ |
| 102 | + (gate: ReturnType<typeof createThreadNotificationGate<string>>) => gate.retain(new Set()), |
| 103 | + (gate: ReturnType<typeof createThreadNotificationGate<string>>) => gate.clear(), |
175 | 104 | ]) { |
176 | | - const h = harness(); |
177 | | - h.observe(working); |
178 | | - h.observe(completed(1)); |
179 | | - end(h); |
180 | | - vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS * 2); |
181 | | - expect(h.notify).not.toHaveBeenCalled(); |
| 105 | + const { gate, notify, observe } = harness(); |
| 106 | + observe(completed(1)); |
| 107 | + reset(gate); |
| 108 | + gate.observe("thread", undefined, working, visible); |
| 109 | + gate.observe("thread", working, completed(1), visible); |
| 110 | + expect(notify.mock.calls).toEqual([["thread", "completion"]]); |
182 | 111 | } |
183 | 112 | }); |
184 | | - |
185 | | - it("never notifies a stopped run or the first observation", () => { |
186 | | - const { notify, observe } = harness(); |
187 | | - observe(completed(1)); |
188 | | - observe(working); |
189 | | - observe(completed(2), { ...visible, quiet: true }); |
190 | | - vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS * 2); |
191 | | - expect(notify).not.toHaveBeenCalled(); |
192 | | - }); |
193 | 113 | }); |
0 commit comments