|
| 1 | +import { HostProcessEnvironment } from "@t3tools/shared/hostProcess"; |
| 2 | +import { assert, describe, it } from "@effect/vitest"; |
| 3 | +import * as Effect from "effect/Effect"; |
| 4 | +import * as Exit from "effect/Exit"; |
| 5 | +import * as Layer from "effect/Layer"; |
| 6 | + |
| 7 | +import { formatEnvValidationTable, validateServerEnvironment } from "./envValidation.ts"; |
| 8 | + |
| 9 | +const runValidation = (env: Record<string, string>) => |
| 10 | + validateServerEnvironment.pipe(Effect.provide(Layer.succeed(HostProcessEnvironment, env))); |
| 11 | + |
| 12 | +const rowFor = <Row extends { variable: string }>( |
| 13 | + rows: ReadonlyArray<Row>, |
| 14 | + variable: string, |
| 15 | +): Row | undefined => rows.find((row) => row.variable === variable); |
| 16 | + |
| 17 | +describe("server environment validation", () => { |
| 18 | + it.effect("documents defaults for every optional variable on a clean environment", () => |
| 19 | + Effect.gen(function* () { |
| 20 | + const rows = yield* runValidation({}); |
| 21 | + assert.ok(rows.length >= 20, `expected many rows, got ${rows.length}`); |
| 22 | + assert.equal(rows.filter((row) => row.status === "invalid").length, 0); |
| 23 | + assert.equal(rows.filter((row) => row.status === "missing").length, 0); |
| 24 | + const port = rowFor(rows, "T3CODE_PORT"); |
| 25 | + assert.ok(port); |
| 26 | + assert.include(port.received ?? "", "(unset)"); |
| 27 | + const protocol = rowFor(rows, "T3CODE_OTLP_PROTOCOL"); |
| 28 | + assert.ok(protocol); |
| 29 | + assert.include(protocol.received ?? "", "(default: http/json)"); |
| 30 | + }), |
| 31 | + ); |
| 32 | + |
| 33 | + it.effect("flags an invalid T3CODE_PORT with the received value", () => |
| 34 | + Effect.gen(function* () { |
| 35 | + const error = yield* runValidation({ T3CODE_PORT: "not-a-port" }).pipe(Effect.flip); |
| 36 | + assert.strictEqual(error._tag, "ServerEnvValidationError"); |
| 37 | + const row = rowFor(error.rows, "T3CODE_PORT"); |
| 38 | + assert.ok(row, "expected a T3CODE_PORT failure row"); |
| 39 | + assert.strictEqual(row.status, "invalid"); |
| 40 | + assert.include(row.received ?? "", "not-a-port"); |
| 41 | + assert.include(row.expected, "1-65535"); |
| 42 | + }), |
| 43 | + ); |
| 44 | + |
| 45 | + it.effect("flags an out-of-range port", () => |
| 46 | + Effect.gen(function* () { |
| 47 | + const error = yield* runValidation({ T3CODE_PORT: "99999" }).pipe(Effect.flip); |
| 48 | + const row = rowFor(error.rows, "T3CODE_PORT"); |
| 49 | + assert.ok(row); |
| 50 | + assert.strictEqual(row.status, "invalid"); |
| 51 | + }), |
| 52 | + ); |
| 53 | + |
| 54 | + it.effect("flags an invalid T3CODE_MODE literal", () => |
| 55 | + Effect.gen(function* () { |
| 56 | + const error = yield* runValidation({ T3CODE_MODE: "space-shuttle" }).pipe(Effect.flip); |
| 57 | + const row = rowFor(error.rows, "T3CODE_MODE"); |
| 58 | + assert.ok(row, "expected a T3CODE_MODE failure row"); |
| 59 | + assert.strictEqual(row.status, "invalid"); |
| 60 | + assert.strictEqual(row.expected, "web | desktop"); |
| 61 | + }), |
| 62 | + ); |
| 63 | + |
| 64 | + it.effect("flags a malformed OTLP traces URL", () => |
| 65 | + Effect.gen(function* () { |
| 66 | + const error = yield* runValidation({ T3CODE_OTLP_TRACES_URL: "not a url" }).pipe(Effect.flip); |
| 67 | + const row = rowFor(error.rows, "T3CODE_OTLP_TRACES_URL"); |
| 68 | + assert.ok(row); |
| 69 | + assert.strictEqual(row.status, "invalid"); |
| 70 | + }), |
| 71 | + ); |
| 72 | + |
| 73 | + it.effect("flags an invalid boolean and an invalid integer", () => |
| 74 | + Effect.gen(function* () { |
| 75 | + const error = yield* runValidation({ |
| 76 | + T3CODE_NO_BROWSER: "maybe", |
| 77 | + T3CODE_TRACE_MAX_FILES: "ten", |
| 78 | + }).pipe(Effect.flip); |
| 79 | + const noBrowser = rowFor(error.rows, "T3CODE_NO_BROWSER"); |
| 80 | + assert.ok(noBrowser); |
| 81 | + assert.strictEqual(noBrowser.status, "invalid"); |
| 82 | + const traceMaxFiles = rowFor(error.rows, "T3CODE_TRACE_MAX_FILES"); |
| 83 | + assert.ok(traceMaxFiles); |
| 84 | + assert.strictEqual(traceMaxFiles.status, "invalid"); |
| 85 | + }), |
| 86 | + ); |
| 87 | + |
| 88 | + it.effect("accepts valid web mode settings with received values", () => |
| 89 | + Effect.gen(function* () { |
| 90 | + const rows = yield* runValidation({ |
| 91 | + T3CODE_MODE: "web", |
| 92 | + T3CODE_PORT: "3773", |
| 93 | + T3CODE_HOST: "127.0.0.1", |
| 94 | + T3CODE_LOG_LEVEL: "Debug", |
| 95 | + }); |
| 96 | + assert.equal(rows.filter((row) => row.status !== "ok").length, 0); |
| 97 | + const port = rowFor(rows, "T3CODE_PORT"); |
| 98 | + assert.ok(port); |
| 99 | + assert.strictEqual(port.received, "3773"); |
| 100 | + }), |
| 101 | + ); |
| 102 | + |
| 103 | + it.effect("flags a too-short dev auth token as invalid", () => |
| 104 | + Effect.gen(function* () { |
| 105 | + const error = yield* runValidation({ T3CODE_DEV_AUTH_TOKEN: "short" }).pipe(Effect.flip); |
| 106 | + const row = rowFor(error.rows, "T3CODE_DEV_AUTH_TOKEN"); |
| 107 | + assert.ok(row, "expected a T3CODE_DEV_AUTH_TOKEN failure row"); |
| 108 | + assert.strictEqual(row.status, "invalid"); |
| 109 | + assert.include(row.expected, "32"); |
| 110 | + }), |
| 111 | + ); |
| 112 | + |
| 113 | + it.effect("redacts secret-looking variables in the received column", () => |
| 114 | + Effect.gen(function* () { |
| 115 | + const error = yield* runValidation({ |
| 116 | + T3CODE_DEV_AUTH_TOKEN: "short", |
| 117 | + // Valid pairs plus one malformed pair -> row surfaces as invalid, and |
| 118 | + // the raw secret-bearing value must never reach the rendered table. |
| 119 | + T3CODE_OTLP_HEADERS: "authorization=super-secret-value, bad-pair-no-equals", |
| 120 | + }).pipe(Effect.flip); |
| 121 | + const tokenRow = rowFor(error.rows, "T3CODE_DEV_AUTH_TOKEN"); |
| 122 | + assert.ok(tokenRow); |
| 123 | + assert.ok((tokenRow.received ?? "").includes("<redacted>")); |
| 124 | + assert.ok(!(tokenRow.received ?? "").includes("short")); |
| 125 | + const headersRow = rowFor(error.rows, "T3CODE_OTLP_HEADERS"); |
| 126 | + assert.ok(headersRow); |
| 127 | + assert.ok(!(headersRow.received ?? "").includes("super-secret-value")); |
| 128 | + }), |
| 129 | + ); |
| 130 | + |
| 131 | + it.effect("renders a table with header, rule and status lines for failures", () => |
| 132 | + Effect.gen(function* () { |
| 133 | + const error = yield* runValidation({ T3CODE_PORT: "70000" }).pipe(Effect.flip); |
| 134 | + const table = formatEnvValidationTable( |
| 135 | + error.rows.map((row) => ({ ...row, received: row.received ?? "" })), |
| 136 | + ); |
| 137 | + assert.include(table, "VARIABLE"); |
| 138 | + assert.include(table, "STATUS"); |
| 139 | + assert.include(table, "EXPECTED"); |
| 140 | + assert.include(table, "RECEIVED"); |
| 141 | + assert.include(table, "DESCRIPTION"); |
| 142 | + assert.include(table, "T3CODE_PORT"); |
| 143 | + assert.include(table, "INVALID"); |
| 144 | + assert.include(table, "+--"); |
| 145 | + }), |
| 146 | + ); |
| 147 | + |
| 148 | + it.effect("validation error carries only failing rows and a summary", () => |
| 149 | + Effect.gen(function* () { |
| 150 | + const error = yield* runValidation({ |
| 151 | + T3CODE_MODE: "nope", |
| 152 | + T3CODE_PORT: "abc", |
| 153 | + }).pipe(Effect.flip); |
| 154 | + assert.strictEqual(error._tag, "ServerEnvValidationError"); |
| 155 | + assert.equal(error.rows.length, 2); |
| 156 | + assert.ok(error.rows.every((row) => row.status === "missing" || row.status === "invalid")); |
| 157 | + assert.include(error.summary, "0 missing"); |
| 158 | + assert.include(error.summary, "2 invalid"); |
| 159 | + }), |
| 160 | + ); |
| 161 | + |
| 162 | + it.effect("success path exits cleanly, failure path exits non-zero", () => |
| 163 | + Effect.gen(function* () { |
| 164 | + const ok = Exit.isFailure(yield* Effect.exit(runValidation({}))); |
| 165 | + assert.equal(ok, false); |
| 166 | + const failed = Exit.isFailure( |
| 167 | + yield* Effect.exit(runValidation({ T3CODE_PORT: "not-a-port" })), |
| 168 | + ); |
| 169 | + assert.equal(failed, true); |
| 170 | + }), |
| 171 | + ); |
| 172 | +}); |
0 commit comments