Files
OmniRoute/tests/unit/kimi-web-401-retry.test.ts
Reza Rezaei 6efb01a957 feat(sse): add kimi web token lifecycle manager, rolling auto-refresh and 401 recovery (#10944)
Validado no worktree combinado: typecheck:core, changelog-integrity, complexity, cognitive-complexity, file-size, lint e 52 testes focados (kimi-jwt, kimi-credentials-extract, kimi-token-refresh, kimi-web-401-retry, provider-refresh-token-route, token-health-check-kimi) todos verdes. Implementação sólida e bem testada de ciclo de vida de token para Kimi Web. CI vermelho é o base-red já rastreado em #9985. Obrigado!
2026-08-21 04:25:56 -03:00

48 lines
1.8 KiB
TypeScript

import { describe, it } from "node:test";
import assert from "node:assert/strict";
import * as mod from "../../open-sse/executors/kimi-web.ts";
describe("Kimi Web Executor 401 Retry", () => {
it("transparently attempts token refresh and retries once on 401 response", async () => {
const executor = new mod.KimiWebExecutor();
let callCount = 0;
const originalFetch = globalThis.fetch;
try {
globalThis.fetch = (async (url: Parameters<typeof fetch>[0], init?: RequestInit) => {
callCount++;
if (callCount === 1) {
// Upstream chat fails with 401 unauthenticated
return new Response(JSON.stringify({ code: "unauthenticated" }), { status: 401 });
}
if (String(url).includes("/api/auth/token/refresh")) {
// Refresh endpoint returns new access token
return new Response(
JSON.stringify({
access_token: "new_refreshed_access_token",
refresh_token: "new_refresh_token",
}),
{ status: 200, headers: { "Content-Type": "application/json" } }
);
}
// Second chat attempt succeeds
return new Response(new ReadableStream({ start: (c) => c.close() }), {
status: 200,
headers: { "content-type": "application/connect+json" },
});
}) as typeof fetch;
const result = await executor.execute({
model: "k2d6",
body: { messages: [{ role: "user", content: "hi" }] },
stream: false,
credentials: { apiKey: "old_expired_token", refreshToken: "sample_refresh" },
signal: null,
} as never);
assert.ok(callCount >= 2, `Expected retry to occur after 401, but callCount was ${callCount}`);
} finally {
globalThis.fetch = originalFetch;
}
});
});