Open History to see the last commit
1// @effect-diagnostics nodeBuiltinImport:off
2import * as NodeFS from "node:fs";
3import * as NodeOS from "node:os";
4
5import { assert, expect, it } from "@effect/vitest";
6import * as ConfigProvider from "effect/ConfigProvider";
7import * as Effect from "effect/Effect";
8import * as FileSystem from "effect/FileSystem";
9import * as Layer from "effect/Layer";
10import * as Option from "effect/Option";
11import * as Path from "effect/Path";
12import * as Redacted from "effect/Redacted";
13import * as Schema from "effect/Schema";
14
15import {
16 DesktopBackendBootstrap,
17 type DesktopBackendBootstrap as DesktopBackendBootstrapValue,
18} from "@t3tools/contracts";
19import * as NetService from "@t3tools/shared/Net";
20import { DEFAULT_SIGNAL_EXPORT } from "@t3tools/shared/observability";
21import * as OtelEnvironment from "@t3tools/shared/otelEnvironment";
22import * as NodeServices from "@effect/platform-node/NodeServices";
23import { deriveServerPaths } from "../config.ts";
24import { resolveServerConfig } from "./config.ts";
25
26const deriveExplicitServerPaths = (baseDir: string, devUrl: URL | undefined) =>
27 deriveServerPaths(baseDir, devUrl, { baseDirIsExplicit: true });
28
29const encodeDesktopBootstrap = Schema.encodeEffect(Schema.fromJsonString(DesktopBackendBootstrap));
30const encodeUnknownJson = Schema.encodeEffect(Schema.fromJsonString(Schema.Unknown));
31
32const makeDesktopBootstrap = (
33 overrides: Partial<DesktopBackendBootstrapValue> = {},
34): DesktopBackendBootstrapValue => ({
35 mode: "desktop",
36 noBrowser: true,
37 port: 4888,
38 t3Home: "/tmp/t3-bootstrap-home",
39 host: "127.0.0.1",
40 desktopBootstrapToken: "desktop-bootstrap-token",
41 tailscaleServeEnabled: false,
42 tailscaleServePort: 443,
43 ...overrides,
44});
45
46it.layer(NodeServices.layer)("cli config resolution", (it) => {
47 const defaultObservabilityConfig = {
48 traceMinLevel: "Info",
49 traceTimingEnabled: true,
50 traceBatchWindowMs: 1_000,
51 traceMaxBytes: 10 * 1024 * 1024,
52 traceMaxFiles: 10,
53 otlpTracesUrl: undefined,
54 otlpMetricsUrl: undefined,
55 otlpLogsUrl: undefined,
56 otlpTracesExport: DEFAULT_SIGNAL_EXPORT,
57 otlpMetricsExport: DEFAULT_SIGNAL_EXPORT,
58 otlpLogsExport: DEFAULT_SIGNAL_EXPORT,
59 otelEnvironment: OtelEnvironment.none,
60 devAllowedOrigins: [],
61 } as const;
62
63 const openBootstrapFd = Effect.fn(function* (payload: DesktopBackendBootstrapValue) {
64 const fs = yield* FileSystem.FileSystem;
65 const filePath = yield* fs.makeTempFileScoped({ prefix: "t3-bootstrap-", suffix: ".ndjson" });
66 const encoded = yield* encodeDesktopBootstrap(payload);
67 yield* fs.writeFileString(filePath, `${encoded}\n`);
68 return yield* Effect.acquireRelease(
69 Effect.sync(() => NodeFS.openSync(filePath, "r")),
70 // Without a /proc or /dev/fd path to reopen, the reader consumes the fd
71 // itself (autoClose), so on Windows it is already closed here.
72 (fd) =>
73 Effect.sync(() => {
74 try {
75 NodeFS.closeSync(fd);
76 } catch (error) {
77 if ((error as NodeJS.ErrnoException).code !== "EBADF") throw error;
78 }
79 }),
80 );
81 });
82
83 it.effect("keeps stale records and supervised startup out of the manual launch preflight", () =>
84 Effect.gen(function* () {
85 const fs = yield* FileSystem.FileSystem;
86 const path = yield* Path.Path;
87 const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-cli-preflight-" });
88 for (const [name, pid, mode, rejectRunningServer] of [
89 ["stale", 2_147_483_647, "web", true],
90 ["desktop", process.pid, "desktop", true],
91 ["serve", process.pid, "web", false],
92 ] as const) {
93 const baseDir = path.join(root, name);
94 const stateDir = path.join(baseDir, "userdata");
95 yield* fs.makeDirectory(stateDir, { recursive: true });
96 yield* fs.writeFileString(
97 path.join(stateDir, "server-runtime.json"),
98 yield* encodeUnknownJson({
99 version: 1,
100 pid,
101 port: 3773,
102 origin: "http://127.0.0.1:3773",
103 startedAt: "2026-10-01T00:00:00.000Z",
104 }),
105 );
106 const cwd = path.join(root, `${name}-project`);
107 const config = yield* resolveServerConfig(
108 {
109 ...minimalWebFlags(baseDir),
110 mode: Option.some(mode),
111 port: Option.some(8788),
112 cwd: Option.some(cwd),
113 },
114 Option.none(),
115 { rejectRunningServer },
116 ).pipe(
117 Effect.provide(
118 Layer.merge(
119 NetService.layer,
120 ConfigProvider.layer(ConfigProvider.fromEnv({ env: {} })),
121 ),
122 ),
123 );
124 expect(config.cwd).toBe(cwd);
125 expect(yield* fs.exists(cwd)).toBe(true);
126 }
127 }),
128 );
129
130 it.effect("enables a trimmed reusable auth token only for web dev mode", () =>
131 Effect.gen(function* () {
132 const baseDir = yield* FileSystem.FileSystem.pipe(
133 Effect.flatMap((fs) => fs.makeTempDirectoryScoped({ prefix: "t3-cli-dev-auth-" })),
134 );
135 const flags = {
136 mode: Option.some("web" as const),
137 port: Option.some(8788),
138 host: Option.none<string>(),
139 baseDir: Option.some(baseDir),
140 cwd: Option.none<string>(),
141 devUrl: Option.some(new URL("http://127.0.0.1:5173")),
142 noBrowser: Option.none<boolean>(),
143 bootstrapFd: Option.none<number>(),
144 autoBootstrapProjectFromCwd: Option.none<boolean>(),
145 logWebSocketEvents: Option.none<boolean>(),
146 tailscaleServeEnabled: Option.none<boolean>(),
147 tailscaleServePort: Option.none<number>(),
148 };
149 const layerConfig = ConfigProvider.layer(
150 ConfigProvider.fromEnv({
151 env: {
152 T3CODE_DEV_AUTH_TOKEN: " reusable-dev-auth-token-that-is-long-enough ",
153 },
154 }),
155 );
156 const web = yield* resolveServerConfig(flags, Option.none()).pipe(
157 Effect.provide(Layer.mergeAll(layerConfig, NetService.layer)),
158 );
159 const desktop = yield* resolveServerConfig(
160 { ...flags, mode: Option.some("desktop" as const) },
161 Option.none(),
162 ).pipe(Effect.provide(Layer.mergeAll(layerConfig, NetService.layer)));
163
164 expect(web.devAuthToken).toBeDefined();
165 if (web.devAuthToken === undefined) {
166 return yield* Effect.die("Expected reusable dev auth token.");
167 }
168 expect(Redacted.value(web.devAuthToken)).toBe("reusable-dev-auth-token-that-is-long-enough");
169 expect(desktop.devAuthToken).toBeUndefined();
170 }),
171 );
172
173 it.effect("does not expose an invalid reusable auth token", () =>
174 Effect.gen(function* () {
175 const secret = "short-secret";
176 const baseDir = yield* FileSystem.FileSystem.pipe(
177 Effect.flatMap((fs) => fs.makeTempDirectoryScoped({ prefix: "t3-cli-dev-auth-invalid-" })),
178 );
179 const flags = {
180 mode: Option.some("web" as const),
181 port: Option.some(8788),
182 host: Option.none<string>(),
183 baseDir: Option.some(baseDir),
184 cwd: Option.none<string>(),
185 devUrl: Option.some(new URL("http://127.0.0.1:5173")),
186 noBrowser: Option.none<boolean>(),
187 bootstrapFd: Option.none<number>(),
188 autoBootstrapProjectFromCwd: Option.none<boolean>(),
189 logWebSocketEvents: Option.none<boolean>(),
190 tailscaleServeEnabled: Option.none<boolean>(),
191 tailscaleServePort: Option.none<number>(),
192 };
193 const layerConfig = ConfigProvider.layer(
194 ConfigProvider.fromEnv({ env: { T3CODE_DEV_AUTH_TOKEN: secret } }),
195 );
196 const error = yield* resolveServerConfig(flags, Option.none()).pipe(
197 Effect.provide(Layer.mergeAll(layerConfig, NetService.layer)),
198 Effect.flip,
199 );
200 const desktop = yield* resolveServerConfig(
201 { ...flags, mode: Option.some("desktop" as const) },
202 Option.none(),
203 ).pipe(Effect.provide(Layer.mergeAll(layerConfig, NetService.layer)));
204 const staticWeb = yield* resolveServerConfig(
205 { ...flags, devUrl: Option.none() },
206 Option.none(),
207 ).pipe(Effect.provide(Layer.mergeAll(layerConfig, NetService.layer)));
208
209 expect(String(error)).not.toContain(secret);
210 const serialized = yield* encodeUnknownJson(error);
211 expect(serialized).not.toContain(secret);
212 expect(desktop.devAuthToken).toBeUndefined();
213 expect(staticWeb.devAuthToken).toBeUndefined();
214 }),
215 );
216
217 it.effect("falls back to effect/config values when flags are omitted", () =>
218 Effect.gen(function* () {
219 const { join } = yield* Path.Path;
220 const baseDir = join(NodeOS.tmpdir(), "t3-cli-config-env-base");
221 const derivedPaths = yield* deriveExplicitServerPaths(
222 baseDir,
223 new URL("http://127.0.0.1:5173"),
224 );
225 const resolved = yield* resolveServerConfig(
226 {
227 mode: Option.none(),
228 port: Option.none(),
229 host: Option.none(),
230 baseDir: Option.none(),
231 cwd: Option.none(),
232 devUrl: Option.none(),
233 noBrowser: Option.none(),
234 bootstrapFd: Option.none(),
235 autoBootstrapProjectFromCwd: Option.none(),
236 logWebSocketEvents: Option.none(),
237 tailscaleServeEnabled: Option.none(),
238 tailscaleServePort: Option.none(),
239 },
240 Option.none(),
241 ).pipe(
242 Effect.provide(
243 Layer.mergeAll(
244 ConfigProvider.layer(
245 ConfigProvider.fromEnv({
246 env: {
247 T3CODE_LOG_LEVEL: "Warn",
248 T3CODE_MODE: "desktop",
249 T3CODE_PORT: "4001",
250 T3CODE_HOST: "0.0.0.0",
251 T3CODE_HOME: baseDir,
252 VITE_DEV_SERVER_URL: "http://127.0.0.1:5173",
253 T3CODE_DEV_ALLOWED_ORIGINS:
254 "https://host.example.ts.net, https://phone.example.ts.net ",
255 T3CODE_NO_BROWSER: "true",
256 T3CODE_AUTO_BOOTSTRAP_PROJECT_FROM_CWD: "false",
257 T3CODE_LOG_WS_EVENTS: "true",
258 },
259 }),
260 ),
261 NetService.layer,
262 ),
263 ),
264 );
265
266 expect(resolved).toEqual({
267 logLevel: "Warn",
268 ...defaultObservabilityConfig,
269 mode: "desktop",
270 port: 4001,
271 cwd: process.cwd(),
272 baseDir,
273 ...derivedPaths,
274 host: "0.0.0.0",
275 staticDir: undefined,
276 devUrl: new URL("http://127.0.0.1:5173"),
277 devAllowedOrigins: ["https://host.example.ts.net", "https://phone.example.ts.net"],
278 noBrowser: true,
279 startupPresentation: "browser",
280 desktopBootstrapToken: undefined,
281 autoBootstrapProjectFromCwd: false,
282 logWebSocketEvents: true,
283 tailscaleServeEnabled: false,
284 tailscaleServePort: 443,
285 });
286 assert.equal(resolved.stateDir, join(baseDir, "userdata"));
287 }),
288 );
289
290 it.effect("uses CLI flags when provided", () =>
291 Effect.gen(function* () {
292 const { join } = yield* Path.Path;
293 const baseDir = join(NodeOS.tmpdir(), "t3-cli-config-flags-base");
294 const derivedPaths = yield* deriveExplicitServerPaths(
295 baseDir,
296 new URL("http://127.0.0.1:4173"),
297 );
298 const resolved = yield* resolveServerConfig(
299 {
300 mode: Option.some("web"),
301 port: Option.some(8788),
302 host: Option.some("127.0.0.1"),
303 baseDir: Option.some(baseDir),
304 cwd: Option.none(),
305 devUrl: Option.some(new URL("http://127.0.0.1:4173")),
306 noBrowser: Option.some(true),
307 bootstrapFd: Option.none(),
308 autoBootstrapProjectFromCwd: Option.some(true),
309 logWebSocketEvents: Option.some(true),
310 tailscaleServeEnabled: Option.some(true),
311 tailscaleServePort: Option.some(8443),
312 },
313 Option.some("Debug"),
314 ).pipe(
315 Effect.provide(
316 Layer.mergeAll(
317 ConfigProvider.layer(
318 ConfigProvider.fromEnv({
319 env: {
320 T3CODE_LOG_LEVEL: "Warn",
321 T3CODE_MODE: "desktop",
322 T3CODE_PORT: "4001",
323 T3CODE_HOST: "0.0.0.0",
324 T3CODE_HOME: join(NodeOS.tmpdir(), "ignored-base"),
325 VITE_DEV_SERVER_URL: "http://127.0.0.1:5173",
326 T3CODE_NO_BROWSER: "false",
327 T3CODE_AUTO_BOOTSTRAP_PROJECT_FROM_CWD: "false",
328 T3CODE_LOG_WS_EVENTS: "false",
329 },
330 }),
331 ),
332 NetService.layer,
333 ),
334 ),
335 );
336
337 expect(resolved).toEqual({
338 logLevel: "Debug",
339 ...defaultObservabilityConfig,
340 mode: "web",
341 port: 8788,
342 cwd: process.cwd(),
343 baseDir,
344 ...derivedPaths,
345 host: "127.0.0.1",
346 staticDir: undefined,
347 devUrl: new URL("http://127.0.0.1:4173"),
348 noBrowser: true,
349 startupPresentation: "browser",
350 desktopBootstrapToken: undefined,
351 autoBootstrapProjectFromCwd: true,
352 logWebSocketEvents: true,
353 tailscaleServeEnabled: true,
354 tailscaleServePort: 8443,
355 });
356 assert.equal(resolved.dbPath, join(baseDir, "userdata", "statev2.sqlite"));
357 }),
358 );
359
360 it.effect("preserves explicit false CLI boolean flags over env and bootstrap values", () =>
361 Effect.gen(function* () {
362 const { join } = yield* Path.Path;
363 const baseDir = join(NodeOS.tmpdir(), "t3-cli-config-false-flags");
364 const fd = yield* openBootstrapFd(
365 makeDesktopBootstrap({
366 noBrowser: true,
367 tailscaleServeEnabled: false,
368 tailscaleServePort: 443,
369 }),
370 );
371 const derivedPaths = yield* deriveExplicitServerPaths(
372 baseDir,
373 new URL("http://127.0.0.1:4173"),
374 );
375
376 const resolved = yield* resolveServerConfig(
377 {
378 mode: Option.some("web"),
379 port: Option.some(8788),
380 host: Option.some("127.0.0.1"),
381 baseDir: Option.some(baseDir),
382 cwd: Option.none(),
383 devUrl: Option.some(new URL("http://127.0.0.1:4173")),
384 noBrowser: Option.some(false),
385 bootstrapFd: Option.none(),
386 autoBootstrapProjectFromCwd: Option.some(false),
387 logWebSocketEvents: Option.some(false),
388 tailscaleServeEnabled: Option.none(),
389 tailscaleServePort: Option.none(),
390 },
391 Option.none(),
392 ).pipe(
393 Effect.provide(
394 Layer.mergeAll(
395 ConfigProvider.layer(
396 ConfigProvider.fromEnv({
397 env: {
398 T3CODE_BOOTSTRAP_FD: String(fd),
399 T3CODE_NO_BROWSER: "true",
400 T3CODE_AUTO_BOOTSTRAP_PROJECT_FROM_CWD: "true",
401 T3CODE_LOG_WS_EVENTS: "true",
402 },
403 }),
404 ),
405 NetService.layer,
406 ),
407 ),
408 );
409
410 expect(resolved).toEqual({
411 logLevel: "Info",
412 ...defaultObservabilityConfig,
413 mode: "web",
414 port: 8788,
415 cwd: process.cwd(),
416 baseDir,
417 ...derivedPaths,
418 host: "127.0.0.1",
419 staticDir: undefined,
420 devUrl: new URL("http://127.0.0.1:4173"),
421 noBrowser: false,
422 startupPresentation: "browser",
423 desktopBootstrapToken: "desktop-bootstrap-token",
424 autoBootstrapProjectFromCwd: false,
425 logWebSocketEvents: false,
426 tailscaleServeEnabled: false,
427 tailscaleServePort: 443,
428 });
429 }),
430 );
431
432 it.effect("uses bootstrap envelope values as fallbacks when flags and env are absent", () =>
433 Effect.gen(function* () {
434 const { join, resolve } = yield* Path.Path;
435 // The resolver absolutises the configured home, so the expectation must
436 // carry the host's drive on Windows.
437 const baseDir = resolve("/tmp/t3-bootstrap-home");
438 const fd = yield* openBootstrapFd(
439 makeDesktopBootstrap({
440 port: 4888,
441 host: "127.0.0.2",
442 t3Home: "/tmp/t3-bootstrap-home",
443 noBrowser: true,
444 desktopBootstrapToken: "desktop-token",
445 desktopTelemetryFd: 4,
446 desktopTelemetryControlFd: 5,
447 tailscaleServeEnabled: false,
448 tailscaleServePort: 443,
449 otlpTracesUrl: "http://localhost:4318/v1/traces",
450 otlpMetricsUrl: "http://localhost:4318/v1/metrics",
451 otlpLogsUrl: "http://localhost:4318/v1/logs",
452 }),
453 );
454 const derivedPaths = yield* deriveServerPaths(baseDir, undefined);
455
456 const resolved = yield* resolveServerConfig(
457 {
458 mode: Option.none(),
459 port: Option.none(),
460 host: Option.none(),
461 baseDir: Option.none(),
462 cwd: Option.none(),
463 devUrl: Option.none(),
464 noBrowser: Option.none(),
465 bootstrapFd: Option.none(),
466 autoBootstrapProjectFromCwd: Option.none(),
467 logWebSocketEvents: Option.none(),
468 tailscaleServeEnabled: Option.none(),
469 tailscaleServePort: Option.none(),
470 },
471 Option.none(),
472 ).pipe(
473 Effect.provide(
474 Layer.mergeAll(
475 ConfigProvider.layer(
476 ConfigProvider.fromEnv({
477 env: {
478 T3CODE_BOOTSTRAP_FD: String(fd),
479 },
480 }),
481 ),
482 NetService.layer,
483 ),
484 ),
485 );
486
487 expect(resolved).toEqual({
488 logLevel: "Info",
489 ...defaultObservabilityConfig,
490 otlpTracesUrl: "http://localhost:4318/v1/traces",
491 otlpMetricsUrl: "http://localhost:4318/v1/metrics",
492 otlpLogsUrl: "http://localhost:4318/v1/logs",
493 mode: "desktop",
494 port: 4888,
495 cwd: process.cwd(),
496 baseDir,
497 ...derivedPaths,
498 host: "127.0.0.2",
499 staticDir: resolved.staticDir,
500 devUrl: undefined,
501 noBrowser: true,
502 startupPresentation: "browser",
503 desktopBootstrapToken: "desktop-token",
504 desktopTelemetryFd: 4,
505 desktopTelemetryControlFd: 5,
506 resourceMonitorPath: undefined,
507 autoBootstrapProjectFromCwd: false,
508 logWebSocketEvents: false,
509 tailscaleServeEnabled: false,
510 tailscaleServePort: 443,
511 });
512 assert.equal(join(baseDir, "userdata"), resolved.stateDir);
513 assert.equal(resolved.desktopTelemetryFd, 4);
514 assert.equal(resolved.desktopTelemetryControlFd, 5);
515 }),
516 );
517
518 it.effect("creates derived runtime directories during config resolution", () =>
519 Effect.gen(function* () {
520 const fs = yield* FileSystem.FileSystem;
521 const path = yield* Path.Path;
522 const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-cli-config-dirs-" });
523 const customCwd = path.join(baseDir, "nested", "project");
524
525 const resolved = yield* resolveServerConfig(
526 {
527 mode: Option.some("desktop"),
528 port: Option.some(4888),
529 host: Option.none(),
530 baseDir: Option.some(baseDir),
531 cwd: Option.some(customCwd),
532 devUrl: Option.some(new URL("http://127.0.0.1:5173")),
533 noBrowser: Option.none(),
534 bootstrapFd: Option.none(),
535 autoBootstrapProjectFromCwd: Option.none(),
536 logWebSocketEvents: Option.none(),
537 tailscaleServeEnabled: Option.none(),
538 tailscaleServePort: Option.none(),
539 },
540 Option.none(),
541 ).pipe(
542 Effect.provide(
543 Layer.mergeAll(
544 ConfigProvider.layer(ConfigProvider.fromEnv({ env: {} })),
545 NetService.layer,
546 ),
547 ),
548 );
549
550 for (const directory of [
551 customCwd,
552 resolved.stateDir,
553 resolved.logsDir,
554 resolved.providerLogsDir,
555 resolved.terminalLogsDir,
556 resolved.attachmentsDir,
557 resolved.worktreesDir,
558 path.dirname(resolved.serverTracePath),
559 ]) {
560 expect(yield* fs.exists(directory)).toBe(true);
561 }
562 expect(resolved.cwd).toBe(path.resolve(customCwd));
563 }),
564 );
565
566 it.effect("applies flag then env precedence over bootstrap envelope values", () =>
567 Effect.gen(function* () {
568 const { join } = yield* Path.Path;
569 const baseDir = join(NodeOS.tmpdir(), "t3-cli-config-env-wins");
570 const fd = yield* openBootstrapFd(
571 makeDesktopBootstrap({
572 port: 4888,
573 host: "127.0.0.2",
574 t3Home: "/tmp/t3-bootstrap-home",
575 noBrowser: false,
576 desktopBootstrapToken: "desktop-token",
577 tailscaleServeEnabled: false,
578 tailscaleServePort: 443,
579 }),
580 );
581 const derivedPaths = yield* deriveExplicitServerPaths(
582 baseDir,
583 new URL("http://127.0.0.1:4173"),
584 );
585
586 const resolved = yield* resolveServerConfig(
587 {
588 mode: Option.none(),
589 port: Option.some(8788),
590 host: Option.some("127.0.0.1"),
591 baseDir: Option.none(),
592 cwd: Option.none(),
593 devUrl: Option.some(new URL("http://127.0.0.1:4173")),
594 noBrowser: Option.none(),
595 bootstrapFd: Option.none(),
596 autoBootstrapProjectFromCwd: Option.none(),
597 logWebSocketEvents: Option.none(),
598 tailscaleServeEnabled: Option.none(),
599 tailscaleServePort: Option.none(),
600 },
601 Option.some("Debug"),
602 ).pipe(
603 Effect.provide(
604 Layer.mergeAll(
605 ConfigProvider.layer(
606 ConfigProvider.fromEnv({
607 env: {
608 T3CODE_MODE: "web",
609 T3CODE_BOOTSTRAP_FD: String(fd),
610 T3CODE_HOME: baseDir,
611 T3CODE_NO_BROWSER: "true",
612 T3CODE_AUTO_BOOTSTRAP_PROJECT_FROM_CWD: "true",
613 T3CODE_LOG_WS_EVENTS: "true",
614 },
615 }),
616 ),
617 NetService.layer,
618 ),
619 ),
620 );
621
622 expect(resolved).toEqual({
623 logLevel: "Debug",
624 ...defaultObservabilityConfig,
625 mode: "web",
626 port: 8788,
627 cwd: process.cwd(),
628 baseDir,
629 ...derivedPaths,
630 host: "127.0.0.1",
631 staticDir: undefined,
632 devUrl: new URL("http://127.0.0.1:4173"),
633 noBrowser: true,
634 startupPresentation: "browser",
635 desktopBootstrapToken: "desktop-token",
636 autoBootstrapProjectFromCwd: true,
637 logWebSocketEvents: true,
638 tailscaleServeEnabled: false,
639 tailscaleServePort: 443,
640 });
641 }),
642 );
643
644 it.effect("falls back to persisted observability settings when env vars are absent", () =>
645 Effect.gen(function* () {
646 const fs = yield* FileSystem.FileSystem;
647 const path = yield* Path.Path;
648 const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-cli-config-settings-" });
649 const derivedPaths = yield* deriveExplicitServerPaths(baseDir, undefined);
650 yield* fs.makeDirectory(path.dirname(derivedPaths.settingsPath), { recursive: true });
651 yield* fs.writeFileString(
652 derivedPaths.settingsPath,
653 // @effect-diagnostics-next-line preferSchemaOverJson:off
654 `${JSON.stringify({
655 observability: {
656 otlpTracesUrl: "http://localhost:4318/v1/traces",
657 otlpMetricsUrl: "http://localhost:4318/v1/metrics",
658 otlpLogsUrl: "http://localhost:4318/v1/logs",
659 },
660 })}\n`,
661 );
662
663 const resolved = yield* resolveServerConfig(
664 {
665 mode: Option.some("desktop"),
666 port: Option.some(4888),
667 host: Option.none(),
668 baseDir: Option.some(baseDir),
669 cwd: Option.none(),
670 devUrl: Option.none(),
671 noBrowser: Option.none(),
672 bootstrapFd: Option.none(),
673 autoBootstrapProjectFromCwd: Option.none(),
674 logWebSocketEvents: Option.none(),
675 tailscaleServeEnabled: Option.none(),
676 tailscaleServePort: Option.none(),
677 },
678 Option.none(),
679 ).pipe(
680 Effect.provide(
681 Layer.mergeAll(
682 ConfigProvider.layer(ConfigProvider.fromEnv({ env: {} })),
683 NetService.layer,
684 ),
685 ),
686 );
687
688 expect(resolved.otlpTracesUrl).toBe("http://localhost:4318/v1/traces");
689 expect(resolved.otlpMetricsUrl).toBe("http://localhost:4318/v1/metrics");
690 expect(resolved.otlpLogsUrl).toBe("http://localhost:4318/v1/logs");
691 expect(resolved).toEqual({
692 logLevel: "Info",
693 ...defaultObservabilityConfig,
694 otlpTracesUrl: "http://localhost:4318/v1/traces",
695 otlpMetricsUrl: "http://localhost:4318/v1/metrics",
696 otlpLogsUrl: "http://localhost:4318/v1/logs",
697 mode: "desktop",
698 port: 4888,
699 cwd: process.cwd(),
700 baseDir,
701 ...derivedPaths,
702 host: "127.0.0.1",
703 staticDir: resolved.staticDir,
704 devUrl: undefined,
705 noBrowser: true,
706 startupPresentation: "browser",
707 desktopBootstrapToken: undefined,
708 autoBootstrapProjectFromCwd: false,
709 logWebSocketEvents: false,
710 tailscaleServeEnabled: false,
711 tailscaleServePort: 443,
712 });
713 }),
714 );
715
716 it.effect("zeroes an endpoint stored in Settings when the SDK is disabled", () =>
717 Effect.gen(function* () {
718 const fs = yield* FileSystem.FileSystem;
719 const path = yield* Path.Path;
720 const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-cli-config-otel-off-" });
721 const derivedPaths = yield* deriveExplicitServerPaths(baseDir, undefined);
722 yield* fs.makeDirectory(path.dirname(derivedPaths.settingsPath), { recursive: true });
723 yield* fs.writeFileString(
724 derivedPaths.settingsPath,
725 // @effect-diagnostics-next-line preferSchemaOverJson:off
726 `${JSON.stringify({
727 observability: {
728 otlpTracesUrl: "http://localhost:4318/v1/traces",
729 otlpMetricsUrl: "http://localhost:4318/v1/metrics",
730 otlpLogsUrl: "http://localhost:4318/v1/logs",
731 },
732 })}\n`,
733 );
734
735 const resolved = yield* resolveServerConfig(
736 {
737 mode: Option.some("desktop"),
738 port: Option.some(4888),
739 host: Option.none(),
740 baseDir: Option.some(baseDir),
741 cwd: Option.none(),
742 devUrl: Option.none(),
743 noBrowser: Option.none(),
744 bootstrapFd: Option.none(),
745 autoBootstrapProjectFromCwd: Option.none(),
746 logWebSocketEvents: Option.none(),
747 tailscaleServeEnabled: Option.none(),
748 tailscaleServePort: Option.none(),
749 },
750 Option.none(),
751 ).pipe(
752 Effect.provide(
753 Layer.mergeAll(
754 ConfigProvider.layer(ConfigProvider.fromEnv({ env: { OTEL_SDK_DISABLED: "true" } })),
755 NetService.layer,
756 ),
757 ),
758 );
759
760 // The switch beats every source, including an endpoint stored in Settings.
761 expect(resolved.otlpTracesUrl).toBeUndefined();
762 expect(resolved.otlpMetricsUrl).toBeUndefined();
763 expect(resolved.otlpLogsUrl).toBeUndefined();
764 expect(resolved.otelEnvironment.disabled).toBe(true);
765 }),
766 );
767
768 it.effect("lets T3CODE_OTEL_SDK_DISABLED=false override an ambient OTEL_SDK_DISABLED=true", () =>
769 Effect.gen(function* () {
770 const fs = yield* FileSystem.FileSystem;
771 const path = yield* Path.Path;
772 const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-cli-config-otel-on-" });
773 const derivedPaths = yield* deriveExplicitServerPaths(baseDir, undefined);
774 yield* fs.makeDirectory(path.dirname(derivedPaths.settingsPath), { recursive: true });
775 yield* fs.writeFileString(
776 derivedPaths.settingsPath,
777 // @effect-diagnostics-next-line preferSchemaOverJson:off
778 `${JSON.stringify({
779 observability: {
780 otlpTracesUrl: "http://localhost:4318/v1/traces",
781 },
782 })}\n`,
783 );
784
785 const resolved = yield* resolveServerConfig(
786 {
787 mode: Option.some("desktop"),
788 port: Option.some(4888),
789 host: Option.none(),
790 baseDir: Option.some(baseDir),
791 cwd: Option.none(),
792 devUrl: Option.none(),
793 noBrowser: Option.none(),
794 bootstrapFd: Option.none(),
795 autoBootstrapProjectFromCwd: Option.none(),
796 logWebSocketEvents: Option.none(),
797 tailscaleServeEnabled: Option.none(),
798 tailscaleServePort: Option.none(),
799 },
800 Option.none(),
801 ).pipe(
802 Effect.provide(
803 Layer.mergeAll(
804 ConfigProvider.layer(
805 ConfigProvider.fromEnv({
806 env: { T3CODE_OTEL_SDK_DISABLED: "false", OTEL_SDK_DISABLED: "true" },
807 }),
808 ),
809 NetService.layer,
810 ),
811 ),
812 );
813
814 expect(resolved.otelEnvironment.disabled).toBe(false);
815 expect(resolved.otlpTracesUrl).toBe("http://localhost:4318/v1/traces");
816 }),
817 );
818
819 it.effect("forces noBrowser and disables auto-bootstrap for headless startup presentation", () =>
820 Effect.gen(function* () {
821 const { join } = yield* Path.Path;
822 const baseDir = join(NodeOS.tmpdir(), "t3-cli-config-headless-base");
823 const derivedPaths = yield* deriveExplicitServerPaths(baseDir, undefined);
824
825 const resolved = yield* resolveServerConfig(
826 {
827 mode: Option.some("web"),
828 port: Option.some(3773),
829 host: Option.none(),
830 baseDir: Option.some(baseDir),
831 cwd: Option.none(),
832 devUrl: Option.none(),
833 noBrowser: Option.none(),
834 bootstrapFd: Option.none(),
835 autoBootstrapProjectFromCwd: Option.none(),
836 logWebSocketEvents: Option.none(),
837 tailscaleServeEnabled: Option.none(),
838 tailscaleServePort: Option.none(),
839 },
840 Option.none(),
841 {
842 startupPresentation: "headless",
843 },
844 ).pipe(
845 Effect.provide(
846 Layer.mergeAll(
847 ConfigProvider.layer(
848 ConfigProvider.fromEnv({
849 env: {
850 T3CODE_NO_BROWSER: "false",
851 T3CODE_AUTO_BOOTSTRAP_PROJECT_FROM_CWD: "true",
852 },
853 }),
854 ),
855 NetService.layer,
856 ),
857 ),
858 );
859
860 expect(resolved).toEqual({
861 logLevel: "Info",
862 ...defaultObservabilityConfig,
863 mode: "web",
864 port: 3773,
865 cwd: process.cwd(),
866 baseDir,
867 ...derivedPaths,
868 host: undefined,
869 staticDir: resolved.staticDir,
870 devUrl: undefined,
871 noBrowser: true,
872 startupPresentation: "headless",
873 desktopBootstrapToken: undefined,
874 autoBootstrapProjectFromCwd: false,
875 logWebSocketEvents: false,
876 tailscaleServeEnabled: false,
877 tailscaleServePort: 443,
878 });
879 }),
880 );
881
882 it.effect("decodes percent-encoded OTLP headers from env", () =>
883 Effect.gen(function* () {
884 const { join } = yield* Path.Path;
885 const baseDir = join(NodeOS.tmpdir(), "t3-cli-config-otlp-headers-base");
886
887 const resolved = yield* resolveServerConfig(
888 {
889 mode: Option.some("web"),
890 port: Option.some(3773),
891 host: Option.none(),
892 baseDir: Option.some(baseDir),
893 cwd: Option.none(),
894 devUrl: Option.none(),
895 noBrowser: Option.none(),
896 bootstrapFd: Option.none(),
897 autoBootstrapProjectFromCwd: Option.none(),
898 logWebSocketEvents: Option.none(),
899 tailscaleServeEnabled: Option.none(),
900 tailscaleServePort: Option.none(),
901 },
902 Option.none(),
903 ).pipe(
904 Effect.provide(
905 Layer.mergeAll(
906 ConfigProvider.layer(
907 ConfigProvider.fromEnv({
908 env: {
909 T3CODE_OTLP_HEADERS: "authorization=Basic%20abc%3D%3D,x-tenant=t3",
910 },
911 }),
912 ),
913 NetService.layer,
914 ),
915 ),
916 );
917
918 expect(resolved.otlpTracesExport.headers).toEqual({
919 authorization: "Basic abc==",
920 "x-tenant": "t3",
921 });
922 }),
923 );
924
925 it.effect("keeps whitespace-separated pairs and literal equals signs in OTLP headers", () =>
926 Effect.gen(function* () {
927 const { join } = yield* Path.Path;
928 const baseDir = join(NodeOS.tmpdir(), "t3-cli-config-otlp-headers-loose-base");
929
930 const resolved = yield* resolveServerConfig(
931 {
932 mode: Option.some("web"),
933 port: Option.some(3773),
934 host: Option.none(),
935 baseDir: Option.some(baseDir),
936 cwd: Option.none(),
937 devUrl: Option.none(),
938 noBrowser: Option.none(),
939 bootstrapFd: Option.none(),
940 autoBootstrapProjectFromCwd: Option.none(),
941 logWebSocketEvents: Option.none(),
942 tailscaleServeEnabled: Option.none(),
943 tailscaleServePort: Option.none(),
944 },
945 Option.none(),
946 ).pipe(
947 Effect.provide(
948 Layer.mergeAll(
949 ConfigProvider.layer(
950 ConfigProvider.fromEnv({
951 env: {
952 T3CODE_OTLP_HEADERS: "authorization=Bearer abc==, x-tenant=t3",
953 T3CODE_OTLP_TRACES_URL: "http://collector.internal:4318",
954 },
955 }),
956 ),
957 NetService.layer,
958 ),
959 ),
960 );
961
962 expect(resolved.otlpTracesExport.headers).toEqual({
963 authorization: "Bearer abc==",
964 "x-tenant": "t3",
965 });
966 expect(resolved.otlpTracesUrl).toBe("http://collector.internal:4318");
967 }),
968 );
969
970 it.effect("gives every signal the protocol named without one", () =>
971 Effect.gen(function* () {
972 const { join } = yield* Path.Path;
973 const baseDir = join(NodeOS.tmpdir(), "t3-cli-config-otlp-protocol-base");
974
975 const resolved = yield* resolveServerConfig(
976 {
977 mode: Option.some("web"),
978 port: Option.some(3773),
979 host: Option.none(),
980 baseDir: Option.some(baseDir),
981 cwd: Option.none(),
982 devUrl: Option.none(),
983 noBrowser: Option.none(),
984 bootstrapFd: Option.none(),
985 autoBootstrapProjectFromCwd: Option.none(),
986 logWebSocketEvents: Option.none(),
987 tailscaleServeEnabled: Option.none(),
988 tailscaleServePort: Option.none(),
989 },
990 Option.none(),
991 ).pipe(
992 Effect.provide(
993 Layer.mergeAll(
994 ConfigProvider.layer(
995 ConfigProvider.fromEnv({ env: { T3CODE_OTLP_PROTOCOL: "http/protobuf" } }),
996 ),
997 NetService.layer,
998 ),
999 ),
1000 );
1001
1002 expect([
1003 resolved.otlpTracesExport.protocol,
1004 resolved.otlpMetricsExport.protocol,
1005 resolved.otlpLogsExport.protocol,
1006 ]).toEqual(["http/protobuf", "http/protobuf", "http/protobuf"]);
1007 }),
1008 );
1009
1010 it.effect("reads the OTLP logs URL from env", () =>
1011 Effect.gen(function* () {
1012 const { join } = yield* Path.Path;
1013 const baseDir = join(NodeOS.tmpdir(), "t3-cli-config-otlp-logs-url-base");
1014
1015 const resolved = yield* resolveServerConfig(
1016 {
1017 mode: Option.some("web"),
1018 port: Option.some(3773),
1019 host: Option.none(),
1020 baseDir: Option.some(baseDir),
1021 cwd: Option.none(),
1022 devUrl: Option.none(),
1023 noBrowser: Option.none(),
1024 bootstrapFd: Option.none(),
1025 autoBootstrapProjectFromCwd: Option.none(),
1026 logWebSocketEvents: Option.none(),
1027 tailscaleServeEnabled: Option.none(),
1028 tailscaleServePort: Option.none(),
1029 },
1030 Option.none(),
1031 ).pipe(
1032 Effect.provide(
1033 Layer.mergeAll(
1034 ConfigProvider.layer(
1035 ConfigProvider.fromEnv({
1036 env: { T3CODE_OTLP_LOGS_URL: "http://collector.internal:4318/v1/logs" },
1037 }),
1038 ),
1039 NetService.layer,
1040 ),
1041 ),
1042 );
1043
1044 expect(resolved.otlpLogsUrl).toBe("http://collector.internal:4318/v1/logs");
1045 }),
1046 );
1047
1048 const minimalWebFlags = (baseDir: string) => ({
1049 mode: Option.some("web" as const),
1050 port: Option.some(3773),
1051 host: Option.none<string>(),
1052 baseDir: Option.some(baseDir),
1053 cwd: Option.none<string>(),
1054 devUrl: Option.none<URL>(),
1055 noBrowser: Option.none<boolean>(),
1056 bootstrapFd: Option.none<number>(),
1057 autoBootstrapProjectFromCwd: Option.none<boolean>(),
1058 logWebSocketEvents: Option.none<boolean>(),
1059 tailscaleServeEnabled: Option.none<boolean>(),
1060 tailscaleServePort: Option.none<number>(),
1061 });
1062
1063 it.effect(
1064 "resolves each signal's endpoint through T3CODE_OTLP_*_URL, an OTEL endpoint, the bootstrap envelope, and persisted Settings, in that order",
1065 () =>
1066 Effect.gen(function* () {
1067 const fs = yield* FileSystem.FileSystem;
1068 const path = yield* Path.Path;
1069 const baseDir = yield* fs.makeTempDirectoryScoped({
1070 prefix: "t3-cli-config-otel-precedence-",
1071 });
1072 const derivedPaths = yield* deriveExplicitServerPaths(baseDir, undefined);
1073 yield* fs.makeDirectory(path.dirname(derivedPaths.settingsPath), { recursive: true });
1074 yield* fs.writeFileString(
1075 derivedPaths.settingsPath,
1076 // @effect-diagnostics-next-line preferSchemaOverJson:off
1077 `${JSON.stringify({ observability: { otlpLogsUrl: "http://settings:4318/v1/logs" } })}\n`,
1078 );
1079
1080 const fd = yield* openBootstrapFd(
1081 makeDesktopBootstrap({
1082 otlpMetricsUrl: "http://bootstrap:4318/v1/metrics",
1083 // Blank, not an endpoint: it must not stand in front of Settings.
1084 otlpLogsUrl: "",
1085 }),
1086 );
1087
1088 const resolved = yield* resolveServerConfig(
1089 {
1090 ...minimalWebFlags(baseDir),
1091 mode: Option.some("desktop"),
1092 port: Option.some(4888),
1093 bootstrapFd: Option.some(fd),
1094 },
1095 Option.none(),
1096 ).pipe(
1097 Effect.provide(
1098 Layer.mergeAll(
1099 ConfigProvider.layer(
1100 ConfigProvider.fromEnv({
1101 env: {
1102 T3CODE_OTLP_TRACES_URL: "http://t3:4318/v1/traces",
1103 T3CODE_OTLP_HEADERS: "x-key=secret",
1104 OTEL_EXPORTER_OTLP_TRACES_ENDPOINT: "http://otel-traces:4318/custom",
1105 OTEL_EXPORTER_OTLP_METRICS_ENDPOINT: "http://otel-metrics:4318/custom",
1106 OTEL_EXPORTER_OTLP_HEADERS: "x-key=otel",
1107 },
1108 }),
1109 ),
1110 NetService.layer,
1111 ),
1112 ),
1113 );
1114
1115 // T3CODE_OTLP_TRACES_URL wins over the OTEL variable for the same
1116 // signal, and keeps T3 Code's own headers since T3 Code still owns it.
1117 expect(resolved.otlpTracesUrl).toBe("http://t3:4318/v1/traces");
1118 expect(resolved.otlpTracesExport.headers).toEqual({ "x-key": "secret" });
1119 // Metrics named no T3CODE_OTLP_METRICS_URL, so the OTEL endpoint wins
1120 // over the bootstrap envelope and brings the OTEL headers and protocol.
1121 expect(resolved.otlpMetricsUrl).toBe("http://otel-metrics:4318/custom");
1122 expect(resolved.otlpMetricsExport).toEqual({
1123 ...DEFAULT_SIGNAL_EXPORT,
1124 protocol: "http/protobuf",
1125 headers: { "x-key": "otel" },
1126 });
1127 // Logs named no T3 or OTEL endpoint and a blank bootstrap value, so
1128 // Settings answers, and logs keep the shared headers since no OTEL
1129 // endpoint claimed them.
1130 expect(resolved.otlpLogsUrl).toBe("http://settings:4318/v1/logs");
1131 expect(resolved.otlpLogsExport.headers).toEqual({ "x-key": "secret" });
1132 }),
1133 );
1134
1135 it.effect(
1136 "exports nothing for a signal an OTEL endpoint claimed with a protocol or headers that do not read",
1137 () =>
1138 Effect.gen(function* () {
1139 const fs = yield* FileSystem.FileSystem;
1140 const path = yield* Path.Path;
1141 const baseDir = yield* fs.makeTempDirectoryScoped({
1142 prefix: "t3-cli-config-otel-off-",
1143 });
1144 const derivedPaths = yield* deriveExplicitServerPaths(baseDir, undefined);
1145 yield* fs.makeDirectory(path.dirname(derivedPaths.settingsPath), { recursive: true });
1146 yield* fs.writeFileString(
1147 derivedPaths.settingsPath,
1148 // @effect-diagnostics-next-line preferSchemaOverJson:off
1149 `${JSON.stringify({ observability: { otlpLogsUrl: "http://settings:4318/v1/logs" } })}\n`,
1150 );
1151
1152 const fd = yield* openBootstrapFd(
1153 makeDesktopBootstrap({ otlpMetricsUrl: "http://bootstrap:4318/v1/metrics" }),
1154 );
1155
1156 const resolved = yield* resolveServerConfig(
1157 {
1158 ...minimalWebFlags(baseDir),
1159 mode: Option.some("desktop"),
1160 port: Option.some(4888),
1161 bootstrapFd: Option.some(fd),
1162 },
1163 Option.none(),
1164 ).pipe(
1165 Effect.provide(
1166 Layer.mergeAll(
1167 ConfigProvider.layer(
1168 ConfigProvider.fromEnv({
1169 env: {
1170 T3CODE_OTLP_TRACES_URL: "http://t3:4318/v1/traces",
1171 OTEL_EXPORTER_OTLP_ENDPOINT: "http://otel:4318",
1172 OTEL_EXPORTER_OTLP_HEADERS: "x-key=%zz",
1173 },
1174 }),
1175 ),
1176 NetService.layer,
1177 ),
1178 ),
1179 );
1180
1181 // T3CODE_OTLP_TRACES_URL still wins outright.
1182 expect(resolved.otlpTracesUrl).toBe("http://t3:4318/v1/traces");
1183 // The OTEL endpoint claimed metrics and logs, so neither the bootstrap
1184 // envelope nor Settings receives them with T3 Code's headers.
1185 expect(resolved.otlpMetricsUrl).toBeUndefined();
1186 expect(resolved.otlpLogsUrl).toBeUndefined();
1187 }),
1188 );
1189});