Files
OmniRoute/tests/unit/provider-test-mistral-401-classify.test.ts
Diego Rodrigues de Sa e Souza 69bbcafcb4 fix(providers): classify ambiguous Mistral 401 instead of hard auth error (#7638) (#7718)
Mistral's quota-exhausted response is a bare 401 with a contentless
{"detail":"Unauthorized"} body — byte-identical to a genuinely revoked
key. classifyFailure() in the connection-test route always resolved
this to upstream_auth_error ("Invalid API key"), hiding the real
quota-exhaustion cause and misleading operators into rotating a still-
valid key.

classifyFailure() now accepts an optional `provider` and, for a bare
Mistral 401 with no explicit auth signal in the message (no "invalid
api key" / "token invalid" / "revoked" / "access denied" text), returns
`upstream_ambiguous_auth_or_quota` instead. A Mistral 401 that DOES
carry an explicit auth signal, and any non-Mistral 401, are unaffected
and still classify as upstream_auth_error (baseline preserved).

The new branching logic lives in a new module
(mistralAmbiguousAuth.ts) rather than inline in route.ts, keeping that
frozen file's line count within its file-size-baseline.json budget.

TDD: tests/unit/provider-test-mistral-401-classify.test.ts reproduces
the bug (RED against unfixed classifyFailure), proves the fix (GREEN),
and pins the baseline non-Mistral-401 behavior per the owner's
explicit requirement.
2026-07-19 02:31:36 -03:00

28 lines
973 B
TypeScript

import test from "node:test";
import assert from "node:assert/strict";
const { classifyFailure } = await import("../../src/app/api/providers/[id]/test/route.ts");
test("#7638: a Mistral ambiguous 401 (contentless body) should not be asserted as a hard auth_failed", () => {
const d = classifyFailure({
error: '{"detail":"Unauthorized"}',
statusCode: 401,
provider: "mistral",
});
assert.notEqual(d.type, "upstream_auth_error");
});
test("#7638 baseline: a non-Mistral bare 401 still correctly classifies as upstream_auth_error", () => {
const d = classifyFailure({ error: '{"detail":"Unauthorized"}', statusCode: 401 });
assert.equal(d.type, "upstream_auth_error");
});
test("#7638: a Mistral 401 with an explicit auth-signal message still classifies as upstream_auth_error", () => {
const d = classifyFailure({
error: "Invalid API key",
statusCode: 401,
provider: "mistral",
});
assert.equal(d.type, "upstream_auth_error");
});