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