Files
OmniRoute/tests/unit/executor-anysearch-fetch.test.ts
Xxx91n 99044ed044 feat(search): add fallback-only AnySearch provider (webSearch + webFetch) (#11690)
Merged via /merge-batch (2026-08-26, v3.8.51). Encontrei e corrigi algumas lacunas ao validar: (1) o tipo do provider em handleWebFetch (MCP) não incluía anysearch-search, (2) a lista FETCH_PROVIDERS do dashboard (/api/search/providers) não tinha a entrada anysearch-search apesar dela ser webSearch+webFetch, (3) contagens desatualizadas em search-registry/search-route/context7-provider/search-providers-catalog (19→20 search, +1 fetch). Enviei tudo para seu branch antes do merge. Validado: 113/113 testes relacionados passando, typecheck limpo. Obrigado pela contribuição — provider bem completo.
2026-08-26 17:54:57 -03:00

87 lines
2.8 KiB
TypeScript

import test from "node:test";
import assert from "node:assert/strict";
const { anysearchFetch } = await import("../../open-sse/executors/anysearch-fetch.ts");
test("anysearchFetch posts a strict { url } body with optional Bearer auth", async () => {
const originalFetch = globalThis.fetch;
let capturedUrl = "";
let capturedInit: RequestInit | undefined;
globalThis.fetch = (async (url: unknown, init?: RequestInit) => {
capturedUrl = String(url);
capturedInit = init;
return new Response(
JSON.stringify({
code: 0,
message: "success",
data: { url: "https://example.com", title: "Example", content: "page text" },
}),
{ status: 200, headers: { "content-type": "application/json" } }
);
}) as typeof fetch;
try {
const result = await anysearchFetch({
url: "https://example.com",
format: "markdown",
includeMetadata: true,
credentials: { apiKey: "as_sk_test" },
});
assert.equal(capturedUrl, "https://api.anysearch.com/v1/extract");
assert.deepEqual(JSON.parse(String(capturedInit?.body)), { url: "https://example.com" });
const headers = (capturedInit?.headers ?? {}) as Record<string, string>;
assert.equal(headers.Authorization, "Bearer as_sk_test");
assert.equal(result.success, true);
if (result.success) {
assert.equal(result.data.provider, "anysearch-search");
assert.equal(result.data.content, "page text");
assert.equal(result.data.metadata?.title, "Example");
}
} finally {
globalThis.fetch = originalFetch;
}
});
test("anysearchFetch works keyless (anonymous tier) and maps envelope failures", async () => {
const originalFetch = globalThis.fetch;
globalThis.fetch = (async () =>
new Response(
JSON.stringify({
code: 0,
message: "success",
data: { url: "https://example.com", content: "anon" },
}),
{ status: 200, headers: { "content-type": "application/json" } }
)) as typeof fetch;
try {
const result = await anysearchFetch({
url: "https://example.com",
format: "markdown",
includeMetadata: false,
credentials: {},
});
assert.equal(result.success, true);
if (result.success) assert.equal(result.data.content, "anon");
} finally {
globalThis.fetch = originalFetch;
}
globalThis.fetch = (async () =>
new Response(
JSON.stringify({ code: -1, error_code: "extract_failed", message: "extract failed" }),
{ status: 200, headers: { "content-type": "application/json" } }
)) as typeof fetch;
try {
const result = await anysearchFetch({
url: "https://example.com/bad",
format: "markdown",
includeMetadata: false,
credentials: { apiKey: "as_sk_test" },
});
assert.equal(result.success, false);
} finally {
globalThis.fetch = originalFetch;
}
});