Open History to see the last commit
1import { assert, beforeEach, it, vi } from "vite-plus/test";
2
3const { execFileMock } = vi.hoisted(() => ({ execFileMock: vi.fn() }));
4
5vi.mock("node:child_process", () => ({ execFile: execFileMock }));
6
7import { activeWindow } from "./ActiveWindow.ts";
8
9beforeEach(() => {
10 execFileMock.mockReset();
11});
12
13function stubMacLookup(stdout: string) {
14 execFileMock.mockImplementation(
15 (
16 _file: string,
17 _args: ReadonlyArray<string>,
18 _options: unknown,
19 callback: (error: Error | null, stdout: string) => void,
20 ) => callback(null, stdout),
21 );
22}
23
24function macLookup(
25 windows: ReadonlyArray<{
26 id: number;
27 title: string;
28 bounds: { x: number; y: number; width: number; height: number };
29 alpha?: number;
30 }>,
31) {
32 return JSON.stringify({
33 owner: {
34 name: "Editor",
35 processId: 123,
36 path: "/Applications/Editor.app",
37 bundleId: "com.example.editor",
38 },
39 windows: windows.map((window) => ({ ...window, alpha: window.alpha ?? 1 })),
40 });
41}
42
43it("parses the frontmost macOS window from the osascript lookup", async () => {
44 stubMacLookup(
45 macLookup([
46 {
47 id: 42,
48 title: "main.ts",
49 bounds: { x: 10, y: 20, width: 800, height: 600 },
50 },
51 ]) + "\n",
52 );
53
54 const window = await activeWindow("darwin");
55
56 assert.deepEqual(window, {
57 platform: "macos",
58 id: 42,
59 title: "main.ts",
60 bounds: { x: 10, y: 20, width: 800, height: 600 },
61 owner: {
62 name: "Editor",
63 processId: 123,
64 path: "/Applications/Editor.app",
65 bundleId: "com.example.editor",
66 },
67 });
68 const [file, args] = execFileMock.mock.calls[0]!;
69 assert.strictEqual(file, "/usr/bin/osascript");
70 assert.deepEqual(args.slice(0, 3), ["-l", "JavaScript", "-e"]);
71});
72
73it.each([
74 {
75 name: "transparent and untitled helpers",
76 helpers: [
77 {
78 id: 1,
79 title: "",
80 bounds: { x: -1, y: -44, width: 1_680, height: 44 },
81 alpha: 0,
82 },
83 { id: 2, title: "", bounds: { x: 0, y: 0, width: 1_680, height: 68 } },
84 ],
85 content: {
86 id: 3,
87 title: "Document",
88 bounds: { x: 0, y: 0, width: 1_680, height: 1_050 },
89 },
90 },
91 {
92 name: "Chrome strips stacked above the content window",
93 helpers: [
94 { id: 75, title: "", bounds: { x: 0, y: 39, width: 1_800, height: 41 } },
95 { id: 74, title: "", bounds: { x: 0, y: 80, width: 1_800, height: 81 } },
96 { id: 1691, title: "", bounds: { x: 0, y: 39, width: 1_800, height: 158 } },
97 ],
98 content: {
99 id: 71,
100 title: "(10) Video - YouTube",
101 bounds: { x: 0, y: 161, width: 1_800, height: 1_008 },
102 },
103 },
104 {
105 name: "vertical strips at a negative desktop origin",
106 helpers: [
107 { id: 1, title: "", bounds: { x: -122, y: -100, width: 41, height: 1_000 } },
108 { id: 2, title: "", bounds: { x: -81, y: -100, width: 81, height: 1_000 } },
109 ],
110 content: {
111 id: 3,
112 title: "Document",
113 bounds: { x: 0, y: -100, width: 1_008, height: 1_000 },
114 },
115 },
116 {
117 name: "the window-sharing indicator shown during a ScreenCaptureKit window share",
118 helpers: [
119 { id: 90, title: "Window", bounds: { x: 1_400, y: 6, width: 66, height: 20 } },
120 { id: 91, title: "", bounds: { x: 0, y: 0, width: 1_512, height: 32 }, alpha: 0 },
121 ],
122 content: {
123 id: 92,
124 title: "T3 Code (Nightly)",
125 bounds: { x: 0, y: 33, width: 1_512, height: 949 },
126 },
127 },
128 {
129 name: "a smaller front window's strips with a larger background window",
130 helpers: [
131 { id: 75, title: "", bounds: { x: 100, y: 139, width: 1_200, height: 41 } },
132 { id: 74, title: "", bounds: { x: 100, y: 180, width: 1_200, height: 81 } },
133 ],
134 content: {
135 id: 71,
136 title: "Video - YouTube",
137 bounds: { x: 100, y: 261, width: 1_200, height: 700 },
138 },
139 background: [
140 { id: 80, title: "Other document", bounds: { x: 0, y: 39, width: 1_800, height: 1_130 } },
141 ],
142 },
143])("selects the content window behind $name", async ({ helpers, content, background = [] }) => {
144 stubMacLookup(macLookup([...helpers, content, ...background]));
145
146 const window = await activeWindow("darwin");
147
148 assert.strictEqual(window?.id, content.id);
149 assert.deepEqual(window?.bounds, content.bounds);
150});
151
152it.each([
153 { name: "untitled dialog", title: "", bounds: { x: 300, y: 200, width: 400, height: 240 } },
154 { name: "titled palette", title: "Palette", bounds: { x: 0, y: 0, width: 800, height: 50 } },
155 { name: "window titled Window", title: "Window", bounds: { x: 0, y: 0, width: 400, height: 24 } },
156 { name: "offset untitled strip", title: "", bounds: { x: 100, y: 100, width: 800, height: 50 } },
157 { name: "tall untitled window", title: "", bounds: { x: 0, y: 0, width: 800, height: 900 } },
158 {
159 name: "horizontal strip below the document",
160 title: "",
161 bounds: { x: 0, y: 1_200, width: 800, height: 50 },
162 },
163 {
164 name: "horizontal strip inside the document",
165 title: "",
166 bounds: { x: 0, y: 600, width: 800, height: 50 },
167 },
168 {
169 name: "horizontal strip far above the document",
170 title: "",
171 bounds: { x: 0, y: -300, width: 800, height: 50 },
172 },
173 {
174 name: "vertical strip right of the document",
175 title: "",
176 bounds: { x: 900, y: 0, width: 50, height: 1_000 },
177 },
178 {
179 name: "vertical strip inside the document",
180 title: "",
181 bounds: { x: 400, y: 0, width: 50, height: 1_000 },
182 },
183 {
184 name: "vertical strip far left of the document",
185 title: "",
186 bounds: { x: -250, y: 0, width: 50, height: 1_000 },
187 },
188])("keeps a frontmost $name ahead of a larger document", async ({ title, bounds }) => {
189 stubMacLookup(
190 macLookup([
191 { id: 1, title, bounds },
192 { id: 2, title: "Document", bounds: { x: 0, y: 0, width: 800, height: 1_000 } },
193 ]),
194 );
195
196 const window = await activeWindow("darwin");
197
198 assert.strictEqual(window?.id, 1);
199 assert.deepEqual(window?.bounds, bounds);
200});
201
202it("keeps an untitled content window ahead of a much taller background window", async () => {
203 const bounds = { x: 0, y: 0, width: 800, height: 900 };
204 stubMacLookup(
205 macLookup([
206 { id: 1, title: "", bounds },
207 { id: 2, title: "Background document", bounds: { x: 0, y: 0, width: 800, height: 4_000 } },
208 ]),
209 );
210
211 const window = await activeWindow("darwin");
212
213 assert.strictEqual(window?.id, 1);
214 assert.deepEqual(window?.bounds, bounds);
215});