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

ThreadNotificationCoordinator.logic.test.ts

193 lines6.5 KB

1import { WORKING_EXIT_GRACE_MS } from "@t3tools/client-runtime/state/thread-inbox";
2import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
3​
4import {
5 createThreadNotificationGate,
6 type ThreadNotificationObservation,
7} from "./ThreadNotificationCoordinator.logic";
8​
9const working: ThreadNotificationObservation = {
10 attention: null,
11 settled: false,
12 completedAt: null,
13};
14const completed = (at: number): ThreadNotificationObservation => ({
15 attention: null,
16 settled: true,
17 completedAt: at,
18});
19const visible = { archived: false, quiet: false };
20​
21function harness() {
22 const notify = vi.fn();
23 const gate = createThreadNotificationGate<string>(notify);
24 let prior: ThreadNotificationObservation | undefined;
25 const observe = (next: ThreadNotificationObservation, options = visible) => {
26 gate.observe("thread", prior, next, options);
27 prior = next;
28 };
29 return { gate, notify, observe };
31​
32describe("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", () => {
51 const { notify, observe } = harness();
52 observe(working);
53 observe(completed(1));
54 vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS - 1);
55 expect(notify).not.toHaveBeenCalled();
56 vi.advanceTimersByTime(1);
57 expect(notify.mock.calls).toEqual([["thread", "completion"]]);
58 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 observe(working);
66 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"]]);
75 });
76​
77 it("notifies one completion across continuation gaps", () => {
78 const { notify, observe } = harness();
79 observe(working);
80 observe(completed(1));
81 vi.advanceTimersByTime(300);
82 observe(working);
83 vi.advanceTimersByTime(5_000);
84 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"]]);
91 });
92​
93 it("keeps a completion interrupted by attention after a working gap from firing", () => {
94 const { notify, observe } = harness();
95 observe(working);
96 observe(completed(1));
97 vi.advanceTimersByTime(500);
98 observe(working);
99 observe({ attention: "run-1:approval", settled: false, completedAt: null });
100 vi.advanceTimersByTime(60_000);
101 observe(completed(1));
102 vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS * 2);
103 expect(notify.mock.calls).toEqual([["thread", "input"]]);
104 });
105​
106 it("adds nothing when attention follows a completion that already fired", () => {
107 const { notify, observe } = harness();
108 observe(working);
109 observe(completed(1));
110 vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS);
111 observe({ attention: "run-1:limited", settled: false, completedAt: null });
112 observe(completed(1));
113 vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS * 2);
114 expect(notify.mock.calls).toEqual([
115 ["thread", "completion"],
116 ["thread", "input"],
117 ]);
118 });
119​
120 it("still notifies a newer completion after attention", () => {
121 const { notify, observe } = harness();
122 observe(working);
123 observe(completed(1));
124 vi.advanceTimersByTime(500);
125 observe({ attention: "run-2:approval", settled: false, completedAt: null });
126 observe(working);
127 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 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);
150 observe(working);
151 observe(completed(1));
152 vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS);
153 expect(notify.mock.calls).toEqual([["thread", "completion"]]);
154 });
155​
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(),
175 ]) {
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();
182 }
183 });
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});