mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-22 07:02:16 +03:00
* fix(sse): de-flake timing-sensitive combo cooldown/breaker tests (#6803) Extracts 3 wall-clock-sensitive assertions (combo-quota-share cooldown ceiling x2, circuit-breaker HALF_OPEN race) into tests/unit/serial/ (--test-concurrency=1, the repo's established remedy for this class of test) and widens their margins, since a starved CI-runner event loop can blow even a serialized test's timing window. Also adds an explicit 30s vitest timeout to the MCP audit shutdown test, which had no override and inherited vitest's 5000ms default. Regression proof: reproduced RED locally under real devbox CPU contention (2644ms/1796ms elapsed vs the old 1500ms ceiling, exactly the reported failure mode); confirmed GREEN after the fix under the same contention. * fix(quality): register new serial timing tests + prune stale any-suppression count - stryker.conf.json: add tests/unit/serial/combo-quota-share-cooldown-wait-timing.test.ts and tests/unit/serial/combo-strategy-fallbacks-half-open-timing.test.ts to tap.testFiles so their mutant kills count for accountFallback.ts and circuitBreaker.ts (PR #6897 added these files but didn't register them). - eslint-suppressions.json: combo-strategy-fallbacks.test.ts's no-explicit-any suppression count was stale (35) after this PR trimmed 2 any-usages out of the file when extracting the half-open timing test; corrected to 33. Co-authored-by: Diego Rodrigues de Sa e Souza <souzamiriamrodrigues790@gmail.com> --------- Co-authored-by: Diego Rodrigues de Sa e Souza <souzamiriamrodrigues790@gmail.com>
194 lines
6.5 KiB
TypeScript
194 lines
6.5 KiB
TypeScript
/**
|
|
* TDD (integration) — quota-share combo cooldown-aware retry (Variante A),
|
|
* exercised through handleComboChat.
|
|
*
|
|
* Scenarios (modelled on model-lockout-max-cooldown.test.ts):
|
|
* 1. strategy="quota-share", single connection, model hits a 429 with a SHORT
|
|
* retry-after → the combo WAITS out the cooldown and re-dispatches; the 2nd
|
|
* pass (lock expired) returns 200 instead of propagating the 429.
|
|
* 2. A 403 (quota_exhausted, locked until midnight) → NO wait, the 403/429 is
|
|
* propagated immediately (the helper's critical exclusion).
|
|
* 3. Client abort DURING the wait → 499 "Request aborted".
|
|
* 4. strategy="priority" (non quota-share) → unchanged: the 429 is propagated
|
|
* immediately with NO wait.
|
|
* 5. comboCooldownWait disabled in settings → unchanged: 429 propagated, no wait.
|
|
*
|
|
* The waits use a real (short) cooldown so the real setTimeout in
|
|
* waitForCooldownAwareRetry elapses fast and the model lock expires naturally.
|
|
*
|
|
* Scenarios 2 and 4 assert a wall-clock ceiling and were extracted to
|
|
* tests/unit/serial/combo-quota-share-cooldown-wait-timing.test.ts (#6803) —
|
|
* see that file's header for why.
|
|
*/
|
|
import test from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import fs from "node:fs";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
|
|
const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omr-combo-cooldown-wait-"));
|
|
process.env.DATA_DIR = TEST_DATA_DIR;
|
|
process.env.API_KEY_SECRET = "test-combo-cooldown-wait-secret";
|
|
|
|
const core = await import("../../src/lib/db/core.ts");
|
|
const { handleComboChat } = await import("../../open-sse/services/combo.ts");
|
|
const { clearAllModelLockouts } = await import("../../open-sse/services/accountFallback.ts");
|
|
|
|
function createLog() {
|
|
return { info: () => {}, warn: () => {}, error: () => {}, debug: () => {} };
|
|
}
|
|
|
|
// A short transient cooldown so the real wait is fast but the lock genuinely
|
|
// expires between passes. The 429 carries a retry-after hint slightly LONGER
|
|
// than baseCooldownMs so waiting it out guarantees the lock has cleared.
|
|
const BASE_COOLDOWN_MS = 150;
|
|
const RETRY_AFTER_MS = 250;
|
|
|
|
function shortModelLockoutSettings() {
|
|
return {
|
|
modelLockout: {
|
|
enabled: true,
|
|
errorCodes: [403, 429],
|
|
baseCooldownMs: BASE_COOLDOWN_MS,
|
|
maxCooldownMs: 5000,
|
|
maxBackoffSteps: 0,
|
|
useExponentialBackoff: false,
|
|
},
|
|
};
|
|
}
|
|
|
|
function jsonResponse(status: number, body: Record<string, unknown>) {
|
|
return new Response(JSON.stringify(body), {
|
|
status,
|
|
headers: { "content-type": "application/json" },
|
|
});
|
|
}
|
|
|
|
function okResponse() {
|
|
return jsonResponse(200, { id: "ok", choices: [{ message: { content: "recovered" } }] });
|
|
}
|
|
|
|
function rateLimitResponse(status: number) {
|
|
return jsonResponse(status, {
|
|
error: { message: `rate limited (${status})` },
|
|
// string ISO retry-after → computeClosestRetryAfter yields ~RETRY_AFTER_MS
|
|
retryAfter: new Date(Date.now() + RETRY_AFTER_MS).toISOString(),
|
|
});
|
|
}
|
|
|
|
function comboOf(strategy: string) {
|
|
return {
|
|
name: `qtSd/${strategy}-${Math.random().toString(16).slice(2, 8)}`,
|
|
strategy,
|
|
models: ["openai/gpt-4"],
|
|
config: { maxRetries: 0, retryDelayMs: 0, fallbackDelayMs: 0, maxSetRetries: 0 },
|
|
};
|
|
}
|
|
|
|
async function resetStorage() {
|
|
core.resetDbInstance();
|
|
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true });
|
|
fs.mkdirSync(TEST_DATA_DIR, { recursive: true });
|
|
}
|
|
|
|
test.beforeEach(async () => {
|
|
clearAllModelLockouts();
|
|
await resetStorage();
|
|
});
|
|
|
|
test.after(async () => {
|
|
clearAllModelLockouts();
|
|
try {
|
|
core.resetDbInstance();
|
|
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true });
|
|
} catch {
|
|
/* best effort */
|
|
}
|
|
});
|
|
|
|
test("quota-share: short 429 cooldown → waits and re-dispatches (2nd pass 200)", async () => {
|
|
let calls = 0;
|
|
const handleSingleModel = async () => {
|
|
calls += 1;
|
|
// 1st dispatch: transient 429 (records a rate_limit lock). 2nd dispatch
|
|
// (after the wait, lock expired): success.
|
|
return calls === 1 ? rateLimitResponse(429) : okResponse();
|
|
};
|
|
|
|
const startedAt = Date.now();
|
|
const res = await handleComboChat({
|
|
body: { model: "openai/gpt-4" },
|
|
combo: comboOf("quota-share"),
|
|
handleSingleModel,
|
|
isModelAvailable: async () => true,
|
|
log: createLog() as never,
|
|
settings: shortModelLockoutSettings(),
|
|
allCombos: null,
|
|
});
|
|
const elapsed = Date.now() - startedAt;
|
|
|
|
assert.equal(res.status, 200, "expected the retried dispatch to succeed with 200");
|
|
assert.equal(calls, 2, "expected exactly one wait+redispatch (2 upstream calls)");
|
|
assert.ok(
|
|
elapsed >= BASE_COOLDOWN_MS,
|
|
`expected to have waited out the cooldown, only ${elapsed}ms elapsed`
|
|
);
|
|
});
|
|
|
|
// NOTE: "quota-share: 403 quota_exhausted → NO wait" and "non quota-share
|
|
// (priority): 429 propagated immediately, NO wait" were extracted to
|
|
// tests/unit/serial/combo-quota-share-cooldown-wait-timing.test.ts (#6803) —
|
|
// both assert a wall-clock ceiling that flaked under CI-runner load; the
|
|
// serial dir (--test-concurrency=1) removes the intra-suite contention that
|
|
// caused it.
|
|
|
|
test("quota-share: client abort during the wait → 499", async () => {
|
|
const controller = new AbortController();
|
|
let calls = 0;
|
|
const handleSingleModel = async () => {
|
|
calls += 1;
|
|
// Always 429 so the loop reaches the wait; abort fires during the wait.
|
|
return rateLimitResponse(429);
|
|
};
|
|
|
|
// Abort shortly after the request starts — within the cooldown wait window.
|
|
setTimeout(() => controller.abort(), 50);
|
|
|
|
const res = await handleComboChat({
|
|
body: { model: "openai/gpt-4" },
|
|
combo: comboOf("quota-share"),
|
|
handleSingleModel,
|
|
isModelAvailable: async () => true,
|
|
log: createLog() as never,
|
|
settings: shortModelLockoutSettings(),
|
|
allCombos: null,
|
|
signal: controller.signal,
|
|
});
|
|
|
|
assert.equal(res.status, 499, "abort during the cooldown wait must return 499");
|
|
});
|
|
|
|
test("quota-share with comboCooldownWait disabled → 429 propagated, NO wait", async () => {
|
|
let calls = 0;
|
|
const handleSingleModel = async () => {
|
|
calls += 1;
|
|
return rateLimitResponse(429);
|
|
};
|
|
|
|
const res = await handleComboChat({
|
|
body: { model: "openai/gpt-4" },
|
|
combo: comboOf("quota-share"),
|
|
handleSingleModel,
|
|
isModelAvailable: async () => true,
|
|
log: createLog() as never,
|
|
settings: {
|
|
...shortModelLockoutSettings(),
|
|
resilienceSettings: { comboCooldownWait: { enabled: false } },
|
|
},
|
|
allCombos: null,
|
|
});
|
|
|
|
assert.equal(res.status, 429, "disabled feature must propagate the 429 unchanged");
|
|
assert.equal(calls, 1, "disabled feature must NOT wait+redispatch");
|
|
});
|