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

config.test.ts

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