diff options
| author | Igor Soarez <igor@soarez.org> | 2026-11-17 08:46:39 +0000 |
|---|---|---|
| committer | Igor Soarez <igor@soarez.org> | 2026-11-17 08:46:39 +0000 |
| commit | 4c1f9b4ea943d03e9610f13344c372e630db5b9d (patch) | |
| tree | 95bf9424161c98af6d384ecbc73379379373e546 /castle-search/test | |
| parent | 561f0ee076c684a79fd5671af1bad0894f33e4de (diff) | |
src/http.ts speaks Open WebUI's external web search protocol. A failed search
answers 502 with its reason, never an empty list, so a captcha cannot pass for
nothing found.
Diffstat (limited to 'castle-search/test')
| -rw-r--r-- | castle-search/test/http.test.ts | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/castle-search/test/http.test.ts b/castle-search/test/http.test.ts new file mode 100644 index 0000000..6b3045b --- /dev/null +++ b/castle-search/test/http.test.ts @@ -0,0 +1,67 @@ +import assert from "node:assert/strict"; +import { test } from "node:test"; + +import { NoResultsError, SearchChallengeError } from "../src/errors.ts"; +import { handleSearch, parseListen, type Searcher } from "../src/http.ts"; + +const signal = new AbortController().signal; + +const returning = (results: { title: string; url: string; snippet: string }[]): Searcher => + async () => ({ results, searchUrl: "", finalUrl: "", endpoint: "", engine: {} as never, engineSource: "" }); + +const throwing = (e: Error): Searcher => async () => { + throw e; +}; + +test("results come back in Open WebUI's shape", async () => { + const reply = await handleSearch( + JSON.stringify({ query: "x", count: 2 }), + returning([{ title: "T", url: "https://e.com/", snippet: "S" }]), + signal, + ); + assert.equal(reply.status, 200); + assert.deepEqual(reply.body, [{ link: "https://e.com/", title: "T", snippet: "S" }]); +}); + +test("count is clamped and defaulted", async () => { + const seen: number[] = []; + const run: Searcher = async (request) => { + seen.push(request.numResults); + return returning([])(request, signal); + }; + await handleSearch(JSON.stringify({ query: "x", count: 500 }), run, signal); + await handleSearch(JSON.stringify({ query: "x", count: 0 }), run, signal); + await handleSearch(JSON.stringify({ query: "x" }), run, signal); + assert.deepEqual(seen, [20, 1, 5]); +}); + +test("a challenge is an error with its reason, never an empty list", async () => { + const reply = await handleSearch( + JSON.stringify({ query: "x" }), + throwing(new SearchChallengeError({ + challenge: "captcha", pageUrl: "https://duckduckgo.com/", endpoint: "e", engine: "DuckDuckGo", alternatives: ["bing"], + })), + signal, + ); + assert.equal(reply.status, 502); + assert.match((reply.body as { error: string }).error, /DuckDuckGo is blocked/); + assert.match((reply.body as { error: string }).error, /cannot switch engines/); +}); + +test("no results is an error too", async () => { + const reply = await handleSearch(JSON.stringify({ query: "x" }), throwing(new NoResultsError("nothing")), signal); + assert.equal(reply.status, 502); +}); + +test("bad requests are refused", async () => { + const run = returning([]); + assert.equal((await handleSearch("not json", run, signal)).status, 400); + assert.equal((await handleSearch(JSON.stringify({ query: " " }), run, signal)).status, 400); + assert.equal((await handleSearch(JSON.stringify({}), run, signal)).status, 400); +}); + +test("listen addresses parse", () => { + assert.deepEqual(parseListen("127.0.0.1:11158"), { host: "127.0.0.1", port: 11158 }); + assert.throws(() => parseListen("11158")); + assert.throws(() => parseListen("h:99999")); +}); |
