Open History to see the last commit
1import os from "node:os";
2
3import { assert, expect, it } from "@effect/vitest";
4import { ConfigProvider, Effect, FileSystem, Layer, Option, Path } from "effect";
5
6import { NetService } from "@t3tools/shared/Net";
7import * as NodeServices from "@effect/platform-node/NodeServices";
8import { deriveServerPaths } from "./config";
9import { resolveServerConfig } from "./cli";
10
11it.layer(NodeServices.layer)("cli config resolution", (it) => {
12 const openBootstrapFd = Effect.fn(function* (payload: Record<string, unknown>) {
13 const fs = yield* FileSystem.FileSystem;
14 const filePath = yield* fs.makeTempFileScoped({ prefix: "t3-bootstrap-", suffix: ".ndjson" });
15 yield* fs.writeFileString(filePath, `${JSON.stringify(payload)}\n`);
16 const { fd } = yield* fs.open(filePath, { flag: "r" });
17 return fd;
18 });
19
20 it.effect("falls back to effect/config values when flags are omitted", () =>
21 Effect.gen(function* () {
22 const { join } = yield* Path.Path;
23 const baseDir = join(os.tmpdir(), "t3-cli-config-env-base");
24 const derivedPaths = yield* deriveServerPaths(baseDir, new URL("http://127.0.0.1:5173"));
25 const resolved = yield* resolveServerConfig(
26 {
27 mode: Option.none(),
28 port: Option.none(),
29 host: Option.none(),
30 baseDir: Option.none(),
31 devUrl: Option.none(),
32 noBrowser: Option.none(),
33 authToken: Option.none(),
34 bootstrapFd: Option.none(),
35 autoBootstrapProjectFromCwd: Option.none(),
36 logWebSocketEvents: Option.none(),
37 },
38 Option.none(),
39 ).pipe(
40 Effect.provide(
41 Layer.mergeAll(
42 ConfigProvider.layer(
43 ConfigProvider.fromEnv({
44 env: {
45 T3CODE_LOG_LEVEL: "Warn",
46 T3CODE_MODE: "desktop",
47 T3CODE_PORT: "4001",
48 T3CODE_HOST: "0.0.0.0",
49 T3CODE_HOME: baseDir,
50 VITE_DEV_SERVER_URL: "http://127.0.0.1:5173",
51 T3CODE_NO_BROWSER: "true",
52 T3CODE_AUTH_TOKEN: "env-token",
53 T3CODE_AUTO_BOOTSTRAP_PROJECT_FROM_CWD: "false",
54 T3CODE_LOG_WS_EVENTS: "true",
55 },
56 }),
57 ),
58 NetService.layer,
59 ),
60 ),
61 );
62
63 expect(resolved).toEqual({
64 logLevel: "Warn",
65 mode: "desktop",
66 port: 4001,
67 cwd: process.cwd(),
68 baseDir,
69 ...derivedPaths,
70 host: "0.0.0.0",
71 staticDir: undefined,
72 devUrl: new URL("http://127.0.0.1:5173"),
73 noBrowser: true,
74 authToken: "env-token",
75 autoBootstrapProjectFromCwd: false,
76 logWebSocketEvents: true,
77 });
78 }),
79 );
80
81 it.effect("uses CLI flags when provided", () =>
82 Effect.gen(function* () {
83 const { join } = yield* Path.Path;
84 const baseDir = join(os.tmpdir(), "t3-cli-config-flags-base");
85 const derivedPaths = yield* deriveServerPaths(baseDir, new URL("http://127.0.0.1:4173"));
86 const resolved = yield* resolveServerConfig(
87 {
88 mode: Option.some("web"),
89 port: Option.some(8788),
90 host: Option.some("127.0.0.1"),
91 baseDir: Option.some(baseDir),
92 devUrl: Option.some(new URL("http://127.0.0.1:4173")),
93 noBrowser: Option.some(true),
94 authToken: Option.some("flag-token"),
95 bootstrapFd: Option.none(),
96 autoBootstrapProjectFromCwd: Option.some(true),
97 logWebSocketEvents: Option.some(true),
98 },
99 Option.some("Debug"),
100 ).pipe(
101 Effect.provide(
102 Layer.mergeAll(
103 ConfigProvider.layer(
104 ConfigProvider.fromEnv({
105 env: {
106 T3CODE_LOG_LEVEL: "Warn",
107 T3CODE_MODE: "desktop",
108 T3CODE_PORT: "4001",
109 T3CODE_HOST: "0.0.0.0",
110 T3CODE_HOME: join(os.tmpdir(), "ignored-base"),
111 VITE_DEV_SERVER_URL: "http://127.0.0.1:5173",
112 T3CODE_NO_BROWSER: "false",
113 T3CODE_AUTH_TOKEN: "ignored-token",
114 T3CODE_AUTO_BOOTSTRAP_PROJECT_FROM_CWD: "false",
115 T3CODE_LOG_WS_EVENTS: "false",
116 },
117 }),
118 ),
119 NetService.layer,
120 ),
121 ),
122 );
123
124 expect(resolved).toEqual({
125 logLevel: "Debug",
126 mode: "web",
127 port: 8788,
128 cwd: process.cwd(),
129 baseDir,
130 ...derivedPaths,
131 host: "127.0.0.1",
132 staticDir: undefined,
133 devUrl: new URL("http://127.0.0.1:4173"),
134 noBrowser: true,
135 authToken: "flag-token",
136 autoBootstrapProjectFromCwd: true,
137 logWebSocketEvents: true,
138 });
139 }),
140 );
141
142 it.effect("uses bootstrap envelope values as fallbacks when flags and env are absent", () =>
143 Effect.gen(function* () {
144 const { join } = yield* Path.Path;
145 const baseDir = "/tmp/t3-bootstrap-home";
146 const fd = yield* openBootstrapFd({
147 mode: "desktop",
148 port: 4888,
149 host: "127.0.0.2",
150 t3Home: baseDir,
151 devUrl: "http://127.0.0.1:5173",
152 noBrowser: true,
153 authToken: "bootstrap-token",
154 autoBootstrapProjectFromCwd: false,
155 logWebSocketEvents: true,
156 });
157 const derivedPaths = yield* deriveServerPaths(baseDir, new URL("http://127.0.0.1:5173"));
158
159 const resolved = yield* resolveServerConfig(
160 {
161 mode: Option.none(),
162 port: Option.none(),
163 host: Option.none(),
164 baseDir: Option.none(),
165 devUrl: Option.none(),
166 noBrowser: Option.none(),
167 authToken: Option.none(),
168 bootstrapFd: Option.none(),
169 autoBootstrapProjectFromCwd: Option.none(),
170 logWebSocketEvents: Option.none(),
171 },
172 Option.none(),
173 ).pipe(
174 Effect.provide(
175 Layer.mergeAll(
176 ConfigProvider.layer(
177 ConfigProvider.fromEnv({
178 env: {
179 T3CODE_BOOTSTRAP_FD: String(fd),
180 },
181 }),
182 ),
183 NetService.layer,
184 ),
185 ),
186 );
187
188 expect(resolved).toEqual({
189 logLevel: "Info",
190 mode: "desktop",
191 port: 4888,
192 cwd: process.cwd(),
193 baseDir,
194 ...derivedPaths,
195 host: "127.0.0.2",
196 staticDir: undefined,
197 devUrl: new URL("http://127.0.0.1:5173"),
198 noBrowser: true,
199 authToken: "bootstrap-token",
200 autoBootstrapProjectFromCwd: false,
201 logWebSocketEvents: true,
202 });
203 assert.equal(join(baseDir, "dev"), resolved.stateDir);
204 }),
205 );
206
207 it.effect("creates derived runtime directories during config resolution", () =>
208 Effect.gen(function* () {
209 const fs = yield* FileSystem.FileSystem;
210 const path = yield* Path.Path;
211 const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-cli-config-dirs-" });
212
213 const resolved = yield* resolveServerConfig(
214 {
215 mode: Option.some("desktop"),
216 port: Option.some(4888),
217 host: Option.none(),
218 baseDir: Option.some(baseDir),
219 devUrl: Option.some(new URL("http://127.0.0.1:5173")),
220 noBrowser: Option.none(),
221 authToken: Option.none(),
222 bootstrapFd: Option.none(),
223 autoBootstrapProjectFromCwd: Option.none(),
224 logWebSocketEvents: Option.none(),
225 },
226 Option.none(),
227 ).pipe(
228 Effect.provide(
229 Layer.mergeAll(
230 ConfigProvider.layer(ConfigProvider.fromEnv({ env: {} })),
231 NetService.layer,
232 ),
233 ),
234 );
235
236 for (const directory of [
237 resolved.stateDir,
238 resolved.logsDir,
239 resolved.providerLogsDir,
240 resolved.terminalLogsDir,
241 resolved.attachmentsDir,
242 resolved.worktreesDir,
243 path.dirname(resolved.serverLogPath),
244 ]) {
245 expect(yield* fs.exists(directory)).toBe(true);
246 }
247 }),
248 );
249
250 it.effect("applies flag then env precedence over bootstrap envelope values", () =>
251 Effect.gen(function* () {
252 const { join } = yield* Path.Path;
253 const baseDir = join(os.tmpdir(), "t3-cli-config-env-wins");
254 const fd = yield* openBootstrapFd({
255 mode: "desktop",
256 port: 4888,
257 host: "127.0.0.2",
258 t3Home: "/tmp/t3-bootstrap-home",
259 devUrl: "http://127.0.0.1:5173",
260 noBrowser: false,
261 authToken: "bootstrap-token",
262 autoBootstrapProjectFromCwd: false,
263 logWebSocketEvents: false,
264 });
265 const derivedPaths = yield* deriveServerPaths(baseDir, new URL("http://127.0.0.1:4173"));
266
267 const resolved = yield* resolveServerConfig(
268 {
269 mode: Option.none(),
270 port: Option.some(8788),
271 host: Option.some("127.0.0.1"),
272 baseDir: Option.none(),
273 devUrl: Option.some(new URL("http://127.0.0.1:4173")),
274 noBrowser: Option.none(),
275 authToken: Option.some("flag-token"),
276 bootstrapFd: Option.none(),
277 autoBootstrapProjectFromCwd: Option.none(),
278 logWebSocketEvents: Option.none(),
279 },
280 Option.some("Debug"),
281 ).pipe(
282 Effect.provide(
283 Layer.mergeAll(
284 ConfigProvider.layer(
285 ConfigProvider.fromEnv({
286 env: {
287 T3CODE_MODE: "web",
288 T3CODE_BOOTSTRAP_FD: String(fd),
289 T3CODE_HOME: baseDir,
290 T3CODE_NO_BROWSER: "true",
291 T3CODE_AUTO_BOOTSTRAP_PROJECT_FROM_CWD: "true",
292 T3CODE_LOG_WS_EVENTS: "true",
293 },
294 }),
295 ),
296 NetService.layer,
297 ),
298 ),
299 );
300
301 expect(resolved).toEqual({
302 logLevel: "Debug",
303 mode: "web",
304 port: 8788,
305 cwd: process.cwd(),
306 baseDir,
307 ...derivedPaths,
308 host: "127.0.0.1",
309 staticDir: undefined,
310 devUrl: new URL("http://127.0.0.1:4173"),
311 noBrowser: true,
312 authToken: "flag-token",
313 autoBootstrapProjectFromCwd: true,
314 logWebSocketEvents: true,
315 });
316 }),
317 );
318});