Skip to content

πŸŽ₯ Make videos programmatically with React

17.1 KB
Raw Blame
import {expect, test} from 'bun:test';
import {absoluteFillSchema} from '../AbsoluteFill.js';
import {animatedImageSchema} from '../animated-image/AnimatedImage.js';
import {canvasImageSchema} from '../canvas-image/CanvasImage.js';
import type {SequenceControls} from '../CompositionManager.js';
import {solidSchema} from '../effects/Solid.js';
import {getComponentsToAddStacksTo} from '../enable-sequence-stack-traces.js';
import {
	flattenActiveSchema,
	getFlatSchemaWithAllKeys,
} from '../flatten-schema.js';
import {htmlInCanvasSchema} from '../HtmlInCanvas.js';
import {imgSchema} from '../Img.js';
import {Interactive} from '../Interactive.js';
import {
	backgroundSchema,
	baseSchema,
	borderRadiusSchema,
	borderSchema,
	captionsSchema,
	cropSchema,
	extendSchemaWithSequenceName,
	loopField,
	premountSchema,
	sequenceCropSchema,
	sequencePremountSchema,
	sequenceSchema,
	sequenceSchemaWithoutFrom,
	sequenceStyleSchema,
	svgPaintSchema,
	svgStrokeSchema,
	textContentSchema,
	textSchema,
	transformSchema,
} from '../interactivity-schema.js';
import {
	getNestedValue,
	mergeValues,
	readValuesFromProps,
	selectActiveKeys,
} from '../with-interactivity-schema.js';

test('sequenceStyleSchema contains visual style and premount fields', () => {
	expect(Object.keys(sequenceStyleSchema).sort()).toEqual(
		[
			...Object.keys(sequenceCropSchema),
			...Object.keys(transformSchema),
			...Object.keys(backgroundSchema),
			...Object.keys(borderSchema),
			...Object.keys(borderRadiusSchema),
			...Object.keys(sequencePremountSchema),
		].sort(),
	);
});

test('CSS box component schemas expose background and border controls', () => {
	for (const schema of [
		absoluteFillSchema,
		sequenceStyleSchema,
		imgSchema,
		animatedImageSchema,
		canvasImageSchema,
		htmlInCanvasSchema,
		solidSchema,
	]) {
		expect('style.backgroundColor' in schema).toBe(true);
		expect('style.borderWidth' in schema).toBe(true);
		expect('style.borderStyle' in schema).toBe(true);
		expect('style.borderColor' in schema).toBe(true);
		expect('style.borderRadius' in schema).toBe(true);
		expect('style.borderTopLeftRadius' in schema).toBe(true);
		expect('style.borderTopRightRadius' in schema).toBe(true);
		expect('style.borderBottomRightRadius' in schema).toBe(true);
		expect('style.borderBottomLeftRadius' in schema).toBe(true);
	}
});

test('premount fields are not keyframable', () => {
	expect(Object.keys(premountSchema).sort()).toEqual(
		['postmountFor', 'premountFor'].sort(),
	);
	expect(premountSchema.premountFor.keyframable).toBe(false);
	expect(premountSchema.postmountFor.keyframable).toBe(false);
});

test('public base schemas expose a constant playback rate', () => {
	for (const schema of [
		Interactive.baseSchema,
		Interactive.sequenceSchema,
		sequenceSchemaWithoutFrom,
	]) {
		expect(schema.playbackRate).toMatchObject({
			type: 'number',
			default: 1,
			keyframable: false,
		});
	}
});

test('Sequence crop fields are keyframable ratios', () => {
	for (const field of Object.values(sequenceCropSchema)) {
		expect(field).toMatchObject({
			type: 'number',
			default: 0,
			min: 0,
			max: 1,
			keyframable: true,
		});
	}
});

test('cropSchema is reusable and supported components opt into it', () => {
	expect(Interactive.cropSchema).toBe(cropSchema);
	expect(sequenceCropSchema).toBe(cropSchema);

	for (const key of Object.keys(cropSchema)) {
		for (const schema of [
			animatedImageSchema,
			canvasImageSchema,
			htmlInCanvasSchema,
			imgSchema,
			solidSchema,
		]) {
			expect(key in schema).toBe(true);
		}
	}

	expect('cropLeft' in baseSchema).toBe(false);
});

test('captionsSchema is reusable', () => {
	expect(Interactive.captionsSchema).toBe(captionsSchema);
	expect(captionsSchema).toEqual({
		captions: {
			type: 'remotion-captions',
			default: undefined,
			description: 'Captions',
			keyframable: false,
		},
	});
});

test('baseSchema exposes common timeline fields', () => {
	expect(Object.keys(baseSchema).sort()).toEqual(
		[
			'durationInFrames',
			'freeze',
			'from',
			'hidden',
			'name',
			'showInTimeline',
			'trimBefore',
			'playbackRate',
		].sort(),
	);
	for (const schema of [imgSchema, canvasImageSchema, solidSchema]) {
		expect('loop' in schema).toBe(false);
	}

	expect(animatedImageSchema.loop).toBe(loopField);
});

test('pixelDensity is exposed only by canvas-backed component schemas', () => {
	const pixelDensitySchema = {
		type: 'number',
		min: 1,
		max: 3,
		step: 0.1,
		default: 1,
		description: 'Pixel density',
		hiddenFromList: false,
	} as const;

	expect(htmlInCanvasSchema.pixelDensity).toEqual(pixelDensitySchema);
	expect(solidSchema.pixelDensity).toEqual(pixelDensitySchema);
	expect('pixelDensity' in baseSchema).toBe(false);
	expect('pixelDensity' in Interactive.baseSchema).toBe(false);
});

test('_internalMakeRemotionComponentIdentity normalizes first-party package names', () => {
	expect(
		Interactive._internalMakeRemotionComponentIdentity({
			packageName: 'remotion',
			componentName: 'Sequence',
		}),
	).toBe('dev.remotion.remotion.Sequence');
	expect(
		Interactive._internalMakeRemotionComponentIdentity({
			packageName: '@remotion/light-leaks',
			componentName: 'LightLeak',
		}),
	).toBe('dev.remotion.lightLeaks.LightLeak');
	expect(
		Interactive._internalMakeRemotionComponentIdentity({
			packageName: '@remotion/rough-notation',
			componentName: 'Highlight',
		}),
	).toBe('dev.remotion.roughNotation.Highlight');
});

test('Interactive.withSchema() adds Sequence stack traces automatically', () => {
	const Component = (_props: {
		readonly controls: SequenceControls | undefined;
	}) => null;
	const Wrapped = Interactive.withSchema({
		Component,
		componentName: '<Component>',
		componentIdentity: 'com.example.Component',
		schema: {},
		supportsEffects: false,
	});

	expect(
		getComponentsToAddStacksTo().filter((component) => component === Wrapped),
	).toHaveLength(1);
});

test('getFlatSchema(sequenceSchema) exposes every variant key', () => {
	const flat = getFlatSchemaWithAllKeys(sequenceSchema);
	expect(Object.keys(flat).sort()).toEqual(
		[
			'cropBottom',
			'cropLeft',
			'cropRight',
			'cropTop',
			'hidden',
			'name',
			'showInTimeline',
			'layout',
			'style.translate',
			'style.scale',
			'style.rotate',
			'style.transformOrigin',
			'style.opacity',
			'style.backgroundColor',
			'style.borderWidth',
			'style.borderStyle',
			'style.borderColor',
			'style.borderRadius',
			'style.borderTopLeftRadius',
			'style.borderTopRightRadius',
			'style.borderBottomRightRadius',
			'style.borderBottomLeftRadius',
			'premountFor',
			'postmountFor',
			'durationInFrames',
			'from',
			'freeze',
			'trimBefore',
			'playbackRate',
		].sort(),
	);
});

test('getFlatSchema allows enum variants to share prop keys', () => {
	const schema = {
		type: {
			type: 'enum',
			default: 'underline',
			description: 'Type',
			variants: {
				underline: {
					color: {
						type: 'color',
						default: '#111111',
						description: 'Underline color',
					},
				},
				highlight: {
					color: {
						type: 'color',
						default: '#eeee00',
						description: 'Highlight color',
					},
				},
				bracket: {},
			},
		},
	} as const;

	const flat = getFlatSchemaWithAllKeys(schema);
	expect(Object.keys(flat).sort()).toEqual(['color', 'type']);
	expect(flat.color.type).toBe('color');
});

test('flattenActiveSchema resolves shared keys from the selected enum variant', () => {
	const schema = {
		type: {
			type: 'enum',
			default: 'underline',
			description: 'Type',
			variants: {
				underline: {
					color: {
						type: 'color',
						default: '#111111',
						description: 'Underline color',
					},
				},
				highlight: {
					color: {
						type: 'color',
						default: '#eeee00',
						description: 'Highlight color',
					},
				},
			},
		},
	} as const;

	const active = flattenActiveSchema(schema, (key) =>
		key === 'type' ? 'highlight' : undefined,
	);
	expect(active.color).toEqual({
		type: 'color',
		default: '#eeee00',
		description: 'Highlight color',
	});
});

test('extendSchemaWithSequenceName adds hidden name to wrapped schemas', () => {
	const flat = getFlatSchemaWithAllKeys(
		extendSchemaWithSequenceName({
			opacity: {
				type: 'number',
				default: 1,
				hiddenFromList: false,
			},
		}),
	);

	expect(flat.name).toEqual({type: 'hidden'});
	expect(flat.opacity.type).toBe('number');
});

test('sequenceSchemaWithoutFrom does not expose from', () => {
	const flat = getFlatSchemaWithAllKeys(sequenceSchemaWithoutFrom);
	expect(Object.keys(flat)).not.toContain('from');
	expect(Object.keys(flat)).toContain('durationInFrames');
	expect(Object.keys(flat)).toContain('freeze');
	expect(Object.keys(flat)).toContain('trimBefore');
});

test('style.scale does not impose a minimum value', () => {
	const scaleSchema = transformSchema['style.scale'];
	expect('min' in scaleSchema).toBe(false);
});

test('textSchema exposes common text style fields', () => {
	expect(Object.keys(textSchema).sort()).toEqual(
		[
			'style.color',
			'style.fontFamily',
			'style.fontSize',
			'style.fontStyle',
			'style.fontWeight',
			'style.letterSpacing',
			'style.lineHeight',
			'style.textAlign',
		].sort(),
	);
	expect(textSchema['style.color'].type).toBe('color');
	expect(textSchema['style.color'].default).toBeUndefined();
	expect(textSchema['style.fontFamily']).toMatchObject({
		type: 'font-family',
		default: undefined,
		keyframable: false,
	});
	expect(textSchema['style.fontSize']).toMatchObject({
		type: 'number',
		default: undefined,
		min: 0,
		step: 1,
		hiddenFromList: false,
	});
	expect(textSchema['style.lineHeight']).toMatchObject({
		type: 'number',
		default: undefined,
		min: 0,
		step: 0.05,
		hiddenFromList: false,
	});
	expect(textSchema['style.fontWeight']).toEqual({
		type: 'font-weight',
		default: 400,
		description: 'Font weight',
	});
	expect(Object.keys(textSchema['style.fontStyle'].variants)).toEqual([
		'normal',
		'italic',
		'oblique',
	]);
	expect(Object.keys(textSchema['style.textAlign'].variants)).toEqual([
		'left',
		'center',
		'right',
		'justify',
		'start',
		'end',
	]);
	expect(textSchema['style.letterSpacing']).toMatchObject({
		type: 'number',
		default: undefined,
		step: 0.1,
		hiddenFromList: false,
	});
});

test('borderSchema exposes the longhand border fields', () => {
	expect(Object.keys(borderSchema).sort()).toEqual(
		['style.borderColor', 'style.borderStyle', 'style.borderWidth'].sort(),
	);

	expect(borderSchema['style.borderWidth']).toMatchObject({
		type: 'number',
		default: undefined,
		min: 0,
		step: 1,
		hiddenFromList: false,
	});
	expect(borderSchema['style.borderStyle']).toMatchObject({
		type: 'enum',
		default: 'none',
	});
	expect(Object.keys(borderSchema['style.borderStyle'].variants)).toEqual([
		'none',
		'hidden',
		'solid',
		'dashed',
		'dotted',
		'double',
		'groove',
		'ridge',
		'inset',
		'outset',
	]);
	expect(borderSchema['style.borderColor']).toMatchObject({
		type: 'color',
		default: undefined,
	});
});

test('borderRadiusSchema exposes the shorthand and four corner radius fields', () => {
	expect(Interactive.borderRadiusSchema).toBe(borderRadiusSchema);
	expect(Object.keys(borderRadiusSchema).sort()).toEqual(
		[
			'style.borderRadius',
			'style.borderTopLeftRadius',
			'style.borderTopRightRadius',
			'style.borderBottomRightRadius',
			'style.borderBottomLeftRadius',
		].sort(),
	);
	for (const radius of [
		'style.borderRadius',
		'style.borderTopLeftRadius',
		'style.borderTopRightRadius',
		'style.borderBottomRightRadius',
		'style.borderBottomLeftRadius',
	] as const) {
		expect(borderRadiusSchema[radius]).toMatchObject({
			type: 'number',
			default: 0,
			min: 0,
			step: 1,
			hiddenFromList: false,
		});
	}

	expect(borderRadiusSchema['style.borderRadius'].keyframable).toBe(true);
});

test('backgroundSchema exposes the background color style field', () => {
	expect(Object.keys(backgroundSchema)).toEqual(['style.backgroundColor']);
	expect(backgroundSchema['style.backgroundColor']).toMatchObject({
		type: 'color',
		default: 'transparent',
	});
});

test('svgStrokeSchema exposes SVG stroke controls', () => {
	expect(Object.keys(svgStrokeSchema)).toEqual([
		'color',
		'stroke',
		'strokeWidth',
	]);
	expect(svgStrokeSchema.color).toMatchObject({
		type: 'color',
		default: undefined,
		description: 'Current color',
	});
	expect(svgStrokeSchema.stroke).toMatchObject({
		type: 'color',
		default: 'none',
		description: 'Stroke',
	});
	expect(svgStrokeSchema.strokeWidth).toMatchObject({
		type: 'number',
		default: 1,
		description: 'Stroke width',
		min: 0,
		step: 1,
		hiddenFromList: false,
	});
	expect(Interactive.svgStrokeSchema).toBe(svgStrokeSchema);
});

test('svgPaintSchema exposes SVG fill and stroke fields', () => {
	expect(Object.keys(svgPaintSchema)).toEqual([
		'fill',
		'color',
		'stroke',
		'strokeWidth',
	]);
	expect(svgPaintSchema.fill).toMatchObject({
		type: 'color',
		default: undefined,
		description: 'Fill',
	});
	expect(svgPaintSchema.color).toBe(svgStrokeSchema.color);
	expect(svgPaintSchema.stroke).toMatchObject({
		type: 'color',
		default: 'none',
		description: 'Stroke',
	});
	expect(svgPaintSchema.strokeWidth).toBe(svgStrokeSchema.strokeWidth);
	expect(Interactive.svgPaintSchema).toBe(svgPaintSchema);
});

test('readValuesFromProps reads dot-notation keys via getNestedValue', () => {
	const props = {
		layout: 'absolute-fill',
		style: {scale: 1.74, opacity: 0.5},
	};
	const flatKeys = Object.keys(getFlatSchemaWithAllKeys(sequenceSchema));
	const values = readValuesFromProps(props, flatKeys);
	expect(values.layout).toBe('absolute-fill');
	expect(values['style.scale']).toBe(1.74);
	expect(values['style.opacity']).toBe(0.5);
	expect(values['style.rotate']).toBeUndefined();
});

test('readValuesFromProps ignores non-string text content runtime values', () => {
	const props = {
		children: {type: 'span'},
	};
	const flatSchema = getFlatSchemaWithAllKeys(textContentSchema);
	const values = readValuesFromProps(props, ['children'], flatSchema);
	expect(values.children).toBeUndefined();

	const stringValues = readValuesFromProps(
		{children: 'Hello'},
		['children'],
		flatSchema,
	);
	expect(stringValues.children).toBe('Hello');
});

test('selectActiveKeys returns only the hidden + layout keys when layout=none', () => {
	const values = {
		layout: 'none',
		'style.scale': 2,
	};
	expect(selectActiveKeys(sequenceSchema, values).sort()).toEqual(
		[
			'hidden',
			'layout',
			'durationInFrames',
			'from',
			'trimBefore',
			'playbackRate',
			'freeze',
		].sort(),
	);
});

test('selectActiveKeys exposes style.* keys when layout=absolute-fill', () => {
	const values = {
		layout: 'absolute-fill',
		'style.scale': 2,
	};
	expect(selectActiveKeys(sequenceSchema, values).sort()).toEqual(
		[
			'cropBottom',
			'cropLeft',
			'cropRight',
			'cropTop',
			'hidden',
			'layout',
			'durationInFrames',
			'from',
			'trimBefore',
			'playbackRate',
			'freeze',
			'style.translate',
			'style.scale',
			'style.rotate',
			'style.transformOrigin',
			'style.opacity',
			'style.backgroundColor',
			'style.borderWidth',
			'style.borderStyle',
			'style.borderColor',
			'style.borderRadius',
			'style.borderTopLeftRadius',
			'style.borderTopRightRadius',
			'style.borderBottomRightRadius',
			'style.borderBottomLeftRadius',
			'premountFor',
			'postmountFor',
		].sort(),
	);

	const values2 = {
		layout: 'none',
		'style.scale': 2,
	};
	expect(selectActiveKeys(sequenceSchema, values2).sort()).toEqual(
		[
			'hidden',
			'layout',
			'durationInFrames',
			'from',
			'trimBefore',
			'playbackRate',
			'freeze',
		].sort(),
	);
});

test('mergeValues writes nested values back into props with dot keys', () => {
	const props = {layout: 'absolute-fill', style: {scale: 1}};
	const values = {layout: 'absolute-fill', 'style.scale': 2};
	const merged = mergeValues({
		flatSchema: getFlatSchemaWithAllKeys(sequenceSchema),
		props,
		valuesDotNotation: values,
		schemaKeys: ['layout', 'style.scale'],
		propsToDelete: new Set(),
	});
	expect(merged.layout).toBe('absolute-fill');
	expect((merged.style as {scale: number}).scale).toBe(2);
});

test('mergeValues preserves non-string text content props', () => {
	const children = {type: 'span'};
	const merged = mergeValues({
		flatSchema: getFlatSchemaWithAllKeys(textContentSchema),
		props: {children},
		valuesDotNotation: {children: undefined},
		schemaKeys: ['children'],
		propsToDelete: new Set(),
	});
	expect(merged.children).toBe(children);
});

test('end-to-end: layout=none drops style.scale from active props', () => {
	const props = {layout: 'none', style: {scale: 2}};
	const flatKeys = Object.keys(getFlatSchemaWithAllKeys(sequenceSchema));
	const values = readValuesFromProps(props, flatKeys);
	const activeKeys = selectActiveKeys(sequenceSchema, values);
	const flatSchema = getFlatSchemaWithAllKeys(sequenceSchema);
	const merged = mergeValues({
		flatSchema,
		props,
		valuesDotNotation: values,
		schemaKeys: activeKeys,
		propsToDelete: new Set(),
	});
	expect(activeKeys.sort()).toEqual(
		[
			'hidden',
			'layout',
			'durationInFrames',
			'from',
			'trimBefore',
			'playbackRate',
			'freeze',
		].sort(),
	);
	// style.scale was not in activeKeys β†’ original style preserved, not overwritten
	expect((merged.style as {scale: number}).scale).toBe(2);
});

test('getNestedValue returns undefined for missing nested keys', () => {
	expect(getNestedValue({a: {}}, 'a.b.c')).toBeUndefined();
	expect(getNestedValue({a: {b: 1}}, 'a.b')).toBe(1);
});