Open History to see the last commit
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 DesktopState from "../app/DesktopState.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 { DesktopBackendStartConfig } from "./DesktopBackendManager.ts";
23
24function layerPool(labelRef: Ref.Ref<string>): Layer.Layer<DesktopBackendPool.DesktopBackendPool> {
25 return DesktopBackendPool.layer.pipe(
26 Layer.provideMerge(
27 Layer.mergeAll(
28 FileSystem.layerNoop({}),
29 Layer.succeed(
30 ChildProcessSpawner.ChildProcessSpawner,
31 ChildProcessSpawner.make(() => Effect.die("unexpected child process spawn")),
32 ),
33 Layer.succeed(
34 HttpClient.HttpClient,
35 HttpClient.make(() => Effect.die("unexpected HTTP request")),
36 ),
37 Layer.succeed(DesktopObservability.DesktopBackendOutputLogFactory, {
38 forInstance: () =>
39 Effect.succeed({
40 beginSession: () => Effect.void,
41 writeOutputChunk: () => Effect.void,
42 persistFailureSnapshot: () => Effect.void,
43 persistFailure: () => Effect.void,
44 discardSession: Effect.void,
45 } satisfies DesktopObservability.DesktopBackendOutputLogShape),
46 } satisfies DesktopObservability.DesktopBackendOutputLogFactory["Service"]),
47 Layer.succeed(DesktopTelemetryPublisher.DesktopTelemetryPublisher, {
48 latest: Effect.succeedNone,
49 changes: Stream.empty,
50 encoded: Stream.empty,
51 handleControlForSource: () => Effect.void,
52 removeControlSource: () => Effect.void,
53 publishUpdateReport: () => Effect.void,
54 updateRequests: Stream.empty,
55 updateCommits: Stream.empty,
56 updateCancellations: Stream.empty,
57 }),
58 DesktopBrowserHost.layer,
59 Layer.succeed(DesktopBackendConfiguration.DesktopBackendConfiguration, {
60 resolvePrimary: Effect.die("unexpected primary config resolve"),
61 resolvePrimaryLabel: Ref.get(labelRef),
62 resolveWsl: () => Effect.die("unexpected WSL config resolve"),
63 currentBootstrapToken: Effect.die("unexpected bootstrap token read"),
64 } satisfies DesktopBackendConfiguration.DesktopBackendConfiguration["Service"]),
65 DesktopAppSettings.layerTest(),
66 DesktopState.layer,
67 DesktopWslEnvironment.layerTest(),
68 ElectronDialog.layer,
69 ElectronApp.layer,
70 Layer.succeed(DesktopWindow.DesktopWindow, {
71 createMain: Effect.die("unexpected window create"),
72 ensureMain: Effect.die("unexpected window ensure"),
73 revealOrCreateMain: Effect.die("unexpected window reveal"),
74 activate: Effect.die("unexpected window activate"),
75 createMainIfBackendReady: Effect.die("unexpected window create"),
76 showConnectingSplash: Effect.void,
77 handleBackendReady: () => Effect.void,
78 handleBackendNotReady: Effect.void,
79 flushMainWindowBounds: Effect.void,
80 prepareCaptureReveal: Effect.void,
81 dispatchMenuAction: () => Effect.die("unexpected menu action"),
82 dispatchSnapShotEvent: () => Effect.void,
83 zoomMain: () => Effect.die("unexpected zoom"),
84 runMainContentsCommand: () => Effect.die("unexpected main contents command"),
85 syncAppearance: Effect.void,
86 } satisfies DesktopWindow.DesktopWindow["Service"]),
87 ),
88 ),
89 );
90}
91
92describe("DesktopBackendPool", () => {
93 it.effect("unregister stops the registered instance", () =>
94 Effect.scoped(
95 Effect.gen(function* () {
96 const id = DesktopBackendPool.BackendInstanceId("wsl:Ubuntu");
97 const pool = yield* DesktopBackendPool.DesktopBackendPool;
98 const instance = yield* pool.register({
99 id,
100 label: Effect.succeed("WSL (Ubuntu)"),
101 configResolve: Effect.succeed({
102 executablePath: "/electron",
103 args: [],
104 entryPath: "/server/bin.mjs",
105 cwd: "/server",
106 env: {},
107 bootstrap: {
108 mode: "desktop",
109 noBrowser: true,
110 port: 3774,
111 t3Home: "/tmp/t3",
112 host: "127.0.0.1",
113 desktopBootstrapToken: "token",
114 tailscaleServeEnabled: false,
115 tailscaleServePort: 443,
116 },
117 bootstrapDelivery: "fd3",
118 extendEnv: true,
119 httpBaseUrl: new URL("http://127.0.0.1:3774"),
120 captureOutput: false,
121 preflightFailure: Option.some({ reason: "WSL starting", fatal: false }),
122 } satisfies DesktopBackendStartConfig),
123 });
124 yield* instance.start;
125 assert.equal((yield* instance.snapshot).restartScheduled, true);
126
127 yield* pool.unregister(id);
128
129 const stopped = yield* instance.snapshot;
130 assert.equal(stopped.desiredRunning, false);
131 assert.equal(stopped.restartScheduled, false);
132 assert.equal(Option.isNone(yield* pool.get(id)), true);
133 }).pipe(Effect.provide(layerPool(Ref.makeUnsafe("Windows")))),
134 ),
135 );
136});