Files
HealthLog/scripts/__tests__/check-env.test.ts
T
Marc-André Bombeck 42a4232691 feat(ops): add pre-deploy env-var sanity check
Adds `pnpm check-env` — a CLI tool that reads
`scripts/env-manifest.json` and verifies every required env var is
present + non-empty. Exit code 0 on green, 1 on missing required vars,
2 on a malformed manifest.

Two modes:

  pnpm check-env                # against process.env
  pnpm check-env --file <path>  # against a Coolify export / .env.production

The manifest is JSON so it's git-trackable and reviewable. Groups can
be marked `required: true` (boot-blocking) or `required: false`
(silently disable a feature). An `allOrNone: true` group emits a
synthetic <all-or-none> required row when partially populated — this
is the v1.4.40 AP-2 silent-disable pattern (three of four APNS_* set,
.p8 missing) made into a hard failure.

Documents the workflow in docs/ops/env-check.md including the planned
v1.4.43 CI integration. 16 unit tests pin parseEnvFile + checkEnv;
renderResults stays untested as a thin shell over the pure helpers.

This is a CLI tool, not a runtime gate — the app still boots without
this check having run. The point is to make the operator-visible step
between "Coolify env block edited" and "deploy fires" loud about
missing vars before the deploy ships.
2026-05-21 19:01:15 +02:00

207 lines
6.3 KiB
TypeScript

/**
* v1.4.42 W6 — env-check sanity tests.
*
* The script's two pure helpers (`parseEnvFile` + `checkEnv`) are
* pinned here. The renderResults / main() entrypoint is intentionally
* not unit-tested — it's a thin shell over the pure helpers and the
* exit-code behaviour is best left to a smoke test in CI (deferred to
* v1.4.43).
*/
import { describe, expect, it } from "vitest";
import { checkEnv, parseEnvFile } from "../check-env";
describe("parseEnvFile", () => {
it("parses simple KEY=VALUE pairs", () => {
expect(parseEnvFile("FOO=bar\nBAZ=qux")).toEqual({
FOO: "bar",
BAZ: "qux",
});
});
it("strips surrounding double quotes", () => {
expect(parseEnvFile('FOO="bar baz"')).toEqual({ FOO: "bar baz" });
});
it("strips surrounding single quotes", () => {
expect(parseEnvFile("FOO='bar baz'")).toEqual({ FOO: "bar baz" });
});
it("skips comments and blank lines", () => {
const src = `# header comment
FOO=bar
# another
BAZ=qux
`;
expect(parseEnvFile(src)).toEqual({ FOO: "bar", BAZ: "qux" });
});
it("preserves an empty value (the v1.4.40 .p8 gap pattern)", () => {
// An empty string MUST round-trip as "" so the presence-check
// downstream classifies it as missing.
expect(parseEnvFile("APNS_KEY=")).toEqual({ APNS_KEY: "" });
});
it("handles CRLF line endings (Coolify exports use \\r\\n)", () => {
expect(parseEnvFile("FOO=bar\r\nBAZ=qux")).toEqual({
FOO: "bar",
BAZ: "qux",
});
});
});
const SAMPLE_MANIFEST = {
description: "test manifest",
groups: [
{
name: "Core",
description: "required boot vars",
required: true,
variables: [
{ name: "DATABASE_URL", purpose: "db" },
{ name: "ENCRYPTION_KEY", purpose: "crypto" },
],
},
{
name: "APNs",
description: "iOS push",
required: false,
variables: [
{ name: "APNS_KEY_ID", purpose: "key id" },
{ name: "APNS_TEAM_ID", purpose: "team id" },
{
name: "APNS_KEY",
purpose: "key body or file",
anyOf: ["APNS_KEY", "APNS_KEY_FILE"],
},
],
},
{
name: "Backups",
description: "all-or-none",
required: false,
allOrNone: true,
variables: [
{ name: "BACKUP_S3_BUCKET", purpose: "bucket" },
{ name: "BACKUP_S3_ACCESS_KEY", purpose: "key" },
],
},
],
};
describe("checkEnv — required group", () => {
it("flags every missing required var", () => {
const out = checkEnv(SAMPLE_MANIFEST, {});
const required = out.filter((r) => r.required && !r.present);
expect(required.map((r) => r.variable)).toEqual([
"DATABASE_URL",
"ENCRYPTION_KEY",
]);
});
it("classifies whitespace-only values as missing (deploy-failure prevention)", () => {
// A `DATABASE_URL=" "` would silently boot with a useless value;
// the deployer expects to see a clear MISSING-REQUIRED row here.
const out = checkEnv(SAMPLE_MANIFEST, {
DATABASE_URL: " ",
ENCRYPTION_KEY: "ok",
});
const row = out.find((r) => r.variable === "DATABASE_URL")!;
expect(row.present).toBe(false);
expect(row.required).toBe(true);
});
it("greenlights when every required var is non-empty", () => {
const out = checkEnv(SAMPLE_MANIFEST, {
DATABASE_URL: "postgresql://x",
ENCRYPTION_KEY: "deadbeef".repeat(8),
});
const required = out.filter((r) => r.required);
expect(required.every((r) => r.present)).toBe(true);
});
});
describe("checkEnv — anyOf alternatives", () => {
it("treats APNS_KEY as satisfied when APNS_KEY_FILE is set instead", () => {
const out = checkEnv(SAMPLE_MANIFEST, {
DATABASE_URL: "x",
ENCRYPTION_KEY: "y",
APNS_KEY_ID: "abc",
APNS_TEAM_ID: "def",
APNS_KEY_FILE: "/secrets/apns.p8",
});
const apnsKey = out.find((r) => r.variable === "APNS_KEY")!;
expect(apnsKey.present).toBe(true);
});
it("treats APNS_KEY as satisfied when APNS_KEY is set (the 12-factor variant)", () => {
const out = checkEnv(SAMPLE_MANIFEST, {
DATABASE_URL: "x",
ENCRYPTION_KEY: "y",
APNS_KEY_ID: "abc",
APNS_TEAM_ID: "def",
APNS_KEY: "-----BEGIN PRIVATE KEY-----...",
});
expect(out.find((r) => r.variable === "APNS_KEY")?.present).toBe(true);
});
it("marks APNS_KEY missing when BOTH alternatives are absent (the v1.4.40 AP-2 gap)", () => {
const out = checkEnv(SAMPLE_MANIFEST, {
DATABASE_URL: "x",
ENCRYPTION_KEY: "y",
APNS_KEY_ID: "abc",
APNS_TEAM_ID: "def",
});
expect(out.find((r) => r.variable === "APNS_KEY")?.present).toBe(false);
});
});
describe("checkEnv — all-or-none groups", () => {
it("emits a synthetic <all-or-none> required row when a group is half-populated", () => {
// The v1.4.40 AP-2 silent-disable pattern: 3 of 4 APNS_* set, .p8
// missing → app silently falls back to Telegram. The all-or-none
// synthetic row catches this even when the surrounding group is
// `required: false`.
const out = checkEnv(SAMPLE_MANIFEST, {
DATABASE_URL: "x",
ENCRYPTION_KEY: "y",
BACKUP_S3_BUCKET: "my-bucket",
// BACKUP_S3_ACCESS_KEY deliberately absent
});
const synth = out.find((r) => r.variable === "<all-or-none>")!;
expect(synth).toBeDefined();
expect(synth.required).toBe(true);
expect(synth.note).toContain("1/2");
});
it("does NOT emit the synthetic row when the group is fully set", () => {
const out = checkEnv(SAMPLE_MANIFEST, {
DATABASE_URL: "x",
ENCRYPTION_KEY: "y",
BACKUP_S3_BUCKET: "my-bucket",
BACKUP_S3_ACCESS_KEY: "key",
});
expect(out.find((r) => r.variable === "<all-or-none>")).toBeUndefined();
});
it("does NOT emit the synthetic row when the group is fully empty", () => {
const out = checkEnv(SAMPLE_MANIFEST, {
DATABASE_URL: "x",
ENCRYPTION_KEY: "y",
});
expect(out.find((r) => r.variable === "<all-or-none>")).toBeUndefined();
});
});
describe("checkEnv — optional groups (informational)", () => {
it("reports optional vars without setting required: true", () => {
const out = checkEnv(SAMPLE_MANIFEST, {
DATABASE_URL: "x",
ENCRYPTION_KEY: "y",
});
const apnsKeyId = out.find((r) => r.variable === "APNS_KEY_ID")!;
expect(apnsKeyId.present).toBe(false);
expect(apnsKeyId.required).toBe(false);
});
});