Skip to content
Source
Sign inSign up

ActiveWindow.test.ts

202 lines5.8 KB

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 );
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 });
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: "a smaller front window's strips with a larger background window",
118 helpers: [
119 { id: 75, title: "", bounds: { x: 100, y: 139, width: 1_200, height: 41 } },
120 { id: 74, title: "", bounds: { x: 100, y: 180, width: 1_200, height: 81 } },
121 ],
122 content: {
123 id: 71,
124 title: "Video - YouTube",
125 bounds: { x: 100, y: 261, width: 1_200, height: 700 },
126 },
127 background: [
128 { id: 80, title: "Other document", bounds: { x: 0, y: 39, width: 1_800, height: 1_130 } },
129 ],
130 },
131])("selects the content window behind $name", async ({ helpers, content, background = [] }) => {
132 stubMacLookup(macLookup([...helpers, content, ...background]));
133​
134 const window = await activeWindow("darwin");
135​
136 assert.strictEqual(window?.id, content.id);
137 assert.deepEqual(window?.bounds, content.bounds);
138});
139​
140it.each([
141 { name: "untitled dialog", title: "", bounds: { x: 300, y: 200, width: 400, height: 240 } },
142 { name: "titled palette", title: "Palette", bounds: { x: 0, y: 0, width: 800, height: 50 } },
143 { name: "offset untitled strip", title: "", bounds: { x: 100, y: 100, width: 800, height: 50 } },
144 { name: "tall untitled window", title: "", bounds: { x: 0, y: 0, width: 800, height: 900 } },
145 {
146 name: "horizontal strip below the document",
147 title: "",
148 bounds: { x: 0, y: 1_200, width: 800, height: 50 },
149 },
150 {
151 name: "horizontal strip inside the document",
152 title: "",
153 bounds: { x: 0, y: 600, width: 800, height: 50 },
154 },
155 {
156 name: "horizontal strip far above the document",
157 title: "",
158 bounds: { x: 0, y: -300, width: 800, height: 50 },
159 },
160 {
161 name: "vertical strip right of the document",
162 title: "",
163 bounds: { x: 900, y: 0, width: 50, height: 1_000 },
164 },
165 {
166 name: "vertical strip inside the document",
167 title: "",
168 bounds: { x: 400, y: 0, width: 50, height: 1_000 },
169 },
170 {
171 name: "vertical strip far left of the document",
172 title: "",
173 bounds: { x: -250, y: 0, width: 50, height: 1_000 },
174 },
175])("keeps a frontmost $name ahead of a larger document", async ({ title, bounds }) => {
176 stubMacLookup(
177 macLookup([
178 { id: 1, title, bounds },
179 { id: 2, title: "Document", bounds: { x: 0, y: 0, width: 800, height: 1_000 } },
180 ]),
181 );
182​
183 const window = await activeWindow("darwin");
184​
185 assert.strictEqual(window?.id, 1);
186 assert.deepEqual(window?.bounds, bounds);
187});
188​
189it("keeps an untitled content window ahead of a much taller background window", async () => {
190 const bounds = { x: 0, y: 0, width: 800, height: 900 };
191 stubMacLookup(
192 macLookup([
193 { id: 1, title: "", bounds },
194 { id: 2, title: "Background document", bounds: { x: 0, y: 0, width: 800, height: 4_000 } },
195 ]),
196 );
197​
198 const window = await activeWindow("darwin");
199​
200 assert.strictEqual(window?.id, 1);
201 assert.deepEqual(window?.bounds, bounds);
202});