Fix Threads post text extraction
This commit is contained in:
parent
bedeb8495d
commit
ece11a8352
4 changed files with 52 additions and 2 deletions
|
|
@ -1,4 +1,5 @@
|
|||
import { describe, expect, it } from "vitest";
|
||||
import { readFileSync } from "node:fs";
|
||||
import { DefinitionEngine, matchesUrlPattern, selectDefinition } from "../src/content/definition-engine";
|
||||
import { BUILTIN_DEFINITIONS } from "../src/content/definitions";
|
||||
import type { SiteDefinition } from "../src/shared/types";
|
||||
|
|
@ -59,4 +60,21 @@ describe("DefinitionEngine", () => {
|
|||
stop();
|
||||
expect(observed).toContain("content warning revealed text");
|
||||
});
|
||||
|
||||
it("discovers Threads posts from stable semantic/data selectors and permalink IDs", () => {
|
||||
document.body.innerHTML = readFileSync("tests/fixtures/threads-feed.html", "utf8");
|
||||
const threads = BUILTIN_DEFINITIONS.find((candidate) => candidate.id === "threads")!;
|
||||
const posts = new DefinitionEngine(threads).discover(document);
|
||||
expect(posts.map((post) => post.id)).toEqual(["threads:http://localhost:3000/@alice/post/ABC123", "threads:http://localhost:3000/@bob/post/DEF456"]);
|
||||
expect(posts.map((post) => post.text)).toEqual(["first threads post with visible text", "a reply in the conversation"]);
|
||||
});
|
||||
|
||||
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>';
|
||||
for (const url of ["https://threads.com/home", "https://www.threads.com/@alice/post/ABC123", "https://threads.net/home", "https://www.threads.net/@alice/post/ABC123"]) {
|
||||
expect(selectDefinition([threads], url)).toBe(threads);
|
||||
}
|
||||
expect(selectDefinition([threads], "https://example.com/home")).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
|
|
|||
15
tests/fixtures/threads-feed.html
vendored
Normal file
15
tests/fixtures/threads-feed.html
vendored
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
<main>
|
||||
<div data-pressable-container="true">
|
||||
<a href="/@alice"><span dir="auto">alice</span></a>
|
||||
<a href="/@alice/post/ABC123"><time><span dir="auto">12h</span></time></a>
|
||||
<span dir="auto">First Threads post with <span>visible text</span></span>
|
||||
<button><span dir="auto">Like</span></button>
|
||||
<time><span dir="auto">1h</span></time>
|
||||
</div>
|
||||
<div role="article">
|
||||
<a href="/@bob"><span dir="auto">bob</span></a>
|
||||
<a href="/@bob/post/DEF456"><time><span dir="auto">1d</span></time></a>
|
||||
<span dir="auto">A reply in the conversation</span>
|
||||
<div aria-hidden="true">Decorative content</div>
|
||||
</div>
|
||||
</main>
|
||||
Loading…
Reference in a new issue