Open History to see the last commit
1// @effect-diagnostics nodeBuiltinImport:off
2import * as NodeChildProcess from "node:child_process";
3import * as NodeEvents from "node:events";
4import * as NodeFS from "node:fs";
5import * as NodeOS from "node:os";
6
7import { assert, expect, it } from "@effect/vitest";
8import * as ConfigProvider from "effect/ConfigProvider";
9import * as Effect from "effect/Effect";
10import * as FileSystem from "effect/FileSystem";
11import * as Layer from "effect/Layer";
12import * as Option from "effect/Option";
13import * as Path from "effect/Path";
14import * as Redacted from "effect/Redacted";
15import * as Result from "effect/Result";
16import * as Schema from "effect/Schema";
17
18import {
19 DesktopBackendBootstrap,
20 type DesktopBackendBootstrap as DesktopBackendBootstrapValue,
21} from "@t3tools/contracts";
22import * as NetService from "@t3tools/shared/Net";
23import { DEFAULT_SIGNAL_EXPORT } from "@t3tools/shared/observability";
24import * as OtelEnvironment from "@t3tools/shared/otelEnvironment";
25import * as NodeServices from "@effect/platform-node/NodeServices";
26import { deriveServerPaths } from "../config.ts";
27import { resolveServerConfig } from "./config.ts";
28
29const deriveExplicitServerPaths = (baseDir: string, devUrl: URL | undefined) =>
30 deriveServerPaths(baseDir, devUrl, { baseDirIsExplicit: true });
31
32const encodeDesktopBootstrap = Schema.encodeEffect(Schema.fromJsonString(DesktopBackendBootstrap));
33const encodeUnknownJson = Schema.encodeEffect(Schema.fromJsonString(Schema.Unknown));
34
35const makeDesktopBootstrap = (
36 overrides: Partial<DesktopBackendBootstrapValue> = {},
37): DesktopBackendBootstrapValue => ({
38 mode: "desktop",
39 noBrowser: true,
40 port: 4888,
41 t3Home: "/tmp/t3-bootstrap-home",
42 host: "127.0.0.1",
43 desktopBootstrapToken: "desktop-bootstrap-token",
44 tailscaleServeEnabled: false,
45 tailscaleServePort: 443,
46 ...overrides,
47});
48
49const liveProcess = Effect.acquireRelease(
50 Effect.promise(async () => {
51 const child = NodeChildProcess.spawn(process.execPath, ["-e", "process.stdin.resume()"], {
52 stdio: ["pipe", "ignore", "ignore"],
53 });
54 await NodeEvents.EventEmitter.once(child, "spawn");
55 return child;
56 }),
57 (child) =>
58 Effect.promise(async () => {
59 const exited = NodeEvents.EventEmitter.once(child, "exit");
60 child.kill();
61 await exited;
62 }),
63);
64
65it.layer(NodeServices.layer)("cli config resolution", (it) => {
66 const defaultObservabilityConfig = {
67 traceMinLevel: "Info",
68 traceTimingEnabled: true,
69 traceBatchWindowMs: 1_000,
70 traceMaxBytes: 10 * 1024 * 1024,
71 traceMaxFiles: 10,
72 otlpTracesUrl: undefined,
73 otlpMetricsUrl: undefined,
74 otlpLogsUrl: undefined,
75 otlpTracesExport: DEFAULT_SIGNAL_EXPORT,
76 otlpMetricsExport: DEFAULT_SIGNAL_EXPORT,
77 otlpLogsExport: DEFAULT_SIGNAL_EXPORT,
78 otelEnvironment: OtelEnvironment.none,
79 devAllowedOrigins: [],
80 } as const;
81
82 const openBootstrapFd = Effect.fn(function* (payload: DesktopBackendBootstrapValue) {
83 const fs = yield* FileSystem.FileSystem;
84 const filePath = yield* fs.makeTempFileScoped({ prefix: "t3-bootstrap-", suffix: ".ndjson" });
85 const encoded = yield* encodeDesktopBootstrap(payload);
86 yield* fs.writeFileString(filePath, `${encoded}\n`);
87 return yield* Effect.acquireRelease(
88 Effect.sync(() => NodeFS.openSync(filePath, "r")),
89 // Without a /proc or /dev/fd path to reopen, the reader consumes the fd
90 // itself (autoClose), so on Windows it is already closed here.
91 (fd) =>
92 Effect.sync(() => {
93 try {
94 NodeFS.closeSync(fd);
95 } catch (error) {
96 if ((error as NodeJS.ErrnoException).code !== "EBADF") throw error;
97 }
98 }),
99 );
100 });
101
102 it.effect("keeps stale records and supervised startup out of the manual launch preflight", () =>
103 Effect.gen(function* () {
104 const fs = yield* FileSystem.FileSystem;
105 const path = yield* Path.Path;
106 const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-cli-preflight-" });
107 const child = yield* liveProcess;
108 assert(child.pid !== undefined);
109 for (const [name, pid, mode, rejectRunningServer] of [
110 ["stale", 2_147_483_647, "web", true],
111 ["own-pid", process.pid, "web", true],
112 ["desktop", child.pid, "desktop", true],
113 ["serve", child.pid, "web", false],
114 ] as const) {
115 const baseDir = path.join(root, name);
116 const stateDir = path.join(baseDir, "userdata");
117 yield* fs.makeDirectory(stateDir, { recursive: true });
118 yield* fs.writeFileString(
119 path.join(stateDir, "server-runtime.json"),
120 yield* encodeUnknownJson({
121 version: 1,
122 pid,
123 port: 3773,
124 origin: "http://127.0.0.1:3773",
125 startedAt: "2026-10-01T00:00:00.000Z",
126 }),
127 );
128 const cwd = path.join(root, `${name}-project`);
129 const configResult = yield* resolveServerConfig(
130 {
131 ...minimalWebFlags(baseDir),
132 mode: Option.some(mode),
133 port: Option.some(8788),
134 cwd: Option.some(cwd),
135 },
136 Option.none(),
137 { rejectRunningServer },
138 ).pipe(
139 Effect.provide(
140 Layer.merge(
141 NetService.layer,
142 ConfigProvider.layer(ConfigProvider.fromEnv({ env: {} })),
143 ),
144 ),
145 Effect.result,
146 );
147 assert(Result.isSuccess(configResult));
148 expect(configResult.success.cwd).toBe(cwd);
149 expect(yield* fs.exists(cwd)).toBe(true);
150 }
151 }),
152 );
153
154 it.effect(
155 "refuses a manual web launch when a runtime record belongs to another live process",
156 () =>
157 Effect.gen(function* () {
158 const fs = yield* FileSystem.FileSystem;
159 const path = yield* Path.Path;
160 const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-cli-preflight-live-" });
161 const child = yield* liveProcess;
162 assert(child.pid !== undefined);
163 const stateDir = path.join(baseDir, "userdata");
164 yield* fs.makeDirectory(stateDir, { recursive: true });
165 yield* fs.writeFileString(
166 path.join(stateDir, "server-runtime.json"),
167 yield* encodeUnknownJson({
168 version: 1,
169 pid: child.pid,
170 port: 3773,
171 origin: "http://127.0.0.1:3773",
172 startedAt: "2026-10-01T00:00:00.000Z",
173 }),
174 );
175 const cwd = path.join(baseDir, "project");
176 const error = yield* resolveServerConfig(
177 { ...minimalWebFlags(baseDir), cwd: Option.some(cwd) },
178 Option.none(),
179 { rejectRunningServer: true },
180 ).pipe(
181 Effect.provide(
182 Layer.merge(
183 NetService.layer,
184 ConfigProvider.layer(ConfigProvider.fromEnv({ env: {} })),
185 ),
186 ),
187 Effect.flip,
188 );
189 expect(String(error)).toContain("A T3 Code server is already running");
190 expect(String(error)).toContain(`pid ${child.pid}`);
191 expect(yield* fs.exists(cwd)).toBe(false);
192 }),
193 );
194
195 it.effect("enables a trimmed reusable auth token only for web dev mode", () =>
196 Effect.gen(function* () {
197 const baseDir = yield* FileSystem.FileSystem.pipe(
198 Effect.flatMap((fs) => fs.makeTempDirectoryScoped({ prefix: "t3-cli-dev-auth-" })),
199 );
200 const flags = {
201 mode: Option.some("web" as const),
202 port: Option.some(8788),
203 host: Option.none<string>(),
204 baseDir: Option.some(baseDir),
205 cwd: Option.none<string>(),
206 devUrl: Option.some(new URL("http://127.0.0.1:5173")),
207 noBrowser: Option.none<boolean>(),
208 bootstrapFd: Option.none<number>(),
209 autoBootstrapProjectFromCwd: Option.none<boolean>(),
210 logWebSocketEvents: Option.none<boolean>(),
211 tailscaleServeEnabled: Option.none<boolean>(),
212 tailscaleServePort: Option.none<number>(),
213 };
214 const layerConfig = ConfigProvider.layer(
215 ConfigProvider.fromEnv({
216 env: {
217 T3CODE_DEV_AUTH_TOKEN: " reusable-dev-auth-token-that-is-long-enough ",
218 },
219 }),
220 );
221 const web = yield* resolveServerConfig(flags, Option.none()).pipe(
222 Effect.provide(Layer.mergeAll(layerConfig, NetService.layer)),
223 );
224 const desktop = yield* resolveServerConfig(
225 { ...flags, mode: Option.some("desktop" as const) },
226 Option.none(),
227 ).pipe(Effect.provide(Layer.mergeAll(layerConfig, NetService.layer)));
228
229 expect(web.devAuthToken).toBeDefined();
230 if (web.devAuthToken === undefined) {
231 return yield* Effect.die("Expected reusable dev auth token.");
232 }
233 expect(Redacted.value(web.devAuthToken)).toBe("reusable-dev-auth-token-that-is-long-enough");
234 expect(desktop.devAuthToken).toBeUndefined();
235 }),
236 );
237
238 it.effect("does not expose an invalid reusable auth token", () =>
239 Effect.gen(function* () {
240 const secret = "short-secret";
241 const baseDir = yield* FileSystem.FileSystem.pipe(
242 Effect.flatMap((fs) => fs.makeTempDirectoryScoped({ prefix: "t3-cli-dev-auth-invalid-" })),
243 );
244 const flags = {
245 mode: Option.some("web" as const),
246 port: Option.some(8788),
247 host: Option.none<string>(),
248 baseDir: Option.some(baseDir),
249 cwd: Option.none<string>(),
250 devUrl: Option.some(new URL("http://127.0.0.1:5173")),
251 noBrowser: Option.none<boolean>(),
252 bootstrapFd: Option.none<number>(),
253 autoBootstrapProjectFromCwd: Option.none<boolean>(),
254 logWebSocketEvents: Option.none<boolean>(),
255 tailscaleServeEnabled: Option.none<boolean>(),
256 tailscaleServePort: Option.none<number>(),
257 };
258 const layerConfig = ConfigProvider.layer(
259 ConfigProvider.fromEnv({ env: { T3CODE_DEV_AUTH_TOKEN: secret } }),
260 );
261 const error = yield* resolveServerConfig(flags, Option.none()).pipe(
262 Effect.provide(Layer.mergeAll(layerConfig, NetService.layer)),
263 Effect.flip,
264 );
265 const desktop = yield* resolveServerConfig(
266 { ...flags, mode: Option.some("desktop" as const) },
267 Option.none(),
268 ).pipe(Effect.provide(Layer.mergeAll(layerConfig, NetService.layer)));
269 const staticWeb = yield* resolveServerConfig(
270 { ...flags, devUrl: Option.none() },
271 Option.none(),
272 ).pipe(Effect.provide(Layer.mergeAll(layerConfig, NetService.layer)));
273
274 expect(String(error)).not.toContain(secret);
275 const serialized = yield* encodeUnknownJson(error);
276 expect(serialized).not.toContain(secret);
277 expect(desktop.devAuthToken).toBeUndefined();
278 expect(staticWeb.devAuthToken).toBeUndefined();
279 }),
280 );
281
282 it.effect("preserves explicit false CLI boolean flags over env and bootstrap values", () =>
283 Effect.gen(function* () {
284 const { join } = yield* Path.Path;
285 const baseDir = join(NodeOS.tmpdir(), "t3-cli-config-false-flags");
286 const fd = yield* openBootstrapFd(
287 makeDesktopBootstrap({
288 noBrowser: true,
289 tailscaleServeEnabled: false,
290 tailscaleServePort: 443,
291 }),
292 );
293 const derivedPaths = yield* deriveExplicitServerPaths(
294 baseDir,
295 new URL("http://127.0.0.1:4173"),
296 );
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(false),
307 bootstrapFd: Option.none(),
308 autoBootstrapProjectFromCwd: Option.some(false),
309 logWebSocketEvents: Option.some(false),
310 tailscaleServeEnabled: Option.none(),
311 tailscaleServePort: Option.none(),
312 },
313 Option.none(),
314 ).pipe(
315 Effect.provide(
316 Layer.mergeAll(
317 ConfigProvider.layer(
318 ConfigProvider.fromEnv({
319 env: {
320 T3CODE_BOOTSTRAP_FD: String(fd),
321 T3CODE_NO_BROWSER: "true",
322 T3CODE_AUTO_BOOTSTRAP_PROJECT_FROM_CWD: "true",
323 T3CODE_LOG_WS_EVENTS: "true",
324 },
325 }),
326 ),
327 NetService.layer,
328 ),
329 ),
330 );
331
332 expect(resolved).toEqual({
333 logLevel: "Info",
334 ...defaultObservabilityConfig,
335 mode: "web",
336 port: 8788,
337 cwd: process.cwd(),
338 baseDir,
339 ...derivedPaths,
340 host: "127.0.0.1",
341 staticDir: undefined,
342 devUrl: new URL("http://127.0.0.1:4173"),
343 noBrowser: false,
344 startupPresentation: "browser",
345 desktopBootstrapToken: "desktop-bootstrap-token",
346 autoBootstrapProjectFromCwd: false,
347 logWebSocketEvents: false,
348 tailscaleServeEnabled: false,
349 tailscaleServePort: 443,
350 });
351 }),
352 );
353
354 it.effect("applies flag then env precedence over bootstrap envelope values", () =>
355 Effect.gen(function* () {
356 const { join } = yield* Path.Path;
357 const baseDir = join(NodeOS.tmpdir(), "t3-cli-config-env-wins");
358 const fd = yield* openBootstrapFd(
359 makeDesktopBootstrap({
360 port: 4888,
361 host: "127.0.0.2",
362 t3Home: "/tmp/t3-bootstrap-home",
363 noBrowser: false,
364 desktopBootstrapToken: "desktop-token",
365 tailscaleServeEnabled: false,
366 tailscaleServePort: 443,
367 }),
368 );
369 const derivedPaths = yield* deriveExplicitServerPaths(
370 baseDir,
371 new URL("http://127.0.0.1:4173"),
372 );
373
374 const resolved = yield* resolveServerConfig(
375 {
376 mode: Option.none(),
377 port: Option.some(8788),
378 host: Option.some("127.0.0.1"),
379 baseDir: Option.none(),
380 cwd: Option.none(),
381 devUrl: Option.some(new URL("http://127.0.0.1:4173")),
382 noBrowser: Option.none(),
383 bootstrapFd: Option.none(),
384 autoBootstrapProjectFromCwd: Option.none(),
385 logWebSocketEvents: Option.none(),
386 tailscaleServeEnabled: Option.none(),
387 tailscaleServePort: Option.none(),
388 },
389 Option.some("Debug"),
390 ).pipe(
391 Effect.provide(
392 Layer.mergeAll(
393 ConfigProvider.layer(
394 ConfigProvider.fromEnv({
395 env: {
396 T3CODE_MODE: "web",
397 T3CODE_BOOTSTRAP_FD: String(fd),
398 T3CODE_HOME: baseDir,
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: "Debug",
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: true,
422 startupPresentation: "browser",
423 desktopBootstrapToken: "desktop-token",
424 autoBootstrapProjectFromCwd: true,
425 logWebSocketEvents: true,
426 tailscaleServeEnabled: false,
427 tailscaleServePort: 443,
428 });
429 }),
430 );
431
432 it.effect("zeroes an endpoint stored in Settings when the SDK is disabled", () =>
433 Effect.gen(function* () {
434 const fs = yield* FileSystem.FileSystem;
435 const path = yield* Path.Path;
436 const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-cli-config-otel-off-" });
437 const derivedPaths = yield* deriveExplicitServerPaths(baseDir, undefined);
438 yield* fs.makeDirectory(path.dirname(derivedPaths.settingsPath), { recursive: true });
439 yield* fs.writeFileString(
440 derivedPaths.settingsPath,
441 `${JSON.stringify({
442 observability: {
443 otlpTracesUrl: "http://localhost:4318/v1/traces",
444 otlpMetricsUrl: "http://localhost:4318/v1/metrics",
445 otlpLogsUrl: "http://localhost:4318/v1/logs",
446 },
447 })}\n`,
448 );
449
450 const resolved = yield* resolveServerConfig(
451 {
452 mode: Option.some("desktop"),
453 port: Option.some(4888),
454 host: Option.none(),
455 baseDir: Option.some(baseDir),
456 cwd: Option.none(),
457 devUrl: Option.none(),
458 noBrowser: Option.none(),
459 bootstrapFd: Option.none(),
460 autoBootstrapProjectFromCwd: Option.none(),
461 logWebSocketEvents: Option.none(),
462 tailscaleServeEnabled: Option.none(),
463 tailscaleServePort: Option.none(),
464 },
465 Option.none(),
466 ).pipe(
467 Effect.provide(
468 Layer.mergeAll(
469 ConfigProvider.layer(ConfigProvider.fromEnv({ env: { OTEL_SDK_DISABLED: "true" } })),
470 NetService.layer,
471 ),
472 ),
473 );
474
475 expect(resolved.otlpTracesUrl).toBeUndefined();
476 expect(resolved.otlpMetricsUrl).toBeUndefined();
477 expect(resolved.otlpLogsUrl).toBeUndefined();
478 expect(resolved.otelEnvironment.disabled).toBe(true);
479 }),
480 );
481
482 it.effect("lets T3CODE_OTEL_SDK_DISABLED=false override an ambient OTEL_SDK_DISABLED=true", () =>
483 Effect.gen(function* () {
484 const fs = yield* FileSystem.FileSystem;
485 const path = yield* Path.Path;
486 const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-cli-config-otel-on-" });
487 const derivedPaths = yield* deriveExplicitServerPaths(baseDir, undefined);
488 yield* fs.makeDirectory(path.dirname(derivedPaths.settingsPath), { recursive: true });
489 yield* fs.writeFileString(
490 derivedPaths.settingsPath,
491 `${JSON.stringify({
492 observability: {
493 otlpTracesUrl: "http://localhost:4318/v1/traces",
494 },
495 })}\n`,
496 );
497
498 const resolved = yield* resolveServerConfig(
499 {
500 mode: Option.some("desktop"),
501 port: Option.some(4888),
502 host: Option.none(),
503 baseDir: Option.some(baseDir),
504 cwd: Option.none(),
505 devUrl: Option.none(),
506 noBrowser: Option.none(),
507 bootstrapFd: Option.none(),
508 autoBootstrapProjectFromCwd: Option.none(),
509 logWebSocketEvents: Option.none(),
510 tailscaleServeEnabled: Option.none(),
511 tailscaleServePort: Option.none(),
512 },
513 Option.none(),
514 ).pipe(
515 Effect.provide(
516 Layer.mergeAll(
517 ConfigProvider.layer(
518 ConfigProvider.fromEnv({
519 env: { T3CODE_OTEL_SDK_DISABLED: "false", OTEL_SDK_DISABLED: "true" },
520 }),
521 ),
522 NetService.layer,
523 ),
524 ),
525 );
526
527 expect(resolved.otelEnvironment.disabled).toBe(false);
528 expect(resolved.otlpTracesUrl).toBe("http://localhost:4318/v1/traces");
529 }),
530 );
531
532 it.effect("forces noBrowser and disables auto-bootstrap for headless startup presentation", () =>
533 Effect.gen(function* () {
534 const { join } = yield* Path.Path;
535 const baseDir = join(NodeOS.tmpdir(), "t3-cli-config-headless-base");
536 const derivedPaths = yield* deriveExplicitServerPaths(baseDir, undefined);
537
538 const resolved = yield* resolveServerConfig(
539 {
540 mode: Option.some("web"),
541 port: Option.some(3773),
542 host: Option.none(),
543 baseDir: Option.some(baseDir),
544 cwd: Option.none(),
545 devUrl: Option.none(),
546 noBrowser: Option.none(),
547 bootstrapFd: Option.none(),
548 autoBootstrapProjectFromCwd: Option.none(),
549 logWebSocketEvents: Option.none(),
550 tailscaleServeEnabled: Option.none(),
551 tailscaleServePort: Option.none(),
552 },
553 Option.none(),
554 {
555 startupPresentation: "headless",
556 },
557 ).pipe(
558 Effect.provide(
559 Layer.mergeAll(
560 ConfigProvider.layer(
561 ConfigProvider.fromEnv({
562 env: {
563 T3CODE_NO_BROWSER: "false",
564 T3CODE_AUTO_BOOTSTRAP_PROJECT_FROM_CWD: "true",
565 },
566 }),
567 ),
568 NetService.layer,
569 ),
570 ),
571 );
572
573 expect(resolved).toEqual({
574 logLevel: "Info",
575 ...defaultObservabilityConfig,
576 mode: "web",
577 port: 3773,
578 cwd: process.cwd(),
579 baseDir,
580 ...derivedPaths,
581 host: undefined,
582 staticDir: resolved.staticDir,
583 devUrl: undefined,
584 noBrowser: true,
585 startupPresentation: "headless",
586 desktopBootstrapToken: undefined,
587 autoBootstrapProjectFromCwd: false,
588 logWebSocketEvents: false,
589 tailscaleServeEnabled: false,
590 tailscaleServePort: 443,
591 });
592 }),
593 );
594
595 it.effect("keeps whitespace-separated pairs and literal equals signs in OTLP headers", () =>
596 Effect.gen(function* () {
597 const { join } = yield* Path.Path;
598 const baseDir = join(NodeOS.tmpdir(), "t3-cli-config-otlp-headers-loose-base");
599
600 const resolved = yield* resolveServerConfig(
601 {
602 mode: Option.some("web"),
603 port: Option.some(3773),
604 host: Option.none(),
605 baseDir: Option.some(baseDir),
606 cwd: Option.none(),
607 devUrl: Option.none(),
608 noBrowser: Option.none(),
609 bootstrapFd: Option.none(),
610 autoBootstrapProjectFromCwd: Option.none(),
611 logWebSocketEvents: Option.none(),
612 tailscaleServeEnabled: Option.none(),
613 tailscaleServePort: Option.none(),
614 },
615 Option.none(),
616 ).pipe(
617 Effect.provide(
618 Layer.mergeAll(
619 ConfigProvider.layer(
620 ConfigProvider.fromEnv({
621 env: {
622 T3CODE_OTLP_HEADERS: "authorization=Bearer abc==, x-tenant=t3",
623 T3CODE_OTLP_TRACES_URL: "http://collector.internal:4318",
624 },
625 }),
626 ),
627 NetService.layer,
628 ),
629 ),
630 );
631
632 expect(resolved.otlpTracesExport.headers).toEqual({
633 authorization: "Bearer abc==",
634 "x-tenant": "t3",
635 });
636 expect(resolved.otlpTracesUrl).toBe("http://collector.internal:4318");
637 }),
638 );
639
640 const minimalWebFlags = (baseDir: string) => ({
641 mode: Option.some("web" as const),
642 port: Option.some(3773),
643 host: Option.none<string>(),
644 baseDir: Option.some(baseDir),
645 cwd: Option.none<string>(),
646 devUrl: Option.none<URL>(),
647 noBrowser: Option.none<boolean>(),
648 bootstrapFd: Option.none<number>(),
649 autoBootstrapProjectFromCwd: Option.none<boolean>(),
650 logWebSocketEvents: Option.none<boolean>(),
651 tailscaleServeEnabled: Option.none<boolean>(),
652 tailscaleServePort: Option.none<number>(),
653 });
654
655 it.effect(
656 "resolves each signal's endpoint through T3CODE_OTLP_*_URL, an OTEL endpoint, the bootstrap envelope, and persisted Settings, in that order",
657 () =>
658 Effect.gen(function* () {
659 const fs = yield* FileSystem.FileSystem;
660 const path = yield* Path.Path;
661 const baseDir = yield* fs.makeTempDirectoryScoped({
662 prefix: "t3-cli-config-otel-precedence-",
663 });
664 const derivedPaths = yield* deriveExplicitServerPaths(baseDir, undefined);
665 yield* fs.makeDirectory(path.dirname(derivedPaths.settingsPath), { recursive: true });
666 yield* fs.writeFileString(
667 derivedPaths.settingsPath,
668 `${JSON.stringify({ observability: { otlpLogsUrl: "http://settings:4318/v1/logs" } })}\n`,
669 );
670
671 const fd = yield* openBootstrapFd(
672 makeDesktopBootstrap({
673 otlpMetricsUrl: "http://bootstrap:4318/v1/metrics",
674 otlpLogsUrl: "",
675 }),
676 );
677
678 const resolved = yield* resolveServerConfig(
679 {
680 ...minimalWebFlags(baseDir),
681 mode: Option.some("desktop"),
682 port: Option.some(4888),
683 bootstrapFd: Option.some(fd),
684 },
685 Option.none(),
686 ).pipe(
687 Effect.provide(
688 Layer.mergeAll(
689 ConfigProvider.layer(
690 ConfigProvider.fromEnv({
691 env: {
692 T3CODE_OTLP_TRACES_URL: "http://t3:4318/v1/traces",
693 T3CODE_OTLP_HEADERS: "x-key=secret",
694 OTEL_EXPORTER_OTLP_TRACES_ENDPOINT: "http://otel-traces:4318/custom",
695 OTEL_EXPORTER_OTLP_METRICS_ENDPOINT: "http://otel-metrics:4318/custom",
696 OTEL_EXPORTER_OTLP_HEADERS: "x-key=otel",
697 },
698 }),
699 ),
700 NetService.layer,
701 ),
702 ),
703 );
704
705 expect(resolved.otlpTracesUrl).toBe("http://t3:4318/v1/traces");
706 expect(resolved.otlpTracesExport.headers).toEqual({ "x-key": "secret" });
707 expect(resolved.otlpMetricsUrl).toBe("http://otel-metrics:4318/custom");
708 expect(resolved.otlpMetricsExport).toEqual({
709 ...DEFAULT_SIGNAL_EXPORT,
710 protocol: "http/protobuf",
711 headers: { "x-key": "otel" },
712 });
713 expect(resolved.otlpLogsUrl).toBe("http://settings:4318/v1/logs");
714 expect(resolved.otlpLogsExport.headers).toEqual({ "x-key": "secret" });
715 }),
716 );
717
718 it.effect(
719 "exports nothing for a signal an OTEL endpoint claimed with a protocol or headers that do not read",
720 () =>
721 Effect.gen(function* () {
722 const fs = yield* FileSystem.FileSystem;
723 const path = yield* Path.Path;
724 const baseDir = yield* fs.makeTempDirectoryScoped({
725 prefix: "t3-cli-config-otel-off-",
726 });
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 `${JSON.stringify({ observability: { otlpLogsUrl: "http://settings:4318/v1/logs" } })}\n`,
732 );
733
734 const fd = yield* openBootstrapFd(
735 makeDesktopBootstrap({ otlpMetricsUrl: "http://bootstrap:4318/v1/metrics" }),
736 );
737
738 const resolved = yield* resolveServerConfig(
739 {
740 ...minimalWebFlags(baseDir),
741 mode: Option.some("desktop"),
742 port: Option.some(4888),
743 bootstrapFd: Option.some(fd),
744 },
745 Option.none(),
746 ).pipe(
747 Effect.provide(
748 Layer.mergeAll(
749 ConfigProvider.layer(
750 ConfigProvider.fromEnv({
751 env: {
752 T3CODE_OTLP_TRACES_URL: "http://t3:4318/v1/traces",
753 OTEL_EXPORTER_OTLP_ENDPOINT: "http://otel:4318",
754 OTEL_EXPORTER_OTLP_HEADERS: "x-key=%zz",
755 },
756 }),
757 ),
758 NetService.layer,
759 ),
760 ),
761 );
762
763 expect(resolved.otlpTracesUrl).toBe("http://t3:4318/v1/traces");
764 expect(resolved.otlpMetricsUrl).toBeUndefined();
765 expect(resolved.otlpLogsUrl).toBeUndefined();
766 }),
767 );
768});