Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

7.3 KB
Raw Blame
/**
 * Regression test for #9289 — credential health scheduler never retries
 * failed connections after the first check.
 *
 * The fix replaces the static interval comparison in `dueConnections` with
 * a time-based per-connection backoff check (`nextAttemptAt`). This test
 * validates that:
 * 1. Connections with failures are retried after the backoff period elapses
 * 2. Healthy connections (no timing entry) are always due; after a success the
 *    connection is due again once its per-connection interval has elapsed
 * 3. OAuth connections respect the same time-based backoff
 * 4. Multiple failure levels have correct backoff durations
 * 5. The `scheduleSweep()` runs on a stable interval independent of
 *    per-connection failures
 */

import test from "node:test";
import assert from "node:assert/strict";

// ── Constants (mirrored from scheduler.ts) ────────────────────────────────

const BACKOFF_SCHEDULE = [300_000, 600_000, 1_800_000, 7_200_000]; // 5min, 10min, 30min, 2h
const DEFAULT_INTERVAL = 300_000; // 5 min

// ── Helper: fixed dueConnections predicate (time-based) ───────────────────

/**
 * Replicate the FIXED dueConnections predicate logic.
 * Uses per-connection timing with `nextAttemptAt` instead of a static
 * interval comparison that permanently excluded failed connections.
 */
function isConnectionDue(
  perConnTiming: Map<string, { lastAttemptAt: number; nextAttemptAt: number }>,
  connId: string,
  now: number
): boolean {
  const timing = perConnTiming.get(connId);
  // No timing entry = never tested or healthy → due now
  if (!timing) return true;
  // Time-based: due when the current time has passed the next attempt time
  return now >= timing.nextAttemptAt;
}

// ── Tests ─────────────────────────────────────────────────────────────────

test("connection with 1 failure IS due after backoff period elapses", () => {
  const perConnTiming = new Map<string, { lastAttemptAt: number; nextAttemptAt: number }>();
  const connId = "conn-bug-9289";
  const now = 1_000_000_000_000; // arbitrary reference time

  // Simulate first failure: set nextAttemptAt = now + backoff(1 failure)
  const backoff = BACKOFF_SCHEDULE[1]; // 600000 ms (10 min)
  perConnTiming.set(connId, { lastAttemptAt: now, nextAttemptAt: now + backoff });

  // Before backoff elapses → NOT due
  assert.equal(
    isConnectionDue(perConnTiming, connId, now + backoff - 1),
    false,
    "Connection should NOT be due before backoff elapses"
  );

  // At the exact backoff time → IS due
  assert.equal(
    isConnectionDue(perConnTiming, connId, now + backoff),
    true,
    "Connection should be due at backoff boundary"
  );

  // After backoff elapses → IS due
  assert.equal(
    isConnectionDue(perConnTiming, connId, now + backoff + 1),
    true,
    "Connection should be due after backoff elapses"
  );
});

test("OAuth connection with 1 failure is due after backoff period elapses", () => {
  const perConnTiming = new Map<string, { lastAttemptAt: number; nextAttemptAt: number }>();
  const connId = "conn-oauth-bug-9289";
  const now = 1_000_000_000_000;

  // OAuth with 1 failure: backoff = 600000
  const backoff = BACKOFF_SCHEDULE[1];
  perConnTiming.set(connId, { lastAttemptAt: now, nextAttemptAt: now + backoff });

  // Before backoff → NOT due
  assert.equal(
    isConnectionDue(perConnTiming, connId, now + backoff - 1),
    false,
    "OAuth connection should NOT be due before backoff elapses"
  );

  // After backoff → IS due
  assert.equal(
    isConnectionDue(perConnTiming, connId, now + backoff + 1),
    true,
    "OAuth connection should be due after backoff elapses"
  );
});

test("never-tested connection is always due (no perConnTiming entry)", () => {
  const perConnTiming = new Map<string, { lastAttemptAt: number; nextAttemptAt: number }>();
  const connId = "conn-fresh-9289";

  // Connection was never tested → no timing entry → always due
  assert.equal(
    isConnectionDue(perConnTiming, connId, Date.now()),
    true,
    "Never-tested connection should always be due"
  );
});

test("connection after success (timing set to interval) is due after the interval elapses", () => {
  const perConnTiming = new Map<string, { lastAttemptAt: number; nextAttemptAt: number }>();
  const connId = "conn-bug-9289";
  const now = 1_000_000_000_000;

  // Simulate failure then success: timing now holds lastAttemptAt + interval
  perConnTiming.set(connId, { lastAttemptAt: now, nextAttemptAt: now + DEFAULT_INTERVAL });

  assert.equal(
    isConnectionDue(perConnTiming, connId, now + DEFAULT_INTERVAL - 1),
    false,
    "Healthy connection should NOT be due before its interval elapses"
  );
  assert.equal(
    isConnectionDue(perConnTiming, connId, now + DEFAULT_INTERVAL),
    true,
    "Healthy connection should be due at the interval boundary"
  );
  assert.equal(
    isConnectionDue(perConnTiming, connId, now + DEFAULT_INTERVAL + 1),
    true,
    "Healthy connection should be due after its interval elapses"
  );
});

test("multiple failure levels have correct backoff durations", () => {
  const perConnTiming = new Map<string, { lastAttemptAt: number; nextAttemptAt: number }>();
  const connId = "conn-multi-fail-9289";
  const now = 1_000_000_000_000;

  for (let failures = 1; failures <= 5; failures++) {
    const backoff = BACKOFF_SCHEDULE[Math.min(failures, BACKOFF_SCHEDULE.length - 1)];
    perConnTiming.set(connId, { lastAttemptAt: now, nextAttemptAt: now + backoff });

    // Before backoff → NOT due
    assert.equal(
      isConnectionDue(perConnTiming, connId, now + backoff - 1),
      false,
      `Connection with ${failures} failures should NOT be due before backoff (${backoff}ms)`
    );

    // After backoff → IS due
    assert.equal(
      isConnectionDue(perConnTiming, connId, now + backoff + 1),
      true,
      `Connection with ${failures} failures should be due after backoff (${backoff}ms)`
    );

    perConnTiming.delete(connId);
  }
});

test("scheduleSweep uses stable interval (decoupled from maxFailures)", () => {
  // The fix decouples scheduleSweep from per-connection failures.
  // Previously, one failed connection would delay the global sweep for all
  // connections. Now the global sweep runs on a stable interval regardless
  // of individual connection failures. This test validates the new behavior
  // by asserting that per-connection timing is independent of the global
  // sweep interval.
  const perConnTiming = new Map<string, { lastAttemptAt: number; nextAttemptAt: number }>();
  const connId = "conn-failed";
  const now = 1_000_000_000_000;

  // A failed connection has a backoff of 10 min
  perConnTiming.set(connId, { lastAttemptAt: now, nextAttemptAt: now + 600_000 });

  // A fresh connection (no timing entry) should always be due
  // regardless of how many failed connections exist
  assert.equal(
    isConnectionDue(perConnTiming, "conn-fresh", now),
    true,
    "Fresh connection should be due even if other connections have pending backoff"
  );

  // The backoff is per-connection, not global
  assert.equal(
    isConnectionDue(perConnTiming, connId, now + 600_000),
    true,
    "Failed connection should be due when its own backoff elapses"
  );
});