Skip to content

Commit 676d545

Browse files
committed
added model unit tests
1 parent df5c2e7 commit 676d545

File tree

3 files changed

+87
-1
lines changed

3 files changed

+87
-1
lines changed

src/models/animation-builder.spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,8 @@ describe('The Animation Builder', () => {
118118
expect(width.input[0]).toBe(12 / 30 * 1000);
119119
expect(width.output[0]).toBe(20);
120120
});
121+
122+
it("should not create an animation if no property has been called first", () => {
123+
expect(() => animate.to(20, 12)).toThrowError();
124+
});
121125
});

src/models/transcript.spec.ts

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import { describe, expect, it } from 'vitest';
99
import { Transcript, Word, WordGroup } from '../models';
10+
import { setFetchMockReturnValue } from '../../vitest.mocks';
1011

1112
describe('Transcript tests', () => {
1213
it('the word should calculate the duration correctly', () => {
@@ -211,4 +212,85 @@ describe('Transcript tests', () => {
211212
expect(subset.groups.at(0)?.words.at(-1)?.start.seconds).toBe(10);
212213
expect(subset.groups.at(0)?.words.at(-1)?.stop.seconds).toBe(13);
213214
});
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+
});
214296
});

src/models/transcript.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export class Transcript implements Serializer {
8888
* Convert the transcript into a SRT compatible
8989
* string and downloadable blob
9090
*/
91-
public toSRT(options: GeneratorOptions): { text: string; blob: Blob } {
91+
public toSRT(options: GeneratorOptions = {}): { text: string; blob: Blob } {
9292
let idx: number = 1;
9393
let srt: string = '';
9494

0 commit comments

Comments
 (0)