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