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", "state.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.serverLogPath),
512 path.dirname(resolved.serverTracePath),
513 ]) {
514 expect(yield* fs.exists(directory)).toBe(true);
515 }
516 expect(resolved.cwd).toBe(path.resolve(customCwd));
517 }),
518 );
519
520 it.effect("applies flag then env precedence over bootstrap envelope values", () =>
521 Effect.gen(function* () {
522 const { join } = yield* Path.Path;
523 const baseDir = join(NodeOS.tmpdir(), "t3-cli-config-env-wins");
524 const fd = yield* openBootstrapFd(
525 makeDesktopBootstrap({
526 port: 4888,
527 host: "127.0.0.2",
528 t3Home: "/tmp/t3-bootstrap-home",
529 noBrowser: false,
530 desktopBootstrapToken: "desktop-token",
531 tailscaleServeEnabled: false,
532 tailscaleServePort: 443,
533 }),
534 );
535 const derivedPaths = yield* deriveExplicitServerPaths(
536 baseDir,
537 new URL("http://127.0.0.1:4173"),
538 );
539
540 const resolved = yield* resolveServerConfig(
541 {
542 mode: Option.none(),
543 port: Option.some(8788),
544 host: Option.some("127.0.0.1"),
545 baseDir: Option.none(),
546 cwd: Option.none(),
547 devUrl: Option.some(new URL("http://127.0.0.1:4173")),
548 noBrowser: Option.none(),
549 bootstrapFd: Option.none(),
550 autoBootstrapProjectFromCwd: Option.none(),
551 logWebSocketEvents: Option.none(),
552 tailscaleServeEnabled: Option.none(),
553 tailscaleServePort: Option.none(),
554 },
555 Option.some("Debug"),
556 ).pipe(
557 Effect.provide(
558 Layer.mergeAll(
559 ConfigProvider.layer(
560 ConfigProvider.fromEnv({
561 env: {
562 T3CODE_MODE: "web",
563 T3CODE_BOOTSTRAP_FD: String(fd),
564 T3CODE_HOME: baseDir,
565 T3CODE_NO_BROWSER: "true",
566 T3CODE_AUTO_BOOTSTRAP_PROJECT_FROM_CWD: "true",
567 T3CODE_LOG_WS_EVENTS: "true",
568 },
569 }),
570 ),
571 NetService.layer,
572 ),
573 ),
574 );
575
576 expect(resolved).toEqual({
577 logLevel: "Debug",
578 ...defaultObservabilityConfig,
579 mode: "web",
580 port: 8788,
581 cwd: process.cwd(),
582 baseDir,
583 ...derivedPaths,
584 host: "127.0.0.1",
585 staticDir: undefined,
586 devUrl: new URL("http://127.0.0.1:4173"),
587 noBrowser: true,
588 startupPresentation: "browser",
589 desktopBootstrapToken: "desktop-token",
590 autoBootstrapProjectFromCwd: true,
591 logWebSocketEvents: true,
592 tailscaleServeEnabled: false,
593 tailscaleServePort: 443,
594 });
595 }),
596 );
597
598 it.effect("falls back to persisted observability settings when env vars are absent", () =>
599 Effect.gen(function* () {
600 const fs = yield* FileSystem.FileSystem;
601 const path = yield* Path.Path;
602 const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-cli-config-settings-" });
603 const derivedPaths = yield* deriveExplicitServerPaths(baseDir, undefined);
604 yield* fs.makeDirectory(path.dirname(derivedPaths.settingsPath), { recursive: true });
605 yield* fs.writeFileString(
606 derivedPaths.settingsPath,
607 // @effect-diagnostics-next-line preferSchemaOverJson:off
608 `${JSON.stringify({
609 observability: {
610 otlpTracesUrl: "http://localhost:4318/v1/traces",
611 otlpMetricsUrl: "http://localhost:4318/v1/metrics",
612 otlpLogsUrl: "http://localhost:4318/v1/logs",
613 },
614 })}\n`,
615 );
616
617 const resolved = yield* resolveServerConfig(
618 {
619 mode: Option.some("desktop"),
620 port: Option.some(4888),
621 host: Option.none(),
622 baseDir: Option.some(baseDir),
623 cwd: Option.none(),
624 devUrl: Option.none(),
625 noBrowser: Option.none(),
626 bootstrapFd: Option.none(),
627 autoBootstrapProjectFromCwd: Option.none(),
628 logWebSocketEvents: Option.none(),
629 tailscaleServeEnabled: Option.none(),
630 tailscaleServePort: Option.none(),
631 },
632 Option.none(),
633 ).pipe(
634 Effect.provide(
635 Layer.mergeAll(
636 ConfigProvider.layer(ConfigProvider.fromEnv({ env: {} })),
637 NetService.layer,
638 ),
639 ),
640 );
641
642 expect(resolved.otlpTracesUrl).toBe("http://localhost:4318/v1/traces");
643 expect(resolved.otlpMetricsUrl).toBe("http://localhost:4318/v1/metrics");
644 expect(resolved.otlpLogsUrl).toBe("http://localhost:4318/v1/logs");
645 expect(resolved).toEqual({
646 logLevel: "Info",
647 ...defaultObservabilityConfig,
648 otlpTracesUrl: "http://localhost:4318/v1/traces",
649 otlpMetricsUrl: "http://localhost:4318/v1/metrics",
650 otlpLogsUrl: "http://localhost:4318/v1/logs",
651 mode: "desktop",
652 port: 4888,
653 cwd: process.cwd(),
654 baseDir,
655 ...derivedPaths,
656 host: "127.0.0.1",
657 staticDir: resolved.staticDir,
658 devUrl: undefined,
659 noBrowser: true,
660 startupPresentation: "browser",
661 desktopBootstrapToken: undefined,
662 autoBootstrapProjectFromCwd: false,
663 logWebSocketEvents: false,
664 tailscaleServeEnabled: false,
665 tailscaleServePort: 443,
666 });
667 }),
668 );
669
670 it.effect("zeroes an endpoint stored in Settings when the SDK is disabled", () =>
671 Effect.gen(function* () {
672 const fs = yield* FileSystem.FileSystem;
673 const path = yield* Path.Path;
674 const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-cli-config-otel-off-" });
675 const derivedPaths = yield* deriveExplicitServerPaths(baseDir, undefined);
676 yield* fs.makeDirectory(path.dirname(derivedPaths.settingsPath), { recursive: true });
677 yield* fs.writeFileString(
678 derivedPaths.settingsPath,
679 // @effect-diagnostics-next-line preferSchemaOverJson:off
680 `${JSON.stringify({
681 observability: {
682 otlpTracesUrl: "http://localhost:4318/v1/traces",
683 otlpMetricsUrl: "http://localhost:4318/v1/metrics",
684 otlpLogsUrl: "http://localhost:4318/v1/logs",
685 },
686 })}\n`,
687 );
688
689 const resolved = yield* resolveServerConfig(
690 {
691 mode: Option.some("desktop"),
692 port: Option.some(4888),
693 host: Option.none(),
694 baseDir: Option.some(baseDir),
695 cwd: Option.none(),
696 devUrl: Option.none(),
697 noBrowser: Option.none(),
698 bootstrapFd: Option.none(),
699 autoBootstrapProjectFromCwd: Option.none(),
700 logWebSocketEvents: Option.none(),
701 tailscaleServeEnabled: Option.none(),
702 tailscaleServePort: Option.none(),
703 },
704 Option.none(),
705 ).pipe(
706 Effect.provide(
707 Layer.mergeAll(
708 ConfigProvider.layer(ConfigProvider.fromEnv({ env: { OTEL_SDK_DISABLED: "true" } })),
709 NetService.layer,
710 ),
711 ),
712 );
713
714 // The switch beats every source, including an endpoint stored in Settings.
715 expect(resolved.otlpTracesUrl).toBeUndefined();
716 expect(resolved.otlpMetricsUrl).toBeUndefined();
717 expect(resolved.otlpLogsUrl).toBeUndefined();
718 expect(resolved.otelEnvironment.disabled).toBe(true);
719 }),
720 );
721
722 it.effect("lets T3CODE_OTEL_SDK_DISABLED=false override an ambient OTEL_SDK_DISABLED=true", () =>
723 Effect.gen(function* () {
724 const fs = yield* FileSystem.FileSystem;
725 const path = yield* Path.Path;
726 const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-cli-config-otel-on-" });
727 const derivedPaths = yield* deriveExplicitServerPaths(baseDir, undefined);
728 yield* fs.makeDirectory(path.dirname(derivedPaths.settingsPath), { recursive: true });
729 yield* fs.writeFileString(
730 derivedPaths.settingsPath,
731 // @effect-diagnostics-next-line preferSchemaOverJson:off
732 `${JSON.stringify({
733 observability: {
734 otlpTracesUrl: "http://localhost:4318/v1/traces",
735 },
736 })}\n`,
737 );
738
739 const resolved = yield* resolveServerConfig(
740 {
741 mode: Option.some("desktop"),
742 port: Option.some(4888),
743 host: Option.none(),
744 baseDir: Option.some(baseDir),
745 cwd: Option.none(),
746 devUrl: Option.none(),
747 noBrowser: Option.none(),
748 bootstrapFd: Option.none(),
749 autoBootstrapProjectFromCwd: Option.none(),
750 logWebSocketEvents: Option.none(),
751 tailscaleServeEnabled: Option.none(),
752 tailscaleServePort: Option.none(),
753 },
754 Option.none(),
755 ).pipe(
756 Effect.provide(
757 Layer.mergeAll(
758 ConfigProvider.layer(
759 ConfigProvider.fromEnv({
760 env: { T3CODE_OTEL_SDK_DISABLED: "false", OTEL_SDK_DISABLED: "true" },
761 }),
762 ),
763 NetService.layer,
764 ),
765 ),
766 );
767
768 expect(resolved.otelEnvironment.disabled).toBe(false);
769 expect(resolved.otlpTracesUrl).toBe("http://localhost:4318/v1/traces");
770 }),
771 );
772
773 it.effect("forces noBrowser and disables auto-bootstrap for headless startup presentation", () =>
774 Effect.gen(function* () {
775 const { join } = yield* Path.Path;
776 const baseDir = join(NodeOS.tmpdir(), "t3-cli-config-headless-base");
777 const derivedPaths = yield* deriveExplicitServerPaths(baseDir, undefined);
778
779 const resolved = yield* resolveServerConfig(
780 {
781 mode: Option.some("web"),
782 port: Option.some(3773),
783 host: Option.none(),
784 baseDir: Option.some(baseDir),
785 cwd: Option.none(),
786 devUrl: Option.none(),
787 noBrowser: Option.none(),
788 bootstrapFd: Option.none(),
789 autoBootstrapProjectFromCwd: Option.none(),
790 logWebSocketEvents: Option.none(),
791 tailscaleServeEnabled: Option.none(),
792 tailscaleServePort: Option.none(),
793 },
794 Option.none(),
795 {
796 startupPresentation: "headless",
797 },
798 ).pipe(
799 Effect.provide(
800 Layer.mergeAll(
801 ConfigProvider.layer(
802 ConfigProvider.fromEnv({
803 env: {
804 T3CODE_NO_BROWSER: "false",
805 T3CODE_AUTO_BOOTSTRAP_PROJECT_FROM_CWD: "true",
806 },
807 }),
808 ),
809 NetService.layer,
810 ),
811 ),
812 );
813
814 expect(resolved).toEqual({
815 logLevel: "Info",
816 ...defaultObservabilityConfig,
817 mode: "web",
818 port: 3773,
819 cwd: process.cwd(),
820 baseDir,
821 ...derivedPaths,
822 host: undefined,
823 staticDir: resolved.staticDir,
824 devUrl: undefined,
825 noBrowser: true,
826 startupPresentation: "headless",
827 desktopBootstrapToken: undefined,
828 autoBootstrapProjectFromCwd: false,
829 logWebSocketEvents: false,
830 tailscaleServeEnabled: false,
831 tailscaleServePort: 443,
832 });
833 }),
834 );
835
836 it.effect("decodes percent-encoded OTLP headers from env", () =>
837 Effect.gen(function* () {
838 const { join } = yield* Path.Path;
839 const baseDir = join(NodeOS.tmpdir(), "t3-cli-config-otlp-headers-base");
840
841 const resolved = yield* resolveServerConfig(
842 {
843 mode: Option.some("web"),
844 port: Option.some(3773),
845 host: Option.none(),
846 baseDir: Option.some(baseDir),
847 cwd: Option.none(),
848 devUrl: Option.none(),
849 noBrowser: Option.none(),
850 bootstrapFd: Option.none(),
851 autoBootstrapProjectFromCwd: Option.none(),
852 logWebSocketEvents: Option.none(),
853 tailscaleServeEnabled: Option.none(),
854 tailscaleServePort: Option.none(),
855 },
856 Option.none(),
857 ).pipe(
858 Effect.provide(
859 Layer.mergeAll(
860 ConfigProvider.layer(
861 ConfigProvider.fromEnv({
862 env: {
863 T3CODE_OTLP_HEADERS: "authorization=Basic%20abc%3D%3D,x-tenant=t3",
864 },
865 }),
866 ),
867 NetService.layer,
868 ),
869 ),
870 );
871
872 expect(resolved.otlpTracesExport.headers).toEqual({
873 authorization: "Basic abc==",
874 "x-tenant": "t3",
875 });
876 }),
877 );
878
879 it.effect("keeps whitespace-separated pairs and literal equals signs in OTLP headers", () =>
880 Effect.gen(function* () {
881 const { join } = yield* Path.Path;
882 const baseDir = join(NodeOS.tmpdir(), "t3-cli-config-otlp-headers-loose-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=Bearer abc==, x-tenant=t3",
907 T3CODE_OTLP_TRACES_URL: "http://collector.internal:4318",
908 },
909 }),
910 ),
911 NetService.layer,
912 ),
913 ),
914 );
915
916 expect(resolved.otlpTracesExport.headers).toEqual({
917 authorization: "Bearer abc==",
918 "x-tenant": "t3",
919 });
920 expect(resolved.otlpTracesUrl).toBe("http://collector.internal:4318");
921 }),
922 );
923
924 it.effect("gives every signal the protocol named without one", () =>
925 Effect.gen(function* () {
926 const { join } = yield* Path.Path;
927 const baseDir = join(NodeOS.tmpdir(), "t3-cli-config-otlp-protocol-base");
928
929 const resolved = yield* resolveServerConfig(
930 {
931 mode: Option.some("web"),
932 port: Option.some(3773),
933 host: Option.none(),
934 baseDir: Option.some(baseDir),
935 cwd: Option.none(),
936 devUrl: Option.none(),
937 noBrowser: Option.none(),
938 bootstrapFd: Option.none(),
939 autoBootstrapProjectFromCwd: Option.none(),
940 logWebSocketEvents: Option.none(),
941 tailscaleServeEnabled: Option.none(),
942 tailscaleServePort: Option.none(),
943 },
944 Option.none(),
945 ).pipe(
946 Effect.provide(
947 Layer.mergeAll(
948 ConfigProvider.layer(
949 ConfigProvider.fromEnv({ env: { T3CODE_OTLP_PROTOCOL: "http/protobuf" } }),
950 ),
951 NetService.layer,
952 ),
953 ),
954 );
955
956 expect([
957 resolved.otlpTracesExport.protocol,
958 resolved.otlpMetricsExport.protocol,
959 resolved.otlpLogsExport.protocol,
960 ]).toEqual(["http/protobuf", "http/protobuf", "http/protobuf"]);
961 }),
962 );
963
964 it.effect("reads the OTLP logs URL from env", () =>
965 Effect.gen(function* () {
966 const { join } = yield* Path.Path;
967 const baseDir = join(NodeOS.tmpdir(), "t3-cli-config-otlp-logs-url-base");
968
969 const resolved = yield* resolveServerConfig(
970 {
971 mode: Option.some("web"),
972 port: Option.some(3773),
973 host: Option.none(),
974 baseDir: Option.some(baseDir),
975 cwd: Option.none(),
976 devUrl: Option.none(),
977 noBrowser: Option.none(),
978 bootstrapFd: Option.none(),
979 autoBootstrapProjectFromCwd: Option.none(),
980 logWebSocketEvents: Option.none(),
981 tailscaleServeEnabled: Option.none(),
982 tailscaleServePort: Option.none(),
983 },
984 Option.none(),
985 ).pipe(
986 Effect.provide(
987 Layer.mergeAll(
988 ConfigProvider.layer(
989 ConfigProvider.fromEnv({
990 env: { T3CODE_OTLP_LOGS_URL: "http://collector.internal:4318/v1/logs" },
991 }),
992 ),
993 NetService.layer,
994 ),
995 ),
996 );
997
998 expect(resolved.otlpLogsUrl).toBe("http://collector.internal:4318/v1/logs");
999 }),
1000 );
1001
1002 const minimalWebFlags = (baseDir: string) => ({
1003 mode: Option.some("web" as const),
1004 port: Option.some(3773),
1005 host: Option.none<string>(),
1006 baseDir: Option.some(baseDir),
1007 cwd: Option.none<string>(),
1008 devUrl: Option.none<URL>(),
1009 noBrowser: Option.none<boolean>(),
1010 bootstrapFd: Option.none<number>(),
1011 autoBootstrapProjectFromCwd: Option.none<boolean>(),
1012 logWebSocketEvents: Option.none<boolean>(),
1013 tailscaleServeEnabled: Option.none<boolean>(),
1014 tailscaleServePort: Option.none<number>(),
1015 });
1016
1017 it.effect(
1018 "resolves each signal's endpoint through T3CODE_OTLP_*_URL, an OTEL endpoint, the bootstrap envelope, and persisted Settings, in that order",
1019 () =>
1020 Effect.gen(function* () {
1021 const fs = yield* FileSystem.FileSystem;
1022 const path = yield* Path.Path;
1023 const baseDir = yield* fs.makeTempDirectoryScoped({
1024 prefix: "t3-cli-config-otel-precedence-",
1025 });
1026 const derivedPaths = yield* deriveExplicitServerPaths(baseDir, undefined);
1027 yield* fs.makeDirectory(path.dirname(derivedPaths.settingsPath), { recursive: true });
1028 yield* fs.writeFileString(
1029 derivedPaths.settingsPath,
1030 // @effect-diagnostics-next-line preferSchemaOverJson:off
1031 `${JSON.stringify({ observability: { otlpLogsUrl: "http://settings:4318/v1/logs" } })}\n`,
1032 );
1033
1034 const fd = yield* openBootstrapFd(
1035 makeDesktopBootstrap({
1036 otlpMetricsUrl: "http://bootstrap:4318/v1/metrics",
1037 // Blank, not an endpoint: it must not stand in front of Settings.
1038 otlpLogsUrl: "",
1039 }),
1040 );
1041
1042 const resolved = yield* resolveServerConfig(
1043 {
1044 ...minimalWebFlags(baseDir),
1045 mode: Option.some("desktop"),
1046 port: Option.some(4888),
1047 bootstrapFd: Option.some(fd),
1048 },
1049 Option.none(),
1050 ).pipe(
1051 Effect.provide(
1052 Layer.mergeAll(
1053 ConfigProvider.layer(
1054 ConfigProvider.fromEnv({
1055 env: {
1056 T3CODE_OTLP_TRACES_URL: "http://t3:4318/v1/traces",
1057 T3CODE_OTLP_HEADERS: "x-key=secret",
1058 OTEL_EXPORTER_OTLP_TRACES_ENDPOINT: "http://otel-traces:4318/custom",
1059 OTEL_EXPORTER_OTLP_METRICS_ENDPOINT: "http://otel-metrics:4318/custom",
1060 OTEL_EXPORTER_OTLP_HEADERS: "x-key=otel",
1061 },
1062 }),
1063 ),
1064 NetService.layer,
1065 ),
1066 ),
1067 );
1068
1069 // T3CODE_OTLP_TRACES_URL wins over the OTEL variable for the same
1070 // signal, and keeps T3 Code's own headers since T3 Code still owns it.
1071 expect(resolved.otlpTracesUrl).toBe("http://t3:4318/v1/traces");
1072 expect(resolved.otlpTracesExport.headers).toEqual({ "x-key": "secret" });
1073 // Metrics named no T3CODE_OTLP_METRICS_URL, so the OTEL endpoint wins
1074 // over the bootstrap envelope and brings the OTEL headers and protocol.
1075 expect(resolved.otlpMetricsUrl).toBe("http://otel-metrics:4318/custom");
1076 expect(resolved.otlpMetricsExport).toEqual({
1077 ...DEFAULT_SIGNAL_EXPORT,
1078 protocol: "http/protobuf",
1079 headers: { "x-key": "otel" },
1080 });
1081 // Logs named no T3 or OTEL endpoint and a blank bootstrap value, so
1082 // Settings answers, and logs keep the shared headers since no OTEL
1083 // endpoint claimed them.
1084 expect(resolved.otlpLogsUrl).toBe("http://settings:4318/v1/logs");
1085 expect(resolved.otlpLogsExport.headers).toEqual({ "x-key": "secret" });
1086 }),
1087 );
1088
1089 it.effect(
1090 "exports nothing for a signal an OTEL endpoint claimed with a protocol or headers that do not read",
1091 () =>
1092 Effect.gen(function* () {
1093 const fs = yield* FileSystem.FileSystem;
1094 const path = yield* Path.Path;
1095 const baseDir = yield* fs.makeTempDirectoryScoped({
1096 prefix: "t3-cli-config-otel-off-",
1097 });
1098 const derivedPaths = yield* deriveExplicitServerPaths(baseDir, undefined);
1099 yield* fs.makeDirectory(path.dirname(derivedPaths.settingsPath), { recursive: true });
1100 yield* fs.writeFileString(
1101 derivedPaths.settingsPath,
1102 // @effect-diagnostics-next-line preferSchemaOverJson:off
1103 `${JSON.stringify({ observability: { otlpLogsUrl: "http://settings:4318/v1/logs" } })}\n`,
1104 );
1105
1106 const fd = yield* openBootstrapFd(
1107 makeDesktopBootstrap({ otlpMetricsUrl: "http://bootstrap:4318/v1/metrics" }),
1108 );
1109
1110 const resolved = yield* resolveServerConfig(
1111 {
1112 ...minimalWebFlags(baseDir),
1113 mode: Option.some("desktop"),
1114 port: Option.some(4888),
1115 bootstrapFd: Option.some(fd),
1116 },
1117 Option.none(),
1118 ).pipe(
1119 Effect.provide(
1120 Layer.mergeAll(
1121 ConfigProvider.layer(
1122 ConfigProvider.fromEnv({
1123 env: {
1124 T3CODE_OTLP_TRACES_URL: "http://t3:4318/v1/traces",
1125 OTEL_EXPORTER_OTLP_ENDPOINT: "http://otel:4318",
1126 OTEL_EXPORTER_OTLP_HEADERS: "x-key=%zz",
1127 },
1128 }),
1129 ),
1130 NetService.layer,
1131 ),
1132 ),
1133 );
1134
1135 // T3CODE_OTLP_TRACES_URL still wins outright.
1136 expect(resolved.otlpTracesUrl).toBe("http://t3:4318/v1/traces");
1137 // The OTEL endpoint claimed metrics and logs, so neither the bootstrap
1138 // envelope nor Settings receives them with T3 Code's headers.
1139 expect(resolved.otlpMetricsUrl).toBeUndefined();
1140 expect(resolved.otlpLogsUrl).toBeUndefined();
1141 }),
1142 );
1143});