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

window.test.ts

224 lines7.3 KB

1import { assert, describe, it } from "@effect/vitest";
2import * as NodeServices from "@effect/platform-node/NodeServices";
3import * as HostProcess from "@t3tools/shared/HostProcess";
4import * as FileSystem from "effect/FileSystem";
5import * as Path from "effect/Path";
6import * as Effect from "effect/Effect";
7import * as Layer from "effect/Layer";
8import * as Option from "effect/Option";
9import { vi } from "vite-plus/test";
10​
11const { focusedWebContents, ownerWindow } = vi.hoisted(() => ({
12 focusedWebContents: vi.fn(),
13 ownerWindow: vi.fn(),
14}));
15vi.mock("electron", () => ({
16 webContents: { getFocusedWebContents: focusedWebContents },
17 BrowserWindow: { fromWebContents: ownerWindow },
18}));
19​
20import * as DesktopBackendConfiguration from "../../backend/DesktopBackendConfiguration.ts";
21import * as DesktopBackendManager from "../../backend/DesktopBackendManager.ts";
22import * as DesktopBackendPool from "../../backend/DesktopBackendPool.ts";
23import {
24 getLocalEnvironmentBootstraps,
25 getRelayTelemetryEnabled,
26 probeRemoteEditors,
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.succeedSome(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​
68const backendConfigurationLayer = Layer.succeed(
69 DesktopBackendConfiguration.DesktopBackendConfiguration,
70 {
71 resolvePrimary: Effect.die("unexpected resolvePrimary"),
72 resolvePrimaryLabel: Effect.succeed("Windows"),
73 resolveWsl: () => Effect.die("unexpected resolveWsl"),
74 currentBootstrapToken: Effect.succeed("current-window-token"),
75 } satisfies DesktopBackendConfiguration.DesktopBackendConfiguration["Service"],
76);
77​
78const bootstrapsLayer = (instances: ReadonlyArray<DesktopBackendManager.DesktopBackendInstance>) =>
79 Layer.merge(DesktopBackendPool.layerTest([...instances]), backendConfigurationLayer);
80​
81describe("getLocalEnvironmentBootstraps", () => {
82 it.effect("hands out the current window's token to a backend launched with the secret", () =>
83 Effect.gen(function* () {
84 const result = yield* getLocalEnvironmentBootstraps.handler();
85​
86 assert.deepEqual(result, [
87 {
88 id: "wsl:default",
89 label: "WSL (Ubuntu)",
90 runningDistro: "Ubuntu",
91 httpBaseUrl: "http://127.0.0.1:3774/",
92 wsBaseUrl: "ws://127.0.0.1:3774/",
93 bootstrapToken: "current-window-token",
94 },
95 ]);
96 }).pipe(
97 Effect.provide(
98 bootstrapsLayer([
99 {
100 ...defaultWslInstance,
101 currentConfig: Effect.succeedSome({
102 ...readyWslConfig,
103 bootstrap: {
104 ...readyWslConfig.bootstrap,
105 desktopBootstrapSecret: "desktop-secret",
106 },
107 }),
108 },
109 ]),
110 ),
111 ),
112 );
113​
114 it.effect("publishes a pending bootstrap only while a transient retry is scheduled", () => {
115 const retryingConfig: DesktopBackendManager.DesktopBackendStartConfig = {
116 ...readyWslConfig,
117 preflightFailure: Option.some({
118 reason: "WSL probe timed out",
119 fatal: false,
120 retryLimit: 12,
121 }),
122 };
123 const retryingInstance: DesktopBackendManager.DesktopBackendInstance = {
124 ...defaultWslInstance,
125 currentConfig: Effect.succeedSome(retryingConfig),
126 snapshot: Effect.succeed({
127 desiredRunning: true,
128 ready: false,
129 activePid: Option.none(),
130 restartAttempt: 2,
131 restartScheduled: true,
132 }),
133 };
134​
135 return Effect.gen(function* () {
136 const result = yield* getLocalEnvironmentBootstraps.handler();
137 assert.deepEqual(result, [
138 {
139 id: "wsl:default",
140 label: "WSL (default distro)",
141 runningDistro: null,
142 httpBaseUrl: null,
143 wsBaseUrl: null,
144 },
145 ]);
146 }).pipe(Effect.provide(bootstrapsLayer([retryingInstance])));
147 });
148​
149 it.effect("omits a bounded transient bootstrap after retries stop", () => {
150 const stoppedInstance: DesktopBackendManager.DesktopBackendInstance = {
151 ...defaultWslInstance,
152 currentConfig: Effect.succeedSome({
153 ...readyWslConfig,
154 preflightFailure: Option.some({
155 reason: "WSL probe timed out",
156 fatal: false,
157 retryLimit: 12,
158 }),
159 }),
160 snapshot: Effect.succeed({
161 desiredRunning: false,
162 ready: false,
163 activePid: Option.none(),
164 restartAttempt: 12,
165 restartScheduled: false,
166 }),
167 };
168​
169 return Effect.gen(function* () {
170 const result = yield* getLocalEnvironmentBootstraps.handler();
171 assert.deepEqual(result, []);
172 }).pipe(Effect.provide(bootstrapsLayer([stoppedInstance])));
173 });
174});
175​
176describe("getRelayTelemetryEnabled", () => {
177 it.effect("reads the desktop host opt-out before returning the renderer policy", () =>
178 Effect.gen(function* () {
179 assert.isFalse(
180 yield* getRelayTelemetryEnabled.handler().pipe(
181 Effect.provideService(HostProcess.Environment, {
182 T3CODE_TELEMETRY_ENABLED: "false",
183 }),
184 ),
185 );
186 assert.isTrue(
187 yield* getRelayTelemetryEnabled
188 .handler()
189 .pipe(Effect.provideService(HostProcess.Environment, {})),
190 );
191 }),
192 );
193});
194​
195it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")(
196 "finds remote editors installed without PATH launchers",
197 () =>
198 Effect.gen(function* () {
199 const fs = yield* FileSystem.FileSystem;
200 const path = yield* Path.Path;
201 const home = yield* fs.makeTempDirectoryScoped({ prefix: "t3-remote-editors-" });
202 for (const app of ["Cursor", "Visual Studio Code", "WebStorm"]) {
203 const executable = path.join(
204 home,
205 "Applications",
206 `${app}.app`,
207 app === "WebStorm" ? "Contents/MacOS/webstorm" : "Contents/Resources/app/bin/code",
208 );
209 yield* fs.makeDirectory(path.dirname(executable), { recursive: true });
210 yield* fs.writeFileString(executable, "#!/bin/sh\n");
211 yield* fs.chmod(executable, 0o755);
212 }
213 const editors = yield* probeRemoteEditors.handler(undefined).pipe(
214 Effect.provideService(HostProcess.Environment, {
215 HOME: home,
216 PATH: path.join(home, "empty"),
217 }),
218 Effect.provideService(HostProcess.Platform, "darwin"),
219 );
220 assert.include(editors, "cursor");
221 assert.include(editors, "vscode");
222 assert.include(editors, "webstorm");
223 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
224);