Files
OmniRoute/tests/unit/chatcore-key-health.test.ts
Webman 98289a8c99 fix(sse): stop keyless pollinations 401s from poisoning the noauth pool (#9827) (#11194)
Validated on the combined batch board over tip 8a42aeeb: static gates clean (changelog, file-size 159 frozen, complexity 2621<=2774, cognitive 1181<=1223, dead-code 408<=416), typecheck:core clean, 107 focused tests green.

Keyless pollinations 401s no longer poison the noauth pool — key health classification treats the now-required-key provider correctly after #11117. chatcore-key-health + executor-pollinations green. Fixes #9827. Thank you @jonlwheat2-gif!
2026-08-23 07:01:04 -03:00

91 lines
3.9 KiB
TypeScript

// tests/unit/chatcore-key-health.test.ts
// Characterization of recordKeyHealthStatus — the per-request API-key health updater extracted
// from handleChatCore (chatCore god-file decomposition, #3501). Locks the observable in-memory
// transitions driven through apiKeyRotator: 401 → failure (warning, then invalid at the threshold),
// 2xx → success/recovery, selectedKeyId scoping, and the no-op paths (missing connectionId, and
// non-401/non-2xx statuses). The DB persistence side effect (updateProviderConnection) is moved
// byte-identically and is fire-and-forget; these tests assert the synchronous health mutations.
import { test, afterEach } from "node:test";
import assert from "node:assert/strict";
import { recordKeyHealthStatus } from "../../open-sse/handlers/chatCore/keyHealth.ts";
import { getAllKeyHealth, removeConnectionHealth } from "../../open-sse/services/apiKeyRotator.ts";
const noopLog = { warn: () => {}, error: () => {} };
const touched: string[] = [];
function creds(connectionId: string, psd: Record<string, unknown> = {}) {
touched.push(connectionId);
// Key health only applies to connections that actually carry key material —
// the synthetic noauth connection (apiKey/accessToken null) is covered by the
// dedicated #9827 no-op test below.
return { connectionId, apiKey: "kh-test-key", accessToken: null, providerSpecificData: psd };
}
afterEach(() => {
for (const c of touched.splice(0)) removeConnectionHealth(c);
});
test("missing connectionId is a no-op (no health entry created)", () => {
const before = Object.keys(getAllKeyHealth()).length;
const r = recordKeyHealthStatus(200, { providerSpecificData: {} }, noopLog);
assert.equal(r, undefined);
assert.equal(Object.keys(getAllKeyHealth()).length, before);
});
test("401 marks the selected key as failed → warning after the first failure", () => {
const conn = "kh-401-warning";
recordKeyHealthStatus(401, creds(conn), noopLog);
const h = getAllKeyHealth()[`${conn}:primary`];
assert.equal(h?.failures, 1);
assert.equal(h?.status, "warning");
});
test("401 reaches invalid at the failure threshold (2 consecutive)", () => {
const conn = "kh-401-invalid";
recordKeyHealthStatus(401, creds(conn), noopLog);
recordKeyHealthStatus(401, creds(conn), noopLog);
const h = getAllKeyHealth()[`${conn}:primary`];
assert.equal(h?.failures, 2);
assert.equal(h?.status, "invalid");
});
test("2xx after a failure resets the key to active with 0 failures", () => {
const conn = "kh-2xx-recover";
recordKeyHealthStatus(401, creds(conn), noopLog);
recordKeyHealthStatus(204, creds(conn), noopLog);
const h = getAllKeyHealth()[`${conn}:primary`];
assert.equal(h?.failures, 0);
assert.equal(h?.status, "active");
});
test("honors selectedKeyId — scopes the update to the active extra key, not primary", () => {
const conn = "kh-selected-key";
recordKeyHealthStatus(401, creds(conn, { selectedKeyId: "extra_1" }), noopLog);
const all = getAllKeyHealth();
assert.equal(all[`${conn}:extra_1`]?.status, "warning");
assert.equal(all[`${conn}:primary`], undefined);
});
test("non-401 / non-2xx status does not touch key health", () => {
const conn = "kh-5xx-noop";
recordKeyHealthStatus(500, creds(conn), noopLog);
assert.equal(getAllKeyHealth()[`${conn}:primary`], undefined);
});
test("401 on a keyless connection is a no-op — no key to fail (#9827)", () => {
const before = Object.keys(getAllKeyHealth()).length;
// Synthetic noauth credentials (authType "none") carry no key material.
recordKeyHealthStatus(
401,
{ connectionId: "noauth", apiKey: null, accessToken: null, providerSpecificData: {} },
noopLog
);
assert.equal(Object.keys(getAllKeyHealth()).length, before);
});
test("CPA pool failures do not poison the selected native connection key", () => {
const conn = "kh-cpa-pool-isolation";
recordKeyHealthStatus(401, creds(conn), noopLog, "cliproxyapi");
assert.equal(getAllKeyHealth()[`${conn}:primary`], undefined);
});