Skip to content

🎥 Make videos programmatically with React

11.5 KB
Raw Blame
import {afterEach, expect, mock, test} from 'bun:test';
import type {GitSource} from '@remotion/studio-shared';
import type {_InternalTypes, ResolvedStackLocation} from 'remotion';
import {
	getCompositionContextMenuItems,
	getCompositionMenuItems,
} from '../components/composition-menu-items';
import {makeSearchResults} from '../helpers/use-menu-structure';

const originalWindowDescriptor = Object.getOwnPropertyDescriptor(
	globalThis,
	'window',
);
const originalNavigatorDescriptor = Object.getOwnPropertyDescriptor(
	globalThis,
	'navigator',
);

afterEach(() => {
	if (originalWindowDescriptor) {
		Object.defineProperty(globalThis, 'window', originalWindowDescriptor);
	} else {
		Reflect.deleteProperty(globalThis, 'window');
	}

	if (originalNavigatorDescriptor) {
		Object.defineProperty(globalThis, 'navigator', originalNavigatorDescriptor);
	} else {
		Reflect.deleteProperty(globalThis, 'navigator');
	}
});

const installTestWindow = () => {
	Object.defineProperty(globalThis, 'window', {
		configurable: true,
		value: {
			remotion_cwd: '/project',
			remotion_editorName: null,
		},
	});
};

const installTestWindowWithEditor = () => {
	Object.defineProperty(globalThis, 'window', {
		configurable: true,
		value: {
			remotion_cwd: '/project',
			remotion_editorName: 'Code',
		},
	});
};

const installTestWindowWithGitSource = ({
	readOnly,
}: {
	readonly readOnly: boolean;
}) => {
	const openedUrls: string[] = [];
	const gitSource: GitSource = {
		name: 'project',
		org: 'example',
		ref: 'main',
		relativeFromGitRoot: '',
		type: 'github',
	};
	Object.defineProperty(globalThis, 'window', {
		configurable: true,
		value: {
			open: (url: string) => {
				openedUrls.push(url);
				return null;
			},
			remotion_cwd: '/project',
			remotion_editorName: readOnly ? null : 'Code',
			remotion_gitSource: gitSource,
			remotion_isReadOnlyStudio: readOnly,
		},
	});

	return openedUrls;
};

const composition = {
	id: 'ConnectedComposition',
	durationInFrames: 100,
} as _InternalTypes['AnyComposition'];

const resolvedLocation: ResolvedStackLocation = {
	column: 1,
	line: 10,
	source: '/project/src/Composition.tsx',
};

const commonArgs = {
	closeMenu: () => undefined,
	composition,
	connectionStatus: 'connected' as const,
	readOnlyStudio: false,
	resolvedLocation: null,
	setSelectedModal: () => undefined,
	openInApps: {
		canOpenDesktopApps: true,
		canOpenInEditor: true,
		codingAgentInfo: null,
		defaultEditorId: 'vscode' as const,
		defaultEditorName: 'Code',
		editorInfo: {
			defaultEditor: 'vscode' as const,
			runningEditors: null,
			installedEditors: [
				{id: 'vscode' as const, name: 'Code', nameWithType: 'Code'},
			],
		},
		onConfigureApps: null,
	},
};

const ids = (items: ReturnType<typeof getCompositionMenuItems>) =>
	items.map((item) => item.id);

test('composition menus exclude creation and include management actions', () => {
	installTestWindow();

	const menuItems = getCompositionMenuItems({
		...commonArgs,
		includeCompositionManagementItems: true,
	});
	const items = getCompositionContextMenuItems({
		...commonArgs,
		includeCompositionManagementItems: true,
	});

	expect(ids(menuItems)).not.toContain('new');
	expect(ids(items)).toContain('rename');
	expect(ids(items)).toContain('duplicate');
	expect(ids(items)).toContain('delete');
});

test('connected composition context menus omit management actions', () => {
	installTestWindow();

	const items = getCompositionContextMenuItems({
		...commonArgs,
		includeCompositionManagementItems: false,
		openInApps: {
			...commonArgs.openInApps,
			canOpenInEditor: false,
			defaultEditorId: null,
			defaultEditorName: null,
			editorInfo: null,
		},
	});

	expect(ids(items)).toEqual([
		'open-in-new-window',
		'open-in-new-window-divider',
		'copy-context-for-agents',
		'copy-file-location',
		'copy-id',
	]);
	const copyContext = items.find(
		(item) => item.id === 'copy-context-for-agents',
	);
	if (copyContext?.type !== 'item') {
		throw new Error('Expected copy context to be a menu item');
	}

	expect(copyContext.disabled).toBe(true);
});

test('copy actions are adjacent', () => {
	installTestWindow();

	const items = getCompositionMenuItems({
		...commonArgs,
		includeCompositionManagementItems: true,
	});
	const copyContextIndex = items.findIndex(
		(item) => item.id === 'copy-context-for-agents',
	);
	const copyFileLocationIndex = items.findIndex(
		(item) => item.id === 'copy-file-location',
	);
	const copyIdIndex = items.findIndex((item) => item.id === 'copy-id');

	expect(copyFileLocationIndex).toBe(copyContextIndex + 1);
	expect(copyIdIndex).toBe(copyFileLocationIndex + 1);
});

test('copies composition context for agents', async () => {
	installTestWindow();
	const writeText = mock(() => Promise.resolve());
	Object.defineProperty(globalThis, 'navigator', {
		configurable: true,
		value: {clipboard: {writeText}},
	});

	const items = getCompositionMenuItems({
		...commonArgs,
		includeCompositionManagementItems: false,
		resolvedLocation,
	});
	const copyContext = items.find(
		(item) => item.id === 'copy-context-for-agents',
	);
	if (copyContext?.type !== 'item') {
		throw new Error('Expected copy context to be a menu item');
	}

	copyContext.onClick(copyContext.id, null);
	await Promise.resolve();
	expect(writeText).toHaveBeenCalledWith(
		'ConnectedComposition in src/Composition.tsx:10',
	);
});

test('editor actions use Open labels and are adjacent', () => {
	installTestWindowWithEditor();

	const items = getCompositionContextMenuItems({
		...commonArgs,
		includeCompositionManagementItems: false,
	});
	const compositionIndex = items.findIndex(
		(item) => item.id === 'show-in-editor',
	);
	const componentIndex = items.findIndex(
		(item) => item.id === 'open-component-in-editor',
	);

	expect(componentIndex).toBe(compositionIndex + 1);
	const compositionItem = items[compositionIndex];
	const componentItem = items[componentIndex];
	if (compositionItem.type !== 'item' || componentItem.type !== 'item') {
		throw new Error('Expected editor actions');
	}

	expect(compositionItem.label).toBe('Open composition in Code');
	expect(componentItem.label).toBe('Open component in Code');
});

test('read-only composition menus keep navigation and copy actions enabled', () => {
	installTestWindowWithGitSource({readOnly: true});

	const items = getCompositionContextMenuItems({
		...commonArgs,
		includeCompositionManagementItems: true,
		openInApps: {
			...commonArgs.openInApps,
			canOpenDesktopApps: false,
			canOpenInEditor: false,
			defaultEditorId: null,
			defaultEditorName: null,
			editorInfo: null,
		},
		readOnlyStudio: true,
		resolvedLocation,
	});
	const itemById = (id: string) => {
		const item = items.find((candidate) => candidate.id === id);
		if (item?.type !== 'item') {
			throw new Error(`Expected ${id} to be a menu item`);
		}

		return item;
	};

	expect(itemById('open-in-new-window').disabled).not.toBe(true);
	expect(itemById('open-composition-in-git-source').disabled).toBe(false);
	expect(itemById('open-component-in-git-source').disabled).toBe(false);
	expect(ids(items)).not.toContain('show-in-editor');
	expect(ids(items)).not.toContain('open-composition-in-another-app');
	expect(ids(items)).not.toContain('open-component-in-another-app');
	expect(itemById('copy-context-for-agents').disabled).toBe(false);
	expect(itemById('copy-file-location').disabled).toBe(false);
	expect(itemById('copy-id').disabled).toBe(false);
	expect(itemById('rename').disabled).toBe(true);
	expect(itemById('duplicate').disabled).toBe(true);
	expect(itemById('delete').disabled).toBe(true);
});

test('composition context menus can open source locations on GitHub', () => {
	const openedUrls = installTestWindowWithGitSource({readOnly: true});
	const items = getCompositionContextMenuItems({
		...commonArgs,
		includeCompositionManagementItems: true,
		openInApps: {
			...commonArgs.openInApps,
			canOpenDesktopApps: false,
			canOpenInEditor: false,
			defaultEditorId: null,
			defaultEditorName: null,
			editorInfo: null,
		},
		readOnlyStudio: true,
		resolvedLocation,
	});
	const compositionItem = items.find(
		(item) => item.id === 'open-composition-in-git-source',
	);
	const componentItem = items.find(
		(item) => item.id === 'open-component-in-git-source',
	);
	if (compositionItem?.type !== 'item' || componentItem?.type !== 'item') {
		throw new Error('Expected GitHub composition actions');
	}

	expect(compositionItem.label).toBe('Open composition in GitHub');
	expect(componentItem.label).toBe('Open component in GitHub');
	expect(compositionItem.disabled).toBe(false);
	expect(componentItem.disabled).toBe(false);
	compositionItem.onClick(compositionItem.id, null);
	expect(openedUrls).toEqual([
		'https://github.com/example/project/blob/main/src/Composition.tsx#L10',
	]);
});

test('interactive composition context menus list GitHub as an alternative', () => {
	installTestWindowWithGitSource({readOnly: false});
	const items = getCompositionContextMenuItems({
		...commonArgs,
		includeCompositionManagementItems: false,
		openInApps: {
			...commonArgs.openInApps,
			codingAgentInfo: {
				defaultCodingAgent: 'cursor',
				runningCodingAgents: null,
				installedCodingAgents: [
					{
						id: 'cursor',
						name: 'Cursor',
						nameWithType: 'Cursor Agent',
					},
				],
				installedGitClients: [],
				installedTerminals: [],
			},
			editorInfo: {
				defaultEditor: 'vscode',
				runningEditors: null,
				installedEditors: [
					{id: 'vscode', name: 'Code', nameWithType: 'Code'},
					{id: 'cursor', name: 'Cursor', nameWithType: 'Cursor Editor'},
				],
			},
			onConfigureApps: () => undefined,
		},
		resolvedLocation,
	});

	expect(ids(items)).toContain('show-in-editor');
	expect(ids(items)).not.toContain('open-composition-in-git-source');
	expect(ids(items)).not.toContain('open-component-in-git-source');

	const compositionAlternatives = items.find(
		(item) => item.id === 'open-composition-in-another-app',
	);
	const componentAlternatives = items.find(
		(item) => item.id === 'open-component-in-another-app',
	);
	if (
		compositionAlternatives?.type !== 'item' ||
		compositionAlternatives.subMenu === null ||
		componentAlternatives?.type !== 'item' ||
		componentAlternatives.subMenu === null
	) {
		throw new Error('Expected composition and component Open in submenus');
	}

	expect(compositionAlternatives.subMenu.items.map((item) => item.id)).toEqual(
		expect.arrayContaining([
			'open-in-cursor',
			'open-in-coding-agent-cursor',
			'open-in-github',
			'change-default-apps',
		]),
	);
	expect(componentAlternatives.subMenu.items.map((item) => item.id)).toEqual(
		expect.arrayContaining([
			'open-in-cursor',
			'open-in-coding-agent-cursor',
			'open-in-github',
			'change-default-apps',
		]),
	);

	const searchResults = makeSearchResults(
		[
			{
				id: 'composition',
				label: 'Composition',
				leaveLeftPadding: false,
				items,
			},
		],
		() => undefined,
	);
	expect(searchResults.map((result) => result.title)).toContain(
		'Composition: Open in Cursor',
	);
	expect(searchResults.map((result) => result.title)).toContain(
		'Component: Open in Cursor',
	);

	const mobileSearchResults = makeSearchResults(
		[
			{
				id: 'remotion',
				label: 'Remotion',
				leaveLeftPadding: false,
				items: [
					{
						id: 'composition',
						value: 'composition',
						label: 'Composition',
						quickSwitcherLabel: null,
						type: 'item',
						onClick: () => undefined,
						keyHint: null,
						leftItem: null,
						subMenu: {
							items,
							leaveLeftSpace: true,
							preselectIndex: 0,
						},
					},
				],
			},
		],
		() => undefined,
	);
	expect(mobileSearchResults.map((result) => result.title)).toContain(
		'Composition: Open in Cursor',
	);
});