|
| 1 | +import { expect, test } from 'vitest' |
| 2 | +import { |
| 3 | + chunkTranscriptEvents, |
| 4 | + type TranscriptEvent, |
| 5 | +} from '../youtube-transcript-chunking.ts' |
| 6 | + |
| 7 | +test('chunkTranscriptEvents merges tiny trailing chunks into previous chunk', () => { |
| 8 | + const events: Array<TranscriptEvent> = [ |
| 9 | + { startMs: 0, durationMs: 1000, text: 'A'.repeat(40) }, |
| 10 | + { startMs: 1200, durationMs: 600, text: 'tail' }, |
| 11 | + ] |
| 12 | + |
| 13 | + const chunks = chunkTranscriptEvents(events, { |
| 14 | + targetChars: 40, |
| 15 | + maxChunkChars: 80, |
| 16 | + minChunkChars: 20, |
| 17 | + }) |
| 18 | + |
| 19 | + expect(chunks).toHaveLength(1) |
| 20 | + expect(chunks[0]?.body).toContain('A'.repeat(40)) |
| 21 | + expect(chunks[0]?.body).toContain('tail') |
| 22 | + expect(chunks[0]?.startMs).toBe(0) |
| 23 | + expect(chunks[0]?.endMs).toBe(1800) |
| 24 | +}) |
| 25 | + |
| 26 | +test('chunkTranscriptEvents keeps a tiny chunk when it is the only chunk', () => { |
| 27 | + const events: Array<TranscriptEvent> = [ |
| 28 | + { startMs: 0, durationMs: 400, text: 'short' }, |
| 29 | + ] |
| 30 | + |
| 31 | + const chunks = chunkTranscriptEvents(events, { |
| 32 | + targetChars: 40, |
| 33 | + maxChunkChars: 80, |
| 34 | + minChunkChars: 20, |
| 35 | + }) |
| 36 | + |
| 37 | + expect(chunks).toHaveLength(1) |
| 38 | + expect(chunks[0]?.body).toBe('short') |
| 39 | +}) |
| 40 | + |
| 41 | +test('chunkTranscriptEvents keeps trailing chunks that meet minimum size', () => { |
| 42 | + const events: Array<TranscriptEvent> = [ |
| 43 | + { startMs: 0, durationMs: 800, text: 'A'.repeat(40) }, |
| 44 | + { startMs: 1000, durationMs: 700, text: 'B'.repeat(25) }, |
| 45 | + ] |
| 46 | + |
| 47 | + const chunks = chunkTranscriptEvents(events, { |
| 48 | + targetChars: 40, |
| 49 | + maxChunkChars: 80, |
| 50 | + minChunkChars: 20, |
| 51 | + }) |
| 52 | + |
| 53 | + expect(chunks).toHaveLength(2) |
| 54 | + expect(chunks[0]?.body).toBe('A'.repeat(40)) |
| 55 | + expect(chunks[1]?.body).toBe('B'.repeat(25)) |
| 56 | +}) |
| 57 | + |
| 58 | +test('chunkTranscriptEvents re-checks oversized lines after flush', () => { |
| 59 | + const events: Array<TranscriptEvent> = [ |
| 60 | + { startMs: 0, durationMs: 800, text: 'A'.repeat(40) }, |
| 61 | + { startMs: 1000, durationMs: 700, text: 'B'.repeat(120) }, |
| 62 | + ] |
| 63 | + |
| 64 | + const chunks = chunkTranscriptEvents(events, { |
| 65 | + targetChars: 50, |
| 66 | + maxChunkChars: 80, |
| 67 | + minChunkChars: 0, |
| 68 | + }) |
| 69 | + |
| 70 | + expect(chunks).toHaveLength(4) |
| 71 | + expect(chunks[0]?.body).toBe('A'.repeat(40)) |
| 72 | + expect(chunks.slice(1).every((chunk) => (chunk.body.length || 0) <= 50)).toBe( |
| 73 | + true, |
| 74 | + ) |
| 75 | +}) |
| 76 | + |
| 77 | +test('chunkTranscriptEvents guards non-positive targetChars', () => { |
| 78 | + const events: Array<TranscriptEvent> = [ |
| 79 | + { startMs: 0, durationMs: 500, text: 'A'.repeat(12) }, |
| 80 | + ] |
| 81 | + |
| 82 | + const chunks = chunkTranscriptEvents(events, { |
| 83 | + targetChars: 0, |
| 84 | + maxChunkChars: 8, |
| 85 | + minChunkChars: 0, |
| 86 | + }) |
| 87 | + |
| 88 | + expect(chunks.length).toBeGreaterThan(0) |
| 89 | + expect(chunks.every((chunk) => chunk.body.length >= 1)).toBe(true) |
| 90 | +}) |
| 91 | + |
| 92 | +test('chunkTranscriptEvents keeps event end at or after start for negative durations', () => { |
| 93 | + const events: Array<TranscriptEvent> = [ |
| 94 | + { startMs: 1000, durationMs: -900, text: 'negative duration' }, |
| 95 | + ] |
| 96 | + |
| 97 | + const chunks = chunkTranscriptEvents(events, { |
| 98 | + targetChars: 40, |
| 99 | + maxChunkChars: 80, |
| 100 | + minChunkChars: 0, |
| 101 | + }) |
| 102 | + |
| 103 | + expect(chunks).toHaveLength(1) |
| 104 | + expect(chunks[0]?.startMs).toBe(1000) |
| 105 | + expect(chunks[0]?.endMs).toBe(1000) |
| 106 | +}) |
0 commit comments