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

DesktopBackendPool.test.ts

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