import { describe, expect, it } from "vitest"; import { DefinitionEngine, matchesUrlPattern, selectDefinition } from "../src/content/definition-engine"; import { BUILTIN_DEFINITIONS } from "../src/content/definitions"; import type { SiteDefinition } from "../src/shared/types"; const definition: SiteDefinition = { id: "example-social", name: "Example Social", urlPatterns: ["https://*.example.test/*"], requiredSelectors: [".feed"], post: { rootSelectors: ["article.post"], textSelectors: [".content"], excludedSelectors: [".exclude"], idAttributes: ["data-id"], permalinkSelectors: ["a.permalink[href]"] } }; describe("DefinitionEngine", () => { it("discovers the supplied root, extracts only visible text, and uses a stable ID", () => { document.body.innerHTML = '
Visible skiphidden
'; const post = new DefinitionEngine(definition).discover(document.querySelector("article")!)[0]; expect(post).toMatchObject({ id: "example-social:42", text: "visible" }); }); it("uses permalink IDs and selects only definitions with URL and marker matches", () => { document.body.innerHTML = '
Hello
'; const engine = new DefinitionEngine(definition); expect(engine.discover(document)[0]?.id).toContain("/users/a/statuses/1"); expect(selectDefinition([definition], "https://social.example.test/home")).toBe(definition); expect(selectDefinition([definition], "https://other.test/home")).toBeUndefined(); }); it("matches browser-style URL patterns", () => { expect(matchesUrlPattern("https://*.example.test/*", "https://a.example.test/path?q=1")).toBe(true); expect(matchesUrlPattern("https://*.example.test/*", "http://a.example.test/path")).toBe(false); }); it("covers Mastodon timeline, profile, and detailed reply status containers", () => { document.body.innerHTML = `
Timeline post
Profile post
Reply in thread
`; const posts = new DefinitionEngine(BUILTIN_DEFINITIONS[0]!).discover(document); expect(posts.map((post) => post.id)).toEqual(["mastodon:timeline", "mastodon:profile", "mastodon:http://localhost:3000/users/a/statuses/3"]); expect(posts.map((post) => post.text)).toEqual(["timeline post", "profile post", "reply in thread"]); }); it("does not include hidden Mastodon content-warning text until it is revealed", () => { document.body.innerHTML = '

Content warning Show more

'; const engine = new DefinitionEngine(BUILTIN_DEFINITIONS[0]!); expect(engine.discover(document)[0]?.text).toBe("content warning"); document.querySelector("#hidden")?.removeAttribute("style"); expect(engine.discover(document)[0]?.text).toBe("content warning hidden post text"); }); it("rediscovers a Mastodon status when a content warning is revealed", async () => { document.body.innerHTML = '

Content warning

'; const engine = new DefinitionEngine(BUILTIN_DEFINITIONS[0]!); const observed: string[] = []; const stop = engine.observe((posts) => observed.push(...posts.map((post) => post.text))); document.querySelector("#hidden")?.removeAttribute("style"); await new Promise((resolve) => setTimeout(resolve, 0)); stop(); expect(observed).toContain("content warning revealed text"); }); });