Files
OmniRoute/tests/unit/service-token-refresh.test.ts
Diego Rodrigues de Sa e Souza 888ce1a73b chore(sse): drop the iflow entry from the token-refresh TTL map (#8966)
* chore(sse): drop the iflow entry from the token-refresh TTL map

The `iflow` provider was removed from the product, but its 24-hour refresh-lead
entry outlived it in REFRESH_LEAD_MS. Surfaced during v3.8.49 homologation on the
production VPS, where startup logs carry:

    [CREDENTIALS] Warning: unknown provider "iflow" in credentials file, skipping.

Measured across src/, open-sse/, tests/ and docs/ — the identifier had exactly two
occurrences repo-wide: the map entry and one test assertion. Nothing dispatches on
it, so `getRefreshLeadMs("iflow")` now falls through to TOKEN_EXPIRY_BUFFER_MS like
any other unknown provider.

The test assertion was not deleted, it was MOVED: from "returns explicit lead time
for known providers" to "falls back to TOKEN_EXPIRY_BUFFER_MS for unknown
providers". That is alignment to the new behavior and strictly more coverage than
before — a silent reintroduction of the entry now turns the fallback case red
instead of passing unnoticed. Flagged explicitly because the test-masking gate
rightly treats a removed assertion as suspicious.

Also removes the now-redundant "Non-rotating providers" section header: every
remaining entry under it is Google-backed and the following comment already says
"permanent (non-rotating)".

    node --import tsx/esm --test tests/unit/service-token-refresh.test.ts
    # 14 pass, 0 fail

* docs(changelog): fragment for #8966

---------

Co-authored-by: diegosouzapw <diegosouzapw@users.noreply.github.com>
2026-07-30 09:07:54 -03:00

115 lines
5.0 KiB
TypeScript

import { describe, it } from "node:test";
import assert from "node:assert/strict";
const mod = await import("../../open-sse/services/tokenRefresh.ts");
describe("tokenRefresh helpers", () => {
describe("getRefreshLeadMs", () => {
it("returns explicit lead time for known providers", () => {
assert.equal(mod.getRefreshLeadMs("codex"), 5 * 60 * 1000);
assert.equal(mod.getRefreshLeadMs("openai"), 5 * 60 * 1000);
assert.equal(mod.getRefreshLeadMs("claude"), 5 * 60 * 1000);
assert.equal(mod.getRefreshLeadMs("antigravity"), 15 * 60 * 1000);
});
it("falls back to TOKEN_EXPIRY_BUFFER_MS for unknown providers", () => {
assert.equal(mod.getRefreshLeadMs("unknown-provider"), mod.TOKEN_EXPIRY_BUFFER_MS);
assert.equal(mod.getRefreshLeadMs(""), mod.TOKEN_EXPIRY_BUFFER_MS);
// `iflow` was removed from the product; its 24h entry outlived it in the TTL map.
// Asserted here rather than deleted from the "known providers" case above, so a
// silent reintroduction of the entry turns this red instead of passing unnoticed.
assert.equal(mod.getRefreshLeadMs("iflow"), mod.TOKEN_EXPIRY_BUFFER_MS);
});
it("honors a positive per-connection refreshLeadMs override", () => {
// Override beats both the provider default and the fallback buffer.
assert.equal(mod.getRefreshLeadMs("codex", { refreshLeadMs: 90_000 }), 90_000);
assert.equal(mod.getRefreshLeadMs("unknown-provider", { refreshLeadMs: 12_345 }), 12_345);
});
it("ignores invalid or non-positive override values", () => {
// Falls through to provider default / buffer when the override is unusable.
assert.equal(mod.getRefreshLeadMs("codex", null), 5 * 60 * 1000);
assert.equal(mod.getRefreshLeadMs("codex", {}), 5 * 60 * 1000);
assert.equal(mod.getRefreshLeadMs("codex", { refreshLeadMs: 0 }), 5 * 60 * 1000);
assert.equal(mod.getRefreshLeadMs("codex", { refreshLeadMs: -1 }), 5 * 60 * 1000);
assert.equal(
mod.getRefreshLeadMs("codex", { refreshLeadMs: "60000" as unknown as number }),
5 * 60 * 1000
);
assert.equal(mod.getRefreshLeadMs("codex", { refreshLeadMs: NaN }), 5 * 60 * 1000);
assert.equal(
mod.getRefreshLeadMs("unknown-provider", { refreshLeadMs: -5 }),
mod.TOKEN_EXPIRY_BUFFER_MS
);
});
});
describe("supportsTokenRefresh", () => {
it("returns true for explicitly supported providers", () => {
assert.equal(mod.supportsTokenRefresh("gemini"), true);
assert.equal(mod.supportsTokenRefresh("claude"), true);
assert.equal(mod.supportsTokenRefresh("codex"), true);
assert.equal(mod.supportsTokenRefresh("github"), true);
assert.equal(mod.supportsTokenRefresh("kiro"), true);
assert.equal(mod.supportsTokenRefresh("cline"), true);
assert.equal(mod.supportsTokenRefresh("windsurf"), true);
});
it("returns false for unknown providers without refreshUrl/tokenUrl", () => {
assert.equal(mod.supportsTokenRefresh("nonexistent-provider"), false);
});
});
describe("isUnrecoverableRefreshError", () => {
it("returns true for unrecoverable error types", () => {
assert.equal(mod.isUnrecoverableRefreshError({ error: "unrecoverable_refresh_error" }), true);
assert.equal(mod.isUnrecoverableRefreshError({ error: "refresh_token_reused" }), true);
assert.equal(mod.isUnrecoverableRefreshError({ error: "invalid_request" }), true);
assert.equal(mod.isUnrecoverableRefreshError({ error: "invalid_grant" }), true);
});
it("returns false for recoverable errors", () => {
assert.equal(mod.isUnrecoverableRefreshError({ error: "rate_limited" }), false);
assert.equal(mod.isUnrecoverableRefreshError({ error: "server_error" }), false);
});
it("returns false for null/undefined/non-object", () => {
assert.equal(mod.isUnrecoverableRefreshError(null) || false, false);
assert.equal(mod.isUnrecoverableRefreshError(undefined) || false, false);
assert.equal(mod.isUnrecoverableRefreshError("string") || false, false);
});
});
describe("isProviderBlocked", () => {
it("returns false for unknown provider", () => {
assert.equal(mod.isProviderBlocked("nonexistent"), false);
});
});
describe("diagnostic functions", () => {
it("getConnectionRefreshMutexStatus returns object", () => {
const status = mod.getConnectionRefreshMutexStatus();
assert.equal(typeof status, "object");
assert.notEqual(status, null);
});
it("getCircuitBreakerStatus returns object", () => {
const status = mod.getCircuitBreakerStatus();
assert.equal(typeof status, "object");
assert.notEqual(status, null);
});
});
describe("constants", () => {
it("TOKEN_EXPIRY_BUFFER_MS is 5 minutes", () => {
assert.equal(mod.TOKEN_EXPIRY_BUFFER_MS, 300000);
});
it("REFRESH_LEAD_MS is a record with expected keys", () => {
assert.equal(typeof mod.REFRESH_LEAD_MS, "object");
assert.equal(mod.REFRESH_LEAD_MS.codex, 5 * 60 * 1000);
});
});
});