import { readFileSync } from "node:fs"; import { fileURLToPath } from "node:url"; import { dirname, resolve } from "node:path"; import { describe, expect, test, vi } from "vitest"; import { queueNames } from "@hivegrid/queue"; import { buildWorkerHealthReport, runWorkerHealthCli } from "./health.js"; const packageJsonPath = resolve(dirname(fileURLToPath(import.meta.url)), "../package.json"); const mailjetEnv = { MAILJET_API_KEY: "fixture-mailjet-key", MAILJET_SECRET_KEY: "fixture-mailjet-secret", MAILJET_FROM_EMAIL: "sender@example.com", }; const cryptoSecret = "shared-tenant-envelope-secret-value"; const gdprExportSseCKey = "BwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwc="; const storageEnv = { STORAGE_ACCESS_KEY_ID: "access-key", STORAGE_BUCKET: "exports", STORAGE_ENDPOINT: "https://storage.example.com", STORAGE_REGION: "eu-central-1", STORAGE_SECRET_ACCESS_KEY: "secret-key", GDPR_EXPORT_SSE_C_KEY_BASE64: gdprExportSseCKey, }; function readPackageScripts(): Record { return JSON.parse(readFileSync(packageJsonPath, "utf8")).scripts; } function createIo() { let output = ""; return { io: { stdout: { write(chunk: string) { output += chunk; return true; }, }, }, readOutput: () => output, }; } describe("worker health", () => { test("production health script runs compiled Node output without tsx", () => { const scripts = readPackageScripts(); expect(scripts.health).toBe("node dist/health.js"); expect(scripts.health).not.toContain("tsx"); }); test("reports disabled without Redis as a successful config-only health state and skips DB", async () => { const databaseProbe = vi.fn(); const report = await buildWorkerHealthReport({ WORKER_ENABLED: "false" }, { databaseProbe }); expect(report).toEqual({ databaseConfigured: false, databaseStatus: "skipped", errors: [], importConfigured: false, importStatus: "skipped", mailjetConfigured: false, mailjetStatus: "skipped", pluginRuntimeConfigured: false, pluginRuntimeStatus: "skipped", queues: [queueNames.email, queueNames.notifications], redisConfigured: false, redisParseStatus: "not_configured", redisSource: "none", redisStatus: "skipped", revision: "unknown", status: "disabled", storageConfigured: false, storageStatus: "skipped", workerEnabled: false, workerGroup: null, }); expect(databaseProbe).not.toHaveBeenCalled(); }); test("reports ready with BULLMQ Redis and ready DB configured", async () => { const databaseProbe = vi.fn(); const redisProbe = vi.fn(); const report = await buildWorkerHealthReport( { BULLMQ_REDIS_URL: "redis://localhost:6379/0", DATABASE_URL: "postgresql://user:db-secret@localhost:5432/app", ...mailjetEnv, WORKER_ENABLED: "true", WORKER_QUEUE_GROUP: "general", }, { databaseProbe, redisProbe } ); expect(report).toMatchObject({ databaseConfigured: true, databaseStatus: "ready", errors: [], redisConfigured: true, redisParseStatus: "valid", redisSource: "BULLMQ_REDIS_URL", redisStatus: "ready", status: "ready", importConfigured: false, importStatus: "skipped", mailjetConfigured: true, mailjetStatus: "ready", storageConfigured: false, storageStatus: "skipped", workerEnabled: true, workerGroup: "general", }); expect(report.queues).toEqual([queueNames.notifications, queueNames.maintenance, queueNames.fileScan, queueNames.filePreview]); expect(redisProbe).toHaveBeenCalledWith("redis://localhost:6379/0"); expect(databaseProbe).toHaveBeenCalledTimes(1); }); test("probes Redis and database readiness concurrently", async () => { const events: string[] = []; let releaseRedis: (() => void) | undefined; let databaseStartedBeforeRedisFinished = false; const redisProbe = vi.fn( () => new Promise((resolve) => { events.push("redis:start"); releaseRedis = () => { events.push("redis:end"); resolve(); }; }) ); const databaseProbe = vi.fn(async () => { databaseStartedBeforeRedisFinished = !events.includes("redis:end"); events.push("db:start"); releaseRedis?.(); events.push("db:end"); }); const report = await buildWorkerHealthReport( { BULLMQ_REDIS_URL: "redis://localhost:6379/0", DATABASE_URL: "postgresql://user:db-secret@localhost:5432/app", ...mailjetEnv, WORKER_ENABLED: "true", WORKER_QUEUE_GROUP: "general", }, { databaseProbe, redisProbe } ); expect(report.status).toBe("ready"); expect(databaseStartedBeforeRedisFinished).toBe(true); expect(events).toEqual(["redis:start", "db:start", "redis:end", "db:end"]); }); test("includes the injected image revision without probing dependencies", async () => { const report = await buildWorkerHealthReport({ APP_REVISION: "abc1234", WORKER_ENABLED: "false" }, { databaseProbe: vi.fn() }); expect(report.revision).toBe("abc1234"); }); test("does not block webhook-capable worker readiness when Mailjet delivery config is absent", async () => { const report = await buildWorkerHealthReport( { BULLMQ_REDIS_URL: "redis://localhost:6379/0", DATABASE_URL: "postgresql://localhost/app", WORKER_ENABLED: "true", WORKER_QUEUE_GROUP: "webhook", }, { databaseProbe: vi.fn(), redisProbe: vi.fn() } ); expect(report.status).toBe("ready"); expect(report.mailjetConfigured).toBe(false); expect(report.mailjetStatus).toBe("skipped"); expect(report.errors).toEqual([]); }); test("reports queue-group-scoped queues for the mail worker", async () => { const report = await buildWorkerHealthReport( { BULLMQ_REDIS_URL: "redis://localhost:6379/0", DATABASE_URL: "postgresql://localhost/app", CRYPTO_SECRET: cryptoSecret, WORKER_ENABLED: "true", WORKER_QUEUE_GROUP: "mail", ...mailjetEnv, }, { databaseProbe: vi.fn(), redisProbe: vi.fn() } ); expect(report.status).toBe("ready"); expect(report.workerGroup).toBe("mail"); expect(report.queues).toEqual([queueNames.email]); }); test("reports general required-mode gaps as redacted health diagnostics", async () => { const report = await buildWorkerHealthReport( { BULLMQ_REDIS_URL: "redis://localhost:6379/0", DATABASE_URL: "postgresql://localhost/app", OUTBOX_DISPATCHER_REQUIRED: "true", WORKER_ENABLED: "true", WORKER_QUEUE_GROUP: "general", WORKFLOW_TRIGGER_OUTBOX_REQUIRED: "true", }, { databaseProbe: vi.fn(), redisProbe: vi.fn() } ); expect(report.status).toBe("misconfigured"); expect(report.errors).toEqual( expect.arrayContaining([ "OUTBOX_DISPATCHER_ENABLED:missing-required-general-worker:Required when WORKER_QUEUE_GROUP=general and OUTBOX_DISPATCHER_REQUIRED=true.", "WORKFLOW_TRIGGER_OUTBOX_ENABLED:missing-required-general-worker:Required when WORKER_QUEUE_GROUP=general and WORKFLOW_TRIGGER_OUTBOX_REQUIRED=true.", "API_ACCESS_TOKEN:missing-workflow-executor-bridge:Required when WORKER_QUEUE_GROUP=general and WORKFLOW_TRIGGER_OUTBOX_REQUIRED=true with http or hybrid executor mode.", "WORKFLOW_TRIGGER_EXECUTOR_URL:missing-workflow-executor-bridge:Required when WORKER_QUEUE_GROUP=general and WORKFLOW_TRIGGER_OUTBOX_REQUIRED=true with http or hybrid executor mode.", ]) ); expect(JSON.stringify(report)).not.toContain("postgresql://localhost/app"); }); test("does not accept the legacy workflow executor token without the web API token", async () => { const report = await buildWorkerHealthReport( { BULLMQ_REDIS_URL: "redis://localhost:6379/0", DATABASE_URL: "postgresql://localhost/app", OUTBOX_DISPATCHER_ENABLED: "true", OUTBOX_DISPATCHER_REQUIRED: "true", WORKER_ENABLED: "true", WORKER_QUEUE_GROUP: "general", WORKFLOW_TRIGGER_EXECUTOR_TOKEN: "legacy-worker-only", WORKFLOW_TRIGGER_EXECUTOR_URL: "https://app.example.com", WORKFLOW_TRIGGER_OUTBOX_ENABLED: "true", WORKFLOW_TRIGGER_OUTBOX_REQUIRED: "true", }, { databaseProbe: vi.fn(), redisProbe: vi.fn() } ); expect(report.status).toBe("misconfigured"); expect(report.errors).toContain( "API_ACCESS_TOKEN:missing-workflow-executor-bridge:Required when WORKER_QUEUE_GROUP=general and WORKFLOW_TRIGGER_OUTBOX_REQUIRED=true with http or hybrid executor mode." ); }); test("reports workflow required-mode outbox dispatcher enablement gaps explicitly", async () => { const report = await buildWorkerHealthReport( { API_ACCESS_TOKEN: "redacted-test-token", APP_URL: "https://app.example.com", BULLMQ_REDIS_URL: "redis://localhost:6379/0", DATABASE_URL: "postgresql://localhost/app", WORKER_ENABLED: "true", WORKER_QUEUE_GROUP: "general", WORKFLOW_TRIGGER_OUTBOX_ENABLED: "true", WORKFLOW_TRIGGER_OUTBOX_REQUIRED: "true", }, { databaseProbe: vi.fn(), redisProbe: vi.fn() } ); expect(report.status).toBe("misconfigured"); expect(report.errors).toEqual([ "OUTBOX_DISPATCHER_ENABLED:missing-workflow-trigger-dispatcher:Required when WORKER_QUEUE_GROUP=general and WORKFLOW_TRIGGER_OUTBOX_REQUIRED=true.", ]); expect(JSON.stringify(report)).not.toContain("redacted-test-token"); expect(JSON.stringify(report)).not.toContain("https://app.example.com"); }); test("reports explicitly enabled workflow outbox bridge gaps even when not required", async () => { const report = await buildWorkerHealthReport( { BULLMQ_REDIS_URL: "redis://localhost:6379/0", DATABASE_URL: "postgresql://localhost/app", OUTBOX_DISPATCHER_ENABLED: "true", OUTBOX_DISPATCHER_REQUIRED: "false", WORKER_ENABLED: "true", WORKER_QUEUE_GROUP: "general", WORKFLOW_TRIGGER_OUTBOX_ENABLED: "true", WORKFLOW_TRIGGER_OUTBOX_REQUIRED: "false", }, { databaseProbe: vi.fn(), redisProbe: vi.fn() } ); expect(report.status).toBe("misconfigured"); expect(report.errors).toEqual([ "API_ACCESS_TOKEN:missing-workflow-executor-bridge:Required when WORKER_QUEUE_GROUP=general and WORKFLOW_TRIGGER_OUTBOX_ENABLED=true with http or hybrid executor mode.", "WORKFLOW_TRIGGER_EXECUTOR_URL:missing-workflow-executor-bridge:Required when WORKER_QUEUE_GROUP=general and WORKFLOW_TRIGGER_OUTBOX_ENABLED=true with http or hybrid executor mode.", ]); expect(JSON.stringify(report)).not.toContain("postgresql://localhost/app"); }); test("accepts general required-mode workflow fanout when native executor mode is explicit", async () => { const report = await buildWorkerHealthReport( { BULLMQ_REDIS_URL: "redis://localhost:6379/0", DATABASE_URL: "postgresql://localhost/app", OUTBOX_DISPATCHER_ENABLED: "true", OUTBOX_DISPATCHER_REQUIRED: "true", WORKER_ENABLED: "true", WORKER_QUEUE_GROUP: "general", WORKFLOW_TRIGGER_EXECUTOR_MODE: "native", WORKFLOW_TRIGGER_OUTBOX_ENABLED: "true", WORKFLOW_TRIGGER_OUTBOX_REQUIRED: "true", }, { databaseProbe: vi.fn(), redisProbe: vi.fn() } ); expect(report.status).toBe("ready"); expect(report.errors).toEqual([]); }); test("accepts general required-mode workflow fanout when native executor mode has whitespace", async () => { const report = await buildWorkerHealthReport( { BULLMQ_REDIS_URL: "redis://localhost:6379/0", DATABASE_URL: "postgresql://localhost/app", OUTBOX_DISPATCHER_ENABLED: "true", OUTBOX_DISPATCHER_REQUIRED: "true", WORKER_ENABLED: "true", WORKER_QUEUE_GROUP: "general", WORKFLOW_TRIGGER_EXECUTOR_MODE: " native ", WORKFLOW_TRIGGER_OUTBOX_ENABLED: "true", WORKFLOW_TRIGGER_OUTBOX_REQUIRED: "true", }, { databaseProbe: vi.fn(), redisProbe: vi.fn() } ); expect(report.status).toBe("ready"); expect(report.errors).toEqual([]); }); test("reports invalid workflow executor mode before required-mode bootstrap fails later", async () => { const report = await buildWorkerHealthReport( { API_ACCESS_TOKEN: "redacted-test-token", APP_URL: "https://app.example.com", BULLMQ_REDIS_URL: "redis://localhost:6379/0", DATABASE_URL: "postgresql://localhost/app", OUTBOX_DISPATCHER_ENABLED: "true", OUTBOX_DISPATCHER_REQUIRED: "true", WORKER_ENABLED: "true", WORKER_QUEUE_GROUP: "general", WORKFLOW_TRIGGER_EXECUTOR_MODE: "sidecar", WORKFLOW_TRIGGER_OUTBOX_ENABLED: "true", WORKFLOW_TRIGGER_OUTBOX_REQUIRED: "true", }, { databaseProbe: vi.fn(), redisProbe: vi.fn() } ); expect(report.status).toBe("misconfigured"); expect(report.errors).toEqual([ "WORKFLOW_TRIGGER_EXECUTOR_MODE:invalid-workflow-executor-mode:Expected http, native, or hybrid when WORKER_QUEUE_GROUP=general and WORKFLOW_TRIGGER_OUTBOX_REQUIRED=true.", ]); expect(JSON.stringify(report)).not.toContain("redacted-test-token"); expect(JSON.stringify(report)).not.toContain("https://app.example.com"); }); test("accepts general required-mode workflow fanout with signed HTTP bridge config", async () => { const report = await buildWorkerHealthReport( { API_ACCESS_TOKEN: "redacted-test-token", APP_URL: "https://app.example.com", BULLMQ_REDIS_URL: "redis://localhost:6379/0", DATABASE_URL: "postgresql://localhost/app", OUTBOX_DISPATCHER_ENABLED: "true", OUTBOX_DISPATCHER_REQUIRED: "true", WORKER_ENABLED: "true", WORKER_QUEUE_GROUP: "general", WORKFLOW_TRIGGER_OUTBOX_ENABLED: "true", WORKFLOW_TRIGGER_OUTBOX_REQUIRED: "true", }, { databaseProbe: vi.fn(), redisProbe: vi.fn() } ); expect(report.status).toBe("ready"); expect(JSON.stringify(report)).not.toContain("redacted-test-token"); expect(JSON.stringify(report)).not.toContain("https://app.example.com"); }); test("reports production plugin runtime gaps on the general worker", async () => { const report = await buildWorkerHealthReport( { BULLMQ_REDIS_URL: "redis://localhost:6379/0", DATABASE_URL: "postgresql://localhost/app", OUTBOX_DISPATCHER_ENABLED: "false", PLUGIN_CAPABILITY_USE_ENABLED: "false", PLUGIN_HOOK_FANOUT_ENABLED: "false", PLUGIN_RUNTIME_PRODUCTION_ENABLED: "true", WORKER_ENABLED: "true", WORKER_QUEUE_GROUP: "general", }, { databaseProbe: vi.fn(), redisProbe: vi.fn() } ); expect(report.status).toBe("misconfigured"); expect(report.pluginRuntimeConfigured).toBe(true); expect(report.pluginRuntimeStatus).toBe("misconfigured"); expect(report.errors).toEqual( expect.arrayContaining([ "PLUGIN_TRUSTED_PUBLIC_KEYS:missing-plugin-trust-root:Required when PLUGIN_RUNTIME_PRODUCTION_ENABLED=true.", "OUTBOX_DISPATCHER_ENABLED:missing-plugin-runtime-dispatcher:Required on WORKER_QUEUE_GROUP=general when PLUGIN_RUNTIME_PRODUCTION_ENABLED=true.", "PLUGIN_HOOK_FANOUT_ENABLED:disabled-plugin-runtime-fanout:Must not be false on WORKER_QUEUE_GROUP=general when PLUGIN_RUNTIME_PRODUCTION_ENABLED=true.", "PLUGIN_CAPABILITY_USE_ENABLED:disabled-plugin-capability-use:Must not be false on WORKER_QUEUE_GROUP=general when PLUGIN_RUNTIME_PRODUCTION_ENABLED=true.", ]) ); }); test("reports production plugin runtime ready when trust root and general subscribers are configured", async () => { const report = await buildWorkerHealthReport( { BULLMQ_REDIS_URL: "redis://localhost:6379/0", DATABASE_URL: "postgresql://localhost/app", OUTBOX_DISPATCHER_ENABLED: "true", PLUGIN_RUNTIME_PRODUCTION_ENABLED: "true", PLUGIN_TRUSTED_PUBLIC_KEYS: "aa".repeat(32), WORKER_ENABLED: "true", WORKER_QUEUE_GROUP: "general", }, { databaseProbe: vi.fn(), redisProbe: vi.fn() } ); expect(report.status).toBe("ready"); expect(report.pluginRuntimeConfigured).toBe(true); expect(report.pluginRuntimeStatus).toBe("ready"); expect(report.errors).toEqual([]); }); test("reports plugin worker production runtime trust-root readiness", async () => { const report = await buildWorkerHealthReport( { BULLMQ_REDIS_URL: "redis://localhost:6379/0", DATABASE_URL: "postgresql://localhost/app", PLUGIN_RUNTIME_PRODUCTION_ENABLED: "true", WORKER_ENABLED: "true", WORKER_QUEUE_GROUP: "plugin", }, { databaseProbe: vi.fn(), redisProbe: vi.fn() } ); expect(report.status).toBe("misconfigured"); expect(report.pluginRuntimeConfigured).toBe(true); expect(report.pluginRuntimeStatus).toBe("misconfigured"); expect(report.errors).toEqual(["PLUGIN_TRUSTED_PUBLIC_KEYS:missing-plugin-trust-root:Required when PLUGIN_RUNTIME_PRODUCTION_ENABLED=true."]); }); test("reports required mail worker misconfigured when Mailjet env is absent", async () => { const report = await buildWorkerHealthReport( { BULLMQ_REDIS_URL: "redis://localhost:6379/0", DATABASE_URL: "postgresql://localhost/app", TRANSACTIONAL_MAIL_OUTBOX_REQUIRED: "true", WORKER_ENABLED: "true", WORKER_QUEUE_GROUP: "mail", }, { databaseProbe: vi.fn(), redisProbe: vi.fn() } ); expect(report.status).toBe("misconfigured"); expect(report.mailjetConfigured).toBe(false); expect(report.mailjetStatus).toBe("misconfigured"); expect(report.errors).toEqual( expect.arrayContaining([ "MAILJET_API_KEY:missing-mail-provider-config:Required when WORKER_QUEUE_GROUP=mail and TRANSACTIONAL_MAIL_OUTBOX_REQUIRED=true.", "MAILJET_SECRET_KEY:missing-mail-provider-config:Required when WORKER_QUEUE_GROUP=mail and TRANSACTIONAL_MAIL_OUTBOX_REQUIRED=true.", "MAILJET_FROM_EMAIL:missing-mail-provider-config:Required when WORKER_QUEUE_GROUP=mail and TRANSACTIONAL_MAIL_OUTBOX_REQUIRED=true.", ]) ); }); test("reports mail-worker readiness misconfigured when the shared envelope key is absent", async () => { const databaseProbe = vi.fn(); const redisProbe = vi.fn(); const report = await buildWorkerHealthReport( { BULLMQ_REDIS_URL: "redis://localhost:6379/0", DATABASE_URL: "postgresql://localhost/app", ...mailjetEnv, TRANSACTIONAL_MAIL_OUTBOX_REQUIRED: "true", WORKER_ENABLED: "true", WORKER_QUEUE_GROUP: "mail", }, { databaseProbe, redisProbe } ); expect(report.status).toBe("misconfigured"); expect(report.errors).toContain("CRYPTO_SECRET:missing-required:CRYPTO_SECRET is required."); expect(databaseProbe).not.toHaveBeenCalled(); expect(redisProbe).not.toHaveBeenCalled(); }); test("reports misconfigured and skips DB probe when enabled worker lacks DATABASE_URL", async () => { const databaseProbe = vi.fn(); const report = await buildWorkerHealthReport( { BULLMQ_REDIS_URL: "redis://localhost:6379/0", ...mailjetEnv, WORKER_ENABLED: "true", WORKER_QUEUE_GROUP: "general", }, { databaseProbe, redisProbe: vi.fn() } ); expect(report.status).toBe("misconfigured"); expect(report.databaseConfigured).toBe(false); expect(report.databaseStatus).toBe("skipped"); expect(report.errors).toEqual(expect.arrayContaining(["DATABASE_URL:missing-required:DATABASE_URL is required when WORKER_ENABLED is true."])); expect(databaseProbe).not.toHaveBeenCalled(); }); test("reports misconfigured and skips dependency probes when enabled worker lacks WORKER_QUEUE_GROUP", async () => { const databaseProbe = vi.fn(); const redisProbe = vi.fn(); const report = await buildWorkerHealthReport( { BULLMQ_REDIS_URL: "redis://localhost:6379/0", DATABASE_URL: "postgresql://localhost/app", ...mailjetEnv, WORKER_ENABLED: "true", }, { databaseProbe, redisProbe } ); expect(report.status).toBe("misconfigured"); expect(report.workerEnabled).toBe(true); expect(report.workerGroup).toBe(null); expect(report.errors).toEqual(expect.arrayContaining(["WORKER_QUEUE_GROUP:missing-required:WORKER_QUEUE_GROUP is required when WORKER_ENABLED is true."])); expect(databaseProbe).not.toHaveBeenCalled(); expect(redisProbe).not.toHaveBeenCalled(); }); test("reports the maintenance queue when export retention processing is enabled", async () => { const report = await buildWorkerHealthReport( { BULLMQ_REDIS_URL: "redis://localhost:6379/0", DATABASE_URL: "postgresql://localhost/app", EXPORT_RETENTION_QUEUE_ENABLED: "true", ...mailjetEnv, STORAGE_ACCESS_KEY_ID: "access-key", STORAGE_BUCKET: "exports", STORAGE_ENDPOINT: "https://storage.example.com", STORAGE_REGION: "us-east-1", STORAGE_SECRET_ACCESS_KEY: "secret-key", WORKER_ENABLED: "true", WORKER_QUEUE_GROUP: "general", }, { databaseProbe: vi.fn(), redisProbe: vi.fn() } ); expect(report.status).toBe("ready"); expect(report.queues).toEqual([queueNames.notifications, queueNames.maintenance, queueNames.fileScan, queueNames.filePreview]); expect(report.storageStatus).toBe("ready"); }); test("keeps the import worker ready with only KWP while generic import gates are disabled", async () => { const report = await buildWorkerHealthReport( { BULLMQ_REDIS_URL: "redis://localhost:6379/0", DATABASE_URL: "postgresql://localhost/app", ...mailjetEnv, WORKER_ENABLED: "true", WORKER_QUEUE_GROUP: "import", }, { databaseProbe: vi.fn(), redisProbe: vi.fn() } ); expect(report.status).toBe("ready"); expect(report.queues).toEqual([queueNames.kwpImports]); expect(report.importStatus).toBe("skipped"); expect(report.errors).not.toEqual(expect.arrayContaining([expect.stringMatching(/missing-import/u)])); }); test("reports the dedicated portal GDPR export mode ready with KWP and exports ownership", async () => { const report = await buildWorkerHealthReport( { BULLMQ_REDIS_URL: "redis://localhost:6379/0", DATABASE_URL: "postgresql://localhost/app", PORTAL_GDPR_EXPORT_QUEUE_ENABLED: "true", ...storageEnv, WORKER_ENABLED: "true", WORKER_QUEUE_GROUP: "import", }, { databaseProbe: vi.fn(), redisProbe: vi.fn() } ); expect(report).toMatchObject({ status: "ready", importConfigured: true, importStatus: "ready", storageConfigured: true, storageStatus: "ready", workerGroup: "import", }); expect(report.queues).toEqual([queueNames.kwpImports, queueNames.exports]); expect(report.errors).toEqual([]); }); test("fails dedicated portal GDPR export readiness without complete storage", async () => { const report = await buildWorkerHealthReport( { BULLMQ_REDIS_URL: "redis://localhost:6379/0", DATABASE_URL: "postgresql://localhost/app", PORTAL_GDPR_EXPORT_QUEUE_ENABLED: "true", WORKER_ENABLED: "true", WORKER_QUEUE_GROUP: "import", }, { databaseProbe: vi.fn(), redisProbe: vi.fn() } ); expect(report.status).toBe("misconfigured"); expect(report.queues).toEqual([queueNames.kwpImports, queueNames.exports]); expect(report.storageStatus).toBe("misconfigured"); expect(report.errors).toEqual( expect.arrayContaining([ "STORAGE_ENDPOINT:missing-storage-config:Required when PORTAL_GDPR_EXPORT_QUEUE_ENABLED=true.", "GDPR_EXPORT_SSE_C_KEY_BASE64:missing-storage-config:Required when PORTAL_GDPR_EXPORT_QUEUE_ENABLED=true.", ]) ); }); test("fails full import readiness without the GDPR SSE-C key", async () => { const { GDPR_EXPORT_SSE_C_KEY_BASE64: _key, ...storageWithoutKey } = storageEnv; const report = await buildWorkerHealthReport( { BULLMQ_REDIS_URL: "redis://localhost:6379/0", DATABASE_URL: "postgresql://localhost/app", IMPORT_QUEUE_ENABLED: "true", IMPORT_QUEUE_RUNTIME_READY: "true", ...storageWithoutKey, WORKER_ENABLED: "true", WORKER_QUEUE_GROUP: "import", }, { databaseProbe: vi.fn(), redisProbe: vi.fn() } ); expect(report.status).toBe("misconfigured"); expect(report.storageStatus).toBe("misconfigured"); expect(report.errors).toEqual( expect.arrayContaining(["GDPR_EXPORT_SSE_C_KEY_BASE64:missing-storage-config:Required when the full import runtime owns GDPR export jobs."]) ); }); test("reports the full import runtime ready only with encrypted GDPR storage", async () => { const report = await buildWorkerHealthReport( { BULLMQ_REDIS_URL: "redis://localhost:6379/0", DATABASE_URL: "postgresql://localhost/app", IMPORT_QUEUE_ENABLED: "true", IMPORT_QUEUE_RUNTIME_READY: "true", ...storageEnv, WORKER_ENABLED: "true", WORKER_QUEUE_GROUP: "import", }, { databaseProbe: vi.fn(), redisProbe: vi.fn() } ); expect(report).toMatchObject({ status: "ready", importStatus: "ready", storageStatus: "ready" }); expect(report.queues).toEqual([queueNames.kwpImports, queueNames.imports, queueNames.exports]); }); test("fails dedicated portal GDPR export readiness on the wrong worker group", async () => { const report = await buildWorkerHealthReport( { BULLMQ_REDIS_URL: "redis://localhost:6379/0", DATABASE_URL: "postgresql://localhost/app", PORTAL_GDPR_EXPORT_QUEUE_ENABLED: "true", ...storageEnv, WORKER_ENABLED: "true", WORKER_QUEUE_GROUP: "general", }, { databaseProbe: vi.fn(), redisProbe: vi.fn() } ); expect(report.status).toBe("misconfigured"); expect(report.errors).toEqual( expect.arrayContaining([ "PORTAL_GDPR_EXPORT_QUEUE_ENABLED:invalid-worker-queue-group:PORTAL_GDPR_EXPORT_QUEUE_ENABLED=true requires WORKER_QUEUE_GROUP=import.", ]) ); }); test("fails closed instead of becoming healthy-idle for a whitespace-padded dedicated gate", async () => { const report = await buildWorkerHealthReport( { BULLMQ_REDIS_URL: "redis://localhost:6379/0", DATABASE_URL: "postgresql://localhost/app", PORTAL_GDPR_EXPORT_QUEUE_ENABLED: " true ", ...storageEnv, WORKER_ENABLED: "true", WORKER_QUEUE_GROUP: "import", }, { databaseProbe: vi.fn(), redisProbe: vi.fn() } ); expect(report.status).toBe("misconfigured"); expect(report.queues).toEqual([]); expect(report.errors).toEqual( expect.arrayContaining(["PORTAL_GDPR_EXPORT_QUEUE_ENABLED:invalid-boolean:PORTAL_GDPR_EXPORT_QUEUE_ENABLED must be exactly true or false."]) ); }); test("reports misconfigured when import worker group is partially enabled", async () => { const report = await buildWorkerHealthReport( { BULLMQ_REDIS_URL: "redis://localhost:6379/0", DATABASE_URL: "postgresql://localhost/app", IMPORT_QUEUE_ENABLED: "true", ...mailjetEnv, WORKER_ENABLED: "true", WORKER_QUEUE_GROUP: "import", }, { databaseProbe: vi.fn(), redisProbe: vi.fn() } ); expect(report.status).toBe("misconfigured"); expect(report.queues).toEqual([]); expect(report.importStatus).toBe("misconfigured"); expect(report.errors).toEqual( expect.arrayContaining([ "IMPORT_QUEUE_RUNTIME_READY:missing-import-runtime-gate:Required with IMPORT_QUEUE_ENABLED=true after import, export, and scan services are wired.", ]) ); }); test("reports misconfigured when export retention is enabled without storage delete config", async () => { const report = await buildWorkerHealthReport( { BULLMQ_REDIS_URL: "redis://localhost:6379/0", DATABASE_URL: "postgresql://localhost/app", EXPORT_RETENTION_QUEUE_ENABLED: "true", ...mailjetEnv, WORKER_ENABLED: "true", WORKER_QUEUE_GROUP: "general", }, { databaseProbe: vi.fn(), redisProbe: vi.fn() } ); expect(report.status).toBe("misconfigured"); expect(report.storageStatus).toBe("misconfigured"); expect(report.errors).toEqual(expect.arrayContaining(["STORAGE_ENDPOINT:missing-storage-config:Required when EXPORT_RETENTION_QUEUE_ENABLED=true."])); }); test("falls back to REDIS_URL when BULLMQ_REDIS_URL is absent", async () => { const report = await buildWorkerHealthReport( { DATABASE_URL: "postgresql://localhost/app", REDIS_URL: "rediss://localhost:6380/1", ...mailjetEnv, WORKER_ENABLED: "true", WORKER_QUEUE_GROUP: "general", }, { databaseProbe: vi.fn(), redisProbe: vi.fn() } ); expect(report).toMatchObject({ databaseStatus: "ready", errors: [], redisConfigured: true, redisParseStatus: "valid", redisSource: "REDIS_URL", redisStatus: "ready", status: "ready", workerEnabled: true, }); }); test("returns non-zero for enabled worker with invalid Redis protocol and skips DB", async () => { const databaseProbe = vi.fn(); const { io, readOutput } = createIo(); const secretRedisUrl = "http://user:redis-secret@localhost:6379"; const exitCode = await runWorkerHealthCli( { BULLMQ_REDIS_URL: secretRedisUrl, DATABASE_URL: "postgresql://db-secret@localhost/app", ...mailjetEnv, WORKER_ENABLED: "true", WORKER_QUEUE_GROUP: "general", }, io, { databaseProbe } ); const output = readOutput(); const report = JSON.parse(output); expect(exitCode).toBe(1); expect(report).toMatchObject({ databaseConfigured: true, databaseStatus: "skipped", redisConfigured: false, redisParseStatus: "invalid", redisSource: "BULLMQ_REDIS_URL", redisStatus: "skipped", status: "misconfigured", workerEnabled: true, }); expect(report.errors).toEqual(["BULLMQ_REDIS_URL:invalid-redis-protocol:Configured Redis URL protocol must be redis: or rediss:."]); expect(databaseProbe).not.toHaveBeenCalled(); expect(output).not.toContain(secretRedisUrl); expect(output).not.toContain("redis-secret"); expect(output).not.toContain("db-secret"); }); test("returns non-zero with redacted DB code when DB readiness fails", async () => { const { io, readOutput } = createIo(); const exitCode = await runWorkerHealthCli( { BULLMQ_REDIS_URL: "redis://localhost:6379/0", DATABASE_URL: "postgresql://user:database-secret@localhost:5432/app", ...mailjetEnv, WORKER_ENABLED: "true", WORKER_QUEUE_GROUP: "general", }, io, { databaseProbe: vi.fn().mockRejectedValue( Object.assign(new Error("postgresql://user:database-secret@localhost:5432/app failed with token abc123"), { code: "P1001", stack: "secret stack trace", }) ), redisProbe: vi.fn(), } ); const output = readOutput(); const report = JSON.parse(output); expect(exitCode).toBe(1); expect(report).toMatchObject({ databaseConfigured: true, databaseErrorCode: "P1001", databaseStatus: "misconfigured", status: "misconfigured", }); expect(output).not.toContain("database-secret"); expect(output).not.toContain("abc123"); expect(output).not.toContain("postgresql://user:database-secret@localhost:5432/app"); expect(output).not.toContain("secret stack trace"); }); test("returns non-zero with redacted Redis code when Redis readiness fails", async () => { const { io, readOutput } = createIo(); const exitCode = await runWorkerHealthCli( { BULLMQ_REDIS_URL: "redis://user:redis-secret@localhost:6379/0", DATABASE_URL: "postgresql://user:db-secret@localhost:5432/app", ...mailjetEnv, WORKER_ENABLED: "true", WORKER_QUEUE_GROUP: "general", }, io, { databaseProbe: vi.fn(), redisProbe: vi.fn().mockRejectedValue(Object.assign(new Error("redis://user:redis-secret@localhost failed"), { code: "ECONNREFUSED" })), } ); const output = readOutput(); const report = JSON.parse(output); expect(exitCode).toBe(1); expect(report).toMatchObject({ redisErrorCode: "ECONNREFUSED", redisStatus: "misconfigured", status: "misconfigured", }); expect(output).not.toContain("redis-secret"); expect(output).not.toContain("db-secret"); }); test("does not leak password-bearing Redis URLs in JSON output", async () => { const { io, readOutput } = createIo(); const exitCode = await runWorkerHealthCli( { BULLMQ_REDIS_URL: "redis://user:super-secret@localhost:6379/0", DATABASE_URL: "postgresql://user:db-secret@localhost:5432/app", ...mailjetEnv, WORKER_ENABLED: "true", WORKER_QUEUE_GROUP: "general", }, io, { databaseProbe: vi.fn(), redisProbe: vi.fn() } ); expect(exitCode).toBe(0); expect(readOutput()).not.toContain("super-secret"); expect(readOutput()).not.toContain("db-secret"); expect(readOutput()).not.toContain("redis://user:super-secret@localhost:6379/0"); expect(JSON.parse(readOutput())).toMatchObject({ databaseStatus: "ready", redisSource: "BULLMQ_REDIS_URL", redisStatus: "ready", status: "ready" }); }); });