Add site definition worker inference

This commit is contained in:
Jordan Wages 2026-08-25 17:05:11 -05:00
commit 3ccdaaf953
17 changed files with 250 additions and 50 deletions

View file

@ -1,6 +1,6 @@
import { describe, expect, it } from "vitest";
import { readFileSync } from "node:fs";
import { DefinitionEngine, matchesUrlPattern, selectDefinition } from "../src/content/definition-engine";
import { DefinitionEngine, hasPotentialDefinitionForUrl, matchesUrlPattern, selectDefinition, selectDefinitionForUrl } from "../src/content/definition-engine";
import { BUILTIN_DEFINITIONS } from "../src/content/definitions";
import type { SiteDefinition } from "../src/shared/types";
@ -84,6 +84,17 @@ describe("DefinitionEngine", () => {
expect(posts.map((post) => post.text)).toEqual(["first threads post with visible text", "a reply in the conversation"]);
});
it("discovers Threads detail-page posts from pagelet containers", () => {
document.body.innerHTML = readFileSync("tests/fixtures/threads-post-page.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(["the main post on the threads detail page", "a reply on the threads detail page"]);
});
it("selects Threads on current and legacy domains", () => {
const threads = BUILTIN_DEFINITIONS.find((candidate) => candidate.id === "threads")!;
document.body.innerHTML = '<div role="article" data-pressable-container="true"></div>';
@ -92,4 +103,23 @@ describe("DefinitionEngine", () => {
}
expect(selectDefinition([threads], "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 = "";
expect(selectDefinition([threads], "https://www.threads.com/")).toBeUndefined();
expect(selectDefinitionForUrl([BUILTIN_DEFINITIONS[0]!, threads], "https://www.threads.com/")).toBe(threads);
});
it("keeps broad definitions pending until their hydration markers appear", () => {
const mastodon = BUILTIN_DEFINITIONS.find((candidate) => candidate.id === "mastodon")!;
const threads = BUILTIN_DEFINITIONS.find((candidate) => candidate.id === "threads")!;
document.body.innerHTML = "";
expect(selectDefinition([mastodon], "https://mastodon.example/@user")).toBeUndefined();
expect(selectDefinitionForUrl([mastodon], "https://mastodon.example/@user")).toBeUndefined();
expect(hasPotentialDefinitionForUrl([mastodon], "https://mastodon.example/@user")).toBe(true);
expect(hasPotentialDefinitionForUrl([threads], "https://example.com/")).toBe(false);
document.body.innerHTML = '<div class="status"><div class="status__content">Ready</div></div>';
expect(selectDefinition([mastodon], "https://mastodon.example/@user")).toBe(mastodon);
});
});