Files
OmniRoute/tests/unit/oauth-grok-cli-browser.test.ts
Diego Rodrigues de Sa e Souza 3d4f3e4960 test(infra): retry recursive temp-dir removal instead of failing a shard on ENOTEMPTY (#11966) (#11968)
* test(infra): retry recursive temp-dir removal instead of failing a shard on ENOTEMPTY (#11966)

Two shards on release/v3.8.51 went red in one day with the same signature —
"ENOTEMPTY, Directory not empty: /tmp/omniroute-<test>-XXXXXX" — from
combo-same-provider-cascade (Unit Tests fast-path 4/4, on a PR that touches only
.github/) and auth-policy-embeddings-webfetch-7785 (the 20k-test TIA step). Both pass
alone and on re-run: the cleanup races something still writing into the directory
(SQLite WAL/-shm checkpoint, a worker, the backup) and under a loaded hosted runner
the window opens. 1154 test files do their own cleanup with
fs.rmSync(dir, { recursive: true, force: true }); 57 already asked for retries.

One-shot codemod (scripts/ad-hoc/codemod-rm-maxretries.mjs, kept for the record):
every rm / rmSync / rmdirSync option object with `recursive: true` and no
`maxRetries` gains `maxRetries: 5, retryDelay: 100` — Node itself then retries
ENOTEMPTY/EBUSY/EPERM for up to ~0.5 s before giving up. 2243 call sites in 1292
files under tests/, the shared tests/_setup/isolateDataDir.ts exit hook included.
Only the option object changes: no call site, assertion or import is touched.

Validation: prettier and ESLint (with the frozen suppressions) clean on all 1292
files; a random 20-file sample runs green (quota-redis-store hangs identically on
the untouched tree — it needs a Redis on localhost, an environment matter). The
four unit shards on this PR are the full run.

* fix(quality): let check-forgotten-sibling-tests read a 1,000-file diff

The gate shells out to `git diff` through execFileSync with Node's default 1 MB
maxBuffer; the 1,292-file codemod in this PR is the first diff large enough to
overflow it, and the gate died with `spawnSync git ENOBUFS` before comparing
anything. 64 MB is far above any real PR and costs nothing when unused.
2026-08-29 01:17:40 -03:00

219 lines
9.4 KiB
TypeScript

// #7013: Grok Build (grok-cli) browser login (PKCE) — unit coverage for
// everything testable without a real auth.x.ai round-trip (that half is
// validated live on the VPS per Hard Rule #18, see the PR description).
//
// DB handles released in test.after (CLAUDE.md learning: unreleased SQLite
// handles hang node:test).
import test from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-oauth-grok-cli-7013-"));
process.env.DATA_DIR = TEST_DATA_DIR;
const core = await import("../../src/lib/db/core.ts");
const settingsDb = await import("../../src/lib/db/settings.ts");
const route = await import("../../src/app/api/oauth/[provider]/[action]/route.ts");
const { generateAuthData } = await import("../../src/lib/oauth/providers.ts");
const { grokCli } = await import("../../src/lib/oauth/providers/grok-cli.ts");
const { GROK_BUILD_OAUTH_CONFIG, XAI_OAUTH_CONFIG } =
await import("../../src/lib/oauth/constants/oauth.ts");
const originalFetch = globalThis.fetch;
test.before(async () => {
await settingsDb.updateSettings({ requireLogin: false });
});
test.after(async () => {
globalThis.fetch = originalFetch;
core.resetDbInstance();
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
});
test.afterEach(() => {
globalThis.fetch = originalFetch;
});
function getRoute(provider: string, action: string, search = "") {
const request = new Request(`http://localhost:20128/api/oauth/${provider}/${action}${search}`);
return route.GET(request, { params: Promise.resolve({ provider, action }) });
}
function postRoute(provider: string, action: string, body: unknown) {
const request = new Request(`http://localhost:20128/api/oauth/${provider}/${action}`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
});
return route.POST(request, { params: Promise.resolve({ provider, action }) });
}
// ── buildAuthUrl ─────────────────────────────────────────────────────────
test("grok-cli buildAuthUrl targets GROK_BUILD_OAUTH_CONFIG.authorizeUrl with the Grok Build scope", () => {
const authData = generateAuthData("grok-cli", "http://127.0.0.1:56122/callback");
const url = new URL(authData.authUrl);
assert.equal(url.origin, "https://auth.x.ai");
assert.equal(url.pathname, "/oauth2/authorize");
assert.equal(url.searchParams.get("response_type"), "code");
assert.equal(url.searchParams.get("client_id"), GROK_BUILD_OAUTH_CONFIG.clientId);
assert.equal(url.searchParams.get("code_challenge_method"), "S256");
assert.equal(url.searchParams.get("scope"), GROK_BUILD_OAUTH_CONFIG.scope);
assert.equal(authData.fixedPort, 56122);
assert.equal(authData.callbackPath, "/callback");
assert.equal(authData.callbackHost, "127.0.0.1");
});
test("grok-cli and xai-oauth reuse the same public client id but scope Grok Build separately", () => {
assert.equal(GROK_BUILD_OAUTH_CONFIG.clientId, XAI_OAUTH_CONFIG.clientId);
assert.notEqual(GROK_BUILD_OAUTH_CONFIG.scope, XAI_OAUTH_CONFIG.scope);
assert.ok(GROK_BUILD_OAUTH_CONFIG.scope.includes("grok-cli:access"));
});
// ── loopback port collision guard ───────────────────────────────────────
test("grok-cli's loopback port does not collide with xai-oauth or codex", () => {
assert.equal(GROK_BUILD_OAUTH_CONFIG.loopbackPort, 56122);
assert.equal(XAI_OAUTH_CONFIG.loopbackPort, 56121);
assert.notEqual(GROK_BUILD_OAUTH_CONFIG.loopbackPort, XAI_OAUTH_CONFIG.loopbackPort);
assert.notEqual(GROK_BUILD_OAUTH_CONFIG.loopbackPort, 1455); // codex's fixedPort
});
// ── exchangeToken ────────────────────────────────────────────────────────
test("grok-cli exchangeToken POSTs grant_type=authorization_code with the PKCE verifier", async () => {
globalThis.fetch = async (input, init) => {
assert.equal(String(input), GROK_BUILD_OAUTH_CONFIG.tokenUrl);
assert.equal(init?.method, "POST");
assert.equal(init?.headers?.["Content-Type"], "application/x-www-form-urlencoded");
const body = init?.body as URLSearchParams;
assert.equal(body.get("grant_type"), "authorization_code");
assert.equal(body.get("client_id"), GROK_BUILD_OAUTH_CONFIG.clientId);
assert.equal(body.get("code"), "auth-code");
assert.equal(body.get("redirect_uri"), "http://127.0.0.1:56122/callback");
assert.equal(body.get("code_verifier"), "verifier");
return Response.json({
access_token: "gb-access",
refresh_token: "gb-refresh",
expires_in: 3600,
});
};
const tokens = await grokCli.exchangeToken(
GROK_BUILD_OAUTH_CONFIG,
"auth-code",
"http://127.0.0.1:56122/callback",
"verifier"
);
assert.equal(tokens.access_token, "gb-access");
});
test("grok-cli exchangeToken throws (not a raw stack leak) on a non-OK upstream response", async () => {
globalThis.fetch = async () => new Response("upstream said no", { status: 400 });
await assert.rejects(
() =>
grokCli.exchangeToken(
GROK_BUILD_OAUTH_CONFIG,
"bad-code",
"http://127.0.0.1:56122/callback",
"verifier"
),
(err: Error) => {
assert.ok(err instanceof Error);
assert.doesNotMatch(err.message, /at \//, "must not carry a stack-trace-shaped fragment");
return true;
}
);
});
// ── mapTokens: unified dispatch (browser vs paste-token) ────────────────
test("grok-cli mapTokens maps a browser PKCE exchange response (access_token shape)", () => {
const mapped = grokCli.mapTokens({
access_token: "browser-access",
refresh_token: "browser-refresh",
expires_in: 3600,
});
assert.equal(mapped.accessToken, "browser-access");
assert.equal(mapped.refreshToken, "browser-refresh");
assert.equal(mapped.expiresIn, 3600);
assert.ok(mapped.providerSpecificData);
});
test("grok-cli mapTokens still maps a pasted JWT (accessToken shape, paste-token path)", () => {
const payload = { sub: "12345", email: "paste@example.com", team_id: "team-1", tier: 2 };
const payloadBase64 = Buffer.from(JSON.stringify(payload)).toString("base64url");
const mockJwt = `eyJhbGciOiJFUzI1NiJ9.${payloadBase64}.signature`;
const mapped = grokCli.mapTokens(mockJwt);
assert.equal(mapped.accessToken, mockJwt);
assert.equal(mapped.email, "paste@example.com");
assert.equal(mapped.providerSpecificData?.userId, "12345");
});
test("grok-cli mapTokens browser output clamps expiresIn to a positive TTL (#5775 pattern, duplicated)", () => {
const mapped = grokCli.mapTokens({
access_token: "browser-access",
refresh_token: "browser-refresh",
expires_in: -100,
});
assert.ok(mapped.expiresIn >= 1, `expected expiresIn >= 1, got ${mapped.expiresIn}`);
});
// ── route dispatch: PKCE path is reachable, import-token still works ────
test("GET /api/oauth/grok-cli/authorize dispatches through the PKCE path (not the disabled-import_token branch)", async () => {
const res = await getRoute("grok-cli", "authorize");
assert.equal(res.status, 200);
const body = await res.json();
// #7013 rework: flowType stays "device_code" (the primary/default method,
// #7358) — the PKCE authUrl is built off the supportsBrowserPkce capability
// marker (providers.ts::generateAuthData), not off flowType equality.
assert.equal(body.flowType, "device_code");
assert.ok(body.authUrl, "authUrl must be present — PKCE is enabled, not disabled");
assert.ok(!("supported" in body) || body.supported !== false);
});
test("POST /api/oauth/grok-cli/exchange requires a codeVerifier (PKCE branch reached)", async () => {
const res = await postRoute("grok-cli", "exchange", {
code: "auth-code",
redirectUri: "http://127.0.0.1:56122/callback",
});
assert.equal(res.status, 400);
const body = await res.json();
assert.match(body.error.details[0].message, /Code verifier is required for grok-cli/);
});
test("POST /api/oauth/grok-cli/exchange failure returns a sanitized 500 (Hard Rule #12)", async () => {
globalThis.fetch = async () =>
new Response("upstream secret leak: token=abc123", { status: 500 });
const res = await postRoute("grok-cli", "exchange", {
code: "auth-code",
redirectUri: "http://127.0.0.1:56122/callback",
codeVerifier: "verifier",
});
assert.equal(res.status, 500);
const body = await res.json();
assert.doesNotMatch(String(body.error), /token=abc123/, "must not leak the upstream error body");
assert.doesNotMatch(String(body.error), /at \//, "must not leak a stack trace");
});
test("POST /api/oauth/grok-cli/import-token still works — no regression to the paste-token path", async () => {
const payload = { sub: "import-1", email: "import-regress@example.com", team_id: "t1", tier: 1 };
const payloadBase64 = Buffer.from(JSON.stringify(payload)).toString("base64url");
const mockJwt = `eyJhbGciOiJFUzI1NiJ9.${payloadBase64}.signature`;
const res = await postRoute("grok-cli", "import-token", { token: mockJwt });
assert.equal(res.status, 200);
const body = await res.json();
assert.equal(body.success, true);
assert.equal(body.connection.email, "import-regress@example.com");
});