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

window.test.ts

189 lines5.8 KB

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​
9import * as DesktopBackendManager from "../../backend/DesktopBackendManager.ts";
10import * as DesktopBackendPool from "../../backend/DesktopBackendPool.ts";
11import * as ElectronDialog from "../../electron/ElectronDialog.ts";
12import * as ElectronWindow from "../../electron/ElectronWindow.ts";
13import {
14 getLocalEnvironmentBootstraps,
15 getWindowFullscreenState,
16 pickProjectFavicon,
17} from "./window.ts";
18​
19const readyWslConfig: DesktopBackendManager.DesktopBackendStartConfig = {
20 executablePath: "wsl.exe",
21 args: ["-d", "Ubuntu", "--", "node", "/app/bin.mjs"],
22 entryPath: "/app/bin.mjs",
23 cwd: "/app",
24 env: {},
25 extendEnv: false,
26 bootstrap: {
27 mode: "desktop",
28 noBrowser: true,
29 port: 3774,
30 host: "0.0.0.0",
31 desktopBootstrapToken: "bootstrap-token",
32 tailscaleServeEnabled: false,
33 tailscaleServePort: 443,
34 },
35 bootstrapDelivery: "stdin",
36 httpBaseUrl: new URL("http://127.0.0.1:3774"),
37 captureOutput: true,
38 preflightFailure: Option.none(),
39 runningDistro: "Ubuntu",
40};
41​
42const defaultWslInstance: DesktopBackendManager.DesktopBackendInstance = {
43 id: DesktopBackendManager.BackendInstanceId("wsl:default"),
44 label: Effect.succeed("WSL (default distro)"),
45 start: Effect.void,
46 stop: () => Effect.void,
47 currentConfig: Effect.succeed(Option.some(readyWslConfig)),
48 snapshot: Effect.succeed({
49 desiredRunning: true,
50 ready: true,
51 activePid: Option.some(123),
52 restartAttempt: 0,
53 restartScheduled: false,
54 }),
55 waitForReady: () => Effect.succeed(true),
56};
57​
58describe("getLocalEnvironmentBootstraps", () => {
59 it.effect("publishes the concrete running distro without replacing the stable instance id", () =>
60 Effect.gen(function* () {
61 const result = yield* getLocalEnvironmentBootstraps.handler();
62​
63 assert.deepEqual(result, [
64 {
65 id: "wsl:default",
66 label: "WSL (Ubuntu)",
67 runningDistro: "Ubuntu",
68 httpBaseUrl: "http://127.0.0.1:3774/",
69 wsBaseUrl: "ws://127.0.0.1:3774/",
70 bootstrapToken: "bootstrap-token",
71 },
72 ]);
73 }).pipe(Effect.provide(DesktopBackendPool.layerTest([defaultWslInstance]))),
74 );
75​
76 it.effect("publishes a pending bootstrap only while a transient retry is scheduled", () => {
77 const retryingConfig: DesktopBackendManager.DesktopBackendStartConfig = {
78 ...readyWslConfig,
79 preflightFailure: Option.some({
80 reason: "WSL probe timed out",
81 fatal: false,
82 retryLimit: 12,
83 }),
84 };
85 const retryingInstance: DesktopBackendManager.DesktopBackendInstance = {
86 ...defaultWslInstance,
87 currentConfig: Effect.succeed(Option.some(retryingConfig)),
88 snapshot: Effect.succeed({
89 desiredRunning: true,
90 ready: false,
91 activePid: Option.none(),
92 restartAttempt: 2,
93 restartScheduled: true,
94 }),
95 };
96​
97 return Effect.gen(function* () {
98 const result = yield* getLocalEnvironmentBootstraps.handler();
99 assert.deepEqual(result, [
100 {
101 id: "wsl:default",
102 label: "WSL (default distro)",
103 runningDistro: null,
104 httpBaseUrl: null,
105 wsBaseUrl: null,
106 },
107 ]);
108 }).pipe(Effect.provide(DesktopBackendPool.layerTest([retryingInstance])));
109 });
110​
111 it.effect("omits a bounded transient bootstrap after retries stop", () => {
112 const stoppedInstance: DesktopBackendManager.DesktopBackendInstance = {
113 ...defaultWslInstance,
114 currentConfig: Effect.succeed(
115 Option.some({
116 ...readyWslConfig,
117 preflightFailure: Option.some({
118 reason: "WSL probe timed out",
119 fatal: false,
120 retryLimit: 12,
121 }),
122 }),
123 ),
124 snapshot: Effect.succeed({
125 desiredRunning: false,
126 ready: false,
127 activePid: Option.none(),
128 restartAttempt: 12,
129 restartScheduled: false,
130 }),
131 };
132​
133 return Effect.gen(function* () {
134 const result = yield* getLocalEnvironmentBootstraps.handler();
135 assert.deepEqual(result, []);
136 }).pipe(Effect.provide(DesktopBackendPool.layerTest([stoppedInstance])));
137 });
138});
139​
140describe("getWindowFullscreenState", () => {
141 it.effect("reads the current native window state", () => {
142 const window = { isFullScreen: () => true } as Electron.BrowserWindow;
143​
144 return Effect.gen(function* () {
145 assert.isTrue(yield* getWindowFullscreenState.handler());
146 }).pipe(
147 Effect.provide(
148 Layer.mock(ElectronWindow.ElectronWindow)({
149 currentMainOrFirst: Effect.succeed(Option.some(window)),
150 }),
151 ),
152 );
153 });
154});
155​
156describe("pickProjectFavicon", () => {
157 it.effect("opens a single-image picker from the project directory", () =>
158 Effect.gen(function* () {
159 const pickFiles = vi.fn(() => Effect.succeed(["/pictures/icon.png"]));
160 const result = yield* pickProjectFavicon.handler("/project").pipe(
161 Effect.provide(
162 Layer.mergeAll(
163 Layer.mock(ElectronDialog.ElectronDialog)({ pickFiles }),
164 Layer.mock(ElectronWindow.ElectronWindow)({
165 focusedMainOrFirst: Effect.succeed(Option.none()),
166 }),
167 ),
168 ),
169 );
170​
171 assert.strictEqual(result, "/pictures/icon.png");
172 assert.deepEqual(pickFiles.mock.calls, [
173 [
174 {
175 owner: Option.none(),
176 defaultPath: Option.some("/project"),
177 multiple: false,
178 filters: [
179 {
180 name: "Images",
181 extensions: ["avif", "gif", "ico", "jpeg", "jpg", "png", "svg", "webp"],
182 },
183 ],
184 },
185 ],
186 ]);
187 }),
188 );
189});