@@ -9,12 +9,14 @@ import { describe, expect, it, vi, beforeEach, afterAll } from 'vitest';
99import { Keyframe , Timestamp , Transcript } from '../../models' ;
1010import { captions } from '../../test/captions' ;
1111import { Composition } from '../../composition' ;
12- import { MediaTrack } from '../../tracks' ;
12+ import { CaptionTrack , MediaTrack } from '../../tracks' ;
13+ import { MediaClipProps } from './media.interfaces' ;
14+ import { Font , TextClip } from '../text' ;
15+ import { ValidationError } from '../../errors' ;
1316import { VisualMixin , VisualMixinProps } from '../mixins' ;
1417import { MediaClip } from './media' ;
1518
1619import type { frame , MixinType } from '../../types' ;
17- import { MediaClipProps } from './media.interfaces' ;
1820
1921describe ( 'The Media Clip' , ( ) => {
2022 const mockFn = vi . fn ( ) ;
@@ -285,6 +287,34 @@ describe('The Media Clip', () => {
285287 expect ( copy . range [ 1 ] ) . toBeInstanceOf ( Timestamp ) ;
286288 expect ( copy . range [ 1 ] . frames ) . toBe ( 80 ) ;
287289 } ) ;
290+
291+ it ( 'should generate a caption track using a transcript' , async ( ) => {
292+ vi . spyOn ( Font . prototype , 'load' ) . mockImplementation ( async ( ) => new Font ( ) ) ;
293+ clip . transcript = Transcript . fromJSON ( captions ) ;
294+ clip . state = 'READY' ;
295+
296+ const composition = new Composition ( ) ;
297+ await composition . add ( clip ) ;
298+
299+ await clip . generateCaptions ( ) ;
300+
301+ expect ( composition . tracks . length ) . toBe ( 2 ) ;
302+
303+ const track = composition . tracks [ 0 ] ;
304+
305+ expect ( track ) . toBeInstanceOf ( CaptionTrack ) ;
306+ expect ( track . clips . length ) . toBe ( 36 ) ;
307+ expect ( track . clips [ 0 ] ) . toBeInstanceOf ( TextClip ) ;
308+ expect ( ( track . clips [ 0 ] as TextClip ) . text ) . toBe ( 'Is the' ) ;
309+ } ) ;
310+
311+ it ( 'should throw an error when trying to generate captions without a composition' , async ( ) => {
312+ vi . spyOn ( Font . prototype , 'load' ) . mockImplementation ( async ( ) => new Font ( ) ) ;
313+ clip . transcript = Transcript . fromJSON ( captions ) ;
314+ clip . state = 'READY' ;
315+
316+ await expect ( ( ) => clip . generateCaptions ( ) ) . rejects . toThrowError ( ValidationError )
317+ } ) ;
288318} ) ;
289319
290320describe ( 'Split tests - the Media Clip object' , ( ) => {
0 commit comments