Files
OmniRoute/tests/unit/grok-cli-oauth.test.ts
fulorgnas 2eefe2f76d feat: Add Grok Build (xAI) provider with OAuth import-token flow (#5020)
Integrated into release/v3.8.38 (rebased on tip; Hard Rule #11 fix — Grok public client_id now via resolvePublicCred(grok_id), 3 literals removed; grok-oauth 7/7 + check:public-creds green)
2026-06-26 22:31:40 -03:00

68 lines
2.7 KiB
TypeScript

import test from "node:test";
import assert from "node:assert/strict";
const { grokCli } = await import("../../src/lib/oauth/providers/grok-cli.ts");
const { resolvePublicCred } = await import("@omniroute/open-sse/utils/publicCreds");
test("Grok Build OAuth Provider - config", () => {
assert.ok(grokCli.config.clientId, "clientId should be defined");
// The public client_id must come from the embedded default (Hard Rule #11),
// not a string literal — assert it matches resolvePublicCred("grok_id").
assert.equal(
grokCli.config.clientId,
resolvePublicCred("grok_id", "GROK_OAUTH_CLIENT_ID"),
"clientId must resolve from the embedded grok_id default"
);
assert.equal(grokCli.config.tokenUrl, "https://auth.x.ai/oauth2/token");
});
test("publicCreds: grok_id embedded default is present and decodes", () => {
const decoded = resolvePublicCred("grok_id");
assert.ok(decoded.length > 0, "grok_id must decode to a non-empty client id");
});
test("Grok Build OAuth Provider - flowType is import_token", () => {
assert.equal(grokCli.flowType, "import_token");
});
test("Grok Build OAuth Provider - mapTokens from raw JWT", () => {
// Create a valid JWT with base64url-encoded payload
const payload = { sub: "12345", email: "test@example.com", team_id: "team-67890", tier: 1 };
const payloadBase64 = Buffer.from(JSON.stringify(payload)).toString("base64url");
const mockJwt = `eyJhbGciOiJFUzI1NiJ9.${payloadBase64}.signature`;
const result = grokCli.mapTokens(mockJwt, null);
assert.equal(result.accessToken, mockJwt);
assert.equal(result.refreshToken, null);
assert.equal(result.email, "test@example.com");
assert.equal(result.expiresIn, 21600);
assert.equal(result.providerSpecificData?.userId, "12345");
assert.equal(result.providerSpecificData?.teamId, "team-67890");
assert.equal(result.providerSpecificData?.tier, 1);
});
test("Grok Build OAuth Provider - mapTokens from auth.json", () => {
const authJson = {
"https://auth.x.ai::clientId": {
key: "eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6InRlc3RAZXhhbXBsZS5jb20ifQ.signature",
refresh_token: "test-refresh-token",
},
};
const result = grokCli.mapTokens(authJson, null);
assert.ok(result.accessToken.includes("eyJ"), "accessToken should be JWT");
assert.equal(result.refreshToken, "test-refresh-token");
assert.equal(result.email, "test@example.com");
});
test("Grok Build OAuth Provider - mapTokens from empty string", () => {
const result = grokCli.mapTokens("", null);
assert.equal(result.accessToken, "");
});
test("Grok Build OAuth Provider - mapTokens from object with accessToken", () => {
const input = { accessToken: "direct-token" };
const result = grokCli.mapTokens(input, null);
assert.equal(result.accessToken, "direct-token");
});