import type { Page, Route } from "@playwright/test"; // SOURCES NO LONGER PUBLIC (lane missing-source): one invented channel with a // video per state, served entirely by route mocks. Every name, id and address // here is made up. `record` overrides let a case make the transcript record and // the summaries disagree (an IndexedDB copy from before the state changed). export const CHANNEL = "Synth Channel"; export const CHANNEL_SLUG = "synth-channel"; export type Vid = { id: string; title: string; state?: "members_only" | "private" | "deleted" | "unlisted" | "maybe_missing"; stateSince?: string; isLivestream?: boolean; // What the transcript record says, when it differs from the summary. record?: { state?: string; stateSince?: string }; // A record with no platform address at all. noUrl?: boolean; }; export const VIDS: Vid[] = [ { id: "synthpub001", title: "Synth public talk" }, { id: "synthmem001", title: "Synth members stream", state: "members_only", stateSince: "20261119", isLivestream: true }, { id: "synthpri001", title: "Synth private talk", state: "private" }, { id: "synthdel001", title: "Synth removed talk", state: "deleted", stateSince: "20251231" }, { id: "synthunl001", title: "Synth unlisted talk", state: "unlisted", stateSince: "20260102" }, // The summaries say members-only; a stale record says nothing. { id: "synthstl001", title: "Synth stale record", state: "members_only", stateSince: "20261001", record: {} }, // The summaries say public again; a stale record still says removed. { id: "synthbak001", title: "Synth back again", record: { state: "deleted", stateSince: "20250101" } }, // Removed, and no address to link to. { id: "synthnou001", title: "Synth no address", state: "deleted", noUrl: true }, ]; export const CUES = [ { start: 0, end: 2, text: "first synth line" }, { start: 2, end: 3, text: "second synth line" }, { start: 3, end: 5, text: "third synth line" }, { start: 5, end: 6, text: "fourth synth line" }, ]; export const slugOf = (id: string) => `${CHANNEL_SLUG}/${id}`; export const watchUrl = (id: string) => `https://www.youtube.com/watch?v=${id}`; function summary(v: Vid) { return { slug: slugOf(v.id), id: v.id, channelSlug: CHANNEL_SLUG, title: v.title, uploadDate: "20260101", date: "2026-01-01", duration: "0:06", channel: CHANNEL, isLivestream: v.isLivestream === true, ageRestricted: false, isDeleted: v.state === "deleted", isUnlisted: v.state === "unlisted", ...(v.state ? { state: v.state } : {}), ...(v.state && v.stateSince ? { stateSince: v.stateSince } : {}), platform: "youtube" as const, webpageUrl: v.noUrl ? "" : watchUrl(v.id), }; } function record(v: Vid, cues: ReadonlyArray<{ start: number; end: number; text: string }> = CUES) { const base = summary(v); const { state: _s, stateSince: _d, isDeleted: _i, isUnlisted: _u, date: _dt, ...rest } = base as Record; const st = v.record ? v.record.state : v.state === "maybe_missing" ? undefined : v.state; const since = v.record ? v.record.stateSince : v.stateSince; return { ...rest, duration: 6, description: "", tags: [], ...(st ? { state: st } : {}), ...(st && since ? { stateSince: since } : {}), cues, }; } async function fulfillJson(route: Route, body: unknown) { await route.fulfill({ status: 200, contentType: "application/json", body: JSON.stringify(body) }); } // Every archive path the workspace asks for, mocked; every request to a video // platform recorded and aborted (`platformHits`). `summariesDelayMs` holds the // summaries pages back, so the transcript record lands first (a returning // visitor's cached record against a slow network). export async function installMissingSourceRoutes( page: Page, opts: { summariesDelayMs?: number; cues?: ReadonlyArray<{ start: number; end: number; text: string }> } = {}, ): Promise<{ platformHits: string[] }> { const platformHits: string[] = []; await page.route(/youtube\.com|youtube-nocookie\.com|ytimg\.com|googlevideo\.com/, async (route) => { platformHits.push(route.request().url()); await route.abort(); }); const list = VIDS.map(summary); await page.route("**/summaries/manifest.json", (route) => fulfillJson(route, { version: 3, totalCount: list.length, pageSize: 1000, pageCount: 1, generatedAt: new Date().toISOString(), channels: [{ name: CHANNEL, count: list.length, groupId: "default" }], groups: [{ id: "default", name: "All channels", selectedByDefault: true }], defaultGroupId: "default", }), ); await page.route("**/summaries/page-*.json", async (route) => { if (opts.summariesDelayMs) await new Promise((r) => setTimeout(r, opts.summariesDelayMs)); await fulfillJson(route, list); }); await page.route(/\/transcripts\/[^/]+\/manifest\.json$/, (route) => fulfillJson(route, { version: 1, channelSlug: CHANNEL_SLUG, pageCount: 1, maxPageBytes: 8388608, generatedAt: new Date().toISOString(), slugToPage: Object.fromEntries(VIDS.map((v) => [v.id, 0])), }), ); await page.route(/\/transcripts\/[^/]+\/page-\d+\.json$/, (route) => fulfillJson(route, VIDS.map((v) => record(v, opts.cues)))); await page.route("**/subs/manifest.json", (route) => fulfillJson(route, { version: 4, channels: [], totalCount: 0, liveChatTotalCount: 0, generatedAt: new Date().toISOString() }), ); return { platformHits }; } // A short mono 16-bit PCM WAV of a quiet tone, made in the test: no binary in // the repo, nothing fetched. export function makeWav(seconds = 8, rate = 8000): Buffer { const samples = seconds * rate; const buf = Buffer.alloc(44 + samples * 2); buf.write("RIFF", 0, "ascii"); buf.writeUInt32LE(36 + samples * 2, 4); buf.write("WAVE", 8, "ascii"); buf.write("fmt ", 12, "ascii"); buf.writeUInt32LE(16, 16); buf.writeUInt16LE(1, 20); // PCM buf.writeUInt16LE(1, 22); // mono buf.writeUInt32LE(rate, 24); buf.writeUInt32LE(rate * 2, 28); buf.writeUInt16LE(2, 32); buf.writeUInt16LE(16, 34); buf.write("data", 36, "ascii"); buf.writeUInt32LE(samples * 2, 40); for (let i = 0; i < samples; i++) { buf.writeInt16LE(Math.round(Math.sin((2 * Math.PI * 440 * i) / rate) * 2000), 44 + i * 2); } return buf; }