Skip to content

armgit / ODS

Public Imported from GitHub
0

ODS V3 Pre-Release: Public testing and refinement ahead of the official V3 launch. Turn your PC, Mac, or Linux box into a private AI server.

19.8 KB
Raw Blame
// Contract tests for the brave-search proxy, focused on the opt-in searxng
// compatibility mode (extensions/services/brave-search/proxy.mjs).
//
// Runs entirely on loopback: an in-process stub stands in for the Brave API,
// and two real proxy child processes are exercised over HTTP — one with
// BRAVE_SEARCH_SEARXNG_COMPAT enabled, one with it left at the default.
//
// Run: bash tests/test-brave-search-searxng-compat.sh

import http from "node:http";
import { spawn } from "node:child_process";
import { once } from "node:events";
import path from "node:path";
import { fileURLToPath } from "node:url";

const ROOT = path.dirname(path.dirname(fileURLToPath(import.meta.url)));
const PROXY_SCRIPT = path.join(ROOT, "extensions", "services", "brave-search", "proxy.mjs");
const STUB_API_KEY = "test-subscription-token";
const PROXY_TIMEOUT_MS = 1000;
const SLOW_UPSTREAM_DELAY_MS = 3000;

let failures = 0;
let redirectTarget = "";
let redirectedRequests = 0;
const observedTokens = [];
let pendingDisconnect;

function check(name, cond, detail) {
  if (cond) {
    console.log(`  ok: ${name}`);
  } else {
    failures += 1;
    console.error(`  FAIL: ${name}${detail === undefined ? "" : ` — ${detail}`}`);
  }
}

// ── stub Brave upstream ─────────────────────────────────────────────────────

function braveBody() {
  return JSON.stringify({
    web: {
      results: [
        {
          title: " First Result ",
          url: "https://example.com/one?a=1#frag",
          description: " First snippet ",
          page_age: "2026-01-15T09:00:00",
          thumbnail: { src: "https://imgs.example.net/thumb1.png" },
        },
        { title: "Second Result", url: "https://example.org/two", description: "Second snippet" },
        { title: "No URL", url: "", description: "must be dropped" },
      ],
    },
  });
}

function stubHandler(req, res) {
  const url = new URL(req.url, "http://stub");
  observedTokens.push(req.headers["x-subscription-token"]);
  if (req.headers["x-subscription-token"] !== STUB_API_KEY) {
    res.writeHead(401, { "content-type": "application/json" });
    res.end(JSON.stringify({ error: "missing subscription token" }));
    return;
  }
  const q = url.searchParams.get("q");
  const respond = (status, body) => {
    res.writeHead(status, { "content-type": "application/json" });
    res.end(body);
  };
  if (q === "disconnect-headers" || q === "disconnect-body") {
    const pending = pendingDisconnect;
    if (q === "disconnect-body") {
      res.writeHead(200, { "content-type": "application/json" });
      res.write("{");
    }
    res.on("close", pending.closed);
    pending.started(res);
  } else if (q === "err500") {
    respond(500, "{}");
  } else if (q === "err429") {
    respond(429, "{}");
  } else if (q === "badjson") {
    respond(200, "this is not json");
  } else if (q === "nulljson") {
    respond(200, "null");
  } else if (q === "badshape") {
    respond(200, JSON.stringify({ web: { results: "not-an-array" } }));
  } else if (q === "slow") {
    setTimeout(() => respond(200, braveBody()), SLOW_UPSTREAM_DELAY_MS);
  } else if (q === "slow-body") {
    res.writeHead(200, { "content-type": "application/json" });
    res.flushHeaders();
    setTimeout(() => res.end(braveBody()), SLOW_UPSTREAM_DELAY_MS);
  } else if (q === "redirect") {
    res.writeHead(302, { location: redirectTarget });
    res.end();
  } else if (q === "echo") {
    const offset = url.searchParams.get("offset") ?? "none";
    const count = url.searchParams.get("count");
    respond(
      200,
      JSON.stringify({
        web: {
          results: [
            {
              title: `offset=${offset} count=${count}`,
              url: "https://example.com/echo",
              description: "echo",
            },
          ],
        },
      }),
    );
  } else {
    respond(200, braveBody());
  }
}

// ── proxy process management ────────────────────────────────────────────────

async function freePort() {
  const srv = http.createServer();
  srv.listen(0, "127.0.0.1");
  await once(srv, "listening");
  const port = srv.address().port;
  srv.close();
  await once(srv, "close");
  return port;
}

async function startProxy(port, stubPort, extraEnv) {
  const child = spawn(process.execPath, [PROXY_SCRIPT], {
    env: {
      ...process.env,
      BRAVE_SEARCH_API_KEY: STUB_API_KEY,
      BRAVE_SEARCH_PORT_INTERNAL: String(port),
      BRAVE_SEARCH_UPSTREAM_URL: `http://127.0.0.1:${stubPort}/res/v1/web/search`,
      BRAVE_SEARCH_TIMEOUT_MS: String(PROXY_TIMEOUT_MS),
      ...extraEnv,
    },
    stdio: ["ignore", "inherit", "inherit"],
  });
  for (let attempt = 0; attempt < 50; attempt += 1) {
    try {
      const res = await fetch(`http://127.0.0.1:${port}/health`);
      if (res.status === 200) {
        return child;
      }
    } catch {
      // not listening yet
    }
    await new Promise((resolve) => setTimeout(resolve, 100));
  }
  child.kill();
  throw new Error(`proxy on :${port} did not become healthy`);
}

async function getJson(base, pathAndQuery) {
  const res = await fetch(`${base}${pathAndQuery}`);
  return { status: res.status, body: await res.json() };
}

async function expectStartupFailure(name, envOverrides, expectedStderrText) {
  const child = spawn(process.execPath, [PROXY_SCRIPT], {
    env: {
      ...process.env,
      BRAVE_SEARCH_API_KEY: STUB_API_KEY,
      BRAVE_SEARCH_PORT_INTERNAL: "0",
      ...envOverrides,
    },
    stdio: ["ignore", "ignore", "pipe"],
  });
  let stderr = "";
  child.stderr.on("data", (chunk) => {
    stderr += chunk;
  });
  const [code] = await once(child, "exit");
  check(`${name} exits 2`, code === 2, `exit=${code}`);
  check(`${name} stderr names the variable`, stderr.includes(expectedStderrText), stderr.trim());
}

// ── test suites ─────────────────────────────────────────────────────────────

async function testV1Route(base) {
  console.log("v1 route (must stay stable):");

  const ok = await getJson(base, "/v1/search?q=ok&count=5");
  const slowBody = await getJson(base, "/v1/search?q=slow-body");
  check("504 when headers arrive but the body stalls", slowBody.status === 504 && slowBody.body.error === "upstream_timeout");
  check("200 on success", ok.status === 200, `got ${ok.status}`);
  check("query echoed", ok.body.query === "ok");
  check("empty-url result dropped", ok.body.results.length === 2, `got ${ok.body.results.length}`);
  check(
    "result shape is {title, url, snippet}",
    ok.body.results.every(
      (r) =>
        typeof r.title === "string" &&
        typeof r.url === "string" &&
        typeof r.snippet === "string" &&
        Object.keys(r).length === 3,
    ),
  );
  check("fields trimmed", ok.body.results[0].title === "First Result");
  check("subscription token is absent from the response", !JSON.stringify(ok.body).includes(STUB_API_KEY));

  const noQ = await getJson(base, "/v1/search");
  check(
    "400 without q",
    noQ.status === 400 && noQ.body.error === "missing_query_param_q",
    JSON.stringify(noQ),
  );

  const err = await getJson(base, "/v1/search?q=err500");
  check(
    "502 on upstream HTTP error",
    err.status === 502 && err.body.error === "upstream_error" && err.body.status === 500,
    JSON.stringify(err),
  );

  const slow = await getJson(base, "/v1/search?q=slow");
  check(
    "504 on upstream timeout",
    slow.status === 504 && slow.body.error === "upstream_timeout",
    JSON.stringify(slow),
  );

  const redirect = await getJson(base, "/v1/search?q=redirect");
  check(
    "502 on upstream redirect (not followed)",
    redirect.status === 502 && redirect.body.error === "upstream_unavailable",
    JSON.stringify(redirect),
  );

  for (const query of ["nulljson", "badshape"]) {
    const oddShape = await getJson(base, `/v1/search?q=${query}`);
    check(
      `valid JSON with ${query} shape degrades to empty results`,
      oddShape.status === 200 && Array.isArray(oddShape.body.results) && oddShape.body.results.length === 0,
      JSON.stringify(oddShape),
    );
  }
}

async function testCompatDisabled(base) {
  console.log("searxng compat disabled (default):");
  const res = await getJson(base, "/search?format=json&q=ok");
  check("/search stays 404", res.status === 404 && res.body.error === "not_found", JSON.stringify(res));
}

function checkEnvelope(body) {
  check("envelope: number_of_results is 0", body.number_of_results === 0);
  for (const key of ["results", "answers", "corrections", "infoboxes", "suggestions", "unresponsive_engines"]) {
    check(`envelope: ${key} is an array`, Array.isArray(body[key]), typeof body[key]);
  }
}

async function testCompatEnabled(base) {
  console.log("searxng compat enabled:");

  const ok = await getJson(base, "/search?format=json&q=ok");
  check("200 on success", ok.status === 200, `got ${ok.status}`);
  check("query echoed", ok.body.query === "ok");
  check("subscription token is absent from compat response", !JSON.stringify(ok.body).includes(STUB_API_KEY));
  checkEnvelope(ok.body);
  check("empty-url result dropped", ok.body.results.length === 2, `got ${ok.body.results.length}`);

  const [first, second] = ok.body.results;
  check("result url", first.url === "https://example.com/one?a=1#frag");
  check("result title trimmed", first.title === "First Result");
  check("result content from description", first.content === "First snippet");
  check("engine is brave", first.engine === "brave");
  check("engines is [brave]", JSON.stringify(first.engines) === '["brave"]');
  check("positions are 1-based", JSON.stringify(first.positions) === "[1]" && JSON.stringify(second.positions) === "[2]");
  check("score follows 1/position", first.score === 1 && second.score === 0.5);
  check("category is general", first.category === "general");
  check("template is default.html", first.template === "default.html");
  check(
    "parsed_url is a urlparse 6-tuple",
    JSON.stringify(first.parsed_url) === '["https","example.com","/one","","a=1","frag"]',
    JSON.stringify(first.parsed_url),
  );
  check("publishedDate from page_age", first.publishedDate === "2026-01-15T09:00:00");
  check("publishedDate null when absent", second.publishedDate === null);
  check("thumbnail mapped when present", first.thumbnail === "https://imgs.example.net/thumb1.png");
  check("thumbnail omitted when absent", !("thumbnail" in second));

  // The exact contract Perplexica's searchSearxng() destructures.
  check(
    "Perplexica contract: results[].title/url/content strings",
    ok.body.results.every(
      (r) => typeof r.title === "string" && typeof r.url === "string" && typeof r.content === "string",
    ),
  );
  check("Perplexica contract: suggestions array present", Array.isArray(ok.body.suggestions));

  const perplexicaStyle = await getJson(base, "/search?format=json&q=ok&categories=general&language=en&pageno=1");
  check(
    "Perplexica-style general query serves results",
    perplexicaStyle.status === 200 && perplexicaStyle.body.results.length === 2,
    JSON.stringify(perplexicaStyle.body.unresponsive_engines),
  );

  const htmlFormat = await getJson(base, "/search?q=ok");
  check(
    "400 without format=json",
    htmlFormat.status === 400 && htmlFormat.body.error === "unsupported_format",
    JSON.stringify(htmlFormat),
  );

  const noQ = await getJson(base, "/search?format=json");
  check(
    "400 without q",
    noQ.status === 400 && noQ.body.error === "missing_query_param_q",
    JSON.stringify(noQ),
  );

  const images = await getJson(base, "/search?format=json&q=ok&categories=images");
  check(
    "unsupported category → honest empty + unresponsive_engines",
    images.status === 200 &&
      images.body.results.length === 0 &&
      JSON.stringify(images.body.unresponsive_engines) ===
        '[["brave","unsupported category: images"]]',
    JSON.stringify(images.body.unresponsive_engines),
  );

  const foreignEngines = await getJson(base, "/search?format=json&q=ok&engines=google%20images,bing%20images");
  check(
    "foreign engines → honest empty + unresponsive_engines",
    foreignEngines.status === 200 &&
      foreignEngines.body.results.length === 0 &&
      JSON.stringify(foreignEngines.body.unresponsive_engines) ===
        '[["google images","engine not available"],["bing images","engine not available"]]',
    JSON.stringify(foreignEngines.body.unresponsive_engines),
  );

  const braveEngine = await getJson(base, "/search?format=json&q=ok&engines=brave");
  check(
    "engines=brave serves results",
    braveEngine.status === 200 && braveEngine.body.results.length === 2,
    JSON.stringify(braveEngine.body.unresponsive_engines),
  );

  const page10 = await getJson(base, "/search?format=json&q=echo&pageno=10");
  check("last supported page maps to offset 9",
    page10.body.results[0]?.title === "offset=9 count=20", page10);

  for (const page of [11, 100, 100000000000000000000]) {
    const before = observedTokens.length;
    const outside = await getJson(base, "/search?format=json&q=echo&pageno=" + page);
    check("out-of-range page has an honest empty envelope",
      outside.status === 200 && outside.body.results.length === 0 &&
      outside.body.unresponsive_engines[0]?.[1] === "page outside supported range (1-10)", outside);
    check("out-of-range page does not spend an upstream request",
      observedTokens.length === before, observedTokens.length - before);
  }

  const page3 = await getJson(base, "/search?format=json&q=echo&pageno=3");
  check(
    "pageno maps to Brave offset (page 3 → offset 2)",
    page3.body.results[0]?.title === "offset=2 count=20",
    JSON.stringify(page3.body.results[0]?.title),
  );

  const page1 = await getJson(base, "/search?format=json&q=echo");
  check(
    "first page sends no offset",
    page1.body.results[0]?.title === "offset=none count=20",
    JSON.stringify(page1.body.results[0]?.title),
  );

  const cases = [
    ["err429", "too many requests"],
    ["err500", "HTTP error 500"],
    ["badjson", "invalid JSON"],
    ["slow", "timeout"],
    ["slow-body", "timeout"],
    ["redirect", "unavailable"],
  ];
  for (const [q, reason] of cases) {
    const res = await getJson(base, `/search?format=json&q=${q}`);
    check(
      `upstream ${q} → 200 with unresponsive_engines "${reason}"`,
      res.status === 200 &&
        res.body.results.length === 0 &&
        JSON.stringify(res.body.unresponsive_engines) === JSON.stringify([["brave", reason]]),
      `status=${res.status} unresponsive=${JSON.stringify(res.body.unresponsive_engines)}`,
    );
  }

  for (const query of ["nulljson", "badshape"]) {
    const oddShape = await getJson(base, `/search?format=json&q=${query}`);
    check(
      `compat ${query} shape degrades to an empty envelope`,
      oddShape.status === 200 &&
        Array.isArray(oddShape.body.results) &&
        oddShape.body.results.length === 0 &&
        Array.isArray(oddShape.body.unresponsive_engines),
      JSON.stringify(oddShape.body),
    );
  }
}

async function testClientDisconnect(base) {
  console.log("downstream disconnect cancellation:");
  for (const route of ["/v1/search?", "/search?format=json&"]) {
    for (const phase of ["headers", "body"]) {
      let entered, closed;
      const started = new Promise(resolve => { entered = resolve; });
      const upstreamClosed = new Promise(resolve => { closed = resolve; });
      pendingDisconnect = { started: entered, closed: () => closed(true) };
      const request = http.get(base + route + "q=disconnect-" + phase);
      request.on("error", () => {});
      let timer, upstream;
      try {
        upstream = await started;
        request.destroy();
        const cancelled = await Promise.race([
          upstreamClosed,
          new Promise(resolve => { timer = setTimeout(() => resolve(false), 500); }),
        ]);
        check(route + " aborts upstream during " + phase, cancelled === true);
      } finally {
        clearTimeout(timer);
        request.destroy();
        upstream?.destroy();
      }
    }
  }
  const healthy = await getJson(base, "/health");
  check("proxy remains healthy after cancelled searches", healthy.status === 200);
}

// ── main ────────────────────────────────────────────────────────────────────

const stub = http.createServer(stubHandler);
stub.listen(0, "127.0.0.1");
await once(stub, "listening");
const stubPort = stub.address().port;
const redirectSink = http.createServer((_req, res) => {
  redirectedRequests += 1;
  res.writeHead(200, { "content-type": "application/json" });
  res.end(braveBody());
});
redirectSink.listen(0, "127.0.0.1");
await once(redirectSink, "listening");
redirectTarget = `http://127.0.0.1:${redirectSink.address().port}/credential-sink`;

const compatPort = await freePort();
const plainPort = await freePort();
const emptyTimeoutPort = await freePort();

const children = [];
try {
  children.push(await startProxy(compatPort, stubPort, { BRAVE_SEARCH_SEARXNG_COMPAT: "1" }));
  children.push(await startProxy(plainPort, stubPort, { BRAVE_SEARCH_SEARXNG_COMPAT: "" }));

  const compatBase = `http://127.0.0.1:${compatPort}`;
  const plainBase = `http://127.0.0.1:${plainPort}`;

  await testV1Route(compatBase);
  await testV1Route(plainBase);
  await testCompatDisabled(plainBase);
  await testCompatEnabled(compatBase);
  await testClientDisconnect(compatBase);

  console.log("env validation:");
  await expectStartupFailure(
    "missing BRAVE_SEARCH_API_KEY",
    { BRAVE_SEARCH_API_KEY: "" },
    "BRAVE_SEARCH_API_KEY",
  );
  await expectStartupFailure(
    "invalid BRAVE_SEARCH_TIMEOUT_MS",
    { BRAVE_SEARCH_TIMEOUT_MS: "abc" },
    "BRAVE_SEARCH_TIMEOUT_MS",
  );
  await expectStartupFailure(
    "non-positive BRAVE_SEARCH_TIMEOUT_MS",
    { BRAVE_SEARCH_TIMEOUT_MS: "0" },
    "BRAVE_SEARCH_TIMEOUT_MS",
  );
  await expectStartupFailure(
    "invalid BRAVE_SEARCH_UPSTREAM_URL",
    { BRAVE_SEARCH_UPSTREAM_URL: "not a url" },
    "BRAVE_SEARCH_UPSTREAM_URL",
  );
  await expectStartupFailure(
    "non-http BRAVE_SEARCH_UPSTREAM_URL",
    { BRAVE_SEARCH_UPSTREAM_URL: "file:///tmp/brave.json" },
    "BRAVE_SEARCH_UPSTREAM_URL",
  );
  await expectStartupFailure(
    "credential-bearing BRAVE_SEARCH_UPSTREAM_URL",
    { BRAVE_SEARCH_UPSTREAM_URL: "https://user:[email protected]/search" },
    "BRAVE_SEARCH_UPSTREAM_URL",
  );
  await expectStartupFailure(
    "invalid BRAVE_SEARCH_SEARXNG_COMPAT",
    { BRAVE_SEARCH_SEARXNG_COMPAT: "enabled-ish" },
    "BRAVE_SEARCH_SEARXNG_COMPAT",
  );

  // Empty values from compose ${VAR:-} interpolation must fall back to the
  // default timeout, not collapse it to 0/NaN and abort every request.
  children.push(
    await startProxy(emptyTimeoutPort, stubPort, {
      BRAVE_SEARCH_SEARXNG_COMPAT: "1",
      BRAVE_SEARCH_TIMEOUT_MS: "",
    }),
  );
  const emptyTimeout = await getJson(
    `http://127.0.0.1:${emptyTimeoutPort}`,
    "/search?format=json&q=ok",
  );
  check(
    "empty BRAVE_SEARCH_TIMEOUT_MS falls back to default (request succeeds)",
    emptyTimeout.status === 200 && emptyTimeout.body.results.length === 2,
    JSON.stringify(emptyTimeout.body.unresponsive_engines ?? emptyTimeout.body),
  );
  check("redirect target never receives the subscription token", redirectedRequests === 0, redirectedRequests);
  check(
    "configured upstream receives only the exact opaque subscription token",
    observedTokens.length > 0 && observedTokens.every((token) => token === STUB_API_KEY),
    JSON.stringify(observedTokens),
  );
} finally {
  for (const child of children) {
    child.kill();
  }
  stub.close();
  redirectSink.close();
}

if (failures > 0) {
  console.error(`${failures} brave-search searxng compat test(s) failed`);
  process.exit(1);
}
console.log("brave-search searxng compat tests passed");