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

QuitHold.test.ts

420 lines16.2 KB

1import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
2​
3import {
4 makeQuitShortcutHandler,
5 QUIT_DOUBLE_PRESS_MS,
6 QUIT_HOLD_DURATION_MS,
7 QUIT_HOLD_RELEASE_GRACE_MS,
8} from "./QuitHold.ts";
9import type { QuitHoldKeyInput } from "./QuitHold.ts";
10import type { QuitConfirmationMode, QuitShortcutHintEvent } from "@t3tools/contracts";
11​
12const HOLD_DOWN = { state: "down", mode: "hold" } as const;
13const DOUBLE_CLICK_DOWN = { state: "down", mode: "double-click" } as const;
14const UP = { state: "up" } as const;
15​
16function makeInput(overrides: Partial<QuitHoldKeyInput>): QuitHoldKeyInput {
17 return {
18 type: "keyDown",
19 key: "q",
20 meta: true,
21 control: false,
22 alt: false,
23 shift: false,
24 isAutoRepeat: false,
25 ...overrides,
26 };
28​
29function makeHarness(options?: {
30 mode?: QuitConfirmationMode;
31 platform?: NodeJS.Platform;
32 getMode?: () => Promise<QuitConfirmationMode>;
33}) {
34 const notifications: Array<QuitShortcutHintEvent> = [];
35 const concealWindow = vi.fn();
36 const quit = vi.fn();
37 const handler = makeQuitShortcutHandler({
38 platform: options?.platform ?? "darwin",
39 getMode: options?.getMode ?? (() => Promise.resolve(options?.mode ?? "hold")),
40 notify: (event) => notifications.push(event),
41 concealWindow,
42 quit,
43 });
44 const preventDefault = vi.fn();
45 const send = async (input: QuitHoldKeyInput) => {
46 handler({ preventDefault }, input);
47 // Let the getMode promise settle.
48 await Promise.resolve();
49 await Promise.resolve();
50 };
51 // Simulates the OS auto-repeating the held shortcut every `intervalMs`.
52 const holdFor = async (
53 durationMs: number,
54 repeatOverrides: Partial<QuitHoldKeyInput> = {},
55 intervalMs = 100,
56 ) => {
57 for (let elapsed = 0; elapsed < durationMs; elapsed += intervalMs) {
58 vi.advanceTimersByTime(intervalMs);
59 await send(makeInput({ isAutoRepeat: true, ...repeatOverrides }));
60 }
61 };
62 return { notifications, concealWindow, quit, preventDefault, send, holdFor };
64​
65describe("makeQuitShortcutHandler", () => {
66 beforeEach(() => {
67 vi.useFakeTimers();
68 });
69 afterEach(() => {
70 vi.useRealTimers();
71 });
72​
73 it("shows the hint on a tap without quitting, even when the release is never seen", async () => {
74 // macOS suppresses the letter's keyUp while Cmd is held, so a tap may
75 // produce no keyUp at all. Quit must still not fire.
76 const harness = makeHarness();
77 await harness.send(makeInput({}));
78 expect(harness.preventDefault).toHaveBeenCalledTimes(1);
79 expect(harness.notifications).toEqual([HOLD_DOWN]);
80​
81 vi.advanceTimersByTime(QUIT_HOLD_DURATION_MS + QUIT_HOLD_RELEASE_GRACE_MS);
82 expect(harness.quit).not.toHaveBeenCalled();
83 // The watchdog dismisses the hint once the press is clearly over.
84 expect(harness.notifications).toEqual([HOLD_DOWN, UP]);
85 });
86​
87 it("conceals a completed hold, then quits after release", async () => {
88 const harness = makeHarness();
89 await harness.send(makeInput({}));
90 await harness.holdFor(QUIT_HOLD_DURATION_MS + 200);
91 expect(harness.concealWindow).toHaveBeenCalledTimes(1);
92 expect(harness.quit).not.toHaveBeenCalled();
93 await harness.send(makeInput({ type: "keyUp", key: "Meta", meta: false }));
94 expect(harness.quit).not.toHaveBeenCalled();
95 vi.advanceTimersByTime(QUIT_HOLD_RELEASE_GRACE_MS);
96 expect(harness.quit).toHaveBeenCalledTimes(1);
97 expect(harness.notifications).toEqual([HOLD_DOWN, UP]);
98 });
99​
100 it("keeps a concealed hold committed when another key is pressed", async () => {
101 const harness = makeHarness();
102 await harness.send(makeInput({}));
103 await harness.holdFor(QUIT_HOLD_DURATION_MS);
104​
105 await harness.send(makeInput({ key: "Shift", shift: true }));
106 expect(harness.concealWindow).toHaveBeenCalledTimes(1);
107 expect(harness.quit).not.toHaveBeenCalled();
108​
109 vi.advanceTimersByTime(QUIT_HOLD_RELEASE_GRACE_MS);
110 expect(harness.quit).toHaveBeenCalledTimes(1);
111 });
112​
113 it("keeps a concealed hold committed through a fresh Cmd+Q press", async () => {
114 const harness = makeHarness();
115 await harness.send(makeInput({}));
116 await harness.holdFor(QUIT_HOLD_DURATION_MS);
117​
118 await harness.send(makeInput({}));
119 expect(harness.concealWindow).toHaveBeenCalledTimes(1);
120 expect(harness.quit).not.toHaveBeenCalled();
121​
122 await harness.send(makeInput({ type: "keyUp" }));
123 expect(harness.quit).toHaveBeenCalledTimes(1);
124 });
125​
126 it("quits when a completed hold goes quiet without release events", async () => {
127 const harness = makeHarness();
128 await harness.send(makeInput({}));
129 await harness.holdFor(QUIT_HOLD_DURATION_MS + QUIT_HOLD_RELEASE_GRACE_MS * 2);
130​
131 // If neither keyUp reaches the handler, continued repeats must keep the
132 // app alive. Once they stop, the quiet period is the release signal.
133 expect(harness.quit).not.toHaveBeenCalled();
134 vi.advanceTimersByTime(QUIT_HOLD_RELEASE_GRACE_MS);
135​
136 expect(harness.quit).toHaveBeenCalledTimes(1);
137 expect(harness.notifications).toEqual([HOLD_DOWN, UP]);
138 });
139​
140 it("waits for slow repeats to stop before quitting", async () => {
141 const harness = makeHarness();
142 await harness.send(makeInput({}));
143​
144 vi.advanceTimersByTime(300);
145 await harness.send(makeInput({ isAutoRepeat: true }));
146 vi.advanceTimersByTime(900);
147 await harness.send(makeInput({ isAutoRepeat: true }));
148​
149 vi.advanceTimersByTime(QUIT_HOLD_RELEASE_GRACE_MS);
150 expect(harness.quit).not.toHaveBeenCalled();
151​
152 vi.advanceTimersByTime(300);
153 await harness.send(makeInput({ isAutoRepeat: true }));
154 vi.advanceTimersByTime(1_799);
155 expect(harness.quit).not.toHaveBeenCalled();
156 vi.advanceTimersByTime(1);
157 expect(harness.quit).toHaveBeenCalledTimes(1);
158 });
159​
160 it("uses the initial repeat delay when the first repeat completes the hold", async () => {
161 const harness = makeHarness();
162 await harness.send(makeInput({}));
163​
164 vi.advanceTimersByTime(QUIT_HOLD_DURATION_MS + 100);
165 await harness.send(makeInput({ isAutoRepeat: true }));
166​
167 vi.advanceTimersByTime(QUIT_HOLD_RELEASE_GRACE_MS);
168 expect(harness.quit).not.toHaveBeenCalled();
169 vi.advanceTimersByTime(1_999);
170 expect(harness.quit).not.toHaveBeenCalled();
171 vi.advanceTimersByTime(1);
172 expect(harness.quit).toHaveBeenCalledTimes(1);
173 });
174​
175 it("waits for Q release when Cmd is released first", async () => {
176 const harness = makeHarness();
177 await harness.send(makeInput({}));
178 await harness.holdFor(QUIT_HOLD_DURATION_MS + 200);
179 await harness.send(makeInput({ type: "keyUp", key: "Meta", meta: false }));
180 harness.preventDefault.mockClear();
181 await harness.send(makeInput({ meta: false, isAutoRepeat: true }));
182 expect(harness.preventDefault).toHaveBeenCalledTimes(1);
183 vi.advanceTimersByTime(QUIT_HOLD_RELEASE_GRACE_MS * 2);
184 expect(harness.quit).not.toHaveBeenCalled();
185 await harness.send(makeInput({ type: "keyUp", meta: false }));
186 expect(harness.quit).toHaveBeenCalledTimes(1);
187 });
188​
189 it("does not quit when the hold stops before the duration", async () => {
190 const harness = makeHarness();
191 await harness.send(makeInput({}));
192 await harness.holdFor(500);
193 await harness.send(makeInput({ type: "keyUp" }));
194 expect(harness.notifications).toEqual([HOLD_DOWN, UP]);
195 vi.advanceTimersByTime((QUIT_HOLD_DURATION_MS + QUIT_HOLD_RELEASE_GRACE_MS) * 2);
196 expect(harness.concealWindow).not.toHaveBeenCalled();
197 expect(harness.quit).not.toHaveBeenCalled();
198 });
199​
200 it("cancels the hold when the modifier is released first", async () => {
201 const harness = makeHarness();
202 await harness.send(makeInput({}));
203 await harness.send(makeInput({ type: "keyUp", key: "Meta", meta: false }));
204 expect(harness.notifications).toEqual([HOLD_DOWN, UP]);
205 vi.advanceTimersByTime((QUIT_HOLD_DURATION_MS + QUIT_HOLD_RELEASE_GRACE_MS) * 2);
206 expect(harness.quit).not.toHaveBeenCalled();
207 });
208​
209 it("quits without showing a hint in direct mode", async () => {
210 const harness = makeHarness({ mode: "direct" });
211 await harness.send(makeInput({}));
212 expect(harness.concealWindow).not.toHaveBeenCalled();
213 expect(harness.quit).toHaveBeenCalledTimes(1);
214 expect(harness.notifications).toEqual([]);
215 });
216​
217 it("honors direct mode when the key is released before its mode read settles", async () => {
218 let resolveMode: ((mode: QuitConfirmationMode) => void) | undefined;
219 const harness = makeHarness({
220 getMode: () =>
221 new Promise((resolve) => {
222 resolveMode = resolve;
223 }),
224 });
225 await harness.send(makeInput({}));
226 await harness.send(makeInput({ type: "keyUp" }));
227​
228 resolveMode?.("direct");
229 await Promise.resolve();
230 await Promise.resolve();
231​
232 expect(harness.quit).toHaveBeenCalledTimes(1);
233 expect(harness.notifications).toEqual([]);
234 });
235​
236 it("does not arm hold mode after a released key's mode read settles", async () => {
237 let resolveMode: ((mode: QuitConfirmationMode) => void) | undefined;
238 const harness = makeHarness({
239 getMode: () =>
240 new Promise((resolve) => {
241 resolveMode = resolve;
242 }),
243 });
244 await harness.send(makeInput({}));
245 await harness.send(makeInput({ type: "keyUp" }));
246​
247 resolveMode?.("hold");
248 await Promise.resolve();
249 await Promise.resolve();
250​
251 expect(harness.quit).not.toHaveBeenCalled();
252 expect(harness.notifications).toEqual([]);
253 });
254​
255 it("honors a quick double press when both key releases beat their mode reads", async () => {
256 const resolvers: Array<(mode: QuitConfirmationMode) => void> = [];
257 const harness = makeHarness({
258 getMode: () => new Promise((resolve) => resolvers.push(resolve)),
259 });
260 await harness.send(makeInput({}));
261 await harness.send(makeInput({ type: "keyUp" }));
262 vi.advanceTimersByTime(QUIT_DOUBLE_PRESS_MS - 100);
263 await harness.send(makeInput({}));
264 await harness.send(makeInput({ type: "keyUp" }));
265​
266 resolvers[1]?.("double-click");
267 await Promise.resolve();
268 await Promise.resolve();
269​
270 expect(harness.quit).toHaveBeenCalledTimes(1);
271 expect(harness.notifications).toEqual([]);
272 });
273​
274 it("discards a stale mode resolution from a superseded press", async () => {
275 // Press #1's mode is still pending when the user releases and
276 // presses again; its late resolution must not act for press #2.
277 const resolvers: Array<(mode: QuitConfirmationMode) => void> = [];
278 const harness = makeHarness({
279 getMode: () => new Promise((resolve) => resolvers.push(resolve)),
280 });
281 await harness.send(makeInput({}));
282 await harness.send(makeInput({ type: "keyUp" }));
283 // Outside the double-press window, so the second press starts a new hold.
284 vi.advanceTimersByTime(QUIT_DOUBLE_PRESS_MS + 100);
285 await harness.send(makeInput({}));
286 expect(resolvers).toHaveLength(2);
287​
288 // Press #1 resolves late with "direct". It must not quit press #2.
289 resolvers[0]?.("direct");
290 await Promise.resolve();
291 await Promise.resolve();
292 expect(harness.quit).not.toHaveBeenCalled();
293​
294 // Press #2 resolves to hold and completes the gesture.
295 resolvers[1]?.("hold");
296 await harness.holdFor(QUIT_HOLD_DURATION_MS + 200);
297 await harness.send(makeInput({ type: "keyUp" }));
298 expect(harness.quit).toHaveBeenCalledTimes(1);
299 });
300​
301 it("quits on a quick double press in double-click mode when the first release is unseen", async () => {
302 const harness = makeHarness({ mode: "double-click" });
303 await harness.send(makeInput({}));
304 vi.advanceTimersByTime(QUIT_DOUBLE_PRESS_MS - 100);
305 await harness.send(makeInput({}));
306 expect(harness.concealWindow).not.toHaveBeenCalled();
307 expect(harness.quit).toHaveBeenCalledTimes(1);
308 expect(harness.notifications).toEqual([DOUBLE_CLICK_DOWN, UP]);
309 });
310​
311 it("keeps the double-press hint visible after key release until the window ends", async () => {
312 const harness = makeHarness({ mode: "double-click" });
313 await harness.send(makeInput({}));
314 vi.advanceTimersByTime(100);
315 await harness.send(makeInput({ type: "keyUp" }));
316 expect(harness.notifications).toEqual([DOUBLE_CLICK_DOWN]);
317​
318 vi.advanceTimersByTime(QUIT_DOUBLE_PRESS_MS - 101);
319 expect(harness.notifications).toEqual([DOUBLE_CLICK_DOWN]);
320 vi.advanceTimersByTime(1);
321 expect(harness.notifications).toEqual([DOUBLE_CLICK_DOWN, UP]);
322 });
323​
324 it("accepts a second full shortcut after the modifier is released and pressed again", async () => {
325 const harness = makeHarness({ mode: "double-click" });
326 await harness.send(makeInput({}));
327 await harness.send(makeInput({ type: "keyUp" }));
328 await harness.send(makeInput({ type: "keyUp", key: "Meta", meta: false }));
329 vi.advanceTimersByTime(100);
330​
331 await harness.send(makeInput({ key: "Meta" }));
332 await harness.send(makeInput({}));
333​
334 expect(harness.quit).toHaveBeenCalledTimes(1);
335 expect(harness.notifications).toEqual([DOUBLE_CLICK_DOWN, UP]);
336 });
337​
338 it("expires a delayed double-press hint from keydown rather than mode resolution", async () => {
339 let resolveMode: ((mode: QuitConfirmationMode) => void) | undefined;
340 const harness = makeHarness({
341 getMode: () =>
342 new Promise((resolve) => {
343 resolveMode = resolve;
344 }),
345 });
346 await harness.send(makeInput({}));
347 vi.advanceTimersByTime(100);
348 await harness.send(makeInput({ type: "keyUp" }));
349 vi.advanceTimersByTime(100);
350 resolveMode?.("double-click");
351 await Promise.resolve();
352 await Promise.resolve();
353 expect(harness.notifications).toEqual([DOUBLE_CLICK_DOWN]);
354​
355 vi.advanceTimersByTime(QUIT_DOUBLE_PRESS_MS - 201);
356 expect(harness.notifications).toEqual([DOUBLE_CLICK_DOWN]);
357 vi.advanceTimersByTime(1);
358 expect(harness.notifications).toEqual([DOUBLE_CLICK_DOWN, UP]);
359 });
360​
361 it("treats two slow presses as separate attempts in double-click mode", async () => {
362 const harness = makeHarness({ mode: "double-click" });
363 await harness.send(makeInput({}));
364 await harness.send(makeInput({ type: "keyUp" }));
365 vi.advanceTimersByTime(QUIT_DOUBLE_PRESS_MS + 100);
366 await harness.send(makeInput({}));
367 expect(harness.quit).not.toHaveBeenCalled();
368 expect(harness.notifications).toEqual([DOUBLE_CLICK_DOWN, UP, DOUBLE_CLICK_DOWN]);
369 });
370​
371 it("does not treat two quick presses as a quit in hold mode", async () => {
372 const harness = makeHarness();
373 await harness.send(makeInput({}));
374 await harness.send(makeInput({ type: "keyUp" }));
375 vi.advanceTimersByTime(QUIT_DOUBLE_PRESS_MS - 100);
376 await harness.send(makeInput({}));
377 expect(harness.quit).not.toHaveBeenCalled();
378 expect(harness.notifications).toEqual([HOLD_DOWN, UP, HOLD_DOWN]);
379 });
380​
381 it("cancels the hold when another key interrupts it", async () => {
382 const harness = makeHarness();
383 await harness.send(makeInput({}));
384 await harness.holdFor(500);
385 // Shift pressed mid-hold breaks the gesture...
386 await harness.send(makeInput({ shift: true }));
387 expect(harness.notifications).toEqual([HOLD_DOWN, UP]);
388 // ...so later repeats past the threshold must not quit.
389 await harness.holdFor(QUIT_HOLD_DURATION_MS);
390 expect(harness.quit).not.toHaveBeenCalled();
391 });
392​
393 it("does not count an interrupted press toward a double press", async () => {
394 const harness = makeHarness({ mode: "double-click" });
395 await harness.send(makeInput({}));
396 await harness.send(makeInput({ shift: true }));
397 // A fresh press right after the interruption starts a new attempt.
398 await harness.send(makeInput({}));
399 expect(harness.quit).not.toHaveBeenCalled();
400 expect(harness.notifications).toEqual([DOUBLE_CLICK_DOWN, UP, DOUBLE_CLICK_DOWN]);
401 });
402​
403 it("ignores other shortcuts", async () => {
404 const harness = makeHarness();
405 await harness.send(makeInput({ key: "w" }));
406 await harness.send(makeInput({ shift: true }));
407 await harness.send(makeInput({ meta: false }));
408 expect(harness.preventDefault).not.toHaveBeenCalled();
409 expect(harness.notifications).toEqual([]);
410 });
411​
412 it("uses control on non-mac platforms", async () => {
413 const harness = makeHarness({ platform: "linux" });
414 await harness.send(makeInput({ meta: false, control: true }));
415 expect(harness.preventDefault).toHaveBeenCalledTimes(1);
416 await harness.holdFor(QUIT_HOLD_DURATION_MS + 200, { meta: false, control: true });
417 await harness.send(makeInput({ type: "keyUp", meta: false, control: true }));
418 expect(harness.quit).toHaveBeenCalledTimes(1);
419 });
420});