diff --git a/.coderabbit.yaml b/.coderabbit.yaml new file mode 100644 index 0000000..3fa0b8a --- /dev/null +++ b/.coderabbit.yaml @@ -0,0 +1,7 @@ +# yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json +reviews: + auto_review: + enabled: true + # Review stacked PRs too, not only PRs that target the default branch. + base_branches: + - '.*' diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 002838c..c4b7dc0 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -2,8 +2,8 @@ name: Lint & Tests on: push: branches: [main] + # Every PR, including ones stacked on another PR's branch. pull_request: - branches: [main] jobs: lint: timeout-minutes: 10 @@ -57,7 +57,8 @@ jobs: e2e: timeout-minutes: 60 - runs-on: ubuntu-latest + # Playwright 1.60 can't install browsers on ubuntu-latest (26.04) yet. + runs-on: ubuntu-24.04 steps: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 diff --git a/packages/starpod/src/index.ts b/packages/starpod/src/index.ts index 047bad1..c555d9d 100644 --- a/packages/starpod/src/index.ts +++ b/packages/starpod/src/index.ts @@ -145,6 +145,7 @@ const ROUTES: Array<{ pattern: string; entrypoint: string }> = [ { pattern: '/robots.txt', entrypoint: 'robots.txt.ts' }, { pattern: '/api/contact', entrypoint: 'api/contact.ts' }, { pattern: '/api/episodes/[page].json', entrypoint: 'api/episodes/[page].json.ts' }, + { pattern: '/api/cues/[episode].json', entrypoint: 'api/cues/[episode].json.ts' }, { pattern: '/api/episodes/search.json', entrypoint: 'api/episodes/search.json.ts' diff --git a/packages/starpod/src/lib/cast.ts b/packages/starpod/src/lib/cast.ts new file mode 100644 index 0000000..41ce3c3 --- /dev/null +++ b/packages/starpod/src/lib/cast.ts @@ -0,0 +1,91 @@ +import type { TranscriptCue } from './cues'; + +/** A person on an episode, as recorded in the guests/sponsors database. */ +export interface CastPerson { + id: string; + img: string | null; + isHost: boolean; + name: string; +} + +/** + * The episode's people in credit order: this episode's hosts first (the + * show's configured hosts in config order, then guest hosts alphabetically), + * then guests alphabetically. Whether someone hosted is the episode's own + * record (`isHost`), so a regular host who joined one episode as a guest is + * credited as a guest there. + */ +export function sortCast( + people: Array, + configuredHosts: Array +): Array { + return [...people].sort((a, b) => { + if (a.isHost !== b.isHost) { + return a.isHost ? -1 : 1; + } + if (!a.isHost) { + return a.name.localeCompare(b.name); + } + const aIndex = configuredHosts.indexOf(a.name); + const bIndex = configuredHosts.indexOf(b.name); + if (aIndex !== -1 && bIndex !== -1) { + return aIndex - bIndex; + } + if (aIndex !== -1) { + return -1; + } + if (bIndex !== -1) { + return 1; + } + return a.name.localeCompare(b.name); + }); +} + +const tokens = (name: string) => + name + .toLowerCase() + .split(/[\s.]+/) + // Letters and digits in any script, so non-Latin names survive. + .map((token) => token.replace(/[^\p{L}\p{N}]/gu, '')) + .filter(Boolean); + +/** + * Do these two names refer to the same person? Loosely: their first names + * start the same way ("Robbie" ~ "RobbieTheWagner"), or they share a surname + * ("Chuck Carpenter" ~ "Charles William Carpenter III"). + */ +function sameName(a: string, b: string) { + const [aFirst = '', ...aRest] = tokens(a); + const [bFirst = '', ...bRest] = tokens(b); + if (aFirst.length >= 3 && bFirst.length >= 3 && (aFirst.startsWith(bFirst) || bFirst.startsWith(aFirst))) { + return true; + } + return aRest.some((token) => token.length >= 4 && bRest.includes(token)); +} + +/** + * Which transcript voice (0 or 1, the two most talkative speakers) a person + * speaks in, so their name on the stage wears the same typeface as their + * lines. People who don't speak as one of those two, or can't be matched, get + * no voice. + */ +export function voiceFor( + name: string, + cues: Array | null +): number | undefined { + if (!cues?.length) { + return undefined; + } + const speakers = [0, 1].map((voice) => ({ + voice, + speaker: cues.find((cue) => cue.v === voice && cue.n)?.n + })); + // An exact name wins over a loose one, whichever voice comes first. A name + // with nothing left after normalizing never matches. + const exact = tokens(name).join(' '); + const match = + (exact + ? speakers.find(({ speaker }) => speaker && tokens(speaker).join(' ') === exact) + : undefined) ?? speakers.find(({ speaker }) => speaker && sameName(name, speaker)); + return match?.voice; +} diff --git a/packages/starpod/src/lib/cues.ts b/packages/starpod/src/lib/cues.ts new file mode 100644 index 0000000..921f0b0 --- /dev/null +++ b/packages/starpod/src/lib/cues.ts @@ -0,0 +1,223 @@ +import { decodeEntities } from './entities'; +import type { Episode } from './rss'; +import { + fetchTranscriptText, + parseCues, + readJsonSegment, + type JsonSegment +} from './transcript'; + +/** + * A short, timed line of speech for the live transcript performance. `v` is + * the voice: the speaker's order of first appearance (0, 1, 2…), absent when + * the source carries no speaker names (plain captions). `n` is the name. + */ +export interface TranscriptCue { + s: number; + t: string; + v?: number; + n?: string; +} + +interface Segment { + start: number; + text: string; + speaker?: string; +} + +// Lines longer than this are split at sentence ends so the live line stays a +// phrase, not a paragraph. +const CUE_TARGET_CHARS = 140; + +export function mergeIntoLines(segments: Array): Array { + const voices = new Map(); + const lines: Array = []; + let current: TranscriptCue | null = null; + let speaker: string | undefined; + + for (const segment of segments) { + const endsSentence = current ? /[.!?]["”’)]?$/.test(current.t) : false; + // Never break mid-sentence unless a line runs far past the target; a + // transcript paragraph that was cut mid-thought joins the next one. + const startNew = + !current || + segment.speaker !== speaker || + (current.t.length >= CUE_TARGET_CHARS / 2 && endsSentence) || + current.t.length >= CUE_TARGET_CHARS * 1.6; + + if (startNew) { + speaker = segment.speaker; + current = { s: Math.round(segment.start * 10) / 10, t: segment.text }; + if (speaker) { + if (!voices.has(speaker)) { + voices.set(speaker, voices.size); + } + current.v = voices.get(speaker); + current.n = speaker; + } + lines.push(current); + } else if (current) { + current.t = `${current.t} ${segment.text}`; + } + } + + return rankVoices(lines); +} + +/** + * The two people who talk the most get the two signature voices (0 and 1, in + * order of first appearance); everyone else (intros, guests, ads) shares the + * supporting voices from 2 up. + */ +function rankVoices(lines: Array): Array { + const counts = new Map(); + const firstSeen: Array = []; + for (const line of lines) { + if (!line.n) { + continue; + } + if (!counts.has(line.n)) { + firstSeen.push(line.n); + } + counts.set(line.n, (counts.get(line.n) ?? 0) + 1); + } + const leads = [...counts.entries()] + .sort((a, b) => b[1] - a[1]) + .slice(0, 2) + .map(([name]) => name) + .sort((a, b) => firstSeen.indexOf(a) - firstSeen.indexOf(b)); + const supporting = firstSeen.filter((name) => !leads.includes(name)); + for (const line of lines) { + if (line.n) { + const lead = leads.indexOf(line.n); + line.v = lead !== -1 ? lead : 2 + (supporting.indexOf(line.n) % 4); + } + } + return lines; +} + +const MARKDOWN_LINE = + /^\s*\[(?:(\d+):)?(\d{1,2}):(\d{2})\]\s*(?:(?:\*\*|__)?([^:\n[\]*_]{1,40}):(?:\*\*|__)?\s*)?(.*)$/; + +function stripInlineMarkdown(text: string): string { + return text + .replace(/\[([^\]]+)\]\([^)]+\)/g, '$1') + .replace(/(\*\*|__|\*|_|`)/g, ''); +} + +/** + * Parse a markdown transcript (`[HH:MM:SS] Speaker: text` paragraphs) into + * timed lines with voices. Long speeches split into sentences that share the + * paragraph's start time, so they still perform one phrase at a time. + */ +export function parseMarkdownCues(markdown: string): Array { + const paragraphs: Array<{ start: number; speaker?: string; sentences: Array }> = []; + for (const block of markdown.replace(/\r\n?/g, '\n').split(/\n{2,}/)) { + const match = block.replace(/\n/g, ' ').match(MARKDOWN_LINE); + if (!match) { + continue; + } + const [, h, m, sec, name, text] = match; + const sentences = ( + stripInlineMarkdown(text).trim().match(/[^.!?]+[.!?]+["”’)]?|[^.!?]+$/g) ?? [] + ) + .map((sentence) => sentence.trim()) + .filter(Boolean); + paragraphs.push({ + start: Number(h ?? 0) * 3600 + Number(m) * 60 + Number(sec), + speaker: name?.trim(), + sentences + }); + } + + // Each sentence starts partway through its paragraph, in proportion to the + // text before it, so the live line moves at roughly the pace of speech. + const segments: Array = []; + paragraphs.forEach((paragraph, index) => { + const total = paragraph.sentences.reduce((sum, item) => sum + item.length, 0) || 1; + const next = paragraphs[index + 1]?.start; + const span = next && next > paragraph.start ? next - paragraph.start : total / 15; + let offset = 0; + for (const sentence of paragraph.sentences) { + segments.push({ + start: paragraph.start + (span * offset) / total, + text: sentence, + speaker: paragraph.speaker + }); + offset += sentence.length; + } + }); + return mergeIntoLines(segments); +} + +/** + * Timed segments from a JSON transcript (either field naming; see + * `readJsonSegment`). Segments without a time or text are dropped. + */ +export function jsonSegments(data: { segments?: Array } | null): Array { + return (data?.segments ?? []).flatMap((segment) => { + const { start, text, speaker } = readJsonSegment(segment); + return start !== undefined && text ? [{ start, text, speaker }] : []; + }); +} + +/** A VTT/SRT cue's text: voice and styling tags removed, references decoded. */ +export function cueText(raw: string): { text: string; speaker?: string } { + // WebVTT voice spans: text + const voice = raw.match(/^]*)?\s+([^>]+)>/); + return { + text: decodeEntities(raw.replace(/<[^>]+>/g, '')).trim(), + speaker: voice ? decodeEntities(voice[1]).trim() : undefined + }; +} + +/** + * Timed lines from the feed's `` (VTT/SRT cues or JSON + * segments). Returns `null` when none is available. + */ +const rssCueCache = new Map | null>>(); + +export function getRssCues( + episode: Episode +): Promise | null> { + const url = episode.transcriptUrl; + if (!url) { + return Promise.resolve(null); + } + let pending = rssCueCache.get(url); + if (!pending) { + pending = fetchRssCues(episode, url); + rssCueCache.set(url, pending); + // Forget misses so a failed download can be retried on a later request. + void pending.then((cues) => { + if (!cues) { + rssCueCache.delete(url); + } + }); + } + return pending; +} + +async function fetchRssCues( + episode: Episode, + url: string +): Promise | null> { + const download = await fetchTranscriptText(url); + if (!download) { + return null; + } + const type = (episode.transcriptType || download.type).toLowerCase(); + try { + if (type.includes('json')) { + const segments = jsonSegments(JSON.parse(download.raw)); + return segments.length ? mergeIntoLines(segments) : null; + } + const cues = parseCues(download.raw).map((cue) => ({ + start: cue.start, + ...cueText(cue.text) + })); + return cues.length ? mergeIntoLines(cues) : null; + } catch { + return null; + } +} diff --git a/packages/starpod/src/lib/entities.ts b/packages/starpod/src/lib/entities.ts new file mode 100644 index 0000000..38c0563 --- /dev/null +++ b/packages/starpod/src/lib/entities.ts @@ -0,0 +1,26 @@ +const NAMED: Record = { + amp: '&', + apos: "'", + gt: '>', + lt: '<', + nbsp: ' ', + quot: '"' +}; + +/** + * Decode character references (`&`, `&`, `&`, and the few named + * ones feeds and captions use) in one pass, so each reference in the original + * text is decoded exactly once: `&lt;` stays the literal `<`. + */ +export function decodeEntities(value: string): string { + return value.replace( + /&(?:#(\d+)|#x([\da-f]+)|(amp|apos|gt|lt|nbsp|quot));/gi, + (reference, decimal?: string, hex?: string, name?: string) => { + if (name) { + return NAMED[name.toLowerCase()]; + } + const codePoint = decimal ? Number(decimal) : parseInt(hex!, 16); + return codePoint <= 0x10ffff ? String.fromCodePoint(codePoint) : reference; + } + ); +} diff --git a/packages/starpod/src/lib/episode-cues.ts b/packages/starpod/src/lib/episode-cues.ts new file mode 100644 index 0000000..a2f5a6e --- /dev/null +++ b/packages/starpod/src/lib/episode-cues.ts @@ -0,0 +1,28 @@ +import { getEntry } from 'astro:content'; + +import { getRssCues, parseMarkdownCues, type TranscriptCue } from './cues'; +import type { Episode } from './rss'; + +/** + * Timed lines for an episode: a markdown transcript in the site's content + * collection wins (it names speakers), then the feed's transcript. Server-only. + */ +export async function getEpisodeCues( + episode: Episode +): Promise | null> { + if (episode.episodeNumber && episode.episodeNumber !== 'Bonus') { + let entry; + try { + entry = await getEntry('transcripts', episode.episodeNumber); + } catch { + entry = undefined; + } + if (entry?.body) { + const cues = parseMarkdownCues(entry.body); + if (cues.length) { + return cues; + } + } + } + return getRssCues(episode); +} diff --git a/packages/starpod/src/lib/feed-media.ts b/packages/starpod/src/lib/feed-media.ts new file mode 100644 index 0000000..2a03e82 --- /dev/null +++ b/packages/starpod/src/lib/feed-media.ts @@ -0,0 +1,101 @@ +/** + * Video discovery for feed items. + * + * `rss-to-json` drops nested Podcasting 2.0 elements such as + * ``, so the raw feed XML is scanned here instead. + * An item's video is its first alternate enclosure whose type is video or an + * HLS playlist; the `` inside it is the stream URL. + */ + +import { decodeEntities } from './entities'; + +export interface FeedVideo { + src: string; + type: string; +} + +const HLS_TYPES = ['application/x-mpegurl', 'application/vnd.apple.mpegurl']; +const FETCH_TIMEOUT_MS = 10_000; + +// Attribute names are fixed by this module, never taken from the feed. +const ATTRIBUTE_PATTERNS: Record<'type' | 'uri', RegExp> = { + type: /\btype\s*=\s*(["'])(.*?)\1/i, + uri: /\buri\s*=\s*(["'])(.*?)\1/i +}; + +function attribute(tag: string, name: 'type' | 'uri'): string | undefined { + const match = tag.match(ATTRIBUTE_PATTERNS[name]); + return match ? decodeEntities(match[2]) : undefined; +} + +/** An element's character data: CDATA sections as-is, the rest decoded. */ +function characterData(raw: string): string { + return raw + .split(/()/) + .map((part) => + part.startsWith(' { + const videos = new Map(); + + for (const item of xml.split(/]/i).slice(1)) { + const body = item.split(/<\/item>/i)[0]; + const guid = body.match(/]*>([\s\S]*?)<\/guid>/i)?.[1]; + if (!guid) { + continue; + } + + const enclosures = body.match( + //gi + ); + for (const enclosure of enclosures ?? []) { + const openTag = enclosure.match(/]*>/i)![0]; + const type = attribute(openTag, 'type') ?? ''; + if (!isVideoType(type)) { + continue; + } + const sourceTag = enclosure.match(/]*>/i)?.[0]; + const src = sourceTag && attribute(sourceTag, 'uri'); + if (src) { + videos.set(characterData(guid), { src, type }); + break; + } + } + } + + return videos; +} + +/** + * Fetch the feed and extract its videos. A failure here must never break the + * build: the site simply falls back to audio-first everywhere. + */ +export async function fetchFeedVideos( + feedUrl: string +): Promise> { + try { + // A deadline on the whole request, body included: a stalled response + // must not hold up the build, which then falls back to audio. + const response = await fetch(feedUrl, { + signal: AbortSignal.timeout(FETCH_TIMEOUT_MS) + }); + if (!response.ok) { + return new Map(); + } + return extractVideosByGuid(await response.text()); + } catch { + return new Map(); + } +} diff --git a/packages/starpod/src/lib/rss.ts b/packages/starpod/src/lib/rss.ts index 91670ce..ddeb1b1 100644 --- a/packages/starpod/src/lib/rss.ts +++ b/packages/starpod/src/lib/rss.ts @@ -10,6 +10,7 @@ import { union } from 'valibot'; +import { fetchFeedVideos, type FeedVideo } from './feed-media'; import { optimizeImage } from './optimize-episode-image'; import type { StarpodConfig } from '../utils/config'; import { dasherize } from '../utils/dasherize'; @@ -60,6 +61,10 @@ export interface Episode { src: string; type: string; }; + // The episode's video stream, from a `` with a + // video or HLS type. When present the site leads with video and offers a + // switch to audio. + video?: FeedVideo; // A transcript referenced by the feed's `` tag, if any. // Used as a fallback when no explicit markdown transcript is provided for the // episode. @@ -146,7 +151,11 @@ export async function getAllEpisodes(config?: StarpodConfig) { ) }); - let feed = (await parseRssFeed(await resolveRssFeed(config))) as Show; + const feedUrl = await resolveRssFeed(config); + const [feed, videos] = await Promise.all([ + parseRssFeed(feedUrl) as Promise, + fetchFeedVideos(feedUrl) + ]); let items = parse(FeedSchema, feed).items; let episodes: Array = await Promise.all( @@ -192,7 +201,8 @@ export async function getAllEpisodes(config?: StarpodConfig) { audio: { src: audioEnclosure.url, type: audioEnclosure.type ?? 'audio/mpeg' - } + }, + video: videos.get(id) }; } ) diff --git a/packages/starpod/src/lib/transcript.ts b/packages/starpod/src/lib/transcript.ts index ce3d36f..db4df78 100644 --- a/packages/starpod/src/lib/transcript.ts +++ b/packages/starpod/src/lib/transcript.ts @@ -12,13 +12,79 @@ export interface TranscriptParagraph { text: string; } +/** A JSON transcript segment, in either field naming. */ +export interface JsonSegment { + body?: string; + speaker?: string; + start?: number; + startTime?: number; + text?: string; +} + interface JsonTranscript { text?: string; - segments?: Array<{ text?: string; start?: number }>; + segments?: Array; +} + +/** + * A JSON segment's time, text, and speaker. The Podcast Namespace format names + * them `startTime`, `body`, and `speaker`; `start` and `text` are accepted too. + */ +export function readJsonSegment(segment: JsonSegment): { + start?: number; + text?: string; + speaker?: string; +} { + const start = segment.startTime ?? segment.start; + return { + start: typeof start === 'number' ? start : undefined, + text: (segment.body ?? segment.text)?.trim() || undefined, + speaker: segment.speaker || undefined + }; +} + +const FETCH_TIMEOUT_MS = 10_000; +const downloads = new Map>(); + +/** + * Download a transcript once per build, shared by the transcript on the page, + * its `.html.md` twin, and the live transcript's timed lines. The deadline + * covers the whole request, body included, so a stalled server can't hold up + * the build. Failures are not remembered, so a later request can retry. + */ +export function fetchTranscriptText( + url: string +): Promise<{ raw: string; type: string } | null> { + let pending = downloads.get(url); + if (!pending) { + pending = (async () => { + try { + const response = await fetch(url, { + signal: AbortSignal.timeout(FETCH_TIMEOUT_MS) + }); + if (!response.ok) { + return null; + } + return { + raw: await response.text(), + type: response.headers.get('content-type') ?? '' + }; + } catch { + return null; + } + })(); + downloads.set(url, pending); + void pending.then((result) => { + if (!result) { + downloads.delete(url); + } + }); + } + return pending; } // Cache parsed transcripts by URL so the episode page and its `.html.md` -// counterpart don't fetch the same transcript twice during a build. +// counterpart don't parse the same transcript twice during a build. const cache = new Map | null>(); /** @@ -40,20 +106,18 @@ export async function getRssTranscriptParagraphs( return cached; } - let paragraphs: Array | null = null; - + const download = await fetchTranscriptText(url); + if (!download) { + // Not cached: the download may succeed on a later request. + return null; + } + let paragraphs: Array | null; try { - const response = await fetch(url); - if (response.ok) { - const raw = await response.text(); - const type = - episode.transcriptType || response.headers.get('content-type') || ''; - paragraphs = parseTranscript(raw, type); - } + paragraphs = parseTranscript(download.raw, episode.transcriptType || download.type); } catch { + // Downloaded but unusable (e.g. a JSON body of `null`): no transcript. paragraphs = null; } - cache.set(url, paragraphs); return paragraphs; } @@ -89,9 +153,9 @@ function parseTranscript( if (Array.isArray(data.segments) && data.segments.length > 0) { const segments: Array<{ text: string; start?: number }> = []; for (const segment of data.segments) { - const text = segment.text?.trim(); + const { start, text } = readJsonSegment(segment); if (text) { - segments.push({ text, start: segment.start }); + segments.push({ text, start }); } } if (segments.length > 0) { @@ -124,7 +188,9 @@ function parseTranscript( * start time (in seconds) of each cue. Blocks without a `-->` timing line * (e.g. the `WEBVTT` header) are skipped. */ -function parseCues(raw: string): Array<{ text: string; start: number }> { +export function parseCues( + raw: string +): Array<{ text: string; start: number }> { const cues: Array<{ text: string; start: number }> = []; for (const block of raw.replace(/\r\n/g, '\n').split(/\n{2,}/)) { diff --git a/packages/starpod/src/pages/api/cues/[episode].json.ts b/packages/starpod/src/pages/api/cues/[episode].json.ts new file mode 100644 index 0000000..1ccac23 --- /dev/null +++ b/packages/starpod/src/pages/api/cues/[episode].json.ts @@ -0,0 +1,23 @@ +import type { APIRoute, GetStaticPaths } from 'astro'; + +import { getEpisodeCues } from '../../../lib/episode-cues'; +import { getAllEpisodes, type Episode } from '../../../lib/rss'; + +/** + * Timed transcript lines for one episode, fetched lazily by the stage's + * live transcript. Episodes without a transcript return an empty list. + */ +export const getStaticPaths: GetStaticPaths = async () => { + const episodes = await getAllEpisodes(); + return episodes.map((episode) => ({ + params: { episode: episode.episodeSlug }, + props: { episode } + })); +}; + +export const GET: APIRoute = async ({ props }) => { + const cues = await getEpisodeCues(props.episode as Episode); + return new Response(JSON.stringify(cues ?? []), { + headers: { 'Content-Type': 'application/json; charset=utf-8' } + }); +}; diff --git a/tests/unit/cast.test.ts b/tests/unit/cast.test.ts new file mode 100644 index 0000000..8add384 --- /dev/null +++ b/tests/unit/cast.test.ts @@ -0,0 +1,85 @@ +import { describe, expect, it } from 'vitest'; +import { sortCast, voiceFor } from 'starpod/src/lib/cast'; + +const person = (name: string, isHost: boolean) => ({ id: name, img: null, isHost, name }); + +describe('sortCast', () => { + const hosts = ['RobbieTheWagner', 'Chuck Carpenter', 'Adam Argyle']; + + it('credits this episode’s hosts first (configured order, then guest hosts), then guests', () => { + const sorted = sortCast( + [ + person('Zed Guest', false), + person('Adam Argyle', true), + person('Nick Taylor', true), + person('Amy Guest', false), + person('RobbieTheWagner', true) + ], + hosts + ); + expect(sorted.map((p) => p.name)).toEqual([ + 'RobbieTheWagner', + 'Adam Argyle', + 'Nick Taylor', + 'Amy Guest', + 'Zed Guest' + ]); + }); + + it('credits a regular host who appeared as a guest among the guests', () => { + const sorted = sortCast( + [person('Adam Argyle', false), person('Chuck Carpenter', true), person('RobbieTheWagner', true)], + hosts + ); + expect(sorted.map((p) => `${p.name}${p.isHost ? '' : ' (guest)'}`)).toEqual([ + 'RobbieTheWagner', + 'Chuck Carpenter', + 'Adam Argyle (guest)' + ]); + }); +}); + +describe('voiceFor', () => { + const cues = [ + { s: 0, t: 'Hi', v: 0, n: 'Robbie Wagner' }, + { s: 5, t: 'Hey', v: 1, n: 'Adam Argyle' }, + { s: 9, t: 'Hello', v: 2, n: 'Denver Cramer' } + ]; + + it('matches people to the voice they speak in, loosely by first name', () => { + expect(voiceFor('RobbieTheWagner', cues)).toBe(0); + expect(voiceFor('Adam Argyle', cues)).toBe(1); + }); + + it('matches a shared surname when first names differ', () => { + const chuck = [ + { s: 0, t: 'Hi', v: 0, n: 'Chuck Carpenter' }, + { s: 5, t: 'Hey', v: 1, n: 'Adam Argyle' } + ]; + expect(voiceFor('Charles William Carpenter III', chuck)).toBe(0); + expect(voiceFor('Robbie Wagner', chuck)).toBeUndefined(); + }); + + it('prefers an exact name over a loose first-name match', () => { + const adams = [ + { s: 0, t: 'Hi', v: 0, n: 'Adam Smith' }, + { s: 5, t: 'Hey', v: 1, n: 'Adam Argyle' } + ]; + expect(voiceFor('Adam Argyle', adams)).toBe(1); + expect(voiceFor('Adam Smith', adams)).toBe(0); + }); + + it('matches non-Latin names exactly and never pairs two different ones', () => { + const names = [ + { s: 0, t: '你好', v: 0, n: '李明' }, + { s: 5, t: '你好', v: 1, n: '张强' } + ]; + expect(voiceFor('张强', names)).toBe(1); + expect(voiceFor('王伟', names)).toBeUndefined(); + }); + + it('gives no voice to guests outside the two main voices, or without a transcript', () => { + expect(voiceFor('Denver Cramer', cues)).toBeUndefined(); + expect(voiceFor('Robbie Wagner', null)).toBeUndefined(); + }); +}); diff --git a/tests/unit/cues.test.ts b/tests/unit/cues.test.ts new file mode 100644 index 0000000..dfc9b50 --- /dev/null +++ b/tests/unit/cues.test.ts @@ -0,0 +1,94 @@ +import { describe, expect, it } from 'vitest'; +import { + cueText, + jsonSegments, + mergeIntoLines, + parseMarkdownCues +} from 'starpod/src/lib/cues'; + +describe('parseMarkdownCues', () => { + const markdown = [ + '[00:00:00] **Intro:** Welcome to the show. Pour a glass.', + '[00:00:10] **Robbie Wagner:** Hey everybody. Good to be here.', + '[00:00:20] **Adam Argyle:** Hi. I brought *the* whiskey.', + '[00:00:30] **Robbie Wagner:** Great.', + '[00:00:40] **Adam Argyle:** Cheers.' + ].join('\n\n'); + const cues = parseMarkdownCues(markdown); + + it('reads Windows (CRLF) line endings the same way', () => { + expect(parseMarkdownCues(markdown.replace(/\n/g, '\r\n'))).toEqual(cues); + }); + + it('reads bold speaker names and strips inline markdown', () => { + expect(cues[0].n).toBe('Intro'); + expect(cues.find((cue) => cue.t.includes('brought'))?.t).toBe( + 'Hi. I brought the whiskey.' + ); + }); + + it('gives the two people who talk most the two signature voices', () => { + const voiceOf = (name: string) => cues.find((cue) => cue.n === name)?.v; + expect(voiceOf('Robbie Wagner')).toBe(0); + expect(voiceOf('Adam Argyle')).toBe(1); + expect(voiceOf('Intro')).toBe(2); + }); + + it('spreads sentences across their paragraph instead of stacking them', () => { + const starts = cues.map((cue) => cue.s); + expect(new Set(starts).size).toBe(starts.length); + expect(starts).toEqual([...starts].sort((a, b) => a - b)); + }); +}); + +describe('mergeIntoLines', () => { + it('leaves voice unset for captions without speakers', () => { + const lines = mergeIntoLines([ + { start: 0, text: 'Hello there.' }, + { start: 2, text: 'General Kenobi.' } + ]); + expect(lines.every((line) => line.v === undefined)).toBe(true); + }); + + it('starts a new line when the speaker changes', () => { + const lines = mergeIntoLines([ + { start: 0, text: 'One', speaker: 'A' }, + { start: 1, text: 'two', speaker: 'A' }, + { start: 2, text: 'Three', speaker: 'B' } + ]); + expect(lines.map((line) => line.t)).toEqual(['One two', 'Three']); + }); +}); + +describe('jsonSegments', () => { + it('reads the Podcast Namespace startTime/body fields', () => { + expect( + jsonSegments({ segments: [{ startTime: 0.5, body: ' Hello ', speaker: 'Alice' }] }) + ).toEqual([{ start: 0.5, text: 'Hello', speaker: 'Alice' }]); + }); + + it('also reads start/text, and drops segments without a time or text', () => { + expect( + jsonSegments({ + segments: [ + { start: 2, text: 'Hi' }, + { start: 3, text: ' ' }, + { text: 'No time' } + ] + }) + ).toEqual([{ start: 2, text: 'Hi', speaker: undefined }]); + }); +}); + +describe('cueText', () => { + it('takes the speaker from a voice span and decodes references once', () => { + expect(cueText('Ship it & see &lt;3')).toEqual({ + text: 'Ship it & see <3', + speaker: 'Robbie Wagner' + }); + }); + + it('strips styling tags from captions without a speaker', () => { + expect(cueText('Hello there')).toEqual({ text: 'Hello there', speaker: undefined }); + }); +}); diff --git a/tests/unit/feed-media.test.ts b/tests/unit/feed-media.test.ts new file mode 100644 index 0000000..4b42768 --- /dev/null +++ b/tests/unit/feed-media.test.ts @@ -0,0 +1,57 @@ +import { describe, expect, it } from 'vitest'; +import { extractVideosByGuid, isVideoType } from 'starpod/src/lib/feed-media'; + +const feed = ` + +With videoflightcast:AAA + + + +Audio onlyflightcast:BBB + + +Quoting + + +Audio alternate firstCCC + + + +`; + +describe('extractVideosByGuid', () => { + const videos = extractVideosByGuid(feed); + + it('finds an HLS alternate enclosure and decodes its URL', () => { + expect(videos.get('flightcast:AAA')).toEqual({ + src: 'https://example.com/v/a.m3u8?x=1&y=2', + type: 'application/x-mpegURL' + }); + }); + + it('reads CDATA guids, single-quoted attributes, and each reference once', () => { + expect(videos.get('episode-A')).toEqual({ + src: 'https://example.com/q.mp4?a=1&b=<2', + type: 'video/mp4' + }); + }); + + it('leaves audio-only items out', () => { + expect(videos.has('flightcast:BBB')).toBe(false); + }); + + it('skips non-video alternates and takes the first video one', () => { + expect(videos.get('CCC')).toEqual({ + src: 'https://example.com/c.mp4', + type: 'video/mp4' + }); + }); +}); + +describe('isVideoType', () => { + it('accepts video and HLS types only', () => { + expect(isVideoType('video/mp4')).toBe(true); + expect(isVideoType('application/vnd.apple.mpegurl')).toBe(true); + expect(isVideoType('audio/mpeg')).toBe(false); + }); +}); diff --git a/tests/unit/transcript.test.ts b/tests/unit/transcript.test.ts index 413f767..4481490 100644 --- a/tests/unit/transcript.test.ts +++ b/tests/unit/transcript.test.ts @@ -4,6 +4,7 @@ import { getRssTranscriptParagraphs, getRssTranscriptText } from 'starpod/src/lib/transcript'; +import { getRssCues } from 'starpod/src/lib/cues'; import type { Episode } from 'starpod/src/lib/rss'; // Each test uses a unique transcript URL because the module caches parsed @@ -239,3 +240,44 @@ describe('getRssTranscriptText', () => { expect(await getRssTranscriptText(episode)).toBeNull(); }); }); + +describe('shared transcript download', () => { + it('reads the Podcast Namespace startTime/body fields', async () => { + mockFetch({ + body: JSON.stringify({ + segments: [{ startTime: 1.5, body: 'Hello from the spec.', speaker: 'Alice' }] + }) + }); + + expect(await getRssTranscriptParagraphs(makeEpisode())).toEqual([ + { start: 1.5, text: 'Hello from the spec.' } + ]); + }); + + it('downloads once for both the page transcript and the live lines', async () => { + const fetchMock = mockFetch({ + body: JSON.stringify({ segments: [{ start: 0, text: 'Hi there.', speaker: 'Al' }] }) + }); + const episode = makeEpisode(); + + await getRssTranscriptParagraphs(episode); + expect(await getRssCues(episode)).toEqual([{ s: 0, t: 'Hi there.', v: 0, n: 'Al' }]); + expect(fetchMock).toHaveBeenCalledTimes(1); + }); + + it('treats a downloaded but unusable JSON body as no transcript', async () => { + mockFetch({ body: 'null' }); + expect(await getRssTranscriptParagraphs(makeEpisode())).toBeNull(); + }); + + it('retries a transcript whose download failed', async () => { + const episode = makeEpisode(); + mockFetch({ ok: false }); + expect(await getRssTranscriptParagraphs(episode)).toBeNull(); + + mockFetch({ body: JSON.stringify({ segments: [{ start: 2, text: 'Back again.' }] }) }); + expect(await getRssTranscriptParagraphs(episode)).toEqual([ + { start: 2, text: 'Back again.' } + ]); + }); +});