🎥 Make videos programmatically with React
import {expect, test} from 'bun:test';
import {
act,
cleanup,
fireEvent,
render,
screen,
waitFor,
} from '@testing-library/react';
import type {AddCaptionJobParams} from '../components/RenderQueue/caption-job-types';
import {StudioServerConnectionCtx} from '../helpers/client-id';
const selectComboboxItem = async (
combobox: HTMLElement,
itemName: string | RegExp,
) => {
fireEvent.click(combobox);
const item = await screen.findByRole('button', {name: itemName});
fireEvent.pointerUp(item, {button: 0});
};
const setNumberSetting = (name: RegExp, value: number) => {
fireEvent.click(screen.getByRole('button', {name}));
const input = screen.getByRole('textbox', {name});
fireEvent.change(input, {target: {value: String(value)}});
fireEvent.blur(input);
};
test('serializes transcription modal settings into caption jobs', async () => {
const {portals} = await import('../components/Menu/portals');
const previousPortals = [...portals];
const createdPortals: HTMLElement[] = [];
const testPortals = Array.from({length: 6}, (_, index) => {
const id = `menuportal-${index}`;
const existing = document.getElementById(id);
if (existing) {
return existing;
}
const element = document.createElement('div');
element.id = id;
document.body.appendChild(element);
createdPortals.push(element);
return element;
});
portals.splice(0, portals.length, ...testPortals);
const originalGpuDescriptor = Object.getOwnPropertyDescriptor(
navigator,
'gpu',
);
const originalSecureContextDescriptor = Object.getOwnPropertyDescriptor(
window,
'isSecureContext',
);
const originalStaticBaseDescriptor = Object.getOwnPropertyDescriptor(
window,
'remotion_staticBase',
);
const originalStaticFiles = window.remotion_staticFiles;
const originalLocalStorageDescriptor = Object.getOwnPropertyDescriptor(
globalThis,
'localStorage',
);
const localStorageValues = new Map<string, string>();
const localStorageMock: Storage = {
clear: () => localStorageValues.clear(),
getItem: (key: string) => localStorageValues.get(key) ?? null,
key: (index: number) => [...localStorageValues.keys()][index] ?? null,
removeItem: (key: string) => localStorageValues.delete(key),
setItem: (key: string, value: string) => localStorageValues.set(key, value),
get length() {
return localStorageValues.size;
},
};
Object.defineProperty(navigator, 'gpu', {
configurable: true,
value: {requestAdapter: () => Promise.resolve({})},
});
Object.defineProperty(window, 'isSecureContext', {
configurable: true,
value: true,
});
Object.defineProperty(window, 'remotion_staticBase', {
configurable: true,
value: '/static-test',
});
window.remotion_staticFiles = [
{
lastModified: 0,
name: 'captions/interview.json',
sizeInBytes: 2,
src: '/static-test/captions/interview.json',
},
];
Object.defineProperty(globalThis, 'localStorage', {
configurable: true,
value: localStorageMock,
});
try {
const {SettingsProvider} = await import('../components/SettingsContext');
const {TranscriptionModal} =
await import('../components/Transcription/TranscriptionModal');
const {RenderQueueContext} =
await import('../components/RenderQueue/context');
const {SetSelectedModalContext} = await import('../state/modals');
const {SidebarContext} = await import('../state/sidebar');
const {StaticFilesProvider} =
await import('../components/use-static-files');
const submittedJobs: AddCaptionJobParams[] = [];
const requestInit = {
credentials: 'include',
headers: {'x-remotion-test': 'modal-serialization'},
} as const;
render(
<StudioServerConnectionCtx.Provider
value={{
previewServerState: {type: 'init'},
configFileChangeRevision: 0,
restartRequired: false,
subscribeToEvent: () => () => undefined,
}}
>
<SettingsProvider>
<SetSelectedModalContext.Provider
value={{setSelectedModal: () => undefined}}
>
<SidebarContext.Provider
value={{
leftSidebarOverlayOpen: false,
setLeftSidebarOverlayOpen: () => undefined,
rightSidebarTemporaryExpansion: false,
setRightSidebarTemporaryExpansion: () => undefined,
sidebarCollapsedDuringDrag: null,
sidebarCollapsedStateLeft: 'collapsed',
sidebarCollapsedStateRight: 'collapsed',
setSidebarCollapsedDuringDrag: () => undefined,
setSidebarCollapsedState: () => undefined,
}}
>
<RenderQueueContext.Provider
value={
{
captionJobs: [],
addCaptionJob: (params: AddCaptionJobParams) => {
submittedJobs.push(params);
return `caption-job-${submittedJobs.length}`;
},
} as never
}
>
<StaticFilesProvider>
<TranscriptionModal
type="transcribe"
captionStyle={null}
audioStreamIndex={2}
displayName="interview.wav"
requestInit={requestInit}
src="/media/interview.wav"
target={null}
/>
</StaticFilesProvider>
</RenderQueueContext.Provider>
</SidebarContext.Provider>
</SetSelectedModalContext.Provider>
</SettingsProvider>
</StudioServerConnectionCtx.Provider>,
);
screen.getByRole('dialog', {
name: 'Transcribe interview.wav',
});
await waitFor(() => {
const buttons = screen.getAllByRole('button', {name: /^Transcribe$/});
expect(buttons).toHaveLength(2);
expect((buttons[0] as HTMLButtonElement).disabled).toBe(false);
});
const [submit] = screen.getAllByRole('button', {
name: /^Transcribe$/,
}) as HTMLButtonElement[];
expect(
screen.queryByText('Runs locally as a background job using WebGPU'),
).toBeNull();
const outputLabel = screen.getByText('Output in public/');
const modelLabel = screen.getByText('Whisper model');
expect(
outputLabel.compareDocumentPosition(modelLabel) &
Node.DOCUMENT_POSITION_FOLLOWING,
).not.toBe(0);
const outputHelp = screen.getByRole('button', {
name: 'Learn more about Output in public/',
});
expect(screen.queryByText('Caption[]')).toBeNull();
fireEvent.pointerUp(outputHelp);
const captionType = screen.getByText('Caption[]');
expect(captionType.style.fontFamily).toBe('monospace');
expect(captionType.style.fontSize).toBe('inherit');
expect(captionType.style.lineHeight).toBe('inherit');
fireEvent.pointerUp(outputHelp);
expect(
screen.queryByRole('checkbox', {name: 'Force full sequences'}),
).toBeNull();
expect(screen.queryByRole('button', {name: 'Task'})).toBeNull();
await selectComboboxItem(
screen.getByRole('button', {name: 'Whisper model'}),
/^tiny ·/,
);
await selectComboboxItem(
screen.getByRole('button', {name: 'Spoken language'}),
'German',
);
await selectComboboxItem(
screen.getByRole('button', {name: 'Task'}),
'Task: Translate to English',
);
const taskLabel = screen.getByText('Translate to English', {
selector: 'span[aria-hidden="true"]',
});
expect(taskLabel.style.color).toBe('inherit');
expect(taskLabel.style.fontFamily).toBe('inherit');
expect(taskLabel.style.fontSize).toBe('inherit');
expect(taskLabel.style.lineHeight).toBe('inherit');
const taskWarning = screen.getByText(
'Word timings may be less accurate when translating to English.',
);
expect(
taskWarning.parentElement?.parentElement?.parentElement?.style.display,
).toBe('flex');
expect(
taskWarning.parentElement?.parentElement?.parentElement?.style
.justifyContent,
).toBe('flex-end');
const outputInput = screen.getByRole('textbox', {
name: 'Caption output file',
});
expect(
outputInput.parentElement?.parentElement?.parentElement?.style
.paddingBottom,
).toBe('12px');
fireEvent.change(outputInput, {
target: {value: 'captions/interview.json'},
});
screen.getByText('Exists, will be overwritten');
screen.getByRole('button', {
name: /^Open in (Finder|File Explorer|File Manager)$/,
});
fireEvent.click(screen.getByRole('button', {name: 'Advanced'}));
screen.getByRole('button', {name: 'Learn more about Chunk length'});
screen.getByRole('button', {name: 'Learn more about Stride length'});
screen.getByRole('button', {
name: 'Learn more about Force full sequences',
});
screen.getByRole('button', {name: 'Learn more about Use sampling'});
screen.getByRole('button', {
name: 'Learn more about Repetition penalty',
});
screen.getByRole('button', {
name: 'Learn more about No-repeat n-gram size',
});
const forceFullSequences = screen.getByRole('checkbox', {
name: 'Force full sequences',
});
const useSampling = screen.getByRole('checkbox', {
name: 'Use sampling',
});
for (const checkbox of [forceFullSequences, useSampling]) {
const checkboxLabel = document.querySelector(
`label[for="${checkbox.id}"]`,
) as HTMLLabelElement | null;
expect(checkboxLabel?.style.color).toBe('inherit');
expect(checkboxLabel?.style.fontFamily).toBe('inherit');
expect(checkboxLabel?.style.fontSize).toBe('inherit');
expect(checkboxLabel?.style.lineHeight).toBe('inherit');
}
act(() => forceFullSequences.click());
act(() => useSampling.click());
screen.getByRole('button', {name: 'Learn more about Temperature'});
screen.getByRole('button', {name: 'Learn more about Top K'});
setNumberSetting(/^Chunk length:/, 22);
setNumberSetting(/^Stride length:/, 4);
setNumberSetting(/^Temperature:/, 0.7);
setNumberSetting(/^Top K:/, 25);
setNumberSetting(/^Repetition penalty:/, 1.2);
setNumberSetting(/^No-repeat n-gram size:/, 3);
act(() => useSampling.click());
fireEvent.click(submit);
act(() => forceFullSequences.click());
act(() => useSampling.click());
fireEvent.click(submit);
const commonJob = {
captionStyle: null,
audioStreamIndex: 2,
chunkLengthInSeconds: 22,
displayName: 'interview.wav',
language: 'de',
model: 'tiny',
noRepeatNgramSize: 3,
outName: 'captions/interview.json',
target: null,
repetitionPenalty: 1.2,
requestInit,
src: '/media/interview.wav',
strideLengthInSeconds: 4,
task: 'translate',
temperature: 0.7,
topK: 25,
} satisfies Omit<AddCaptionJobParams, 'doSample' | 'forceFullSequences'>;
expect(submittedJobs).toEqual([
{...commonJob, doSample: false, forceFullSequences: true},
{...commonJob, doSample: true, forceFullSequences: false},
]);
} finally {
cleanup();
portals.splice(0, portals.length, ...previousPortals);
for (const portal of createdPortals) {
portal.remove();
}
if (originalGpuDescriptor) {
Object.defineProperty(navigator, 'gpu', originalGpuDescriptor);
} else {
Reflect.deleteProperty(navigator, 'gpu');
}
if (originalSecureContextDescriptor) {
Object.defineProperty(
window,
'isSecureContext',
originalSecureContextDescriptor,
);
} else {
Reflect.deleteProperty(window, 'isSecureContext');
}
if (originalStaticBaseDescriptor) {
Object.defineProperty(
window,
'remotion_staticBase',
originalStaticBaseDescriptor,
);
} else {
Reflect.deleteProperty(window, 'remotion_staticBase');
}
window.remotion_staticFiles = originalStaticFiles;
if (originalLocalStorageDescriptor) {
Object.defineProperty(
globalThis,
'localStorage',
originalLocalStorageDescriptor,
);
} else {
Reflect.deleteProperty(globalThis, 'localStorage');
}
}
});