diff options
Diffstat (limited to 'castle-search/test/http.test.ts')
| -rw-r--r-- | castle-search/test/http.test.ts | 70 |
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"); +}); |
