|
7 | 7 |
|
8 | 8 | import { describe, expect, it } from 'vitest'; |
9 | 9 | import { Transcript, Word, WordGroup } from '../models'; |
| 10 | +import { setFetchMockReturnValue } from '../../vitest.mocks'; |
10 | 11 |
|
11 | 12 | describe('Transcript tests', () => { |
12 | 13 | it('the word should calculate the duration correctly', () => { |
@@ -211,4 +212,85 @@ describe('Transcript tests', () => { |
211 | 212 | expect(subset.groups.at(0)?.words.at(-1)?.start.seconds).toBe(10); |
212 | 213 | expect(subset.groups.at(0)?.words.at(-1)?.stop.seconds).toBe(13); |
213 | 214 | }); |
| 215 | + |
| 216 | + it('should optimize the word timestamps', () => { |
| 217 | + const transcript = new Transcript([ |
| 218 | + new WordGroup([new Word('Lorem', 0, 12), new Word('Ipsum', 15, 21)]), |
| 219 | + new WordGroup([new Word('is', 18, 27)]), |
| 220 | + ]); |
| 221 | + |
| 222 | + transcript.optimize(); |
| 223 | + |
| 224 | + expect(transcript.groups.length).toBe(2); |
| 225 | + expect(transcript.groups[0].words[0].start.millis).toBe(0); |
| 226 | + expect(transcript.groups[0].words[0].stop.millis).toBe(14); |
| 227 | + |
| 228 | + expect(transcript.groups[0].words[1].start.millis).toBe(15); |
| 229 | + expect(transcript.groups[0].words[1].stop.millis).toBe(21); |
| 230 | + |
| 231 | + expect(transcript.groups[1].words[0].start.millis).toBe(22); |
| 232 | + expect(transcript.groups[1].words[0].stop.millis).toBe(27); |
| 233 | + }); |
| 234 | + |
| 235 | + it('should be converatble to an srt', () => { |
| 236 | + const transcript = new Transcript([ |
| 237 | + new WordGroup([new Word('Lorem', 0, 1e3), new Word('Ipsum', 2e3, 5e3)]), |
| 238 | + new WordGroup([new Word('is', 7e3, 8e3)]), |
| 239 | + ]); |
| 240 | + |
| 241 | + const { text, blob } = transcript.toSRT({ count: [2] }); |
| 242 | + |
| 243 | + expect(text).toContain(`1 |
| 244 | +00:00:00,000 --> 00:00:05,000 |
| 245 | +Lorem Ipsum` |
| 246 | + ); |
| 247 | + |
| 248 | + expect(text).toContain(`2 |
| 249 | +00:00:07,000 --> 00:00:08,000 |
| 250 | +is` |
| 251 | + ); |
| 252 | + |
| 253 | + expect(blob.type).toBe('text/plain;charset=utf8'); |
| 254 | + }); |
| 255 | + |
| 256 | + it('should be able to instantiate from an url', async () => { |
| 257 | + const resetFetch = setFetchMockReturnValue({ |
| 258 | + ok: true, |
| 259 | + json: async () => ([ |
| 260 | + [ |
| 261 | + { token: 'Lorem', start: 0, stop: 12 }, |
| 262 | + { token: 'Ipsum', start: 15, stop: 20 }, |
| 263 | + ], |
| 264 | + [ |
| 265 | + { token: 'is', start: 21, stop: 38 }, |
| 266 | + ] |
| 267 | + ]), |
| 268 | + }); |
| 269 | + |
| 270 | + const transcript = await Transcript.from('http://diffusion.mov/caption.json'); |
| 271 | + |
| 272 | + |
| 273 | + expect(transcript.groups.length).toBe(2); |
| 274 | + expect(transcript.groups[0].words[0].start.millis).toBe(0); |
| 275 | + expect(transcript.groups[0].words[0].stop.millis).toBe(12); |
| 276 | + expect(transcript.groups[0].words[0].text).toBe('Lorem'); |
| 277 | + |
| 278 | + expect(transcript.groups[0].words[1].start.millis).toBe(15); |
| 279 | + expect(transcript.groups[0].words[1].stop.millis).toBe(20); |
| 280 | + expect(transcript.groups[0].words[1].text).toBe('Ipsum'); |
| 281 | + |
| 282 | + expect(transcript.groups[1].words[0].start.millis).toBe(21); |
| 283 | + expect(transcript.groups[1].words[0].stop.millis).toBe(38); |
| 284 | + expect(transcript.groups[1].words[0].text).toBe('is'); |
| 285 | + |
| 286 | + resetFetch(); |
| 287 | + }); |
| 288 | + |
| 289 | + it('should not be able to instantiate when the response is not ok', async () => { |
| 290 | + const resetFetch = setFetchMockReturnValue({ ok: false, }); |
| 291 | + |
| 292 | + await expect(() => Transcript.from('http://diffusion.mov/caption.json')).rejects.toThrowError(); |
| 293 | + |
| 294 | + resetFetch(); |
| 295 | + }); |
214 | 296 | }); |
0 commit comments