Skip to content

Commit 6980b81

Browse files
committed
added track unit tests
1 parent c528574 commit 6980b81

File tree

4 files changed

+245
-0
lines changed

4 files changed

+245
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* Copyright (c) 2024 The Diffusion Studio Authors
3+
*
4+
* This Source Code Form is subject to the terms of the Mozilla
5+
* Public License, v. 2.0 that can be found in the LICENSE file.
6+
*/
7+
8+
import { describe, expect, it, vi, beforeEach } from 'vitest';
9+
import { Composition } from '../../composition';
10+
import { MediaClip, TextClip } from '../../clips';
11+
import { Transcript, Word, WordGroup } from '../../models';
12+
import { CaptionTrack } from './caption';
13+
import { SolarCaptionPreset } from './preset.solar';
14+
import { GlowFilter } from 'pixi-filters';
15+
16+
describe('The SolarCaptionPreset', () => {
17+
const mockFn = vi.fn();
18+
Object.assign(document, { fonts: { add: mockFn } });
19+
20+
let composition: Composition;
21+
let track: CaptionTrack;
22+
23+
beforeEach(() => {
24+
composition = new Composition();
25+
track = composition.createTrack('caption');
26+
});
27+
28+
it('should apply complex clips to the track', async () => {
29+
await track
30+
.from(new MediaClip({ transcript }))
31+
.generate(SolarCaptionPreset);
32+
33+
expect(track.clips.length).toBe(13);
34+
expect(track.clips[0]).toBeInstanceOf(TextClip);
35+
expect(track.clips[0].start.frames).toBe(0);
36+
expect(track.clips[0].text).toBe('Lorem');
37+
expect(track.clips[0].filters).toBeInstanceOf(GlowFilter);
38+
});
39+
40+
it('should not apply clips if the transcript or composition is not devined', async () => {
41+
await expect(() => track
42+
.from(new MediaClip())
43+
.generate(SolarCaptionPreset)).rejects.toThrowError();
44+
45+
await expect(() => new CaptionTrack()
46+
.from(new MediaClip({ transcript }))
47+
.generate(SolarCaptionPreset)).rejects.toThrowError();
48+
});
49+
});
50+
51+
const transcript = new Transcript([
52+
new WordGroup([
53+
new Word('Lorem', 0, 1e3),
54+
new Word('Ipsum', 2e3, 3e3),
55+
new Word('is', 4e3, 5e3),
56+
new Word('simply', 6e3, 7e3),
57+
new Word('dummy', 8e3, 9e3),
58+
new Word('text', 10e3, 11e3),
59+
new Word('of', 12e3, 13e3),
60+
new Word('the', 14e3, 15e3),
61+
new Word('printing', 16e3, 17e3),
62+
new Word('and', 18e3, 19e3),
63+
new Word('typesetting', 20e3, 21e3),
64+
new Word('industry', 22e3, 23e3),
65+
]),
66+
new WordGroup([
67+
new Word('Lorem', 24e3, 25e3),
68+
]),
69+
]);
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* Copyright (c) 2024 The Diffusion Studio Authors
3+
*
4+
* This Source Code Form is subject to the terms of the Mozilla
5+
* Public License, v. 2.0 that can be found in the LICENSE file.
6+
*/
7+
8+
import { describe, expect, it, vi, beforeEach } from 'vitest';
9+
import { Composition } from '../../composition';
10+
import { ComplexTextClip, MediaClip } from '../../clips';
11+
import { Transcript, Word, WordGroup } from '../../models';
12+
import { CaptionTrack } from './caption';
13+
import { VerdantCaptionPreset } from './preset.verdant';
14+
15+
describe('The VerdantCaptionPreset', () => {
16+
const mockFn = vi.fn();
17+
Object.assign(document, { fonts: { add: mockFn } });
18+
19+
let composition: Composition;
20+
let track: CaptionTrack;
21+
22+
beforeEach(() => {
23+
composition = new Composition();
24+
track = composition.createTrack('caption');
25+
});
26+
27+
it('should apply complex clips to the track', async () => {
28+
await track
29+
.from(new MediaClip({ transcript }))
30+
.generate(VerdantCaptionPreset);
31+
32+
expect(track.clips.length).toBe(13);
33+
expect(track.clips[0]).toBeInstanceOf(ComplexTextClip);
34+
expect(track.clips[0].start.frames).toBe(0);
35+
expect(track.clips[0].text).toBe('Lorem');
36+
});
37+
38+
it('should not apply clips if the transcript or composition is not devined', async () => {
39+
await expect(() => track
40+
.from(new MediaClip())
41+
.generate(VerdantCaptionPreset)).rejects.toThrowError();
42+
43+
await expect(() => new CaptionTrack()
44+
.from(new MediaClip({ transcript }))
45+
.generate(VerdantCaptionPreset)).rejects.toThrowError();
46+
});
47+
});
48+
49+
const transcript = new Transcript([
50+
new WordGroup([
51+
new Word('Lorem', 0, 1e3),
52+
new Word('Ipsum', 2e3, 3e3),
53+
new Word('is', 4e3, 5e3),
54+
new Word('simply', 6e3, 7e3),
55+
new Word('dummy', 8e3, 9e3),
56+
new Word('text', 10e3, 11e3),
57+
new Word('of', 12e3, 13e3),
58+
new Word('the', 14e3, 15e3),
59+
new Word('printing', 16e3, 17e3),
60+
new Word('and', 18e3, 19e3),
61+
new Word('typesetting', 20e3, 21e3),
62+
new Word('industry', 22e3, 23e3),
63+
]),
64+
new WordGroup([
65+
new Word('Lorem', 24e3, 25e3),
66+
]),
67+
]);
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* Copyright (c) 2024 The Diffusion Studio Authors
3+
*
4+
* This Source Code Form is subject to the terms of the Mozilla
5+
* Public License, v. 2.0 that can be found in the LICENSE file.
6+
*/
7+
8+
import { describe, expect, it, vi, beforeEach } from 'vitest';
9+
import { Composition } from '../../composition';
10+
import { ComplexTextClip, MediaClip } from '../../clips';
11+
import { Transcript, Word, WordGroup } from '../../models';
12+
import { CaptionTrack } from './caption';
13+
import { WhisperCaptionPreset } from './preset.whisper';
14+
15+
describe('The WhisperCaptionPreset', () => {
16+
const mockFn = vi.fn();
17+
Object.assign(document, { fonts: { add: mockFn } });
18+
19+
let composition: Composition;
20+
let track: CaptionTrack;
21+
22+
beforeEach(() => {
23+
composition = new Composition();
24+
track = composition.createTrack('caption');
25+
});
26+
27+
it('should apply complex clips to the track', async () => {
28+
await track
29+
.from(new MediaClip({ transcript }))
30+
.generate(WhisperCaptionPreset);
31+
32+
expect(track.clips.length).toBe(13);
33+
expect(track.clips[0]).toBeInstanceOf(ComplexTextClip);
34+
expect(track.clips[0].start.frames).toBe(0);
35+
expect(track.clips[0].text).toBe('Lorem Ipsum is simply');
36+
});
37+
38+
it('should not apply clips if the transcript or composition is not devined', async () => {
39+
await expect(() => track
40+
.from(new MediaClip())
41+
.generate(WhisperCaptionPreset)).rejects.toThrowError();
42+
43+
await expect(() => new CaptionTrack()
44+
.from(new MediaClip({ transcript }))
45+
.generate(WhisperCaptionPreset)).rejects.toThrowError();
46+
});
47+
});
48+
49+
const transcript = new Transcript([
50+
new WordGroup([
51+
new Word('Lorem', 0, 1e3),
52+
new Word('Ipsum', 2e3, 3e3),
53+
new Word('is', 4e3, 5e3),
54+
new Word('simply', 6e3, 7e3),
55+
new Word('dummy', 8e3, 9e3),
56+
new Word('text', 10e3, 11e3),
57+
new Word('of', 12e3, 13e3),
58+
new Word('the', 14e3, 15e3),
59+
new Word('printing', 16e3, 17e3),
60+
new Word('and', 18e3, 19e3),
61+
new Word('typesetting', 20e3, 21e3),
62+
new Word('industry', 22e3, 23e3),
63+
]),
64+
new WordGroup([
65+
new Word('Lorem', 24e3, 25e3),
66+
]),
67+
]);

src/tracks/track/track.spec.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,48 @@ describe('The Track Object', () => {
290290
expect(track.clips.at(0)?.stop.frames).toBe(10);
291291
});
292292

293+
it('should switch from stack to default', async () => {
294+
track.stacked();
295+
296+
await track.add(new Clip({ stop: 12}));
297+
await track.add(new Clip({ stop: 24}));
298+
299+
expect(track.clips.length).toBe(2);
300+
expect(track.stop.frames).toBe(36);
301+
302+
track.stacked(false);
303+
304+
// should not be realigned
305+
await track.add(new Clip({ start: 72, stop: 99 }));
306+
307+
expect(track.stop.frames).toBe(99);
308+
});
309+
310+
it('should apply values to all clips', async () => {
311+
track.stacked();
312+
313+
await track.add(new Clip({ stop: 12}));
314+
await track.add(new Clip({ stop: 24}));
315+
316+
track.apply(clip => clip.set({ name: 'foo' }));
317+
318+
expect(track.clips[0].name).toBe('foo');
319+
expect(track.clips[1].name).toBe('foo');
320+
});
321+
322+
it('should offset all clips by a given frame numer', async () => {
323+
await track.add(new Clip({ start: 6, stop: 12}));
324+
await track.add(new Clip({ start: 15, stop: 24}));
325+
326+
track.offsetBy(6);
327+
328+
expect(track.clips[0].start.frames).toBe(12);
329+
expect(track.clips[0].stop.frames).toBe(18);
330+
331+
expect(track.clips[1].start.frames).toBe(21);
332+
expect(track.clips[1].stop.frames).toBe(30);
333+
});
334+
293335
it('should be be able to add a base clip with offset', async () => {
294336
const clip = new Clip({ stop: 30 });
295337

0 commit comments

Comments
 (0)