Skip to content

Commit a083bd5

Browse files
committed
test(xai): pin hosted-call destination boundary
1 parent f63cdcb commit a083bd5

2 files changed

Lines changed: 87 additions & 0 deletions

File tree

‎tests/responses-undeclared-tool-guard.test.ts‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1311,3 +1311,68 @@ describe("provider-executed hosted calls", () => {
13111311
)).toBeUndefined();
13121312
});
13131313
});
1314+
1315+
describe("xAI hosted-call authorization through handleResponses", () => {
1316+
const hostedCall = {
1317+
type: "custom_tool_call",
1318+
id: "ctc_search",
1319+
call_id: "xs_call-1",
1320+
name: "x_keyword_search",
1321+
input: "{}",
1322+
status: "completed",
1323+
};
1324+
1325+
async function post(baseUrl: string): Promise<Response> {
1326+
const config = {
1327+
port: 0,
1328+
defaultProvider: "fixture",
1329+
providers: {
1330+
fixture: {
1331+
adapter: "openai-responses",
1332+
baseUrl,
1333+
authMode: "key",
1334+
apiKey: "fixture-key",
1335+
},
1336+
},
1337+
} as OcxConfig;
1338+
const savedFetch = globalThis.fetch;
1339+
globalThis.fetch = (async () => Response.json({
1340+
id: "resp_search",
1341+
status: "completed",
1342+
output: [hostedCall],
1343+
})) as typeof fetch;
1344+
try {
1345+
return await handleResponses(new Request("http://localhost/v1/responses", {
1346+
method: "POST",
1347+
headers: { "content-type": "application/json" },
1348+
body: JSON.stringify({
1349+
model: "fixture/grok-4.6",
1350+
stream: false,
1351+
input: [{ role: "user", content: [{ type: "input_text", text: "search" }] }],
1352+
tools: [
1353+
{ type: "function", name: "shell", parameters: { type: "object" } },
1354+
{ type: "x_search" },
1355+
],
1356+
}),
1357+
}), config, { model: "", provider: "" });
1358+
} finally {
1359+
globalThis.fetch = savedFetch;
1360+
}
1361+
}
1362+
1363+
test("accepts the measured xs_call shape for an exact xAI destination", async () => {
1364+
const response = await post("https://api.x.ai/v1");
1365+
1366+
expect(response.status).toBe(200);
1367+
const body = await response.json() as { output: Array<Record<string, unknown>> };
1368+
expect(body.output[0]).toMatchObject(hostedCall);
1369+
});
1370+
1371+
test("rejects the identical item for a lookalike destination", async () => {
1372+
const response = await post("https://api.x.ai.evil.test/v1");
1373+
1374+
expect(response.status).toBe(502);
1375+
const body = await response.json() as { error: { message: string } };
1376+
expect(body.error.message).toContain('undeclared client tool "x_keyword_search"');
1377+
});
1378+
});

‎tests/xai-transport.test.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { createOpenAIChatAdapter } from "../src/adapters/openai-chat";
33
import { parseRequest } from "../src/responses/parser";
44
import { buildModelsRequest } from "../src/oauth";
55
import {
6+
isXaiResponsesDestination,
67
resolveProviderTransport,
78
deriveXaiConvId,
89
XAI_CONV_ID_HEADER,
@@ -51,6 +52,27 @@ function parsed(): OcxParsedRequest {
5152
};
5253
}
5354

55+
describe("xAI Responses destination detection", () => {
56+
test.each([
57+
"https://api.x.ai/v1",
58+
"https://api.x.ai:443/v1",
59+
XAI_GROK_CLI_BASE_URL,
60+
"https://CLI-CHAT-PROXY.GROK.COM:443/v1",
61+
])("accepts the exact xAI HTTPS destination %s", baseUrl => {
62+
expect(isXaiResponsesDestination({ baseUrl })).toBe(true);
63+
});
64+
65+
test.each([
66+
"http://api.x.ai/v1",
67+
"https://api.x.ai:444/v1",
68+
"https://api.x.ai.evil.test/v1",
69+
"https://cli-chat-proxy.grok.com.evil.test/v1",
70+
"not a URL",
71+
])("rejects a non-xAI or malformed destination %s", baseUrl => {
72+
expect(isXaiResponsesDestination({ baseUrl })).toBe(false);
73+
});
74+
});
75+
5476
describe("xAI auth-mode transport selection", () => {
5577
test("OAuth selects the Grok CLI subscription transport and required headers", () => {
5678
const effective = resolveProviderTransport("xai", provider("oauth"));

0 commit comments

Comments
 (0)