Files
antisocial/test/x.test.ts
T
thatguygriffandClaude Opus 5 6325f0ff32
CI / Typecheck, test, build (pull_request) Successful in 9s
Add Reddit with its comment threads, and show quoted posts whole
Two changes. They share the `Segment` model, which is why they arrive
together.

## Reddit

A new adapter under /reddit, plus the thread beneath the post -- on Reddit the
conversation is usually the reason the link was shared, so a viewer that showed
only the post would be showing the wrong half.

The `.json` twin of a post URL is the post and the whole first page of comments
in one response, far better than anything the page gives up, so it is the only
layer that normally runs. Reddit refuses it to a browser it has never seen and
answers with a JavaScript challenge, which any ordinary navigation solves by
itself; the adapter navigates once and retries, and the cookie left behind
serves every later post. Below that, `shreddit-comment` elements are read from
the rendered page -- flat, each carrying its own `depth`, so `treeFromDepths`
rebuilds the nesting.

`Post.comments` is a tree rather than a flat list with depths, because folding
a comment has to take everything under it along and nesting is what makes that
free. Each comment renders as a `<details open>`, so collapsing works with the
stylesheet off and from the keyboard, and a collapsed one says how many replies
it is hiding. "Collapse all" is the only part that needs the script, so it
ships hidden and appears once the script has run. What sits behind a "load
more" is not fetched -- that is a second page and often a third -- but it is
counted and said out loud rather than quietly dropped.

Four things the payloads got wrong on the first try, each now with a fixture:
`fallback_url` is the video track alone whenever `has_audio` is true, so a post
with sound has to use `hls_url` and only a silent one gets the proxied MP4;
`scrubber_media_url` looks like a poster and is a second MP4 for the timeline
thumbnails, while the still is in `preview.images`; a gallery's pictures live
in `media_metadata` keyed and unordered, with their order only in
`gallery_data`; and `replies` is the string "" rather than an object when there
are none. Comment bodies are Markdown, rendered by a new render/markdown.ts
that escapes first and then puts back only the constructs we chose to support
-- never Reddit's own `body_html`, which would mean trusting markup a stranger
caused to be generated.

An app share link (/r/<sub>/s/<code>) is a plain 301, so one request told not
to follow it is enough. The permalink it resolves to is what the copy button
hands back, since an opaque share code is a tracking parameter by another name.

## Quoted posts

Both X and Bluesky lifted the quoted post's media out and showed it as the
quoter's own, dropping the quoted words and the quoted author entirely. A quote
of a photo post therefore rendered as somebody else's picture under the wrong
name with nothing to say so, and a quote that had a picture of its own dropped
the quoted one instead -- the two could never both appear. Half the quote posts
people share are someone answering a stranger and the other half are someone
continuing a thought from an earlier post; neither reads with only one side of
it on the page.

`Segment.quoted` now carries the whole thing -- author, words, pictures, time
and a link to it -- and renders as a post inside the post. Neither payload
carries a usable address for it: X has no permalink and it is rebuilt from the
handle and `id_str`, Bluesky has an `at://` URI nobody can open and it is
rebuilt from the handle and the record key. On Bluesky the record sits at
`embed.record` for a plain quote and at `embed.record.record` when the quoting
post has media of its own, and a quote can also point at a feed, a list or a
post since deleted, which arrive in the same slot under a different `$type` --
only `app.bsky.embed.record#viewRecord` is taken.

Two things about X's text, both visible on any post and not only a quote. It
arrives pre-escaped, so an ampersand someone typed was reaching the page as the
literal `&amp;`; it is decoded in the adapter, where the encoding comes from,
leaving the escape-on-the-way-out rule alone. And every link is a `t.co`, which
tells the reader nothing and routes them through X's click tracker to find out
-- `entities.urls` carries the real address alongside, so it is put back. The
shortlink X staples onto the end of a quote post is dropped rather than
expanded, since the post it points at is already on the page;
`display_text_range` is where that boundary is and it keeps a link the author
put there deliberately. Its indices are UTF-16 units into the escaped text, so
the slice happens before decoding and before expanding, and splitting to
codepoints first overshoots past an emoji -- checked against a post carrying
one.

A quote is context, and context that fills the screen has stopped being
context, so a segment carrying one gives up the window-filling cap on its own
media.

## Along the way

setupMedia only ever wired `document.querySelector('.media')`, the first rail
on the page. That was already wrong for a Bluesky or Threads chain with media
in more than one post, and a quoted carousel would have hit it too. It now runs
per rail.

## Verified

108 tests, typecheck and build clean. Resolved end to end against the live
platforms: Reddit self, gallery, video, link and share-link posts; X quotes
with no media, with media on the quoted side, and with media on both; Bluesky
quotes in both embed shapes, including a ten-post chain where every post quotes
a different account and all nine quotes come back under the right name.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017nMQ2eDKnqALYhAibpTKTu
2026-08-27 11:45:40 -03:00

169 lines
6.7 KiB
TypeScript

import assert from 'node:assert/strict';
import { test } from 'node:test';
import { mediaFromDetails, quotedFrom, 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 carries the post it quotes, whose it is included', () => {
// Someone continuing their own thought from an earlier post. Without the
// quoted half on the page the remaining half says nothing.
const post = toPost(fixture('x/quote-text.json'), URL_);
const quoted = post.segments[0]?.quoted;
assert.ok(quoted, 'the quoted post must survive');
assert.equal(quoted?.author.handle, '@MikeMcMahonCHN');
assert.ok(quoted?.author.displayName);
assert.match(quoted?.text ?? '', /Matthew Mayich/);
assert.ok(quoted?.postedAt);
// No permalink in the payload, so it is rebuilt from the handle and the id.
assert.equal(quoted?.url, 'https://x.com/MikeMcMahonCHN/status/2092384930034548928');
});
test('the quoted post keeps its own pictures instead of lending them to the quoter', () => {
const post = toPost(fixture('x/quote-photo.json'), URL_);
assert.deepEqual(post.segments[0]?.media, [], 'the quoter attached nothing');
assert.equal(post.segments[0]?.quoted?.media.length, 1);
assert.equal(post.segments[0]?.quoted?.media[0]?.kind, 'image');
});
test('media on both sides stays on the side it came from', () => {
// This is the case the old either-or could not represent at all: it showed
// the quoter's picture and silently dropped the one being talked about.
const segment = toPost(fixture('x/quote-both.json'), URL_).segments[0];
assert.equal(segment?.media.length, 1);
assert.equal(segment?.quoted?.media.length, 1);
assert.notEqual(segment?.media[0]?.url, segment?.quoted?.media[0]?.url);
});
test('a post quoting nothing has no quoted post', () => {
assert.equal(toPost(fixture('x/photo.json'), URL_).segments[0]?.quoted, undefined);
assert.equal(quotedFrom(undefined), undefined);
// A quoted post X will not describe is nothing to show.
assert.equal(quotedFrom({ text: 'orphaned' }), undefined);
});
test('the shortlink X staples to a quote post is not shown', () => {
// The quoted post is right there on the page; a t.co pointing at it is
// noise, which is why X hides it too. `display_text_range` says where it
// starts, and keeps a link the author put there deliberately.
const both = toPost(fixture('x/quote-both.json'), URL_).segments[0];
// Two links arrive: one the author wrote, one X appended pointing at the
// quoted post. Exactly the second goes.
assert.equal((both?.text?.match(/https?:\/\//g) ?? []).length, 1, "the author's own link stays");
assert.match(both?.text ?? '', /on3\.com/);
assert.ok(!both?.text?.includes('QdJhOVu4En'));
const photo = toPost(fixture('x/quote-photo.json'), URL_).segments[0];
assert.ok(!photo?.quoted?.text?.includes('t.co'), 'and the same on the quoted post');
});
test('a t.co stands aside for the address it stands in for', () => {
// Left alone the page says `t.co/rEGx9JN5pJ`, which tells the reader
// nothing and sends them through X's click tracker to find out.
const segment = toPost(fixture('x/quote-both.json'), URL_).segments[0];
assert.ok(!segment?.text?.includes('t.co/'), `still shortened: ${segment?.text}`);
assert.match(segment?.text ?? '', /on3\.com/);
assert.match(segment?.quoted?.text ?? '', /on3\.com/);
});
test('a shortlink the payload does not explain is left as it is', () => {
const post = toPost(
{
text: 'see https://t.co/unknown1 and https://t.co/known123',
user: { screen_name: 'someone' },
entities: { urls: [{ url: 'https://t.co/known123', expanded_url: 'https://example.com/real' }] },
},
URL_,
);
assert.equal(post.segments[0]?.text, 'see https://t.co/unknown1 and https://example.com/real');
});
test('an expansion that is not an http address is refused', () => {
const post = toPost(
{
text: 'https://t.co/abc',
user: { screen_name: 'someone' },
entities: { urls: [{ url: 'https://t.co/abc', expanded_url: 'javascript:alert(1)' }] },
},
URL_,
);
assert.equal(post.segments[0]?.text, 'https://t.co/abc');
});
test('the visible range is applied before the entities are decoded', () => {
// The indices are into the escaped text, where `&amp;` is five characters.
// Decoding first shifts everything after it and truncates the tail.
const post = toPost(
{
text: 'Tom &amp; Jerry https://t.co/xxx',
display_text_range: [0, 15],
user: { screen_name: 'someone' },
},
URL_,
);
assert.equal(post.segments[0]?.text, 'Tom & Jerry');
});
test('escaped characters X hands back are decoded, not shown as entities', () => {
// The syndication payload arrives pre-escaped; everything downstream
// escapes again on the way out, so leaving these puts `&amp;` on screen.
const post = toPost(
{
text: 'Tom &amp; Jerry &lt;3 &quot;quoted&quot;',
user: { screen_name: 'someone' },
quoted_tweet: { text: 'me &amp; you', user: { screen_name: 'other' }, id_str: '9' },
},
URL_,
);
assert.equal(post.segments[0]?.text, 'Tom & Jerry <3 "quoted"');
assert.equal(post.segments[0]?.quoted?.text, 'me & you');
});