aboutsummaryrefslogtreecommitdiff
path: root/castle-search/test/http.test.ts
diff options
context:
space:
mode:
authorIgor Soarez <igor@soarez.org>2026-11-18 12:11:11 +0000
committerIgor Soarez <igor@soarez.org>2026-11-18 12:11:11 +0000
commitf8835d53b96bb6e4152bc5bf84e61b8b54396008 (patch)
treed3815441efcb5a87498dfff8054c0786c84afffd /castle-search/test/http.test.ts
parent910ac7cd424f6401ea042a84603c42ba055994f8 (diff)
castle-search: X.com: read posts whole with thread and replies, profiles, trends
/x-post, /x-user and /x-trends beside /x-search, sharing its limits and its sign-in. Posts now carry photos (image link, alt text), videos, link cards and the quoted post's time. A post's own page lists the quoted post's time first; the probe now takes the post's own linked time.
Diffstat (limited to 'castle-search/test/http.test.ts')
-rw-r--r--castle-search/test/http.test.ts70
1 files changed, 70 insertions, 0 deletions
diff --git a/castle-search/test/http.test.ts b/castle-search/test/http.test.ts
index b2c90ab..c076863 100644
--- a/castle-search/test/http.test.ts
+++ b/castle-search/test/http.test.ts
@@ -116,3 +116,73 @@ test("X's action-bar label parses, singular and plural", () => {
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");
+});