aboutsummaryrefslogtreecommitdiff
path: root/castle-search/test
diff options
context:
space:
mode:
Diffstat (limited to 'castle-search/test')
-rw-r--r--castle-search/test/http.test.ts51
1 files changed, 51 insertions, 0 deletions
diff --git a/castle-search/test/http.test.ts b/castle-search/test/http.test.ts
index 6b3045b..b2c90ab 100644
--- a/castle-search/test/http.test.ts
+++ b/castle-search/test/http.test.ts
@@ -65,3 +65,54 @@ test("listen addresses parse", () => {
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");
+});