-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathscreencast.test.ts
More file actions
213 lines (184 loc) · 6.57 KB
/
screencast.test.ts
File metadata and controls
213 lines (184 loc) · 6.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/**
* @license
* Copyright 2026 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import assert from 'node:assert';
import {describe, it, afterEach} from 'node:test';
import sinon from 'sinon';
import type {ParsedArguments} from '../../src/bin/chrome-devtools-mcp-cli-options.js';
import {startScreencast, stopScreencast} from '../../src/tools/screencast.js';
import {withMcpContext} from '../utils.js';
function createMockRecorder() {
return {
stop: sinon.stub().resolves(),
};
}
describe('screencast', () => {
afterEach(() => {
sinon.restore();
});
describe('screencast_start', () => {
it('starts a screencast recording with filePath', async () => {
await withMcpContext(async (response, context) => {
const mockRecorder = createMockRecorder();
const selectedPage = context.getSelectedPptrPage();
const screencastStub = sinon
.stub(selectedPage, 'screencast')
.resolves(mockRecorder as never);
await startScreencast().handler(
{
params: {path: '/tmp/test-recording.mp4'},
page: context.getSelectedMcpPage(),
},
response,
context,
);
sinon.assert.calledOnce(screencastStub);
const callArgs = screencastStub.firstCall.args[0];
assert.ok(callArgs);
assert.ok(callArgs.path?.endsWith('test-recording.mp4'));
assert.ok(context.getScreenRecorder() !== null);
assert.ok(
response.responseLines
.join('\n')
.includes('Screencast recording started'),
);
});
});
it('starts a screencast recording with temp file when no filePath', async () => {
await withMcpContext(async (response, context) => {
const mockRecorder = createMockRecorder();
const selectedPage = context.getSelectedPptrPage();
const screencastStub = sinon
.stub(selectedPage, 'screencast')
.resolves(mockRecorder as never);
await startScreencast().handler(
{params: {}, page: context.getSelectedMcpPage()},
response,
context,
);
sinon.assert.calledOnce(screencastStub);
const callArgs = screencastStub.firstCall.args[0];
assert.ok(callArgs);
assert.ok(callArgs.path?.endsWith('.mp4'));
assert.ok(context.getScreenRecorder() !== null);
});
});
it('errors if a recording is already active', async () => {
await withMcpContext(async (response, context) => {
const mockRecorder = createMockRecorder();
context.setScreenRecorder({
recorder: mockRecorder as never,
filePath: '/tmp/existing.mp4',
});
const selectedPage = context.getSelectedPptrPage();
const screencastStub = sinon.stub(selectedPage, 'screencast');
await startScreencast().handler(
{params: {}, page: context.getSelectedMcpPage()},
response,
context,
);
sinon.assert.notCalled(screencastStub);
assert.ok(
response.responseLines
.join('\n')
.includes('a screencast recording is already in progress'),
);
});
});
it('provides a clear error when ffmpeg is not found', async () => {
await withMcpContext(async (response, context) => {
const selectedPage = context.getSelectedPptrPage();
const error = new Error('spawn ffmpeg ENOENT');
sinon.stub(selectedPage, 'screencast').rejects(error);
await assert.rejects(
startScreencast().handler(
{
params: {path: '/tmp/test.mp4'},
page: context.getSelectedMcpPage(),
},
response,
context,
),
/ffmpeg is required for screencast recording/,
);
assert.strictEqual(context.getScreenRecorder(), null);
});
});
it('passes ffmpegPath from args to puppeteer', async () => {
await withMcpContext(async (response, context) => {
const mockRecorder = createMockRecorder();
const selectedPage = context.getSelectedPptrPage();
const screencastStub = sinon
.stub(selectedPage, 'screencast')
.resolves(mockRecorder as never);
const experimentalFfmpegPath = '/custom/path/to/ffmpeg';
await startScreencast({
experimentalFfmpegPath,
} as ParsedArguments).handler(
{params: {}, page: context.getSelectedMcpPage()},
response,
context,
);
sinon.assert.calledOnce(screencastStub);
const callArgs = screencastStub.firstCall.args[0];
assert.strictEqual(callArgs?.ffmpegPath, experimentalFfmpegPath);
});
});
});
describe('screencast_stop', () => {
it('does nothing if no recording is active', async () => {
await withMcpContext(async (response, context) => {
assert.strictEqual(context.getScreenRecorder(), null);
await stopScreencast.handler(
{params: {}, page: context.getSelectedMcpPage()},
response,
context,
);
assert.strictEqual(response.responseLines.length, 0);
});
});
it('stops an active recording and reports the file path', async () => {
await withMcpContext(async (response, context) => {
const mockRecorder = createMockRecorder();
const filePath = '/tmp/test-recording.mp4';
context.setScreenRecorder({
recorder: mockRecorder as never,
filePath,
});
await stopScreencast.handler(
{params: {}, page: context.getSelectedMcpPage()},
response,
context,
);
sinon.assert.calledOnce(mockRecorder.stop);
assert.strictEqual(context.getScreenRecorder(), null);
assert.ok(
response.responseLines
.join('\n')
.includes('stopped and saved to /tmp/test-recording.mp4'),
);
});
});
it('clears the recorder even if stop() throws', async () => {
await withMcpContext(async (response, context) => {
const mockRecorder = createMockRecorder();
mockRecorder.stop.rejects(new Error('ffmpeg process error'));
context.setScreenRecorder({
recorder: mockRecorder as never,
filePath: '/tmp/test.mp4',
});
await assert.rejects(
stopScreencast.handler(
{params: {}, page: context.getSelectedMcpPage()},
response,
context,
),
/ffmpeg process error/,
);
assert.strictEqual(context.getScreenRecorder(), null);
});
});
});
});