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