import { describe, expect, test } from "vitest"; import { validateBooleanFlag, validateOptionalSecretString, validateOptionalSecretUrl, validateRequiredSecretString } from "./secret-validator.js"; const SECRET_VALUE = "redis://:super-secret-password@redis.example.com:6379/0"; const SENTINEL_INVALID_URL = "redis://:sentinel-invalid-url-secret@"; const SENTINEL_INVALID_PROTOCOL_URL = SECRET_VALUE.replace("redis://", "https://"); const SENTINEL_INVALID_BOOLEAN = "sentinel-invalid-boolean-secret"; const SENTINEL_MISSING_REQUIRED = "sentinel-missing-required-secret"; function expectRedacted(payload: unknown, sentinels: readonly string[] = [SECRET_VALUE, "super-secret-password"]) { const serialized = JSON.stringify(payload); for (const sentinel of sentinels) { expect(serialized).not.toContain(sentinel); } } describe("secret validator", () => { test("reports missing required values with catalog classification", () => { const result = validateRequiredSecretString("MAILJET_API_KEY", undefined); expect(result).toEqual({ ok: false, configured: false, diagnostics: [ { code: "missing-required", name: "MAILJET_API_KEY", classification: "optional-provider", message: "MAILJET_API_KEY is required.", }, ], }); expectRedacted(result, [SENTINEL_MISSING_REQUIRED]); }); test("classifies dynamic site keys while leaving unknown names unclassified", () => { expect(validateRequiredSecretString("SITE_KEY_EXAMPLE_SITE", undefined)).toEqual({ ok: false, configured: false, diagnostics: [ { code: "missing-required", name: "SITE_KEY_EXAMPLE_SITE", classification: "optional-provider", message: "SITE_KEY_EXAMPLE_SITE is required.", }, ], }); expect(validateRequiredSecretString("UNKNOWN_SECRET_NAME", undefined)).toEqual({ ok: false, configured: false, diagnostics: [ { code: "missing-required", name: "UNKNOWN_SECRET_NAME", message: "UNKNOWN_SECRET_NAME is required.", }, ], }); }); test("treats whitespace-only required secrets as missing and trims valid strings", () => { expect(validateRequiredSecretString("MAILJET_API_KEY", " ")).toMatchObject({ ok: false, configured: false, diagnostics: [expect.objectContaining({ code: "missing-required" })], }); expect(validateRequiredSecretString("MAILJET_API_KEY", " mailer-key ")).toMatchObject({ ok: true, configured: true, value: "mailer-key", diagnostics: [], }); }); test("normalizes optional strings without treating empty input as configured", () => { expect(validateOptionalSecretString("MAILJET_FROM_NAME", " ")).toEqual({ ok: true, configured: false, value: undefined, diagnostics: [], }); expect(validateOptionalSecretString("MAILJET_FROM_NAME", " Support ")).toMatchObject({ ok: true, configured: true, value: "Support", }); }); test("validates optional URLs and rejects invalid protocol without leaking values", () => { const invalidUrl = validateOptionalSecretUrl("BULLMQ_REDIS_URL", SENTINEL_INVALID_URL, { allowedProtocols: ["redis", "rediss"] }); const invalidProtocol = validateOptionalSecretUrl("BULLMQ_REDIS_URL", SENTINEL_INVALID_PROTOCOL_URL, { allowedProtocols: ["redis", "rediss"] }); expect(invalidUrl).toMatchObject({ ok: false, configured: true, diagnostics: [expect.objectContaining({ code: "invalid-url", name: "BULLMQ_REDIS_URL", classification: "optional-provider" })], }); expect(invalidProtocol).toMatchObject({ ok: false, configured: true, diagnostics: [expect.objectContaining({ code: "invalid-url-protocol", name: "BULLMQ_REDIS_URL" })], }); expectRedacted([invalidUrl, invalidProtocol], [SENTINEL_INVALID_URL, SENTINEL_INVALID_PROTOCOL_URL, "super-secret-password"]); }); test("parses boolean flags and rejects unrelated boolean strings", () => { expect(validateBooleanFlag("WORKER_ENABLED", " true ")).toMatchObject({ ok: true, configured: true, value: true }); expect(validateBooleanFlag("WORKER_ENABLED", "0")).toMatchObject({ ok: true, configured: true, value: false }); expect(validateBooleanFlag("WORKER_ENABLED", undefined, true)).toMatchObject({ ok: true, configured: false, value: true }); const invalid = validateBooleanFlag("WORKER_ENABLED", SENTINEL_INVALID_BOOLEAN); expect(invalid).toMatchObject({ ok: false, configured: true, diagnostics: [expect.objectContaining({ code: "invalid-boolean", name: "WORKER_ENABLED", classification: "optional-provider" })], }); expectRedacted(invalid, [SENTINEL_INVALID_BOOLEAN]); }); });