Add Reddit comments and X definitions

This commit is contained in:
Jordan Wages 2026-08-25 22:34:25 -05:00
commit 1a9ab6ebdf
7 changed files with 129 additions and 2 deletions

View file

@ -95,6 +95,22 @@ describe("DefinitionEngine", () => {
expect(posts.map((post) => post.text)).toEqual(["the main post on the threads detail page", "a reply on the threads detail page"]);
});
it("discovers posts from the old Reddit interface using fullname IDs", () => {
document.body.innerHTML = readFileSync("tests/fixtures/reddit-old-feed.html", "utf8");
const reddit = BUILTIN_DEFINITIONS.find((candidate) => candidate.id === "reddit-old")!;
const posts = new DefinitionEngine(reddit).discover(document);
expect(posts.map((post) => post.id)).toEqual(["reddit-old:t3_abc123", "reddit-old:t1_def456"]);
expect(posts.map((post) => post.text)).toEqual(["the post body with visible text.", "a useful comment with visible text."]);
});
it("discovers X posts using semantic tweet markers and permalink IDs", () => {
document.body.innerHTML = readFileSync("tests/fixtures/x-feed.html", "utf8");
const x = BUILTIN_DEFINITIONS.find((candidate) => candidate.id === "x")!;
const posts = new DefinitionEngine(x).discover(document);
expect(posts.map((post) => post.id)).toEqual(["x:http://localhost:3000/alice/status/1234567890"]);
expect(posts[0]?.text).toBe("a useful post on x.");
});
it("selects Threads on current and legacy domains", () => {
const threads = BUILTIN_DEFINITIONS.find((candidate) => candidate.id === "threads")!;
document.body.innerHTML = '<div data-pressable-container="true"></div>';
@ -104,6 +120,17 @@ describe("DefinitionEngine", () => {
expect(selectDefinition([threads], "https://example.com/home")).toBeUndefined();
});
it("selects Reddit old and X only on their supported domains", () => {
const reddit = BUILTIN_DEFINITIONS.find((candidate) => candidate.id === "reddit-old")!;
const x = BUILTIN_DEFINITIONS.find((candidate) => candidate.id === "x")!;
document.body.innerHTML = '<div id="siteTable"><div class="thing link"></div></div><article data-testid="tweet"></article>';
expect(selectDefinition([reddit], "https://old.reddit.com/r/all")).toBe(reddit);
expect(selectDefinition([reddit], "https://www.reddit.com/r/all")).toBeUndefined();
expect(selectDefinition([x], "https://x.com/home")).toBe(x);
expect(selectDefinition([x], "https://twitter.com/alice/status/123")).toBe(x);
expect(selectDefinition([x], "https://example.com/home")).toBeUndefined();
});
it("can identify a specific site while its SPA markers are still loading", () => {
const threads = BUILTIN_DEFINITIONS.find((candidate) => candidate.id === "threads")!;
document.body.innerHTML = "";

9
tests/fixtures/reddit-old-feed.html vendored Normal file
View file

@ -0,0 +1,9 @@
<div id="siteTable">
<div class="thing link" data-fullname="t3_abc123">
<p class="title"><a class="title" href="https://old.reddit.com/r/example/comments/abc123/example_post/">A useful Reddit post</a></p>
<div class="entry"><div class="expando"><div class="md">Hidden expanded media controls</div></div><div class="md"><p>The post body with visible text.</p></div></div>
</div>
<div class="thing comment" data-fullname="t1_def456">
<div class="entry"><div class="md"><p>A useful comment with visible text.</p></div><a class="bylink" href="https://old.reddit.com/r/example/comments/abc123/example_post/def456/">permalink</a></div>
</div>
</div>

7
tests/fixtures/x-feed.html vendored Normal file
View file

@ -0,0 +1,7 @@
<main>
<article data-testid="tweet">
<div data-testid="User-Name"><span>alice</span></div>
<a href="/alice/status/1234567890"><time datetime="2026-08-25T00:00:00.000Z">1h</time></a>
<div data-testid="tweetText">A useful post on X.</div>
</article>
</main>