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";
5
6import type * as Electron from "electron";
7
8import * as DesktopBackendManager from "../../backend/DesktopBackendManager.ts";
9import * as DesktopBackendPool from "../../backend/DesktopBackendPool.ts";
10import * as ElectronWindow from "../../electron/ElectronWindow.ts";
11import { getLocalEnvironmentBootstraps, getWindowFullscreenState } from "./window.ts";
12
13const readyWslConfig: DesktopBackendManager.DesktopBackendStartConfig = {
14 executablePath: "wsl.exe",
15 args: ["-d", "Ubuntu", "--", "node", "/app/bin.mjs"],
16 entryPath: "/app/bin.mjs",
17 cwd: "/app",
18 env: {},
19 extendEnv: false,
20 bootstrap: {
21 mode: "desktop",
22 noBrowser: true,
23 port: 3774,
24 host: "0.0.0.0",
25 desktopBootstrapToken: "bootstrap-token",
26 tailscaleServeEnabled: false,
27 tailscaleServePort: 443,
28 },
29 bootstrapDelivery: "stdin",
30 httpBaseUrl: new URL("http://127.0.0.1:3774"),
31 captureOutput: true,
32 preflightFailure: Option.none(),
33 runningDistro: "Ubuntu",
34};
35
36const defaultWslInstance: DesktopBackendManager.DesktopBackendInstance = {
37 id: DesktopBackendManager.BackendInstanceId("wsl:default"),
38 label: Effect.succeed("WSL (default distro)"),
39 start: Effect.void,
40 stop: () => Effect.void,
41 currentConfig: Effect.succeed(Option.some(readyWslConfig)),
42 snapshot: Effect.succeed({
43 desiredRunning: true,
44 ready: true,
45 activePid: Option.some(123),
46 restartAttempt: 0,
47 restartScheduled: false,
48 }),
49 waitForReady: () => Effect.succeed(true),
50};
51
52describe("getLocalEnvironmentBootstraps", () => {
53 it.effect("publishes the concrete running distro without replacing the stable instance id", () =>
54 Effect.gen(function* () {
55 const result = yield* getLocalEnvironmentBootstraps.handler();
56
57 assert.deepEqual(result, [
58 {
59 id: "wsl:default",
60 label: "WSL (Ubuntu)",
61 runningDistro: "Ubuntu",
62 httpBaseUrl: "http://127.0.0.1:3774/",
63 wsBaseUrl: "ws://127.0.0.1:3774/",
64 bootstrapToken: "bootstrap-token",
65 },
66 ]);
67 }).pipe(Effect.provide(DesktopBackendPool.layerTest([defaultWslInstance]))),
68 );
69
70 it.effect("publishes a pending bootstrap only while a transient retry is scheduled", () => {
71 const retryingConfig: DesktopBackendManager.DesktopBackendStartConfig = {
72 ...readyWslConfig,
73 preflightFailure: Option.some({
74 reason: "WSL probe timed out",
75 fatal: false,
76 retryLimit: 12,
77 }),
78 };
79 const retryingInstance: DesktopBackendManager.DesktopBackendInstance = {
80 ...defaultWslInstance,
81 currentConfig: Effect.succeed(Option.some(retryingConfig)),
82 snapshot: Effect.succeed({
83 desiredRunning: true,
84 ready: false,
85 activePid: Option.none(),
86 restartAttempt: 2,
87 restartScheduled: true,
88 }),
89 };
90
91 return Effect.gen(function* () {
92 const result = yield* getLocalEnvironmentBootstraps.handler();
93 assert.deepEqual(result, [
94 {
95 id: "wsl:default",
96 label: "WSL (default distro)",
97 runningDistro: null,
98 httpBaseUrl: null,
99 wsBaseUrl: null,
100 },
101 ]);
102 }).pipe(Effect.provide(DesktopBackendPool.layerTest([retryingInstance])));
103 });
104
105 it.effect("omits a bounded transient bootstrap after retries stop", () => {
106 const stoppedInstance: DesktopBackendManager.DesktopBackendInstance = {
107 ...defaultWslInstance,
108 currentConfig: Effect.succeed(
109 Option.some({
110 ...readyWslConfig,
111 preflightFailure: Option.some({
112 reason: "WSL probe timed out",
113 fatal: false,
114 retryLimit: 12,
115 }),
116 }),
117 ),
118 snapshot: Effect.succeed({
119 desiredRunning: false,
120 ready: false,
121 activePid: Option.none(),
122 restartAttempt: 12,
123 restartScheduled: false,
124 }),
125 };
126
127 return Effect.gen(function* () {
128 const result = yield* getLocalEnvironmentBootstraps.handler();
129 assert.deepEqual(result, []);
130 }).pipe(Effect.provide(DesktopBackendPool.layerTest([stoppedInstance])));
131 });
132});
133
134describe("getWindowFullscreenState", () => {
135 it.effect("reads the current native window state", () => {
136 const window = { isFullScreen: () => true } as Electron.BrowserWindow;
137
138 return Effect.gen(function* () {
139 assert.isTrue(yield* getWindowFullscreenState.handler());
140 }).pipe(
141 Effect.provide(
142 Layer.mock(ElectronWindow.ElectronWindow)({
143 currentMainOrFirst: Effect.succeed(Option.some(window)),
144 }),
145 ),
146 );
147 });
148});