Open History to see the last commit
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/unstable/http";
10import { ChildProcessSpawner } from "effect/unstable/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 ElectronDialog from "../electron/ElectronDialog.ts";
16import * as DesktopWindow from "../window/DesktopWindow.ts";
17import * as DesktopWslEnvironment from "../wsl/DesktopWslEnvironment.ts";
18import * as DesktopBackendConfiguration from "./DesktopBackendConfiguration.ts";
19import * as DesktopBackendPool from "./DesktopBackendPool.ts";
20import type { DesktopBackendSnapshot, DesktopBackendStartConfig } from "./DesktopBackendManager.ts";
21
22function makeStubInstance(
23 id: DesktopBackendPool.BackendInstanceId,
24 label: string,
25): DesktopBackendPool.DesktopBackendInstance {
26 const snapshot: DesktopBackendSnapshot = {
27 desiredRunning: false,
28 ready: false,
29 activePid: Option.none(),
30 restartAttempt: 0,
31 restartScheduled: false,
32 };
33 return {
34 id,
35 label: Effect.succeed(label),
36 start: Effect.void,
37 stop: () => Effect.void,
38 currentConfig: Effect.succeed(Option.none<DesktopBackendStartConfig>()),
39 snapshot: Effect.succeed(snapshot),
40 waitForReady: (_timeout: Duration.Duration) => Effect.succeed(false),
41 };
42}
43
44function makePoolLayer(
45 labelRef: Ref.Ref<string>,
46): 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.succeed(Option.none()),
71 changes: Stream.empty,
72 encoded: Stream.empty,
73 handleControl: () => Effect.void,
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 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 } satisfies DesktopBackendConfiguration.DesktopBackendConfiguration["Service"]),
86 DesktopAppSettings.layerTest(),
87 DesktopWslEnvironment.layerTest(),
88 ElectronDialog.layer,
89 Layer.succeed(DesktopWindow.DesktopWindow, {
90 createMain: Effect.die("unexpected window create"),
91 ensureMain: Effect.die("unexpected window ensure"),
92 revealOrCreateMain: Effect.die("unexpected window reveal"),
93 activate: Effect.die("unexpected window activate"),
94 createMainIfBackendReady: Effect.die("unexpected window create"),
95 showConnectingSplash: Effect.void,
96 handleBackendReady: () => Effect.void,
97 handleBackendNotReady: Effect.void,
98 flushMainWindowBounds: Effect.void,
99 prepareCaptureReveal: Effect.void,
100 dispatchMenuAction: () => Effect.die("unexpected menu action"),
101 dispatchSnapShotEvent: () => Effect.void,
102 zoomMain: () => Effect.die("unexpected zoom"),
103 syncAppearance: Effect.void,
104 } satisfies DesktopWindow.DesktopWindow["Service"]),
105 ),
106 ),
107 );
108}
109
110describe("DesktopBackendPool", () => {
111 it.effect("layerTest exposes registered instances by id", () =>
112 Effect.gen(function* () {
113 const pool = yield* DesktopBackendPool.DesktopBackendPool;
114 const fetchedPrimary = yield* pool.get(DesktopBackendPool.PRIMARY_INSTANCE_ID);
115 const fetchedWsl = yield* pool.get(DesktopBackendPool.BackendInstanceId("wsl:ubuntu"));
116 const fetchedMissing = yield* pool.get(DesktopBackendPool.BackendInstanceId("missing"));
117 const all = yield* pool.list;
118 const resolvedPrimary = yield* pool.primary;
119
120 assert.equal(yield* Option.getOrThrow(fetchedPrimary).label, "Windows");
121 assert.equal(yield* Option.getOrThrow(fetchedWsl).label, "WSL (Ubuntu)");
122 assert.isTrue(Option.isNone(fetchedMissing));
123 assert.lengthOf(all, 2);
124 // First instance becomes primary in layerTest so single-instance
125 // stubs don't have to wire an explicit primary.
126 assert.equal(resolvedPrimary.id, DesktopBackendPool.PRIMARY_INSTANCE_ID);
127 }).pipe(
128 Effect.provide(
129 DesktopBackendPool.layerTest([
130 makeStubInstance(DesktopBackendPool.PRIMARY_INSTANCE_ID, "Windows"),
131 makeStubInstance(DesktopBackendPool.BackendInstanceId("wsl:ubuntu"), "WSL (Ubuntu)"),
132 ]),
133 ),
134 ),
135 );
136
137 it.effect("layerTest dies when no instances are supplied", () =>
138 Effect.exit(
139 Effect.gen(function* () {
140 yield* DesktopBackendPool.DesktopBackendPool;
141 }).pipe(Effect.provide(DesktopBackendPool.layerTest([]))),
142 ).pipe(Effect.map((exit) => assert.equal(exit._tag, "Failure"))),
143 );
144
145 it.effect("resolves the primary label lazily after pool layer construction", () =>
146 Effect.scoped(
147 Effect.gen(function* () {
148 const labelRef = yield* Ref.make("Windows");
149 const pool = yield* DesktopBackendPool.DesktopBackendPool.pipe(
150 Effect.provide(makePoolLayer(labelRef)),
151 );
152 const primary = yield* pool.primary;
153
154 yield* Ref.set(labelRef, "WSL (Ubuntu)");
155
156 assert.equal(yield* primary.label, "WSL (Ubuntu)");
157 }),
158 ),
159 );
160});