import { expect, test, type Page } from "@playwright/test"; import { installRoutes, urlParams } from "./helpers"; import { CHANNEL, CHANNEL_SLUG, VIDEO_CHAT_SMALL, VIDEO_TRANSCRIPT_ONLY, } from "./fixtures/data"; // The search-result duplicate affordance: a badge saying "this exists elsewhere // in the archive", plus a strip of jump buttons to the other copies. // // The load-bearing assertion here is the HONESTY RULE. Matching content does not // imply matching timings — a mirror with a longer intro carries the same words at // shifted times — so a jump only carries the current timestamp when the detector // MEASURED the two as aligned. An unaligned (or unmeasured) sibling opens at 0. // Get this wrong and the viewer lands mid-sentence in the wrong place on a jump // that looks like it worked, which is the exact failure measureAlignment exists // to prevent. const ALIGNED_SIBLING = "mirror-chan/aligned-copy"; const UNALIGNED_SIBLING = "mirror-chan/drifted-copy"; // "gamma" matches one transcript cue per fixture video, at start=100s. const HIT_SECONDS = 100; function dupRef( slug: string, over: Record = {}, ): Record { const [channelSlug, id] = slug.split("/"); return { slug, channelSlug, channel: channelSlug === CHANNEL_SLUG ? CHANNEL : "Mirror Chan", platform: channelSlug === CHANNEL_SLUG ? "youtube" : "rumble", id, title: `Title ${id}`, duration: 300, uploadDate: "20260101", hasTranscript: true, ...over, }; } function duplicatesReport() { return { version: 1, generatedAt: "2026-07-01T00:00:00.000Z", runConfig: { thresholdSeconds: null, durationToleranceSeconds: 2, nearThreshold: 0.6, containmentThreshold: 0.8, shingleSize: 5, blocking: "title", }, totals: { videosScanned: 4, clusters: 2, videosInClusters: 4 }, clusters: [ { // Measured as aligned: the jump may carry the moment. clusterId: "cluster-aligned", matchKind: "transcript-exact", score: 1, contained: false, durationBucket: 300, crossPlatform: true, crossChannel: true, canonicalSlug: `${CHANNEL_SLUG}/${VIDEO_TRANSCRIPT_ONLY}`, videoRefs: [ dupRef(`${CHANNEL_SLUG}/${VIDEO_TRANSCRIPT_ONLY}`, { aligned: true, offsetSeconds: 0, }), dupRef(ALIGNED_SIBLING, { aligned: true, offsetSeconds: 1.2 }), ], }, { // Same content, DIFFERENT timings — a 41s intro difference. The jump // must fall back to the start of the sibling. clusterId: "cluster-drifted", matchKind: "transcript-near", score: 0.91, contained: false, durationBucket: 300, crossPlatform: true, crossChannel: true, canonicalSlug: `${CHANNEL_SLUG}/${VIDEO_CHAT_SMALL}`, videoRefs: [ dupRef(`${CHANNEL_SLUG}/${VIDEO_CHAT_SMALL}`, { aligned: true, offsetSeconds: 0, }), dupRef(UNALIGNED_SIBLING, { aligned: false, offsetSeconds: 41.5 }), ], }, ], }; } async function installDuplicates(page: Page, report: unknown) { await page.route("**/duplicates.json", async (route) => { await route.fulfill({ status: 200, contentType: "application/json", body: JSON.stringify(report), }); }); } function qt(query: string): string { return encodeURIComponent( JSON.stringify({ k: "g", o: "AND", c: [{ k: "l", q: query, s: "transcripts" }], }), ); } function card(page: Page, slug: string) { return page.locator(`[data-result-slug="${slug}"]`); } test.describe("search result duplicates", () => { test("badges a result that exists elsewhere and jumps to the moment when aligned", async ({ page, }) => { await installRoutes(page); await installDuplicates(page, duplicatesReport()); await page.goto(`/?qt=${qt("gamma")}`); const alignedCard = card(page, `${CHANNEL_SLUG}/${VIDEO_TRANSCRIPT_ONLY}`); await expect(alignedCard).toBeVisible(); await expect(alignedCard.getByText("Dupe", { exact: true })).toBeVisible(); // The jump button states where it will land, and it lands there. const jump = alignedCard.locator( `[data-duplicate-slug="${ALIGNED_SIBLING}"]`, ); await expect(jump).toHaveAttribute("data-duplicate-aligned", "true"); await expect(jump).toContainText("01:40"); await jump.click(); const params = await urlParams(page); expect(params.get("v")).toBe(ALIGNED_SIBLING); expect(params.get("t")).toBe(String(HIT_SECONDS)); }); test("an unaligned sibling opens at the start, not at the current moment", async ({ page, }) => { await installRoutes(page); await installDuplicates(page, duplicatesReport()); await page.goto(`/?qt=${qt("gamma")}`); const driftedCard = card(page, `${CHANNEL_SLUG}/${VIDEO_CHAT_SMALL}`); await expect(driftedCard).toBeVisible(); await expect(driftedCard.getByText("Dupe", { exact: true })).toBeVisible(); const jump = driftedCard.locator( `[data-duplicate-slug="${UNALIGNED_SIBLING}"]`, ); await expect(jump).toHaveAttribute("data-duplicate-aligned", "false"); // No timestamp is offered, because none was measured. await expect(jump).not.toContainText("@"); await jump.click(); const params = await urlParams(page); expect(params.get("v")).toBe(UNALIGNED_SIBLING); expect(params.get("t")).toBe("0"); }); test("a result in no cluster carries no badge and no jump strip", async ({ page, }) => { await installRoutes(page); await installDuplicates(page, duplicatesReport()); await page.goto(`/?qt=${qt("gamma")}`); const lone = card(page, `${CHANNEL_SLUG}/vid-chat-large`); await expect(lone).toBeVisible(); await expect(lone.getByText("Dupe", { exact: true })).toHaveCount(0); await expect(lone.locator("[data-duplicate-strip]")).toHaveCount(0); }); test("no duplicates.json means no duplicate affordance at all", async ({ page, }) => { // The overwhelmingly common case: the file is absent, and search must be // completely unaffected rather than erroring on the 404. await installRoutes(page); await page.goto(`/?qt=${qt("gamma")}`); const anyCard = card(page, `${CHANNEL_SLUG}/${VIDEO_TRANSCRIPT_ONLY}`); await expect(anyCard).toBeVisible(); await expect(page.locator("[data-duplicate-strip]")).toHaveCount(0); }); });