Skip to content

🎥 Make videos programmatically with React

7.9 KB
Raw Blame
import {expect, test} from 'bun:test';
import {
	chmodSync,
	existsSync,
	mkdirSync,
	mkdtempSync,
	readFileSync,
	realpathSync,
	rmSync,
	writeFileSync,
} from 'node:fs';
import {tmpdir} from 'node:os';
import path from 'node:path';
import {
	getCodingAgentLaunchCommand,
	getCodingAgentLaunchCommands,
	launchCodingAgent,
} from '../helpers/coding-agent-registry';

test('constructs macOS coding agent launch commands', () => {
	const chatGPT = {
		applicationPath: '/Applications/ChatGPT.app',
		id: 'codex',
		launchMode: 'direct',
		name: 'ChatGPT',
		nameWithType: 'ChatGPT',
		platform: 'darwin',
		terminal: null,
	} as const;

	expect(
		getCodingAgentLaunchCommand({
			codingAgent: chatGPT,
			projectPath: '/Users/test/My Project',
		}),
	).toEqual({
		command: 'open',
		args: ['codex://new?path=%2FUsers%2Ftest%2FMy+Project'],
		cwd: null,
	});
	expect(
		getCodingAgentLaunchCommands({
			codingAgent: chatGPT,
			projectPath: '/Users/test/My Project',
			prompt: null,
		}),
	).toEqual([
		{
			command: 'open',
			args: ['codex://new?path=%2FUsers%2Ftest%2FMy+Project'],
			cwd: null,
			waitForExit: false,
		},
	]);

	expect(
		getCodingAgentLaunchCommand({
			codingAgent: {
				applicationPath: '/Applications/Cursor.app',
				id: 'cursor',
				launchMode: 'direct',
				name: 'Cursor',
				nameWithType: 'Cursor Agent',
				platform: 'darwin',
				terminal: null,
			},
			projectPath: '/Users/test/My Project',
		}),
	).toEqual({
		command: '/Applications/Cursor.app/Contents/Resources/app/bin/cursor',
		args: ['--glass', '--suppress-popups-on-startup', '/Users/test/My Project'],
		cwd: null,
	});

	for (const codingAgent of [
		{
			applicationPath: '/Applications/GitHub Copilot.app',
			id: 'copilot' as const,
			launchMode: 'direct' as const,
			name: 'GitHub Copilot',
			nameWithType: 'GitHub Copilot',
			platform: 'darwin' as const,
			terminal: null,
		},
		{
			applicationPath: '/Applications/Claude.app',
			id: 'claude-code' as const,
			launchMode: 'direct' as const,
			name: 'Claude Code',
			nameWithType: 'Claude Code',
			platform: 'darwin' as const,
			terminal: null,
		},
	]) {
		expect(
			getCodingAgentLaunchCommand({
				codingAgent,
				projectPath: '/Users/test/My Project',
			}),
		).toEqual({
			command: 'open',
			args: ['-a', codingAgent.applicationPath, '/Users/test/My Project'],
			cwd: null,
		});
	}
});

test('constructs Linux coding agent launch commands', () => {
	expect(
		getCodingAgentLaunchCommand({
			codingAgent: {
				applicationPath: '/usr/local/bin/codex',
				id: 'codex',
				launchMode: 'terminal',
				name: 'Codex',
				nameWithType: 'Codex',
				platform: 'linux',
				terminal: {
					id: 'gnome-terminal',
					path: '/usr/bin/gnome-terminal',
				},
			},
			projectPath: '/home/test/My Project',
		}),
	).toEqual({
		command: '/usr/bin/gnome-terminal',
		args: [
			'--working-directory=/home/test/My Project',
			'--',
			'/usr/local/bin/codex',
		],
		cwd: '/home/test/My Project',
	});

	expect(
		getCodingAgentLaunchCommand({
			codingAgent: {
				applicationPath: '/usr/bin/cursor',
				id: 'cursor',
				launchMode: 'direct',
				name: 'Cursor',
				nameWithType: 'Cursor Agent',
				platform: 'linux',
				terminal: null,
			},
			projectPath: '/home/test/My Project',
		}),
	).toEqual({
		command: '/usr/bin/cursor',
		args: ['--glass', '--suppress-popups-on-startup', '/home/test/My Project'],
		cwd: null,
	});
});

test('constructs Windows coding agent launch commands', () => {
	expect(
		getCodingAgentLaunchCommand({
			codingAgent: {
				applicationPath: 'C:\\Tools\\copilot.cmd',
				id: 'copilot',
				launchMode: 'terminal',
				name: 'GitHub Copilot',
				nameWithType: 'GitHub Copilot',
				platform: 'win32',
				terminal: {
					id: 'cmd',
					path: 'C:\\Windows\\System32\\cmd.exe',
				},
			},
			projectPath: 'C:\\Users\\test\\My Project',
		}),
	).toEqual({
		command: 'C:\\Windows\\System32\\cmd.exe',
		args: [
			'/d',
			'/s',
			'/c',
			'start',
			'',
			'/d',
			'C:\\Users\\test\\My Project',
			'C:\\Windows\\System32\\cmd.exe',
			'/k',
			'C:\\Tools\\copilot.cmd',
		],
		cwd: null,
	});
});

test('prefills editable macOS coding agents without submitting to Copilot', () => {
	const projectPath = '/Users/test/My Project';
	const prompt = 'MySequence in src/Comp.tsx:42';

	expect(
		getCodingAgentLaunchCommands({
			codingAgent: {
				applicationPath: '/Applications/ChatGPT.app',
				id: 'codex',
				launchMode: 'direct',
				name: 'ChatGPT',
				nameWithType: 'ChatGPT',
				platform: 'darwin',
				terminal: null,
			},
			projectPath,
			prompt,
		}),
	).toEqual([
		{
			command: 'open',
			args: [
				'codex://new?path=%2FUsers%2Ftest%2FMy+Project&prompt=MySequence+in+src%2FComp.tsx%3A42',
			],
			cwd: null,
			waitForExit: false,
		},
	]);

	expect(
		getCodingAgentLaunchCommands({
			codingAgent: {
				applicationPath: '/Applications/Cursor.app',
				id: 'cursor',
				launchMode: 'direct',
				name: 'Cursor',
				nameWithType: 'Cursor Agent',
				platform: 'darwin',
				terminal: null,
			},
			projectPath,
			prompt,
		}),
	).toEqual([
		{
			command: '/Applications/Cursor.app/Contents/Resources/app/bin/cursor',
			args: ['--glass', '--suppress-popups-on-startup', projectPath],
			cwd: null,
			waitForExit: true,
		},
		{
			command: 'open',
			args: [
				'cursor://anysphere.cursor-deeplink/prompt?text=MySequence+in+src%2FComp.tsx%3A42',
			],
			cwd: null,
			waitForExit: false,
		},
	]);

	expect(
		getCodingAgentLaunchCommands({
			codingAgent: {
				applicationPath: '/Applications/Claude.app',
				id: 'claude-code',
				launchMode: 'direct',
				name: 'Claude Code',
				nameWithType: 'Claude Code',
				platform: 'darwin',
				terminal: null,
			},
			projectPath,
			prompt,
		}),
	).toEqual([
		{
			command: 'open',
			args: [
				'claude://code/new?folder=%2FUsers%2Ftest%2FMy+Project&q=MySequence+in+src%2FComp.tsx%3A42',
			],
			cwd: null,
			waitForExit: false,
		},
	]);

	expect(
		getCodingAgentLaunchCommands({
			codingAgent: {
				applicationPath: '/Applications/GitHub Copilot.app',
				id: 'copilot',
				launchMode: 'direct',
				name: 'GitHub Copilot',
				nameWithType: 'GitHub Copilot',
				platform: 'darwin',
				terminal: null,
			},
			projectPath,
			prompt,
		}),
	).toEqual([
		{
			command: 'open',
			args: ['-a', '/Applications/GitHub Copilot.app', projectPath],
			cwd: null,
			waitForExit: false,
		},
	]);
});

test.skipIf(process.platform === 'win32')(
	'launches a terminal coding agent in the project directory',
	async () => {
		const directory = mkdtempSync(
			path.join(tmpdir(), 'remotion-terminal-coding-agent-launch-'),
		);
		const projectPath = path.join(directory, 'My Project');
		const applicationPath = path.join(directory, 'codex');
		const terminalPath = path.join(directory, 'gnome-terminal');
		const output = path.join(directory, 'received.txt');
		mkdirSync(projectPath);
		writeFileSync(
			applicationPath,
			`#!/bin/sh\nprintf "%s\\n" "$PWD" > "${output}"\n`,
		);
		writeFileSync(
			terminalPath,
			'#!/bin/sh\nwhile [ "$#" -gt 0 ]; do\n  if [ "$1" = "--" ]; then\n    shift\n    exec "$@"\n  fi\n  shift\ndone\n',
		);
		chmodSync(applicationPath, 0o755);
		chmodSync(terminalPath, 0o755);

		try {
			const success = await launchCodingAgent({
				codingAgent: {
					applicationPath,
					id: 'codex',
					launchMode: 'terminal',
					name: 'Codex',
					nameWithType: 'Codex',
					platform: 'linux',
					terminal: {id: 'gnome-terminal', path: terminalPath},
				},
				logLevel: 'error',
				projectPath,
				prompt: null,
			});
			expect(success).toBe(true);

			const expectedOutput = `${realpathSync(projectPath)}\n`;
			let receivedOutput = '';
			for (let attempt = 0; attempt < 100; attempt++) {
				if (existsSync(output)) {
					receivedOutput = readFileSync(output, 'utf8');
					if (receivedOutput === expectedOutput) {
						break;
					}
				}

				await Bun.sleep(10);
			}

			expect(receivedOutput).toBe(expectedOutput);
		} finally {
			rmSync(directory, {force: true, recursive: true});
		}
	},
);