Files
OmniRoute/tests/unit/chat-early-schema-validation-6412.test.ts
Ravi Tharuma 137e49e393 feat(search): first-class X Search via SuperGrok x_search (#10988)
5 — Provider x-search de primeira classe (SuperGrok/xAI x_search) em POST /v1/search e MCP omniroute_x_search. Fallback de credenciais xai-oauth→xao→xai; distinto de web search e do X Developer MCP.

Reconciliado com o release tip (que já incluía #10981 "skip catalog-default SearXNG" deste mesmo lote): merge trouxe 5 conflitos reais de contagem gerada (llm.txt/README.md/AGENTS.md/PROVIDER_REFERENCE.md/SVGs/46 mirrors i18n, todos verificados como bump puro 347→348, sem perda de conteúdo do HEAD) + 1 conflito real de mergeable=CONFLICTING.

Durante a validação, os 3 testes novos de SearXNG expuseram um bug real de interação com #10981: `isUnconfiguredLoopbackSearchProvider()` checava o baseUrl ESTÁTICO do catálogo em vez do baseUrl efetivo (após override de `provider_options.baseUrl` ou `providerSpecificData.baseUrl` da conexão), então QUALQUER request a searxng-search — mesmo com override customizado — era rejeitado como se fosse o default não-configurado. Corrigido em `open-sse/handlers/search.ts` (resolve o baseUrl efetivo via `resolveSearchBaseUrl()` antes do skip-check, tanto para o provider primário quanto o alternate). Um teste do próprio #10988 que assumia o comportamento pré-#10981 (default localhost:8888 sempre atendido) foi atualizado para refletir o comportamento já mesclado e intencional (503 quando não configurado).

Validação completa: typecheck limpo, 70/70 testes unit (search-route/search-registry/x-search-provider/searxng-loopback-default), 24/24 vitest MCP, 14/14 integration (search-providers-catalog), lint limpo nos arquivos tocados, docs-counts-sync OK (2 drifts soft pré-existentes, não relacionados), gates estáticos (file-size/complexity/cognitive/dead-code/changelog) todos OK.
2026-08-21 14:16:42 -03:00

124 lines
4.0 KiB
TypeScript

import test from "node:test";
import assert from "node:assert/strict";
import { createChatPipelineHarness } from "../integration/_chatPipelineHarness.ts";
const harness = await createChatPipelineHarness("chat-early-schema-6412");
const { buildRequest, handleChat, resetStorage } = harness;
test.beforeEach(async () => {
await resetStorage();
});
test.after(async () => {
await harness.cleanup();
});
/**
* Regression guard for #6412 — schema validation of scalar params (temperature,
* top_p, max_tokens, n) MUST run BEFORE provider/model resolution. Previously,
* a bad `temperature: "not-a-number"` combined with an unknown provider
* returned 404 "model_not_found" — hiding the real schema error.
*/
interface ChatTestRequestBody {
model: string;
messages: Array<{ role: string; content: string }>;
// Intentionally loose: these are the scalar params under test, and several
// cases below deliberately pass the WRONG runtime type (e.g. temperature as
// a string) to prove schema validation catches it before provider lookup.
temperature?: unknown;
top_p?: unknown;
max_tokens?: unknown;
n?: unknown;
}
interface ChatTestResponsePayload {
error?: unknown;
}
async function postChat(body: ChatTestRequestBody) {
const response = await handleChat(buildRequest({ body }));
const payload = (await response.json()) as ChatTestResponsePayload;
return { status: response.status, payload };
}
test("bad temperature (string) on unknown provider → 400, not 404", async () => {
const { status, payload } = await postChat({
model: "nonexistent-provider/nonexistent-model",
messages: [{ role: "user", content: "hi" }],
temperature: "not-a-number",
});
assert.equal(status, 400);
assert.match(JSON.stringify(payload.error), /temperature/i);
});
test("out-of-range temperature (5.0) on unknown provider → 400, not 404", async () => {
const { status, payload } = await postChat({
model: "nonexistent-provider/nonexistent-model",
messages: [{ role: "user", content: "hi" }],
temperature: 5.0,
});
assert.equal(status, 400);
assert.match(JSON.stringify(payload.error), /temperature/i);
});
test("bad top_p (string) → 400", async () => {
const { status, payload } = await postChat({
model: "nonexistent-provider/nonexistent-model",
messages: [{ role: "user", content: "hi" }],
top_p: "bad",
});
assert.equal(status, 400);
assert.match(JSON.stringify(payload.error), /top_p/i);
});
test("bad max_tokens (negative) → 400", async () => {
const { status, payload } = await postChat({
model: "nonexistent-provider/nonexistent-model",
messages: [{ role: "user", content: "hi" }],
max_tokens: -1,
});
assert.equal(status, 400);
assert.match(JSON.stringify(payload.error), /max_tokens/i);
});
test("bad n (0) → 400", async () => {
const { status, payload } = await postChat({
model: "nonexistent-provider/nonexistent-model",
messages: [{ role: "user", content: "hi" }],
n: 0,
});
assert.equal(status, 400);
assert.match(JSON.stringify(payload.error), /n:/);
});
test("valid params (temperature=0.7) on unknown provider still 404 (provider lookup runs after schema ok)", async () => {
const { status, payload } = await postChat({
model: "nonexistent-provider/nonexistent-model",
messages: [{ role: "user", content: "hi" }],
temperature: 0.7,
max_tokens: 100,
});
assert.ok(
status === 404 || status === 401,
`schema-ok unknown provider should 404 or 401, got ${status}`
);
assert.match(
JSON.stringify(payload.error),
/model_not_found|No active credentials|unauthorized|authentication/i
);
});
test("params omitted entirely → schema passes, no false 400", async () => {
const { status } = await postChat({
model: "nonexistent-provider/nonexistent-model",
messages: [{ role: "user", content: "hi" }],
});
// Auth may run before catalog lookup (401) or catalog may 404 the unknown model.
assert.ok(
status === 404 || status === 401,
`expected routing 404/401 after schema pass-through, got ${status}`
);
});