Open History to see the last commit
1import { scopeThreadRef } from "@t3tools/client-runtime/environment";
2import { EnvironmentId, ThreadId } from "@t3tools/contracts";
3import { describe, expect, it, vi } from "vite-plus/test";
4
5import { selectThreadRightPanelState, useRightPanelStore } from "../rightPanelStore";
6import { useTerminalUiStateStore } from "../terminalUiStateStore";
7import {
8 consumePendingTerminalFocus,
9 focusActiveDrawerTerminal,
10 focusActivePanelTerminal,
11 focusPanelTerminalAfterClose,
12 focusTerminal,
13 focusTerminalOnNavigation,
14 focusTerminalOnThreadEnter,
15 registerTerminalFocusTarget,
16} from "./terminalFocus";
17
18const threadRef = (threadId: string) =>
19 scopeThreadRef(EnvironmentId.make("env"), ThreadId.make(threadId));
20
21describe("focusTerminal", () => {
22 it("focuses a mounted target right away", () => {
23 const ref = threadRef("mounted");
24 const focus = vi.fn(() => true);
25 const unregister = registerTerminalFocusTarget({ threadRef: ref, terminalId: "a", focus });
26
27 focusTerminal(ref, "a");
28
29 expect(focus).toHaveBeenCalledTimes(1);
30 unregister();
31 });
32
33 it("waits for the requested terminal to mount, then consumes the request", () => {
34 const ref = threadRef("pending");
35 focusTerminal(ref, "b");
36
37 const other = vi.fn(() => true);
38 const unregisterOther = registerTerminalFocusTarget({
39 threadRef: ref,
40 terminalId: "a",
41 focus: other,
42 });
43 const target = vi.fn(() => true);
44 const unregisterTarget = registerTerminalFocusTarget({
45 threadRef: ref,
46 terminalId: "b",
47 focus: target,
48 });
49 unregisterTarget();
50 const remount = vi.fn(() => true);
51 const unregisterRemount = registerTerminalFocusTarget({
52 threadRef: ref,
53 terminalId: "b",
54 focus: remount,
55 });
56
57 expect(other).not.toHaveBeenCalled();
58 expect(target).toHaveBeenCalledTimes(1);
59 expect(remount).not.toHaveBeenCalled();
60 unregisterOther();
61 unregisterRemount();
62 });
63
64 it("keeps only the newest request for a thread", () => {
65 const ref = threadRef("newest");
66 focusTerminal(ref, "a");
67 focusTerminal(ref, "b");
68
69 const first = vi.fn(() => true);
70 const unregisterFirst = registerTerminalFocusTarget({
71 threadRef: ref,
72 terminalId: "a",
73 focus: first,
74 });
75 const second = vi.fn(() => true);
76 const unregisterSecond = registerTerminalFocusTarget({
77 threadRef: ref,
78 terminalId: "b",
79 focus: second,
80 });
81
82 expect(first).not.toHaveBeenCalled();
83 expect(second).toHaveBeenCalledTimes(1);
84 unregisterFirst();
85 unregisterSecond();
86 });
87
88 it("keeps the request when a mounted target cannot take focus", () => {
89 const ref = threadRef("hidden");
90 const hidden = vi.fn(() => false);
91 const unregisterHidden = registerTerminalFocusTarget({
92 threadRef: ref,
93 terminalId: "a",
94 focus: hidden,
95 });
96
97 focusTerminal(ref, "a");
98 unregisterHidden();
99 const shown = vi.fn(() => true);
100 const unregisterShown = registerTerminalFocusTarget({
101 threadRef: ref,
102 terminalId: "a",
103 focus: shown,
104 });
105
106 expect(hidden).toHaveBeenCalledTimes(1);
107 expect(shown).toHaveBeenCalledTimes(1);
108 unregisterShown();
109 });
110
111 it("focuses a registered hidden target once it is shown", () => {
112 const ref = threadRef("shown-later");
113 let canFocus = false;
114 const focus = vi.fn(() => canFocus);
115 const unregister = registerTerminalFocusTarget({ threadRef: ref, terminalId: "a", focus });
116
117 focusTerminal(ref, "a");
118 canFocus = true;
119 consumePendingTerminalFocus(ref, "a");
120 consumePendingTerminalFocus(ref, "a");
121
122 expect(focus).toHaveBeenCalledTimes(2);
123 unregister();
124 });
125
126 it("drops a parked request once a newer one targets another thread", () => {
127 const parked = threadRef("parked");
128 const hidden = vi.fn(() => false);
129 const unregisterHidden = registerTerminalFocusTarget({
130 threadRef: parked,
131 terminalId: "a",
132 focus: hidden,
133 });
134 focusTerminal(parked, "a");
135
136 const other = threadRef("other");
137 const unregisterOther = registerTerminalFocusTarget({
138 threadRef: other,
139 terminalId: "b",
140 focus: () => true,
141 });
142 focusTerminal(other, "b");
143 unregisterHidden();
144 const remount = vi.fn(() => true);
145 const unregisterRemount = registerTerminalFocusTarget({
146 threadRef: parked,
147 terminalId: "a",
148 focus: remount,
149 });
150
151 expect(remount).not.toHaveBeenCalled();
152 unregisterOther();
153 unregisterRemount();
154 });
155
156 it("drops a parked request when the thread's drawer closes", () => {
157 const ref = threadRef("closed");
158 useTerminalUiStateStore.getState().ensureTerminal(ref, "a", { open: true, active: true });
159 focusActiveDrawerTerminal(ref);
160 useTerminalUiStateStore.getState().setTerminalOpen(ref, false);
161 focusActiveDrawerTerminal(ref);
162
163 const focus = vi.fn(() => true);
164 const unregister = registerTerminalFocusTarget({ threadRef: ref, terminalId: "a", focus });
165
166 expect(focus).not.toHaveBeenCalled();
167 unregister();
168 });
169
170 it("entering a thread with its drawer open focuses the active drawer terminal", () => {
171 const ref = threadRef("entered");
172 useTerminalUiStateStore.getState().ensureTerminal(ref, "a", { open: true, active: true });
173 const focus = vi.fn(() => true);
174 const unregister = registerTerminalFocusTarget({ threadRef: ref, terminalId: "a", focus });
175
176 const leave = focusTerminalOnThreadEnter(ref);
177
178 expect(focus).toHaveBeenCalledTimes(1);
179 leave();
180 unregister();
181 });
182
183 it("entering a thread keeps the request its navigating handler made", () => {
184 const ref = threadRef("revealed");
185 useTerminalUiStateStore.getState().ensureTerminal(ref, "drawer", { open: true, active: true });
186 focusTerminalOnNavigation(ref, "panel");
187
188 // React commit order: the new thread's viewports attach and take the
189 // request before ChatView's thread-enter effect runs.
190 const drawer = vi.fn(() => true);
191 const unregisterDrawer = registerTerminalFocusTarget({
192 threadRef: ref,
193 terminalId: "drawer",
194 focus: drawer,
195 });
196 const panel = vi.fn(() => true);
197 const unregisterPanel = registerTerminalFocusTarget({
198 threadRef: ref,
199 terminalId: "panel",
200 focus: panel,
201 });
202 const leave = focusTerminalOnThreadEnter(ref);
203
204 expect(drawer).not.toHaveBeenCalled();
205 expect(panel).toHaveBeenCalledTimes(1);
206 leave();
207 unregisterDrawer();
208 unregisterPanel();
209 });
210
211 it("re-entering a thread after in-thread terminal actions focuses the drawer", () => {
212 const ref = threadRef("re-entered");
213 useTerminalUiStateStore.getState().ensureTerminal(ref, "drawer", { open: true, active: true });
214 const drawer = vi.fn(() => true);
215 const unregisterDrawer = registerTerminalFocusTarget({
216 threadRef: ref,
217 terminalId: "drawer",
218 focus: drawer,
219 });
220
221 const leaveFirst = focusTerminalOnThreadEnter(ref);
222 focusTerminal(ref, "drawer");
223 focusTerminalOnNavigation(ref, "drawer");
224 leaveFirst();
225 drawer.mockClear();
226 const leaveSecond = focusTerminalOnThreadEnter(ref);
227
228 expect(drawer).toHaveBeenCalledTimes(1);
229 leaveSecond();
230 unregisterDrawer();
231 });
232
233 it("a navigating handler's request only outranks the next thread entry", () => {
234 const ref = threadRef("revisited");
235 useTerminalUiStateStore.getState().ensureTerminal(ref, "drawer", { open: true, active: true });
236 focusTerminalOnNavigation(ref, "panel");
237 focusTerminalOnThreadEnter(threadRef("elsewhere"))();
238
239 const drawer = vi.fn(() => true);
240 const unregisterDrawer = registerTerminalFocusTarget({
241 threadRef: ref,
242 terminalId: "drawer",
243 focus: drawer,
244 });
245 const leave = focusTerminalOnThreadEnter(ref);
246
247 expect(drawer).toHaveBeenCalledTimes(1);
248 leave();
249 unregisterDrawer();
250 });
251
252 it("focuses the panel terminal a closed surface falls back to", () => {
253 const ref = threadRef("panel-fallback");
254 const panel = useRightPanelStore.getState();
255 panel.openTerminal(ref, "a");
256 panel.open(ref, "diff");
257 const { activeSurfaceId: diffSurfaceId } = selectThreadRightPanelState(
258 useRightPanelStore.getState().byThreadKey,
259 ref,
260 );
261 const focus = vi.fn(() => true);
262 const unregister = registerTerminalFocusTarget({ threadRef: ref, terminalId: "a", focus });
263 expect(diffSurfaceId).not.toBeNull();
264
265 useRightPanelStore.getState().closeSurface(ref, diffSurfaceId!);
266 focusActivePanelTerminal(ref);
267
268 expect(focus).toHaveBeenCalledTimes(1);
269 unregister();
270 });
271
272 it("closing a surface's last terminal focuses the terminal surface it falls back to", () => {
273 const ref = threadRef("panel-terminal-fallback");
274 const panel = useRightPanelStore.getState();
275 panel.openTerminal(ref, "b");
276 panel.openTerminal(ref, "a");
277 const surfaceA = selectThreadRightPanelState(
278 useRightPanelStore.getState().byThreadKey,
279 ref,
280 ).activeSurfaceId!;
281 const focusB = vi.fn(() => true);
282 const unregister = registerTerminalFocusTarget({
283 threadRef: ref,
284 terminalId: "b",
285 focus: focusB,
286 });
287
288 panel.closeTerminal(ref, surfaceA, "a");
289 focusActivePanelTerminal(ref);
290
291 expect(focusB).toHaveBeenCalledTimes(1);
292 unregister();
293 });
294
295 it("refocuses the panel terminal only when closing changed the active surface", () => {
296 const ref = threadRef("panel-after-close");
297 const panel = useRightPanelStore.getState();
298 panel.openTerminal(ref, "a");
299 const terminalSurfaceId = selectThreadRightPanelState(
300 useRightPanelStore.getState().byThreadKey,
301 ref,
302 ).activeSurfaceId!;
303 panel.open(ref, "diff");
304 const diffSurfaceId = selectThreadRightPanelState(
305 useRightPanelStore.getState().byThreadKey,
306 ref,
307 ).activeSurfaceId!;
308 const focus = vi.fn(() => true);
309 const unregister = registerTerminalFocusTarget({ threadRef: ref, terminalId: "a", focus });
310
311 panel.activateSurface(ref, terminalSurfaceId);
312 panel.closeSurface(ref, diffSurfaceId);
313 focusPanelTerminalAfterClose(ref, terminalSurfaceId);
314 expect(focus).not.toHaveBeenCalled();
315
316 panel.open(ref, "diff");
317 const reopenedDiffId = selectThreadRightPanelState(
318 useRightPanelStore.getState().byThreadKey,
319 ref,
320 ).activeSurfaceId!;
321 panel.closeSurface(ref, reopenedDiffId);
322 focusPanelTerminalAfterClose(ref, reopenedDiffId);
323 expect(focus).toHaveBeenCalledTimes(1);
324 unregister();
325 });
326});