Skip to content

🎥 Make videos programmatically with React

12.0 KB
Raw Blame
import {expect, test} from 'bun:test';
import {
	Internals,
	type EffectDefinition,
	type SequencePropsSubscriptionKey,
} from 'remotion';
import {
	SCHEMA_FIELD_ROW_HEIGHT,
	getEffectFieldsToShow,
	getFieldsToShow,
	getSchemaFieldGroup,
} from '../schema-field-info';

const effect = {
	type: 'dev.remotion.test.effect',
	label: 'Test effect',
	documentationLink: null,
	backend: '2d',
	calculateKey: () => 'test',
	setup: () => null,
	apply: () => undefined,
	cleanup: () => undefined,
	validateParams: () => undefined,
	schema: {
		colorMode: {
			type: 'enum',
			default: 'solid' as const,
			variants: {
				solid: {
					dotColor: {
						type: 'color',
						default: 'red',
					},
				},
				source: {},
			},
		},
		position: {
			type: 'uv-coordinate',
			default: [0, 0.5] as const,
			description: 'Position',
		},
		colors: {
			type: 'array',
			item: {
				type: 'color',
			},
			default: undefined,
			minLength: 2,
			newItemDefault: '#ff0000',
			description: 'Colors',
			keyframable: false,
		},
	},
} satisfies EffectDefinition<unknown>;

const nodePath: SequencePropsSubscriptionKey = {
	absolutePath: 'Comp',
	nodePath: [],
	sequenceKeys: [],
	effectKeys: [],
	videoConfigValues: null,
};

const borderRadiusSchema = {
	'style.borderRadius': {
		type: 'number' as const,
		default: undefined,
		hiddenFromList: false,
	},
	'style.borderTopLeftRadius': {
		type: 'number' as const,
		default: undefined,
		hiddenFromList: false,
	},
	'style.borderTopRightRadius': {
		type: 'number' as const,
		default: undefined,
		hiddenFromList: false,
	},
	'style.borderBottomRightRadius': {
		type: 'number' as const,
		default: undefined,
		hiddenFromList: false,
	},
	'style.borderBottomLeftRadius': {
		type: 'number' as const,
		default: undefined,
		hiddenFromList: false,
	},
};

const getBorderRadiusFields = (
	props: Record<
		string,
		| {
				status: 'static';
				keyframeDisplayOffsetAdjustment: null;
				codeValue: unknown;
		  }
		| {status: 'computed'}
		| {
				status: 'keyframed';
				keyframeDisplayOffsetAdjustment: null;
				interpolationFunction: 'interpolate';
				keyframes: {frame: number; value: number}[];
				easing: [];
				clamping: {left: 'extend'; right: 'extend'};
				posterize: undefined;
				output: undefined;
		  }
	>,
) =>
	getFieldsToShow({
		schema: borderRadiusSchema,
		currentRuntimeValueDotNotation: {},
		getDragOverrides: () => ({}),
		propStatuses: {
			[Internals.makeSequencePropsSubscriptionKey(nodePath)]: {
				durationDependentKeyframes: null,
				canUpdate: true,
				props,
				effects: [],
			},
		},
		nodePath,
	})?.map((field) => field.key);

test('getEffectFieldsToShow uses the active enum variant', () => {
	const fields = getEffectFieldsToShow({
		effect,
		effectIndex: 0,
		nodePath,
		propStatuses: {
			[Internals.makeSequencePropsSubscriptionKey(nodePath)]: {
				durationDependentKeyframes: null,
				canUpdate: true,
				props: {},
				effects: [
					{
						canUpdate: true,
						callee: 'halftone',
						importPath: null,
						effectIndex: 0,
						props: {
							colorMode: {
								status: 'static',
								keyframeDisplayOffsetAdjustment: null,
								codeValue: 'source',
							},
						},
					},
				],
			},
		},
		getEffectDragOverrides: () => ({}),
	});

	expect(fields.map((field) => field.key)).toEqual([
		'colorMode',
		'position',
		'colors',
	]);
});

test('getEffectFieldsToShow uses default enum variant if no code value exists', () => {
	const fields = getEffectFieldsToShow({
		effect,
		effectIndex: 0,
		nodePath: null,
		propStatuses: {},
		getEffectDragOverrides: () => ({}),
	});

	expect(fields.map((field) => field.key)).toEqual([
		'colorMode',
		'dotColor',
		'position',
		'colors',
	]);
});

test('getEffectFieldsToShow returns array fields', () => {
	const fields = getEffectFieldsToShow({
		effect,
		effectIndex: 0,
		nodePath: null,
		propStatuses: {},
		getEffectDragOverrides: () => ({}),
	});

	const colors = fields.find((field) => field.key === 'colors');
	expect(colors?.typeName).toBe('array');
	expect(colors?.rowHeight).toBe(SCHEMA_FIELD_ROW_HEIGHT * 3);
});

test('getEffectFieldsToShow sizes array fields from the current value', () => {
	const fields = getEffectFieldsToShow({
		effect,
		effectIndex: 0,
		nodePath,
		propStatuses: {
			[Internals.makeSequencePropsSubscriptionKey(nodePath)]: {
				durationDependentKeyframes: null,
				canUpdate: true,
				props: {},
				effects: [
					{
						canUpdate: true,
						callee: 'starburst',
						importPath: null,
						effectIndex: 0,
						props: {
							colors: {
								status: 'static',
								keyframeDisplayOffsetAdjustment: null,
								codeValue: ['red', 'green', 'blue'],
							},
						},
					},
				],
			},
		},
		getEffectDragOverrides: () => ({}),
	});

	const colors = fields.find((field) => field.key === 'colors');
	expect(colors?.rowHeight).toBe(SCHEMA_FIELD_ROW_HEIGHT * 4);
});

test('getFieldsToShow leaves captions to the caption inspector', () => {
	const fields = getFieldsToShow({
		schema: {
			captions: {
				type: 'remotion-captions',
				default: undefined,
				keyframable: false,
			},
		},
		currentRuntimeValueDotNotation: {captions: []},
		getDragOverrides: () => ({}),
		propStatuses: {},
		nodePath,
	});

	expect(fields).toEqual([]);
});

test('getFieldsToShow sorts fields by inspector group order', () => {
	const fields = getFieldsToShow({
		schema: {
			src: {
				type: 'asset',
				default: undefined,
				keyframable: false,
			},
			'style.translate': {
				type: 'translate',
				default: '0px 0px',
			},
			playbackRate: {
				type: 'number',
				default: 1,
				hiddenFromList: false,
			},
			'style.fontSize': {
				type: 'number',
				default: undefined,
				hiddenFromList: false,
			},
			'style.color': {
				type: 'color',
				default: undefined,
			},
			'style.fontFamily': {
				type: 'font-family',
				default: undefined,
				keyframable: false,
			},
			'style.rotate': {
				type: 'rotation-css',
				default: '0deg',
			},
			'style.opacity': {
				type: 'number',
				default: 1,
				hiddenFromList: false,
			},
			cropLeft: {
				type: 'number',
				default: 0,
				hiddenFromList: false,
			},
			'style.backgroundColor': {
				type: 'color',
				default: 'transparent',
			},
			'style.borderWidth': {
				type: 'number',
				default: undefined,
				hiddenFromList: false,
			},
			'style.borderStyle': {
				type: 'enum',
				default: 'none',
				variants: {
					none: {},
					solid: {},
				},
			},
			'style.borderColor': {
				type: 'color',
				default: undefined,
			},
			'style.borderTopLeftRadius': {
				type: 'number',
				default: undefined,
				hiddenFromList: false,
			},
			volume: {
				type: 'number',
				default: 1,
				hiddenFromList: false,
			},
			layout: {
				type: 'enum',
				default: 'absolute-fill',
				variants: {
					'absolute-fill': {},
					none: {},
				},
			},
			premountFor: {
				type: 'number',
				default: 0,
				hiddenFromList: false,
			},
			'style.letterSpacing': {
				type: 'number',
				default: undefined,
				hiddenFromList: false,
			},
			'style.textAlign': {
				type: 'enum',
				default: 'left',
				variants: {
					left: {},
					center: {},
				},
			},
		},
		currentRuntimeValueDotNotation: {},
		getDragOverrides: () => ({}),
		propStatuses: {},
		nodePath,
	});

	expect(fields?.map((field) => field.key)).toEqual([
		'src',
		'playbackRate',
		'volume',
		'style.translate',
		'style.rotate',
		'style.opacity',
		'style.fontSize',
		'style.color',
		'style.fontFamily',
		'style.letterSpacing',
		'style.textAlign',
		'style.backgroundColor',
		'style.borderWidth',
		'style.borderStyle',
		'style.borderColor',
		'style.borderTopLeftRadius',
		'cropLeft',
		'layout',
		'premountFor',
	]);
	expect(fields?.map((field) => field.group)).toEqual([
		'source',
		'controls',
		'controls',
		'transforms',
		'transforms',
		'transforms',
		'text',
		'text',
		'text',
		'text',
		'text',
		'background',
		'border',
		'border',
		'border',
		'border-radius',
		'crop',
		'layout',
		'layout',
	]);
});

test('groups Sequence crop controls into the Crop inspector section', () => {
	expect(
		['cropLeft', 'cropRight', 'cropTop', 'cropBottom'].map(getSchemaFieldGroup),
	).toEqual(['crop', 'crop', 'crop', 'crop']);
});

test('groups border radius controls into their own inspector section', () => {
	expect(
		[
			'style.borderRadius',
			'style.borderTopLeftRadius',
			'style.borderTopRightRadius',
			'style.borderBottomRightRadius',
			'style.borderBottomLeftRadius',
		].map(getSchemaFieldGroup),
	).toEqual([
		'border-radius',
		'border-radius',
		'border-radius',
		'border-radius',
		'border-radius',
	]);
});

test('getFieldsToShow selects one border radius representation', () => {
	expect(getBorderRadiusFields({})).toEqual(['style.borderRadius']);
	expect(
		getBorderRadiusFields({
			'style.borderRadius': {
				status: 'static',
				keyframeDisplayOffsetAdjustment: null,
				codeValue: 12,
			},
			'style.borderTopLeftRadius': {
				status: 'static',
				keyframeDisplayOffsetAdjustment: null,
				codeValue: 12,
			},
			'style.borderTopRightRadius': {
				status: 'static',
				keyframeDisplayOffsetAdjustment: null,
				codeValue: 12,
			},
			'style.borderBottomRightRadius': {
				status: 'static',
				keyframeDisplayOffsetAdjustment: null,
				codeValue: 12,
			},
			'style.borderBottomLeftRadius': {
				status: 'static',
				keyframeDisplayOffsetAdjustment: null,
				codeValue: 12,
			},
		}),
	).toEqual(['style.borderRadius']);
	expect(
		getBorderRadiusFields({
			'style.borderRadius': {
				status: 'static',
				keyframeDisplayOffsetAdjustment: null,
				codeValue: undefined,
			},
			'style.borderTopLeftRadius': {
				status: 'static',
				keyframeDisplayOffsetAdjustment: null,
				codeValue: 4,
			},
			'style.borderTopRightRadius': {
				status: 'static',
				keyframeDisplayOffsetAdjustment: null,
				codeValue: 8,
			},
			'style.borderBottomRightRadius': {
				status: 'static',
				keyframeDisplayOffsetAdjustment: null,
				codeValue: 12,
			},
			'style.borderBottomLeftRadius': {
				status: 'static',
				keyframeDisplayOffsetAdjustment: null,
				codeValue: 16,
			},
		}),
	).toEqual([
		'style.borderTopLeftRadius',
		'style.borderTopRightRadius',
		'style.borderBottomRightRadius',
		'style.borderBottomLeftRadius',
	]);
	expect(
		getBorderRadiusFields({
			'style.borderRadius': {
				status: 'keyframed',
				keyframeDisplayOffsetAdjustment: null,
				interpolationFunction: 'interpolate',
				keyframes: [
					{frame: 0, value: 0},
					{frame: 30, value: 20},
				],
				easing: [],
				clamping: {left: 'extend', right: 'extend'},
				posterize: undefined,
				output: undefined,
			},
		}),
	).toEqual(['style.borderRadius']);
	expect(
		getBorderRadiusFields(
			Object.fromEntries(
				Object.keys(borderRadiusSchema).map((key) => [
					key,
					{status: 'computed'},
				]),
			),
		),
	).toEqual(['style.borderRadius']);
});

test('groups Sequence layout controls into the final Layout inspector section', () => {
	expect(['layout', 'premountFor'].map(getSchemaFieldGroup)).toEqual([
		'layout',
		'layout',
	]);
});

test('getFieldsToShow does not reserve extra height for text content fields', () => {
	const fields = getFieldsToShow({
		schema: {
			children: {
				type: 'text-content',
				default: '',
			},
		},
		currentRuntimeValueDotNotation: {},
		getDragOverrides: () => ({}),
		propStatuses: {},
		nodePath,
		includeTextContent: true,
	});

	expect(fields?.[0].rowHeight).toBe(SCHEMA_FIELD_ROW_HEIGHT);
});

test('getEffectFieldsToShow sorts fields by inspector group order', () => {
	const fields = getEffectFieldsToShow({
		effect: {
			...effect,
			schema: {
				'style.scale': {
					type: 'scale',
					default: 1,
				},
				intensity: {
					type: 'number',
					default: 1,
					hiddenFromList: false,
				},
				'style.fontWeight': {
					type: 'enum',
					default: '400',
					variants: {
						'400': {},
						'700': {},
					},
				},
				'style.translate': {
					type: 'translate',
					default: '0px 0px',
				},
			},
		},
		effectIndex: 0,
		nodePath: null,
		propStatuses: {},
		getEffectDragOverrides: () => ({}),
	});

	expect(fields.map((field) => field.key)).toEqual([
		'intensity',
		'style.scale',
		'style.translate',
		'style.fontWeight',
	]);
});