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

DesktopBackendPool.test.ts

152 lines6.5 KB

1import { assert, describe, it } from "@effect/vitest";
2import * as Effect from "effect/Effect";
3import * as FileSystem from "effect/FileSystem";
4import * as Layer from "effect/Layer";
5import * as Option from "effect/Option";
6import * as Ref from "effect/Ref";
7import * as Stream from "effect/Stream";
8import { HttpClient } from "effect/http";
9import { ChildProcessSpawner } from "effect/process";
10​
11import * as DesktopObservability from "../app/DesktopObservability.ts";
12import * as DesktopAppSettings from "../settings/DesktopAppSettings.ts";
13import * as DesktopTelemetryPublisher from "../telemetry/DesktopTelemetryPublisher.ts";
14import * as DesktopBrowserHost from "../preview/DesktopBrowserHost.ts";
15import * as ElectronDialog from "../electron/ElectronDialog.ts";
16import * as ElectronApp from "../electron/ElectronApp.ts";
17import * as DesktopWindow from "../window/DesktopWindow.ts";
18import * as DesktopWslEnvironment from "../wsl/DesktopWslEnvironment.ts";
19import * as DesktopBackendConfiguration from "./DesktopBackendConfiguration.ts";
20import * as DesktopBackendPool from "./DesktopBackendPool.ts";
21import type { DesktopBackendStartConfig } from "./DesktopBackendManager.ts";
22​
23function layerPool(labelRef: Ref.Ref<string>): Layer.Layer<DesktopBackendPool.DesktopBackendPool> {
24 return DesktopBackendPool.layer.pipe(
25 Layer.provideMerge(
26 Layer.mergeAll(
27 FileSystem.layerNoop({}),
28 Layer.succeed(
29 ChildProcessSpawner.ChildProcessSpawner,
30 ChildProcessSpawner.make(() => Effect.die("unexpected child process spawn")),
31 ),
32 Layer.succeed(
33 HttpClient.HttpClient,
34 HttpClient.make(() => Effect.die("unexpected HTTP request")),
35 ),
36 Layer.succeed(DesktopObservability.DesktopBackendOutputLogFactory, {
37 forInstance: () =>
38 Effect.succeed({
39 beginSession: () => Effect.void,
40 writeOutputChunk: () => Effect.void,
41 persistFailureSnapshot: () => Effect.void,
42 persistFailure: () => Effect.void,
43 discardSession: Effect.void,
44 } satisfies DesktopObservability.DesktopBackendOutputLogShape),
45 } satisfies DesktopObservability.DesktopBackendOutputLogFactory["Service"]),
46 Layer.succeed(DesktopTelemetryPublisher.DesktopTelemetryPublisher, {
47 latest: Effect.succeedNone,
48 changes: Stream.empty,
49 encoded: Stream.empty,
50 handleControlForSource: () => Effect.void,
51 removeControlSource: () => Effect.void,
52 publishUpdateReport: () => Effect.void,
53 updateRequests: Stream.empty,
54 updateCommits: Stream.empty,
55 updateCancellations: Stream.empty,
56 }),
57 DesktopBrowserHost.layer,
58 Layer.succeed(DesktopBackendConfiguration.DesktopBackendConfiguration, {
59 resolvePrimary: Effect.die("unexpected primary config resolve"),
60 resolvePrimaryLabel: Ref.get(labelRef),
61 resolveWsl: () => Effect.die("unexpected WSL config resolve"),
62 currentBootstrapToken: Effect.die("unexpected bootstrap token read"),
63 } satisfies DesktopBackendConfiguration.DesktopBackendConfiguration["Service"]),
64 DesktopAppSettings.layerTest(),
65 DesktopWslEnvironment.layerTest(),
66 ElectronDialog.layer,
67 ElectronApp.layer,
68 Layer.succeed(DesktopWindow.DesktopWindow, {
69 createMain: Effect.die("unexpected window create"),
70 ensureMain: Effect.die("unexpected window ensure"),
71 revealOrCreateMain: Effect.die("unexpected window reveal"),
72 activate: Effect.die("unexpected window activate"),
73 createMainIfBackendReady: Effect.die("unexpected window create"),
74 showConnectingSplash: Effect.void,
75 handleBackendReady: () => Effect.void,
76 handleBackendNotReady: Effect.void,
77 flushMainWindowBounds: Effect.void,
78 prepareCaptureReveal: Effect.void,
79 dispatchMenuAction: () => Effect.die("unexpected menu action"),
80 dispatchSnapShotEvent: () => Effect.void,
81 zoomMain: () => Effect.die("unexpected zoom"),
82 runMainContentsCommand: () => Effect.die("unexpected main contents command"),
83 syncAppearance: Effect.void,
84 } satisfies DesktopWindow.DesktopWindow["Service"]),
85 ),
86 ),
87 );
89​
90describe("DesktopBackendPool", () => {
91 it.effect("resolves the primary label lazily after pool layer construction", () =>
92 Effect.scoped(
93 Effect.gen(function* () {
94 const labelRef = yield* Ref.make("Windows");
95 const pool = yield* DesktopBackendPool.DesktopBackendPool.pipe(
96 Effect.provide(layerPool(labelRef)),
97 );
98 const primary = yield* pool.primary;
99​
100 yield* Ref.set(labelRef, "WSL (Ubuntu)");
101​
102 assert.equal(yield* primary.label, "WSL (Ubuntu)");
103 }),
104 ),
105 );
106​
107 it.effect("unregister stops the registered instance", () =>
108 Effect.scoped(
109 Effect.gen(function* () {
110 const id = DesktopBackendPool.BackendInstanceId("wsl:Ubuntu");
111 const pool = yield* DesktopBackendPool.DesktopBackendPool;
112 const instance = yield* pool.register({
113 id,
114 label: Effect.succeed("WSL (Ubuntu)"),
115 // A transient preflight failure leaves a restart fiber scheduled
116 // without needing a real child process.
117 configResolve: Effect.succeed({
118 executablePath: "/electron",
119 args: [],
120 entryPath: "/server/bin.mjs",
121 cwd: "/server",
122 env: {},
123 bootstrap: {
124 mode: "desktop",
125 noBrowser: true,
126 port: 3774,
127 t3Home: "/tmp/t3",
128 host: "127.0.0.1",
129 desktopBootstrapToken: "token",
130 tailscaleServeEnabled: false,
131 tailscaleServePort: 443,
132 },
133 bootstrapDelivery: "fd3",
134 extendEnv: true,
135 httpBaseUrl: new URL("http://127.0.0.1:3774"),
136 captureOutput: false,
137 preflightFailure: Option.some({ reason: "WSL starting", fatal: false }),
138 } satisfies DesktopBackendStartConfig),
139 });
140 yield* instance.start;
141 assert.equal((yield* instance.snapshot).restartScheduled, true);
142​
143 yield* pool.unregister(id);
144​
145 const stopped = yield* instance.snapshot;
146 assert.equal(stopped.desiredRunning, false);
147 assert.equal(stopped.restartScheduled, false);
148 assert.equal(Option.isNone(yield* pool.get(id)), true);
149 }).pipe(Effect.provide(layerPool(Ref.makeUnsafe("Windows")))),
150 ),
151 );
152});