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

config.test.ts

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