|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2026 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import assert from 'node:assert'; |
| 8 | +import {describe, it, afterEach} from 'node:test'; |
| 9 | + |
| 10 | +import sinon from 'sinon'; |
| 11 | + |
| 12 | +import {startScreencast, stopScreencast} from '../../src/tools/screencast.js'; |
| 13 | +import {withMcpContext} from '../utils.js'; |
| 14 | + |
| 15 | +function createMockRecorder() { |
| 16 | + return { |
| 17 | + stop: sinon.stub().resolves(), |
| 18 | + }; |
| 19 | +} |
| 20 | + |
| 21 | +describe('screencast', () => { |
| 22 | + afterEach(() => { |
| 23 | + sinon.restore(); |
| 24 | + }); |
| 25 | + |
| 26 | + describe('screencast_start', () => { |
| 27 | + it('starts a screencast recording with filePath', async () => { |
| 28 | + await withMcpContext(async (response, context) => { |
| 29 | + const mockRecorder = createMockRecorder(); |
| 30 | + const selectedPage = context.getSelectedPage(); |
| 31 | + const screencastStub = sinon |
| 32 | + .stub(selectedPage, 'screencast') |
| 33 | + .resolves(mockRecorder as never); |
| 34 | + |
| 35 | + await startScreencast.handler( |
| 36 | + {params: {path: '/tmp/test-recording.mp4'}}, |
| 37 | + response, |
| 38 | + context, |
| 39 | + ); |
| 40 | + |
| 41 | + sinon.assert.calledOnce(screencastStub); |
| 42 | + const callArgs = screencastStub.firstCall.args[0]; |
| 43 | + assert.ok(callArgs); |
| 44 | + assert.ok(callArgs.path?.endsWith('test-recording.mp4')); |
| 45 | + |
| 46 | + assert.ok(context.getScreenRecorder() !== null); |
| 47 | + assert.ok( |
| 48 | + response.responseLines |
| 49 | + .join('\n') |
| 50 | + .includes('Screencast recording started'), |
| 51 | + ); |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + it('starts a screencast recording with temp file when no filePath', async () => { |
| 56 | + await withMcpContext(async (response, context) => { |
| 57 | + const mockRecorder = createMockRecorder(); |
| 58 | + const selectedPage = context.getSelectedPage(); |
| 59 | + const screencastStub = sinon |
| 60 | + .stub(selectedPage, 'screencast') |
| 61 | + .resolves(mockRecorder as never); |
| 62 | + |
| 63 | + await startScreencast.handler({params: {}}, response, context); |
| 64 | + |
| 65 | + sinon.assert.calledOnce(screencastStub); |
| 66 | + const callArgs = screencastStub.firstCall.args[0]; |
| 67 | + assert.ok(callArgs); |
| 68 | + assert.ok(callArgs.path?.endsWith('.mp4')); |
| 69 | + assert.ok(context.getScreenRecorder() !== null); |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + it('errors if a recording is already active', async () => { |
| 74 | + await withMcpContext(async (response, context) => { |
| 75 | + const mockRecorder = createMockRecorder(); |
| 76 | + context.setScreenRecorder({ |
| 77 | + recorder: mockRecorder as never, |
| 78 | + filePath: '/tmp/existing.mp4', |
| 79 | + }); |
| 80 | + |
| 81 | + const selectedPage = context.getSelectedPage(); |
| 82 | + const screencastStub = sinon.stub(selectedPage, 'screencast'); |
| 83 | + |
| 84 | + await startScreencast.handler({params: {}}, response, context); |
| 85 | + |
| 86 | + sinon.assert.notCalled(screencastStub); |
| 87 | + assert.ok( |
| 88 | + response.responseLines |
| 89 | + .join('\n') |
| 90 | + .includes('a screencast recording is already in progress'), |
| 91 | + ); |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | + it('provides a clear error when ffmpeg is not found', async () => { |
| 96 | + await withMcpContext(async (response, context) => { |
| 97 | + const selectedPage = context.getSelectedPage(); |
| 98 | + const error = new Error('spawn ffmpeg ENOENT'); |
| 99 | + sinon.stub(selectedPage, 'screencast').rejects(error); |
| 100 | + |
| 101 | + await assert.rejects( |
| 102 | + startScreencast.handler( |
| 103 | + {params: {path: '/tmp/test.mp4'}}, |
| 104 | + response, |
| 105 | + context, |
| 106 | + ), |
| 107 | + /ffmpeg is required for screencast recording/, |
| 108 | + ); |
| 109 | + |
| 110 | + assert.strictEqual(context.getScreenRecorder(), null); |
| 111 | + }); |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + describe('screencast_stop', () => { |
| 116 | + it('does nothing if no recording is active', async () => { |
| 117 | + await withMcpContext(async (response, context) => { |
| 118 | + assert.strictEqual(context.getScreenRecorder(), null); |
| 119 | + await stopScreencast.handler({params: {}}, response, context); |
| 120 | + assert.strictEqual(response.responseLines.length, 0); |
| 121 | + }); |
| 122 | + }); |
| 123 | + |
| 124 | + it('stops an active recording and reports the file path', async () => { |
| 125 | + await withMcpContext(async (response, context) => { |
| 126 | + const mockRecorder = createMockRecorder(); |
| 127 | + const filePath = '/tmp/test-recording.mp4'; |
| 128 | + context.setScreenRecorder({ |
| 129 | + recorder: mockRecorder as never, |
| 130 | + filePath, |
| 131 | + }); |
| 132 | + |
| 133 | + await stopScreencast.handler({params: {}}, response, context); |
| 134 | + |
| 135 | + sinon.assert.calledOnce(mockRecorder.stop); |
| 136 | + assert.strictEqual(context.getScreenRecorder(), null); |
| 137 | + assert.ok( |
| 138 | + response.responseLines |
| 139 | + .join('\n') |
| 140 | + .includes('stopped and saved to /tmp/test-recording.mp4'), |
| 141 | + ); |
| 142 | + }); |
| 143 | + }); |
| 144 | + |
| 145 | + it('clears the recorder even if stop() throws', async () => { |
| 146 | + await withMcpContext(async (response, context) => { |
| 147 | + const mockRecorder = createMockRecorder(); |
| 148 | + mockRecorder.stop.rejects(new Error('ffmpeg process error')); |
| 149 | + context.setScreenRecorder({ |
| 150 | + recorder: mockRecorder as never, |
| 151 | + filePath: '/tmp/test.mp4', |
| 152 | + }); |
| 153 | + |
| 154 | + await assert.rejects( |
| 155 | + stopScreencast.handler({params: {}}, response, context), |
| 156 | + /ffmpeg process error/, |
| 157 | + ); |
| 158 | + |
| 159 | + assert.strictEqual(context.getScreenRecorder(), null); |
| 160 | + }); |
| 161 | + }); |
| 162 | + }); |
| 163 | +}); |
0 commit comments