Skip to content

Commit fcf16fe

Browse files
fix(server): address review findings in env validation
- Remove leftover self-referencing validateConfigCommand export that threw a ReferenceError at import time and broke every t3 invocation - Treat present-but-empty env values as invalid, matching live config behavior (T3CODE_PORT= no longer passes validation) - Restrict log-level variables to the exact literals Config.LogLevel accepts (All, Fatal, Error, Warn, Info, Debug, Trace, None) - Accept the same boolean literals as Config.Boolean (true/yes/on/1/y, false/no/off/0/n) instead of only true/false - Align table rule width with row rendering (" | " joins) - Extend test suite to 14 tests covering the corrected behaviors 🤖 Generated with Codebuff Co-Authored-By: Codebuff <[email protected]>
1 parent aba4144 commit fcf16fe

3 files changed

Lines changed: 91 additions & 19 deletions

File tree

‎apps/server/src/cli/envValidation.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,25 @@ describe("server environment validation", () => {
5151
}),
5252
);
5353

54+
it.effect("flags a present-but-empty value as invalid, matching live config", () =>
55+
Effect.gen(function* () {
56+
const error = yield* runValidation({ T3CODE_PORT: "" }).pipe(Effect.flip);
57+
const row = rowFor(error.rows, "T3CODE_PORT");
58+
assert.ok(row);
59+
assert.strictEqual(row.status, "invalid");
60+
}),
61+
);
62+
63+
it.effect("flags an unknown log level literal, matching live config", () =>
64+
Effect.gen(function* () {
65+
const error = yield* runValidation({ T3CODE_LOG_LEVEL: "Verbose" }).pipe(Effect.flip);
66+
const row = rowFor(error.rows, "T3CODE_LOG_LEVEL");
67+
assert.ok(row);
68+
assert.strictEqual(row.status, "invalid");
69+
assert.include(row.expected, "Warn");
70+
}),
71+
);
72+
5473
it.effect("flags an invalid T3CODE_MODE literal", () =>
5574
Effect.gen(function* () {
5675
const error = yield* runValidation({ T3CODE_MODE: "space-shuttle" }).pipe(Effect.flip);
@@ -92,6 +111,7 @@ describe("server environment validation", () => {
92111
T3CODE_PORT: "3773",
93112
T3CODE_HOST: "127.0.0.1",
94113
T3CODE_LOG_LEVEL: "Debug",
114+
T3CODE_NO_BROWSER: "1",
95115
});
96116
assert.equal(rows.filter((row) => row.status !== "ok").length, 0);
97117
const port = rowFor(rows, "T3CODE_PORT");

‎apps/server/src/cli/envValidation.ts‎

Lines changed: 71 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -52,25 +52,72 @@ export class ServerEnvValidationError extends Schema.TaggedError<ServerEnvValida
5252
}
5353
}
5454

55+
/**
56+
* Accepts exactly the values Effect's own `Config.Boolean` accepts
57+
* (`Schema.BooleanLiterals`): true/yes/on/1/y and false/no/off/0/n.
58+
*/
5559
const BooleanFromString = Schema.String.pipe(
5660
Schema.decodeTo(
5761
Schema.Boolean,
5862
SchemaTransformation.transformEffect({
59-
decode: (value: string) =>
60-
value === "true"
61-
? Effect.succeed(true)
62-
: value === "false"
63-
? Effect.succeed(false)
64-
: Effect.fail(
65-
new SchemaIssue.InvalidValue({
66-
message: `expected "true" or "false", received "${value}"`,
67-
}),
68-
),
63+
decode: (value: string) => {
64+
switch (value) {
65+
case "true":
66+
case "yes":
67+
case "on":
68+
case "1":
69+
case "y":
70+
return Effect.succeed(true);
71+
case "false":
72+
case "no":
73+
case "off":
74+
case "0":
75+
case "n":
76+
return Effect.succeed(false);
77+
default:
78+
return Effect.fail(
79+
new SchemaIssue.InvalidValue({
80+
message: `expected a boolean flag (true/yes/on/1/y or false/no/off/0/n), received "${value}"`,
81+
}),
82+
);
83+
}
84+
},
6985
encode: (value: boolean) => Effect.succeed(value ? "true" : "false"),
7086
}),
7187
),
7288
);
7389

90+
/**
91+
* Accepts exactly the literals Effect's own `Config.LogLevel` accepts
92+
* (`Schema.Literals(LogLevel.values)`).
93+
*/
94+
const LOG_LEVEL_VALUES = [
95+
"All",
96+
"Fatal",
97+
"Error",
98+
"Warn",
99+
"Info",
100+
"Debug",
101+
"Trace",
102+
"None",
103+
] as const;
104+
const LogLevelFromString = Schema.String.pipe(
105+
Schema.decodeTo(
106+
Schema.Literals(LOG_LEVEL_VALUES),
107+
SchemaTransformation.transformEffect({
108+
decode: (value: string) =>
109+
(LOG_LEVEL_VALUES as readonly string[]).includes(value)
110+
? Effect.succeed(value as (typeof LOG_LEVEL_VALUES)[number])
111+
: Effect.fail(
112+
new SchemaIssue.InvalidValue({
113+
message: `expected one of ${LOG_LEVEL_VALUES.join(", ")}, received "${value}"`,
114+
}),
115+
),
116+
encode: (value: (typeof LOG_LEVEL_VALUES)[number]) => Effect.succeed(value),
117+
}),
118+
),
119+
);
120+
74121
const IntFromString = Schema.String.pipe(
75122
Schema.decodeTo(
76123
Schema.Int,
@@ -154,14 +201,17 @@ const stringSpec = (
154201
});
155202

156203
export const serverEnvSpecs: ReadonlyArray<ServerEnvVarSpec> = [
157-
stringSpec("T3CODE_LOG_LEVEL", "log level (e.g. Error, Info, Debug)", "Server log verbosity.", {
158-
defaultText: "Info",
159-
}),
204+
stringSpec(
205+
"T3CODE_LOG_LEVEL",
206+
"log level (All, Fatal, Error, Warn, Info, Debug, Trace, None)",
207+
"Server log verbosity.",
208+
{ schema: LogLevelFromString, defaultText: "Info" },
209+
),
160210
stringSpec(
161211
"T3CODE_TRACE_MIN_LEVEL",
162-
"log level (e.g. Error, Info, Debug)",
212+
"log level (All, Fatal, Error, Warn, Info, Debug, Trace, None)",
163213
"Minimum level for trace output.",
164-
{ defaultText: "Info" },
214+
{ schema: LogLevelFromString, defaultText: "Info" },
165215
),
166216
stringSpec("T3CODE_TRACE_TIMING_ENABLED", "boolean", "Include timing data in traces.", {
167217
schema: BooleanFromString,
@@ -343,7 +393,9 @@ export const formatEnvValidationTable = (rows: ReadonlyArray<ServerEnvVariableRo
343393
}
344394
return width;
345395
});
346-
const rule = `+-${widths.map((width) => "-".repeat(width)).join("-+--")}-+`;
396+
// Row cells join with " | " (3 chars), so rule boundaries join with "-+-"
397+
// to keep the '+' centered under each '|'.
398+
const rule = `+-${widths.map((width) => "-".repeat(width)).join("-+-")}-+`;
347399
const formatLine = (cells: readonly string[]) =>
348400
`| ${cells.map((cell, index) => cell.padEnd(widths[index] ?? cell.length)).join(" | ")} |`;
349401

@@ -383,7 +435,9 @@ export const validateServerEnvironment = Effect.gen(function* () {
383435
for (const spec of serverEnvSpecs) {
384436
const raw = env[spec.variable];
385437
const value = typeof raw === "string" ? raw : undefined;
386-
if (value === undefined || value.trim().length === 0) {
438+
// A present-but-empty value must reach schema validation, matching live
439+
// config behavior where `T3CODE_PORT=` is invalid rather than defaulted.
440+
if (value === undefined || (spec.required && value.trim().length === 0)) {
387441
rows.push({
388442
variable: spec.variable,
389443
expected: spec.expected,

‎apps/server/src/cli/server.ts‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ export const startCommand = Command.make("start", { ...sharedServerCommandFlags
3030
Command.withHandler((flags) => runServerCommand(flags)),
3131
);
3232

33-
export const validateConfigCommand = validateConfigCommand;
34-
3533
export const serveCommand = Command.make("serve", { ...sharedServerCommandFlags }).pipe(
3634
Command.withDescription(
3735
"Run the T3 Code server without opening a browser and print headless pairing details.",

0 commit comments

Comments
 (0)