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

DesktopBackendPool.test.ts

210 lines8.8 KB

1import { assert, describe, it } from "@effect/vitest";
2import * as Duration from "effect/Duration";
3import * as Effect from "effect/Effect";
4import * as FileSystem from "effect/FileSystem";
5import * as Layer from "effect/Layer";
6import * as Option from "effect/Option";
7import * as Ref from "effect/Ref";
8import * as Stream from "effect/Stream";
9import { HttpClient } from "effect/http";
10import { ChildProcessSpawner } from "effect/process";
11​
12import * as DesktopObservability from "../app/DesktopObservability.ts";
13import * as DesktopAppSettings from "../settings/DesktopAppSettings.ts";
14import * as DesktopShellEnvironment from "../shell/DesktopShellEnvironment.ts";
15import * as DesktopTelemetryPublisher from "../telemetry/DesktopTelemetryPublisher.ts";
16import * as DesktopBrowserHost from "../preview/DesktopBrowserHost.ts";
17import * as ElectronDialog from "../electron/ElectronDialog.ts";
18import * as ElectronApp from "../electron/ElectronApp.ts";
19import * as DesktopWindow from "../window/DesktopWindow.ts";
20import * as DesktopWslEnvironment from "../wsl/DesktopWslEnvironment.ts";
21import * as DesktopBackendConfiguration from "./DesktopBackendConfiguration.ts";
22import * as DesktopBackendPool from "./DesktopBackendPool.ts";
23import type { DesktopBackendSnapshot, DesktopBackendStartConfig } from "./DesktopBackendManager.ts";
24​
25function makeStubInstance(
26 id: DesktopBackendPool.BackendInstanceId,
27 label: string,
28): DesktopBackendPool.DesktopBackendInstance {
29 const snapshot: DesktopBackendSnapshot = {
30 desiredRunning: false,
31 ready: false,
32 activePid: Option.none(),
33 restartAttempt: 0,
34 restartScheduled: false,
35 };
36 return {
37 id,
38 label: Effect.succeed(label),
39 start: Effect.void,
40 stop: () => Effect.void,
41 currentConfig: Effect.succeed(Option.none<DesktopBackendStartConfig>()),
42 snapshot: Effect.succeed(snapshot),
43 waitForReady: (_timeout: Duration.Duration) => Effect.succeed(false),
44 };
46​
47function layerPool(labelRef: Ref.Ref<string>): Layer.Layer<DesktopBackendPool.DesktopBackendPool> {
48 return DesktopBackendPool.layer.pipe(
49 Layer.provideMerge(
50 Layer.mergeAll(
51 FileSystem.layerNoop({}),
52 Layer.succeed(
53 ChildProcessSpawner.ChildProcessSpawner,
54 ChildProcessSpawner.make(() => Effect.die("unexpected child process spawn")),
55 ),
56 Layer.succeed(
57 HttpClient.HttpClient,
58 HttpClient.make(() => Effect.die("unexpected HTTP request")),
59 ),
60 Layer.succeed(DesktopObservability.DesktopBackendOutputLogFactory, {
61 forInstance: () =>
62 Effect.succeed({
63 beginSession: () => Effect.void,
64 writeOutputChunk: () => Effect.void,
65 persistFailureSnapshot: () => Effect.void,
66 persistFailure: () => Effect.void,
67 discardSession: Effect.void,
68 } satisfies DesktopObservability.DesktopBackendOutputLogShape),
69 } satisfies DesktopObservability.DesktopBackendOutputLogFactory["Service"]),
70 Layer.succeed(DesktopTelemetryPublisher.DesktopTelemetryPublisher, {
71 latest: Effect.succeedNone,
72 changes: Stream.empty,
73 encoded: Stream.empty,
74 handleControlForSource: () => Effect.void,
75 removeControlSource: () => Effect.void,
76 publishUpdateReport: () => Effect.void,
77 updateRequests: Stream.empty,
78 updateCommits: Stream.empty,
79 updateCancellations: Stream.empty,
80 }),
81 DesktopBrowserHost.layer,
82 Layer.succeed(DesktopBackendConfiguration.DesktopBackendConfiguration, {
83 resolvePrimary: Effect.die("unexpected primary config resolve"),
84 resolvePrimaryLabel: Ref.get(labelRef),
85 resolveWsl: () => Effect.die("unexpected WSL config resolve"),
86 currentBootstrapToken: Effect.die("unexpected bootstrap token read"),
87 } satisfies DesktopBackendConfiguration.DesktopBackendConfiguration["Service"]),
88 DesktopAppSettings.layerTest(),
89 Layer.succeed(DesktopShellEnvironment.DesktopShellEnvironment, {
90 installIntoProcess: Effect.succeed(true),
91 secretStorageReady: Effect.void,
92 }),
93 DesktopWslEnvironment.layerTest(),
94 ElectronDialog.layer,
95 ElectronApp.layer,
96 Layer.succeed(DesktopWindow.DesktopWindow, {
97 createMain: Effect.die("unexpected window create"),
98 ensureMain: Effect.die("unexpected window ensure"),
99 revealOrCreateMain: Effect.die("unexpected window reveal"),
100 activate: Effect.die("unexpected window activate"),
101 createMainIfBackendReady: Effect.die("unexpected window create"),
102 showConnectingSplash: Effect.void,
103 handleBackendReady: () => Effect.void,
104 handleBackendNotReady: Effect.void,
105 flushMainWindowBounds: Effect.void,
106 prepareCaptureReveal: Effect.void,
107 dispatchMenuAction: () => Effect.die("unexpected menu action"),
108 dispatchSnapShotEvent: () => Effect.void,
109 zoomMain: () => Effect.die("unexpected zoom"),
110 runMainContentsCommand: () => Effect.die("unexpected main contents command"),
111 syncAppearance: Effect.void,
112 } satisfies DesktopWindow.DesktopWindow["Service"]),
113 ),
114 ),
115 );
117​
118describe("DesktopBackendPool", () => {
119 it.effect("layerTest exposes registered instances by id", () =>
120 Effect.gen(function* () {
121 const pool = yield* DesktopBackendPool.DesktopBackendPool;
122 const fetchedPrimary = yield* pool.get(DesktopBackendPool.PRIMARY_INSTANCE_ID);
123 const fetchedWsl = yield* pool.get(DesktopBackendPool.BackendInstanceId("wsl:ubuntu"));
124 const fetchedMissing = yield* pool.get(DesktopBackendPool.BackendInstanceId("missing"));
125 const all = yield* pool.list;
126 const resolvedPrimary = yield* pool.primary;
127​
128 assert.equal(yield* Option.getOrThrow(fetchedPrimary).label, "Windows");
129 assert.equal(yield* Option.getOrThrow(fetchedWsl).label, "WSL (Ubuntu)");
130 assert.isTrue(Option.isNone(fetchedMissing));
131 assert.lengthOf(all, 2);
132 assert.equal(resolvedPrimary.id, DesktopBackendPool.PRIMARY_INSTANCE_ID);
133 }).pipe(
134 Effect.provide(
135 DesktopBackendPool.layerTest([
136 makeStubInstance(DesktopBackendPool.PRIMARY_INSTANCE_ID, "Windows"),
137 makeStubInstance(DesktopBackendPool.BackendInstanceId("wsl:ubuntu"), "WSL (Ubuntu)"),
138 ]),
139 ),
140 ),
141 );
142​
143 it.effect("layerTest dies when no instances are supplied", () =>
144 Effect.exit(
145 DesktopBackendPool.DesktopBackendPool.pipe(Effect.provide(DesktopBackendPool.layerTest([]))),
146 ).pipe(Effect.map((exit) => assert.equal(exit._tag, "Failure"))),
147 );
148​
149 it.effect("resolves the primary label lazily after pool layer construction", () =>
150 Effect.scoped(
151 Effect.gen(function* () {
152 const labelRef = yield* Ref.make("Windows");
153 const pool = yield* DesktopBackendPool.DesktopBackendPool.pipe(
154 Effect.provide(layerPool(labelRef)),
155 );
156 const primary = yield* pool.primary;
157​
158 yield* Ref.set(labelRef, "WSL (Ubuntu)");
159​
160 assert.equal(yield* primary.label, "WSL (Ubuntu)");
161 }),
162 ),
163 );
164​
165 it.effect("unregister stops the registered instance", () =>
166 Effect.scoped(
167 Effect.gen(function* () {
168 const id = DesktopBackendPool.BackendInstanceId("wsl:Ubuntu");
169 const pool = yield* DesktopBackendPool.DesktopBackendPool;
170 const instance = yield* pool.register({
171 id,
172 label: Effect.succeed("WSL (Ubuntu)"),
173 // A transient preflight failure leaves a restart fiber scheduled
174 // without needing a real child process.
175 configResolve: Effect.succeed({
176 executablePath: "/electron",
177 args: [],
178 entryPath: "/server/bin.mjs",
179 cwd: "/server",
180 env: {},
181 bootstrap: {
182 mode: "desktop",
183 noBrowser: true,
184 port: 3774,
185 t3Home: "/tmp/t3",
186 host: "127.0.0.1",
187 desktopBootstrapToken: "token",
188 tailscaleServeEnabled: false,
189 tailscaleServePort: 443,
190 },
191 bootstrapDelivery: "fd3",
192 extendEnv: true,
193 httpBaseUrl: new URL("http://127.0.0.1:3774"),
194 captureOutput: false,
195 preflightFailure: Option.some({ reason: "WSL starting", fatal: false }),
196 } satisfies DesktopBackendStartConfig),
197 });
198 yield* instance.start;
199 assert.equal((yield* instance.snapshot).restartScheduled, true);
200​
201 yield* pool.unregister(id);
202​
203 const stopped = yield* instance.snapshot;
204 assert.equal(stopped.desiredRunning, false);
205 assert.equal(stopped.restartScheduled, false);
206 assert.equal(Option.isNone(yield* pool.get(id)), true);
207 }).pipe(Effect.provide(layerPool(Ref.makeUnsafe("Windows")))),
208 ),
209 );
210});