| 1 | import test from 'node:test'; |
| 2 | import assert from 'node:assert/strict'; |
| 3 | |
| 4 | import { |
| 5 | collectTweetResultsFromEntry, |
| 6 | extractArticleText, |
| 7 | extractCursorFromInstructions, |
| 8 | extractMedia, |
| 9 | findTweetInInstructions, |
| 10 | mapTweetResult, |
| 11 | parseTweetsFromInstructions, |
| 12 | parseUsersFromInstructions, |
| 13 | renderContentState, |
| 14 | unwrapTweetResult, |
| 15 | } from '../skills/last30days/scripts/lib/vendor/bird-search/lib/twitter-client-utils.js'; |
| 16 | import { withSearch } from '../skills/last30days/scripts/lib/vendor/bird-search/lib/twitter-client-search.js'; |
| 17 | |
| 18 | function makeRawTweet(id, username, text, extra = {}) { |
| 19 | return { |
| 20 | __typename: 'Tweet', |
| 21 | rest_id: id, |
| 22 | core: { |
| 23 | user_results: { |
| 24 | result: { |
| 25 | rest_id: `u_${id}`, |
| 26 | legacy: { |
| 27 | screen_name: username, |
| 28 | name: username, |
| 29 | }, |
| 30 | }, |
| 31 | }, |
| 32 | }, |
| 33 | legacy: { |
| 34 | full_text: text, |
| 35 | created_at: 'Wed Aug 12 15:55:18 +0000 2026', |
| 36 | favorite_count: 10, |
| 37 | retweet_count: 2, |
| 38 | reply_count: 1, |
| 39 | conversation_id_str: id, |
| 40 | }, |
| 41 | ...extra, |
| 42 | }; |
| 43 | } |
| 44 | |
| 45 | test('HIGH-162: collectTweetResultsFromEntry and parseTweetsFromInstructions unwrap TweetWithVisibilityResults', () => { |
| 46 | const directWrapped = { |
| 47 | __typename: 'TweetWithVisibilityResults', |
| 48 | tweet: makeRawTweet('101', 'alice', 'visibility-wrapped direct tweet'), |
| 49 | }; |
| 50 | const moduleWrapped = { |
| 51 | __typename: 'TweetWithVisibilityResults', |
| 52 | tweet: makeRawTweet('102', 'bob', 'visibility-wrapped module tweet'), |
| 53 | }; |
| 54 | const nestedWrapped = { |
| 55 | __typename: 'TweetWithVisibilityResults', |
| 56 | tweet: makeRawTweet('103', 'carol', 'visibility-wrapped item tweet'), |
| 57 | }; |
| 58 | |
| 59 | const entryDirect = { |
| 60 | content: { |
| 61 | itemContent: { |
| 62 | tweet_results: { result: directWrapped }, |
| 63 | }, |
| 64 | }, |
| 65 | }; |
| 66 | const entryModule = { |
| 67 | content: { |
| 68 | items: [ |
| 69 | { item: { itemContent: { tweet_results: { result: moduleWrapped } } } }, |
| 70 | { content: { itemContent: { tweet_results: { result: nestedWrapped } } } }, |
| 71 | ], |
| 72 | }, |
| 73 | }; |
| 74 | |
| 75 | assert.deepEqual( |
| 76 | collectTweetResultsFromEntry(entryDirect).map((t) => t.rest_id), |
| 77 | ['101'], |
| 78 | ); |
| 79 | assert.deepEqual( |
| 80 | collectTweetResultsFromEntry(entryModule).map((t) => t.rest_id), |
| 81 | ['102', '103'], |
| 82 | ); |
| 83 | |
| 84 | const instructions = [ |
| 85 | { |
| 86 | type: 'TimelineAddEntries', |
| 87 | entries: [entryDirect, entryModule], |
| 88 | }, |
| 89 | ]; |
| 90 | const parsed = parseTweetsFromInstructions(instructions, { quoteDepth: 1 }); |
| 91 | assert.deepEqual( |
| 92 | parsed.map((t) => ({ id: t.id, user: t.author.username, text: t.text })), |
| 93 | [ |
| 94 | { id: '101', user: 'alice', text: 'visibility-wrapped direct tweet' }, |
| 95 | { id: '102', user: 'bob', text: 'visibility-wrapped module tweet' }, |
| 96 | { id: '103', user: 'carol', text: 'visibility-wrapped item tweet' }, |
| 97 | ], |
| 98 | ); |
| 99 | |
| 100 | const found = findTweetInInstructions(instructions, '101'); |
| 101 | assert.equal(found?.rest_id, '101'); |
| 102 | assert.equal(unwrapTweetResult(null), undefined); |
| 103 | assert.equal(unwrapTweetResult('bad'), undefined); |
| 104 | }); |
| 105 | |
| 106 | test('HIGH-162: extractCursorFromInstructions supports TimelineReplaceEntry singular instruction.entry', () => { |
| 107 | const instructions = [ |
| 108 | null, |
| 109 | { type: 'TimelineAddEntries', entries: [null, { content: { entryType: 'TimelineTimelineItem' } }] }, |
| 110 | { |
| 111 | type: 'TimelineReplaceEntry', |
| 112 | entry_id_to_replace: 'cursor-bottom-0', |
| 113 | entry: { |
| 114 | entryId: 'cursor-bottom-0', |
| 115 | content: { |
| 116 | entryType: 'TimelineTimelineCursor', |
| 117 | cursorType: 'Bottom', |
| 118 | value: 'CURSOR_PAGE_2', |
| 119 | }, |
| 120 | }, |
| 121 | }, |
| 122 | ]; |
| 123 | assert.equal(extractCursorFromInstructions(instructions), 'CURSOR_PAGE_2'); |
| 124 | assert.equal(extractCursorFromInstructions(instructions, 'Top'), undefined); |
| 125 | assert.equal(extractCursorFromInstructions(null), undefined); |
| 126 | assert.equal(extractCursorFromInstructions('not-array'), undefined); |
| 127 | }); |
| 128 | |
| 129 | test('HIGH-162: TimelineReplaceEntry overrides an earlier TimelineAddEntries cursor', () => { |
| 130 | const instructions = [ |
| 131 | { |
| 132 | type: 'TimelineAddEntries', |
| 133 | entries: [ |
| 134 | { |
| 135 | entryId: 'cursor-bottom-0', |
| 136 | content: { |
| 137 | entryType: 'TimelineTimelineCursor', |
| 138 | cursorType: 'Bottom', |
| 139 | value: 'OLD_CUR', |
| 140 | }, |
| 141 | }, |
| 142 | ], |
| 143 | }, |
| 144 | { |
| 145 | type: 'TimelineReplaceEntry', |
| 146 | entry_id_to_replace: 'cursor-bottom-0', |
| 147 | entry: { |
| 148 | entryId: 'cursor-bottom-0', |
| 149 | content: { |
| 150 | entryType: 'TimelineTimelineCursor', |
| 151 | cursorType: 'Bottom', |
| 152 | value: 'NEW_CUR', |
| 153 | }, |
| 154 | }, |
| 155 | }, |
| 156 | ]; |
| 157 | assert.equal(extractCursorFromInstructions(instructions), 'NEW_CUR'); |
| 158 | }); |
| 159 | |
| 160 | test('HIGH-162: renderContentState handles malformed blocks, entityMap, link ranges, and atomic blocks without TypeError', () => { |
| 161 | assert.equal(renderContentState(null), undefined); |
| 162 | assert.equal(renderContentState({ blocks: 'not-an-array' }), undefined); |
| 163 | assert.equal(renderContentState({ blocks: [null, undefined, 42] }), undefined); |
| 164 | |
| 165 | const rendered = renderContentState({ |
| 166 | entityMap: [ |
| 167 | null, |
| 168 | { key: '0', value: { type: 'LINK' } }, // missing data |
| 169 | { key: '1', value: { type: 'LINK', data: { url: 'https://example.com' } } }, |
| 170 | { key: '2', value: { type: 'MARKDOWN' } }, // missing data |
| 171 | { key: '3', value: { type: 'MARKDOWN', data: { markdown: 12345 } } }, // non-string markdown |
| 172 | { key: '4', value: { type: 'TWEET' } }, // missing data |
| 173 | { key: '5', value: { type: 'LINK' } }, // atomic LINK missing data |
| 174 | { key: '6', value: { type: 'MARKDOWN', data: { markdown: ' ```js\n1+1\n``` ' } } }, |
| 175 | { key: '7', value: { type: 'TWEET', data: { tweetId: '999' } } }, |
| 176 | { key: '8', value: { type: 'LINK', data: { url: 'https://x.com/test' } } }, |
| 177 | ], |
| 178 | blocks: [ |
| 179 | null, |
| 180 | { type: 'unstyled', text: undefined, entityRanges: [null, { key: 0, offset: 0, length: 4 }] }, |
| 181 | { type: 'unstyled', text: null, entityRanges: 'bad' }, |
| 182 | { |
| 183 | type: 'unstyled', |
| 184 | text: 'Read docs and broken', |
| 185 | entityRanges: [ |
| 186 | null, |
| 187 | { key: 0, offset: 14, length: 6 }, // missing entity.data |
| 188 | { key: 1, offset: 5, length: 4 }, // valid LINK |
| 189 | { key: 1, offset: 'bad', length: 4 }, // non-numeric offset |
| 190 | { key: 1, offset: -2, length: 4 }, // negative offset |
| 191 | { key: 1, offset: 0, length: -1 }, // negative length |
| 192 | { key: 1, offset: Number.NaN, length: 4 }, // NaN offset |
| 193 | { key: 1, offset: 0, length: Number.POSITIVE_INFINITY }, // Infinity length |
| 194 | { key: 1, offset: 1.5, length: 2 }, // non-integer offset |
| 195 | ], |
| 196 | }, |
| 197 | { type: 'atomic', entityRanges: [null] }, |
| 198 | { type: 'atomic', entityRanges: [{ key: 2 }] }, |
| 199 | { type: 'atomic', entityRanges: [{ key: 3 }] }, |
| 200 | { type: 'atomic', entityRanges: [{ key: 4 }] }, |
| 201 | { type: 'atomic', entityRanges: [{ key: 5 }] }, |
| 202 | { type: 'atomic', entityRanges: [{ key: 6 }] }, |
| 203 | { type: 'atomic', entityRanges: [{ key: 7 }] }, |
| 204 | { type: 'atomic', entityRanges: [{ key: 8 }] }, |
| 205 | ], |
| 206 | }); |
| 207 | |
| 208 | assert.equal( |
| 209 | rendered, |
| 210 | [ |
| 211 | 'Read [docs](https://example.com) and broken', |
| 212 | '```js\n1+1\n```', |
| 213 | '[Embedded Tweet: https://x.com/i/status/999]', |
| 214 | '[Link: https://x.com/test]', |
| 215 | ].join('\n\n'), |
| 216 | ); |
| 217 | |
| 218 | // Primitive entityMap should not throw |
| 219 | assert.equal( |
| 220 | renderContentState({ entityMap: 123, blocks: [{ type: 'unstyled', text: 'plain' }] }), |
| 221 | 'plain', |
| 222 | ); |
| 223 | }); |
| 224 | |
| 225 | test('HIGH-162: extractMedia handles non-array media/variants and null elements without TypeError', () => { |
| 226 | assert.equal(extractMedia({ legacy: { extended_entities: { media: 'not-array' } } }), undefined); |
| 227 | assert.equal(extractMedia({ legacy: { extended_entities: { media: [null, {}] } } }), undefined); |
| 228 | |
| 229 | const tweet = { |
| 230 | legacy: { |
| 231 | extended_entities: { |
| 232 | media: [ |
| 233 | null, |
| 234 | { |
| 235 | type: 'video', |
| 236 | media_url_https: 'https://pbs.twimg.com/thumb.jpg', |
| 237 | video_info: { variants: 'not-an-array' }, |
| 238 | }, |
| 239 | { |
| 240 | type: 'video', |
| 241 | media_url_https: 'https://pbs.twimg.com/thumb2.jpg', |
| 242 | video_info: { |
| 243 | duration_millis: 5000, |
| 244 | variants: [ |
| 245 | null, |
| 246 | { content_type: 'application/x-mpegURL', url: 'https://video.twimg.com/p.m3u8' }, |
| 247 | { content_type: 'video/mp4', bitrate: 832000, url: 'https://video.twimg.com/hi.mp4' }, |
| 248 | { content_type: 'video/mp4', bitrate: 256000, url: 'https://video.twimg.com/lo.mp4' }, |
| 249 | ], |
| 250 | }, |
| 251 | }, |
| 252 | ], |
| 253 | }, |
| 254 | }, |
| 255 | }; |
| 256 | |
| 257 | const media = extractMedia(tweet); |
| 258 | assert.equal(media?.length, 2); |
| 259 | assert.equal(media[0].videoUrl, undefined); |
| 260 | assert.equal(media[1].videoUrl, 'https://video.twimg.com/hi.mp4'); |
| 261 | assert.equal(media[1].durationMs, 5000); |
| 262 | }); |
| 263 | |
| 264 | test('HIGH-162: parseTweetsFromInstructions isolates throwing/malformed tweets and keeps valid siblings', () => { |
| 265 | const throwingTweet = { |
| 266 | rest_id: '201', |
| 267 | get core() { |
| 268 | throw new TypeError('unexpected getter crash'); |
| 269 | }, |
| 270 | }; |
| 271 | const badArticleTweet = makeRawTweet('202', 'alice', 'fallback text', { |
| 272 | article: { |
| 273 | article_results: { |
| 274 | result: { |
| 275 | content_state: { |
| 276 | blocks: [{ type: 'unstyled', text: null }, { type: 'atomic', entityRanges: [null] }], |
| 277 | entityMap: [null], |
| 278 | }, |
| 279 | }, |
| 280 | }, |
| 281 | }, |
| 282 | }); |
| 283 | const goodTweet = makeRawTweet('203', 'bob', 'valid sibling survives'); |
| 284 | |
| 285 | const instructions = [ |
| 286 | null, |
| 287 | 'not-an-object', |
| 288 | { |
| 289 | type: 'TimelineAddEntries', |
| 290 | entries: [ |
| 291 | null, |
| 292 | { content: { items: 'not-an-array' } }, |
| 293 | { content: { itemContent: { tweet_results: { result: throwingTweet } } } }, |
| 294 | { content: { itemContent: { tweet_results: { result: badArticleTweet } } } }, |
| 295 | { content: { itemContent: { tweet_results: { result: goodTweet } } } }, |
| 296 | ], |
| 297 | }, |
| 298 | ]; |
| 299 | |
| 300 | const tweets = parseTweetsFromInstructions(instructions, { quoteDepth: 1 }); |
| 301 | assert.deepEqual( |
| 302 | tweets.map((t) => t.id), |
| 303 | ['202', '203'], |
| 304 | ); |
| 305 | assert.equal(tweets[0].text, 'fallback text'); |
| 306 | assert.equal(tweets[1].text, 'valid sibling survives'); |
| 307 | assert.equal(extractArticleText({ article: 'bad' }), undefined); |
| 308 | assert.equal(mapTweetResult(goodTweet)?.id, '203'); |
| 309 | assert.deepEqual(parseUsersFromInstructions([null, { entries: 'bad' }]), []); |
| 310 | }); |
| 311 | |
| 312 | test('HIGH-162: searchPaged follows TimelineReplaceEntry cursor and preserves TweetWithVisibilityResults across pages', async () => { |
| 313 | class DummyBase { |
| 314 | constructor() { |
| 315 | this.quoteDepth = 1; |
| 316 | this.timeoutMs = 5000; |
| 317 | } |
| 318 | async getSearchTimelineQueryIds() { |
| 319 | return ['ValidQueryId123']; |
| 320 | } |
| 321 | getHeaders() { |
| 322 | return {}; |
| 323 | } |
| 324 | async refreshQueryIds() {} |
| 325 | async fetchTextWithTimeout(url) { |
| 326 | const parsed = new URL(url); |
| 327 | const vars = JSON.parse(parsed.searchParams.get('variables')); |
| 328 | if (!vars.cursor) { |
| 329 | return { |
| 330 | status: 200, |
| 331 | ok: true, |
| 332 | text: JSON.stringify({ |
| 333 | data: { |
| 334 | search_by_raw_query: { |
| 335 | search_timeline: { |
| 336 | timeline: { |
| 337 | instructions: [ |
| 338 | { |
| 339 | type: 'TimelineAddEntries', |
| 340 | entries: [ |
| 341 | { |
| 342 | content: { |
| 343 | itemContent: { |
| 344 | tweet_results: { |
| 345 | result: { |
| 346 | __typename: 'TweetWithVisibilityResults', |
| 347 | tweet: makeRawTweet('301', 'alice', 'page 1 visibility tweet'), |
| 348 | }, |
| 349 | }, |
| 350 | }, |
| 351 | }, |
| 352 | }, |
| 353 | ], |
| 354 | }, |
| 355 | { |
| 356 | type: 'TimelineReplaceEntry', |
| 357 | entry: { |
| 358 | content: { |
| 359 | cursorType: 'Bottom', |
| 360 | value: 'REPLACE_CURSOR_2', |
| 361 | }, |
| 362 | }, |
| 363 | }, |
| 364 | ], |
| 365 | }, |
| 366 | }, |
| 367 | }, |
| 368 | }, |
| 369 | }), |
| 370 | }; |
| 371 | } |
| 372 | assert.equal(vars.cursor, 'REPLACE_CURSOR_2'); |
| 373 | return { |
| 374 | status: 200, |
| 375 | ok: true, |
| 376 | text: JSON.stringify({ |
| 377 | data: { |
| 378 | search_by_raw_query: { |
| 379 | search_timeline: { |
| 380 | timeline: { |
| 381 | instructions: [ |
| 382 | { |
| 383 | type: 'TimelineAddEntries', |
| 384 | entries: [ |
| 385 | { |
| 386 | content: { |
| 387 | itemContent: { |
| 388 | tweet_results: { |
| 389 | result: makeRawTweet('302', 'bob', 'page 2 tweet'), |
| 390 | }, |
| 391 | }, |
| 392 | }, |
| 393 | }, |
| 394 | ], |
| 395 | }, |
| 396 | ], |
| 397 | }, |
| 398 | }, |
| 399 | }, |
| 400 | }, |
| 401 | }), |
| 402 | }; |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | const SearchClient = withSearch(DummyBase); |
| 407 | const client = new SearchClient(); |
| 408 | const res = await client.search('test', 25, { maxPages: 3 }); |
| 409 | assert.equal(res.success, true); |
| 410 | assert.deepEqual( |
| 411 | res.tweets.map((t) => t.id), |
| 412 | ['301', '302'], |
| 413 | ); |
| 414 | }); |
| 415 |