Skip to content

🎥 Make videos programmatically with React

9.5 KB
Raw Blame
import {expect, test} from 'bun:test';
import {mkdtempSync, readFileSync, rmSync, writeFileSync} from 'node:fs';
import {tmpdir} from 'node:os';
import {join} from 'node:path';
import type {EventSourceEvent} from '@remotion/studio-shared';
import type {InteractivitySchema} from 'remotion';
import {NoReactInternals} from 'remotion/no-react';
import {captureJsxNodePaths} from '../codemods/get-node-path-remappings';
import {parseAst} from '../codemods/parse-ast';
import {
	createFileWatcherRegistry,
	setFileWatcherRegistry,
} from '../file-watcher';
import {setLiveEventsListener} from '../preview-server/live-events';
import {
	addKeyframes,
	addKeyframesHandler,
} from '../preview-server/routes/add-keyframes';
import {
	clearUndoStackForTests,
	getUndoStack,
	popRedo,
	popUndo,
} from '../preview-server/undo-stack';
import {lineColumnToNodePath, lineContainingToNodePath} from './test-utils';

const input = `import {tint} from '@remotion/effects/tint';
import {AbsoluteFill, interpolate, useCurrentFrame} from 'remotion';

export const Comp = () => {
\tconst frame = useCurrentFrame();
\treturn (
\t\t<AbsoluteFill
\t\t\tstyle={{opacity: interpolate(frame, [0, 10], [0, 1])}}
\t\t\teffects={[tint({amount: interpolate(frame, [0, 20], [0.2, 0.8])})]}
\t\t/>
\t);
};
`;

const effectSchema = {
	amount: {
		type: 'number',
		default: 0,
		hiddenFromList: false,
	},
} satisfies InteractivitySchema;

const linearProgressiveBlurInput = `import {linearProgressiveBlur} from '@remotion/effects/linear-progressive-blur';
import {HtmlInCanvas} from '@remotion/html-in-canvas';
import {Solid} from 'remotion';

export const Comp = () => {
\treturn (
\t\t<HtmlInCanvas
\t\t\twidth={1280}
\t\t\theight={720}
\t\t\teffects={[
\t\t\t\tlinearProgressiveBlur({
\t\t\t\t\tstart: [0.5, 0.5],
\t\t\t\t\tend: [0.816, -0.06],
\t\t\t\t}),
\t\t\t]}
\t\t>
\t\t\t<Solid width={1280} height={720} color="white" />
\t\t</HtmlInCanvas>
\t);
};
`;

const linearProgressiveBlurSchema = {
	start: {
		type: 'uv-coordinate',
		default: [0, 0.5],
	},
	end: {
		type: 'uv-coordinate',
		default: [1, 0.5],
	},
	startBlur: {
		type: 'number',
		default: 0,
		hiddenFromList: false,
	},
	endBlur: {
		type: 'number',
		default: 50,
		hiddenFromList: false,
	},
} satisfies InteractivitySchema;

test('addKeyframes batches sequence and effect adds into one undo entry', async () => {
	clearUndoStackForTests();
	const cleanupFileWatcher = setFileWatcherRegistry(
		createFileWatcherRegistry(),
	);
	const cleanupLiveEvents = setLiveEventsListener({
		addNewClientListener: () => () => undefined,
		closeConnections: () => Promise.resolve(),
		router: () => Promise.resolve(),
		sendEventToClient: () => undefined,
		sendEventToClientId: () => true,
	});
	const dir = mkdtempSync(join(tmpdir(), 'remotion-add-keyframes-'));
	const fileName = 'Comp.tsx';
	const filePath = join(dir, fileName);
	const nodePath = {
		absolutePath: fileName,
		nodePath: lineColumnToNodePath(input, 7),
		sequenceKeys: ['style.opacity'],
		effectKeys: [['amount']],
		videoConfigValues: null,
	};

	try {
		writeFileSync(filePath, input);

		await addKeyframes({
			sequenceKeyframes: [
				{
					fileName,
					nodePath,
					key: 'style.opacity',
					frame: 5,
					value: JSON.stringify(0.5),
					schema: NoReactInternals.sequenceSchema,
				},
			],
			effectKeyframes: [
				{
					fileName,
					sequenceNodePath: nodePath,
					effectIndex: 0,
					key: 'amount',
					frame: 10,
					value: JSON.stringify(0.4),
					schema: effectSchema,
				},
			],
			clientId: 'test-client',
			remotionRoot: dir,
			logLevel: 'error',
		});

		const output = readFileSync(filePath, 'utf-8');
		expect(output).toContain(
			'opacity: interpolate(frame, [0, 5, 10], [0, 0.5, 1])',
		);
		expect(output).toContain(
			'amount: interpolate(frame, [0, 10, 20], [0.2, 0.4, 0.8])',
		);
		expect(getUndoStack()).toHaveLength(1);

		expect(popUndo()).toEqual({
			success: true,
			nodePathMutation: null,
			route: null,
		});
		expect(readFileSync(filePath, 'utf-8')).toBe(input);

		expect(popRedo()).toEqual({
			success: true,
			nodePathMutation: null,
			route: null,
		});
		expect(readFileSync(filePath, 'utf-8')).toBe(output);
	} finally {
		clearUndoStackForTests();
		cleanupLiveEvents();
		cleanupFileWatcher();
		rmSync(dir, {force: true, recursive: true});
	}
});

test('first keyframe in a concise-arrow component remaps its JSX node path for the next edit', async () => {
	clearUndoStackForTests();
	const cleanupFileWatcher = setFileWatcherRegistry(
		createFileWatcherRegistry(),
	);
	const events: EventSourceEvent[] = [];
	const cleanupLiveEvents = setLiveEventsListener({
		addNewClientListener: () => () => undefined,
		closeConnections: () => Promise.resolve(),
		router: () => Promise.resolve(),
		sendEventToClient: (event) => events.push(event),
		sendEventToClientId: () => true,
	});
	const dir = mkdtempSync(join(tmpdir(), 'remotion-element-color-keyframe-'));
	const fileName = 'Comp.tsx';
	const filePath = join(dir, fileName);
	const source = `import {Interactive, Sequence} from 'remotion';

const ColorCardInner = ({color, controls, durationInFrames, name}) => (
  <Sequence controls={controls} durationInFrames={durationInFrames} name={name} style={{backgroundColor: color}} />
);
const ColorCard = Interactive.withSchema({
  Component: ColorCardInner,
  componentName: '<ColorCard>',
  schema: {color: {type: 'color', default: '#ffffff'}},
  supportsEffects: false,
});
export const Target = () => (
  <ColorCard color="#ffffff" durationInFrames={60} name="Color card" />
);
`;
	const schema = {
		color: {type: 'color', default: '#ffffff'},
	} as const satisfies InteractivitySchema;
	const oldPath = lineContainingToNodePath(source, '<ColorCard color=');
	const nodePath = {
		absolutePath: filePath,
		assetKeys: [],
		effectKeys: [],
		nodePath: oldPath,
		sequenceKeys: ['color'],
		videoConfigValues: null,
	};
	const handlerContext = {
		binariesDirectory: null,
		configFile: null,
		entryPoint: filePath,
		getDefaultCodingAgent: () => null,
		getDefaultEditor: () => null,
		logLevel: 'error' as const,
		methods: {
			addJob: () => undefined,
			cancelJob: () => undefined,
			removeJob: () => undefined,
		},
		publicDir: dir,
		remotionRoot: dir,
		request: {} as never,
		response: {} as never,
	};

	try {
		writeFileSync(filePath, source);
		const first = await addKeyframesHandler({
			...handlerContext,
			input: {
				clientId: 'test-client',
				effectKeyframes: [],
				sequenceKeyframes: [
					{
						fileName,
						nodePath,
						key: 'color',
						frame: 0,
						value: JSON.stringify('#ffffff'),
						schema,
					},
				],
			},
		});
		const mutation = first.nodePathMutation;
		if (!mutation) {
			throw new Error('Expected a JSX node path mutation');
		}

		const remapping = mutation.files[0].remappings.find(
			(entry) => JSON.stringify(entry.oldNodePath) === JSON.stringify(oldPath),
		);
		if (!remapping?.newNodePath) {
			throw new Error('The ColorCard invocation was not remapped');
		}

		expect(events).toContainEqual({
			type: 'sequence-node-paths-remapped',
			mutation,
		});
		const afterFirst = readFileSync(filePath, 'utf-8');
		const colorCardNode = captureJsxNodePaths(parseAst(afterFirst)).at(-1);
		if (!colorCardNode) {
			throw new Error('ColorCard invocation missing after first keyframe');
		}

		expect(remapping.newNodePath).toEqual(colorCardNode.nodePath);
		const second = await addKeyframesHandler({
			...handlerContext,
			input: {
				clientId: 'test-client',
				effectKeyframes: [],
				sequenceKeyframes: [
					{
						fileName,
						nodePath: {...nodePath, nodePath: remapping.newNodePath},
						key: 'color',
						frame: 30,
						value: JSON.stringify('#ff0000'),
						schema,
					},
				],
			},
		});
		expect(second.success).toBe(true);
		const output = readFileSync(filePath, 'utf-8');
		expect(output).toContain("['#ffffff', '#ff0000']");
		expect(output).toContain('[0, 30]');
	} finally {
		clearUndoStackForTests();
		cleanupLiveEvents();
		cleanupFileWatcher();
		rmSync(dir, {force: true, recursive: true});
	}
});

test('addKeyframes adds a missing effect prop before keyframing it', async () => {
	clearUndoStackForTests();
	const cleanupFileWatcher = setFileWatcherRegistry(
		createFileWatcherRegistry(),
	);
	const cleanupLiveEvents = setLiveEventsListener({
		addNewClientListener: () => () => undefined,
		closeConnections: () => Promise.resolve(),
		router: () => Promise.resolve(),
		sendEventToClient: () => undefined,
		sendEventToClientId: () => true,
	});
	const dir = mkdtempSync(join(tmpdir(), 'remotion-add-missing-effect-'));
	const fileName = 'Comp.tsx';
	const filePath = join(dir, fileName);
	const nodePath = {
		absolutePath: fileName,
		nodePath: lineColumnToNodePath(linearProgressiveBlurInput, 7),
		sequenceKeys: [],
		effectKeys: [['start', 'end', 'startBlur', 'endBlur']],
		videoConfigValues: null,
	};

	try {
		writeFileSync(filePath, linearProgressiveBlurInput);

		await addKeyframes({
			sequenceKeyframes: [],
			effectKeyframes: [
				{
					fileName,
					sequenceNodePath: nodePath,
					effectIndex: 0,
					key: 'startBlur',
					frame: 55,
					value: JSON.stringify(12),
					schema: linearProgressiveBlurSchema,
				},
			],
			clientId: 'test-client',
			remotionRoot: dir,
			logLevel: 'error',
		});

		const output = readFileSync(filePath, 'utf-8');
		expect(output).toContain('startBlur: interpolate(frame, [55], [12], {');
		expect(output).toContain("extrapolateLeft: 'clamp'");
		expect(output).toContain("extrapolateRight: 'clamp'");
	} finally {
		clearUndoStackForTests();
		cleanupLiveEvents();
		cleanupFileWatcher();
		rmSync(dir, {force: true, recursive: true});
	}
});