Open History to see the last commit
1import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
2
3import {
4 makeQuitHoldHandler,
5 QUIT_DOUBLE_TAP_MS,
6 QUIT_HOLD_DURATION_MS,
7 QUIT_HOLD_RELEASE_GRACE_MS,
8} from "./QuitHold.ts";
9import type { QuitHoldKeyInput, QuitHoldState } from "./QuitHold.ts";
10
11function makeInput(overrides: Partial<QuitHoldKeyInput>): QuitHoldKeyInput {
12 return {
13 type: "keyDown",
14 key: "q",
15 meta: true,
16 control: false,
17 alt: false,
18 shift: false,
19 isAutoRepeat: false,
20 ...overrides,
21 };
22}
23
24function makeHarness(options?: {
25 enabled?: boolean;
26 platform?: NodeJS.Platform;
27 isEnabled?: () => Promise<boolean>;
28}) {
29 const notifications: Array<QuitHoldState> = [];
30 const quit = vi.fn();
31 const handler = makeQuitHoldHandler({
32 platform: options?.platform ?? "darwin",
33 isEnabled: options?.isEnabled ?? (() => Promise.resolve(options?.enabled ?? true)),
34 notify: (state) => notifications.push(state),
35 quit,
36 });
37 const preventDefault = vi.fn();
38 const send = async (input: QuitHoldKeyInput) => {
39 handler({ preventDefault }, input);
40 // Let the isEnabled promise settle.
41 await Promise.resolve();
42 await Promise.resolve();
43 };
44 // Simulates the OS auto-repeating the held shortcut every `intervalMs`.
45 const holdFor = async (
46 durationMs: number,
47 repeatOverrides: Partial<QuitHoldKeyInput> = {},
48 intervalMs = 100,
49 ) => {
50 for (let elapsed = 0; elapsed < durationMs; elapsed += intervalMs) {
51 vi.advanceTimersByTime(intervalMs);
52 await send(makeInput({ isAutoRepeat: true, ...repeatOverrides }));
53 }
54 };
55 return { notifications, quit, preventDefault, send, holdFor };
56}
57
58describe("makeQuitHoldHandler", () => {
59 beforeEach(() => {
60 vi.useFakeTimers();
61 });
62 afterEach(() => {
63 vi.useRealTimers();
64 });
65
66 it("shows the hint on a tap without quitting, even when the release is never seen", async () => {
67 // macOS suppresses the letter's keyUp while Cmd is held, so a tap may
68 // produce no keyUp at all. Quit must still not fire.
69 const harness = makeHarness();
70 await harness.send(makeInput({}));
71 expect(harness.preventDefault).toHaveBeenCalledTimes(1);
72 expect(harness.notifications).toEqual(["down"]);
73
74 vi.advanceTimersByTime(QUIT_HOLD_DURATION_MS + QUIT_HOLD_RELEASE_GRACE_MS);
75 expect(harness.quit).not.toHaveBeenCalled();
76 // The watchdog dismisses the hint once the press is clearly over.
77 expect(harness.notifications).toEqual(["down", "up"]);
78 });
79
80 it("quits after a completed hold is released", async () => {
81 const harness = makeHarness();
82 await harness.send(makeInput({}));
83 await harness.holdFor(QUIT_HOLD_DURATION_MS + 200);
84 expect(harness.quit).not.toHaveBeenCalled();
85 await harness.send(makeInput({ type: "keyUp", key: "Meta", meta: false }));
86 expect(harness.quit).not.toHaveBeenCalled();
87 vi.advanceTimersByTime(QUIT_HOLD_RELEASE_GRACE_MS);
88 expect(harness.quit).toHaveBeenCalledTimes(1);
89 expect(harness.notifications).toEqual(["down", "up"]);
90 });
91
92 it("waits for Q release when Cmd is released first", async () => {
93 const harness = makeHarness();
94 await harness.send(makeInput({}));
95 await harness.holdFor(QUIT_HOLD_DURATION_MS + 200);
96 await harness.send(makeInput({ type: "keyUp", key: "Meta", meta: false }));
97 harness.preventDefault.mockClear();
98 await harness.send(makeInput({ meta: false, isAutoRepeat: true }));
99 expect(harness.preventDefault).toHaveBeenCalledTimes(1);
100 vi.advanceTimersByTime(QUIT_HOLD_RELEASE_GRACE_MS * 2);
101 expect(harness.quit).not.toHaveBeenCalled();
102 await harness.send(makeInput({ type: "keyUp", meta: false }));
103 expect(harness.quit).toHaveBeenCalledTimes(1);
104 });
105
106 it("does not quit when the hold stops before the duration", async () => {
107 const harness = makeHarness();
108 await harness.send(makeInput({}));
109 await harness.holdFor(500);
110 await harness.send(makeInput({ type: "keyUp" }));
111 expect(harness.notifications).toEqual(["down", "up"]);
112 vi.advanceTimersByTime((QUIT_HOLD_DURATION_MS + QUIT_HOLD_RELEASE_GRACE_MS) * 2);
113 expect(harness.quit).not.toHaveBeenCalled();
114 });
115
116 it("cancels the hold when the modifier is released first", async () => {
117 const harness = makeHarness();
118 await harness.send(makeInput({}));
119 await harness.send(makeInput({ type: "keyUp", key: "Meta", meta: false }));
120 expect(harness.notifications).toEqual(["down", "up"]);
121 vi.advanceTimersByTime((QUIT_HOLD_DURATION_MS + QUIT_HOLD_RELEASE_GRACE_MS) * 2);
122 expect(harness.quit).not.toHaveBeenCalled();
123 });
124
125 it("quits without showing a hint when hold-to-quit is disabled", async () => {
126 const harness = makeHarness({ enabled: false });
127 await harness.send(makeInput({}));
128 expect(harness.quit).toHaveBeenCalledTimes(1);
129 expect(harness.notifications).toEqual([]);
130 });
131
132 it("discards a stale isEnabled resolution from a superseded press", async () => {
133 // Press #1's isEnabled is still pending when the user releases and
134 // presses again; its late resolution must not act for press #2.
135 const resolvers: Array<(enabled: boolean) => void> = [];
136 const harness = makeHarness({
137 isEnabled: () => new Promise((resolve) => resolvers.push(resolve)),
138 });
139 await harness.send(makeInput({}));
140 await harness.send(makeInput({ type: "keyUp" }));
141 // Outside the double-tap window, so the second press starts a new hold.
142 vi.advanceTimersByTime(QUIT_DOUBLE_TAP_MS + 100);
143 await harness.send(makeInput({}));
144 expect(resolvers).toHaveLength(2);
145
146 // Press #1 resolves late with "disabled" — it must not quit press #2.
147 resolvers[0]?.(false);
148 await Promise.resolve();
149 await Promise.resolve();
150 expect(harness.quit).not.toHaveBeenCalled();
151
152 // Press #2 resolves enabled and completes a full hold.
153 resolvers[1]?.(true);
154 await harness.holdFor(QUIT_HOLD_DURATION_MS + 200);
155 await harness.send(makeInput({ type: "keyUp" }));
156 expect(harness.quit).toHaveBeenCalledTimes(1);
157 });
158
159 it("quits on a quick double tap, even when the first release was never seen", async () => {
160 const harness = makeHarness();
161 await harness.send(makeInput({}));
162 vi.advanceTimersByTime(QUIT_DOUBLE_TAP_MS - 100);
163 await harness.send(makeInput({}));
164 expect(harness.quit).toHaveBeenCalledTimes(1);
165 });
166
167 it("treats two slow taps as separate presses", async () => {
168 const harness = makeHarness();
169 await harness.send(makeInput({}));
170 await harness.send(makeInput({ type: "keyUp" }));
171 vi.advanceTimersByTime(QUIT_DOUBLE_TAP_MS + 100);
172 await harness.send(makeInput({}));
173 expect(harness.quit).not.toHaveBeenCalled();
174 expect(harness.notifications).toEqual(["down", "up", "down"]);
175 });
176
177 it("cancels the hold when another key interrupts it", async () => {
178 const harness = makeHarness();
179 await harness.send(makeInput({}));
180 await harness.holdFor(500);
181 // Shift pressed mid-hold breaks the gesture...
182 await harness.send(makeInput({ shift: true }));
183 expect(harness.notifications).toEqual(["down", "up"]);
184 // ...so later repeats past the threshold must not quit.
185 await harness.holdFor(QUIT_HOLD_DURATION_MS);
186 expect(harness.quit).not.toHaveBeenCalled();
187 });
188
189 it("does not count an interrupted press toward a double tap", async () => {
190 const harness = makeHarness();
191 await harness.send(makeInput({}));
192 await harness.send(makeInput({ shift: true }));
193 // A fresh press right after the interruption starts a new hold, not a
194 // double-tap quit.
195 await harness.send(makeInput({}));
196 expect(harness.quit).not.toHaveBeenCalled();
197 expect(harness.notifications).toEqual(["down", "up", "down"]);
198 });
199
200 it("ignores other shortcuts", async () => {
201 const harness = makeHarness();
202 await harness.send(makeInput({ key: "w" }));
203 await harness.send(makeInput({ shift: true }));
204 await harness.send(makeInput({ meta: false }));
205 expect(harness.preventDefault).not.toHaveBeenCalled();
206 expect(harness.notifications).toEqual([]);
207 });
208
209 it("uses control on non-mac platforms", async () => {
210 const harness = makeHarness({ platform: "linux" });
211 await harness.send(makeInput({ meta: false, control: true }));
212 expect(harness.preventDefault).toHaveBeenCalledTimes(1);
213 await harness.holdFor(QUIT_HOLD_DURATION_MS + 200, { meta: false, control: true });
214 await harness.send(makeInput({ type: "keyUp", meta: false, control: true }));
215 expect(harness.quit).toHaveBeenCalledTimes(1);
216 });
217});