Open History to see the last commit
1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { assert, describe, it } from "@effect/vitest";
3import * as Effect from "effect/Effect";
4import * as FileSystem from "effect/FileSystem";
5import * as Layer from "effect/Layer";
6import * as Schema from "effect/Schema";
7
8import * as DesktopEnvironment from "../app/DesktopEnvironment.ts";
9import * as DesktopBackendConfiguration from "./DesktopBackendConfiguration.ts";
10import * as DesktopConfig from "../app/DesktopConfig.ts";
11import * as DesktopServerExposure from "./DesktopServerExposure.ts";
12
13const PersistedServerObservabilitySettingsDocument = Schema.Struct({
14 observability: Schema.Struct({
15 otlpTracesUrl: Schema.String,
16 otlpMetricsUrl: Schema.String,
17 }),
18});
19
20const encodePersistedServerObservabilitySettingsDocument = Schema.encodeEffect(
21 Schema.fromJsonString(PersistedServerObservabilitySettingsDocument),
22);
23
24const serverExposureLayer = Layer.succeed(DesktopServerExposure.DesktopServerExposure, {
25 getState: Effect.die("unexpected getState"),
26 backendConfig: Effect.succeed({
27 port: 4888,
28 bindHost: "0.0.0.0",
29 httpBaseUrl: new URL("http://127.0.0.1:4888"),
30 tailscaleServeEnabled: true,
31 tailscaleServePort: 8443,
32 }),
33 configureFromSettings: () => Effect.die("unexpected configureFromSettings"),
34 setMode: () => Effect.die("unexpected setMode"),
35 setTailscaleServeEnabled: () => Effect.die("unexpected setTailscaleServeEnabled"),
36 getAdvertisedEndpoints: Effect.succeed([]),
37} satisfies DesktopServerExposure.DesktopServerExposureShape);
38
39function makeEnvironmentLayer(
40 baseDir: string,
41 options?: {
42 readonly isPackaged?: boolean;
43 readonly devServerUrl?: string;
44 },
45) {
46 return DesktopEnvironment.layer({
47 dirname: "/repo/apps/desktop/src",
48 homeDirectory: baseDir,
49 platform: "darwin",
50 processArch: "x64",
51 appVersion: "1.2.3",
52 appPath: "/repo",
53 isPackaged: options?.isPackaged ?? true,
54 resourcesPath: "/missing/resources",
55 runningUnderArm64Translation: false,
56 }).pipe(
57 Layer.provide(
58 Layer.mergeAll(
59 NodeServices.layer,
60 DesktopConfig.layerTest({
61 T3CODE_HOME: baseDir,
62 T3CODE_PORT: "9999",
63 T3CODE_MODE: "desktop",
64 T3CODE_DESKTOP_LAN_HOST: "192.168.1.50",
65 VITE_DEV_SERVER_URL: options?.devServerUrl,
66 }),
67 ),
68 ),
69 );
70}
71
72const withHarness = <A, E, R>(
73 effect: Effect.Effect<
74 A,
75 E,
76 | R
77 | DesktopEnvironment.DesktopEnvironment
78 | FileSystem.FileSystem
79 | DesktopBackendConfiguration.DesktopBackendConfiguration
80 >,
81) =>
82 Effect.gen(function* () {
83 const fileSystem = yield* FileSystem.FileSystem;
84 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
85 prefix: "t3-desktop-backend-config-test-",
86 });
87
88 return yield* effect.pipe(
89 Effect.provide(
90 DesktopBackendConfiguration.layer.pipe(
91 Layer.provideMerge(serverExposureLayer),
92 Layer.provideMerge(makeEnvironmentLayer(baseDir)),
93 ),
94 ),
95 );
96 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer));
97
98describe("DesktopBackendConfiguration", () => {
99 it.effect("resolves backend start config with a stable scoped bootstrap token", () =>
100 withHarness(
101 Effect.gen(function* () {
102 const environment = yield* DesktopEnvironment.DesktopEnvironment;
103 const configuration = yield* DesktopBackendConfiguration.DesktopBackendConfiguration;
104
105 const first = yield* configuration.resolve;
106 const second = yield* configuration.resolve;
107
108 assert.equal(first.executablePath, process.execPath);
109 assert.equal(first.entryPath, environment.backendEntryPath);
110 assert.equal(first.cwd, environment.backendCwd);
111 assert.equal(first.captureOutput, true);
112 assert.equal(first.env.ELECTRON_RUN_AS_NODE, "1");
113 assert.isUndefined(first.env.T3CODE_PORT);
114 assert.isUndefined(first.env.T3CODE_MODE);
115 assert.isUndefined(first.env.T3CODE_DESKTOP_LAN_HOST);
116
117 assert.equal(first.bootstrap.mode, "desktop");
118 assert.equal(first.bootstrap.noBrowser, true);
119 assert.equal(first.bootstrap.port, 4888);
120 assert.equal(first.bootstrap.host, "0.0.0.0");
121 assert.equal(first.bootstrap.t3Home, environment.baseDir);
122 assert.equal(first.bootstrap.tailscaleServeEnabled, true);
123 assert.equal(first.bootstrap.tailscaleServePort, 8443);
124 assert.match(first.bootstrap.desktopBootstrapToken, /^[0-9a-f]{48}$/i);
125 assert.equal(second.bootstrap.desktopBootstrapToken, first.bootstrap.desktopBootstrapToken);
126 }),
127 ),
128 );
129
130 it.effect("includes persisted backend observability endpoints when present", () =>
131 withHarness(
132 Effect.gen(function* () {
133 const fileSystem = yield* FileSystem.FileSystem;
134 const environment = yield* DesktopEnvironment.DesktopEnvironment;
135 const configuration = yield* DesktopBackendConfiguration.DesktopBackendConfiguration;
136
137 yield* fileSystem.makeDirectory(environment.path.dirname(environment.serverSettingsPath), {
138 recursive: true,
139 });
140 yield* fileSystem.writeFileString(
141 environment.serverSettingsPath,
142 yield* encodePersistedServerObservabilitySettingsDocument({
143 observability: {
144 otlpTracesUrl: " http://127.0.0.1:4318/v1/traces ",
145 otlpMetricsUrl: " http://127.0.0.1:4318/v1/metrics ",
146 },
147 }),
148 );
149
150 const config = yield* configuration.resolve;
151 assert.equal(config.bootstrap.otlpTracesUrl, "http://127.0.0.1:4318/v1/traces");
152 assert.equal(config.bootstrap.otlpMetricsUrl, "http://127.0.0.1:4318/v1/metrics");
153 }),
154 ),
155 );
156
157 it.effect("omits backend observability endpoints when settings are missing", () =>
158 withHarness(
159 Effect.gen(function* () {
160 const configuration = yield* DesktopBackendConfiguration.DesktopBackendConfiguration;
161 const config = yield* configuration.resolve;
162
163 assert.isUndefined(config.bootstrap.otlpTracesUrl);
164 assert.isUndefined(config.bootstrap.otlpMetricsUrl);
165 }),
166 ),
167 );
168
169 it.effect("captures backend output in development so child process logs can be persisted", () =>
170 Effect.gen(function* () {
171 const fileSystem = yield* FileSystem.FileSystem;
172 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
173 prefix: "t3-desktop-backend-config-test-",
174 });
175
176 yield* Effect.gen(function* () {
177 const configuration = yield* DesktopBackendConfiguration.DesktopBackendConfiguration;
178 const config = yield* configuration.resolve;
179 assert.equal(config.captureOutput, true);
180 }).pipe(
181 Effect.provide(
182 DesktopBackendConfiguration.layer.pipe(
183 Layer.provideMerge(serverExposureLayer),
184 Layer.provideMerge(
185 makeEnvironmentLayer(baseDir, {
186 isPackaged: false,
187 devServerUrl: "http://127.0.0.1:5733",
188 }),
189 ),
190 ),
191 ),
192 );
193 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
194 );
195});