import { Queue } from "bullmq";
import type IORedis from "ioredis";
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
import {
  ATTACHMENT_EXPORT_JOB_OPTIONS,
  ATTACHMENT_EXPORT_QUEUE_NAME,
  AUTHZED_PROJECTION_QUEUE_NAME,
  AUTHZED_RECONCILIATION_QUEUE_NAME,
  DATA_RETENTION_QUEUE_NAME,
  JOBS_DEFAULT_JOB_OPTIONS,
  JOBS_DEFAULT_JOB_SCHEDULER_TEMPLATE_OPTIONS,
  JOBS_PREFIX,
  JOBS_QUEUE_NAME,
  JOBS_QUEUE_NAMES,
  JOB_NAMES,
  WEBHOOK_DELIVERY_JOB_OPTIONS,
} from "./constants";
import { backgroundJobDefinitions } from "./definitions";
import {
  createJobsQueue,
  enqueueAttachmentExportJob,
  enqueueResponsePipelineJob,
  enqueueTestLogJob,
  enqueueWebhookDeliveryJob,
  enqueueWorkflowRunJob,
  getBackgroundJobProducer,
  getJobsQueue,
  recurringJobs,
  resetJobsQueueFactory,
  scheduleTestLogJobAt,
  upsertRecurringTestLogJobSchedule,
} from "./queue";
import { getRecurringJobSchedulerId } from "./schedules";

const {
  mockCloseRedisConnection,
  mockLoggerError,
  mockLoggerInfo,
  mockQueueAdd,
  mockQueueClose,
  mockQueueOn,
  mockQueueRemoveJobScheduler,
  mockQueueUpsertJobScheduler,
  mockQueueWaitUntilReady,
} = vi.hoisted(() => ({
  mockCloseRedisConnection: vi.fn(),
  mockLoggerError: vi.fn(),
  mockLoggerInfo: vi.fn(),
  mockQueueAdd: vi.fn(),
  mockQueueClose: vi.fn(),
  mockQueueOn: vi.fn(),
  mockQueueRemoveJobScheduler: vi.fn(),
  mockQueueUpsertJobScheduler: vi.fn(),
  mockQueueWaitUntilReady: vi.fn(),
}));

const mockConnection = {
  on: vi.fn(),
  quit: vi.fn().mockResolvedValue(undefined),
  disconnect: vi.fn(),
  status: "ready",
} as unknown as IORedis;

const responsePipelineJobData = {
  workspaceId: "cm8cmpnjj000108jfdr9dfqe8",
  event: "responseCreated" as const,
  response: {
    contact: null,
    contactAttributes: null,
    createdAt: new Date("2026-04-07T10:00:00.000Z"),
    data: {},
    displayId: null,
    endingId: null,
    finished: false,
    id: "cm8cmpnjj000108jfdr9dfqe6",
    language: null,
    meta: {},
    singleUseId: null,
    surveyId: "cm8cmpnjj000108jfdr9dfqe7",
    tags: [],
    updatedAt: new Date("2026-04-07T10:00:00.000Z"),
    variables: {},
  },
  surveyId: "cm8cmpnjj000108jfdr9dfqe7",
};

const surveySchedulingJobData = {
  scope: "global" as const,
};

const workflowRunJobData = {
  workflowRunId: "cm8cmpnjj000108jfdr9wrun1",
  workflowId: "cm8cmpnjj000108jfdr9wflo1",
  workspaceId: "cm8cmpnjj000108jfdr9wksp1",
};

const webhookDeliveryJobData = {
  webhookId: "cm8cmpnjj000108jfdr9whk01",
  workspaceId: responsePipelineJobData.workspaceId,
  surveyId: responsePipelineJobData.surveyId,
  event: "responseFinished" as const,
  webhookMessageId: "f".repeat(64),
  response: responsePipelineJobData.response,
  survey: {
    name: "Survey",
    type: "link" as const,
    status: "inProgress" as const,
    createdAt: new Date("2026-04-01T00:00:00.000Z"),
    updatedAt: new Date("2026-04-07T00:00:00.000Z"),
  },
};

vi.mock("@formbricks/logger", () => ({
  logger: {
    error: mockLoggerError,
    info: mockLoggerInfo,
    warn: vi.fn(),
    debug: vi.fn(),
  },
}));

vi.mock("./connection", () => ({
  createProducerConnection: vi.fn(() => mockConnection),
  getRedisUrlFromEnv: vi.fn(() => "redis://localhost:6379"),
  closeRedisConnection: mockCloseRedisConnection.mockResolvedValue(undefined),
}));

vi.mock("bullmq", () => ({
  // Every queue instance shares these mocks; `mock.contexts` records the instance each call was made on,
  // which is how a test tells the default queue from the dedicated one (see `queueNamesOf`).
  Queue: vi.fn(function MockQueue(name: string) {
    mockQueueWaitUntilReady.mockResolvedValue(undefined);

    return {
      name,
      add: mockQueueAdd,
      close: mockQueueClose,
      on: mockQueueOn,
      removeJobScheduler: mockQueueRemoveJobScheduler,
      upsertJobScheduler: mockQueueUpsertJobScheduler,
      waitUntilReady: mockQueueWaitUntilReady,
    };
  }),
}));

/** The queue each recorded call of a shared queue-method mock was made on, in call order. */
const queueNamesOf = (mock: { mock: { contexts: unknown[] } }): string[] =>
  mock.mock.contexts.map((context) => (context as { name: string }).name);

describe("@formbricks/jobs queue helpers", () => {
  beforeEach(async () => {
    await resetJobsQueueFactory();
    vi.clearAllMocks();
    vi.useFakeTimers();
    vi.setSystemTime(new Date("2026-04-07T10:00:00.000Z"));
  });

  afterEach(() => {
    vi.useRealTimers();
  });

  test("creates the shared queue with the expected defaults", () => {
    createJobsQueue({ connection: mockConnection });

    expect(Queue).toHaveBeenCalledWith(
      JOBS_QUEUE_NAME,
      expect.objectContaining({
        connection: mockConnection,
        defaultJobOptions: JOBS_DEFAULT_JOB_OPTIONS,
        prefix: JOBS_PREFIX,
      })
    );
  });

  test("uses a Redis Cluster hash-tagged prefix for BullMQ keys", () => {
    expect(JOBS_PREFIX).toBe("{formbricks:jobs}");
  });

  // An unhandled 'error' event on the queue would otherwise take the process down.
  test("logs queue errors instead of leaving the event unhandled", () => {
    createJobsQueue({ connection: mockConnection });

    expect(mockQueueOn).toHaveBeenCalledWith("error", expect.any(Function));

    const errorListener = mockQueueOn.mock.calls.find((call) => call[0] === "error")?.[1] as (
      error: Error
    ) => void;
    const queueError = new Error("queue exploded");
    errorListener(queueError);

    expect(mockLoggerError).toHaveBeenCalledWith(
      expect.objectContaining({ err: queueError, queueName: JOBS_QUEUE_NAME }),
      "BullMQ queue error"
    );
  });

  test("memoizes the producer queue", async () => {
    const first = await getJobsQueue();
    const second = await getJobsQueue();

    expect(first.queue).toBe(second.queue);
    // One Queue per known queue name, created once.
    expect(Queue).toHaveBeenCalledTimes(JOBS_QUEUE_NAMES.length);
  });

  test("creates one producer queue per queue name on a single shared connection", async () => {
    const defaultQueue = await getJobsQueue();
    const dedicatedQueue = await getJobsQueue(AUTHZED_PROJECTION_QUEUE_NAME);

    expect(defaultQueue.queue.name).toBe(JOBS_QUEUE_NAME);
    expect(dedicatedQueue.queue.name).toBe(AUTHZED_PROJECTION_QUEUE_NAME);
    expect(dedicatedQueue.connection).toBe(defaultQueue.connection);
    expect(Queue).toHaveBeenCalledWith(
      AUTHZED_PROJECTION_QUEUE_NAME,
      expect.objectContaining({
        connection: mockConnection,
        defaultJobOptions: JOBS_DEFAULT_JOB_OPTIONS,
        prefix: JOBS_PREFIX,
      })
    );
  });

  // A delivery queued behind a long sweep makes every authorization check fail closed, and the sweeps
  // themselves would hold up every default-queue job, so this pins the routing of every job: those have
  // queues of their own, everything else shares the default one.
  test("routes AuthZed delivery, the AuthZed audits, the data retention sweep and attachment exports to their own queues, and only them", () => {
    const queueNameByJob = Object.fromEntries(
      Object.values(backgroundJobDefinitions).map((definition) => [definition.name, definition.queueName])
    );
    const dedicated: Record<string, string> = {
      [JOB_NAMES.authzedProjectionDelivery]: AUTHZED_PROJECTION_QUEUE_NAME,
      [JOB_NAMES.authzedReconciliationAudit]: AUTHZED_RECONCILIATION_QUEUE_NAME,
      [JOB_NAMES.authzedSurveyAudit]: AUTHZED_RECONCILIATION_QUEUE_NAME,
      [JOB_NAMES.dataRetentionSweep]: DATA_RETENTION_QUEUE_NAME,
      [JOB_NAMES.attachmentExport]: ATTACHMENT_EXPORT_QUEUE_NAME,
    };

    for (const [jobName, queueName] of Object.entries(queueNameByJob)) {
      expect(queueName, jobName).toBe(dedicated[jobName] ?? JOBS_QUEUE_NAME);
    }
  });

  test("upserts the data retention sweep schedule on its own queue, then retires the legacy one", async () => {
    mockQueueUpsertJobScheduler.mockResolvedValue({ id: "sweep-1" });
    mockQueueRemoveJobScheduler.mockResolvedValue(false);

    await recurringJobs.dataRetentionSweep.upsert({ cronPattern: "0 1 * * *", kind: "cron" });

    expect(queueNamesOf(mockQueueUpsertJobScheduler)).toEqual([DATA_RETENTION_QUEUE_NAME]);
    expect(queueNamesOf(mockQueueRemoveJobScheduler)).toEqual([JOBS_QUEUE_NAME]);
  });

  test.each([
    ["authzedReconciliationAudit", 6 * 60 * 60 * 1_000],
    ["authzedSurveyAudit", 24 * 60 * 60 * 1_000],
  ] as const)(
    "upserts the %s schedule on the AuthZed reconciliation queue, then retires the legacy one",
    async (key, everyMs) => {
      mockQueueUpsertJobScheduler.mockResolvedValue({ id: `${key}-1` });
      mockQueueRemoveJobScheduler.mockResolvedValue(true);

      await recurringJobs[key].upsert({ everyMs, kind: "every" });

      expect(queueNamesOf(mockQueueUpsertJobScheduler)).toEqual([AUTHZED_RECONCILIATION_QUEUE_NAME]);
      expect(queueNamesOf(mockQueueRemoveJobScheduler)).toEqual([JOBS_QUEUE_NAME]);
      expect(mockQueueUpsertJobScheduler.mock.invocationCallOrder[0]).toBeLessThan(
        mockQueueRemoveJobScheduler.mock.invocationCallOrder[0]
      );
    }
  );

  test("upserts the AuthZed delivery schedule on its own queue, then retires the legacy one", async () => {
    mockQueueUpsertJobScheduler.mockResolvedValue({ id: "delivery-1" });
    mockQueueRemoveJobScheduler.mockResolvedValue(true);

    await recurringJobs.authzedProjectionDelivery.upsert({ everyMs: 5_000, kind: "every" });

    const schedulerId = "authzed-projection.deliver:global:authzed-projection-delivery";
    expect(mockQueueUpsertJobScheduler).toHaveBeenCalledOnce();
    expect(mockQueueUpsertJobScheduler).toHaveBeenCalledWith(
      schedulerId,
      { endDate: undefined, every: 5_000, limit: undefined, startDate: undefined },
      expect.objectContaining({ name: JOB_NAMES.authzedProjectionDelivery })
    );
    expect(queueNamesOf(mockQueueUpsertJobScheduler)).toEqual([AUTHZED_PROJECTION_QUEUE_NAME]);
    // The scheduler a previous build left on the default queue is removed by the same id…
    expect(mockQueueRemoveJobScheduler).toHaveBeenCalledExactlyOnceWith(schedulerId);
    expect(queueNamesOf(mockQueueRemoveJobScheduler)).toEqual([JOBS_QUEUE_NAME]);
    // …and only after the new one exists, so the job is never left without a schedule.
    expect(mockQueueUpsertJobScheduler.mock.invocationCallOrder[0]).toBeLessThan(
      mockQueueRemoveJobScheduler.mock.invocationCallOrder[0]
    );
    expect(mockLoggerInfo).toHaveBeenCalledWith(
      {
        jobName: JOB_NAMES.authzedProjectionDelivery,
        legacyQueueName: JOBS_QUEUE_NAME,
        queueName: AUTHZED_PROJECTION_QUEUE_NAME,
        schedulerId,
      },
      "Removed legacy BullMQ schedule from the default queue"
    );
  });

  test("treats an absent legacy schedule as already retired", async () => {
    mockQueueUpsertJobScheduler.mockResolvedValue({ id: "delivery-1" });
    mockQueueRemoveJobScheduler.mockResolvedValue(false);

    await expect(
      recurringJobs.authzedProjectionDelivery.upsert({ everyMs: 5_000, kind: "every" })
    ).resolves.toEqual({ id: "delivery-1" });

    expect(mockQueueRemoveJobScheduler).toHaveBeenCalledOnce();
    expect(mockLoggerInfo).not.toHaveBeenCalled();
  });

  test("fails the upsert when the legacy schedule cannot be retired, so registration retries it", async () => {
    const redisError = new Error("redis down");
    mockQueueUpsertJobScheduler.mockResolvedValue({ id: "delivery-1" });
    mockQueueRemoveJobScheduler.mockRejectedValueOnce(redisError);

    await expect(
      recurringJobs.authzedProjectionDelivery.upsert({ everyMs: 5_000, kind: "every" })
    ).rejects.toThrow("redis down");

    expect(mockLoggerError).toHaveBeenCalledWith(
      expect.objectContaining({ err: redisError, jobName: JOB_NAMES.authzedProjectionDelivery }),
      "Failed to upsert BullMQ AuthZed projection delivery schedule"
    );
  });

  test("leaves the default queue alone for jobs that never moved", async () => {
    mockQueueUpsertJobScheduler.mockResolvedValue({ id: "drain-1" });

    await recurringJobs.deletionCleanupDrain.upsert({ everyMs: 300_000, kind: "every" });

    expect(queueNamesOf(mockQueueUpsertJobScheduler)).toEqual([JOBS_QUEUE_NAME]);
    expect(mockQueueRemoveJobScheduler).not.toHaveBeenCalled();
  });

  test("removes the AuthZed delivery schedule from its own queue and the legacy one", async () => {
    // Only the legacy queue still holds the scheduler. Keyed on the queue rather than call order, so a
    // routing regression fails this test instead of leaking queued return values into the next one.
    mockQueueRemoveJobScheduler.mockImplementation(function (this: { name: string }) {
      return Promise.resolve(this.name === JOBS_QUEUE_NAME);
    });

    await expect(recurringJobs.authzedProjectionDelivery.remove()).resolves.toBe(true);

    expect(queueNamesOf(mockQueueRemoveJobScheduler)).toEqual([
      AUTHZED_PROJECTION_QUEUE_NAME,
      JOBS_QUEUE_NAME,
    ]);
  });

  test("enqueues the test log job with the shared queue", async () => {
    const mockJob = { id: "job-1" };
    mockQueueAdd.mockResolvedValue(mockJob);

    const job = await enqueueTestLogJob({ message: "hello world" });

    expect(job).toBe(mockJob);
    expect(mockQueueAdd).toHaveBeenCalledWith(JOB_NAMES.testLog, { message: "hello world" }, undefined);
    expect(queueNamesOf(mockQueueAdd)).toEqual([JOBS_QUEUE_NAME]);
  });

  test("enqueues the response pipeline job with the shared queue", async () => {
    const mockJob = { id: "job-response-1" };
    mockQueueAdd.mockResolvedValue(mockJob);

    const job = await enqueueResponsePipelineJob(responsePipelineJobData);

    expect(job).toBe(mockJob);
    expect(mockQueueAdd).toHaveBeenCalledWith(JOB_NAMES.responsePipeline, responsePipelineJobData, undefined);
  });

  test("enqueues the workflow run job with a deterministic jobId and the shared retry policy", async () => {
    const mockJob = { id: "job-workflow-run-1" };
    mockQueueAdd.mockResolvedValue(mockJob);

    const job = await enqueueWorkflowRunJob(workflowRunJobData, { jobId: workflowRunJobData.workflowRunId });

    expect(job).toBe(mockJob);
    // No per-job attempts override: the job inherits attempts + backoff from the queue's
    // defaultJobOptions (retries are safe now that execution is idempotent per step — ENG-1228).
    expect(mockQueueAdd).toHaveBeenCalledWith(JOB_NAMES.workflowRun, workflowRunJobData, {
      jobId: workflowRunJobData.workflowRunId,
    });
  });

  test("enqueues an attachment export on its own queue with a short retry budget and the caller's jobId", async () => {
    const data = {
      attachmentExportId: "cm8cmpnjj000108jfdr9exp01",
      workspaceId: "cm8cmpnjj000108jfdr9wks01",
      dispatchCount: 0,
    };
    mockQueueAdd.mockResolvedValue({ id: "cm8cmpnjj000108jfdr9exp01-0" });

    await enqueueAttachmentExportJob(data, { jobId: "cm8cmpnjj000108jfdr9exp01-0" });

    expect(queueNamesOf(mockQueueAdd)).toEqual([ATTACHMENT_EXPORT_QUEUE_NAME]);
    // A retry rebuilds the whole archive, so the budget is below the shared default.
    expect(mockQueueAdd).toHaveBeenCalledWith(JOB_NAMES.attachmentExport, data, {
      attempts: 2,
      backoff: { type: "fixed", delay: 30_000 },
      jobId: "cm8cmpnjj000108jfdr9exp01-0",
    });
    expect(ATTACHMENT_EXPORT_JOB_OPTIONS.attempts).toBeLessThan(JOBS_DEFAULT_JOB_OPTIONS.attempts);
  });

  test("rejects an attachment export payload without valid ids before enqueueing", async () => {
    await expect(
      enqueueAttachmentExportJob(
        { attachmentExportId: "not a cuid", workspaceId: "ws", dispatchCount: 0 },
        { jobId: "x" }
      )
    ).rejects.toThrow();
    expect(mockQueueAdd).not.toHaveBeenCalled();
  });

  test("enqueues a webhook delivery with its own retry policy and the caller's deterministic jobId", async () => {
    const mockJob = { id: "whd-job-response-1-cm8cmpnjj000108jfdr9whk01" };
    mockQueueAdd.mockResolvedValue(mockJob);

    const job = await enqueueWebhookDeliveryJob(webhookDeliveryJobData, {
      jobId: "whd-job-response-1-cm8cmpnjj000108jfdr9whk01",
    });

    expect(job).toBe(mockJob);
    // Unlike the other one-shot jobs this one overrides the queue defaults: one endpoint's retries are
    // its own budget, and the jobId makes a pipeline retry after a partial fan-out idempotent.
    expect(mockQueueAdd).toHaveBeenCalledWith(JOB_NAMES.webhookDelivery, webhookDeliveryJobData, {
      attempts: 5,
      backoff: { type: "exponential", delay: 30_000 },
      jobId: "whd-job-response-1-cm8cmpnjj000108jfdr9whk01",
    });
    expect(WEBHOOK_DELIVERY_JOB_OPTIONS.attempts).toBeGreaterThan(JOBS_DEFAULT_JOB_OPTIONS.attempts);
  });

  test("rejects a webhook delivery payload that fails schema validation before touching the queue", async () => {
    await expect(
      enqueueWebhookDeliveryJob(
        { ...webhookDeliveryJobData, webhookMessageId: "not-a-sha256" },
        { jobId: "x" }
      )
    ).rejects.toThrow();

    expect(mockQueueAdd).not.toHaveBeenCalled();
    expect(mockLoggerError).toHaveBeenCalledWith(
      expect.objectContaining({
        jobName: JOB_NAMES.webhookDelivery,
        webhookId: webhookDeliveryJobData.webhookId,
        workspaceId: webhookDeliveryJobData.workspaceId,
      }),
      "Failed to enqueue BullMQ webhook delivery job"
    );
  });

  test("exposes response pipeline enqueues through the engine-neutral producer interface", async () => {
    const producer = getBackgroundJobProducer();
    mockQueueAdd.mockResolvedValue({
      id: "job-response-1",
      name: JOB_NAMES.responsePipeline,
      queueName: JOBS_QUEUE_NAME,
    });

    const job = await producer.enqueueResponsePipeline(responsePipelineJobData);

    expect(job).toEqual({
      jobId: "job-response-1",
      jobName: JOB_NAMES.responsePipeline,
      queueName: JOBS_QUEUE_NAME,
    });
  });

  test("schedules a delayed job using the runAt schedule type", async () => {
    mockQueueAdd.mockResolvedValue({ id: "job-3" });

    await scheduleTestLogJobAt(
      { runAt: new Date("2026-04-07T10:00:05.000Z") },
      { message: "hello delayed world" }
    );

    expect(mockQueueAdd).toHaveBeenCalledWith(
      JOB_NAMES.testLog,
      { message: "hello delayed world" },
      { delay: 5000 }
    );
  });

  test("upserts a recurring scheduler using engine-neutral schedule types", async () => {
    mockQueueUpsertJobScheduler.mockResolvedValue({
      id: "job-4",
      name: JOB_NAMES.testLog,
      queueName: JOBS_QUEUE_NAME,
    });

    await upsertRecurringTestLogJobSchedule(
      {
        scheduleId: "nightly-test-log",
        scope: "environment_123",
      },
      {
        cronPattern: "0 2 * * *",
        kind: "cron",
        timeZone: "UTC",
      },
      { message: "hello recurring world" }
    );

    expect(mockQueueUpsertJobScheduler).toHaveBeenCalledWith(
      getRecurringJobSchedulerId(JOB_NAMES.testLog, {
        scheduleId: "nightly-test-log",
        scope: "environment_123",
      }),
      {
        endDate: undefined,
        immediately: undefined,
        limit: undefined,
        pattern: "0 2 * * *",
        startDate: undefined,
        tz: "UTC",
      },
      {
        data: { message: "hello recurring world" },
        name: JOB_NAMES.testLog,
        opts: JOBS_DEFAULT_JOB_SCHEDULER_TEMPLATE_OPTIONS,
      }
    );
  });

  test("upserts a recurring schedule through its handle using an every schedule", async () => {
    mockQueueUpsertJobScheduler.mockResolvedValue({
      id: "job-reconcile-1",
      name: JOB_NAMES.workflowRunReconcile,
      queueName: JOBS_QUEUE_NAME,
    });

    await recurringJobs.workflowRunReconcile.upsert({ everyMs: 180_000, kind: "every" });

    expect(mockQueueUpsertJobScheduler).toHaveBeenCalledWith(
      "workflow-run.reconcile:global:workflow-run-reconcile",
      { endDate: undefined, every: 180_000, limit: undefined, startDate: undefined },
      {
        data: { scope: "global" },
        name: JOB_NAMES.workflowRunReconcile,
        opts: JOBS_DEFAULT_JOB_SCHEDULER_TEMPLATE_OPTIONS,
      }
    );
  });

  test("upserts a recurring schedule through its handle using a cron schedule", async () => {
    mockQueueUpsertJobScheduler.mockResolvedValue({
      id: "job-scheduling-1",
      name: JOB_NAMES.surveyScheduling,
      queueName: JOBS_QUEUE_NAME,
    });

    const scheduledJob = await recurringJobs.surveyScheduling.upsert({
      cronPattern: "0 0 * * *",
      kind: "cron",
      timeZone: "Etc/GMT-1",
    });

    expect(mockQueueUpsertJobScheduler).toHaveBeenCalledWith(
      "survey-scheduling.reconcile:global:daily-survey-scheduling",
      {
        endDate: undefined,
        immediately: undefined,
        limit: undefined,
        pattern: "0 0 * * *",
        startDate: undefined,
        tz: "Etc/GMT-1",
      },
      {
        data: surveySchedulingJobData,
        name: JOB_NAMES.surveyScheduling,
        opts: JOBS_DEFAULT_JOB_SCHEDULER_TEMPLATE_OPTIONS,
      }
    );
    expect(scheduledJob.id).toBe("job-scheduling-1");
  });

  test("removes a recurring schedule using the identity it owns", async () => {
    mockQueueRemoveJobScheduler.mockResolvedValue(true);

    const removed = await recurringJobs.surveyScheduling.remove();

    expect(removed).toBe(true);
    expect(mockQueueRemoveJobScheduler).toHaveBeenCalledWith(
      "survey-scheduling.reconcile:global:daily-survey-scheduling"
    );
  });

  // These ids address schedules that already exist in production Redis. Changing one orphans the live
  // schedule instead of updating it, so they are pinned as literals here rather than derived.
  test.each([
    ["authzedProjectionDelivery", "authzed-projection.deliver:global:authzed-projection-delivery"],
    ["authzedReconciliationAudit", "authzed-reconciliation.audit:global:authzed-reconciliation-audit"],
    ["authzedSurveyAudit", "authzed-survey.audit:global:daily-authzed-survey-audit"],
    ["creativeStatsFlush", "creative-stats.flush:global:creative-stats-flush"],
    ["dataRetentionSweep", "data-retention.sweep:global:daily-data-retention-sweep"],
    ["deletionCleanupDrain", "deletion-cleanup.drain:global:deletion-cleanup-drain"],
    ["surveyArchivePurge", "survey-archive-purge.process:global:daily-survey-archive-purge"],
    ["surveyScheduling", "survey-scheduling.reconcile:global:daily-survey-scheduling"],
    ["usageTelemetry", "usage-telemetry.process:global:daily-usage-telemetry"],
    ["workflowRunReconcile", "workflow-run.reconcile:global:workflow-run-reconcile"],
    ["workflowRunRetention", "workflow-run.retention:global:workflow-run-retention"],
    ["workflowsUsageSnapshot", "workflows-usage.snapshot:global:daily-workflows-usage-snapshot"],
  ] as const)("keeps the %s scheduler id stable", async (key, expectedSchedulerId) => {
    mockQueueUpsertJobScheduler.mockResolvedValue({
      id: "job-id-parity",
      name: recurringJobs[key].name,
      queueName: JOBS_QUEUE_NAME,
    });

    await recurringJobs[key].upsert({ everyMs: 60_000, kind: "every" });

    expect(mockQueueUpsertJobScheduler).toHaveBeenCalledWith(
      expectedSchedulerId,
      expect.anything(),
      expect.objectContaining({ name: recurringJobs[key].name })
    );
    expect(getRecurringJobSchedulerId(recurringJobs[key].name, recurringJobs[key])).toBe(expectedSchedulerId);
  });

  test("rejects engine-neutral enqueues when BullMQ returns a job without an id", async () => {
    const producer = getBackgroundJobProducer();
    mockQueueAdd.mockResolvedValue({
      id: undefined,
      name: JOB_NAMES.responsePipeline,
      queueName: JOBS_QUEUE_NAME,
    });

    await expect(producer.enqueueResponsePipeline(responsePipelineJobData)).rejects.toThrow(
      "Missing BullMQ job.id in toEnqueuedJob for jobName=response-pipeline.process"
    );
  });

  test("cleans up producer resources when queue initialization fails", async () => {
    mockQueueWaitUntilReady.mockRejectedValueOnce(new Error("redis unavailable"));

    await expect(getJobsQueue()).rejects.toThrow("redis unavailable");

    // Every queue is closed, not only the one that failed to become ready.
    expect(mockQueueClose).toHaveBeenCalledTimes(JOBS_QUEUE_NAMES.length);
    expect(mockCloseRedisConnection).toHaveBeenCalledWith(mockConnection);
  });

  test("keeps cleaning up when queue shutdown fails during reset", async () => {
    await getJobsQueue();
    mockQueueClose.mockRejectedValueOnce(new Error("queue close failed"));

    await expect(resetJobsQueueFactory()).resolves.toBeUndefined();

    // The other queues and the connection are still closed after the first close fails.
    expect(mockQueueClose).toHaveBeenCalledTimes(JOBS_QUEUE_NAMES.length);
    expect(mockCloseRedisConnection).toHaveBeenCalledWith(mockConnection);
    expect(mockLoggerError).toHaveBeenCalledTimes(1);
    const loggerCalls = mockLoggerError.mock.calls as [{ err: Error; queueName: string }, string][];
    const [context, message] = loggerCalls[0];
    expect(context.err).toBeInstanceOf(Error);
    expect(JOBS_QUEUE_NAMES).toContain(context.queueName);
    expect(message).toBe("Failed to close BullMQ producer queue");
  });

  test("clears memoized state after reset so a new queue can be created", async () => {
    await getJobsQueue();

    await resetJobsQueueFactory();
    const nextQueueResult = await getJobsQueue();

    expect(nextQueueResult.queue).toBeDefined();
    expect(Queue).toHaveBeenCalledTimes(2 * JOBS_QUEUE_NAMES.length);
  });
});
