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

ElectronWindow.test.ts

241 lines8.3 KB

1import { assert, describe, it } from "@effect/vitest";
2import { HostProcessPlatform } from "@t3tools/shared/hostProcess";
3import * as Cause from "effect/Cause";
4import * as Effect from "effect/Effect";
5import * as Layer from "effect/Layer";
6import type * as Electron from "electron";
7import { beforeEach, vi } from "vite-plus/test";
8​
9const { appFocusMock, browserWindowMock, getAllWindowsMock, getFocusedWindowMock } = vi.hoisted(
10 () => ({
11 appFocusMock: vi.fn(),
12 browserWindowMock: vi.fn(function BrowserWindowMock() {}),
13 getAllWindowsMock: vi.fn(),
14 getFocusedWindowMock: vi.fn(),
15 }),
16);
17​
18vi.mock("electron", () => ({
19 app: {
20 focus: appFocusMock,
21 },
22 BrowserWindow: Object.assign(browserWindowMock, {
23 getAllWindows: getAllWindowsMock,
24 getFocusedWindow: getFocusedWindowMock,
25 }),
26}));
27​
28import * as ElectronWindow from "./ElectronWindow.ts";
29​
30const TestLayer = ElectronWindow.layer.pipe(
31 Layer.provide(Layer.succeed(HostProcessPlatform, "linux")),
32);
33​
34function makeBrowserWindow(input: { readonly id: number; readonly destroyed: boolean }) {
35 return {
36 id: input.id,
37 isDestroyed: vi.fn(() => input.destroyed),
38 } as unknown as Electron.BrowserWindow;
40​
41describe("ElectronWindow", () => {
42 beforeEach(() => {
43 appFocusMock.mockReset();
44 browserWindowMock.mockReset();
45 getAllWindowsMock.mockReset();
46 getFocusedWindowMock.mockReset();
47 });
48​
49 it.effect("preserves schema-safe creation context and the Electron cause", () =>
50 Effect.gen(function* () {
51 const cause = new Error("native BrowserWindow construction failed");
52 browserWindowMock.mockImplementationOnce(function BrowserWindowFailure() {
53 throw cause;
54 });
55 const options = {
56 title: "T3 Code",
57 width: 1100,
58 height: 780,
59 minWidth: 840,
60 minHeight: 620,
61 show: false,
62 modal: false,
63 frame: true,
64 transparent: false,
65 backgroundColor: "#101010",
66 icon: {} as Electron.NativeImage,
67 webPreferences: {
68 preload: "/tmp/preload.js",
69 partition: "persist:t3code-preview-test",
70 sandbox: true,
71 contextIsolation: true,
72 nodeIntegration: false,
73 webviewTag: true,
74 spellcheck: true,
75 },
76 } satisfies Electron.BrowserWindowConstructorOptions;
77 const electronWindow = yield* ElectronWindow.ElectronWindow;
78​
79 const error = yield* electronWindow.create(options).pipe(Effect.flip);
80​
81 assert.instanceOf(error, ElectronWindow.ElectronWindowCreateError);
82 assert.isTrue(ElectronWindow.isElectronWindowCreateError(error));
83 assert.deepEqual(error.options, {
84 title: "T3 Code",
85 width: 1100,
86 height: 780,
87 minWidth: 840,
88 minHeight: 620,
89 show: false,
90 modal: false,
91 frame: true,
92 transparent: false,
93 backgroundColor: "#101010",
94 webPreferences: {
95 preload: "/tmp/preload.js",
96 partition: "persist:t3code-preview-test",
97 backgroundThrottling: null,
98 sandbox: true,
99 contextIsolation: true,
100 nodeIntegration: false,
101 webviewTag: true,
102 },
103 });
104 assert.isFalse("icon" in error.options);
105 assert.isFalse("spellcheck" in error.options.webPreferences);
106 assert.strictEqual(error.cause, cause);
107 assert.equal(error.message, 'Failed to create Electron BrowserWindow "T3 Code" (1100x780).');
108 assert.notInclude(error.message, cause.message);
109 assert.deepEqual(browserWindowMock.mock.calls, [[options]]);
110 }).pipe(Effect.provide(TestLayer)),
111 );
112​
113 it.effect("skips windows destroyed before appearance sync runs", () =>
114 Effect.gen(function* () {
115 const liveWindow = makeBrowserWindow({ id: 1, destroyed: false });
116 const destroyedWindow = makeBrowserWindow({ id: 2, destroyed: true });
117 getAllWindowsMock.mockReturnValue([destroyedWindow, liveWindow]);
118​
119 const syncedWindows: Electron.BrowserWindow[] = [];
120 const electronWindow = yield* ElectronWindow.ElectronWindow;
121 yield* electronWindow.syncAllAppearance((window) =>
122 Effect.sync(() => {
123 syncedWindows.push(window);
124 }),
125 );
126​
127 assert.deepEqual(syncedWindows, [liveWindow]);
128 }).pipe(Effect.provide(TestLayer)),
129 );
130​
131 it.effect("preserves window enumeration failures as structured defects", () =>
132 Effect.gen(function* () {
133 const cause = new Error("window enumeration failed");
134 getAllWindowsMock.mockImplementationOnce(() => {
135 throw cause;
136 });
137​
138 const electronWindow = yield* ElectronWindow.ElectronWindow;
139 const exit = yield* Effect.exit(electronWindow.currentMainOrFirst);
140​
141 assert.equal(exit._tag, "Failure");
142 if (exit._tag === "Failure") {
143 const error = Cause.squash(exit.cause);
144 assert.instanceOf(error, ElectronWindow.ElectronWindowOperationError);
145 assert.equal(error.operation, "list-windows");
146 assert.equal(error.platform, "linux");
147 assert.isNull(error.windowId);
148 assert.isNull(error.channel);
149 assert.strictEqual(error.cause, cause);
150 assert.notInclude(error.message, cause.message);
151 }
152 }).pipe(Effect.provide(TestLayer)),
153 );
154​
155 it.effect("preserves reveal failures with the target window", () =>
156 Effect.gen(function* () {
157 const cause = new Error("window restore failed");
158 const window = {
159 id: 41,
160 isDestroyed: vi.fn(() => false),
161 isMinimized: vi.fn(() => true),
162 restore: vi.fn(() => {
163 throw cause;
164 }),
165 } as unknown as Electron.BrowserWindow;
166​
167 const electronWindow = yield* ElectronWindow.ElectronWindow;
168 const exit = yield* Effect.exit(electronWindow.reveal(window));
169​
170 assert.equal(exit._tag, "Failure");
171 if (exit._tag === "Failure") {
172 const error = Cause.squash(exit.cause);
173 assert.instanceOf(error, ElectronWindow.ElectronWindowOperationError);
174 assert.equal(error.operation, "reveal-window");
175 assert.equal(error.windowId, 41);
176 assert.isNull(error.channel);
177 assert.strictEqual(error.cause, cause);
178 }
179 }).pipe(Effect.provide(TestLayer)),
180 );
181​
182 it.effect("preserves message delivery failures with window and channel context", () =>
183 Effect.gen(function* () {
184 const cause = new Error("renderer send failed");
185 const window = {
186 id: 42,
187 isDestroyed: vi.fn(() => false),
188 webContents: {
189 send: vi.fn(() => {
190 throw cause;
191 }),
192 },
193 } as unknown as Electron.BrowserWindow;
194 getAllWindowsMock.mockReturnValueOnce([window]);
195​
196 const electronWindow = yield* ElectronWindow.ElectronWindow;
197 const exit = yield* Effect.exit(electronWindow.sendAll("desktop:update", { ready: true }));
198​
199 assert.equal(exit._tag, "Failure");
200 if (exit._tag === "Failure") {
201 const error = Cause.squash(exit.cause);
202 assert.instanceOf(error, ElectronWindow.ElectronWindowOperationError);
203 assert.equal(error.operation, "send-window-message");
204 assert.equal(error.windowId, 42);
205 assert.equal(error.channel, "desktop:update");
206 assert.strictEqual(error.cause, cause);
207 }
208 }).pipe(Effect.provide(TestLayer)),
209 );
210​
211 it.effect("preserves destroy failures and continues with later windows", () =>
212 Effect.gen(function* () {
213 const cause = new Error("window destroy failed");
214 const window = {
215 id: 43,
216 destroy: vi.fn(() => {
217 throw cause;
218 }),
219 } as unknown as Electron.BrowserWindow;
220 const laterWindow = {
221 id: 44,
222 destroy: vi.fn(),
223 } as unknown as Electron.BrowserWindow;
224 getAllWindowsMock.mockReturnValueOnce([window, laterWindow]);
225​
226 const electronWindow = yield* ElectronWindow.ElectronWindow;
227 const exit = yield* Effect.exit(electronWindow.destroyAll);
228​
229 assert.equal(exit._tag, "Failure");
230 if (exit._tag === "Failure") {
231 const error = Cause.squash(exit.cause);
232 assert.instanceOf(error, ElectronWindow.ElectronWindowOperationError);
233 assert.equal(error.operation, "destroy-window");
234 assert.equal(error.windowId, 43);
235 assert.isNull(error.channel);
236 assert.strictEqual(error.cause, cause);
237 }
238 assert.equal(vi.mocked(laterWindow.destroy).mock.calls.length, 1);
239 }).pipe(Effect.provide(TestLayer)),
240 );
241});