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 `${JSON.stringify({
654 observability: {
655 otlpTracesUrl: "http://localhost:4318/v1/traces",
656 otlpMetricsUrl: "http://localhost:4318/v1/metrics",
657 otlpLogsUrl: "http://localhost:4318/v1/logs",
658 },
659 })}\n`,
660 );
661
662 const resolved = yield* resolveServerConfig(
663 {
664 mode: Option.some("desktop"),
665 port: Option.some(4888),
666 host: Option.none(),
667 baseDir: Option.some(baseDir),
668 cwd: Option.none(),
669 devUrl: Option.none(),
670 noBrowser: Option.none(),
671 bootstrapFd: Option.none(),
672 autoBootstrapProjectFromCwd: Option.none(),
673 logWebSocketEvents: Option.none(),
674 tailscaleServeEnabled: Option.none(),
675 tailscaleServePort: Option.none(),
676 },
677 Option.none(),
678 ).pipe(
679 Effect.provide(
680 Layer.mergeAll(
681 ConfigProvider.layer(ConfigProvider.fromEnv({ env: {} })),
682 NetService.layer,
683 ),
684 ),
685 );
686
687 expect(resolved.otlpTracesUrl).toBe("http://localhost:4318/v1/traces");
688 expect(resolved.otlpMetricsUrl).toBe("http://localhost:4318/v1/metrics");
689 expect(resolved.otlpLogsUrl).toBe("http://localhost:4318/v1/logs");
690 expect(resolved).toEqual({
691 logLevel: "Info",
692 ...defaultObservabilityConfig,
693 otlpTracesUrl: "http://localhost:4318/v1/traces",
694 otlpMetricsUrl: "http://localhost:4318/v1/metrics",
695 otlpLogsUrl: "http://localhost:4318/v1/logs",
696 mode: "desktop",
697 port: 4888,
698 cwd: process.cwd(),
699 baseDir,
700 ...derivedPaths,
701 host: "127.0.0.1",
702 staticDir: resolved.staticDir,
703 devUrl: undefined,
704 noBrowser: true,
705 startupPresentation: "browser",
706 desktopBootstrapToken: undefined,
707 autoBootstrapProjectFromCwd: false,
708 logWebSocketEvents: false,
709 tailscaleServeEnabled: false,
710 tailscaleServePort: 443,
711 });
712 }),
713 );
714
715 it.effect("zeroes an endpoint stored in Settings when the SDK is disabled", () =>
716 Effect.gen(function* () {
717 const fs = yield* FileSystem.FileSystem;
718 const path = yield* Path.Path;
719 const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-cli-config-otel-off-" });
720 const derivedPaths = yield* deriveExplicitServerPaths(baseDir, undefined);
721 yield* fs.makeDirectory(path.dirname(derivedPaths.settingsPath), { recursive: true });
722 yield* fs.writeFileString(
723 derivedPaths.settingsPath,
724 `${JSON.stringify({
725 observability: {
726 otlpTracesUrl: "http://localhost:4318/v1/traces",
727 otlpMetricsUrl: "http://localhost:4318/v1/metrics",
728 otlpLogsUrl: "http://localhost:4318/v1/logs",
729 },
730 })}\n`,
731 );
732
733 const resolved = yield* resolveServerConfig(
734 {
735 mode: Option.some("desktop"),
736 port: Option.some(4888),
737 host: Option.none(),
738 baseDir: Option.some(baseDir),
739 cwd: Option.none(),
740 devUrl: Option.none(),
741 noBrowser: Option.none(),
742 bootstrapFd: Option.none(),
743 autoBootstrapProjectFromCwd: Option.none(),
744 logWebSocketEvents: Option.none(),
745 tailscaleServeEnabled: Option.none(),
746 tailscaleServePort: Option.none(),
747 },
748 Option.none(),
749 ).pipe(
750 Effect.provide(
751 Layer.mergeAll(
752 ConfigProvider.layer(ConfigProvider.fromEnv({ env: { OTEL_SDK_DISABLED: "true" } })),
753 NetService.layer,
754 ),
755 ),
756 );
757
758 // The switch beats every source, including an endpoint stored in Settings.
759 expect(resolved.otlpTracesUrl).toBeUndefined();
760 expect(resolved.otlpMetricsUrl).toBeUndefined();
761 expect(resolved.otlpLogsUrl).toBeUndefined();
762 expect(resolved.otelEnvironment.disabled).toBe(true);
763 }),
764 );
765
766 it.effect("lets T3CODE_OTEL_SDK_DISABLED=false override an ambient OTEL_SDK_DISABLED=true", () =>
767 Effect.gen(function* () {
768 const fs = yield* FileSystem.FileSystem;
769 const path = yield* Path.Path;
770 const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-cli-config-otel-on-" });
771 const derivedPaths = yield* deriveExplicitServerPaths(baseDir, undefined);
772 yield* fs.makeDirectory(path.dirname(derivedPaths.settingsPath), { recursive: true });
773 yield* fs.writeFileString(
774 derivedPaths.settingsPath,
775 `${JSON.stringify({
776 observability: {
777 otlpTracesUrl: "http://localhost:4318/v1/traces",
778 },
779 })}\n`,
780 );
781
782 const resolved = yield* resolveServerConfig(
783 {
784 mode: Option.some("desktop"),
785 port: Option.some(4888),
786 host: Option.none(),
787 baseDir: Option.some(baseDir),
788 cwd: Option.none(),
789 devUrl: Option.none(),
790 noBrowser: Option.none(),
791 bootstrapFd: Option.none(),
792 autoBootstrapProjectFromCwd: Option.none(),
793 logWebSocketEvents: Option.none(),
794 tailscaleServeEnabled: Option.none(),
795 tailscaleServePort: Option.none(),
796 },
797 Option.none(),
798 ).pipe(
799 Effect.provide(
800 Layer.mergeAll(
801 ConfigProvider.layer(
802 ConfigProvider.fromEnv({
803 env: { T3CODE_OTEL_SDK_DISABLED: "false", OTEL_SDK_DISABLED: "true" },
804 }),
805 ),
806 NetService.layer,
807 ),
808 ),
809 );
810
811 expect(resolved.otelEnvironment.disabled).toBe(false);
812 expect(resolved.otlpTracesUrl).toBe("http://localhost:4318/v1/traces");
813 }),
814 );
815
816 it.effect("forces noBrowser and disables auto-bootstrap for headless startup presentation", () =>
817 Effect.gen(function* () {
818 const { join } = yield* Path.Path;
819 const baseDir = join(NodeOS.tmpdir(), "t3-cli-config-headless-base");
820 const derivedPaths = yield* deriveExplicitServerPaths(baseDir, undefined);
821
822 const resolved = yield* resolveServerConfig(
823 {
824 mode: Option.some("web"),
825 port: Option.some(3773),
826 host: Option.none(),
827 baseDir: Option.some(baseDir),
828 cwd: Option.none(),
829 devUrl: Option.none(),
830 noBrowser: Option.none(),
831 bootstrapFd: Option.none(),
832 autoBootstrapProjectFromCwd: Option.none(),
833 logWebSocketEvents: Option.none(),
834 tailscaleServeEnabled: Option.none(),
835 tailscaleServePort: Option.none(),
836 },
837 Option.none(),
838 {
839 startupPresentation: "headless",
840 },
841 ).pipe(
842 Effect.provide(
843 Layer.mergeAll(
844 ConfigProvider.layer(
845 ConfigProvider.fromEnv({
846 env: {
847 T3CODE_NO_BROWSER: "false",
848 T3CODE_AUTO_BOOTSTRAP_PROJECT_FROM_CWD: "true",
849 },
850 }),
851 ),
852 NetService.layer,
853 ),
854 ),
855 );
856
857 expect(resolved).toEqual({
858 logLevel: "Info",
859 ...defaultObservabilityConfig,
860 mode: "web",
861 port: 3773,
862 cwd: process.cwd(),
863 baseDir,
864 ...derivedPaths,
865 host: undefined,
866 staticDir: resolved.staticDir,
867 devUrl: undefined,
868 noBrowser: true,
869 startupPresentation: "headless",
870 desktopBootstrapToken: undefined,
871 autoBootstrapProjectFromCwd: false,
872 logWebSocketEvents: false,
873 tailscaleServeEnabled: false,
874 tailscaleServePort: 443,
875 });
876 }),
877 );
878
879 it.effect("decodes percent-encoded OTLP headers from env", () =>
880 Effect.gen(function* () {
881 const { join } = yield* Path.Path;
882 const baseDir = join(NodeOS.tmpdir(), "t3-cli-config-otlp-headers-base");
883
884 const resolved = yield* resolveServerConfig(
885 {
886 mode: Option.some("web"),
887 port: Option.some(3773),
888 host: Option.none(),
889 baseDir: Option.some(baseDir),
890 cwd: Option.none(),
891 devUrl: Option.none(),
892 noBrowser: Option.none(),
893 bootstrapFd: Option.none(),
894 autoBootstrapProjectFromCwd: Option.none(),
895 logWebSocketEvents: Option.none(),
896 tailscaleServeEnabled: Option.none(),
897 tailscaleServePort: Option.none(),
898 },
899 Option.none(),
900 ).pipe(
901 Effect.provide(
902 Layer.mergeAll(
903 ConfigProvider.layer(
904 ConfigProvider.fromEnv({
905 env: {
906 T3CODE_OTLP_HEADERS: "authorization=Basic%20abc%3D%3D,x-tenant=t3",
907 },
908 }),
909 ),
910 NetService.layer,
911 ),
912 ),
913 );
914
915 expect(resolved.otlpTracesExport.headers).toEqual({
916 authorization: "Basic abc==",
917 "x-tenant": "t3",
918 });
919 }),
920 );
921
922 it.effect("keeps whitespace-separated pairs and literal equals signs in OTLP headers", () =>
923 Effect.gen(function* () {
924 const { join } = yield* Path.Path;
925 const baseDir = join(NodeOS.tmpdir(), "t3-cli-config-otlp-headers-loose-base");
926
927 const resolved = yield* resolveServerConfig(
928 {
929 mode: Option.some("web"),
930 port: Option.some(3773),
931 host: Option.none(),
932 baseDir: Option.some(baseDir),
933 cwd: Option.none(),
934 devUrl: Option.none(),
935 noBrowser: Option.none(),
936 bootstrapFd: Option.none(),
937 autoBootstrapProjectFromCwd: Option.none(),
938 logWebSocketEvents: Option.none(),
939 tailscaleServeEnabled: Option.none(),
940 tailscaleServePort: Option.none(),
941 },
942 Option.none(),
943 ).pipe(
944 Effect.provide(
945 Layer.mergeAll(
946 ConfigProvider.layer(
947 ConfigProvider.fromEnv({
948 env: {
949 T3CODE_OTLP_HEADERS: "authorization=Bearer abc==, x-tenant=t3",
950 T3CODE_OTLP_TRACES_URL: "http://collector.internal:4318",
951 },
952 }),
953 ),
954 NetService.layer,
955 ),
956 ),
957 );
958
959 expect(resolved.otlpTracesExport.headers).toEqual({
960 authorization: "Bearer abc==",
961 "x-tenant": "t3",
962 });
963 expect(resolved.otlpTracesUrl).toBe("http://collector.internal:4318");
964 }),
965 );
966
967 it.effect("gives every signal the protocol named without one", () =>
968 Effect.gen(function* () {
969 const { join } = yield* Path.Path;
970 const baseDir = join(NodeOS.tmpdir(), "t3-cli-config-otlp-protocol-base");
971
972 const resolved = yield* resolveServerConfig(
973 {
974 mode: Option.some("web"),
975 port: Option.some(3773),
976 host: Option.none(),
977 baseDir: Option.some(baseDir),
978 cwd: Option.none(),
979 devUrl: Option.none(),
980 noBrowser: Option.none(),
981 bootstrapFd: Option.none(),
982 autoBootstrapProjectFromCwd: Option.none(),
983 logWebSocketEvents: Option.none(),
984 tailscaleServeEnabled: Option.none(),
985 tailscaleServePort: Option.none(),
986 },
987 Option.none(),
988 ).pipe(
989 Effect.provide(
990 Layer.mergeAll(
991 ConfigProvider.layer(
992 ConfigProvider.fromEnv({ env: { T3CODE_OTLP_PROTOCOL: "http/protobuf" } }),
993 ),
994 NetService.layer,
995 ),
996 ),
997 );
998
999 expect([
1000 resolved.otlpTracesExport.protocol,
1001 resolved.otlpMetricsExport.protocol,
1002 resolved.otlpLogsExport.protocol,
1003 ]).toEqual(["http/protobuf", "http/protobuf", "http/protobuf"]);
1004 }),
1005 );
1006
1007 it.effect("reads the OTLP logs URL from env", () =>
1008 Effect.gen(function* () {
1009 const { join } = yield* Path.Path;
1010 const baseDir = join(NodeOS.tmpdir(), "t3-cli-config-otlp-logs-url-base");
1011
1012 const resolved = yield* resolveServerConfig(
1013 {
1014 mode: Option.some("web"),
1015 port: Option.some(3773),
1016 host: Option.none(),
1017 baseDir: Option.some(baseDir),
1018 cwd: Option.none(),
1019 devUrl: Option.none(),
1020 noBrowser: Option.none(),
1021 bootstrapFd: Option.none(),
1022 autoBootstrapProjectFromCwd: Option.none(),
1023 logWebSocketEvents: Option.none(),
1024 tailscaleServeEnabled: Option.none(),
1025 tailscaleServePort: Option.none(),
1026 },
1027 Option.none(),
1028 ).pipe(
1029 Effect.provide(
1030 Layer.mergeAll(
1031 ConfigProvider.layer(
1032 ConfigProvider.fromEnv({
1033 env: { T3CODE_OTLP_LOGS_URL: "http://collector.internal:4318/v1/logs" },
1034 }),
1035 ),
1036 NetService.layer,
1037 ),
1038 ),
1039 );
1040
1041 expect(resolved.otlpLogsUrl).toBe("http://collector.internal:4318/v1/logs");
1042 }),
1043 );
1044
1045 const minimalWebFlags = (baseDir: string) => ({
1046 mode: Option.some("web" as const),
1047 port: Option.some(3773),
1048 host: Option.none<string>(),
1049 baseDir: Option.some(baseDir),
1050 cwd: Option.none<string>(),
1051 devUrl: Option.none<URL>(),
1052 noBrowser: Option.none<boolean>(),
1053 bootstrapFd: Option.none<number>(),
1054 autoBootstrapProjectFromCwd: Option.none<boolean>(),
1055 logWebSocketEvents: Option.none<boolean>(),
1056 tailscaleServeEnabled: Option.none<boolean>(),
1057 tailscaleServePort: Option.none<number>(),
1058 });
1059
1060 it.effect(
1061 "resolves each signal's endpoint through T3CODE_OTLP_*_URL, an OTEL endpoint, the bootstrap envelope, and persisted Settings, in that order",
1062 () =>
1063 Effect.gen(function* () {
1064 const fs = yield* FileSystem.FileSystem;
1065 const path = yield* Path.Path;
1066 const baseDir = yield* fs.makeTempDirectoryScoped({
1067 prefix: "t3-cli-config-otel-precedence-",
1068 });
1069 const derivedPaths = yield* deriveExplicitServerPaths(baseDir, undefined);
1070 yield* fs.makeDirectory(path.dirname(derivedPaths.settingsPath), { recursive: true });
1071 yield* fs.writeFileString(
1072 derivedPaths.settingsPath,
1073 `${JSON.stringify({ observability: { otlpLogsUrl: "http://settings:4318/v1/logs" } })}\n`,
1074 );
1075
1076 const fd = yield* openBootstrapFd(
1077 makeDesktopBootstrap({
1078 otlpMetricsUrl: "http://bootstrap:4318/v1/metrics",
1079 // Blank, not an endpoint: it must not stand in front of Settings.
1080 otlpLogsUrl: "",
1081 }),
1082 );
1083
1084 const resolved = yield* resolveServerConfig(
1085 {
1086 ...minimalWebFlags(baseDir),
1087 mode: Option.some("desktop"),
1088 port: Option.some(4888),
1089 bootstrapFd: Option.some(fd),
1090 },
1091 Option.none(),
1092 ).pipe(
1093 Effect.provide(
1094 Layer.mergeAll(
1095 ConfigProvider.layer(
1096 ConfigProvider.fromEnv({
1097 env: {
1098 T3CODE_OTLP_TRACES_URL: "http://t3:4318/v1/traces",
1099 T3CODE_OTLP_HEADERS: "x-key=secret",
1100 OTEL_EXPORTER_OTLP_TRACES_ENDPOINT: "http://otel-traces:4318/custom",
1101 OTEL_EXPORTER_OTLP_METRICS_ENDPOINT: "http://otel-metrics:4318/custom",
1102 OTEL_EXPORTER_OTLP_HEADERS: "x-key=otel",
1103 },
1104 }),
1105 ),
1106 NetService.layer,
1107 ),
1108 ),
1109 );
1110
1111 // T3CODE_OTLP_TRACES_URL wins over the OTEL variable for the same
1112 // signal, and keeps T3 Code's own headers since T3 Code still owns it.
1113 expect(resolved.otlpTracesUrl).toBe("http://t3:4318/v1/traces");
1114 expect(resolved.otlpTracesExport.headers).toEqual({ "x-key": "secret" });
1115 // Metrics named no T3CODE_OTLP_METRICS_URL, so the OTEL endpoint wins
1116 // over the bootstrap envelope and brings the OTEL headers and protocol.
1117 expect(resolved.otlpMetricsUrl).toBe("http://otel-metrics:4318/custom");
1118 expect(resolved.otlpMetricsExport).toEqual({
1119 ...DEFAULT_SIGNAL_EXPORT,
1120 protocol: "http/protobuf",
1121 headers: { "x-key": "otel" },
1122 });
1123 // Logs named no T3 or OTEL endpoint and a blank bootstrap value, so
1124 // Settings answers, and logs keep the shared headers since no OTEL
1125 // endpoint claimed them.
1126 expect(resolved.otlpLogsUrl).toBe("http://settings:4318/v1/logs");
1127 expect(resolved.otlpLogsExport.headers).toEqual({ "x-key": "secret" });
1128 }),
1129 );
1130
1131 it.effect(
1132 "exports nothing for a signal an OTEL endpoint claimed with a protocol or headers that do not read",
1133 () =>
1134 Effect.gen(function* () {
1135 const fs = yield* FileSystem.FileSystem;
1136 const path = yield* Path.Path;
1137 const baseDir = yield* fs.makeTempDirectoryScoped({
1138 prefix: "t3-cli-config-otel-off-",
1139 });
1140 const derivedPaths = yield* deriveExplicitServerPaths(baseDir, undefined);
1141 yield* fs.makeDirectory(path.dirname(derivedPaths.settingsPath), { recursive: true });
1142 yield* fs.writeFileString(
1143 derivedPaths.settingsPath,
1144 `${JSON.stringify({ observability: { otlpLogsUrl: "http://settings:4318/v1/logs" } })}\n`,
1145 );
1146
1147 const fd = yield* openBootstrapFd(
1148 makeDesktopBootstrap({ otlpMetricsUrl: "http://bootstrap:4318/v1/metrics" }),
1149 );
1150
1151 const resolved = yield* resolveServerConfig(
1152 {
1153 ...minimalWebFlags(baseDir),
1154 mode: Option.some("desktop"),
1155 port: Option.some(4888),
1156 bootstrapFd: Option.some(fd),
1157 },
1158 Option.none(),
1159 ).pipe(
1160 Effect.provide(
1161 Layer.mergeAll(
1162 ConfigProvider.layer(
1163 ConfigProvider.fromEnv({
1164 env: {
1165 T3CODE_OTLP_TRACES_URL: "http://t3:4318/v1/traces",
1166 OTEL_EXPORTER_OTLP_ENDPOINT: "http://otel:4318",
1167 OTEL_EXPORTER_OTLP_HEADERS: "x-key=%zz",
1168 },
1169 }),
1170 ),
1171 NetService.layer,
1172 ),
1173 ),
1174 );
1175
1176 // T3CODE_OTLP_TRACES_URL still wins outright.
1177 expect(resolved.otlpTracesUrl).toBe("http://t3:4318/v1/traces");
1178 // The OTEL endpoint claimed metrics and logs, so neither the bootstrap
1179 // envelope nor Settings receives them with T3 Code's headers.
1180 expect(resolved.otlpMetricsUrl).toBeUndefined();
1181 expect(resolved.otlpLogsUrl).toBeUndefined();
1182 }),
1183 );
1184});