|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { formatTranscriptAsVTT } from "@/app/s/[videoId]/_components/utils/transcript-utils"; |
| 3 | +import { |
| 4 | + createEditTranscript, |
| 5 | + editTranscriptWordsToCaptionVtt, |
| 6 | + groupEditTranscriptWords, |
| 7 | + parseEditTranscript, |
| 8 | + remapEditTranscriptThroughSpec, |
| 9 | + serializeEditTranscript, |
| 10 | +} from "@/lib/edit-transcript"; |
| 11 | +import { formatTranscriptAsParagraphs } from "@/lib/transcript-text"; |
| 12 | +import { |
| 13 | + formatVttCueText, |
| 14 | + parseVTT, |
| 15 | + parseVttCueText, |
| 16 | + updateVttEntryText, |
| 17 | +} from "@/lib/transcript-vtt"; |
| 18 | + |
| 19 | +const transcript = createEditTranscript( |
| 20 | + { |
| 21 | + words: [ |
| 22 | + { text: "Hello", start: 100, end: 300, speaker: "A" }, |
| 23 | + { text: "there", start: 300, end: 600, speaker: "A" }, |
| 24 | + { text: "Hi", start: 650, end: 800, speaker: "B" }, |
| 25 | + { text: "again", start: 850, end: 1000, speaker: "A" }, |
| 26 | + { text: "unknown", start: 1100, end: 1300, speaker: null }, |
| 27 | + ], |
| 28 | + }, |
| 29 | + 2000, |
| 30 | +); |
| 31 | + |
| 32 | +describe("speaker diarization", () => { |
| 33 | + it("splits captions on every speaker transition without needing punctuation or silence", () => { |
| 34 | + const cues = parseVTT(editTranscriptWordsToCaptionVtt(transcript.words)); |
| 35 | + expect( |
| 36 | + cues.map(({ text, speaker, startTime, endTime }) => ({ |
| 37 | + text, |
| 38 | + speaker, |
| 39 | + startTime, |
| 40 | + endTime, |
| 41 | + })), |
| 42 | + ).toEqual([ |
| 43 | + { text: "Hello there", speaker: "A", startTime: 0.1, endTime: 0.6 }, |
| 44 | + { text: "Hi", speaker: "B", startTime: 0.65, endTime: 0.8 }, |
| 45 | + { text: "again", speaker: "A", startTime: 0.85, endTime: 1 }, |
| 46 | + { text: "unknown", speaker: null, startTime: 1.1, endTime: 1.3 }, |
| 47 | + ]); |
| 48 | + }); |
| 49 | + |
| 50 | + it("preserves labels through storage, video cuts, caption regeneration, and download", () => { |
| 51 | + const stored = parseEditTranscript(serializeEditTranscript(transcript)); |
| 52 | + expect(stored).not.toBeNull(); |
| 53 | + if (!stored) throw new Error("Missing transcript"); |
| 54 | + const edited = remapEditTranscriptThroughSpec(stored, { |
| 55 | + version: 1, |
| 56 | + sourceDuration: 2, |
| 57 | + keepRanges: [{ start: 0.6, end: 2 }], |
| 58 | + }); |
| 59 | + const cues = parseVTT(editTranscriptWordsToCaptionVtt(edited.words)); |
| 60 | + expect(cues[0]).toMatchObject({ |
| 61 | + text: "Hi", |
| 62 | + speaker: "B", |
| 63 | + startTime: 0.05, |
| 64 | + }); |
| 65 | + expect(parseVTT(formatTranscriptAsVTT(cues))).toEqual(cues); |
| 66 | + }); |
| 67 | + |
| 68 | + it("shows separate editor groups and text paragraphs for speakers and unknown speech", () => { |
| 69 | + expect( |
| 70 | + groupEditTranscriptWords(transcript.words).map( |
| 71 | + ({ startIndex, endIndex }) => [startIndex, endIndex], |
| 72 | + ), |
| 73 | + ).toEqual([ |
| 74 | + [0, 1], |
| 75 | + [2, 2], |
| 76 | + [3, 3], |
| 77 | + [4, 4], |
| 78 | + ]); |
| 79 | + expect( |
| 80 | + formatTranscriptAsParagraphs( |
| 81 | + parseVTT(editTranscriptWordsToCaptionVtt(transcript.words)), |
| 82 | + ), |
| 83 | + ).toBe( |
| 84 | + "Speaker A: Hello there\n\nSpeaker B: Hi\n\nSpeaker A: again\n\nunknown", |
| 85 | + ); |
| 86 | + }); |
| 87 | + |
| 88 | + it("preserves the voice when editing spoken text and escapes markup", () => { |
| 89 | + const vtt = editTranscriptWordsToCaptionVtt(transcript.words); |
| 90 | + const updated = updateVttEntryText(vtt, 2, "Yes <script> & no"); |
| 91 | + expect(updated.updated).toBe(true); |
| 92 | + expect(updated.content).toContain( |
| 93 | + "<v Speaker B>Yes <script> & no</v>", |
| 94 | + ); |
| 95 | + expect(parseVTT(updated.content)[1]).toMatchObject({ |
| 96 | + speaker: "B", |
| 97 | + text: "Yes <script> & no", |
| 98 | + startTime: 0.65, |
| 99 | + endTime: 0.8, |
| 100 | + }); |
| 101 | + }); |
| 102 | + |
| 103 | + it("handles multiline voice cues, legacy plain cues, and escaped labels", () => { |
| 104 | + const cues = parseVTT( |
| 105 | + "WEBVTT\r\n\r\n1\r\n00:00:00.125 --> 00:00:01.500\r\n<v Speaker A>Hello\r\nthere</v>\r\n\r\n2\r\n00:00:01.500 --> 00:00:02.000\r\nLegacy text\r\n", |
| 106 | + ); |
| 107 | + expect(cues[0]).toMatchObject({ |
| 108 | + text: "Hello there", |
| 109 | + speaker: "A", |
| 110 | + startTime: 0.125, |
| 111 | + endTime: 1.5, |
| 112 | + }); |
| 113 | + expect(cues[1]).toMatchObject({ text: "Legacy text", speaker: null }); |
| 114 | + expect(parseVttCueText(formatVttCueText("2 < 3 & 4 > 1", "A & B"))).toEqual( |
| 115 | + { text: "2 < 3 & 4 > 1", speaker: "A & B" }, |
| 116 | + ); |
| 117 | + }); |
| 118 | +}); |
| 119 | + |
| 120 | +it("keeps speaker metadata and literal text through the agent transcript API", async () => { |
| 121 | + const { parseAgentVtt, renderAgentVtt } = await import("@/lib/agent-api"); |
| 122 | + const cues = [ |
| 123 | + { startMs: 125, endMs: 500, text: "R&D < planning", speaker: "B" }, |
| 124 | + ]; |
| 125 | + expect(parseAgentVtt(renderAgentVtt(cues))).toEqual(cues); |
| 126 | +}); |
0 commit comments