Files
OmniRoute/tests/unit/grok-cli-proactive-refresh-7610.test.ts
backryun 0df0ff2ddb feat(grok-cli): align with official Grok Build client (#7358)
Rebuilt clean on release/v3.8.49 (branch forked from old main, ~drift). Resolved
2 real conflicts against the current tip: providerModelsConfig.ts keeps BOTH the
tip's DashScope text-model helpers (#7882) and this PR's ProviderModelsHeaderContext
type; OAuthModal.tsx takes this PR's DEVICE_CODE_PROVIDERS set (superset of the
tip's hardcoded chain + grok-cli), dropping the now-dead qwen entry (#7866 removed
qwen OAuth).

Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>
2026-07-20 19:28:21 -03:00

61 lines
2.4 KiB
TypeScript

import { test } from "node:test";
import assert from "node:assert/strict";
import { GrokCliExecutor } from "../../open-sse/executors/grok-cli.ts";
import type { ExecuteInput, ProviderCredentials } from "../../open-sse/executors/base.ts";
test("GrokCliExecutor.execute() proactively refreshes an expired access token (#7610)", async () => {
const executor = new GrokCliExecutor();
// #7358 moved grok-cli off the raw https.request()/nativePost dispatch onto the
// shared fetch-based BaseExecutor.execute() path (buildUrl/buildHeaders only) —
// which already runs the same proactive-refresh gate generically (base.ts:600).
// Stub refreshCredentials() (the network call to auth.x.ai) and global fetch (the
// upstream Grok Build call) so only the wiring is under test: does execute() call
// refreshCredentials() before dispatch, and does the refreshed token reach the
// outgoing Authorization header.
let refreshCalled = false;
executor.refreshCredentials = async () => {
refreshCalled = true;
return {
accessToken: "FRESH_ACCESS_TOKEN",
refreshToken: "rotated-refresh-token",
expiresAt: new Date(Date.now() + 3600_000).toISOString(),
};
};
let capturedHeaders: Record<string, string> | null = null;
const originalFetch = globalThis.fetch;
globalThis.fetch = (async (_url: string, init: RequestInit = {}) => {
capturedHeaders = Object.fromEntries(
new Headers(init.headers as HeadersInit).entries()
) as Record<string, string>;
return new Response(JSON.stringify({ ok: true }), { status: 200 });
}) as typeof fetch;
try {
const expiredAt = new Date(Date.now() - 60_000).toISOString();
const credentials: ProviderCredentials = {
accessToken: "STALE_ACCESS_TOKEN",
refreshToken: "valid-refresh-token",
expiresAt: expiredAt,
};
await executor.execute({
model: "grok-composer-2.5-fast",
body: { messages: [{ role: "user", content: "hi" }] },
stream: false,
credentials,
} as ExecuteInput);
assert.equal(
refreshCalled,
true,
"expected GrokCliExecutor.execute() to proactively call refreshCredentials()"
);
assert.notEqual(capturedHeaders?.["authorization"], "Bearer STALE_ACCESS_TOKEN");
assert.equal(capturedHeaders?.["authorization"], "Bearer FRESH_ACCESS_TOKEN");
} finally {
globalThis.fetch = originalFetch;
}
});