Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## [Unreleased]

### Fixed

- Write simulator recordings to a tool-managed temporary file instead of the current working directory, so a recording no longer grows inside the Git checkout before it is exported ([#577](https://github.com/getsentry/MobileBuildMCP/issues/577)).

## [2.7.1]

### Changed
Expand Down
112 changes: 112 additions & 0 deletions src/utils/__tests__/video_capture.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import { EventEmitter } from 'node:events';
import { rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { afterEach, describe, expect, it } from 'vitest';

import {
listActiveVideoCaptureSessionIds,
startSimulatorVideoCapture,
stopSimulatorVideoCapture,
} from '../video_capture.ts';
import type { CommandExecutor } from '../execution/index.ts';

const AXE_HELPERS = {
getAxePath: () => '/fake/axe',
getBundledAxeEnvironment: () => ({}),
};

const managedPaths: string[] = [];

function createFakeChild(): EventEmitter & Record<string, unknown> {
const child = new EventEmitter() as EventEmitter & Record<string, unknown>;
child.stdout = new EventEmitter();
child.stderr = new EventEmitter();
child.exitCode = 0;
child.signalCode = null;
child.kill = () => {};
return child;
}

function createExecutor(child: EventEmitter, commands: string[][]): CommandExecutor {
return (async (command: string[]) => {
commands.push(command);
return { success: true, process: child };
}) as unknown as CommandExecutor;
}

function outputPathFrom(command: string[]): string {
const index = command.indexOf('--output');
expect(index).toBeGreaterThanOrEqual(0);
const path = command[index + 1];
managedPaths.push(path);
return path;
}

afterEach(() => {
for (const path of managedPaths.splice(0)) {
rmSync(path, { force: true });
}
});

describe('startSimulatorVideoCapture', () => {
it('writes the recording to a tool-managed path outside the working directory', async () => {
const simulatorUuid = '11111111-1111-1111-1111-111111111111';
const commands: string[][] = [];
const executor = createExecutor(createFakeChild(), commands);

const result = await startSimulatorVideoCapture(
{ simulatorUuid, fps: 30 },
executor,
AXE_HELPERS,
);

expect(result.started).toBe(true);
const command = commands[0];
const outputPath = outputPathFrom(command);

expect(command).toContain('record-video');
expect(outputPath.startsWith(tmpdir())).toBe(true);
expect(outputPath.endsWith('.mp4')).toBe(true);
expect(command).not.toContain('axe-video');

await stopSimulatorVideoCapture({ simulatorUuid }, executor);
});
});

describe('stopSimulatorVideoCapture', () => {
it('returns the managed recording path when the file exists', async () => {
const simulatorUuid = '22222222-2222-2222-2222-222222222222';
const commands: string[][] = [];
const executor = createExecutor(createFakeChild(), commands);

await startSimulatorVideoCapture({ simulatorUuid }, executor, AXE_HELPERS);
const outputPath = outputPathFrom(commands[0]);
writeFileSync(outputPath, 'video');

const result = await stopSimulatorVideoCapture({ simulatorUuid }, executor);

expect(result.stopped).toBe(true);
if (result.stopped) {
expect(result.parsedPath).toBe(outputPath);
}
expect(listActiveVideoCaptureSessionIds()).not.toContain(simulatorUuid);
});

it('falls back to the path printed by AXe when the managed file is missing', async () => {
const simulatorUuid = '33333333-3333-3333-3333-333333333333';
const commands: string[][] = [];
const child = createFakeChild();
const executor = createExecutor(child, commands);

await startSimulatorVideoCapture({ simulatorUuid }, executor, AXE_HELPERS);
const axePath = `/tmp/axe-video-${simulatorUuid}.mp4`;
(child.stdout as EventEmitter).emit('data', `${axePath}\n`);

const result = await stopSimulatorVideoCapture({ simulatorUuid }, executor);

expect(result.stopped).toBe(true);
if (result.stopped) {
expect(result.parsedPath).toBe(axePath);
}
});
});
29 changes: 25 additions & 4 deletions src/utils/video_capture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
*/

import type { ChildProcess } from 'child_process';
import { existsSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { log } from './logging/index.ts';
import { getAxePath, getBundledAxeEnvironment } from './axe-helpers.ts';
import type { CommandExecutor } from './execution/index.ts';
Expand All @@ -17,6 +20,7 @@ type Session = {
startedAt: number;
buffer: string;
ended: boolean;
outputPath: string;
releaseActivity?: () => void;
};

Expand Down Expand Up @@ -109,14 +113,17 @@ async function stopSession(
await waitForChildToStop(session, options.timeoutMs ?? 5000);

const combinedOutput = session.buffer;
const parsedPath = parseLastAbsoluteMp4Path(combinedOutput) ?? undefined;
const recordedPath =
session.outputPath && existsSync(session.outputPath)
? session.outputPath
: (parseLastAbsoluteMp4Path(combinedOutput) ?? undefined);

session.releaseActivity?.();

return {
sessionId: session.sessionId,
stdout: combinedOutput,
parsedPath,
parsedPath: recordedPath,
};
}

Expand Down Expand Up @@ -183,10 +190,23 @@ export async function startSimulatorVideoCapture(
}

const fps = Number.isFinite(params.fps as number) ? Number(params.fps) : 30;
const command = [axeBinary, 'record-video', '--udid', simulatorUuid, '--fps', String(fps)];
const outputPath = join(tmpdir(), `mobilebuildmcp-${simulatorUuid}-${Date.now()}.mp4`);
const command = [
axeBinary,
'record-video',
'--udid',
simulatorUuid,
'--fps',
String(fps),
'--output',
outputPath,
];
const env = helpers.getBundledAxeEnvironment?.() ?? {};

log('info', `Starting AXe video recording for simulator ${simulatorUuid} at ${fps} fps`);
log(
'info',
`Starting AXe video recording for simulator ${simulatorUuid} at ${fps} fps to ${outputPath}`,
);

const result = await executor(command, 'Start Simulator Video Capture', false, { env }, true);

Expand All @@ -204,6 +224,7 @@ export async function startSimulatorVideoCapture(
startedAt: Date.now(),
buffer: '',
ended: false,
outputPath,
releaseActivity: acquireDaemonActivity('video.capture'),
};

Expand Down