Open History to see the last commit
1import os from "node:os";
2
3import { assert, expect, it } from "@effect/vitest";
4import { ConfigProvider, Effect, FileSystem, Layer, Option, Path, Schema } from "effect";
5
6import {
7 DesktopBackendBootstrap,
8 type DesktopBackendBootstrap as DesktopBackendBootstrapValue,
9} from "@t3tools/contracts";
10import { NetService } from "@t3tools/shared/Net";
11import * as NodeServices from "@effect/platform-node/NodeServices";
12import { deriveServerPaths } from "../config.ts";
13import { resolveServerConfig } from "./config.ts";
14
15const encodeDesktopBootstrap = Schema.encodeEffect(Schema.fromJsonString(DesktopBackendBootstrap));
16
17const makeDesktopBootstrap = (
18 overrides: Partial<DesktopBackendBootstrapValue> = {},
19): DesktopBackendBootstrapValue => ({
20 mode: "desktop",
21 noBrowser: true,
22 port: 4888,
23 t3Home: "/tmp/t3-bootstrap-home",
24 host: "127.0.0.1",
25 desktopBootstrapToken: "desktop-bootstrap-token",
26 tailscaleServeEnabled: false,
27 tailscaleServePort: 443,
28 ...overrides,
29});
30
31it.layer(NodeServices.layer)("cli config resolution", (it) => {
32 const defaultObservabilityConfig = {
33 traceMinLevel: "Info",
34 traceTimingEnabled: true,
35 traceBatchWindowMs: 200,
36 traceMaxBytes: 10 * 1024 * 1024,
37 traceMaxFiles: 10,
38 otlpTracesUrl: undefined,
39 otlpMetricsUrl: undefined,
40 otlpExportIntervalMs: 10_000,
41 otlpServiceName: "t3-server",
42 } as const;
43
44 const openBootstrapFd = Effect.fn(function* (payload: DesktopBackendBootstrapValue) {
45 const fs = yield* FileSystem.FileSystem;
46 const filePath = yield* fs.makeTempFileScoped({ prefix: "t3-bootstrap-", suffix: ".ndjson" });
47 const encoded = yield* encodeDesktopBootstrap(payload);
48 yield* fs.writeFileString(filePath, `${encoded}\n`);
49 const { fd } = yield* fs.open(filePath, { flag: "r" });
50 return fd;
51 });
52
53 it.effect("falls back to effect/config values when flags are omitted", () =>
54 Effect.gen(function* () {
55 const { join } = yield* Path.Path;
56 const baseDir = join(os.tmpdir(), "t3-cli-config-env-base");
57 const derivedPaths = yield* deriveServerPaths(baseDir, new URL("http://127.0.0.1:5173"));
58 const resolved = yield* resolveServerConfig(
59 {
60 mode: Option.none(),
61 port: Option.none(),
62 host: Option.none(),
63 baseDir: Option.none(),
64 cwd: Option.none(),
65 devUrl: Option.none(),
66 noBrowser: Option.none(),
67 bootstrapFd: Option.none(),
68 autoBootstrapProjectFromCwd: Option.none(),
69 logWebSocketEvents: Option.none(),
70 tailscaleServeEnabled: Option.none(),
71 tailscaleServePort: Option.none(),
72 },
73 Option.none(),
74 ).pipe(
75 Effect.provide(
76 Layer.mergeAll(
77 ConfigProvider.layer(
78 ConfigProvider.fromEnv({
79 env: {
80 T3CODE_LOG_LEVEL: "Warn",
81 T3CODE_MODE: "desktop",
82 T3CODE_PORT: "4001",
83 T3CODE_HOST: "0.0.0.0",
84 T3CODE_HOME: baseDir,
85 VITE_DEV_SERVER_URL: "http://127.0.0.1:5173",
86 T3CODE_NO_BROWSER: "true",
87 T3CODE_AUTO_BOOTSTRAP_PROJECT_FROM_CWD: "false",
88 T3CODE_LOG_WS_EVENTS: "true",
89 },
90 }),
91 ),
92 NetService.layer,
93 ),
94 ),
95 );
96
97 expect(resolved).toEqual({
98 logLevel: "Warn",
99 ...defaultObservabilityConfig,
100 mode: "desktop",
101 port: 4001,
102 cwd: process.cwd(),
103 baseDir,
104 ...derivedPaths,
105 host: "0.0.0.0",
106 staticDir: undefined,
107 devUrl: new URL("http://127.0.0.1:5173"),
108 noBrowser: true,
109 startupPresentation: "browser",
110 desktopBootstrapToken: undefined,
111 autoBootstrapProjectFromCwd: false,
112 logWebSocketEvents: true,
113 tailscaleServeEnabled: false,
114 tailscaleServePort: 443,
115 });
116 }),
117 );
118
119 it.effect("uses CLI flags when provided", () =>
120 Effect.gen(function* () {
121 const { join } = yield* Path.Path;
122 const baseDir = join(os.tmpdir(), "t3-cli-config-flags-base");
123 const derivedPaths = yield* deriveServerPaths(baseDir, new URL("http://127.0.0.1:4173"));
124 const resolved = yield* resolveServerConfig(
125 {
126 mode: Option.some("web"),
127 port: Option.some(8788),
128 host: Option.some("127.0.0.1"),
129 baseDir: Option.some(baseDir),
130 cwd: Option.none(),
131 devUrl: Option.some(new URL("http://127.0.0.1:4173")),
132 noBrowser: Option.some(true),
133 bootstrapFd: Option.none(),
134 autoBootstrapProjectFromCwd: Option.some(true),
135 logWebSocketEvents: Option.some(true),
136 tailscaleServeEnabled: Option.some(true),
137 tailscaleServePort: Option.some(8443),
138 },
139 Option.some("Debug"),
140 ).pipe(
141 Effect.provide(
142 Layer.mergeAll(
143 ConfigProvider.layer(
144 ConfigProvider.fromEnv({
145 env: {
146 T3CODE_LOG_LEVEL: "Warn",
147 T3CODE_MODE: "desktop",
148 T3CODE_PORT: "4001",
149 T3CODE_HOST: "0.0.0.0",
150 T3CODE_HOME: join(os.tmpdir(), "ignored-base"),
151 VITE_DEV_SERVER_URL: "http://127.0.0.1:5173",
152 T3CODE_NO_BROWSER: "false",
153 T3CODE_AUTO_BOOTSTRAP_PROJECT_FROM_CWD: "false",
154 T3CODE_LOG_WS_EVENTS: "false",
155 },
156 }),
157 ),
158 NetService.layer,
159 ),
160 ),
161 );
162
163 expect(resolved).toEqual({
164 logLevel: "Debug",
165 ...defaultObservabilityConfig,
166 mode: "web",
167 port: 8788,
168 cwd: process.cwd(),
169 baseDir,
170 ...derivedPaths,
171 host: "127.0.0.1",
172 staticDir: undefined,
173 devUrl: new URL("http://127.0.0.1:4173"),
174 noBrowser: true,
175 startupPresentation: "browser",
176 desktopBootstrapToken: undefined,
177 autoBootstrapProjectFromCwd: true,
178 logWebSocketEvents: true,
179 tailscaleServeEnabled: true,
180 tailscaleServePort: 8443,
181 });
182 }),
183 );
184
185 it.effect("preserves explicit false CLI boolean flags over env and bootstrap values", () =>
186 Effect.gen(function* () {
187 const { join } = yield* Path.Path;
188 const baseDir = join(os.tmpdir(), "t3-cli-config-false-flags");
189 const fd = yield* openBootstrapFd(
190 makeDesktopBootstrap({
191 noBrowser: true,
192 tailscaleServeEnabled: false,
193 tailscaleServePort: 443,
194 }),
195 );
196 const derivedPaths = yield* deriveServerPaths(baseDir, new URL("http://127.0.0.1:4173"));
197
198 const resolved = yield* resolveServerConfig(
199 {
200 mode: Option.some("web"),
201 port: Option.some(8788),
202 host: Option.some("127.0.0.1"),
203 baseDir: Option.some(baseDir),
204 cwd: Option.none(),
205 devUrl: Option.some(new URL("http://127.0.0.1:4173")),
206 noBrowser: Option.some(false),
207 bootstrapFd: Option.none(),
208 autoBootstrapProjectFromCwd: Option.some(false),
209 logWebSocketEvents: Option.some(false),
210 tailscaleServeEnabled: Option.none(),
211 tailscaleServePort: Option.none(),
212 },
213 Option.none(),
214 ).pipe(
215 Effect.provide(
216 Layer.mergeAll(
217 ConfigProvider.layer(
218 ConfigProvider.fromEnv({
219 env: {
220 T3CODE_BOOTSTRAP_FD: String(fd),
221 T3CODE_NO_BROWSER: "true",
222 T3CODE_AUTO_BOOTSTRAP_PROJECT_FROM_CWD: "true",
223 T3CODE_LOG_WS_EVENTS: "true",
224 },
225 }),
226 ),
227 NetService.layer,
228 ),
229 ),
230 );
231
232 expect(resolved).toEqual({
233 logLevel: "Info",
234 ...defaultObservabilityConfig,
235 mode: "web",
236 port: 8788,
237 cwd: process.cwd(),
238 baseDir,
239 ...derivedPaths,
240 host: "127.0.0.1",
241 staticDir: undefined,
242 devUrl: new URL("http://127.0.0.1:4173"),
243 noBrowser: false,
244 startupPresentation: "browser",
245 desktopBootstrapToken: "desktop-bootstrap-token",
246 autoBootstrapProjectFromCwd: false,
247 logWebSocketEvents: false,
248 tailscaleServeEnabled: false,
249 tailscaleServePort: 443,
250 });
251 }),
252 );
253
254 it.effect("uses bootstrap envelope values as fallbacks when flags and env are absent", () =>
255 Effect.gen(function* () {
256 const { join } = yield* Path.Path;
257 const baseDir = "/tmp/t3-bootstrap-home";
258 const fd = yield* openBootstrapFd(
259 makeDesktopBootstrap({
260 port: 4888,
261 host: "127.0.0.2",
262 t3Home: baseDir,
263 noBrowser: true,
264 desktopBootstrapToken: "desktop-token",
265 tailscaleServeEnabled: false,
266 tailscaleServePort: 443,
267 otlpTracesUrl: "http://localhost:4318/v1/traces",
268 otlpMetricsUrl: "http://localhost:4318/v1/metrics",
269 }),
270 );
271 const derivedPaths = yield* deriveServerPaths(baseDir, undefined);
272
273 const resolved = yield* resolveServerConfig(
274 {
275 mode: Option.none(),
276 port: Option.none(),
277 host: Option.none(),
278 baseDir: Option.none(),
279 cwd: Option.none(),
280 devUrl: Option.none(),
281 noBrowser: Option.none(),
282 bootstrapFd: Option.none(),
283 autoBootstrapProjectFromCwd: Option.none(),
284 logWebSocketEvents: Option.none(),
285 tailscaleServeEnabled: Option.none(),
286 tailscaleServePort: Option.none(),
287 },
288 Option.none(),
289 ).pipe(
290 Effect.provide(
291 Layer.mergeAll(
292 ConfigProvider.layer(
293 ConfigProvider.fromEnv({
294 env: {
295 T3CODE_BOOTSTRAP_FD: String(fd),
296 },
297 }),
298 ),
299 NetService.layer,
300 ),
301 ),
302 );
303
304 expect(resolved).toEqual({
305 logLevel: "Info",
306 ...defaultObservabilityConfig,
307 otlpTracesUrl: "http://localhost:4318/v1/traces",
308 otlpMetricsUrl: "http://localhost:4318/v1/metrics",
309 mode: "desktop",
310 port: 4888,
311 cwd: process.cwd(),
312 baseDir,
313 ...derivedPaths,
314 host: "127.0.0.2",
315 staticDir: resolved.staticDir,
316 devUrl: undefined,
317 noBrowser: true,
318 startupPresentation: "browser",
319 desktopBootstrapToken: "desktop-token",
320 autoBootstrapProjectFromCwd: false,
321 logWebSocketEvents: false,
322 tailscaleServeEnabled: false,
323 tailscaleServePort: 443,
324 });
325 assert.equal(join(baseDir, "userdata"), resolved.stateDir);
326 }),
327 );
328
329 it.effect("creates derived runtime directories during config resolution", () =>
330 Effect.gen(function* () {
331 const fs = yield* FileSystem.FileSystem;
332 const path = yield* Path.Path;
333 const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-cli-config-dirs-" });
334 const customCwd = path.join(baseDir, "nested", "project");
335
336 const resolved = yield* resolveServerConfig(
337 {
338 mode: Option.some("desktop"),
339 port: Option.some(4888),
340 host: Option.none(),
341 baseDir: Option.some(baseDir),
342 cwd: Option.some(customCwd),
343 devUrl: Option.some(new URL("http://127.0.0.1:5173")),
344 noBrowser: Option.none(),
345 bootstrapFd: Option.none(),
346 autoBootstrapProjectFromCwd: Option.none(),
347 logWebSocketEvents: Option.none(),
348 tailscaleServeEnabled: Option.none(),
349 tailscaleServePort: Option.none(),
350 },
351 Option.none(),
352 ).pipe(
353 Effect.provide(
354 Layer.mergeAll(
355 ConfigProvider.layer(ConfigProvider.fromEnv({ env: {} })),
356 NetService.layer,
357 ),
358 ),
359 );
360
361 for (const directory of [
362 customCwd,
363 resolved.stateDir,
364 resolved.logsDir,
365 resolved.providerLogsDir,
366 resolved.terminalLogsDir,
367 resolved.attachmentsDir,
368 resolved.worktreesDir,
369 path.dirname(resolved.serverLogPath),
370 path.dirname(resolved.serverTracePath),
371 ]) {
372 expect(yield* fs.exists(directory)).toBe(true);
373 }
374 expect(resolved.cwd).toBe(path.resolve(customCwd));
375 }),
376 );
377
378 it.effect("applies flag then env precedence over bootstrap envelope values", () =>
379 Effect.gen(function* () {
380 const { join } = yield* Path.Path;
381 const baseDir = join(os.tmpdir(), "t3-cli-config-env-wins");
382 const fd = yield* openBootstrapFd(
383 makeDesktopBootstrap({
384 port: 4888,
385 host: "127.0.0.2",
386 t3Home: "/tmp/t3-bootstrap-home",
387 noBrowser: false,
388 desktopBootstrapToken: "desktop-token",
389 tailscaleServeEnabled: false,
390 tailscaleServePort: 443,
391 }),
392 );
393 const derivedPaths = yield* deriveServerPaths(baseDir, new URL("http://127.0.0.1:4173"));
394
395 const resolved = yield* resolveServerConfig(
396 {
397 mode: Option.none(),
398 port: Option.some(8788),
399 host: Option.some("127.0.0.1"),
400 baseDir: Option.none(),
401 cwd: Option.none(),
402 devUrl: Option.some(new URL("http://127.0.0.1:4173")),
403 noBrowser: Option.none(),
404 bootstrapFd: Option.none(),
405 autoBootstrapProjectFromCwd: Option.none(),
406 logWebSocketEvents: Option.none(),
407 tailscaleServeEnabled: Option.none(),
408 tailscaleServePort: Option.none(),
409 },
410 Option.some("Debug"),
411 ).pipe(
412 Effect.provide(
413 Layer.mergeAll(
414 ConfigProvider.layer(
415 ConfigProvider.fromEnv({
416 env: {
417 T3CODE_MODE: "web",
418 T3CODE_BOOTSTRAP_FD: String(fd),
419 T3CODE_HOME: baseDir,
420 T3CODE_NO_BROWSER: "true",
421 T3CODE_AUTO_BOOTSTRAP_PROJECT_FROM_CWD: "true",
422 T3CODE_LOG_WS_EVENTS: "true",
423 },
424 }),
425 ),
426 NetService.layer,
427 ),
428 ),
429 );
430
431 expect(resolved).toEqual({
432 logLevel: "Debug",
433 ...defaultObservabilityConfig,
434 mode: "web",
435 port: 8788,
436 cwd: process.cwd(),
437 baseDir,
438 ...derivedPaths,
439 host: "127.0.0.1",
440 staticDir: undefined,
441 devUrl: new URL("http://127.0.0.1:4173"),
442 noBrowser: true,
443 startupPresentation: "browser",
444 desktopBootstrapToken: "desktop-token",
445 autoBootstrapProjectFromCwd: true,
446 logWebSocketEvents: true,
447 tailscaleServeEnabled: false,
448 tailscaleServePort: 443,
449 });
450 }),
451 );
452
453 it.effect("falls back to persisted observability settings when env vars are absent", () =>
454 Effect.gen(function* () {
455 const fs = yield* FileSystem.FileSystem;
456 const path = yield* Path.Path;
457 const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-cli-config-settings-" });
458 const derivedPaths = yield* deriveServerPaths(baseDir, undefined);
459 yield* fs.makeDirectory(path.dirname(derivedPaths.settingsPath), { recursive: true });
460 yield* fs.writeFileString(
461 derivedPaths.settingsPath,
462 `${JSON.stringify({
463 observability: {
464 otlpTracesUrl: "http://localhost:4318/v1/traces",
465 otlpMetricsUrl: "http://localhost:4318/v1/metrics",
466 },
467 })}\n`,
468 );
469
470 const resolved = yield* resolveServerConfig(
471 {
472 mode: Option.some("desktop"),
473 port: Option.some(4888),
474 host: Option.none(),
475 baseDir: Option.some(baseDir),
476 cwd: Option.none(),
477 devUrl: Option.none(),
478 noBrowser: Option.none(),
479 bootstrapFd: Option.none(),
480 autoBootstrapProjectFromCwd: Option.none(),
481 logWebSocketEvents: Option.none(),
482 tailscaleServeEnabled: Option.none(),
483 tailscaleServePort: Option.none(),
484 },
485 Option.none(),
486 ).pipe(
487 Effect.provide(
488 Layer.mergeAll(
489 ConfigProvider.layer(ConfigProvider.fromEnv({ env: {} })),
490 NetService.layer,
491 ),
492 ),
493 );
494
495 expect(resolved.otlpTracesUrl).toBe("http://localhost:4318/v1/traces");
496 expect(resolved.otlpMetricsUrl).toBe("http://localhost:4318/v1/metrics");
497 expect(resolved).toEqual({
498 logLevel: "Info",
499 ...defaultObservabilityConfig,
500 otlpTracesUrl: "http://localhost:4318/v1/traces",
501 otlpMetricsUrl: "http://localhost:4318/v1/metrics",
502 mode: "desktop",
503 port: 4888,
504 cwd: process.cwd(),
505 baseDir,
506 ...derivedPaths,
507 host: "127.0.0.1",
508 staticDir: resolved.staticDir,
509 devUrl: undefined,
510 noBrowser: true,
511 startupPresentation: "browser",
512 desktopBootstrapToken: undefined,
513 autoBootstrapProjectFromCwd: false,
514 logWebSocketEvents: false,
515 tailscaleServeEnabled: false,
516 tailscaleServePort: 443,
517 });
518 }),
519 );
520
521 it.effect("forces noBrowser and disables auto-bootstrap for headless startup presentation", () =>
522 Effect.gen(function* () {
523 const { join } = yield* Path.Path;
524 const baseDir = join(os.tmpdir(), "t3-cli-config-headless-base");
525 const derivedPaths = yield* deriveServerPaths(baseDir, undefined);
526
527 const resolved = yield* resolveServerConfig(
528 {
529 mode: Option.some("web"),
530 port: Option.some(3773),
531 host: Option.none(),
532 baseDir: Option.some(baseDir),
533 cwd: Option.none(),
534 devUrl: Option.none(),
535 noBrowser: Option.none(),
536 bootstrapFd: Option.none(),
537 autoBootstrapProjectFromCwd: Option.none(),
538 logWebSocketEvents: Option.none(),
539 tailscaleServeEnabled: Option.none(),
540 tailscaleServePort: Option.none(),
541 },
542 Option.none(),
543 {
544 startupPresentation: "headless",
545 },
546 ).pipe(
547 Effect.provide(
548 Layer.mergeAll(
549 ConfigProvider.layer(
550 ConfigProvider.fromEnv({
551 env: {
552 T3CODE_NO_BROWSER: "false",
553 T3CODE_AUTO_BOOTSTRAP_PROJECT_FROM_CWD: "true",
554 },
555 }),
556 ),
557 NetService.layer,
558 ),
559 ),
560 );
561
562 expect(resolved).toEqual({
563 logLevel: "Info",
564 ...defaultObservabilityConfig,
565 mode: "web",
566 port: 3773,
567 cwd: process.cwd(),
568 baseDir,
569 ...derivedPaths,
570 host: undefined,
571 staticDir: resolved.staticDir,
572 devUrl: undefined,
573 noBrowser: true,
574 startupPresentation: "headless",
575 desktopBootstrapToken: undefined,
576 autoBootstrapProjectFromCwd: false,
577 logWebSocketEvents: false,
578 tailscaleServeEnabled: false,
579 tailscaleServePort: 443,
580 });
581 }),
582 );
583});