import assert from 'node:assert/strict'; import { test } from 'node:test'; import { mediaFromDetails, toPost } from '../src/platforms/x.ts'; import { fixture } from './helpers.ts'; const URL_ = 'https://x.com/example/status/1'; test('a text-only post carries no media', () => { const post = toPost(fixture('x/text-only.json'), URL_); assert.equal(post.platform, 'x'); assert.equal(post.textPosition, 'above'); assert.deepEqual(post.segments[0]?.media, []); assert.equal(post.author.handle, '@jack'); assert.ok(post.segments[0]?.postedAt); }); test('a photo post keeps every photo and asks for the original size', () => { const media = toPost(fixture('x/photo.json'), URL_).segments[0]?.media ?? []; assert.ok(media.length > 1, 'expected more than one photo'); assert.ok(media.every((m) => m.kind === 'image')); assert.ok(media.every((m) => m.url.endsWith('?name=orig'))); }); test('a video picks the highest-bitrate mp4 and ignores the streaming variants', () => { const media = mediaFromDetails([ { type: 'video', media_url_https: 'https://pbs.twimg.com/poster.jpg', original_info: { w: 1280, h: 720 }, video_info: { duration_millis: 30_500, variants: [ { content_type: 'application/x-mpegURL', url: 'https://video.twimg.com/p.m3u8' }, { content_type: 'video/mp4', bitrate: 832_000, url: 'https://video.twimg.com/low.mp4' }, { content_type: 'video/mp4', bitrate: 2_176_000, url: 'https://video.twimg.com/high.mp4' }, ], }, }, ]); assert.deepEqual(media, [ { kind: 'video', url: 'https://video.twimg.com/high.mp4', poster: { url: 'https://pbs.twimg.com/poster.jpg' }, durationSec: 31, width: 1280, height: 720, }, ]); }); test('a quote post shows the media of the post it quotes', () => { // The quote itself carries none; without this the page is text and nothing // else, which is exactly the thing the reader wanted to see. const post = toPost( { text: 'look at this', user: { screen_name: 'someone' }, mediaDetails: [], quoted_tweet: { mediaDetails: [{ type: 'photo', media_url_https: 'https://pbs.twimg.com/q.jpg' }], }, }, URL_, ); assert.equal(post.segments[0]?.media.length, 1); assert.equal(post.segments[0]?.media[0]?.url, 'https://pbs.twimg.com/q.jpg?name=orig'); });