Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

40.2 KB
Raw Blame
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";

import { logger } from "@app/lib/logger";

import { cronJobFactory } from "./cron-job";

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

// ── helpers ───────────────────────────────────────────────────────────────────

const makeRedis = () => ({
  set: vi.fn().mockResolvedValue(null),
  get: vi.fn().mockResolvedValue(null),
  del: vi.fn().mockResolvedValue(1),
  exists: vi.fn().mockResolvedValue(0),
  hset: vi.fn().mockResolvedValue(1),
  hgetall: vi.fn().mockResolvedValue(null),
  zrangebyscore: vi.fn().mockResolvedValue([]),
  zadd: vi.fn().mockResolvedValue(1),
  zrem: vi.fn().mockResolvedValue(1),
  eval: vi.fn().mockResolvedValue(0)
});

const makeRedlock = () => ({
  using: vi.fn(async (_keys: string[], _duration: number, fn: (signal: AbortSignal) => Promise<void>) => {
    const controller = new AbortController();
    return fn(controller.signal);
  })
});

// Slot claims/refreshes are serialized through a promise chain, so a tick's
// Redis write lands several microtasks after the tick itself. A single
// `await Promise.resolve()` doesn't drain that; this does.
// `advanceTimersByTimeAsync` yields to the real event loop, which drains the
// whole pending microtask queue — deterministic, unlike counting `await
// Promise.resolve()` hops.
const flushSlotOps = () => vi.advanceTimersByTimeAsync(0);

// Use a cast via unknown to satisfy the type system for the mocks
type FakeDeps = {
  redis: ReturnType<typeof makeRedis>;
  redlock: ReturnType<typeof makeRedlock>;
};
const makeFactory = (deps?: Partial<FakeDeps>) => {
  const redis = deps?.redis ?? makeRedis();
  const redlock = deps?.redlock ?? makeRedlock();
  return {
    ...cronJobFactory({
      redis: redis as never,
      redlock: redlock as never,
      slotRefreshMs: 50,
      enqueueIntervalMs: 100,
      processIntervalMs: 100,
      slotTtlMs: 200,
      leaseDurationMs: 1000,
      handlerTimeoutMs: 1000
    }),
    redis,
    redlock
  };
};

// ── lifecycle ─────────────────────────────────────────────────────────────────

beforeEach(() => {
  vi.useFakeTimers();
});
afterEach(() => {
  vi.clearAllTimers();
  vi.useRealTimers();
  vi.restoreAllMocks();
});

// ── tests ─────────────────────────────────────────────────────────────────────

describe("register", () => {
  test("throws on invalid cron pattern", () => {
    const { register } = makeFactory();
    expect(() => register({ name: "x", pattern: "not-a-cron", handler: vi.fn(), runHashTtlS: 3600 })).toThrow();
  });

  test("throws on duplicate name", () => {
    const { register } = makeFactory();
    register({ name: "x", pattern: "0 0 * * *", handler: vi.fn(), runHashTtlS: 3600 });
    expect(() => register({ name: "x", pattern: "0 0 * * *", handler: vi.fn(), runHashTtlS: 3600 })).toThrow(
      "already registered"
    );
  });

  test("disabled entry is skipped", async () => {
    const { register, start, stop, redis } = makeFactory();
    register({ name: "x", pattern: "0 0 * * *", handler: vi.fn(), runHashTtlS: 3600, enabled: false });
    start();
    expect(redis.eval).not.toHaveBeenCalled();
    await stop();
  });

  test("throws when per-entry handlerTimeoutMs exceeds factory leaseDurationMs", () => {
    const { register } = makeFactory(); // factory leaseDurationMs=1000
    expect(() =>
      register({
        name: "x",
        pattern: "0 0 * * *",
        handler: vi.fn(),
        runHashTtlS: 3600,
        handlerTimeoutMs: 5_000
      })
    ).toThrow(/handlerTimeoutMs.*must be <=.*leaseDurationMs/);
  });

  test("accepts per-entry handlerTimeoutMs when matched by per-entry leaseDurationMs", () => {
    const { register } = makeFactory();
    expect(() =>
      register({
        name: "x",
        pattern: "0 0 * * *",
        handler: vi.fn(),
        runHashTtlS: 3600,
        handlerTimeoutMs: 30 * 60_000,
        leaseDurationMs: 30 * 60_000
      })
    ).not.toThrow();
  });

  // The retry model assumes minute granularity: a 6-field pattern firing every 30s would get a
  // 7.5s window whose first retry lands at 38.5s, past its own next fire, and be marked
  // failed-final instead of retried. Reject it at registration rather than degrade quietly.
  test("rejects a sub-minute (6-field) pattern", () => {
    const { register } = makeFactory();
    expect(() => register({ name: "x", pattern: "*/30 * * * * *", handler: vi.fn(), runHashTtlS: 3600 })).toThrow(
      "must have 5 fields"
    );
  });

  test("accepts the 5-field patterns the codebase registers", () => {
    const { register } = makeFactory();
    ["*/5 * * * *", "0 0 * * *", "0 0 19 2,5,8,11 *", "23 3 * * *"].forEach((pattern, i) => {
      expect(() => register({ name: `x${i}`, pattern, handler: vi.fn(), runHashTtlS: 3 * 24 * 60 * 60 })).not.toThrow();
    });
  });

  test("throws when factory handlerTimeoutMs default exceeds factory leaseDurationMs", () => {
    const redis = makeRedis();
    const redlock = makeRedlock();
    const { register } = cronJobFactory({
      redis: redis as never,
      redlock: redlock as never,
      slotRefreshMs: 50,
      enqueueIntervalMs: 100,
      processIntervalMs: 100,
      slotTtlMs: 200,
      // No per-knob overrides — handlerTimeoutMs (default 5min) > leaseDurationMs (1s).
      leaseDurationMs: 1000
    });
    expect(() => register({ name: "x", pattern: "0 0 * * *", handler: vi.fn(), runHashTtlS: 3600 })).toThrow(
      /handlerTimeoutMs.*must be <=.*leaseDurationMs/
    );
  });
});

describe("per-entry lease duration", () => {
  test("redlock.using receives per-entry leaseDurationMs when set", async () => {
    vi.setSystemTime(new Date("2024-01-01T00:00:30Z"));
    const enqueuedAt = Date.parse("2024-01-01T00:00:00Z");

    const redis = makeRedis();
    redis.set.mockResolvedValue("OK");
    redis.eval.mockResolvedValue(1);
    redis.zrangebyscore.mockResolvedValue([`x:${enqueuedAt}`]);
    redis.hgetall.mockResolvedValue({
      name: "x",
      status: "pending",
      attempts: "0",
      enqueued_at_ms: "0"
    });

    const redlock = makeRedlock();
    const f = cronJobFactory({
      redis: redis as never,
      redlock: redlock as never,
      slotRefreshMs: 50,
      enqueueIntervalMs: 100,
      processIntervalMs: 100,
      slotTtlMs: 200,
      leaseDurationMs: 1000,
      handlerTimeoutMs: 1000
    });
    f.register({
      name: "x",
      pattern: "* * * * *",
      handler: vi.fn(),
      runHashTtlS: 3600,
      handlerTimeoutMs: 30 * 60_000,
      leaseDurationMs: 30 * 60_000
    });
    f.start();
    await vi.advanceTimersByTimeAsync(300);

    expect(redlock.using).toHaveBeenCalled();
    const [, durationArg] = redlock.using.mock.calls[0] as [unknown, number, unknown];
    expect(durationArg).toBe(30 * 60_000);

    await f.stop();
  });

  test("redlock.using falls back to factory leaseDurationMs when entry omits it", async () => {
    vi.setSystemTime(new Date("2024-01-01T00:00:30Z"));
    const enqueuedAt = Date.parse("2024-01-01T00:00:00Z");

    const redis = makeRedis();
    redis.set.mockResolvedValue("OK");
    redis.eval.mockResolvedValue(1);
    redis.zrangebyscore.mockResolvedValue([`x:${enqueuedAt}`]);
    redis.hgetall.mockResolvedValue({
      name: "x",
      status: "pending",
      attempts: "0",
      enqueued_at_ms: "0"
    });

    const redlock = makeRedlock();
    const f = cronJobFactory({
      redis: redis as never,
      redlock: redlock as never,
      slotRefreshMs: 50,
      enqueueIntervalMs: 100,
      processIntervalMs: 100,
      slotTtlMs: 200,
      leaseDurationMs: 1234,
      handlerTimeoutMs: 1000
    });
    f.register({ name: "x", pattern: "* * * * *", handler: vi.fn(), runHashTtlS: 3600 });
    f.start();
    await vi.advanceTimersByTimeAsync(300);

    expect(redlock.using).toHaveBeenCalled();
    const [, durationArg] = redlock.using.mock.calls[0] as [unknown, number, unknown];
    expect(durationArg).toBe(1234);

    await f.stop();
  });
});

describe("slot election", () => {
  test("claims slot 0 on boot when free", async () => {
    const redis = makeRedis();
    redis.set.mockResolvedValueOnce("OK");
    const { start, stop } = makeFactory({ redis });
    start();
    // Let the immediate claimOrRefreshSlot call in start() complete
    await flushSlotOps();
    expect(redis.set).toHaveBeenCalledWith("{cron}:slot:0", expect.any(String), "PX", expect.any(Number), "NX");
    await stop();
  });

  test("falls through to slot 1 when slot 0 is held", async () => {
    const redis = makeRedis();
    redis.set.mockResolvedValueOnce(null).mockResolvedValueOnce("OK");
    const { start, stop } = makeFactory({ redis });
    start();
    await flushSlotOps();
    expect(redis.set).toHaveBeenNthCalledWith(1, "{cron}:slot:0", expect.any(String), "PX", expect.any(Number), "NX");
    expect(redis.set).toHaveBeenNthCalledWith(2, "{cron}:slot:1", expect.any(String), "PX", expect.any(Number), "NX");
    await stop();
  });

  test("sits idle when all slots are held", async () => {
    const redis = makeRedis();
    redis.set.mockResolvedValue(null);
    const { register, start, stop } = makeFactory({ redis });
    register({ name: "x", pattern: "0 0 * * *", handler: vi.fn(), runHashTtlS: 3600 });
    start();
    await flushSlotOps();
    expect(redis.eval).not.toHaveBeenCalled();
    await stop();
  });

  test("refreshes held slot on next interval", async () => {
    const redis = makeRedis();
    redis.set.mockResolvedValueOnce("OK").mockResolvedValue("OK");
    const { start, stop } = makeFactory({ redis });
    start();
    await flushSlotOps();
    vi.advanceTimersByTime(60);
    await flushSlotOps();
    const xxCalls = redis.set.mock.calls.filter((c) => (c as string[]).includes("XX"));
    expect(xxCalls.length).toBeGreaterThan(0);
    await stop();
  });

  test("re-attempts NX after slot refresh fails", async () => {
    const redis = makeRedis();
    redis.set.mockResolvedValueOnce("OK").mockResolvedValueOnce(null).mockResolvedValueOnce("OK");
    const { start, stop } = makeFactory({ redis });
    start();
    await flushSlotOps();
    vi.advanceTimersByTime(60);
    await flushSlotOps();
    // Claim (NX) → refresh loses the slot (XX returns null) → re-claim (NX).
    const nxCalls = redis.set.mock.calls.filter((c) => (c as string[]).includes("NX"));
    expect(nxCalls.length).toBeGreaterThanOrEqual(2);
    await stop();
  });

  // Regression guard for the double-claim race: two refresh ticks that overlap
  // must not each run the NX loop and leave the pod holding two slots — only
  // the last-assigned one would ever be released.
  test("overlapping refresh ticks never claim two different slots", async () => {
    const redis = makeRedis();
    // Models real SET NX/XX semantics against an in-memory slot table, with the
    // held slot expiring right as the refresh observes it (XX misses). Without
    // serialization, two ticks both observe the loss, both null `currentSlot`,
    // and the second walks past the slot the first just re-took.
    const held = new Map<string, string>();
    redis.set.mockImplementation(async (key: string, value: string, _px: string, _ttl: number, mode: string) => {
      if (mode === "NX") {
        if (held.has(key)) return null;
        held.set(key, value);
        return "OK";
      }
      held.delete(key); // XX: the slot's TTL lapsed before this refresh landed
      return null;
    });

    const { start, stop } = makeFactory({ redis });
    start();
    await flushSlotOps();

    // Two refresh intervals fire back-to-back with no drain in between.
    vi.advanceTimersByTime(50);
    vi.advanceTimersByTime(50);
    await flushSlotOps();

    expect([...held.keys()]).toEqual(["{cron}:slot:0"]);
    await stop();
  });

  // These two strings are what alerting matches on, so pin them. The initial
  // claim and the periodic refresh share one code path now, and it would be easy
  // to collapse them into a single message without noticing.
  test("slot failures are logged under their own labels", async () => {
    const redis = makeRedis();
    redis.set.mockRejectedValue(new Error("redis unavailable"));
    const { start, stop } = makeFactory({ redis });
    vi.mocked(logger.error).mockClear();

    start(); // initial claim fails
    await flushSlotOps();
    vi.advanceTimersByTime(50); // refresh tick fails
    await flushSlotOps();

    const messages = vi.mocked(logger.error).mock.calls.map(([, message]) => message);
    expect(messages).toContain("cron: initial slot claim failed");
    expect(messages).toContain("cron: slot refresh failed");

    await stop();
  });
});

describe("claim and execute", () => {
  test("boundary cache skips eval when timestamp unchanged", async () => {
    const redis = makeRedis();
    redis.set.mockResolvedValue("OK");
    redis.eval.mockResolvedValue(1);
    const { register, start, stop } = makeFactory({ redis });
    register({ name: "x", pattern: "0 0 * * *", handler: vi.fn(), runHashTtlS: 3600 });
    start();
    await flushSlotOps();
    vi.advanceTimersByTime(300);
    await Promise.resolve();
    const firstCount = redis.eval.mock.calls.length;
    expect(firstCount).toBeGreaterThan(0);
    vi.advanceTimersByTime(30_000);
    await Promise.resolve();
    expect(redis.eval.mock.calls.length).toBe(firstCount);
    await stop();
  });

  test("skips row whose name is not in local registry", async () => {
    const redis = makeRedis();
    redis.set.mockResolvedValue("OK");
    redis.zrangebyscore.mockResolvedValue(["unknown:1000000000000"]);
    redis.hgetall.mockResolvedValue({ name: "unknown", status: "pending", attempts: "0" });
    const redlock = makeRedlock();
    const { register, start, stop } = makeFactory({ redis, redlock });
    register({ name: "x", pattern: "0 0 * * *", handler: vi.fn(), runHashTtlS: 3600 });
    start();
    vi.advanceTimersByTime(300);
    await Promise.resolve();
    expect(redlock.using).not.toHaveBeenCalled();
    await stop();
  });
});

// Multi-key Lua scripts (currently just ENQUEUE_RUN_LUA) must pass keys that
// hash to the same Redis Cluster slot, otherwise the server returns CROSSSLOT
// in cluster mode and every enqueue tick fails silently. This invariant test
// guards the {cron} hash-tag convention against future regressions, mirroring
// the per-queue {<name>} pattern that queue-service.ts already uses for BullMQ.
describe("redis cluster compatibility", () => {
  // Extracts the Redis Cluster hash tag (text between the first `{` and `}`)
  // from a key, or undefined if the key isn't tagged. Two keys hash to the
  // same slot iff this returns a non-empty equal value for both.
  const hashTagOf = (key: unknown): string | undefined => {
    const s = String(key);
    const start = s.indexOf("{");
    if (start < 0) return undefined;
    const end = s.indexOf("}", start + 1);
    if (end <= start + 1) return undefined;
    return s.slice(start + 1, end);
  };

  test("multi-key Lua EVAL passes keys that share a Cluster hash tag", async () => {
    vi.setSystemTime(new Date("2024-01-01T00:00:30Z"));
    const redis = makeRedis();
    redis.set.mockResolvedValue("OK");
    redis.eval.mockResolvedValue(1);

    const { register, start, stop } = makeFactory({ redis });
    register({ name: "x", pattern: "* * * * *", handler: vi.fn(), runHashTtlS: 3600 });
    start();

    // Drive at least one enqueue tick (enqueueIntervalMs=100 in makeFactory).
    await vi.advanceTimersByTimeAsync(150);

    // ENQUEUE_RUN_LUA is the only multi-key script (KEYS[1]=run, KEYS[2]=pending, KEYS[3]=last).
    // Identify its eval call by numKeys === 3 (RELEASE_SLOT_IF_MINE_LUA uses 1).
    const multiKeyEvalCalls = redis.eval.mock.calls.filter((c) => c[1] === 3);
    expect(multiKeyEvalCalls.length).toBeGreaterThan(0);

    for (const call of multiKeyEvalCalls) {
      const numKeys = Number(call[1]);
      const keys = (call as unknown[]).slice(2, 2 + numKeys);
      const tags = keys.map(hashTagOf);
      // Every key in a multi-key EVAL must carry a non-empty hash tag and
      // every tag must match — that's the precondition for Redis Cluster to
      // accept the command without returning CROSSSLOT.
      expect(tags.every((t) => t && t.length > 0)).toBe(true);
      expect(new Set(tags).size).toBe(1);
    }

    await stop();
  });
});

describe("retry backoff and handler timeout", () => {
  // Helper: wires up the mock chain so the factory will pick up a single
  // pending run for cron name "x" and invoke the registered handler.
  const setupPendingRun = (redis: ReturnType<typeof makeRedis>, runIdScheduledAt: number) => {
    redis.set.mockResolvedValue("OK"); // slot claim succeeds
    redis.eval.mockResolvedValue(1); // enqueue succeeds
    redis.zrangebyscore.mockResolvedValue([`x:${runIdScheduledAt}`]);
    redis.hgetall.mockResolvedValue({
      name: "x",
      status: "pending",
      attempts: "0",
      enqueued_at_ms: "0"
    });
  };

  test("non-final failure updates zset score to a future next_attempt_at", async () => {
    // Early in a minute: default 30s backoff fits comfortably before next fire.
    vi.setSystemTime(new Date("2024-01-01T00:00:05Z"));
    const redis = makeRedis();
    setupPendingRun(redis, Date.parse("2024-01-01T00:00:00Z"));

    const handler = vi.fn().mockRejectedValue(new Error("boom"));
    const { register, start, stop } = makeFactory({ redis });
    register({ name: "x", pattern: "* * * * *", handler, runHashTtlS: 3600, maxAttempts: 3 });
    start();

    // Slot claim + enqueue + process tick + handler reject
    await vi.advanceTimersByTimeAsync(300);

    expect(handler).toHaveBeenCalled();
    expect(redis.zadd).toHaveBeenCalled();
    const zaddArgs = redis.zadd.mock.calls[0] as unknown[];
    expect(Number(zaddArgs[1])).toBeGreaterThan(Date.now());

    // Status hash was updated to pending with next_attempt_at
    const wroteNextAttempt = redis.hset.mock.calls.some((c) =>
      (c as unknown[]).some((arg) => arg === "next_attempt_at")
    );
    expect(wroteNextAttempt).toBe(true);

    await stop();
  });

  test("retry whose backoff overflows next fire is marked failed (zrem, not zadd)", async () => {
    // 5s before next minute: backoffBase=30s overflows the interval.
    vi.setSystemTime(new Date("2024-01-01T00:00:55Z"));
    const redis = makeRedis();
    setupPendingRun(redis, Date.parse("2024-01-01T00:00:00Z"));

    const handler = vi.fn().mockRejectedValue(new Error("boom"));
    const { register, start, stop } = makeFactory({ redis });
    register({ name: "x", pattern: "* * * * *", handler, runHashTtlS: 3600, maxAttempts: 5 });
    start();

    await vi.advanceTimersByTimeAsync(300);

    expect(handler).toHaveBeenCalled();
    // Guard short-circuits before zadd
    expect(redis.zadd).not.toHaveBeenCalled();
    expect(redis.zrem).toHaveBeenCalled();
    // Status flipped to failed
    const wroteFailed = redis.hset.mock.calls.some((c) =>
      (c as unknown[]).some((arg, i, arr) => arg === "status" && arr[i + 1] === "failed")
    );
    expect(wroteFailed).toBe(true);

    await stop();
  });

  test("hung handler is marked failed-final (not pending-retry) to prevent zombie concurrency", async () => {
    vi.setSystemTime(new Date("2024-01-01T00:00:30Z"));
    const redis = makeRedis();
    setupPendingRun(redis, Date.parse("2024-01-01T00:00:00Z"));

    const handler = vi.fn().mockImplementation(() => new Promise<void>(() => {})); // never resolves
    // Build factory inline because makeFactory doesn't expose handlerTimeoutMs.
    const redlock = makeRedlock();
    const f = cronJobFactory({
      redis: redis as never,
      redlock: redlock as never,
      slotRefreshMs: 50,
      enqueueIntervalMs: 100,
      processIntervalMs: 100,
      slotTtlMs: 200,
      leaseDurationMs: 1000,
      handlerTimeoutMs: 200,
      // Bound the test's stop() drain. The handler hangs forever and new ticks
      // keep dispatching it, so without a short drainTimeoutMs the cleanup
      // would block until the production default (25s) and time the test out.
      drainTimeoutMs: 50
    });
    f.register({ name: "x", pattern: "* * * * *", handler, runHashTtlS: 3600, maxAttempts: 3 });
    f.start();

    // Slot + enqueue + handler starts hanging, then past handlerTimeoutMs.
    await vi.advanceTimersByTimeAsync(200);
    await vi.advanceTimersByTimeAsync(500);

    expect(handler).toHaveBeenCalled();
    // The handler-timeout setTimeout's reject path writes last_error containing
    // "exceeded" — strongest evidence the timeout fired and the catch ran.
    const wroteTimeoutError = redis.hset.mock.calls.some((c) =>
      (c as unknown[]).some((arg) => typeof arg === "string" && arg.includes("exceeded"))
    );
    expect(wroteTimeoutError).toBe(true);

    // Regression guard: handlerTimeoutMs must mark the run failed-final so a
    // retry doesn't race with the still-running zombie. zrem (failed-final
    // path) must fire; zadd (pending-retry path) must NOT.
    const wroteFailed = redis.hset.mock.calls.some((c) =>
      (c as unknown[]).some((arg, i, arr) => arg === "status" && arr[i + 1] === "failed")
    );
    expect(wroteFailed).toBe(true);
    expect(redis.zrem).toHaveBeenCalled();
    expect(redis.zadd).not.toHaveBeenCalled();

    // Advance past drainTimeoutMs so stop() can resolve via the drain timeout.
    const stopPromise = f.stop();
    await vi.advanceTimersByTimeAsync(100);
    await stopPromise;
  });
});

describe("min-age gate", () => {
  test("redlock is skipped while run is fresh, then fires once minProcessAgeMs has elapsed", async () => {
    vi.setSystemTime(new Date("2024-01-01T00:00:30Z"));
    const enqueuedAt = Date.now(); // captured at fake-clock baseline

    const redis = makeRedis();
    redis.set.mockResolvedValue("OK"); // slot claim succeeds
    redis.zrangebyscore.mockResolvedValue([`x:${enqueuedAt}`]);
    redis.hgetall.mockResolvedValue({
      name: "x",
      status: "pending",
      attempts: "0",
      enqueued_at_ms: String(enqueuedAt)
    });

    const redlock = makeRedlock();
    const f = cronJobFactory({
      redis: redis as never,
      redlock: redlock as never,
      slotRefreshMs: 50,
      enqueueIntervalMs: 100,
      processIntervalMs: 100,
      slotTtlMs: 200,
      leaseDurationMs: 1000,
      handlerTimeoutMs: 1000,
      minProcessAgeMs: 500
    });
    f.register({ name: "x", pattern: "* * * * *", handler: vi.fn(), runHashTtlS: 3600 });
    f.start();

    // Process ticks at 100 / 200 / 300ms have all seen the run, but age
    // (300ms) < minProcessAgeMs (500ms) — redlock must remain idle.
    await vi.advanceTimersByTimeAsync(300);
    expect(redlock.using).not.toHaveBeenCalled();

    // The tick at ≥500ms sees age ≥ 500ms and clears the gate.
    await vi.advanceTimersByTimeAsync(300); // total 600ms elapsed
    expect(redlock.using).toHaveBeenCalled();

    await f.stop();
  });
});

describe("stop", () => {
  // Locally mirrored from the retry-backoff suite — `setupPendingRun` lives in
  // that describe's scope, but the graceful-shutdown tests below need the same
  // pending-run fixture without coupling the two suites.
  const setupPendingRun = (redis: ReturnType<typeof makeRedis>, runIdScheduledAt: number) => {
    redis.set.mockResolvedValue("OK");
    redis.eval.mockResolvedValue(1);
    redis.zrangebyscore.mockResolvedValue([`x:${runIdScheduledAt}`]);
    redis.hgetall.mockResolvedValue({
      name: "x",
      status: "pending",
      attempts: "0",
      enqueued_at_ms: "0"
    });
  };

  // Identifies the slot-release eval (only Lua script that contains `del`).
  const isSlotReleaseEval = (call: unknown[]): boolean => typeof call[0] === "string" && call[0].includes("del");

  test("releases held slot atomically", async () => {
    const redis = makeRedis();
    redis.set.mockResolvedValue("OK");
    redis.eval.mockResolvedValue(1);
    const { start, stop } = makeFactory({ redis });
    start();
    vi.advanceTimersByTime(300);
    await Promise.resolve();
    await stop();
    const lastEval = redis.eval.mock.calls.at(-1) as unknown[];
    expect(lastEval[0]).toContain("del");
    expect(String(lastEval[2])).toContain("{cron}:slot:");
  });

  // Regression guard for the BullMQ Worker.close() drain semantics: stop()
  // must wait for in-flight handlers to settle before releasing the slot,
  // otherwise destructive handlers (rotations, deletions) get torn down
  // mid-flight and leases linger until their TTL elapses.
  test("drains in-flight handler before releasing the slot", async () => {
    vi.setSystemTime(new Date("2024-01-01T00:00:30Z"));
    const redis = makeRedis();
    setupPendingRun(redis, Date.parse("2024-01-01T00:00:00Z"));

    // Deferred-style handler: the test resolves it manually so we can assert
    // ordering between handler completion and slot release. Multiple ticks
    // can fire while the handler is in-flight (status stays "pending" in the
    // mock), so we collect every resolver to release them all together.
    const pendingResolvers: Array<() => void> = [];
    const handler = vi.fn().mockImplementation(
      () =>
        new Promise<void>((resolve) => {
          pendingResolvers.push(resolve);
        })
    );
    const redlock = makeRedlock();
    const f = cronJobFactory({
      redis: redis as never,
      redlock: redlock as never,
      slotRefreshMs: 50,
      enqueueIntervalMs: 100,
      processIntervalMs: 100,
      slotTtlMs: 200,
      // Lease must cover handlerTimeoutMs; raised together so the
      // registration-time invariant accepts the entry.
      leaseDurationMs: 60_000,
      handlerTimeoutMs: 60_000,
      drainTimeoutMs: 5_000
    });
    f.register({ name: "x", pattern: "* * * * *", handler, runHashTtlS: 3600, maxAttempts: 3 });
    f.start();

    // Drive ticks until at least one handler is mid-execution.
    await vi.advanceTimersByTimeAsync(300);
    expect(handler).toHaveBeenCalled();
    expect(pendingResolvers.length).toBeGreaterThan(0);
    expect(redis.eval.mock.calls.some(isSlotReleaseEval)).toBe(false);

    // Begin stop() but don't await yet — the drain race must keep it pending
    // while handlers are still in flight.
    const stopPromise = f.stop();

    // Flush a couple of microtask cycles. clearInterval has fired, but the
    // slot release must not yet have been issued.
    await Promise.resolve();
    await Promise.resolve();
    expect(redis.eval.mock.calls.some(isSlotReleaseEval)).toBe(false);

    // Release every in-flight handler. Once the chain
    //   handler.resolve → markCompleted → redlock.using.resolve → inFlight.delete
    // settles for all of them, drain wins the race and stop() proceeds to
    // slot release.
    pendingResolvers.forEach((resolve) => resolve());
    await stopPromise;

    expect(redis.eval.mock.calls.some(isSlotReleaseEval)).toBe(true);
  });

  // The orchestrator's grace period (e.g. Kubernetes' default 30s) caps how
  // long shutdown can block. drainTimeoutMs guards against a single hung
  // handler holding stop() open past that window — anything still in flight
  // after the timeout falls back to lease-TTL crash recovery.
  test("respects drainTimeoutMs when an in-flight handler hangs", async () => {
    vi.setSystemTime(new Date("2024-01-01T00:00:30Z"));
    const redis = makeRedis();
    setupPendingRun(redis, Date.parse("2024-01-01T00:00:00Z"));

    const handler = vi.fn().mockImplementation(() => new Promise<void>(() => {})); // never resolves
    const redlock = makeRedlock();
    const f = cronJobFactory({
      redis: redis as never,
      redlock: redlock as never,
      slotRefreshMs: 50,
      enqueueIntervalMs: 100,
      processIntervalMs: 100,
      slotTtlMs: 200,
      leaseDurationMs: 60_000,
      // Long enough that handlerTimeoutMs doesn't naturally drain inFlight.
      handlerTimeoutMs: 60_000,
      drainTimeoutMs: 200
    });
    f.register({ name: "x", pattern: "* * * * *", handler, runHashTtlS: 3600, maxAttempts: 3 });
    f.start();

    await vi.advanceTimersByTimeAsync(300);
    expect(handler).toHaveBeenCalled();

    const stopPromise = f.stop();
    // Advance past drainTimeoutMs so the timeout branch wins the race.
    await vi.advanceTimersByTimeAsync(300);
    await stopPromise;

    // Slot is released even though the handler is still hung — recovery is
    // delegated to the lease TTL on a replacement pod.
    expect(redis.eval.mock.calls.some(isSlotReleaseEval)).toBe(true);
  });
});
// Cron patterns cluster: the every-5, every-10 and every-15 minute families all
// coincide on the hour and the half hour, so a single pod can claim a dozen
// handlers in one tick and block the event loop the HTTP API shares. Jitter
// spreads the pickups; the concurrency cap below bounds them outright.
describe("derived jitter", () => {
  const SCHEDULED_AT = Date.parse("2024-01-01T00:00:00Z");
  // Mid-interval, so prevFire is SCHEDULED_AT for every pattern used below.
  const NOW = new Date("2024-01-01T00:00:30Z");
  const MAX_JITTER_MS = 5 * 60_000; // factory default
  const FRACTION = 0.25;
  const BACKOFF_MS = 30_000; // retryBackoffBaseMs default
  const NEXT_FIRE_BUFFER_MS = 1_000;

  // What the manager should derive for a given interval. Kept as an independent
  // expression of the rule rather than a copy of the implementation's `Math.min`.
  const expectedWindowMs = (intervalMs: number, maxJitterMs = MAX_JITTER_MS) =>
    Math.min(intervalMs * FRACTION, maxJitterMs, intervalMs - NEXT_FIRE_BUFFER_MS);

  // ENQUEUE_RUN_LUA is the only three-key script:
  //   eval(script, 3, runKey, pendingZset, lastKey, name, scheduledAt, ttl, id, enqueuedAtMs, eligibleAt, justBecameDue)
  const enqueuedRuns = (redis: ReturnType<typeof makeRedis>) =>
    redis.eval.mock.calls
      .filter((c) => c[1] === 3)
      .map((c) => ({
        name: String(c[5]),
        scheduledAt: Number(c[6]),
        id: String(c[8]),
        eligibleAt: Number(c[10])
      }));

  // Boots a manager, drives one enqueue tick, and returns what it wrote.
  const enqueueOnce = async (
    entries: Array<{ name: string; pattern: string; runHashTtlS?: number }>,
    maxJitterMs?: number
  ) => {
    vi.setSystemTime(NOW);
    const redis = makeRedis();
    redis.set.mockResolvedValue("OK"); // slot claim succeeds
    redis.eval.mockResolvedValue(1); // enqueue wins the race
    const f = cronJobFactory({
      redis: redis as never,
      redlock: makeRedlock() as never,
      slotRefreshMs: 50,
      enqueueIntervalMs: 100,
      processIntervalMs: 100,
      slotTtlMs: 200,
      leaseDurationMs: 1000,
      handlerTimeoutMs: 1000,
      ...(maxJitterMs === undefined ? {} : { maxJitterMs })
    });
    entries.forEach(({ name, pattern, runHashTtlS }) =>
      f.register({ name, pattern, handler: vi.fn(), runHashTtlS: runHashTtlS ?? 3 * 24 * 60 * 60 })
    );
    f.start();
    await vi.advanceTimersByTimeAsync(150);
    await f.stop();
    return enqueuedRuns(redis);
  };

  // The whole point of deriving rather than configuring: nothing is passed at the
  // registration, and the window still tracks how often the job runs.
  test("window is a fraction of the job's own interval, capped", async () => {
    const cases: Array<[string, number]> = [
      ["*/5 * * * *", 5 * 60_000],
      ["*/10 * * * *", 10 * 60_000],
      ["*/15 * * * *", 15 * 60_000],
      ["0 * * * *", 60 * 60_000],
      ["0 0 * * *", 24 * 60 * 60_000]
    ];
    // One registration per manager so each name's offset is read against its own pattern.
    for (const [pattern, intervalMs] of cases) {
      // eslint-disable-next-line no-await-in-loop
      const [run] = await enqueueOnce([{ name: "x", pattern }]);
      const offset = run.eligibleAt - run.scheduledAt;
      expect(offset).toBeGreaterThanOrEqual(0);
      expect(offset).toBeLessThan(expectedWindowMs(intervalMs));
    }
  });

  // An hourly job and a daily job both sit on the cap, so past ~1h the window stops growing.
  test("cap bounds the window for long intervals", async () => {
    const [hourly] = await enqueueOnce([{ name: "x", pattern: "0 * * * *" }]);
    const [daily] = await enqueueOnce([{ name: "x", pattern: "0 0 * * *" }]);
    expect(hourly.eligibleAt - hourly.scheduledAt).toBeLessThan(MAX_JITTER_MS);
    expect(daily.eligibleAt - daily.scheduledAt).toBeLessThan(MAX_JITTER_MS);
  });

  // The offset has to come from a stable hash of the job name, never Math.random():
  // every pod computes a run's eligibility time independently, so a per-pod offset
  // would make pods disagree and break the run-id dedup and the lease logic.
  test("two instances derive the same offset for the same job name", async () => {
    const [first] = await enqueueOnce([{ name: "telemetry-aggregated-events", pattern: "*/10 * * * *" }]);
    const [second] = await enqueueOnce([{ name: "telemetry-aggregated-events", pattern: "*/10 * * * *" }]);

    expect(first.eligibleAt).toBe(second.eligibleAt);
    expect(first.eligibleAt).toBeGreaterThan(SCHEDULED_AT);
  });

  // A shared-but-constant offset would satisfy determinism while spreading nothing.
  test("different job names land on different offsets", async () => {
    const runs = await enqueueOnce([
      { name: "telemetry-aggregated-events", pattern: "*/10 * * * *" },
      { name: "daily-resource-cleanup", pattern: "*/10 * * * *" },
      { name: "certificate-cleanup", pattern: "*/10 * * * *" },
      { name: "pki-sync-health-check", pattern: "*/10 * * * *" },
      { name: "health-alert", pattern: "*/10 * * * *" }
    ]);

    const offsets = runs.map((r) => r.eligibleAt - r.scheduledAt);
    expect(offsets).toHaveLength(5);
    expect(new Set(offsets).size).toBe(5);
  });

  // A run due at or past its own next fire would invert the run order and
  // invalidate `fitsBeforeNextFire`, which measures a retry against the raw next fire.
  test("a run is never eligible at or past its own next fire", async () => {
    const [run] = await enqueueOnce([{ name: "x", pattern: "* * * * *", runHashTtlS: 60 * 60 }]);
    const nextFire = Date.parse("2024-01-01T00:01:00Z");

    expect(run.scheduledAt).toBe(SCHEDULED_AT);
    expect(run.eligibleAt).toBeGreaterThanOrEqual(SCHEDULED_AT);
    expect(run.eligibleAt).toBeLessThanOrEqual(nextFire - NEXT_FIRE_BUFFER_MS);
  });

  // The reason JITTER_INTERVAL_FRACTION is 0.25 and not something larger: the offset is
  // spent from the same interval the retry backoff needs, so a wide window on a short
  // pattern silently converts every failure into failed-final. Cron cannot fire faster
  // than once a minute, so checking the minute case checks the worst case.
  test("the first retry still fits before the next fire, at every interval", async () => {
    const intervals: Array<[string, number]> = [
      ["* * * * *", 60_000],
      ["*/5 * * * *", 5 * 60_000],
      ["*/15 * * * *", 15 * 60_000],
      ["0 * * * *", 60 * 60_000],
      ["0 0 * * *", 24 * 60 * 60_000]
    ];
    for (const [, intervalMs] of intervals) {
      const worstOffset = expectedWindowMs(intervalMs);
      expect(worstOffset + BACKOFF_MS + NEXT_FIRE_BUFFER_MS).toBeLessThan(intervalMs);
    }

    // And the derived offset really does stay under that bound in practice.
    const [run] = await enqueueOnce([{ name: "x", pattern: "* * * * *", runHashTtlS: 60 * 60 }]);
    const offset = run.eligibleAt - run.scheduledAt;
    expect(offset + BACKOFF_MS + NEXT_FIRE_BUFFER_MS).toBeLessThan(60_000);
  });

  // Run identity is what the Redis dedup and the Redlock lease key are built
  // from, so it must stay keyed on the unjittered fire.
  test("run id and scheduled_at stay keyed on the unjittered fire", async () => {
    const [run] = await enqueueOnce([{ name: "x", pattern: "*/10 * * * *" }]);

    expect(run.id).toBe(`x:${SCHEDULED_AT}`);
    expect(run.scheduledAt).toBe(SCHEDULED_AT);
  });

  test("maxJitterMs: 0 makes every run eligible at its exact scheduled fire", async () => {
    const runs = await enqueueOnce(
      [
        { name: "x", pattern: "*/10 * * * *" },
        { name: "y", pattern: "0 0 * * *" }
      ],
      0
    );

    expect(runs).toHaveLength(2);
    runs.forEach((run) => expect(run.eligibleAt).toBe(run.scheduledAt));
  });

  test("factory rejects a negative maxJitterMs", () => {
    expect(() =>
      cronJobFactory({ redis: makeRedis() as never, redlock: makeRedlock() as never, maxJitterMs: -1 })
    ).toThrow("maxJitterMs");
  });

  // A run hash that expires before its jittered pickup is reaped as an orphan, so the job
  // would never run. The comparison is against the cap rather than the job's own window,
  // which is what keeps it independent of the pattern.
  test("register rejects a runHashTtlS that the cap could outlive", () => {
    const { register } = makeFactory();
    expect(() => register({ name: "x", pattern: "0 0 * * *", handler: vi.fn(), runHashTtlS: 60 })).toThrow(
      "must exceed the maximum jitter window"
    );
  });

  // The cap is deliberately set below every live registration's TTL, so the unconditional
  // check costs nothing. This is the tightest one in the codebase.
  test("register accepts the smallest runHashTtlS any live job uses", () => {
    const { register } = makeFactory();
    expect(() =>
      register({ name: "x", pattern: "*/15 * * * *", handler: vi.fn(), runHashTtlS: 15 * 60 })
    ).not.toThrow();
  });

  // Each fire derives its window from the gap it sits in, so an uneven pattern gives different
  // fires different windows. A guard that sampled one gap had a verdict that depended on which
  // gap the process booted next to: registering at :00:10 rejected a 20s TTL while :01:10
  // accepted it, and fires in the wider gap then outlived the hash and vanished with no trace.
  // Comparing against the cap removes the sampling, so the verdict is the same from anywhere.
  describe("uneven patterns", () => {
    // Fires at :00,:01,:05,:06,... so gaps alternate 1 minute and 4 minutes.
    const UNEVEN = "0,1,5,6,10,11,15,16,20,21,25,26,30,31,35,36,40,41,45,46,50,51,55,56 * * * *";
    const BOOT_TIMES = ["2024-01-01T00:00:10Z", "2024-01-01T00:01:10Z", "2024-01-01T00:02:00Z"];

    test("the guard's verdict does not depend on when the process booted", () => {
      for (const now of BOOT_TIMES) {
        vi.setSystemTime(new Date(now));
        const { register } = makeFactory();
        expect(() => register({ name: "x", pattern: UNEVEN, handler: vi.fn(), runHashTtlS: 20 })).toThrow(
          "must exceed the maximum jitter window"
        );
      }
    });

    test("a TTL above the cap is accepted from any gap", () => {
      for (const now of BOOT_TIMES) {
        vi.setSystemTime(new Date(now));
        const { register } = makeFactory();
        expect(() => register({ name: "x", pattern: UNEVEN, handler: vi.fn(), runHashTtlS: 30 * 60 })).not.toThrow();
      }
    });

    // The property the cap buys: no fire of any pattern, uneven included, is held back
    // longer than a TTL that already clears the cap.
    test("no fire of an uneven pattern is held back past the cap", async () => {
      const runs = await enqueueOnce([{ name: "x", pattern: UNEVEN, runHashTtlS: 30 * 60 }]);
      expect(runs.length).toBeGreaterThan(0);
      runs.forEach((run) => expect(run.eligibleAt - run.scheduledAt).toBeLessThan(MAX_JITTER_MS));
    });
  });

  // A run whose hash expires before it is claimed is gone, and nothing else records it:
  // it never completed, never failed, and its zset entry is reaped on sight. The log line
  // is the only trace, so it is the thing an operator can alert on.
  test("reaping a run whose hash expired before pickup is logged as an error", async () => {
    vi.setSystemTime(new Date("2024-01-01T00:00:30Z"));
    const redis = makeRedis();
    redis.set.mockResolvedValue("OK");
    redis.zrangebyscore.mockResolvedValue(["x:1704067200000"]);
    redis.hgetall.mockResolvedValue({}); // hash gone, zset entry lingers
    vi.mocked(logger.error).mockClear();

    const { register, start, stop } = makeFactory({ redis });
    register({ name: "x", pattern: "*/10 * * * *", handler: vi.fn(), runHashTtlS: 3600 });
    start();
    await vi.advanceTimersByTimeAsync(300);

    const messages = vi.mocked(logger.error).mock.calls.map(([a, b]) => (typeof a === "string" ? a : b));
    expect(messages.some((m) => typeof m === "string" && m.includes("expired before it was claimed"))).toBe(true);
    expect(redis.zrem).toHaveBeenCalledWith("{cron}:pending", "x:1704067200000");

    await stop();
  });
});