Open History to see the last commit
1import { assert, describe, it } from "@effect/vitest";
2import * as Effect from "effect/Effect";
3import * as Layer from "effect/Layer";
4import * as Option from "effect/Option";
5import { vi } from "vite-plus/test";
6
7import type * as Electron from "electron";
8
9const { focusedWebContents, ownerWindow } = vi.hoisted(() => ({
10 focusedWebContents: vi.fn(),
11 ownerWindow: vi.fn(),
12}));
13vi.mock("electron", () => ({
14 webContents: { getFocusedWebContents: focusedWebContents },
15 BrowserWindow: { fromWebContents: ownerWindow },
16}));
17
18import * as DesktopBackendManager from "../../backend/DesktopBackendManager.ts";
19import * as DesktopBackendPool from "../../backend/DesktopBackendPool.ts";
20import * as ElectronDialog from "../../electron/ElectronDialog.ts";
21import * as ElectronWindow from "../../electron/ElectronWindow.ts";
22import * as DesktopAppSettings from "../../settings/DesktopAppSettings.ts";
23import type { DesktopSettings } from "../../settings/DesktopAppSettings.ts";
24import {
25 getLocalEnvironmentBootstraps,
26 getWindowFullscreenState,
27 pasteAsText,
28 pickProjectFavicon,
29} from "./window.ts";
30
31const readyWslConfig: DesktopBackendManager.DesktopBackendStartConfig = {
32 executablePath: "wsl.exe",
33 args: ["-d", "Ubuntu", "--", "node", "/app/bin.mjs"],
34 entryPath: "/app/bin.mjs",
35 cwd: "/app",
36 env: {},
37 extendEnv: false,
38 bootstrap: {
39 mode: "desktop",
40 noBrowser: true,
41 port: 3774,
42 host: "0.0.0.0",
43 desktopBootstrapToken: "bootstrap-token",
44 tailscaleServeEnabled: false,
45 tailscaleServePort: 443,
46 },
47 bootstrapDelivery: "stdin",
48 httpBaseUrl: new URL("http://127.0.0.1:3774"),
49 captureOutput: true,
50 preflightFailure: Option.none(),
51 runningDistro: "Ubuntu",
52};
53
54const defaultWslInstance: DesktopBackendManager.DesktopBackendInstance = {
55 id: DesktopBackendManager.BackendInstanceId("wsl:default"),
56 label: Effect.succeed("WSL (default distro)"),
57 start: Effect.void,
58 stop: () => Effect.void,
59 currentConfig: Effect.succeed(Option.some(readyWslConfig)),
60 snapshot: Effect.succeed({
61 desiredRunning: true,
62 ready: true,
63 activePid: Option.some(123),
64 restartAttempt: 0,
65 restartScheduled: false,
66 }),
67 waitForReady: () => Effect.succeed(true),
68};
69
70describe("getLocalEnvironmentBootstraps", () => {
71 it.effect("publishes the concrete running distro without replacing the stable instance id", () =>
72 Effect.gen(function* () {
73 const result = yield* getLocalEnvironmentBootstraps.handler();
74
75 assert.deepEqual(result, [
76 {
77 id: "wsl:default",
78 label: "WSL (Ubuntu)",
79 runningDistro: "Ubuntu",
80 httpBaseUrl: "http://127.0.0.1:3774/",
81 wsBaseUrl: "ws://127.0.0.1:3774/",
82 bootstrapToken: "bootstrap-token",
83 },
84 ]);
85 }).pipe(Effect.provide(DesktopBackendPool.layerTest([defaultWslInstance]))),
86 );
87
88 it.effect("publishes a pending bootstrap only while a transient retry is scheduled", () => {
89 const retryingConfig: DesktopBackendManager.DesktopBackendStartConfig = {
90 ...readyWslConfig,
91 preflightFailure: Option.some({
92 reason: "WSL probe timed out",
93 fatal: false,
94 retryLimit: 12,
95 }),
96 };
97 const retryingInstance: DesktopBackendManager.DesktopBackendInstance = {
98 ...defaultWslInstance,
99 currentConfig: Effect.succeed(Option.some(retryingConfig)),
100 snapshot: Effect.succeed({
101 desiredRunning: true,
102 ready: false,
103 activePid: Option.none(),
104 restartAttempt: 2,
105 restartScheduled: true,
106 }),
107 };
108
109 return Effect.gen(function* () {
110 const result = yield* getLocalEnvironmentBootstraps.handler();
111 assert.deepEqual(result, [
112 {
113 id: "wsl:default",
114 label: "WSL (default distro)",
115 runningDistro: null,
116 httpBaseUrl: null,
117 wsBaseUrl: null,
118 },
119 ]);
120 }).pipe(Effect.provide(DesktopBackendPool.layerTest([retryingInstance])));
121 });
122
123 it.effect("omits a bounded transient bootstrap after retries stop", () => {
124 const stoppedInstance: DesktopBackendManager.DesktopBackendInstance = {
125 ...defaultWslInstance,
126 currentConfig: Effect.succeed(
127 Option.some({
128 ...readyWslConfig,
129 preflightFailure: Option.some({
130 reason: "WSL probe timed out",
131 fatal: false,
132 retryLimit: 12,
133 }),
134 }),
135 ),
136 snapshot: Effect.succeed({
137 desiredRunning: false,
138 ready: false,
139 activePid: Option.none(),
140 restartAttempt: 12,
141 restartScheduled: false,
142 }),
143 };
144
145 return Effect.gen(function* () {
146 const result = yield* getLocalEnvironmentBootstraps.handler();
147 assert.deepEqual(result, []);
148 }).pipe(Effect.provide(DesktopBackendPool.layerTest([stoppedInstance])));
149 });
150});
151
152describe("getWindowFullscreenState", () => {
153 it.effect("reads the current native window state", () => {
154 const window = { isFullScreen: () => true } as Electron.BrowserWindow;
155
156 return Effect.gen(function* () {
157 assert.isTrue(yield* getWindowFullscreenState.handler());
158 }).pipe(
159 Effect.provide(
160 Layer.mock(ElectronWindow.ElectronWindow)({
161 currentMainOrFirst: Effect.succeed(Option.some(window)),
162 }),
163 ),
164 );
165 });
166});
167
168describe("pasteAsText", () => {
169 it.effect(
170 "pastes into the focused guest only after the main renderer acknowledges the menu action",
171 () => {
172 const paste = vi.fn();
173 const mainPaste = vi.fn();
174 const window = {
175 webContents: { id: 42, paste: mainPaste },
176 isDestroyed: () => false,
177 } as unknown as Electron.BrowserWindow;
178 focusedWebContents.mockReturnValue({ paste, isDestroyed: () => false });
179 ownerWindow.mockReturnValue(window);
180
181 return Effect.gen(function* () {
182 yield* pasteAsText.handler(undefined, { sender: { id: 42 } });
183 assert.equal(paste.mock.calls.length, 1);
184 assert.equal(mainPaste.mock.calls.length, 0);
185
186 yield* pasteAsText.handler(undefined, { sender: { id: 99 } });
187 assert.equal(paste.mock.calls.length, 1);
188 ownerWindow.mockReturnValue({}); // A focused PiP/other BrowserWindow.
189 yield* pasteAsText.handler(undefined, { sender: { id: 42 } });
190 assert.equal(paste.mock.calls.length, 1);
191 ownerWindow.mockReturnValue(null); // Detached contents.
192 yield* pasteAsText.handler(undefined, { sender: { id: 42 } });
193 assert.equal(paste.mock.calls.length, 1);
194 ownerWindow.mockReturnValue(window);
195 focusedWebContents.mockReturnValue({ paste, isDestroyed: () => true });
196 yield* pasteAsText.handler(undefined, { sender: { id: 42 } });
197 assert.equal(paste.mock.calls.length, 1);
198 focusedWebContents.mockReturnValue(null);
199 yield* pasteAsText.handler(undefined, { sender: { id: 42 } });
200 assert.equal(paste.mock.calls.length, 1);
201 }).pipe(
202 Effect.provide(
203 Layer.mock(ElectronWindow.ElectronWindow)({
204 main: Effect.succeed(Option.some(window)),
205 }),
206 ),
207 );
208 },
209 );
210});
211
212describe("pickProjectFavicon", () => {
213 const pickerLayer = (pickFiles: () => Effect.Effect<Array<string>>, settings?: DesktopSettings) =>
214 Layer.mergeAll(
215 Layer.mock(ElectronDialog.ElectronDialog)({ pickFiles }),
216 Layer.mock(ElectronWindow.ElectronWindow)({
217 focusedMainOrFirst: Effect.succeed(Option.none()),
218 }),
219 DesktopAppSettings.layerTest(settings),
220 );
221
222 it.effect("opens a single-image picker from the project directory", () =>
223 Effect.gen(function* () {
224 const pickFiles = vi.fn(() => Effect.succeed(["/pictures/icon.png"]));
225 const result = yield* pickProjectFavicon
226 .handler("/project")
227 .pipe(Effect.provide(pickerLayer(pickFiles)));
228
229 assert.strictEqual(result, "/pictures/icon.png");
230 assert.deepEqual(pickFiles.mock.calls, [
231 [
232 {
233 owner: Option.none(),
234 defaultPath: Option.some("/project"),
235 multiple: false,
236 filters: [
237 {
238 name: "Images",
239 extensions: ["avif", "gif", "ico", "jpeg", "jpg", "png", "svg", "webp"],
240 },
241 ],
242 },
243 ],
244 ]);
245 }),
246 );
247
248 it.effect("does not open a picker while the local environment is off", () =>
249 Effect.gen(function* () {
250 const pickFiles = vi.fn(() => Effect.succeed(["/pictures/icon.png"]));
251 const result = yield* pickProjectFavicon.handler("/project").pipe(
252 Effect.provide(
253 pickerLayer(pickFiles, {
254 ...DesktopAppSettings.DEFAULT_DESKTOP_SETTINGS,
255 localEnvironmentEnabled: false,
256 }),
257 ),
258 );
259
260 assert.strictEqual(result, null);
261 assert.strictEqual(pickFiles.mock.calls.length, 0);
262 }),
263 );
264});