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

DesktopBackendConfiguration.test.ts

1,190 lines49.3 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { assert, describe, it } from "@effect/vitest";
3import * as Effect from "effect/Effect";
4import * as FileSystem from "effect/FileSystem";
5import * as Layer from "effect/Layer";
6import * as Logger from "effect/Logger";
7import * as ManagedRuntime from "effect/ManagedRuntime";
8import * as Option from "effect/Option";
9import * as Path from "effect/Path";
10import * as PlatformError from "effect/PlatformError";
11import * as Schema from "effect/Schema";
12import { ChildProcessSpawner } from "effect/unstable/process";
13​
14import * as DesktopEnvironment from "../app/DesktopEnvironment.ts";
15import * as DesktopBackendConfiguration from "./DesktopBackendConfiguration.ts";
16import * as DesktopConfig from "../app/DesktopConfig.ts";
17import * as DesktopServerExposure from "./DesktopServerExposure.ts";
18import * as DesktopAppSettings from "../settings/DesktopAppSettings.ts";
19import * as DesktopWslEnvironment from "../wsl/DesktopWslEnvironment.ts";
20​
21const PersistedServerObservabilitySettingsDocument = Schema.Struct({
22 observability: Schema.Struct({
23 otlpTracesUrl: Schema.String,
24 otlpMetricsUrl: Schema.String,
25 }),
26});
27​
28const encodePersistedServerObservabilitySettingsDocument = Schema.encodeEffect(
29 Schema.fromJsonString(PersistedServerObservabilitySettingsDocument),
30);
31​
32const isDesktopBackendObservabilitySettingsReadError = Schema.is(
33 DesktopBackendConfiguration.DesktopBackendObservabilitySettingsReadError,
34);
35​
36const serverExposureLayer = Layer.succeed(DesktopServerExposure.DesktopServerExposure, {
37 getState: Effect.die("unexpected getState"),
38 backendConfig: Effect.succeed({
39 port: 4888,
40 bindHost: "0.0.0.0",
41 httpBaseUrl: new URL("http://127.0.0.1:4888"),
42 tailscaleServeEnabled: true,
43 tailscaleServePort: 8443,
44 }),
45 configureFromSettings: () => Effect.die("unexpected configureFromSettings"),
46 setMode: () => Effect.die("unexpected setMode"),
47 setTailscaleServeEnabled: () => Effect.die("unexpected setTailscaleServeEnabled"),
48 getAdvertisedEndpoints: Effect.succeed([]),
49} satisfies DesktopServerExposure.DesktopServerExposure["Service"]);
50​
51function makeEnvironmentLayer(
52 baseDir: string,
53 options?: {
54 readonly appPath?: string;
55 readonly dirname?: string;
56 readonly isPackaged?: boolean;
57 readonly devServerUrl?: string;
58 readonly platform?: NodeJS.Platform;
59 readonly resourcesPath?: string;
60 },
61) {
62 return DesktopEnvironment.layer({
63 dirname: options?.dirname ?? "/repo/apps/desktop/src",
64 homeDirectory: baseDir,
65 platform: options?.platform ?? "darwin",
66 processArch: "x64",
67 appVersion: "1.2.3",
68 appPath: options?.appPath ?? "/repo",
69 isPackaged: options?.isPackaged ?? true,
70 resourcesPath: options?.resourcesPath ?? "/missing/resources",
71 runningUnderArm64Translation: false,
72 }).pipe(
73 Layer.provide(
74 Layer.mergeAll(
75 NodeServices.layer,
76 DesktopConfig.layerTest({
77 T3CODE_HOME: baseDir,
78 T3CODE_PORT: "9999",
79 T3CODE_MODE: "desktop",
80 T3CODE_DESKTOP_LAN_HOST: "192.168.1.50",
81 VITE_DEV_SERVER_URL: options?.devServerUrl,
82 }),
83 ),
84 ),
85 );
87​
88const restoreEnv = (name: string, value: string | undefined) => {
89 if (value === undefined) {
90 delete process.env[name];
91 } else {
92 process.env[name] = value;
93 }
94};
95​
96const withHarness = <A, E, R>(
97 effect: Effect.Effect<
98 A,
99 E,
100 | R
101 | DesktopEnvironment.DesktopEnvironment
102 | FileSystem.FileSystem
103 | DesktopBackendConfiguration.DesktopBackendConfiguration
104 >,
105) =>
106 Effect.gen(function* () {
107 const fileSystem = yield* FileSystem.FileSystem;
108 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
109 prefix: "t3-desktop-backend-config-test-",
110 });
111​
112 return yield* effect.pipe(
113 Effect.provide(
114 DesktopBackendConfiguration.layer.pipe(
115 Layer.provideMerge(serverExposureLayer),
116 Layer.provideMerge(DesktopAppSettings.layerTest()),
117 Layer.provideMerge(DesktopWslEnvironment.layerTest()),
118 Layer.provideMerge(makeEnvironmentLayer(baseDir)),
119 ),
120 ),
121 );
122 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer));
123​
124describe("DesktopBackendConfiguration", () => {
125 it.effect("resolvePrimary produces a stable scoped bootstrap token", () =>
126 withHarness(
127 Effect.gen(function* () {
128 const environment = yield* DesktopEnvironment.DesktopEnvironment;
129 const configuration = yield* DesktopBackendConfiguration.DesktopBackendConfiguration;
130​
131 const first = yield* configuration.resolvePrimary;
132 const second = yield* configuration.resolvePrimary;
133​
134 assert.equal(first.executablePath, process.execPath);
135 assert.equal(first.entryPath, environment.backendEntryPath);
136 assert.equal(first.cwd, environment.backendCwd);
137 assert.equal(first.captureOutput, true);
138 assert.equal(first.env.ELECTRON_RUN_AS_NODE, "1");
139 assert.isUndefined(first.env.T3CODE_PORT);
140 assert.isUndefined(first.env.T3CODE_MODE);
141 assert.isUndefined(first.env.T3CODE_DESKTOP_LAN_HOST);
142​
143 assert.equal(first.bootstrap.mode, "desktop");
144 assert.equal(first.bootstrap.noBrowser, true);
145 assert.equal(first.bootstrap.port, 4888);
146 assert.equal(first.bootstrap.host, "0.0.0.0");
147 assert.equal(first.bootstrap.t3Home, environment.baseDir);
148 assert.equal(first.bootstrap.tailscaleServeEnabled, true);
149 assert.equal(first.bootstrap.tailscaleServePort, 8443);
150 assert.match(first.bootstrap.desktopBootstrapToken, /^[0-9a-f]{48}$/i);
151 assert.equal(second.bootstrap.desktopBootstrapToken, first.bootstrap.desktopBootstrapToken);
152 }),
153 ),
154 );
155​
156 it.effect("resolveWsl reuses the primary's bootstrap token", () =>
157 withHarness(
158 Effect.gen(function* () {
159 const configuration = yield* DesktopBackendConfiguration.DesktopBackendConfiguration;
160​
161 const primary = yield* configuration.resolvePrimary;
162 const wsl = yield* configuration.resolveWsl({ port: 5000, distro: null });
163​
164 assert.equal(wsl.bootstrap.desktopBootstrapToken, primary.bootstrap.desktopBootstrapToken);
165 }),
166 ),
167 );
168​
169 it.effect("resolveWsl pins a default-tracking run to the concrete default distro", () =>
170 Effect.gen(function* () {
171 const fileSystem = yield* FileSystem.FileSystem;
172 const path = yield* Path.Path;
173 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
174 prefix: "t3-desktop-backend-config-test-",
175 });
176 const entryPath = path.join(baseDir, "app.asar.unpacked/apps/server/dist/bin.mjs");
177 yield* fileSystem.makeDirectory(path.dirname(entryPath), { recursive: true });
178 yield* fileSystem.writeFileString(entryPath, "");
179 const bundledNodePath = path.join(baseDir, "wsl-node/bin/node");
180 yield* fileSystem.makeDirectory(path.dirname(bundledNodePath), { recursive: true });
181 yield* fileSystem.writeFileString(bundledNodePath, "bundled-node-fixture");
182​
183 const observedDistros: Array<string | null> = [];
184 const config = yield* Effect.gen(function* () {
185 const configuration = yield* DesktopBackendConfiguration.DesktopBackendConfiguration;
186 return yield* configuration.resolveWsl({ port: 5000, distro: null });
187 }).pipe(
188 Effect.provide(
189 DesktopBackendConfiguration.layer.pipe(
190 Layer.provideMerge(serverExposureLayer),
191 Layer.provideMerge(DesktopAppSettings.layerTest()),
192 Layer.provideMerge(
193 DesktopWslEnvironment.layerTest({
194 isAvailable: true,
195 distros: [
196 { name: "Debian", isDefault: false, version: 2 },
197 { name: "Ubuntu", isDefault: true, version: 2 },
198 ],
199 windowsToWslPath: (distro) => {
200 observedDistros.push(distro);
201 return Option.some("/repo/apps/server/dist/bin.mjs");
202 },
203 ensureNodePty: (distro, _windowsRepoRoot, options) => {
204 observedDistros.push(distro);
205 assert.equal(options?.bundledNodeWindowsPath, bundledNodePath);
206 return { ok: true, nodePath: "/usr/bin/node", resolvedPath: "/usr/bin:/bin" };
207 },
208 getDistroIp: (distro) => {
209 observedDistros.push(distro);
210 return Option.some("172.27.0.99");
211 },
212 }),
213 ),
214 Layer.provideMerge(
215 makeEnvironmentLayer(baseDir, {
216 appPath: baseDir,
217 platform: "win32",
218 resourcesPath: baseDir,
219 }),
220 ),
221 ),
222 ),
223 );
224​
225 assert.equal(config.runningDistro, "Ubuntu");
226 assert.deepEqual(config.args.slice(0, 2), ["-d", "Ubuntu"]);
227 assert.deepEqual(observedDistros, ["Ubuntu", "Ubuntu", "Ubuntu"]);
228 assert.isTrue(Option.isNone(config.preflightFailure));
229 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
230 );
231​
232 it.effect("resolveWsl lets Linux replace an occupied preferred secondary port", () =>
233 Effect.gen(function* () {
234 const fileSystem = yield* FileSystem.FileSystem;
235 const path = yield* Path.Path;
236 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
237 prefix: "t3-desktop-backend-config-test-",
238 });
239 const entryPath = path.join(baseDir, "app.asar.unpacked/apps/server/dist/bin.mjs");
240 yield* fileSystem.makeDirectory(path.dirname(entryPath), { recursive: true });
241 yield* fileSystem.writeFileString(entryPath, "");
242 const bundledNodePath = path.join(baseDir, "wsl-node/bin/node");
243 yield* fileSystem.makeDirectory(path.dirname(bundledNodePath), { recursive: true });
244 yield* fileSystem.writeFileString(bundledNodePath, "bundled-node-fixture");
245​
246 let allocationInput:
247 | {
248 readonly distro: string;
249 readonly nodePath: string;
250 readonly host: string;
251 readonly preferredPort: number;
252 readonly fallbackToEphemeral: boolean;
253 }
254 | undefined;
255​
256 const config = yield* Effect.gen(function* () {
257 const configuration = yield* DesktopBackendConfiguration.DesktopBackendConfiguration;
258 return yield* configuration.resolveWsl({
259 port: 5000,
260 distro: "Ubuntu",
261 portStrategy: "wsl-auto",
262 });
263 }).pipe(
264 Effect.provide(
265 DesktopBackendConfiguration.layer.pipe(
266 Layer.provideMerge(serverExposureLayer),
267 Layer.provideMerge(DesktopAppSettings.layerTest()),
268 Layer.provideMerge(
269 DesktopWslEnvironment.layerTest({
270 isAvailable: true,
271 distros: [{ name: "Ubuntu", isDefault: true, version: 2 }],
272 windowsToWslPath: () => Option.some("/repo/apps/server/dist/bin.mjs"),
273 ensureNodePty: () => ({
274 ok: true,
275 nodePath: "/usr/bin/node",
276 resolvedPath: "/usr/bin:/bin",
277 }),
278 getDistroIp: () => Option.some("172.27.0.99"),
279 allocateTcpPort: (input) => {
280 allocationInput = input;
281 return { ok: true, port: 49152, usedEphemeralFallback: true };
282 },
283 }),
284 ),
285 Layer.provideMerge(
286 makeEnvironmentLayer(baseDir, {
287 appPath: baseDir,
288 platform: "win32",
289 resourcesPath: baseDir,
290 }),
291 ),
292 ),
293 ),
294 );
295​
296 assert.deepEqual(allocationInput, {
297 distro: "Ubuntu",
298 nodePath: "/usr/bin/node",
299 host: "172.27.0.99",
300 preferredPort: 5000,
301 fallbackToEphemeral: true,
302 });
303 assert.equal(config.bootstrap.port, 49152);
304 assert.equal(config.httpBaseUrl.href, "http://172.27.0.99:49152/");
305 assert.isTrue(Option.isNone(config.preflightFailure));
306 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
307 );
308​
309 it.effect("resolveWsl surfaces a Linux-side fixed-port collision before server spawn", () =>
310 Effect.gen(function* () {
311 const fileSystem = yield* FileSystem.FileSystem;
312 const path = yield* Path.Path;
313 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
314 prefix: "t3-desktop-backend-config-test-",
315 });
316 const entryPath = path.join(baseDir, "app.asar.unpacked/apps/server/dist/bin.mjs");
317 yield* fileSystem.makeDirectory(path.dirname(entryPath), { recursive: true });
318 yield* fileSystem.writeFileString(entryPath, "");
319 const bundledNodePath = path.join(baseDir, "wsl-node/bin/node");
320 yield* fileSystem.makeDirectory(path.dirname(bundledNodePath), { recursive: true });
321 yield* fileSystem.writeFileString(bundledNodePath, "bundled-node-fixture");
322​
323 const config = yield* Effect.gen(function* () {
324 const configuration = yield* DesktopBackendConfiguration.DesktopBackendConfiguration;
325 return yield* configuration.resolveWsl({
326 port: 5000,
327 distro: "Ubuntu",
328 portStrategy: "fixed",
329 });
330 }).pipe(
331 Effect.provide(
332 DesktopBackendConfiguration.layer.pipe(
333 Layer.provideMerge(serverExposureLayer),
334 Layer.provideMerge(DesktopAppSettings.layerTest()),
335 Layer.provideMerge(
336 DesktopWslEnvironment.layerTest({
337 isAvailable: true,
338 distros: [{ name: "Ubuntu", isDefault: true, version: 2 }],
339 windowsToWslPath: () => Option.some("/repo/apps/server/dist/bin.mjs"),
340 ensureNodePty: () => ({
341 ok: true,
342 nodePath: "/usr/bin/node",
343 resolvedPath: "/usr/bin:/bin",
344 }),
345 getDistroIp: () => Option.some("172.27.0.99"),
346 allocateTcpPort: () => ({
347 ok: false,
348 reason: "EADDRINUSE:listen EADDRINUSE 172.27.0.99:5000",
349 }),
350 }),
351 ),
352 Layer.provideMerge(
353 makeEnvironmentLayer(baseDir, {
354 appPath: baseDir,
355 platform: "win32",
356 resourcesPath: baseDir,
357 }),
358 ),
359 ),
360 ),
361 );
362​
363 const failure = Option.getOrThrow(config.preflightFailure);
364 assert.isFalse(failure.fatal);
365 assert.equal(failure.retryLimit, 3);
366 assert.include(failure.reason, "EADDRINUSE");
367 assert.include(failure.reason, "required WSL-only primary port 5000");
368 assert.equal(config.bootstrap.port, 5000);
369 assert.deepEqual(config.args, ["-d", "Ubuntu", "--", "node", "--version"]);
370 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
371 );
372​
373 it.effect(
374 "resolveWsl preserves inherited PATH with quote-sensitive values as separate args",
375 () =>
376 Effect.gen(function* () {
377 const fileSystem = yield* FileSystem.FileSystem;
378 const path = yield* Path.Path;
379 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
380 prefix: "t3-desktop-backend-config-test-",
381 });
382 const entryPath = path.join(baseDir, "app.asar.unpacked/apps/server/dist/bin.mjs");
383 yield* fileSystem.makeDirectory(path.dirname(entryPath), { recursive: true });
384 yield* fileSystem.writeFileString(entryPath, "");
385 const bundledNodePath = path.join(baseDir, "wsl-node/bin/node");
386 yield* fileSystem.makeDirectory(path.dirname(bundledNodePath), { recursive: true });
387 yield* fileSystem.writeFileString(bundledNodePath, "bundled-node-fixture");
388​
389 const nodePath = "/home/test user's/.nvm/versions/node/v22.0.0/bin/node";
390 const linuxEntryPath = "/tmp/t3 code's launch/entry file.mjs";
391 const resolvedPath = "/home/test user/bin:/opt/test's tools/bin:/usr/bin:/bin";
392 const devServerUrl = "http://127.0.0.1:5733/dev%20assets/?label=hello%20world";
393 const config = yield* Effect.gen(function* () {
394 const configuration = yield* DesktopBackendConfiguration.DesktopBackendConfiguration;
395 return yield* configuration.resolveWsl({ port: 5000, distro: "Ubuntu" });
396 }).pipe(
397 Effect.provide(
398 DesktopBackendConfiguration.layer.pipe(
399 Layer.provideMerge(serverExposureLayer),
400 Layer.provideMerge(DesktopAppSettings.layerTest()),
401 Layer.provideMerge(
402 DesktopWslEnvironment.layerTest({
403 isAvailable: true,
404 distros: [{ name: "Ubuntu", isDefault: true, version: 2 }],
405 windowsToWslPath: () => Option.some(linuxEntryPath),
406 ensureNodePty: () => ({ ok: true, nodePath, resolvedPath }),
407 getDistroIp: () => Option.some("172.27.0.99"),
408 }),
409 ),
410 Layer.provideMerge(
411 makeEnvironmentLayer(baseDir, {
412 appPath: baseDir,
413 devServerUrl,
414 isPackaged: true,
415 platform: "win32",
416 resourcesPath: baseDir,
417 }),
418 ),
419 ),
420 ),
421 );
422​
423 assert.equal(config.bootstrapDelivery, "stdin");
424 assert.deepEqual(config.args, [
425 "-d",
426 "Ubuntu",
427 "--exec",
428 "env",
429 "PATH=/home/test user's/.nvm/versions/node/v22.0.0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/home/test user/bin:/opt/test's tools/bin:/usr/bin:/bin",
430 nodePath,
431 linuxEntryPath,
432 "--bootstrap-fd",
433 "0",
434 "--dev-url",
435 devServerUrl,
436 ]);
437 assert.notInclude(config.args, "bash");
438 assert.notInclude(config.args, "/bin/sh");
439 assert.notInclude(config.args, "-c");
440 assert.isTrue(Option.isNone(config.preflightFailure));
441 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
442 );
443​
444 it.effect("resolvePrimary and resolveWsl share one token under concurrent resolution", () =>
445 withHarness(
446 Effect.gen(function* () {
447 const configuration = yield* DesktopBackendConfiguration.DesktopBackendConfiguration;
448​
449 // Resolve both before any token is cached, concurrently, so the
450 // generate step (a yield point) can interleave. The atomic
451 // get-or-create must still hand both the same token; a non-atomic
452 // Ref would let each generate its own and break the shared-token
453 // invariant.
454 const [primary, wsl] = yield* Effect.all(
455 [configuration.resolvePrimary, configuration.resolveWsl({ port: 5000, distro: null })],
456 { concurrency: "unbounded" },
457 );
458​
459 assert.equal(wsl.bootstrap.desktopBootstrapToken, primary.bootstrap.desktopBootstrapToken);
460 }),
461 ),
462 );
463​
464 it.effect("resolvePrimary surfaces persisted backend observability endpoints", () =>
465 withHarness(
466 Effect.gen(function* () {
467 const fileSystem = yield* FileSystem.FileSystem;
468 const environment = yield* DesktopEnvironment.DesktopEnvironment;
469 const configuration = yield* DesktopBackendConfiguration.DesktopBackendConfiguration;
470​
471 yield* fileSystem.makeDirectory(environment.path.dirname(environment.serverSettingsPath), {
472 recursive: true,
473 });
474 yield* fileSystem.writeFileString(
475 environment.serverSettingsPath,
476 yield* encodePersistedServerObservabilitySettingsDocument({
477 observability: {
478 otlpTracesUrl: " http://127.0.0.1:4318/v1/traces ",
479 otlpMetricsUrl: " http://127.0.0.1:4318/v1/metrics ",
480 },
481 }),
482 );
483​
484 const config = yield* configuration.resolvePrimary;
485 assert.equal(config.bootstrap.otlpTracesUrl, "http://127.0.0.1:4318/v1/traces");
486 assert.equal(config.bootstrap.otlpMetricsUrl, "http://127.0.0.1:4318/v1/metrics");
487 }),
488 ),
489 );
490​
491 it.effect("resolvePrimary omits backend observability endpoints when settings are missing", () =>
492 withHarness(
493 Effect.gen(function* () {
494 const configuration = yield* DesktopBackendConfiguration.DesktopBackendConfiguration;
495 const config = yield* configuration.resolvePrimary;
496​
497 assert.isUndefined(config.bootstrap.otlpTracesUrl);
498 assert.isUndefined(config.bootstrap.otlpMetricsUrl);
499 }),
500 ),
501 );
502​
503 it.effect("logs structured context when persisted observability settings cannot be read", () =>
504 Effect.gen(function* () {
505 const fileSystem = yield* FileSystem.FileSystem;
506 const path = yield* Path.Path;
507 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
508 prefix: "t3-desktop-backend-config-test-",
509 });
510 const settingsPath = path.join(baseDir, "userdata", "settings.json");
511 const cause = PlatformError.systemError({
512 _tag: "PermissionDenied",
513 module: "FileSystem",
514 method: "readFileString",
515 pathOrDescriptor: settingsPath,
516 });
517 const messages: Array<unknown> = [];
518 const logger = Logger.make(({ message }) => {
519 messages.push(message);
520 });
521 const failingFileSystemLayer = Layer.succeed(
522 FileSystem.FileSystem,
523 FileSystem.makeNoop({
524 readFileString: () => Effect.fail(cause),
525 }),
526 );
527​
528 const config = yield* Effect.gen(function* () {
529 const configuration = yield* DesktopBackendConfiguration.DesktopBackendConfiguration;
530 return yield* configuration.resolvePrimary;
531 }).pipe(
532 Effect.provide(
533 Layer.mergeAll(
534 DesktopBackendConfiguration.layer.pipe(
535 Layer.provideMerge(serverExposureLayer),
536 Layer.provideMerge(DesktopAppSettings.layerTest()),
537 Layer.provideMerge(DesktopWslEnvironment.layerTest()),
538 Layer.provideMerge(makeEnvironmentLayer(baseDir)),
539 Layer.provideMerge(failingFileSystemLayer),
540 ),
541 Logger.layer([logger], { mergeWithExisting: false }),
542 ),
543 ),
544 );
545​
546 assert.isUndefined(config.bootstrap.otlpTracesUrl);
547 assert.isUndefined(config.bootstrap.otlpMetricsUrl);
548​
549 const error = messages
550 .flatMap((message) => (Array.isArray(message) ? message : [message]))
551 .find(isDesktopBackendObservabilitySettingsReadError);
552 assert.isDefined(error);
553 assert.equal(error.settingsPath, settingsPath);
554 assert.equal(error.cause, cause);
555 assert.equal(
556 error.message,
557 `Failed to read persisted backend observability settings at ${settingsPath}.`,
558 );
559 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
560 );
561​
562 it.effect("resolvePrimary captures backend output in dev so child logs can be persisted", () =>
563 Effect.gen(function* () {
564 const fileSystem = yield* FileSystem.FileSystem;
565 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
566 prefix: "t3-desktop-backend-config-test-",
567 });
568​
569 yield* Effect.gen(function* () {
570 const configuration = yield* DesktopBackendConfiguration.DesktopBackendConfiguration;
571 const config = yield* configuration.resolvePrimary;
572 assert.equal(config.captureOutput, true);
573 }).pipe(
574 Effect.provide(
575 DesktopBackendConfiguration.layer.pipe(
576 Layer.provideMerge(serverExposureLayer),
577 Layer.provideMerge(DesktopAppSettings.layerTest()),
578 Layer.provideMerge(DesktopWslEnvironment.layerTest()),
579 Layer.provideMerge(
580 makeEnvironmentLayer(baseDir, {
581 isPackaged: false,
582 devServerUrl: "http://127.0.0.1:5733",
583 }),
584 ),
585 ),
586 ),
587 );
588 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
589 );
590​
591 it.effect("resolveWsl preserves existing WSLENV entries when forwarding backend secrets", () =>
592 Effect.gen(function* () {
593 const fileSystem = yield* FileSystem.FileSystem;
594 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
595 prefix: "t3-desktop-backend-config-test-",
596 });
597​
598 const previousWslEnv = process.env.WSLENV;
599 const previousOpenAiKey = process.env.OPENAI_API_KEY;
600 const previousAnthropicKey = process.env.ANTHROPIC_API_KEY;
601 try {
602 process.env.WSLENV = "GOPATH/p:OPENAI_API_KEY/u:EMPTY::AZURE_DEVOPS_EXT_PAT/u";
603 process.env.OPENAI_API_KEY = "openai-key";
604 process.env.ANTHROPIC_API_KEY = "anthropic-key";
605​
606 yield* Effect.gen(function* () {
607 const configuration = yield* DesktopBackendConfiguration.DesktopBackendConfiguration;
608 const config = yield* configuration.resolveWsl({ port: 5050, distro: null });
609​
610 assert.equal(config.executablePath, "wsl.exe");
611 assert.equal(config.bootstrap.port, 5050);
612 // NAT-mode WSL2 binds only the concrete distro interface, never
613 // every Linux interface. The renderer URL uses the same address.
614 assert.equal(config.bootstrap.host, "172.27.0.99");
615 assert.equal(config.bootstrap.tailscaleServeEnabled, false);
616 assert.notProperty(config.bootstrap, "desktopTelemetryFd");
617 assert.notProperty(config.bootstrap, "resourceMonitorPath");
618 // httpBaseUrl uses the resolved distro IP from the test stub,
619 // not localhost — the renderer reaches the backend directly to
620 // avoid relying on wslhost forwarding.
621 assert.equal(config.httpBaseUrl.href, "http://172.27.0.99:5050/");
622 assert.equal(config.env.OPENAI_API_KEY, "openai-key");
623 assert.equal(config.env.ANTHROPIC_API_KEY, "anthropic-key");
624 // The existing WSLENV is preserved byte-for-byte (note the empty
625 // "::" segment survives — WSL ignores it, so we don't normalize
626 // it away) and ANTHROPIC_API_KEY is appended. OPENAI_API_KEY is
627 // already declared, so it isn't forwarded twice.
628 assert.equal(
629 config.env.WSLENV,
630 "GOPATH/p:OPENAI_API_KEY/u:EMPTY::AZURE_DEVOPS_EXT_PAT/u:ANTHROPIC_API_KEY",
631 );
632 }).pipe(
633 Effect.provide(
634 DesktopBackendConfiguration.layer.pipe(
635 Layer.provideMerge(serverExposureLayer),
636 Layer.provideMerge(DesktopAppSettings.layerTest()),
637 Layer.provideMerge(
638 DesktopWslEnvironment.layerTest({
639 isAvailable: true,
640 windowsToWslPath: () => Option.some("/mnt/c/repo/apps/server/src/index.ts"),
641 getDistroIp: () => Option.some("172.27.0.99"),
642 }),
643 ),
644 Layer.provideMerge(makeEnvironmentLayer(baseDir, { platform: "win32" })),
645 ),
646 ),
647 );
648 } finally {
649 restoreEnv("WSLENV", previousWslEnv);
650 restoreEnv("OPENAI_API_KEY", previousOpenAiKey);
651 restoreEnv("ANTHROPIC_API_KEY", previousAnthropicKey);
652 }
653 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
654 );
655​
656 it.effect("resolveWsl fails closed when the packaged Linux Node runtime is missing", () =>
657 Effect.gen(function* () {
658 const fileSystem = yield* FileSystem.FileSystem;
659 const path = yield* Path.Path;
660 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
661 prefix: "t3-desktop-backend-config-test-",
662 });
663 const entryPath = path.join(baseDir, "app.asar.unpacked/apps/server/dist/bin.mjs");
664 yield* fileSystem.makeDirectory(path.dirname(entryPath), { recursive: true });
665 yield* fileSystem.writeFileString(entryPath, "");
666​
667 let nodePtyProbeCount = 0;
668 yield* Effect.gen(function* () {
669 const configuration = yield* DesktopBackendConfiguration.DesktopBackendConfiguration;
670 const config = yield* configuration.resolveWsl({ port: 5050, distro: "Ubuntu" });
671 const failure = Option.getOrThrow(config.preflightFailure);
672​
673 assert.isTrue(failure.fatal);
674 assert.include(failure.reason, "missing packaged WSL Node runtime");
675 assert.include(failure.reason, "wsl-node");
676 assert.equal(nodePtyProbeCount, 0);
677 }).pipe(
678 Effect.provide(
679 DesktopBackendConfiguration.layer.pipe(
680 Layer.provideMerge(serverExposureLayer),
681 Layer.provideMerge(DesktopAppSettings.layerTest()),
682 Layer.provideMerge(
683 DesktopWslEnvironment.layerTest({
684 isAvailable: true,
685 distros: [{ name: "Ubuntu", isDefault: true, version: 2 }],
686 windowsToWslPath: () => Option.some("/should/not/reach/native-probe"),
687 ensureNodePty: () => {
688 nodePtyProbeCount += 1;
689 return { ok: true, nodePath: "/usr/bin/node", resolvedPath: "/usr/bin:/bin" };
690 },
691 }),
692 ),
693 Layer.provideMerge(
694 makeEnvironmentLayer(baseDir, {
695 appPath: baseDir,
696 platform: "win32",
697 resourcesPath: baseDir,
698 }),
699 ),
700 ),
701 ),
702 );
703 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
704 );
705​
706 it.effect("resolveWsl rejects WSL1 before path or native-module preflight", () =>
707 Effect.gen(function* () {
708 const fileSystem = yield* FileSystem.FileSystem;
709 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
710 prefix: "t3-desktop-backend-config-test-",
711 });
712​
713 let pathProbeCount = 0;
714 let nodePtyProbeCount = 0;
715 yield* Effect.gen(function* () {
716 const configuration = yield* DesktopBackendConfiguration.DesktopBackendConfiguration;
717 const config = yield* configuration.resolveWsl({ port: 5050, distro: "Legacy-Ubuntu" });
718 const failure = Option.getOrThrow(config.preflightFailure);
719​
720 assert.isTrue(failure.fatal);
721 assert.include(failure.reason, "WSL 1");
722 assert.include(failure.reason, 'wsl.exe --set-version "Legacy-Ubuntu" 2');
723 assert.equal(pathProbeCount, 0);
724 assert.equal(nodePtyProbeCount, 0);
725 assert.equal(config.bootstrap.host, "127.0.0.1");
726 assert.isUndefined(config.runningDistro);
727 }).pipe(
728 Effect.provide(
729 DesktopBackendConfiguration.layer.pipe(
730 Layer.provideMerge(serverExposureLayer),
731 Layer.provideMerge(DesktopAppSettings.layerTest()),
732 Layer.provideMerge(
733 DesktopWslEnvironment.layerTest({
734 isAvailable: true,
735 distros: [{ name: "Legacy-Ubuntu", isDefault: true, version: 1 }],
736 windowsToWslPath: () => {
737 pathProbeCount += 1;
738 return Option.some("/should/not/be/used");
739 },
740 ensureNodePty: () => {
741 nodePtyProbeCount += 1;
742 return { ok: true, nodePath: "/usr/bin/node", resolvedPath: "/usr/bin:/bin" };
743 },
744 }),
745 ),
746 Layer.provideMerge(makeEnvironmentLayer(baseDir, { platform: "win32" })),
747 ),
748 ),
749 );
750 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
751 );
752​
753 it.effect("resolveWsl falls back to loopback when the WSL2 address probe fails", () =>
754 Effect.gen(function* () {
755 const fileSystem = yield* FileSystem.FileSystem;
756 const path = yield* Path.Path;
757 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
758 prefix: "t3-desktop-backend-config-test-",
759 });
760 const entryPath = path.join(baseDir, "app.asar.unpacked/apps/server/dist/bin.mjs");
761 yield* fileSystem.makeDirectory(path.dirname(entryPath), { recursive: true });
762 yield* fileSystem.writeFileString(entryPath, "");
763 const bundledNodePath = path.join(baseDir, "wsl-node/bin/node");
764 yield* fileSystem.makeDirectory(path.dirname(bundledNodePath), { recursive: true });
765 yield* fileSystem.writeFileString(bundledNodePath, "bundled-node-fixture");
766​
767 yield* Effect.gen(function* () {
768 const configuration = yield* DesktopBackendConfiguration.DesktopBackendConfiguration;
769 const config = yield* configuration.resolveWsl({ port: 5050, distro: "Ubuntu" });
770​
771 assert.isTrue(Option.isNone(config.preflightFailure));
772 assert.equal(config.bootstrap.host, "127.0.0.1");
773 assert.equal(config.httpBaseUrl.href, "http://127.0.0.1:5050/");
774 }).pipe(
775 Effect.provide(
776 DesktopBackendConfiguration.layer.pipe(
777 Layer.provideMerge(serverExposureLayer),
778 Layer.provideMerge(DesktopAppSettings.layerTest()),
779 Layer.provideMerge(
780 DesktopWslEnvironment.layerTest({
781 isAvailable: true,
782 distros: [{ name: "Ubuntu", isDefault: true, version: 2 }],
783 windowsToWslPath: () => Option.some("/repo/apps/server/dist/bin.mjs"),
784 ensureNodePty: () => ({
785 ok: true,
786 nodePath: "/usr/bin/node",
787 resolvedPath: "/usr/bin:/bin",
788 }),
789 getDistroIp: () => Option.none(),
790 }),
791 ),
792 Layer.provideMerge(
793 makeEnvironmentLayer(baseDir, {
794 appPath: baseDir,
795 platform: "win32",
796 resourcesPath: baseDir,
797 }),
798 ),
799 ),
800 ),
801 );
802 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
803 );
804​
805 it.effect(
806 "resolvePrimary falls back to the Windows primary when wsl-only but WSL is unavailable",
807 () =>
808 Effect.gen(function* () {
809 const fileSystem = yield* FileSystem.FileSystem;
810 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
811 prefix: "t3-desktop-backend-config-test-",
812 });
813​
814 yield* Effect.gen(function* () {
815 const environment = yield* DesktopEnvironment.DesktopEnvironment;
816 const configuration = yield* DesktopBackendConfiguration.DesktopBackendConfiguration;
817 const config = yield* configuration.resolvePrimary;
818​
819 // wsl-only is persisted but WSL is unavailable, so the primary must
820 // not spawn wsl.exe (which would loop on preflight failures while the
821 // Connections backend control is hidden). Resolve the Windows primary.
822 assert.equal(config.executablePath, process.execPath);
823 assert.equal(config.bootstrap.t3Home, environment.baseDir);
824 assert.isTrue(Option.isNone(config.preflightFailure));
825 }).pipe(
826 Effect.provide(
827 DesktopBackendConfiguration.layer.pipe(
828 Layer.provideMerge(serverExposureLayer),
829 Layer.provideMerge(
830 DesktopAppSettings.layerTest({
831 ...DesktopAppSettings.DEFAULT_DESKTOP_SETTINGS,
832 wslBackendEnabled: true,
833 wslOnly: true,
834 }),
835 ),
836 Layer.provideMerge(DesktopWslEnvironment.layerTest({ isAvailable: false })),
837 Layer.provideMerge(makeEnvironmentLayer(baseDir, { platform: "win32" })),
838 ),
839 ),
840 );
841 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
842 );
843​
844 it.effect(
845 "resolvePrimary marks a removed persisted WSL distro as a fatal preflight failure",
846 () =>
847 Effect.gen(function* () {
848 const fileSystem = yield* FileSystem.FileSystem;
849 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
850 prefix: "t3-desktop-backend-config-test-",
851 });
852​
853 yield* Effect.gen(function* () {
854 const configuration = yield* DesktopBackendConfiguration.DesktopBackendConfiguration;
855 const config = yield* configuration.resolvePrimary;
856 const failure = Option.getOrThrow(config.preflightFailure);
857​
858 assert.equal(config.executablePath, "wsl.exe");
859 assert.isTrue(failure.fatal);
860 assert.include(failure.reason, "Removed-Distro");
861 }).pipe(
862 Effect.provide(
863 DesktopBackendConfiguration.layer.pipe(
864 Layer.provideMerge(serverExposureLayer),
865 Layer.provideMerge(
866 DesktopAppSettings.layerTest({
867 ...DesktopAppSettings.DEFAULT_DESKTOP_SETTINGS,
868 wslBackendEnabled: true,
869 wslOnly: true,
870 wslDistro: "Removed-Distro",
871 }),
872 ),
873 Layer.provideMerge(
874 DesktopWslEnvironment.layerTest({
875 isAvailable: true,
876 distros: [{ name: "Ubuntu", isDefault: true, version: 2 }],
877 }),
878 ),
879 Layer.provideMerge(makeEnvironmentLayer(baseDir, { platform: "win32" })),
880 ),
881 ),
882 );
883 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
884 );
885​
886 it.effect("resolveWsl keeps a transient distro-list failure retryable", () =>
887 Effect.gen(function* () {
888 const fileSystem = yield* FileSystem.FileSystem;
889 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
890 prefix: "t3-desktop-backend-config-test-",
891 });
892​
893 yield* Effect.gen(function* () {
894 const configuration = yield* DesktopBackendConfiguration.DesktopBackendConfiguration;
895 const config = yield* configuration.resolveWsl({ port: 5050, distro: "Ubuntu" });
896 const failure = Option.getOrThrow(config.preflightFailure);
897​
898 assert.isFalse(failure.fatal);
899 assert.equal(failure.retryLimit, 12);
900 assert.include(failure.reason, "timed out");
901 }).pipe(
902 Effect.provide(
903 DesktopBackendConfiguration.layer.pipe(
904 Layer.provideMerge(serverExposureLayer),
905 Layer.provideMerge(DesktopAppSettings.layerTest()),
906 Layer.provideMerge(
907 DesktopWslEnvironment.layerTest({
908 isAvailable: true,
909 distroListError: new DesktopWslEnvironment.DesktopWslDistroListError({
910 reason: "wsl.exe --list --verbose timed out",
911 }),
912 }),
913 ),
914 Layer.provideMerge(makeEnvironmentLayer(baseDir, { platform: "win32" })),
915 ),
916 ),
917 );
918 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
919 );
920​
921 it.effect("resolveWsl marks a missing packaged server entry as fatal", () =>
922 Effect.gen(function* () {
923 const fileSystem = yield* FileSystem.FileSystem;
924 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
925 prefix: "t3-desktop-backend-config-test-",
926 });
927​
928 yield* Effect.gen(function* () {
929 const configuration = yield* DesktopBackendConfiguration.DesktopBackendConfiguration;
930 const config = yield* configuration.resolveWsl({ port: 5050, distro: "Ubuntu" });
931 const failure = Option.getOrThrow(config.preflightFailure);
932​
933 assert.isTrue(failure.fatal);
934 assert.include(failure.reason, "missing server entry");
935 }).pipe(
936 Effect.provide(
937 DesktopBackendConfiguration.layer.pipe(
938 Layer.provideMerge(serverExposureLayer),
939 Layer.provideMerge(DesktopAppSettings.layerTest()),
940 Layer.provideMerge(
941 DesktopWslEnvironment.layerTest({
942 isAvailable: true,
943 distros: [{ name: "Ubuntu", isDefault: true, version: 2 }],
944 }),
945 ),
946 Layer.provideMerge(makeEnvironmentLayer(baseDir, { platform: "win32" })),
947 ),
948 ),
949 );
950 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
951 );
952​
953 it.effect("resolveWsl marks a missing selected distro as a fatal preflight failure", () =>
954 Effect.gen(function* () {
955 const fileSystem = yield* FileSystem.FileSystem;
956 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
957 prefix: "t3-desktop-backend-config-test-",
958 });
959​
960 yield* Effect.gen(function* () {
961 const configuration = yield* DesktopBackendConfiguration.DesktopBackendConfiguration;
962 const config = yield* configuration.resolveWsl({ port: 5050, distro: "Removed-Distro" });
963 const failure = Option.getOrThrow(config.preflightFailure);
964​
965 assert.isTrue(failure.fatal);
966 assert.include(failure.reason, "Removed-Distro");
967 }).pipe(
968 Effect.provide(
969 DesktopBackendConfiguration.layer.pipe(
970 Layer.provideMerge(serverExposureLayer),
971 Layer.provideMerge(DesktopAppSettings.layerTest()),
972 Layer.provideMerge(
973 DesktopWslEnvironment.layerTest({
974 isAvailable: true,
975 distros: [{ name: "Ubuntu", isDefault: true, version: 2 }],
976 }),
977 ),
978 Layer.provideMerge(makeEnvironmentLayer(baseDir, { platform: "win32" })),
979 ),
980 ),
981 );
982 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
983 );
984​
985 it.effect("resolvePrimaryLabel reports the WSL distro when wsl-only and WSL is available", () =>
986 Effect.gen(function* () {
987 const fileSystem = yield* FileSystem.FileSystem;
988 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
989 prefix: "t3-desktop-backend-config-test-",
990 });
991​
992 yield* Effect.gen(function* () {
993 const configuration = yield* DesktopBackendConfiguration.DesktopBackendConfiguration;
994 const label = yield* configuration.resolvePrimaryLabel;
995 assert.equal(label, "WSL (Ubuntu)");
996 }).pipe(
997 Effect.provide(
998 DesktopBackendConfiguration.layer.pipe(
999 Layer.provideMerge(serverExposureLayer),
1000 Layer.provideMerge(
1001 DesktopAppSettings.layerTest({
1002 ...DesktopAppSettings.DEFAULT_DESKTOP_SETTINGS,
1003 wslBackendEnabled: true,
1004 wslOnly: true,
1005 wslDistro: "Ubuntu",
1006 }),
1007 ),
1008 Layer.provideMerge(DesktopWslEnvironment.layerTest({ isAvailable: true })),
1009 Layer.provideMerge(makeEnvironmentLayer(baseDir, { platform: "win32" })),
1010 ),
1011 ),
1012 );
1013 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
1014 );
1015​
1016 it.effect("prefers the external packaged resource monitor over the copy inside the asar", () =>
1017 Effect.gen(function* () {
1018 const fileSystem = yield* FileSystem.FileSystem;
1019 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
1020 prefix: "t3-desktop-backend-config-test-",
1021 });
1022 const resourcesPath = `${baseDir}/resources`;
1023 const dirname = `${resourcesPath}/app.asar/apps/desktop/dist-electron`;
1024 const embeddedMonitorPath = `${resourcesPath}/app.asar/apps/desktop/prod-resources/resource-monitor/t3-resource-monitor`;
1025 const monitorPath = `${resourcesPath}/resource-monitor/t3-resource-monitor`;
1026 yield* fileSystem.makeDirectory(
1027 `${resourcesPath}/app.asar/apps/desktop/prod-resources/resource-monitor`,
1028 { recursive: true },
1029 );
1030 yield* fileSystem.makeDirectory(`${resourcesPath}/resource-monitor`, {
1031 recursive: true,
1032 });
1033 yield* fileSystem.writeFileString(embeddedMonitorPath, "embedded");
1034 yield* fileSystem.writeFileString(monitorPath, "binary");
1035 yield* fileSystem.chmod(monitorPath, 0o755);
1036​
1037 yield* Effect.gen(function* () {
1038 const configuration = yield* DesktopBackendConfiguration.DesktopBackendConfiguration;
1039 const config = yield* configuration.resolvePrimary;
1040 assert.equal(config.bootstrap.resourceMonitorPath, monitorPath);
1041 assert.equal(config.bootstrap.desktopTelemetryFd, 4);
1042 assert.equal(config.bootstrap.desktopTelemetryControlFd, 5);
1043 }).pipe(
1044 Effect.provide(
1045 DesktopBackendConfiguration.layer.pipe(
1046 Layer.provideMerge(serverExposureLayer),
1047 Layer.provideMerge(DesktopAppSettings.layerTest()),
1048 Layer.provideMerge(DesktopWslEnvironment.layerTest()),
1049 Layer.provideMerge(
1050 makeEnvironmentLayer(baseDir, {
1051 appPath: `${resourcesPath}/app.asar`,
1052 dirname,
1053 isPackaged: true,
1054 resourcesPath,
1055 }),
1056 ),
1057 ),
1058 ),
1059 );
1060 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
1061 );
1062​
1063 it.effect("prefers the release resource monitor when both development builds exist", () =>
1064 Effect.gen(function* () {
1065 const fileSystem = yield* FileSystem.FileSystem;
1066 const path = yield* Path.Path;
1067 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
1068 prefix: "t3-desktop-backend-config-test-",
1069 });
1070 const dirname = path.join(baseDir, "apps/desktop/src");
1071 const releaseMonitorPath = path.join(
1072 baseDir,
1073 "native/resource-monitor/target/release/t3-resource-monitor",
1074 );
1075 const debugMonitorPath = path.join(
1076 baseDir,
1077 "native/resource-monitor/target/debug/t3-resource-monitor",
1078 );
1079 yield* fileSystem.makeDirectory(path.dirname(releaseMonitorPath), { recursive: true });
1080 yield* fileSystem.makeDirectory(path.dirname(debugMonitorPath), { recursive: true });
1081 yield* fileSystem.writeFileString(releaseMonitorPath, "release");
1082 yield* fileSystem.writeFileString(debugMonitorPath, "debug");
1083​
1084 yield* Effect.gen(function* () {
1085 const configuration = yield* DesktopBackendConfiguration.DesktopBackendConfiguration;
1086 const config = yield* configuration.resolvePrimary;
1087 assert.equal(config.bootstrap.resourceMonitorPath, releaseMonitorPath);
1088 }).pipe(
1089 Effect.provide(
1090 DesktopBackendConfiguration.layer.pipe(
1091 Layer.provideMerge(serverExposureLayer),
1092 Layer.provideMerge(DesktopAppSettings.layerTest()),
1093 Layer.provideMerge(DesktopWslEnvironment.layerTest()),
1094 Layer.provideMerge(
1095 makeEnvironmentLayer(baseDir, {
1096 dirname,
1097 devServerUrl: "http://127.0.0.1:5733",
1098 isPackaged: false,
1099 }),
1100 ),
1101 ),
1102 ),
1103 );
1104 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
1105 );
1106​
1107 it.effect("resolvePrimaryLabel reports the local environment on non-Windows platforms", () =>
1108 withHarness(
1109 Effect.gen(function* () {
1110 const configuration = yield* DesktopBackendConfiguration.DesktopBackendConfiguration;
1111 const label = yield* configuration.resolvePrimaryLabel;
1112 assert.equal(label, "Local environment");
1113 }),
1114 ),
1115 );
1116​
1117 it.effect("resolvePrimaryLabel reports Windows when wsl-only but WSL is unavailable", () =>
1118 Effect.gen(function* () {
1119 const fileSystem = yield* FileSystem.FileSystem;
1120 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
1121 prefix: "t3-desktop-backend-config-test-",
1122 });
1123​
1124 yield* Effect.gen(function* () {
1125 const configuration = yield* DesktopBackendConfiguration.DesktopBackendConfiguration;
1126 // Mirrors the resolvePrimary fall-back: the label must follow the
1127 // backend that actually resolves, not the persisted preference, so the
1128 // env switcher can't show "WSL" for a Windows backend.
1129 const label = yield* configuration.resolvePrimaryLabel;
1130 assert.equal(label, "Windows");
1131 }).pipe(
1132 Effect.provide(
1133 DesktopBackendConfiguration.layer.pipe(
1134 Layer.provideMerge(serverExposureLayer),
1135 Layer.provideMerge(
1136 DesktopAppSettings.layerTest({
1137 ...DesktopAppSettings.DEFAULT_DESKTOP_SETTINGS,
1138 wslBackendEnabled: true,
1139 wslOnly: true,
1140 wslDistro: "Ubuntu",
1141 }),
1142 ),
1143 Layer.provideMerge(DesktopWslEnvironment.layerTest({ isAvailable: false })),
1144 Layer.provideMerge(makeEnvironmentLayer(baseDir, { platform: "win32" })),
1145 ),
1146 ),
1147 );
1148 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
1149 );
1150​
1151 it("resolvePrimaryLabel is runSync-safe against the real WSL availability probe", async () => {
1152 // getLocalEnvironmentBootstraps is a sync IPC method: it resolves the
1153 // primary instance's lazy label through Effect.runSync. The label chains
1154 // to wslEnvironment.isAvailable, whose real layer probes the filesystem.
1155 // That probe must run once at layer build and expose a resolved value, not
1156 // a live async effect — otherwise runSync throws in the handler. Build the
1157 // real WSL layer (not the sync test stub) and resolve the label with a
1158 // top-level runSync, exactly as the handler does.
1159 // oxlint-disable-next-line t3code/no-manual-effect-runtime-in-tests -- This test intentionally replicates the sync IPC handler's runSync path to catch a regression to async-only resolution; it.effect would mask it.
1160 const runtime = ManagedRuntime.make(
1161 DesktopBackendConfiguration.layer.pipe(
1162 Layer.provideMerge(serverExposureLayer),
1163 Layer.provideMerge(DesktopAppSettings.layerTest()),
1164 Layer.provideMerge(DesktopWslEnvironment.layer),
1165 // isAvailable on win32 only touches the filesystem, never the spawner,
1166 // so a die-stub is enough to satisfy the layer's deps.
1167 Layer.provideMerge(
1168 Layer.succeed(
1169 ChildProcessSpawner.ChildProcessSpawner,
1170 ChildProcessSpawner.make(() =>
1171 Effect.die("spawner should not be used while probing WSL availability"),
1172 ),
1173 ),
1174 ),
1175 Layer.provideMerge(makeEnvironmentLayer("/tmp/t3-wsl-isavailable", { platform: "win32" })),
1176 Layer.provide(NodeServices.layer),
1177 ),
1178 );
1179 try {
1180 const configuration = await runtime.runPromise(
1181 DesktopBackendConfiguration.DesktopBackendConfiguration,
1182 );
1183 // oxlint-disable-next-line t3code/no-manual-effect-runtime-in-tests -- Same reason: this is the synchronous resolution the IPC handler performs.
1184 const label = Effect.runSync(configuration.resolvePrimaryLabel);
1185 assert.equal(typeof label, "string");
1186 } finally {
1187 await runtime.dispose();
1189 });
1190});