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

QuitHold.test.ts

459 lines17.5 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.each(["direct", "hold", "double-click"] as const)(
256 "quits on a quick second press without waiting for a pending %s mode read",
257 async (mode) => {
258 const resolvers: Array<(mode: QuitConfirmationMode) => void> = [];
259 const harness = makeHarness({
260 getMode: () => new Promise((resolve) => resolvers.push(resolve)),
261 });
262 await harness.send(makeInput({}));
263 await harness.send(makeInput({ type: "keyUp" }));
264 vi.advanceTimersByTime(QUIT_DOUBLE_PRESS_MS - 100);
265 await harness.send(makeInput({}));
266​
267 expect(harness.quit).toHaveBeenCalledTimes(1);
268 expect(harness.notifications).toEqual([]);
269​
270 await harness.send(makeInput({ type: "keyUp" }));
271​
272 resolvers[0]?.(mode);
273 await Promise.resolve();
274 await Promise.resolve();
275​
276 expect(harness.quit).toHaveBeenCalledTimes(1);
277 expect(harness.notifications).toEqual([]);
278 },
279 );
280​
281 it("discards a stale mode resolution from a superseded press", async () => {
282 // Press #1's mode is still pending when the user releases and
283 // presses again; its late resolution must not act for press #2.
284 const resolvers: Array<(mode: QuitConfirmationMode) => void> = [];
285 const harness = makeHarness({
286 getMode: () => new Promise((resolve) => resolvers.push(resolve)),
287 });
288 await harness.send(makeInput({}));
289 await harness.send(makeInput({ type: "keyUp" }));
290 // Outside the double-press window, so the second press starts a new hold.
291 vi.advanceTimersByTime(QUIT_DOUBLE_PRESS_MS + 100);
292 await harness.send(makeInput({}));
293 expect(resolvers).toHaveLength(2);
294​
295 // Press #1 resolves late with "direct". It must not quit press #2.
296 resolvers[0]?.("direct");
297 await Promise.resolve();
298 await Promise.resolve();
299 expect(harness.quit).not.toHaveBeenCalled();
300​
301 // Press #2 resolves to hold and completes the gesture.
302 resolvers[1]?.("hold");
303 await harness.holdFor(QUIT_HOLD_DURATION_MS + 200);
304 await harness.send(makeInput({ type: "keyUp" }));
305 expect(harness.quit).toHaveBeenCalledTimes(1);
306 });
307​
308 it("quits on a quick double press in double-click mode when the first release is unseen", async () => {
309 const harness = makeHarness({ mode: "double-click" });
310 await harness.send(makeInput({}));
311 vi.advanceTimersByTime(QUIT_DOUBLE_PRESS_MS - 100);
312 await harness.send(makeInput({}));
313 expect(harness.concealWindow).not.toHaveBeenCalled();
314 expect(harness.quit).toHaveBeenCalledTimes(1);
315 expect(harness.notifications).toEqual([DOUBLE_CLICK_DOWN, UP]);
316 });
317​
318 it("keeps the double-press hint visible after key release until the window ends", async () => {
319 const harness = makeHarness({ mode: "double-click" });
320 await harness.send(makeInput({}));
321 vi.advanceTimersByTime(100);
322 await harness.send(makeInput({ type: "keyUp" }));
323 expect(harness.notifications).toEqual([DOUBLE_CLICK_DOWN]);
324​
325 vi.advanceTimersByTime(QUIT_DOUBLE_PRESS_MS - 101);
326 expect(harness.notifications).toEqual([DOUBLE_CLICK_DOWN]);
327 vi.advanceTimersByTime(1);
328 expect(harness.notifications).toEqual([DOUBLE_CLICK_DOWN, UP]);
329 });
330​
331 it("accepts a second full shortcut after the modifier is released and pressed again", async () => {
332 const harness = makeHarness({ mode: "double-click" });
333 await harness.send(makeInput({}));
334 await harness.send(makeInput({ type: "keyUp" }));
335 await harness.send(makeInput({ type: "keyUp", key: "Meta", meta: false }));
336 vi.advanceTimersByTime(100);
337​
338 await harness.send(makeInput({ key: "Meta" }));
339 await harness.send(makeInput({}));
340​
341 expect(harness.quit).toHaveBeenCalledTimes(1);
342 expect(harness.notifications).toEqual([DOUBLE_CLICK_DOWN, UP]);
343 });
344​
345 it("expires a delayed double-press hint from keydown rather than mode resolution", async () => {
346 let resolveMode: ((mode: QuitConfirmationMode) => void) | undefined;
347 const harness = makeHarness({
348 getMode: () =>
349 new Promise((resolve) => {
350 resolveMode = resolve;
351 }),
352 });
353 await harness.send(makeInput({}));
354 vi.advanceTimersByTime(100);
355 await harness.send(makeInput({ type: "keyUp" }));
356 vi.advanceTimersByTime(100);
357 resolveMode?.("double-click");
358 await Promise.resolve();
359 await Promise.resolve();
360 expect(harness.notifications).toEqual([DOUBLE_CLICK_DOWN]);
361​
362 vi.advanceTimersByTime(QUIT_DOUBLE_PRESS_MS - 201);
363 expect(harness.notifications).toEqual([DOUBLE_CLICK_DOWN]);
364 vi.advanceTimersByTime(1);
365 expect(harness.notifications).toEqual([DOUBLE_CLICK_DOWN, UP]);
366 });
367​
368 it("treats two slow presses as separate attempts in double-click mode", async () => {
369 const harness = makeHarness({ mode: "double-click" });
370 await harness.send(makeInput({}));
371 await harness.send(makeInput({ type: "keyUp" }));
372 vi.advanceTimersByTime(QUIT_DOUBLE_PRESS_MS + 100);
373 await harness.send(makeInput({}));
374 expect(harness.quit).not.toHaveBeenCalled();
375 expect(harness.notifications).toEqual([DOUBLE_CLICK_DOWN, UP, DOUBLE_CLICK_DOWN]);
376 });
377​
378 it("quits on a quick second press in hold mode", async () => {
379 const harness = makeHarness();
380 await harness.send(makeInput({}));
381 await harness.send(makeInput({ type: "keyUp" }));
382 vi.advanceTimersByTime(QUIT_DOUBLE_PRESS_MS - 100);
383 await harness.send(makeInput({}));
384 expect(harness.quit).toHaveBeenCalledTimes(1);
385 expect(harness.notifications).toEqual([HOLD_DOWN, UP]);
386 });
387​
388 it("quits on a quick second press in hold mode when the first release is unseen", async () => {
389 const harness = makeHarness();
390 await harness.send(makeInput({}));
391 vi.advanceTimersByTime(QUIT_DOUBLE_PRESS_MS - 100);
392 await harness.send(makeInput({}));
393​
394 expect(harness.concealWindow).not.toHaveBeenCalled();
395 expect(harness.quit).toHaveBeenCalledTimes(1);
396 expect(harness.notifications).toEqual([HOLD_DOWN, UP]);
397 });
398​
399 it("does not count auto-repeat as a second press", async () => {
400 const harness = makeHarness();
401 await harness.send(makeInput({}));
402 await harness.holdFor(QUIT_DOUBLE_PRESS_MS - 100);
403​
404 expect(harness.quit).not.toHaveBeenCalled();
405 expect(harness.notifications).toEqual([HOLD_DOWN]);
406 });
407​
408 it("does not count a released tap after another shortcut interrupts it", async () => {
409 const harness = makeHarness();
410 await harness.send(makeInput({}));
411 await harness.send(makeInput({ type: "keyUp" }));
412 await harness.send(makeInput({ key: "c" }));
413 vi.advanceTimersByTime(100);
414 await harness.send(makeInput({}));
415​
416 expect(harness.quit).not.toHaveBeenCalled();
417 expect(harness.notifications).toEqual([HOLD_DOWN, UP, HOLD_DOWN]);
418 });
419​
420 it("cancels the hold when another key interrupts it", async () => {
421 const harness = makeHarness();
422 await harness.send(makeInput({}));
423 await harness.holdFor(500);
424 // Shift pressed mid-hold breaks the gesture...
425 await harness.send(makeInput({ shift: true }));
426 expect(harness.notifications).toEqual([HOLD_DOWN, UP]);
427 // ...so later repeats past the threshold must not quit.
428 await harness.holdFor(QUIT_HOLD_DURATION_MS);
429 expect(harness.quit).not.toHaveBeenCalled();
430 });
431​
432 it("does not count an interrupted press toward a double press", async () => {
433 const harness = makeHarness({ mode: "double-click" });
434 await harness.send(makeInput({}));
435 await harness.send(makeInput({ shift: true }));
436 // A fresh press right after the interruption starts a new attempt.
437 await harness.send(makeInput({}));
438 expect(harness.quit).not.toHaveBeenCalled();
439 expect(harness.notifications).toEqual([DOUBLE_CLICK_DOWN, UP, DOUBLE_CLICK_DOWN]);
440 });
441​
442 it("ignores other shortcuts", async () => {
443 const harness = makeHarness();
444 await harness.send(makeInput({ key: "w" }));
445 await harness.send(makeInput({ shift: true }));
446 await harness.send(makeInput({ meta: false }));
447 expect(harness.preventDefault).not.toHaveBeenCalled();
448 expect(harness.notifications).toEqual([]);
449 });
450​
451 it("uses control on non-mac platforms", async () => {
452 const harness = makeHarness({ platform: "linux" });
453 await harness.send(makeInput({ meta: false, control: true }));
454 expect(harness.preventDefault).toHaveBeenCalledTimes(1);
455 await harness.holdFor(QUIT_HOLD_DURATION_MS + 200, { meta: false, control: true });
456 await harness.send(makeInput({ type: "keyUp", meta: false, control: true }));
457 expect(harness.quit).toHaveBeenCalledTimes(1);
458 });
459});