diff options
Diffstat (limited to 'castle-search/test')
| -rw-r--r-- | castle-search/test/google-links.test.ts | 41 | ||||
| -rw-r--r-- | castle-search/test/http.test.ts | 67 | ||||
| -rw-r--r-- | castle-search/test/resolve.test.ts | 50 |
3 files changed, 108 insertions, 50 deletions
diff --git a/castle-search/test/google-links.test.ts b/castle-search/test/google-links.test.ts new file mode 100644 index 0000000..f31f112 --- /dev/null +++ b/castle-search/test/google-links.test.ts @@ -0,0 +1,41 @@ +import assert from "node:assert/strict"; +import { test } from "node:test"; + +import { formatResults } from "../src/format.ts"; +import { isGoogleGoto } from "../src/google-links.ts"; +import { getEngine } from "../src/engines.ts"; +import { resolveLink } from "../src/search.ts"; + +test("only well-formed google /goto links count", () => { + assert.equal(isGoogleGoto("https://www.google.com/goto?url=CAESabc_-9"), true); + assert.equal(isGoogleGoto("https://www.google.co.uk/goto?url=CAESabc"), true); + assert.equal(isGoogleGoto("https://www.google.com/search?q=x"), false); + assert.equal(isGoogleGoto("https://example.com/goto?url=CAES"), false); + // Not a general navigator: nothing may ride along after the token. + assert.equal(isGoogleGoto("https://www.google.com/goto?url=CAES&x=https://evil.example"), false); + assert.equal(isGoogleGoto("https://www.google.com.evil.example/goto?url=CAES"), false); +}); + +test("the google self-host filter lets /goto through but still blocks google's own pages", () => { + const re = new RegExp(getEngine("google").extraction.selfHostPattern); + assert.equal(re.test("https://www.google.com/goto?url=CAES"), false); + assert.equal(re.test("https://www.google.com/search?q=x"), true); + assert.equal(re.test("https://accounts.google.com/ServiceLogin"), false); +}); + +test("resolveLink refuses anything that is not a google /goto link, before touching the browser", async () => { + await assert.rejects(() => resolveLink("https://example.com/", AbortSignal.timeout(1000)), /only follows Google result links/); + await assert.rejects(() => resolveLink("file:///etc/passwd", AbortSignal.timeout(1000)), /only follows Google result links/); +}); + +test("a wrapped result says so, shows Google's breadcrumb, and names the tool to resolve it", () => { + const text = formatResults({ + query: "q", searchUrl: "u", finalUrl: "u", endpoint: "e", engine: "Google", engineSource: "s", + results: [ + { title: "Wrapped", url: "https://www.google.com/goto?url=CAES", snippet: "snip", display: "GitHub — https://github.com › a › b" }, + { title: "Direct", url: "https://example.com/", snippet: "" }, + ], + }); + assert.match(text, /\[Google-wrapped link, real URL not yet known\. Google shows it as: GitHub — https:\/\/github\.com › a › b\. Pass it to castle_cdp_resolve/); + assert.equal((text.match(/Google-wrapped/g) ?? []).length, 1); +}); 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")); +}); diff --git a/castle-search/test/resolve.test.ts b/castle-search/test/resolve.test.ts deleted file mode 100644 index bcd4ef4..0000000 --- a/castle-search/test/resolve.test.ts +++ /dev/null @@ -1,50 +0,0 @@ -import assert from "node:assert/strict"; -import { test } from "node:test"; - -import { isGoogleGoto, resolveGoogleLinks } from "../src/resolve.ts"; -import { getEngine } from "../src/engines.ts"; - -test("google /goto links are recognised, other google links are not", () => { - assert.equal(isGoogleGoto("https://www.google.com/goto?url=CAES"), true); - assert.equal(isGoogleGoto("https://www.google.co.uk/goto?url=CAES"), true); - assert.equal(isGoogleGoto("https://www.google.com/search?q=x"), false); - assert.equal(isGoogleGoto("https://example.com/goto?url=x"), false); -}); - -test("the google self-host filter lets /goto through but still blocks google's own pages", () => { - const re = new RegExp(getEngine("google").extraction.selfHostPattern); - assert.equal(re.test("https://www.google.com/goto?url=CAES"), false); - assert.equal(re.test("https://www.google.com/search?q=x"), true); - assert.equal(re.test("https://accounts.google.com/ServiceLogin"), false); -}); - -test("non-google results pass through untouched", async () => { - const input = [{ title: "t", url: "https://example.com/", snippet: "" }]; - assert.deepEqual(await resolveGoogleLinks(input), input); -}); - -test("a link that cannot be resolved keeps the page's breadcrumb in its snippet", async () => { - const wrapped = "https://www.google.com/goto?url=CAES"; - const real = globalThis.fetch; - // Google's interstitial: 200 with no Location header. - globalThis.fetch = (async () => new Response("<html>Verifying your request</html>", { status: 200 })) as typeof fetch; - try { - const [r] = await resolveGoogleLinks([{ title: "t", url: wrapped, snippet: "s", display: "GitHub — https://github.com › a › b" }]); - assert.equal(r?.url, wrapped); - assert.match(r?.snippet ?? "", /^\[link unresolved; Google shows it as: GitHub — https:\/\/github\.com › a › b\] s$/); - } finally { - globalThis.fetch = real; - } -}); - -test("a link that resolves is replaced and the snippet is left alone", async () => { - const real = globalThis.fetch; - globalThis.fetch = (async () => new Response(null, { status: 302, headers: { location: "https://github.com/a/b" } })) as typeof fetch; - try { - const [r] = await resolveGoogleLinks([{ title: "t", url: "https://www.google.com/goto?url=CAES", snippet: "s", display: "x" }]); - assert.equal(r?.url, "https://github.com/a/b"); - assert.equal(r?.snippet, "s"); - } finally { - globalThis.fetch = real; - } -}); |
