import { describe, expect, test } from "vitest"; import { getEnvVariableDefinition, loadRequiredWebConfig, loadWebConfig, WebConfigError } from "./index.js"; const GDPR_EXPORT_SSE_C_KEY_BASE64 = "BwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwc="; const REQUIRED_PRODUCTION_ENV = { APP_NAME: "Production App", DATABASE_URL: "postgres://example.invalid/app", APP_BASE_DOMAIN: "example.com", WEB_APP_HOST: "app.example.com", ADMIN_APP_HOST: "admin.example.com", API_HOST: "api.example.com", PORTAL_APP_HOST: "portal.example.com", WEB_RUNTIME_SURFACE: "core", APP_REVISION: "0123456789abcdef0123456789abcdef01234567", SESSION_SECRET: "session-secret-value-prod-0123456789", AUDIT_CHAIN_SECRET: "audit-chain-secret-value-prod-0123456789", CRYPTO_SECRET: "crypto-secret-value-prod-0123456789", ADMIN_CONNECTION_MASTER_KEY: "admin-connection-key-prod-0123456789", API_ACCESS_TOKEN: "api-access-token-value-prod-0123456789", JWT_SECRET: "jwt-secret-value-prod-0123456789abcd", MCP_RESOURCE_URL: "https://mcp.example.com", MCP_AUTHORIZATION_SERVER_URL: "https://app.example.com", WEB_PUSH_VAPID_PUBLIC_KEY: "web-push-public-key-prod", STORAGE_ENDPOINT: "https://storage.example.com", STORAGE_REGION: "eu-central-1", STORAGE_BUCKET: "private-exports", STORAGE_ACCESS_KEY_ID: "storage-access-key", STORAGE_SECRET_ACCESS_KEY: "storage-secret-key", GDPR_EXPORT_SSE_C_KEY_BASE64, REDIS_URL: "rediss://core-redis.example.test:6380/2", }; const PORTAL_PRODUCTION_ENV = { ...REQUIRED_PRODUCTION_ENV, WEB_RUNTIME_SURFACE: "portal", SESSION_SECRET: undefined, AUDIT_CHAIN_SECRET: undefined, WEB_APP_HOST: undefined, ADMIN_APP_HOST: undefined, API_HOST: undefined, CRYPTO_SECRET: undefined, API_ACCESS_TOKEN: undefined, JWT_SECRET: undefined, WEB_PUSH_VAPID_PUBLIC_KEY: undefined, PORTAL_SESSION_SECRET: "portal-session-secret-value-prod-0123456789", AUTH_MAGIC_LINK_ENABLED: "true", AUTH_MAGIC_LINK_TOKEN_TTL_MINUTES: "15", CAPTCHA_PROVIDER: "mcaptcha", MCAPTCHA_INSTANCE_URL: "https://captcha.example.test", MCAPTCHA_SITEKEY_LOGIN: "portal-login-sitekey", MCAPTCHA_ACCOUNT_SECRET: "portal-mcaptcha-account-secret", REDIS_URL: "rediss://portal-redis.example.test:6380/3", }; describe("loadRequiredWebConfig", () => { test("defaults enabled locales to de,en and gates catalog-ready review locales by environment", () => { expect(loadWebConfig({}, { nodeEnv: "development" }).config.i18n.enabledLocales).toEqual(["de", "en"]); expect(loadWebConfig({ I18N_ENABLED_LOCALES: "de,en,pl" }, { nodeEnv: "development" }).config.i18n.enabledLocales).toEqual(["de", "en", "pl"]); const production = loadWebConfig({ ...REQUIRED_PRODUCTION_ENV, I18N_ENABLED_LOCALES: "de,en,pl" }, { nodeEnv: "production" }); expect(production.ok).toBe(false); expect(production.diagnostics).toContainEqual(expect.objectContaining({ code: "invalid-enum", name: "I18N_ENABLED_LOCALES" })); const staging = loadWebConfig( { ...REQUIRED_PRODUCTION_ENV, SENTRY_ENVIRONMENT: "staging", I18N_ENABLED_LOCALES: "de,en,pl" }, { nodeEnv: "production" } ); expect(staging.ok).toBe(true); expect(staging.config.i18n.enabledLocales).toEqual(["de", "en", "pl"]); }); test.each(["de,en,en", "de,fr", "en", "de,,en"])('rejects invalid I18N_ENABLED_LOCALES value "%s"', (value) => { const result = loadWebConfig({ I18N_ENABLED_LOCALES: value }, { nodeEnv: "development" }); expect(result.ok).toBe(false); expect(result.diagnostics).toContainEqual(expect.objectContaining({ code: "invalid-enum", name: "I18N_ENABLED_LOCALES" })); }); test("returns production config when required values are valid", () => { const config = loadRequiredWebConfig( { ...REQUIRED_PRODUCTION_ENV, PORT: "8080", }, { nodeEnv: "production" } ); expect(config).toMatchObject({ appName: "Production App", nodeEnv: "production", runtimeSurface: "core", releaseRevision: "0123456789abcdef0123456789abcdef01234567", sessionSecret: "session-secret-value-prod-0123456789", listen: { port: 8080, }, routing: { appBaseDomain: "example.com", protocol: "https", }, }); }); test("requires a valid distributed Redis connection for core GDPR export download gates", () => { const missing = loadWebConfig({ ...REQUIRED_PRODUCTION_ENV, REDIS_URL: undefined }, { nodeEnv: "production" }); const invalid = loadWebConfig({ ...REQUIRED_PRODUCTION_ENV, REDIS_URL: "https://redis-secret-sentinel.example.test" }, { nodeEnv: "production" }); expect(missing.ok).toBe(false); expect(invalid.ok).toBe(false); if (missing.ok || invalid.ok) throw new Error("invalid production Redis fixtures unexpectedly passed"); expect(missing.diagnostics).toContainEqual(expect.objectContaining({ code: "missing-required", name: "REDIS_URL" })); expect(invalid.diagnostics).toContainEqual(expect.objectContaining({ code: "invalid-url", name: "REDIS_URL" })); expect(JSON.stringify([missing.diagnostics, invalid.diagnostics])).not.toContain("redis-secret-sentinel"); }); test("throws redacted WebConfigError for missing production SESSION_SECRET", () => { const secretValues = ["crypto-secret-value", "api-access-token-value", "jwt-secret-value"]; expect(() => loadRequiredWebConfig( { ...REQUIRED_PRODUCTION_ENV, SESSION_SECRET: undefined, }, { nodeEnv: "production" } ) ).toThrow(WebConfigError); try { loadRequiredWebConfig( { ...REQUIRED_PRODUCTION_ENV, SESSION_SECRET: undefined, }, { nodeEnv: "production" } ); } catch (error) { expect(error).toBeInstanceOf(WebConfigError); const webConfigError = error as WebConfigError; expect(webConfigError.diagnostics).toContainEqual( expect.objectContaining({ code: "missing-required", name: "SESSION_SECRET", }) ); const errorText = JSON.stringify({ message: webConfigError.message, details: webConfigError.details }); for (const secretValue of secretValues) { expect(errorText).not.toContain(secretValue); } } }); test("throws redacted WebConfigError for invalid PORT", () => { const secretValues = ["session-secret-value", "crypto-secret-value", "api-access-token-value", "jwt-secret-value"]; try { loadRequiredWebConfig( { ...REQUIRED_PRODUCTION_ENV, PORT: "secret-like-invalid-port-value", }, { nodeEnv: "production" } ); } catch (error) { expect(error).toBeInstanceOf(WebConfigError); const webConfigError = error as WebConfigError; expect(webConfigError.diagnostics).toEqual([ expect.objectContaining({ code: "invalid-port", name: "PORT", }), ]); const errorText = JSON.stringify({ message: webConfigError.message, details: webConfigError.details }); expect(errorText).not.toContain("secret-like-invalid-port-value"); for (const secretValue of secretValues) { expect(errorText).not.toContain(secretValue); } } }); test("requires the browser VAPID public key in production", () => { const result = loadWebConfig({ ...REQUIRED_PRODUCTION_ENV, WEB_PUSH_VAPID_PUBLIC_KEY: undefined }, { nodeEnv: "production" }); expect(result.ok).toBe(false); expect(result.diagnostics).toContainEqual(expect.objectContaining({ code: "missing-required", name: "WEB_PUSH_VAPID_PUBLIC_KEY" })); }); test("requires an explicit valid runtime surface in production", () => { const missing = loadWebConfig({ ...REQUIRED_PRODUCTION_ENV, WEB_RUNTIME_SURFACE: undefined }, { nodeEnv: "production" }); const invalid = loadWebConfig({ ...REQUIRED_PRODUCTION_ENV, WEB_RUNTIME_SURFACE: "customer" }, { nodeEnv: "production" }); expect(missing.diagnostics).toContainEqual(expect.objectContaining({ code: "missing-required", name: "WEB_RUNTIME_SURFACE" })); expect(invalid.diagnostics).toContainEqual(expect.objectContaining({ code: "invalid-enum", name: "WEB_RUNTIME_SURFACE" })); expect(JSON.stringify(invalid.diagnostics)).not.toContain("customer"); }); test("loads a portal production surface with only its own session secret", () => { const result = loadWebConfig(PORTAL_PRODUCTION_ENV, { nodeEnv: "production" }); expect(result.ok).toBe(true); expect(result.config).toMatchObject({ runtimeSurface: "portal", portalSessionSecret: "portal-session-secret-value-prod-0123456789", routing: { portalAppHost: "portal.example.com" }, }); expect(result.config.sessionSecret).toBeUndefined(); }); test("loads the isolated admin runtime with the core session authority", () => { const result = loadWebConfig( { ...REQUIRED_PRODUCTION_ENV, WEB_RUNTIME_SURFACE: "admin", }, { nodeEnv: "production" } ); expect(result.ok).toBe(true); expect(result.config).toMatchObject({ runtimeSurface: "admin", sessionSecret: "session-secret-value-prod-0123456789", routing: { adminAppHost: "admin.example.com" }, }); expect(result.config.portalSessionSecret).toBeUndefined(); }); test("requires HTTPS for a public production portal while allowing the loopback runtime smoke", () => { const publicHttp = loadWebConfig({ ...PORTAL_PRODUCTION_ENV, APP_PROTOCOL: "http" }, { nodeEnv: "production" }); const lookalikeHttp = loadWebConfig({ ...PORTAL_PRODUCTION_ENV, APP_PROTOCOL: "http", PORTAL_APP_HOST: "127.evil.com" }, { nodeEnv: "production" }); const loopbackHttp = loadWebConfig( { ...PORTAL_PRODUCTION_ENV, APP_BASE_DOMAIN: "localhost", PORTAL_APP_HOST: "portal.localhost", APP_PROTOCOL: "http", }, { nodeEnv: "production" } ); expect(publicHttp.diagnostics).toContainEqual(expect.objectContaining({ code: "invalid-enum", name: "APP_PROTOCOL" })); expect(lookalikeHttp.diagnostics).toContainEqual(expect.objectContaining({ code: "invalid-enum", name: "APP_PROTOCOL" })); expect(loopbackHttp.ok).toBe(true); }); test("requires the selected login CAPTCHA contract for the production portal only", () => { const missingProvider = loadWebConfig({ ...PORTAL_PRODUCTION_ENV, CAPTCHA_PROVIDER: undefined }, { nodeEnv: "production" }); const invalidProvider = loadWebConfig({ ...PORTAL_PRODUCTION_ENV, CAPTCHA_PROVIDER: "other" }, { nodeEnv: "production" }); const incompleteMcaptcha = loadWebConfig( { ...PORTAL_PRODUCTION_ENV, MCAPTCHA_INSTANCE_URL: undefined, MCAPTCHA_SITEKEY_LOGIN: undefined, MCAPTCHA_ACCOUNT_SECRET: undefined, }, { nodeEnv: "production" } ); expect(missingProvider.diagnostics).toContainEqual(expect.objectContaining({ code: "missing-required", name: "CAPTCHA_PROVIDER" })); expect(invalidProvider.diagnostics).toContainEqual(expect.objectContaining({ code: "invalid-enum", name: "CAPTCHA_PROVIDER" })); expect(incompleteMcaptcha.diagnostics).toEqual( expect.arrayContaining([ expect.objectContaining({ code: "missing-required", name: "MCAPTCHA_INSTANCE_URL" }), expect.objectContaining({ code: "missing-required", name: "MCAPTCHA_SITEKEY_LOGIN" }), expect.objectContaining({ code: "missing-required", name: "MCAPTCHA_ACCOUNT_SECRET" }), ]) ); expect(loadWebConfig(REQUIRED_PRODUCTION_ENV, { nodeEnv: "production" }).diagnostics).not.toEqual( expect.arrayContaining([expect.objectContaining({ name: expect.stringMatching(/^(?:CAPTCHA_PROVIDER|MCAPTCHA_|CAP_|REDIS_URL|BULLMQ_REDIS_URL)/u) })]) ); }); test("accepts Cap portal login verification with either its login secret or the shared fallback", () => { const capEnv = { ...PORTAL_PRODUCTION_ENV, CAPTCHA_PROVIDER: "cap", MCAPTCHA_INSTANCE_URL: undefined, MCAPTCHA_SITEKEY_LOGIN: undefined, MCAPTCHA_ACCOUNT_SECRET: undefined, CAP_INSTANCE_URL: "https://cap.example.test", CAP_SITEKEY_LOGIN: "portal-cap-login-sitekey", }; const loginSecret = loadWebConfig({ ...capEnv, CAP_SITE_SECRET_LOGIN: "cap-login-secret" }, { nodeEnv: "production" }); const sharedSecret = loadWebConfig({ ...capEnv, CAP_SITE_SECRET: "cap-shared-secret" }, { nodeEnv: "production" }); expect(loginSecret.ok).toBe(true); expect(sharedSecret.ok).toBe(true); }); test("rejects incomplete Cap login verification without leaking provider values", () => { const result = loadWebConfig( { ...PORTAL_PRODUCTION_ENV, CAPTCHA_PROVIDER: "cap", CAP_INSTANCE_URL: "http://captcha-secret-sentinel.example.test", CAP_SITEKEY_LOGIN: undefined, CAP_SITE_SECRET_LOGIN: undefined, CAP_SITE_SECRET: undefined, }, { nodeEnv: "production" } ); expect(result.diagnostics).toEqual( expect.arrayContaining([ expect.objectContaining({ code: "invalid-url", name: "CAP_INSTANCE_URL" }), expect.objectContaining({ code: "missing-required", name: "CAP_SITEKEY_LOGIN" }), expect.objectContaining({ code: "missing-required", name: "CAP_SITE_SECRET_LOGIN" }), ]) ); expect(JSON.stringify(result.diagnostics)).not.toContain("captcha-secret-sentinel"); }); test("requires a valid shared Redis connection and validates a preferred BullMQ override", () => { const missing = loadWebConfig({ ...PORTAL_PRODUCTION_ENV, REDIS_URL: undefined }, { nodeEnv: "production" }); const invalidShared = loadWebConfig({ ...PORTAL_PRODUCTION_ENV, REDIS_URL: "https://redis-secret-sentinel.example.test" }, { nodeEnv: "production" }); const invalidBullmq = loadWebConfig( { ...PORTAL_PRODUCTION_ENV, BULLMQ_REDIS_URL: "redis://portal-redis.example.test/not-a-database" }, { nodeEnv: "production" } ); expect(missing.diagnostics).toContainEqual(expect.objectContaining({ code: "missing-required", name: "REDIS_URL" })); expect(invalidShared.diagnostics).toContainEqual(expect.objectContaining({ code: "invalid-url", name: "REDIS_URL" })); expect(invalidBullmq.diagnostics).toContainEqual(expect.objectContaining({ code: "invalid-url", name: "BULLMQ_REDIS_URL" })); expect(JSON.stringify(invalidShared.diagnostics)).not.toContain("redis-secret-sentinel"); }); test("requires the dedicated session secret for an explicitly selected portal surface", () => { const result = loadWebConfig({ WEB_RUNTIME_SURFACE: "portal" }, { nodeEnv: "development" }); expect(result.diagnostics).toContainEqual( expect.objectContaining({ code: "missing-required", name: "PORTAL_SESSION_SECRET", }) ); }); test("forbids crossing core and portal session-secret boundaries", () => { const core = loadWebConfig({ ...REQUIRED_PRODUCTION_ENV, PORTAL_SESSION_SECRET: "portal-secret-value-prod-0123456789" }, { nodeEnv: "production" }); const portal = loadWebConfig( { ...REQUIRED_PRODUCTION_ENV, WEB_RUNTIME_SURFACE: "portal", PORTAL_SESSION_SECRET: "portal-secret-value-prod-0123456789", }, { nodeEnv: "production" } ); expect(core.diagnostics).toContainEqual(expect.objectContaining({ code: "forbidden", name: "PORTAL_SESSION_SECRET" })); expect(portal.diagnostics).toContainEqual(expect.objectContaining({ code: "forbidden", name: "SESSION_SECRET" })); }); test("requires a baked full Git SHA and the portal host in production", () => { const result = loadWebConfig({ ...REQUIRED_PRODUCTION_ENV, APP_REVISION: "main", PORTAL_APP_HOST: undefined }, { nodeEnv: "production" }); expect(result.diagnostics).toContainEqual(expect.objectContaining({ code: "invalid-revision", name: "APP_REVISION" })); expect(result.diagnostics).toContainEqual(expect.objectContaining({ code: "missing-required", name: "PORTAL_APP_HOST" })); }); test("rejects malformed or shared portal hosts", () => { const malformed = loadWebConfig({ ...REQUIRED_PRODUCTION_ENV, PORTAL_APP_HOST: "[invalid-host" }, { nodeEnv: "production" }); const shared = loadWebConfig({ ...REQUIRED_PRODUCTION_ENV, PORTAL_APP_HOST: "app.example.com" }, { nodeEnv: "production" }); const defaultPortAlias = loadWebConfig({ ...REQUIRED_PRODUCTION_ENV, PORTAL_APP_HOST: "app.example.com:443" }, { nodeEnv: "production" }); const derivedCoreHost = loadWebConfig( { ...REQUIRED_PRODUCTION_ENV, WEB_APP_HOST: undefined, PORTAL_APP_HOST: "app.example.com" }, { nodeEnv: "production" } ); expect(malformed.diagnostics).toContainEqual(expect.objectContaining({ code: "invalid-host", name: "PORTAL_APP_HOST" })); expect(shared.diagnostics).toContainEqual(expect.objectContaining({ code: "host-conflict", name: "PORTAL_APP_HOST" })); expect(defaultPortAlias.diagnostics).toContainEqual(expect.objectContaining({ code: "host-conflict", name: "PORTAL_APP_HOST" })); expect(derivedCoreHost.diagnostics).toContainEqual(expect.objectContaining({ code: "host-conflict", name: "PORTAL_APP_HOST" })); }); test("preserves default development behavior by throwing diagnostics instead of silently succeeding", () => { expect(() => loadRequiredWebConfig({})).toThrow(WebConfigError); }); }); describe("secret strength (production)", () => { test("rejects a present-but-too-short SESSION_SECRET in production", () => { const result = loadWebConfig({ ...REQUIRED_PRODUCTION_ENV, SESSION_SECRET: "too-short" }, { nodeEnv: "production" }); expect(result.ok).toBe(false); expect(result.diagnostics).toContainEqual(expect.objectContaining({ code: "weak-secret", name: "SESSION_SECRET" })); }); test("rejects reusing one value for SESSION_SECRET and CRYPTO_SECRET in production", () => { const shared = "shared-secret-value-prod-0123456789"; const result = loadWebConfig({ ...REQUIRED_PRODUCTION_ENV, SESSION_SECRET: shared, CRYPTO_SECRET: shared }, { nodeEnv: "production" }); expect(result.ok).toBe(false); expect(result.diagnostics).toContainEqual(expect.objectContaining({ code: "weak-secret", name: "CRYPTO_SECRET" })); }); test("does not leak the weak secret value in diagnostics", () => { const result = loadWebConfig({ ...REQUIRED_PRODUCTION_ENV, JWT_SECRET: "short-jwt-secret" }, { nodeEnv: "production" }); expect(JSON.stringify(result.diagnostics)).not.toContain("short-jwt-secret"); }); test("fails production web closed for missing or malformed GDPR export encryption", () => { const missing = loadWebConfig({ ...REQUIRED_PRODUCTION_ENV, GDPR_EXPORT_SSE_C_KEY_BASE64: undefined }, { nodeEnv: "production" }); const whitespace = loadWebConfig({ ...REQUIRED_PRODUCTION_ENV, GDPR_EXPORT_SSE_C_KEY_BASE64: " " }, { nodeEnv: "production" }); const malformed = "malformed-customer-key"; const invalid = loadWebConfig({ ...REQUIRED_PRODUCTION_ENV, GDPR_EXPORT_SSE_C_KEY_BASE64: malformed }, { nodeEnv: "production" }); const insecureEndpoint = loadWebConfig({ ...REQUIRED_PRODUCTION_ENV, STORAGE_ENDPOINT: "http://storage.example.com" }, { nodeEnv: "production" }); expect(missing.diagnostics).toContainEqual(expect.objectContaining({ code: "missing-required", name: "GDPR_EXPORT_SSE_C_KEY_BASE64" })); expect(whitespace.diagnostics).toContainEqual(expect.objectContaining({ code: "missing-required", name: "GDPR_EXPORT_SSE_C_KEY_BASE64" })); expect(invalid.diagnostics).toContainEqual(expect.objectContaining({ code: "weak-secret", name: "GDPR_EXPORT_SSE_C_KEY_BASE64" })); expect(insecureEndpoint.diagnostics).toContainEqual(expect.objectContaining({ code: "invalid-url", name: "STORAGE_ENDPOINT" })); expect(JSON.stringify(invalid.diagnostics)).not.toContain(malformed); }); test("enforces the production METRICS_TOKEN byte range", () => { const short = loadWebConfig({ ...REQUIRED_PRODUCTION_ENV, METRICS_TOKEN: "short" }, { nodeEnv: "production" }); const long = loadWebConfig({ ...REQUIRED_PRODUCTION_ENV, METRICS_TOKEN: "x".repeat(513) }, { nodeEnv: "production" }); expect(short.diagnostics).toContainEqual(expect.objectContaining({ code: "weak-secret", name: "METRICS_TOKEN" })); expect(long.diagnostics).toContainEqual(expect.objectContaining({ code: "weak-secret", name: "METRICS_TOKEN" })); }); test("rejects weak API-key and MCP token hash secrets in production", () => { const apiKeyHash = loadWebConfig({ ...REQUIRED_PRODUCTION_ENV, API_KEY_HASH_SECRET: "short" }, { nodeEnv: "production" }); const mcpHash = loadWebConfig({ ...REQUIRED_PRODUCTION_ENV, MCP_OAUTH_TOKEN_HASH_SECRET: "short" }, { nodeEnv: "production" }); expect(apiKeyHash.diagnostics).toContainEqual(expect.objectContaining({ code: "weak-secret", name: "API_KEY_HASH_SECRET" })); expect(mcpHash.diagnostics).toContainEqual(expect.objectContaining({ code: "weak-secret", name: "MCP_OAUTH_TOKEN_HASH_SECRET" })); }); test("does not enforce secret length outside production (placeholders allowed)", () => { const result = loadWebConfig({ ...REQUIRED_PRODUCTION_ENV, NODE_ENV: "test", SESSION_SECRET: "short" }, { nodeEnv: "test" }); expect(result.diagnostics).not.toContainEqual(expect.objectContaining({ code: "weak-secret" })); }); }); describe("loadWebConfig", () => { test("returns local defaults and domain routing defaults", () => { const result = loadWebConfig({}); expect(result.ok).toBe(false); expect(result.config).toMatchObject({ appName: "HiveGrid", nodeEnv: "development", runtimeSurface: "core", releaseRevision: "unknown", listen: { host: "0.0.0.0", port: 3000, }, routing: { appBaseDomain: "localhost", webAppHost: "app.localhost", adminAppHost: "admin.localhost", apiHost: "api.localhost", portalAppHost: "portal.localhost", protocol: "http", }, }); }); test("normalizes explicit listen and server-side provider values", () => { const result = loadWebConfig({ ...REQUIRED_PRODUCTION_ENV, NODE_ENV: "production", HOST: "127.0.0.1", PORT: "8080", WEBHOOKS_URL: "https://hooks.example.com", CRISP_CHAT_WEBSITE_ID: "crisp-id", MCAPTCHA_INSTANCE_URL: "https://captcha.example.test", MCAPTCHA_SITEKEY_LOGIN: "login-key", ANALYTICS_TOKEN: "analytics-token-value", NOTIFICATIONS_NOVU_API_KEY: "novu-secret-value", WEB_PUSH_VAPID_PUBLIC_KEY: "web-push-public-key", MAILJET_API_KEY: "mailjet-public-value", MAILJET_SECRET_KEY: "mailjet-secret-value", MAILJET_FROM_EMAIL: "sender@example.com", MAILJET_FROM_NAME: "Example Support", IPSTACK_API_KEY: "ipstack-api-key", IPAPIIS_API_KEY: "ipapiis-api-key", LAGO_API_URL: "https://lago.example.test", LAGO_API_KEY: "lago-secret-value", LAGO_WEBHOOK_SECRET: "lago-webhook-secret-value", LAGO_TIMEOUT_MS: "5000", AUTH_MAGIC_LINK_ENABLED: "true", AUTH_MAGIC_LINK_TOKEN_TTL_MINUTES: "15", STORAGE_PROVIDER: "s3", STORAGE_FALLBACK_PROVIDER: "local", STORAGE_ENDPOINT: "https://storage.example.com", STORAGE_REGION: "eu-central-1", STORAGE_BUCKET: "uploads", STORAGE_ACCESS_KEY_ID: "storage-access-key", STORAGE_SECRET_ACCESS_KEY: "storage-secret-key", GDPR_EXPORT_SSE_C_KEY_BASE64, STORAGE_SESSION_TOKEN: "storage-session-token", STORAGE_PUBLIC_BASE_URL: "https://cdn.example.com", STORAGE_FORCE_PATH_STYLE: "true", STORAGE_LOCAL_ROOT: "/tmp/hivegrid-storage", STORAGE_LOCAL_PUBLIC_BASE_URL: "http://localhost:3000/storage", DEMO_USER: "demo@example.com:password", }); expect(result.ok).toBe(true); expect(result.diagnostics).toEqual([]); expect(result.config).toMatchObject({ appName: "Production App", nodeEnv: "production", sessionSecret: "session-secret-value-prod-0123456789", listen: { host: "127.0.0.1", port: 8080, }, routing: { appBaseDomain: "example.com", webAppHost: "app.example.com", adminAppHost: "admin.example.com", apiHost: "api.example.com", protocol: "https", }, webhooksUrl: "https://hooks.example.com", crisp: { chatWebsiteId: "crisp-id", }, captcha: { provider: "mcaptcha", instanceUrl: "https://captcha.example.test", loginSiteKey: "login-key", }, analytics: { token: "analytics-token-value", }, novu: { apiKey: "novu-secret-value", }, webPush: { vapidPublicKey: "web-push-public-key", }, mailjet: { apiKey: "mailjet-public-value", secretKey: "mailjet-secret-value", fromEmail: "sender@example.com", fromName: "Example Support", }, ipProviders: { ipstackApiKey: "ipstack-api-key", ipapiIsApiKey: "ipapiis-api-key", }, lago: { apiUrl: "https://lago.example.test", apiKey: "lago-secret-value", webhookSecret: "lago-webhook-secret-value", timeoutMs: 5000, }, magicLink: { enabled: true, tokenTtlMinutes: 15, }, storage: { provider: "s3", fallbackProvider: "local", endpoint: "https://storage.example.com", region: "eu-central-1", bucket: "uploads", accessKeyId: "storage-access-key", secretAccessKey: "storage-secret-key", gdprExportSseCKey: GDPR_EXPORT_SSE_C_KEY_BASE64, sessionToken: "storage-session-token", publicBaseUrl: "https://cdn.example.com", forcePathStyle: "true", localRoot: "/tmp/hivegrid-storage", localPublicBaseUrl: "http://localhost:3000/storage", }, cryptoSecret: "crypto-secret-value-prod-0123456789", apiAccessToken: "api-access-token-value-prod-0123456789", jwtSecret: "jwt-secret-value-prod-0123456789abcd", demoUser: undefined, }); }); test("exposes Aircall provider configuration without making it a core runtime requirement", () => { const result = loadWebConfig( { ...REQUIRED_PRODUCTION_ENV, AIRCALL_CLIENT_ID: "aircall-client", AIRCALL_CLIENT_SECRET: "aircall-secret", AIRCALL_REDIRECT_URI: "https://app.example.com/integrations/aircall/oauth/callback", AIRCALL_WEBHOOK_URL: "https://api.example.com/v1/webhooks/telephony/aircall", }, { nodeEnv: "production" } ); expect(result.ok).toBe(true); expect(result.config.aircall).toEqual({ clientId: "aircall-client", clientSecret: "aircall-secret", redirectUri: "https://app.example.com/integrations/aircall/oauth/callback", webhookUrl: "https://api.example.com/v1/webhooks/telephony/aircall", }); expect(getEnvVariableDefinition("AIRCALL_CLIENT_SECRET")?.requiredInProduction).not.toBe(true); }); test("exposes the optional HalloPetra webhook URL", () => { const result = loadWebConfig( { ...REQUIRED_PRODUCTION_ENV, HALLOPETRA_WEBHOOK_URL: "https://api.example.com/v1/webhooks/telephony/hallopetra", }, { nodeEnv: "production" } ); expect(result.ok).toBe(true); expect(result.config.hallopetra).toEqual({ webhookUrl: "https://api.example.com/v1/webhooks/telephony/hallopetra", }); expect(getEnvVariableDefinition("HALLOPETRA_WEBHOOK_URL")?.requiredInProduction).not.toBe(true); }); test("exposes KWP OAuth provider configuration without a redirect override or core runtime requirement", () => { const result = loadWebConfig( { ...REQUIRED_PRODUCTION_ENV, KWP_OAUTH_CLIENT_ID: " kwp-client ", KWP_OAUTH_CLIENT_SECRET: " kwp-secret ", KWP_OAUTH_SCOPES: " openid offline_access ", KWP_CUSTOMER_CODE: " customer-code ", KWP_OIDC_ISSUER: " https://id.example.test/realms/example ", KWP_HIVEGRID_TENANT_ID: " tenant-geerkens ", KWP_MANDANT: " mandant-code ", }, { nodeEnv: "production" } ); expect(result.ok).toBe(true); expect(result.config.kwp).toEqual({ clientId: "kwp-client", clientSecret: "kwp-secret", scopes: "openid offline_access", customerCode: "customer-code", oidcIssuer: "https://id.example.test/realms/example", hiveGridTenantId: "tenant-geerkens", mandant: "mandant-code", }); for (const key of ["KWP_OAUTH_CLIENT_ID", "KWP_OAUTH_CLIENT_SECRET", "KWP_OAUTH_SCOPES", "KWP_CUSTOMER_CODE", "KWP_OIDC_ISSUER", "KWP_HIVEGRID_TENANT_ID", "KWP_MANDANT"]) { expect(getEnvVariableDefinition(key)?.requiredInProduction, `${key} must remain optional in production`).not.toBe(true); } expect(getEnvVariableDefinition("KWP_OAUTH_REDIRECT_URI")).toBeUndefined(); }); test("normalizes Cap captcha provider values", () => { const result = loadWebConfig({ ...REQUIRED_PRODUCTION_ENV, NODE_ENV: "production", CAPTCHA_PROVIDER: "cap", CAP_INSTANCE_URL: "https://captcha.hivegrid.de", CAP_SITEKEY_LOGIN: "cap-login-key", }); expect(result.config.captcha).toEqual({ provider: "cap", instanceUrl: "https://captcha.hivegrid.de", loginSiteKey: "cap-login-key", }); }); test("exposes demo login hints only for development config", () => { expect(loadWebConfig({ NODE_ENV: "development", DEMO_USER: "demo@example.com:password" }).config.demoUser).toBe("demo@example.com:password"); expect(loadWebConfig({ NODE_ENV: "test", DEMO_USER: "demo@example.com:password" }).config.demoUser).toBeUndefined(); expect(loadWebConfig({ NODE_ENV: "production", DEMO_USER: "demo@example.com:password" }).config.demoUser).toBeUndefined(); }); test("keeps Magic-Link provider capability safely disabled by default", () => { const result = loadWebConfig({}); expect(result.config.magicLink).toEqual({ enabled: false, tokenTtlMinutes: undefined, }); expect(result.diagnostics).not.toContainEqual( expect.objectContaining({ name: expect.stringMatching(/^AUTH_MAGIC_LINK_/), }) ); }); test("does not enable Magic-Link auth for malformed env values", () => { const invalidEnabled = loadWebConfig({ AUTH_MAGIC_LINK_ENABLED: "sometimes", AUTH_MAGIC_LINK_TOKEN_TTL_MINUTES: "15" }); const invalidTtl = loadWebConfig({ AUTH_MAGIC_LINK_ENABLED: "true", AUTH_MAGIC_LINK_TOKEN_TTL_MINUTES: "0" }); expect(invalidEnabled.config.magicLink).toEqual({ enabled: false, tokenTtlMinutes: undefined }); expect(invalidEnabled.diagnostics).toContainEqual( expect.objectContaining({ code: "invalid-boolean", name: "AUTH_MAGIC_LINK_ENABLED", }) ); expect(JSON.stringify(invalidEnabled.diagnostics)).not.toContain("sometimes"); expect(invalidTtl.config.magicLink).toEqual({ enabled: false, tokenTtlMinutes: undefined }); expect(invalidTtl.diagnostics).toContainEqual( expect.objectContaining({ code: "invalid-duration", name: "AUTH_MAGIC_LINK_TOKEN_TTL_MINUTES", }) ); expect(JSON.stringify(invalidTtl.diagnostics)).not.toContain("0"); }); test("catalogs Magic-Link provider env inputs", () => { expect(getEnvVariableDefinition("AUTH_MAGIC_LINK_ENABLED")).toMatchObject({ classification: "optional-provider", defaultValue: "false", targets: ["web", "api"], }); }); test("reports missing production session secret with redacted diagnostics", () => { const result = loadWebConfig( { ...REQUIRED_PRODUCTION_ENV, SESSION_SECRET: undefined, }, { nodeEnv: "production" } ); expect(result.ok).toBe(false); expect(result.diagnostics).toContainEqual( expect.objectContaining({ code: "missing-required", name: "SESSION_SECRET", }) ); expect(JSON.stringify(result.diagnostics)).not.toContain("session-secret-value"); }); test("falls back for invalid PORT and keeps supplied secret values out of diagnostics", () => { const secretValues = ["session-secret-value", "billing-secret-value", "crypto-secret-value", "api-token-value", "jwt-secret-value"]; const result = loadWebConfig({ SESSION_SECRET: secretValues[0], MOLLIE_API_KEY: secretValues[1], CRYPTO_SECRET: secretValues[2], API_ACCESS_TOKEN: secretValues[3], JWT_SECRET: secretValues[4], PORT: "not-a-port", }); expect(result.ok).toBe(false); expect(result.config.listen.port).toBe(3000); expect(result.diagnostics).toContainEqual( expect.objectContaining({ code: "invalid-port", name: "PORT", }) ); const diagnosticText = JSON.stringify(result.diagnostics); for (const secretValue of secretValues) { expect(diagnosticText).not.toContain(secretValue); } }); test("keeps web-owned PORT and HOST in the catalog", () => { expect(getEnvVariableDefinition("PORT")).toMatchObject({ targets: ["web"], defaultValue: "3000", }); expect(getEnvVariableDefinition("HOST")).toMatchObject({ targets: ["web"], defaultValue: "0.0.0.0", }); }); test("allows S3 as an explicit storage fallback provider", () => { const result = loadWebConfig({ STORAGE_PROVIDER: "local", STORAGE_FALLBACK_PROVIDER: "s3", }); expect(result.config.storage).toMatchObject({ provider: "local", fallbackProvider: "s3", }); }); test("loads Mollie config for server-side admin billing sync", () => { const result = loadWebConfig({ MOLLIE_API_KEY: " mollie-secret-value ", MOLLIE_PROFILE_ID: " mollie-profile-value ", MOLLIE_TIMEOUT_MS: "invalid", }); expect(result.config.mollie).toEqual({ apiKey: "mollie-secret-value", profileId: "mollie-profile-value", timeoutMs: 10_000, }); }); });