diff options
Diffstat (limited to 'castle-search/test/http.test.ts')
| -rw-r--r-- | castle-search/test/http.test.ts | 188 |
1 files changed, 188 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..c076863 --- /dev/null +++ b/castle-search/test/http.test.ts @@ -0,0 +1,188 @@ +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")); +}); + +import { buildXSearchUrl, parseMetrics } from "../src/x-search.ts"; +import { handleXSearch, OPENAPI, type XSearcher } from "../src/http.ts"; + +const xReturning = (seen: unknown[] = []): XSearcher => async (request) => { + seen.push(request); + return { query: request.query, sort: request.sort ?? "latest", searchUrl: "", posts: [] }; +}; + +test("x-search defaults to the 10 latest posts and clamps count", async () => { + const seen: unknown[] = []; + await handleXSearch(JSON.stringify({ query: " from:nasa " }), xReturning(seen), signal); + await handleXSearch(JSON.stringify({ query: "x", count: 99, sort: "top" }), xReturning(seen), signal); + assert.deepEqual(seen, [ + { query: "from:nasa", count: 10, sort: "latest" }, + { query: "x", count: 20, sort: "top" }, + ]); +}); + +test("x-search refuses a bad sort or query rather than guessing", async () => { + assert.equal((await handleXSearch(JSON.stringify({ query: "x", sort: "new" }), xReturning(), signal)).status, 400); + assert.equal((await handleXSearch(JSON.stringify({ query: "" }), xReturning(), signal)).status, 400); +}); + +test("a failed x-search is a 502 with its reason", async () => { + const reply = await handleXSearch( + JSON.stringify({ query: "x" }), + async () => { + throw new Error("signed out"); + }, + signal, + ); + assert.equal(reply.status, 502); + assert.deepEqual(reply.body, { error: "signed out" }); +}); + +test("x search URLs: latest is f=live, top has no tab parameter", () => { + assert.equal(buildXSearchUrl("from:a b", "latest"), "https://x.com/search?q=from%3Aa+b&src=typed_query&f=live"); + assert.equal(buildXSearchUrl("from:a b", "top"), "https://x.com/search?q=from%3Aa+b&src=typed_query"); +}); + +test("X's action-bar label parses, singular and plural", () => { + assert.deepEqual(parseMetrics("1 reply, 971 reposts, 8852 likes, 1247 bookmarks, 16029782 views"), { + replies: 1, reposts: 971, likes: 8852, views: 16029782, + }); + assert.deepEqual(parseMetrics(null), {}); +}); + +test("the OpenAPI description names the operation Open WebUI will offer", () => { + assert.equal(OPENAPI.paths["/x-search"].post.operationId, "search_x"); +}); + +import { handleXPost, handleXTrends, handleXUser } from "../src/http.ts"; +import { handleOf, parseProfile, parseTrend, postIdOf } from "../src/x-search.ts"; + +test("post links of every shape give the post id", () => { + assert.equal(postIdOf("https://x.com/NASA/status/2122740072386609201"), "2122740072386609201"); + assert.equal(postIdOf("https://twitter.com/nasa/status/2122740072386609201?s=20"), "2122740072386609201"); + assert.equal(postIdOf("https://x.com/i/web/status/2122740072386609201"), "2122740072386609201"); + assert.equal(postIdOf("2122740072386609201"), "2122740072386609201"); + assert.equal(postIdOf("https://x.com/NASA"), undefined); +}); + +test("handles are read from @name, name or a profile link", () => { + assert.equal(handleOf("@NASA"), "NASA"); + assert.equal(handleOf("https://x.com/nasa/"), "nasa"); + assert.equal(handleOf("not a handle"), undefined); + assert.equal(handleOf("waytoolonghandle_123"), undefined); +}); + +test("a trend cell's lines become a trend", () => { + assert.deepEqual(parseTrend("2\n·\nVideo game genres · Trending\nGTA 6\nTrending with #GTAVI, Metacritic"), { + rank: 2, context: "Video game genres · Trending", name: "GTA 6", query: "GTA 6", detail: "Trending with #GTAVI, Metacritic", + }); + assert.deepEqual(parseTrend("Trending in United Kingdom\nGlastonbury"), { context: "Trending in United Kingdom", name: "Glastonbury", query: "Glastonbury" }); + assert.equal(parseTrend("1"), undefined); +}); + +test("a profile's JSON-LD gives exact counts", () => { + const p = parseProfile( + JSON.stringify({ + dateCreated: "2007-12-19T20:20:32.000Z", + relatedLink: ["https://t.co/9NkQJKAnuU", "http://www.nasa.gov/"], + mainEntity: { + additionalName: "NASA", name: "NASA", description: "Making the seemingly impossible, possible.", + homeLocation: { name: "Pale Blue Dot" }, url: "https://x.com/NASA", + interactionStatistic: [ + { name: "Follows", userInteractionCount: 92225170 }, + { name: "Friends", userInteractionCount: 115 }, + { name: "Tweets", userInteractionCount: 74553 }, + ], + }, + }), + "nasa", + ); + assert.equal(p.handle, "@NASA"); + assert.equal(p.followers, 92225170); + assert.equal(p.following, 115); + assert.equal(p.posts, 74553); + assert.equal(p.website, "http://www.nasa.gov/"); + assert.equal(p.location, "Pale Blue Dot"); +}); + +test("the X reads check what they are given before touching the browser", async () => { + const never = async (): Promise<never> => { + throw new Error("must not run"); + }; + assert.equal((await handleXPost(JSON.stringify({ url: "https://x.com/NASA" }), never, signal)).status, 400); + assert.equal((await handleXUser(JSON.stringify({ handle: "two words" }), never, signal)).status, 400); + assert.equal((await handleXTrends(JSON.stringify({ tab: "music" }), never, signal)).status, 400); + assert.equal((await handleXPost("[1]", never, signal)).status, 400); + const seen: unknown[] = []; + await handleXPost(JSON.stringify({ url: "https://x.com/a/status/12345678", replies: 99 }), async (r) => (seen.push(r), { post: {}, thread: [], replies: [] }) as never, signal); + await handleXTrends("", async (t) => (seen.push(t), { tab: t, trends: [] }), signal); + assert.deepEqual(seen, [{ url: "https://x.com/a/status/12345678", replies: 20 }, "trending"]); +}); + +test("every X read is in the OpenAPI description", () => { + assert.deepEqual(Object.keys(OPENAPI.paths), ["/x-search", "/x-post", "/x-user", "/x-trends"]); + assert.equal(OPENAPI.info.title, "X.com"); +}); |
