aboutsummaryrefslogtreecommitdiff
path: root/castle-search/test/http.test.ts
diff options
context:
space:
mode:
Diffstat (limited to 'castle-search/test/http.test.ts')
-rw-r--r--castle-search/test/http.test.ts67
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"));
+});