// A TRENDING PAGE'S ANSWERS IN THE SHAPE THE REAL PAGE GIVES THEM (lane // x-followups), with INVENTED data: every id, handle, name, word, cursor and // timeline id here is made up. The shapes are what the first real read of a // trending page saved (18 Nov 2026), reduced to the keys the parser reads and // the ones that could mislead it: // // AiTrendByRestId the page itself: data.ai_trend_by_rest_id.result // .page — article {title, article_text.text}, // last_updated_at_ms, disclaimer, post_timelines // [{label, post_timeline.id}] (two tabs) — and // result.contexts.topics. // GenericTimelineById a tab's posts. Its request's `variables` name the // timeline (`timelineId`), `count`, and from the // second request on the `cursor` it continues from. // FIRST answer: TimelineClearCache, TimelineTerminate // Timeline (direction Top), TimelineAddEntries with // tweet entries, one MODULE of accounts, a cursor-top // and a cursor-bottom entry. LATER answers: // TimelineAddEntries with tweet entries and NO cursor // entry, then two TimelineReplaceEntry instructions // carrying the new top and bottom cursors. // TrendRelevantUsers accounts, under the page's own `data` key, in a // timeline with instructions and no post. // useStoryTopicQuery other trends of the same story (names, counts). // small ones DataSaverMode, ViewerBadgeCounts: no post. // // THE END OF A TIMELINE IS NOT HERE. The read these shapes come from stopped // at its post cap after three scrolls; no saved capture shows what the page // fetches when a tab has no more posts. Its shape is NOT guessed: there is no // builder for it, and `realContinuation` with no post (or only posts already // shown) is the parser's own rule for an end acted out in the LATER-answer // shape, not something X was seen to send. Until a read reaches a real end // and saves it, `pageEnd` is true only for a capture that looks like that. import { fixtureTweetResult, type FixtureResponse, type FixtureTweet } from "./xTrendingCapture"; export const INVENTED_TREND_ID = "1900000000000000002"; export const INVENTED_TREND_URL = `https://x.com/i/trending/${INVENTED_TREND_ID}`; // The two operation names are X's own (they are in every such page's // requests); everything named INVENTED_ below is made up. export const REAL_PAGE_OP = "AiTrendByRestId"; export const REAL_TIMELINE_OP = "GenericTimelineById"; export const INVENTED_TITLE = "Kettle festival opens with a record queue"; export const INVENTED_SUMMARY = "The annual kettle festival opened this morning. Visitors queued from dawn, and the organisers added a second hall for the afternoon demonstrations."; export const INVENTED_LAST_UPDATED_MS = Date.UTC(2026, 10, 16, 11, 30, 0); export const INVENTED_TOP_TIMELINE = "VGltZWxpbmU6Zml4dHVyZS10b3A="; export const INVENTED_LATEST_TIMELINE = "VGltZWxpbmU6Zml4dHVyZS1sYXRlc3Q="; const url = (op: string, variables: Record) => `https://x.com/i/api/graphql/fixtureQueryId/${op}?variables=${encodeURIComponent(JSON.stringify(variables))}&features=${encodeURIComponent("{}")}`; export type RealPage = { trendId?: string; title?: string; summary?: string; lastUpdatedAtMs?: number; tabs?: Array<{ label: string; timelineId: string }>; }; // The page's own answer. export function realPageBody(o: RealPage = {}): Record { const trendId = o.trendId ?? INVENTED_TREND_ID; const tabs = o.tabs ?? [ { label: "Top", timelineId: INVENTED_TOP_TIMELINE }, { label: "Latest", timelineId: INVENTED_LATEST_TIMELINE }, ]; return { data: { ai_trend_by_rest_id: { rest_id: trendId, result: { contexts: { topics: ["Food and drink"] }, page: { article: { article_text: { entities: [], text: o.summary ?? INVENTED_SUMMARY }, title: o.title ?? INVENTED_TITLE, }, available_actions: [], disclaimer: "This story is a summary of posts and may evolve over time.", last_updated_at_ms: o.lastUpdatedAtMs ?? INVENTED_LAST_UPDATED_MS, post_timelines: tabs.map((t) => ({ label: t.label, post_timeline: { id: t.timelineId } })), }, rest_id: trendId, }, }, }, }; } export function realPageAnswer(o: RealPage = {}): FixtureResponse { return { url: url(REAL_PAGE_OP, { trendId: o.trendId ?? INVENTED_TREND_ID, includePromotedContent: false, withBirdwatchNotes: true, withVoice: true, withCommunity: true }), status: 200, body: realPageBody(o), }; } function tweetEntry(t: FixtureTweet, sort: number): Record { return { content: { __typename: "TimelineTimelineItem", clientEventInfo: { component: "trends_tweets", element: "tweet" }, entryType: "TimelineTimelineItem", itemContent: { __typename: "TimelineTweet", itemType: "TimelineTweet", tweetDisplayType: "Tweet", tweet_results: { result: fixtureTweetResult(t) }, }, }, entryId: `tweet-${t.id}`, sortIndex: String(sort), }; } function cursorEntry(kind: "Top" | "Bottom", value: string, n: number): Record { return { content: { __typename: "TimelineTimelineCursor", cursorType: kind, entryType: "TimelineTimelineCursor", value }, entryId: `cursor-${kind.toLowerCase()}-${n}`, sortIndex: String(n), }; } // The accounts module the first answer carries among its entries: users, no post. function accountsModule(): Record { const user = (handle: string, i: number) => ({ entryId: `trends-accounts-7-user-${i}`, item: { clientEventInfo: { component: "trends_accounts", element: "user" }, itemContent: { __typename: "TimelineUser", itemType: "TimelineUser", userDisplayType: "User", user_results: { result: { __typename: "User", rest_id: `80000${i}`, core: { screen_name: handle, name: handle }, legacy: { description: "An invented account." } } }, }, }, }); return { content: { __typename: "TimelineTimelineModule", clientEventInfo: { component: "trends_accounts" }, displayType: "Vertical", entryType: "TimelineTimelineModule", header: { displayType: "Classic", sticky: false, text: "Relevant people" }, items: [user("kettle_org", 1), user("teapot_news", 2), user("spout_clips", 3)], }, entryId: "trends-accounts-7", sortIndex: "7", }; } const timelineBody = (instructions: unknown[]) => ({ data: { timeline: { timeline: { instructions, metadata: { scribeConfig: { page: "trends" } } } } }, }); export const realBottomCursor = (n: number) => `fixture-bottom-cursor-${n}`; export const realTopCursor = (n: number) => `fixture-top-cursor-${n}`; // A tab's FIRST answer: no cursor in the request; the cursors are entries. export function realFirstPage(tweets: FixtureTweet[], o: { timelineId?: string; bottom?: string } = {}): FixtureResponse { return { url: url(REAL_TIMELINE_OP, { timelineId: o.timelineId ?? INVENTED_TOP_TIMELINE, count: 20, withQuickPromoteEligibilityTweetFields: true }), status: 200, body: timelineBody([ { type: "TimelineClearCache" }, { direction: "Top", type: "TimelineTerminateTimeline" }, { type: "TimelineAddEntries", entries: [ ...tweets.slice(0, 2).map((t, i) => tweetEntry(t, 100 - i)), accountsModule(), ...tweets.slice(2).map((t, i) => tweetEntry(t, 90 - i)), cursorEntry("Top", realTopCursor(1), 999), cursorEntry("Bottom", o.bottom ?? realBottomCursor(1), 1), ], }, ]), }; } // A LATER answer of a tab: the request carries the cursor it continues from; // the answer lists no cursor entry and REPLACES the two it had. export function realContinuation( tweets: FixtureTweet[], o: { timelineId?: string; asked?: string; bottom?: string; n?: number } = {}, ): FixtureResponse { const n = o.n ?? 2; return { url: url(REAL_TIMELINE_OP, { timelineId: o.timelineId ?? INVENTED_TOP_TIMELINE, count: 20, cursor: o.asked ?? realBottomCursor(n - 1), withQuickPromoteEligibilityTweetFields: true, }), status: 200, body: timelineBody([ { type: "TimelineAddEntries", entries: tweets.map((t, i) => tweetEntry(t, 80 - i)) }, { entry: cursorEntry("Top", realTopCursor(n), 999), entry_id_to_replace: "cursor-top-999", type: "TimelineReplaceEntry" }, { entry: cursorEntry("Bottom", o.bottom ?? realBottomCursor(n), 1), entry_id_to_replace: "cursor-bottom-1", type: "TimelineReplaceEntry" }, ]), }; } // The accounts the page names, under the page's own `data` key: a timeline // with instructions and no post. export function realRelevantUsers(trendId: string = INVENTED_TREND_ID): FixtureResponse { return { url: url("TrendRelevantUsers", { trendId }), status: 200, body: { data: { ai_trend_by_rest_id: { rest_id: trendId, result: { trend_relevant_users: { timeline: { instructions: [{ type: "TimelineAddEntries", entries: [accountsModule()] }] } } }, }, }, }, }; } // Other trends of the same story. export function realStoryTopics(): FixtureResponse { const trend = (id: string, name: string) => ({ trend_results: { id: `trend-${id}`, result: { __typename: "Trend", core: { category: "Food and drink", created_at_ms: INVENTED_LAST_UPDATED_MS, hook: "An invented hook.", name }, id: `trend-${id}`, post_count: "1.2K", rest_id: id, social_proof: [], }, }, }); return { url: url("useStoryTopicQuery", { rest_id: "story-fixture", limit: 3 }), status: 200, body: { data: { story_topic: { id: "story-fixture", stories: { items: [trend("1900000000000000003", "Teapot prices"), trend("1900000000000000004", "Saucer shortage")] } } } }, }; } const small = (op: string, body: unknown): FixtureResponse => ({ url: url(op, {}), status: 200, body }); export const INVENTED_FIRST: FixtureTweet[] = [ { id: "1900000000000003001", handle: "teapot_news", name: "Teapot News", text: "The kettle festival opens today.", media: ["photo"] }, { id: "1900000000000003002", handle: "saucer_fan", text: "Quoting the festival post.", quoted: { id: "1900000000000002900", handle: "kettle_org", text: "Festival programme attached.", media: ["photo"] }, }, { id: "1900000000000003003", handle: "spout_clips", name: "Spout Clips", text: "Video from the opening.", media: ["video"], namesInCore: true }, ]; export const INVENTED_SECOND: FixtureTweet[] = [ { id: "1900000000000003004", handle: "lid_watch", text: "The queue at noon.", media: ["animated_gif"] }, { id: "1900000000000003005", handle: "kettle_org", text: "A second hall is open." }, ]; export const INVENTED_THIRD: FixtureTweet[] = [ { id: "1900000000000003006", handle: "teapot_news", name: "Teapot News", text: "Afternoon demonstrations begin." }, ]; // What the page fetched, in the order the real read saved it: small answers, // the page, its accounts, its story, the first page of the default tab, then // one continuation per scroll (`later`: the posts each brought). export function realCaptureResponses(o: { page?: RealPage; first?: FixtureTweet[]; later?: FixtureTweet[][] } = {}): FixtureResponse[] { const later = o.later ?? [INVENTED_SECOND, INVENTED_THIRD]; return [ small("DataSaverMode", { data: { viewer: { data_saver_mode: false } } }), realPageAnswer(o.page), realRelevantUsers(o.page?.trendId), realStoryTopics(), realFirstPage(o.first ?? INVENTED_FIRST), small("ViewerBadgeCounts", { data: { viewer_v2: { badge_counts: { ntab_unread_count: 0 } } } }), ...later.map((tweets, i) => realContinuation(tweets, { n: i + 2 })), ]; }