Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

config.test.ts

892 lines29.7 KB

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 * as NodeServices from "@effect/platform-node/NodeServices";
21import { deriveServerPaths } from "../config.ts";
22import { resolveServerConfig } from "./config.ts";
23​
24const deriveExplicitServerPaths = (baseDir: string, devUrl: URL | undefined) =>
25 deriveServerPaths(baseDir, devUrl, { baseDirIsExplicit: true });
26​
27const encodeDesktopBootstrap = Schema.encodeEffect(Schema.fromJsonString(DesktopBackendBootstrap));
28const encodeUnknownJson = Schema.encodeEffect(Schema.fromJsonString(Schema.Unknown));
29​
30const makeDesktopBootstrap = (
31 overrides: Partial<DesktopBackendBootstrapValue> = {},
32): DesktopBackendBootstrapValue => ({
33 mode: "desktop",
34 noBrowser: true,
35 port: 4888,
36 t3Home: "/tmp/t3-bootstrap-home",
37 host: "127.0.0.1",
38 desktopBootstrapToken: "desktop-bootstrap-token",
39 tailscaleServeEnabled: false,
40 tailscaleServePort: 443,
41 ...overrides,
42});
43​
44it.layer(NodeServices.layer)("cli config resolution", (it) => {
45 const defaultObservabilityConfig = {
46 traceMinLevel: "Info",
47 traceTimingEnabled: true,
48 traceBatchWindowMs: 1_000,
49 traceMaxBytes: 10 * 1024 * 1024,
50 traceMaxFiles: 10,
51 otlpTracesUrl: undefined,
52 otlpMetricsUrl: undefined,
53 otlpLogsUrl: undefined,
54 otlpExportIntervalMs: 10_000,
55 otlpServiceName: "t3-server",
56 otlpHeaders: undefined,
57 otlpProtocol: "http/json",
58 devAllowedOrigins: [],
59 } as const;
60​
61 const openBootstrapFd = Effect.fn(function* (payload: DesktopBackendBootstrapValue) {
62 const fs = yield* FileSystem.FileSystem;
63 const filePath = yield* fs.makeTempFileScoped({ prefix: "t3-bootstrap-", suffix: ".ndjson" });
64 const encoded = yield* encodeDesktopBootstrap(payload);
65 yield* fs.writeFileString(filePath, `${encoded}\n`);
66 return yield* Effect.acquireRelease(
67 Effect.sync(() => NodeFS.openSync(filePath, "r")),
68 // Without a /proc or /dev/fd path to reopen, the reader consumes the fd
69 // itself (autoClose), so on Windows it is already closed here.
70 (fd) =>
71 Effect.sync(() => {
72 try {
73 NodeFS.closeSync(fd);
74 } catch (error) {
75 if ((error as NodeJS.ErrnoException).code !== "EBADF") throw error;
76 }
77 }),
78 );
79 });
80​
81 it.effect("enables a trimmed reusable auth token only for web dev mode", () =>
82 Effect.gen(function* () {
83 const baseDir = yield* FileSystem.FileSystem.pipe(
84 Effect.flatMap((fs) => fs.makeTempDirectoryScoped({ prefix: "t3-cli-dev-auth-" })),
85 );
86 const flags = {
87 mode: Option.some("web" as const),
88 port: Option.some(8788),
89 host: Option.none<string>(),
90 baseDir: Option.some(baseDir),
91 cwd: Option.none<string>(),
92 devUrl: Option.some(new URL("http://127.0.0.1:5173")),
93 noBrowser: Option.none<boolean>(),
94 bootstrapFd: Option.none<number>(),
95 autoBootstrapProjectFromCwd: Option.none<boolean>(),
96 logWebSocketEvents: Option.none<boolean>(),
97 tailscaleServeEnabled: Option.none<boolean>(),
98 tailscaleServePort: Option.none<number>(),
99 };
100 const configLayer = ConfigProvider.layer(
101 ConfigProvider.fromEnv({
102 env: {
103 T3CODE_DEV_AUTH_TOKEN: " reusable-dev-auth-token-that-is-long-enough ",
104 },
105 }),
106 );
107 const web = yield* resolveServerConfig(flags, Option.none()).pipe(
108 Effect.provide(Layer.mergeAll(configLayer, NetService.layer)),
109 );
110 const desktop = yield* resolveServerConfig(
111 { ...flags, mode: Option.some("desktop" as const) },
112 Option.none(),
113 ).pipe(Effect.provide(Layer.mergeAll(configLayer, NetService.layer)));
114​
115 expect(web.devAuthToken).toBeDefined();
116 if (web.devAuthToken === undefined) {
117 return yield* Effect.die("Expected reusable dev auth token.");
118 }
119 expect(Redacted.value(web.devAuthToken)).toBe("reusable-dev-auth-token-that-is-long-enough");
120 expect(desktop.devAuthToken).toBeUndefined();
121 }),
122 );
123​
124 it.effect("does not expose an invalid reusable auth token", () =>
125 Effect.gen(function* () {
126 const secret = "short-secret";
127 const baseDir = yield* FileSystem.FileSystem.pipe(
128 Effect.flatMap((fs) => fs.makeTempDirectoryScoped({ prefix: "t3-cli-dev-auth-invalid-" })),
129 );
130 const flags = {
131 mode: Option.some("web" as const),
132 port: Option.some(8788),
133 host: Option.none<string>(),
134 baseDir: Option.some(baseDir),
135 cwd: Option.none<string>(),
136 devUrl: Option.some(new URL("http://127.0.0.1:5173")),
137 noBrowser: Option.none<boolean>(),
138 bootstrapFd: Option.none<number>(),
139 autoBootstrapProjectFromCwd: Option.none<boolean>(),
140 logWebSocketEvents: Option.none<boolean>(),
141 tailscaleServeEnabled: Option.none<boolean>(),
142 tailscaleServePort: Option.none<number>(),
143 };
144 const configLayer = ConfigProvider.layer(
145 ConfigProvider.fromEnv({ env: { T3CODE_DEV_AUTH_TOKEN: secret } }),
146 );
147 const error = yield* resolveServerConfig(flags, Option.none()).pipe(
148 Effect.provide(Layer.mergeAll(configLayer, NetService.layer)),
149 Effect.flip,
150 );
151 const desktop = yield* resolveServerConfig(
152 { ...flags, mode: Option.some("desktop" as const) },
153 Option.none(),
154 ).pipe(Effect.provide(Layer.mergeAll(configLayer, NetService.layer)));
155 const staticWeb = yield* resolveServerConfig(
156 { ...flags, devUrl: Option.none() },
157 Option.none(),
158 ).pipe(Effect.provide(Layer.mergeAll(configLayer, NetService.layer)));
159​
160 expect(String(error)).not.toContain(secret);
161 const serialized = yield* encodeUnknownJson(error);
162 expect(serialized).not.toContain(secret);
163 expect(desktop.devAuthToken).toBeUndefined();
164 expect(staticWeb.devAuthToken).toBeUndefined();
165 }),
166 );
167​
168 it.effect("falls back to effect/config values when flags are omitted", () =>
169 Effect.gen(function* () {
170 const { join } = yield* Path.Path;
171 const baseDir = join(NodeOS.tmpdir(), "t3-cli-config-env-base");
172 const derivedPaths = yield* deriveExplicitServerPaths(
173 baseDir,
174 new URL("http://127.0.0.1:5173"),
175 );
176 const resolved = yield* resolveServerConfig(
177 {
178 mode: Option.none(),
179 port: Option.none(),
180 host: Option.none(),
181 baseDir: Option.none(),
182 cwd: Option.none(),
183 devUrl: Option.none(),
184 noBrowser: Option.none(),
185 bootstrapFd: Option.none(),
186 autoBootstrapProjectFromCwd: Option.none(),
187 logWebSocketEvents: Option.none(),
188 tailscaleServeEnabled: Option.none(),
189 tailscaleServePort: Option.none(),
190 },
191 Option.none(),
192 ).pipe(
193 Effect.provide(
194 Layer.mergeAll(
195 ConfigProvider.layer(
196 ConfigProvider.fromEnv({
197 env: {
198 T3CODE_LOG_LEVEL: "Warn",
199 T3CODE_MODE: "desktop",
200 T3CODE_PORT: "4001",
201 T3CODE_HOST: "0.0.0.0",
202 T3CODE_HOME: baseDir,
203 VITE_DEV_SERVER_URL: "http://127.0.0.1:5173",
204 T3CODE_DEV_ALLOWED_ORIGINS:
205 "https://host.example.ts.net, https://phone.example.ts.net ",
206 T3CODE_NO_BROWSER: "true",
207 T3CODE_AUTO_BOOTSTRAP_PROJECT_FROM_CWD: "false",
208 T3CODE_LOG_WS_EVENTS: "true",
209 },
210 }),
211 ),
212 NetService.layer,
213 ),
214 ),
215 );
216​
217 expect(resolved).toEqual({
218 logLevel: "Warn",
219 ...defaultObservabilityConfig,
220 mode: "desktop",
221 port: 4001,
222 cwd: process.cwd(),
223 baseDir,
224 ...derivedPaths,
225 host: "0.0.0.0",
226 staticDir: undefined,
227 devUrl: new URL("http://127.0.0.1:5173"),
228 devAllowedOrigins: ["https://host.example.ts.net", "https://phone.example.ts.net"],
229 noBrowser: true,
230 startupPresentation: "browser",
231 desktopBootstrapToken: undefined,
232 autoBootstrapProjectFromCwd: false,
233 logWebSocketEvents: true,
234 tailscaleServeEnabled: false,
235 tailscaleServePort: 443,
236 });
237 assert.equal(resolved.stateDir, join(baseDir, "userdata"));
238 }),
239 );
240​
241 it.effect("uses CLI flags when provided", () =>
242 Effect.gen(function* () {
243 const { join } = yield* Path.Path;
244 const baseDir = join(NodeOS.tmpdir(), "t3-cli-config-flags-base");
245 const derivedPaths = yield* deriveExplicitServerPaths(
246 baseDir,
247 new URL("http://127.0.0.1:4173"),
248 );
249 const resolved = yield* resolveServerConfig(
250 {
251 mode: Option.some("web"),
252 port: Option.some(8788),
253 host: Option.some("127.0.0.1"),
254 baseDir: Option.some(baseDir),
255 cwd: Option.none(),
256 devUrl: Option.some(new URL("http://127.0.0.1:4173")),
257 noBrowser: Option.some(true),
258 bootstrapFd: Option.none(),
259 autoBootstrapProjectFromCwd: Option.some(true),
260 logWebSocketEvents: Option.some(true),
261 tailscaleServeEnabled: Option.some(true),
262 tailscaleServePort: Option.some(8443),
263 },
264 Option.some("Debug"),
265 ).pipe(
266 Effect.provide(
267 Layer.mergeAll(
268 ConfigProvider.layer(
269 ConfigProvider.fromEnv({
270 env: {
271 T3CODE_LOG_LEVEL: "Warn",
272 T3CODE_MODE: "desktop",
273 T3CODE_PORT: "4001",
274 T3CODE_HOST: "0.0.0.0",
275 T3CODE_HOME: join(NodeOS.tmpdir(), "ignored-base"),
276 VITE_DEV_SERVER_URL: "http://127.0.0.1:5173",
277 T3CODE_NO_BROWSER: "false",
278 T3CODE_AUTO_BOOTSTRAP_PROJECT_FROM_CWD: "false",
279 T3CODE_LOG_WS_EVENTS: "false",
280 },
281 }),
282 ),
283 NetService.layer,
284 ),
285 ),
286 );
287​
288 expect(resolved).toEqual({
289 logLevel: "Debug",
290 ...defaultObservabilityConfig,
291 mode: "web",
292 port: 8788,
293 cwd: process.cwd(),
294 baseDir,
295 ...derivedPaths,
296 host: "127.0.0.1",
297 staticDir: undefined,
298 devUrl: new URL("http://127.0.0.1:4173"),
299 noBrowser: true,
300 startupPresentation: "browser",
301 desktopBootstrapToken: undefined,
302 autoBootstrapProjectFromCwd: true,
303 logWebSocketEvents: true,
304 tailscaleServeEnabled: true,
305 tailscaleServePort: 8443,
306 });
307 assert.equal(resolved.dbPath, join(baseDir, "userdata", "state.sqlite"));
308 }),
309 );
310​
311 it.effect("preserves explicit false CLI boolean flags over env and bootstrap values", () =>
312 Effect.gen(function* () {
313 const { join } = yield* Path.Path;
314 const baseDir = join(NodeOS.tmpdir(), "t3-cli-config-false-flags");
315 const fd = yield* openBootstrapFd(
316 makeDesktopBootstrap({
317 noBrowser: true,
318 tailscaleServeEnabled: false,
319 tailscaleServePort: 443,
320 }),
321 );
322 const derivedPaths = yield* deriveExplicitServerPaths(
323 baseDir,
324 new URL("http://127.0.0.1:4173"),
325 );
326​
327 const resolved = yield* resolveServerConfig(
328 {
329 mode: Option.some("web"),
330 port: Option.some(8788),
331 host: Option.some("127.0.0.1"),
332 baseDir: Option.some(baseDir),
333 cwd: Option.none(),
334 devUrl: Option.some(new URL("http://127.0.0.1:4173")),
335 noBrowser: Option.some(false),
336 bootstrapFd: Option.none(),
337 autoBootstrapProjectFromCwd: Option.some(false),
338 logWebSocketEvents: Option.some(false),
339 tailscaleServeEnabled: Option.none(),
340 tailscaleServePort: Option.none(),
341 },
342 Option.none(),
343 ).pipe(
344 Effect.provide(
345 Layer.mergeAll(
346 ConfigProvider.layer(
347 ConfigProvider.fromEnv({
348 env: {
349 T3CODE_BOOTSTRAP_FD: String(fd),
350 T3CODE_NO_BROWSER: "true",
351 T3CODE_AUTO_BOOTSTRAP_PROJECT_FROM_CWD: "true",
352 T3CODE_LOG_WS_EVENTS: "true",
353 },
354 }),
355 ),
356 NetService.layer,
357 ),
358 ),
359 );
360​
361 expect(resolved).toEqual({
362 logLevel: "Info",
363 ...defaultObservabilityConfig,
364 mode: "web",
365 port: 8788,
366 cwd: process.cwd(),
367 baseDir,
368 ...derivedPaths,
369 host: "127.0.0.1",
370 staticDir: undefined,
371 devUrl: new URL("http://127.0.0.1:4173"),
372 noBrowser: false,
373 startupPresentation: "browser",
374 desktopBootstrapToken: "desktop-bootstrap-token",
375 autoBootstrapProjectFromCwd: false,
376 logWebSocketEvents: false,
377 tailscaleServeEnabled: false,
378 tailscaleServePort: 443,
379 });
380 }),
381 );
382​
383 it.effect("uses bootstrap envelope values as fallbacks when flags and env are absent", () =>
384 Effect.gen(function* () {
385 const { join, resolve } = yield* Path.Path;
386 // The resolver absolutises the configured home, so the expectation must
387 // carry the host's drive on Windows.
388 const baseDir = resolve("/tmp/t3-bootstrap-home");
389 const fd = yield* openBootstrapFd(
390 makeDesktopBootstrap({
391 port: 4888,
392 host: "127.0.0.2",
393 t3Home: "/tmp/t3-bootstrap-home",
394 noBrowser: true,
395 desktopBootstrapToken: "desktop-token",
396 desktopTelemetryFd: 4,
397 desktopTelemetryControlFd: 5,
398 tailscaleServeEnabled: false,
399 tailscaleServePort: 443,
400 otlpTracesUrl: "http://localhost:4318/v1/traces",
401 otlpMetricsUrl: "http://localhost:4318/v1/metrics",
402 otlpLogsUrl: "http://localhost:4318/v1/logs",
403 }),
404 );
405 const derivedPaths = yield* deriveServerPaths(baseDir, undefined);
406​
407 const resolved = yield* resolveServerConfig(
408 {
409 mode: Option.none(),
410 port: Option.none(),
411 host: Option.none(),
412 baseDir: Option.none(),
413 cwd: Option.none(),
414 devUrl: Option.none(),
415 noBrowser: Option.none(),
416 bootstrapFd: Option.none(),
417 autoBootstrapProjectFromCwd: Option.none(),
418 logWebSocketEvents: Option.none(),
419 tailscaleServeEnabled: Option.none(),
420 tailscaleServePort: Option.none(),
421 },
422 Option.none(),
423 ).pipe(
424 Effect.provide(
425 Layer.mergeAll(
426 ConfigProvider.layer(
427 ConfigProvider.fromEnv({
428 env: {
429 T3CODE_BOOTSTRAP_FD: String(fd),
430 },
431 }),
432 ),
433 NetService.layer,
434 ),
435 ),
436 );
437​
438 expect(resolved).toEqual({
439 logLevel: "Info",
440 ...defaultObservabilityConfig,
441 otlpTracesUrl: "http://localhost:4318/v1/traces",
442 otlpMetricsUrl: "http://localhost:4318/v1/metrics",
443 otlpLogsUrl: "http://localhost:4318/v1/logs",
444 mode: "desktop",
445 port: 4888,
446 cwd: process.cwd(),
447 baseDir,
448 ...derivedPaths,
449 host: "127.0.0.2",
450 staticDir: resolved.staticDir,
451 devUrl: undefined,
452 noBrowser: true,
453 startupPresentation: "browser",
454 desktopBootstrapToken: "desktop-token",
455 desktopTelemetryFd: 4,
456 desktopTelemetryControlFd: 5,
457 resourceMonitorPath: undefined,
458 autoBootstrapProjectFromCwd: false,
459 logWebSocketEvents: false,
460 tailscaleServeEnabled: false,
461 tailscaleServePort: 443,
462 });
463 assert.equal(join(baseDir, "userdata"), resolved.stateDir);
464 assert.equal(resolved.desktopTelemetryFd, 4);
465 assert.equal(resolved.desktopTelemetryControlFd, 5);
466 }),
467 );
468​
469 it.effect("creates derived runtime directories during config resolution", () =>
470 Effect.gen(function* () {
471 const fs = yield* FileSystem.FileSystem;
472 const path = yield* Path.Path;
473 const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-cli-config-dirs-" });
474 const customCwd = path.join(baseDir, "nested", "project");
475​
476 const resolved = yield* resolveServerConfig(
477 {
478 mode: Option.some("desktop"),
479 port: Option.some(4888),
480 host: Option.none(),
481 baseDir: Option.some(baseDir),
482 cwd: Option.some(customCwd),
483 devUrl: Option.some(new URL("http://127.0.0.1:5173")),
484 noBrowser: Option.none(),
485 bootstrapFd: Option.none(),
486 autoBootstrapProjectFromCwd: Option.none(),
487 logWebSocketEvents: Option.none(),
488 tailscaleServeEnabled: Option.none(),
489 tailscaleServePort: Option.none(),
490 },
491 Option.none(),
492 ).pipe(
493 Effect.provide(
494 Layer.mergeAll(
495 ConfigProvider.layer(ConfigProvider.fromEnv({ env: {} })),
496 NetService.layer,
497 ),
498 ),
499 );
500​
501 for (const directory of [
502 customCwd,
503 resolved.stateDir,
504 resolved.logsDir,
505 resolved.providerLogsDir,
506 resolved.terminalLogsDir,
507 resolved.attachmentsDir,
508 resolved.worktreesDir,
509 path.dirname(resolved.serverLogPath),
510 path.dirname(resolved.serverTracePath),
511 ]) {
512 expect(yield* fs.exists(directory)).toBe(true);
513 }
514 expect(resolved.cwd).toBe(path.resolve(customCwd));
515 }),
516 );
517​
518 it.effect("applies flag then env precedence over bootstrap envelope values", () =>
519 Effect.gen(function* () {
520 const { join } = yield* Path.Path;
521 const baseDir = join(NodeOS.tmpdir(), "t3-cli-config-env-wins");
522 const fd = yield* openBootstrapFd(
523 makeDesktopBootstrap({
524 port: 4888,
525 host: "127.0.0.2",
526 t3Home: "/tmp/t3-bootstrap-home",
527 noBrowser: false,
528 desktopBootstrapToken: "desktop-token",
529 tailscaleServeEnabled: false,
530 tailscaleServePort: 443,
531 }),
532 );
533 const derivedPaths = yield* deriveExplicitServerPaths(
534 baseDir,
535 new URL("http://127.0.0.1:4173"),
536 );
537​
538 const resolved = yield* resolveServerConfig(
539 {
540 mode: Option.none(),
541 port: Option.some(8788),
542 host: Option.some("127.0.0.1"),
543 baseDir: Option.none(),
544 cwd: Option.none(),
545 devUrl: Option.some(new URL("http://127.0.0.1:4173")),
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.some("Debug"),
554 ).pipe(
555 Effect.provide(
556 Layer.mergeAll(
557 ConfigProvider.layer(
558 ConfigProvider.fromEnv({
559 env: {
560 T3CODE_MODE: "web",
561 T3CODE_BOOTSTRAP_FD: String(fd),
562 T3CODE_HOME: baseDir,
563 T3CODE_NO_BROWSER: "true",
564 T3CODE_AUTO_BOOTSTRAP_PROJECT_FROM_CWD: "true",
565 T3CODE_LOG_WS_EVENTS: "true",
566 },
567 }),
568 ),
569 NetService.layer,
570 ),
571 ),
572 );
573​
574 expect(resolved).toEqual({
575 logLevel: "Debug",
576 ...defaultObservabilityConfig,
577 mode: "web",
578 port: 8788,
579 cwd: process.cwd(),
580 baseDir,
581 ...derivedPaths,
582 host: "127.0.0.1",
583 staticDir: undefined,
584 devUrl: new URL("http://127.0.0.1:4173"),
585 noBrowser: true,
586 startupPresentation: "browser",
587 desktopBootstrapToken: "desktop-token",
588 autoBootstrapProjectFromCwd: true,
589 logWebSocketEvents: true,
590 tailscaleServeEnabled: false,
591 tailscaleServePort: 443,
592 });
593 }),
594 );
595​
596 it.effect("falls back to persisted observability settings when env vars are absent", () =>
597 Effect.gen(function* () {
598 const fs = yield* FileSystem.FileSystem;
599 const path = yield* Path.Path;
600 const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-cli-config-settings-" });
601 const derivedPaths = yield* deriveExplicitServerPaths(baseDir, undefined);
602 yield* fs.makeDirectory(path.dirname(derivedPaths.settingsPath), { recursive: true });
603 yield* fs.writeFileString(
604 derivedPaths.settingsPath,
605 // @effect-diagnostics-next-line preferSchemaOverJson:off
606 `${JSON.stringify({
607 observability: {
608 otlpTracesUrl: "http://localhost:4318/v1/traces",
609 otlpMetricsUrl: "http://localhost:4318/v1/metrics",
610 otlpLogsUrl: "http://localhost:4318/v1/logs",
611 },
612 })}\n`,
613 );
614​
615 const resolved = yield* resolveServerConfig(
616 {
617 mode: Option.some("desktop"),
618 port: Option.some(4888),
619 host: Option.none(),
620 baseDir: Option.some(baseDir),
621 cwd: Option.none(),
622 devUrl: Option.none(),
623 noBrowser: Option.none(),
624 bootstrapFd: Option.none(),
625 autoBootstrapProjectFromCwd: Option.none(),
626 logWebSocketEvents: Option.none(),
627 tailscaleServeEnabled: Option.none(),
628 tailscaleServePort: Option.none(),
629 },
630 Option.none(),
631 ).pipe(
632 Effect.provide(
633 Layer.mergeAll(
634 ConfigProvider.layer(ConfigProvider.fromEnv({ env: {} })),
635 NetService.layer,
636 ),
637 ),
638 );
639​
640 expect(resolved.otlpTracesUrl).toBe("http://localhost:4318/v1/traces");
641 expect(resolved.otlpMetricsUrl).toBe("http://localhost:4318/v1/metrics");
642 expect(resolved.otlpLogsUrl).toBe("http://localhost:4318/v1/logs");
643 expect(resolved).toEqual({
644 logLevel: "Info",
645 ...defaultObservabilityConfig,
646 otlpTracesUrl: "http://localhost:4318/v1/traces",
647 otlpMetricsUrl: "http://localhost:4318/v1/metrics",
648 otlpLogsUrl: "http://localhost:4318/v1/logs",
649 mode: "desktop",
650 port: 4888,
651 cwd: process.cwd(),
652 baseDir,
653 ...derivedPaths,
654 host: "127.0.0.1",
655 staticDir: resolved.staticDir,
656 devUrl: undefined,
657 noBrowser: true,
658 startupPresentation: "browser",
659 desktopBootstrapToken: undefined,
660 autoBootstrapProjectFromCwd: false,
661 logWebSocketEvents: false,
662 tailscaleServeEnabled: false,
663 tailscaleServePort: 443,
664 });
665 }),
666 );
667​
668 it.effect("forces noBrowser and disables auto-bootstrap for headless startup presentation", () =>
669 Effect.gen(function* () {
670 const { join } = yield* Path.Path;
671 const baseDir = join(NodeOS.tmpdir(), "t3-cli-config-headless-base");
672 const derivedPaths = yield* deriveExplicitServerPaths(baseDir, undefined);
673​
674 const resolved = yield* resolveServerConfig(
675 {
676 mode: Option.some("web"),
677 port: Option.some(3773),
678 host: Option.none(),
679 baseDir: Option.some(baseDir),
680 cwd: Option.none(),
681 devUrl: Option.none(),
682 noBrowser: Option.none(),
683 bootstrapFd: Option.none(),
684 autoBootstrapProjectFromCwd: Option.none(),
685 logWebSocketEvents: Option.none(),
686 tailscaleServeEnabled: Option.none(),
687 tailscaleServePort: Option.none(),
688 },
689 Option.none(),
690 {
691 startupPresentation: "headless",
692 },
693 ).pipe(
694 Effect.provide(
695 Layer.mergeAll(
696 ConfigProvider.layer(
697 ConfigProvider.fromEnv({
698 env: {
699 T3CODE_NO_BROWSER: "false",
700 T3CODE_AUTO_BOOTSTRAP_PROJECT_FROM_CWD: "true",
701 },
702 }),
703 ),
704 NetService.layer,
705 ),
706 ),
707 );
708​
709 expect(resolved).toEqual({
710 logLevel: "Info",
711 ...defaultObservabilityConfig,
712 mode: "web",
713 port: 3773,
714 cwd: process.cwd(),
715 baseDir,
716 ...derivedPaths,
717 host: undefined,
718 staticDir: resolved.staticDir,
719 devUrl: undefined,
720 noBrowser: true,
721 startupPresentation: "headless",
722 desktopBootstrapToken: undefined,
723 autoBootstrapProjectFromCwd: false,
724 logWebSocketEvents: false,
725 tailscaleServeEnabled: false,
726 tailscaleServePort: 443,
727 });
728 }),
729 );
730​
731 it.effect("decodes percent-encoded OTLP headers from env", () =>
732 Effect.gen(function* () {
733 const { join } = yield* Path.Path;
734 const baseDir = join(NodeOS.tmpdir(), "t3-cli-config-otlp-headers-base");
735​
736 const resolved = yield* resolveServerConfig(
737 {
738 mode: Option.some("web"),
739 port: Option.some(3773),
740 host: Option.none(),
741 baseDir: Option.some(baseDir),
742 cwd: Option.none(),
743 devUrl: Option.none(),
744 noBrowser: Option.none(),
745 bootstrapFd: Option.none(),
746 autoBootstrapProjectFromCwd: Option.none(),
747 logWebSocketEvents: Option.none(),
748 tailscaleServeEnabled: Option.none(),
749 tailscaleServePort: Option.none(),
750 },
751 Option.none(),
752 ).pipe(
753 Effect.provide(
754 Layer.mergeAll(
755 ConfigProvider.layer(
756 ConfigProvider.fromEnv({
757 env: {
758 T3CODE_OTLP_HEADERS: "authorization=Basic%20abc%3D%3D,x-tenant=t3",
759 },
760 }),
761 ),
762 NetService.layer,
763 ),
764 ),
765 );
766​
767 expect(resolved.otlpHeaders).toEqual({
768 authorization: "Basic abc==",
769 "x-tenant": "t3",
770 });
771 }),
772 );
773​
774 it.effect("keeps whitespace-separated pairs and literal equals signs in OTLP headers", () =>
775 Effect.gen(function* () {
776 const { join } = yield* Path.Path;
777 const baseDir = join(NodeOS.tmpdir(), "t3-cli-config-otlp-headers-loose-base");
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 ).pipe(
796 Effect.provide(
797 Layer.mergeAll(
798 ConfigProvider.layer(
799 ConfigProvider.fromEnv({
800 env: {
801 T3CODE_OTLP_HEADERS: "authorization=Bearer abc==, x-tenant=t3",
802 T3CODE_OTLP_TRACES_URL: "http://collector.internal:4318",
803 },
804 }),
805 ),
806 NetService.layer,
807 ),
808 ),
809 );
810​
811 expect(resolved.otlpHeaders).toEqual({
812 authorization: "Bearer abc==",
813 "x-tenant": "t3",
814 });
815 expect(resolved.otlpTracesUrl).toBe("http://collector.internal:4318");
816 }),
817 );
818​
819 it.effect("reads the OTLP protocol from env", () =>
820 Effect.gen(function* () {
821 const { join } = yield* Path.Path;
822 const baseDir = join(NodeOS.tmpdir(), "t3-cli-config-otlp-protocol-base");
823​
824 const resolved = yield* resolveServerConfig(
825 {
826 mode: Option.some("web"),
827 port: Option.some(3773),
828 host: Option.none(),
829 baseDir: Option.some(baseDir),
830 cwd: Option.none(),
831 devUrl: Option.none(),
832 noBrowser: Option.none(),
833 bootstrapFd: Option.none(),
834 autoBootstrapProjectFromCwd: Option.none(),
835 logWebSocketEvents: Option.none(),
836 tailscaleServeEnabled: Option.none(),
837 tailscaleServePort: Option.none(),
838 },
839 Option.none(),
840 ).pipe(
841 Effect.provide(
842 Layer.mergeAll(
843 ConfigProvider.layer(
844 ConfigProvider.fromEnv({ env: { T3CODE_OTLP_PROTOCOL: "http/protobuf" } }),
845 ),
846 NetService.layer,
847 ),
848 ),
849 );
850​
851 expect(resolved.otlpProtocol).toBe("http/protobuf");
852 }),
853 );
854​
855 it.effect("reads the OTLP logs URL from env", () =>
856 Effect.gen(function* () {
857 const { join } = yield* Path.Path;
858 const baseDir = join(NodeOS.tmpdir(), "t3-cli-config-otlp-logs-url-base");
859​
860 const resolved = yield* resolveServerConfig(
861 {
862 mode: Option.some("web"),
863 port: Option.some(3773),
864 host: Option.none(),
865 baseDir: Option.some(baseDir),
866 cwd: Option.none(),
867 devUrl: Option.none(),
868 noBrowser: Option.none(),
869 bootstrapFd: Option.none(),
870 autoBootstrapProjectFromCwd: Option.none(),
871 logWebSocketEvents: Option.none(),
872 tailscaleServeEnabled: Option.none(),
873 tailscaleServePort: Option.none(),
874 },
875 Option.none(),
876 ).pipe(
877 Effect.provide(
878 Layer.mergeAll(
879 ConfigProvider.layer(
880 ConfigProvider.fromEnv({
881 env: { T3CODE_OTLP_LOGS_URL: "http://collector.internal:4318/v1/logs" },
882 }),
883 ),
884 NetService.layer,
885 ),
886 ),
887 );
888​
889 expect(resolved.otlpLogsUrl).toBe("http://collector.internal:4318/v1/logs");
890 }),
891 );
892});