Files
gerbeur/src/test/components/dump/RichContentCard.test.tsx
khannurien 2c3ba6ad11 v4: link previews are read once, in one place, and no longer take an error page, a relative image or a mask icon for the page's — each provider fetched its page and scanned it with regular expressions of its own, the generic one some fifteen times, and Bandcamp and SoundCloud read only OG tags. fetchPage (remote/fetch.ts) now fetches every page, follows redirects to the address it was served from, and refuses an error status, so a 404 or a bot challenge is unreachable rather than a preview titled "Just a moment...", and a refresh no longer overwrites good metadata with it. parsePage (remote/html.ts) reads it in one linear pass with a real attribute reader: unquoted values, a > inside quotes, og: tags under name as well as property, and nothing from comments or scripts. Every URL a page names is resolved against where it was served from, so a relative og:image works; entities are decoded in one pass, &eacute; and &rsquo; included, and one that names no character no longer fails the preview (bandcamp-tralbum.ts shares the decoder). The mask icon is no longer the favicon, JSON-LD prefers the page's node over the site's, tracking pixels and lazy placeholders aren't thumbnails, an og:url with a slug keeps the OG tags, and a <title> loses its site suffix. genericPreview is the base of the Bandcamp, SoundCloud and YouTube channel previews; SoundCloud takes www., m. and on.soundcloud.com short links, embedding the page they lead to, and Bandcamp keeps only its own player. The service tidies every preview: text on one line, titles and descriptions capped, only http(s) images and players. RichContent.type is now RichContentType, and the unused videoId is gone.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-09 20:46:32 +00:00

110 lines
3.2 KiB
TypeScript

import { describe, expect, test, vi } from "vitest";
import { screen } from "@testing-library/react";
import { userEvent } from "@testing-library/user-event";
import { RichContentCard } from "../../../components/dump/RichContentCard.tsx";
import { renderWithContexts } from "../../render.tsx";
import type { RichContent } from "../../../../shared/dump.ts";
describe("RichContentCard", () => {
const yt = {
type: "youtube",
url: "https://youtube.com/watch?v=x",
title: "A video",
siteName: "YouTube",
description: "About it",
thumbnailUrl: "https://i.ytimg.com/x.jpg",
embedUrl: "https://www.youtube.com/embed/x",
} satisfies RichContent;
test("full card: play button, source link and details", async () => {
const play = vi.fn();
renderWithContexts(
<RichContentCard richContent={yt} dumpHref="/dumps/d" />,
{
player: { play },
},
);
expect(screen.getByRole("link", { name: /A video/ })).toHaveAttribute(
"href",
yt.url,
);
expect(screen.getByText("About it")).toBeInTheDocument();
await userEvent.click(screen.getByRole("button", { name: "Play" }));
expect(play).toHaveBeenCalledWith(expect.objectContaining({
kind: "embed",
embedUrl: yt.embedUrl,
dumpHref: "/dumps/d",
}));
});
test("compact: shows Pause while this card plays", () => {
renderWithContexts(<RichContentCard richContent={yt} compact />, {
player: {
playing: true,
current: { kind: "embed", embedUrl: yt.embedUrl, type: "youtube" },
},
});
expect(screen.getByRole("button", { name: "Pause" })).toHaveClass(
"is-playing",
);
});
test("compact and unplayable: a link to the source", () => {
renderWithContexts(
<RichContentCard
richContent={{
type: "generic",
url: "https://blog.test/p",
siteName: "Blog",
}}
compact
/>,
);
expect(screen.getByRole("link", { name: "Open on Blog" })).toHaveAttribute(
"href",
"https://blog.test/p",
);
});
test("a dump thumbnail overrides the provider's", () => {
renderWithContexts(
<RichContentCard
richContent={yt}
thumbnailOverrideUrl="http://localhost:8000/api/dumps/d/thumbnail"
/>,
);
expect(screen.getByRole("img", { name: "A video" })).toHaveAttribute(
"src",
"http://localhost:8000/api/dumps/d/thumbnail",
);
});
test("a native stream that is the active item toggles instead of restarting", async () => {
const togglePlay = vi.fn();
const play = vi.fn();
const bc = {
type: "bandcamp",
url: "https://a.bandcamp.com/album/x",
embedUrl: "https://bandcamp.com/EmbeddedPlayer/1",
} satisfies RichContent;
renderWithContexts(<RichContentCard richContent={bc} compact />, {
player: {
togglePlay,
play,
playing: true,
current: {
kind: "stream",
type: "bandcamp",
streamUrl: "s",
resolveUrl: bc.url,
resolveIndex: 0,
resolvedAt: 0,
},
},
});
await userEvent.click(screen.getByRole("button", { name: "Pause" }));
expect(togglePlay).toHaveBeenCalled();
expect(play).not.toHaveBeenCalled();
});
});