From 38b8d5fdcece40ec3e352b798dccb6638d491e44 Mon Sep 17 00:00:00 2001 From: Nayan Bhut Date: Sat, 5 Dec 2026 10:09:22 +0530 Subject: [PATCH] fix(video): write recordings to a temp file record-video was started without --output, so AXe wrote axe-video-.mp4 into the current working directory. Pass an explicit path under os.tmpdir(), prefer it on stop, and keep the printed-path fallback for older AXe versions. --- CHANGELOG.md | 6 ++ src/utils/__tests__/video_capture.test.ts | 112 ++++++++++++++++++++++ src/utils/video_capture.ts | 29 +++++- 3 files changed, 143 insertions(+), 4 deletions(-) create mode 100644 src/utils/__tests__/video_capture.test.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index f47e609ad..1fe166a8b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/utils/__tests__/video_capture.test.ts b/src/utils/__tests__/video_capture.test.ts new file mode 100644 index 000000000..68339be3b --- /dev/null +++ b/src/utils/__tests__/video_capture.test.ts @@ -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 { + const child = new EventEmitter() as EventEmitter & Record; + 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); + } + }); +}); diff --git a/src/utils/video_capture.ts b/src/utils/video_capture.ts index 30208c07d..7cb5e5384 100644 --- a/src/utils/video_capture.ts +++ b/src/utils/video_capture.ts @@ -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'; @@ -17,6 +20,7 @@ type Session = { startedAt: number; buffer: string; ended: boolean; + outputPath: string; releaseActivity?: () => void; }; @@ -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, }; } @@ -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); @@ -204,6 +224,7 @@ export async function startSimulatorVideoCapture( startedAt: Date.now(), buffer: '', ended: false, + outputPath, releaseActivity: acquireDaemonActivity('video.capture'), };