aboutsummaryrefslogtreecommitdiff
path: root/castle-search/test
diff options
context:
space:
mode:
authorIgor Soarez <igor@soarez.org>2026-11-18 04:55:45 +0000
committerIgor Soarez <igor@soarez.org>2026-11-18 04:55:45 +0000
commitb03415aafeea985561f3980f8b484173530a05f8 (patch)
tree3748f62ae52a5c4d7906ec3268bb8b2863a5114a /castle-search/test
parent4c1f9b4ea943d03e9610f13344c372e630db5b9d (diff)
castle-search: search X over HTTP, through the browser's signed-in session
POST /x-search reads x.com/search in a background tab: posts with their link, author, time, text, counts and quoted post, scrolling for up to 20. GET /openapi.json describes it for Open WebUI's tool servers. Signed out, an X error page or a stuck page are errors, never an empty list.
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");
+});