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

ActiveWindow.test.ts

244 lines7.3 KB

1import { assert, beforeEach, it, vi } from "vite-plus/test";
2​
3const { execFileMock, loadWindowsForegroundApiMock } = vi.hoisted(() => ({
4 execFileMock: vi.fn(),
5 loadWindowsForegroundApiMock: vi.fn(),
6}));
7​
8vi.mock("node:child_process", () => ({ execFile: execFileMock }));
9vi.mock("../electron/WindowsForeground.ts", () => ({
10 loadWindowsForegroundApi: loadWindowsForegroundApiMock,
11}));
12​
13import { activeWindow } from "./ActiveWindow.ts";
14​
15beforeEach(() => {
16 execFileMock.mockReset();
17 loadWindowsForegroundApiMock.mockReset();
18});
19​
20function stubMacLookup(stdout: string) {
21 execFileMock.mockImplementation(
22 (
23 _file: string,
24 _args: ReadonlyArray<string>,
25 _options: unknown,
26 callback: (error: Error | null, stdout: string) => void,
27 ) => callback(null, stdout),
28 );
30​
31function macLookup(
32 windows: ReadonlyArray<{
33 id: number;
34 title: string;
35 bounds: { x: number; y: number; width: number; height: number };
36 alpha?: number;
37 }>,
38) {
39 return JSON.stringify({
40 owner: {
41 name: "Editor",
42 processId: 123,
43 path: "/Applications/Editor.app",
44 bundleId: "com.example.editor",
45 },
46 windows: windows.map((window) => ({ ...window, alpha: window.alpha ?? 1 })),
47 });
49​
50it("parses the frontmost macOS window from the osascript lookup", async () => {
51 stubMacLookup(
52 macLookup([
53 {
54 id: 42,
55 title: "main.ts",
56 bounds: { x: 10, y: 20, width: 800, height: 600 },
57 },
58 ]) + "\n",
59 );
60​
61 const window = await activeWindow("darwin");
62​
63 assert.deepEqual(window, {
64 platform: "macos",
65 id: 42,
66 title: "main.ts",
67 bounds: { x: 10, y: 20, width: 800, height: 600 },
68 owner: {
69 name: "Editor",
70 processId: 123,
71 path: "/Applications/Editor.app",
72 bundleId: "com.example.editor",
73 },
74 });
75 const [file, args] = execFileMock.mock.calls[0]!;
76 assert.strictEqual(file, "/usr/bin/osascript");
77 assert.deepEqual(args.slice(0, 3), ["-l", "JavaScript", "-e"]);
78 assert.lengthOf(loadWindowsForegroundApiMock.mock.calls, 0);
79});
80​
81it("omits an empty macOS bundle identifier", async () => {
82 stubMacLookup(
83 JSON.stringify({
84 owner: { name: "cli", processId: 9, path: "", bundleId: "" },
85 windows: [
86 {
87 id: 7,
88 title: "",
89 bounds: { x: 0, y: 0, width: 1, height: 1 },
90 alpha: 1,
91 },
92 ],
93 }),
94 );
95​
96 const window = await activeWindow("darwin");
97​
98 assert.deepEqual(window?.owner, { name: "cli", processId: 9, path: "" });
99});
100​
101it.each([
102 {
103 name: "transparent and untitled helpers",
104 helpers: [
105 {
106 id: 1,
107 title: "",
108 bounds: { x: -1, y: -44, width: 1_680, height: 44 },
109 alpha: 0,
110 },
111 { id: 2, title: "", bounds: { x: 0, y: 0, width: 1_680, height: 68 } },
112 ],
113 content: {
114 id: 3,
115 title: "Document",
116 bounds: { x: 0, y: 0, width: 1_680, height: 1_050 },
117 },
118 },
119 {
120 name: "Chrome strips stacked above the content window",
121 helpers: [
122 { id: 75, title: "", bounds: { x: 0, y: 39, width: 1_800, height: 41 } },
123 { id: 74, title: "", bounds: { x: 0, y: 80, width: 1_800, height: 81 } },
124 { id: 1691, title: "", bounds: { x: 0, y: 39, width: 1_800, height: 158 } },
125 ],
126 content: {
127 id: 71,
128 title: "(10) Video - YouTube",
129 bounds: { x: 0, y: 161, width: 1_800, height: 1_008 },
130 },
131 },
132 {
133 name: "a smaller front window's strips with a larger background window",
134 helpers: [
135 { id: 75, title: "", bounds: { x: 100, y: 139, width: 1_200, height: 41 } },
136 { id: 74, title: "", bounds: { x: 100, y: 180, width: 1_200, height: 81 } },
137 ],
138 content: {
139 id: 71,
140 title: "Video - YouTube",
141 bounds: { x: 100, y: 261, width: 1_200, height: 700 },
142 },
143 background: [
144 { id: 80, title: "Other document", bounds: { x: 0, y: 39, width: 1_800, height: 1_130 } },
145 ],
146 },
147])("selects the content window behind $name", async ({ helpers, content, background = [] }) => {
148 stubMacLookup(macLookup([...helpers, content, ...background]));
149​
150 const window = await activeWindow("darwin");
151​
152 assert.strictEqual(window?.id, content.id);
153 assert.deepEqual(window?.bounds, content.bounds);
154});
155​
156it.each([
157 { name: "untitled dialog", title: "", bounds: { x: 300, y: 200, width: 400, height: 240 } },
158 { name: "titled palette", title: "Palette", bounds: { x: 0, y: 0, width: 800, height: 50 } },
159 { name: "offset untitled strip", title: "", bounds: { x: 100, y: 100, width: 800, height: 50 } },
160 { name: "tall untitled window", title: "", bounds: { x: 0, y: 0, width: 800, height: 900 } },
161])("keeps a frontmost $name ahead of a larger document", async ({ title, bounds }) => {
162 stubMacLookup(
163 macLookup([
164 { id: 1, title, bounds },
165 { id: 2, title: "Document", bounds: { x: 0, y: 0, width: 800, height: 1_000 } },
166 ]),
167 );
168​
169 const window = await activeWindow("darwin");
170​
171 assert.strictEqual(window?.id, 1);
172 assert.deepEqual(window?.bounds, bounds);
173});
174​
175it("keeps an untitled content window ahead of a much taller background window", async () => {
176 const bounds = { x: 0, y: 0, width: 800, height: 900 };
177 stubMacLookup(
178 macLookup([
179 { id: 1, title: "", bounds },
180 { id: 2, title: "Background document", bounds: { x: 0, y: 0, width: 800, height: 4_000 } },
181 ]),
182 );
183​
184 const window = await activeWindow("darwin");
185​
186 assert.strictEqual(window?.id, 1);
187 assert.deepEqual(window?.bounds, bounds);
188});
189​
190it("resolves undefined when macOS has no frontmost window", async () => {
191 stubMacLookup("\n");
192​
193 assert.isUndefined(await activeWindow("darwin"));
194});
195​
196it("resolves undefined when macOS reports no visible windows", async () => {
197 stubMacLookup(macLookup([]));
198​
199 assert.isUndefined(await activeWindow("darwin"));
200});
201​
202it("composes the Windows foreground window from Win32 calls", async () => {
203 const api = {
204 getForegroundWindow: vi.fn(() => 0x1_f123_4567n),
205 getWindowRect: vi.fn(() => ({ x: 10, y: 20, width: 800, height: 600 })),
206 getWindowThreadAndProcessId: vi.fn(() => ({ threadId: 5, processId: 123 })),
207 getProcessImagePath: vi.fn(() => "C:\\Program Files\\Editor\\editor.exe"),
208 getWindowText: vi.fn(() => "main.ts - Editor"),
209 };
210 loadWindowsForegroundApiMock.mockResolvedValue(api);
211​
212 const window = await activeWindow("win32");
213​
214 assert.deepEqual(window, {
215 platform: "windows",
216 id: 0x1_f123_4567,
217 title: "main.ts - Editor",
218 bounds: { x: 10, y: 20, width: 800, height: 600 },
219 owner: { name: "editor.exe", processId: 123, path: "C:\\Program Files\\Editor\\editor.exe" },
220 });
221 assert.deepEqual(api.getWindowRect.mock.calls, [[0x1_f123_4567n]]);
222 assert.deepEqual(api.getProcessImagePath.mock.calls, [[123]]);
223 assert.lengthOf(execFileMock.mock.calls, 0);
224});
225​
226it("resolves undefined when Windows reports no foreground window", async () => {
227 const api = {
228 getForegroundWindow: vi.fn(() => 0n),
229 getWindowRect: vi.fn(),
230 getWindowThreadAndProcessId: vi.fn(),
231 getProcessImagePath: vi.fn(),
232 getWindowText: vi.fn(),
233 };
234 loadWindowsForegroundApiMock.mockResolvedValue(api);
235​
236 assert.isUndefined(await activeWindow("win32"));
237 assert.lengthOf(api.getWindowRect.mock.calls, 0);
238});
239​
240it("resolves undefined on other platforms without querying the OS", async () => {
241 assert.isUndefined(await activeWindow("linux"));
242 assert.lengthOf(execFileMock.mock.calls, 0);
243 assert.lengthOf(loadWindowsForegroundApiMock.mock.calls, 0);
244});