-
Notifications
You must be signed in to change notification settings - Fork 9.5k
/
Copy pathtarget-manager-test.js
378 lines (314 loc) · 14.8 KB
/
target-manager-test.js
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/**
* @license
* Copyright 2021 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import {EventEmitter} from 'events';
import {CdpCDPSession} from 'puppeteer-core/lib/cjs/puppeteer/cdp/CDPSession.js';
import {TargetManager} from '../../../gather/driver/target-manager.js';
import {createMockCdpSession} from '../mock-driver.js';
import {createMockSendCommandFn} from '../../gather/mock-commands.js';
import {fnAny} from '../../test-utils.js';
/**
* @param {{type?: string, targetId?: string}} [overrides]
* @return {LH.Crdp.Target.TargetInfo}
*/
function createTargetInfo(overrides) {
return {
type: 'page',
targetId: 'page',
title: '',
url: '',
attached: true,
canAccessOpener: false,
...overrides,
};
}
describe('TargetManager', () => {
let sessionMock = createMockCdpSession();
let sendMock = sessionMock.send;
let targetManager = new TargetManager(sessionMock.asCdpSession());
let targetInfo = createTargetInfo();
beforeEach(() => {
sessionMock = createMockCdpSession();
sendMock = sessionMock.send;
sendMock
.mockResponse('Page.enable')
.mockResponse('Page.getFrameTree', {frameTree: {frame: {id: 'mainFrameId'}}})
.mockResponse('Runtime.enable')
.mockResponse('Page.disable')
.mockResponse('Runtime.disable')
.mockResponse('Runtime.runIfWaitingForDebugger');
targetManager = new TargetManager(sessionMock.asCdpSession());
targetInfo = createTargetInfo();
});
describe('.enable()', () => {
it('should autoattach to root session', async () => {
sendMock
.mockResponse('Target.getTargetInfo', {targetInfo})
.mockResponse('Network.enable')
.mockResponse('Target.setAutoAttach');
await targetManager.enable();
expect(sendMock.findAllInvocations('Target.setAutoAttach')).toHaveLength(1);
expect(sendMock.findAllInvocations('Runtime.runIfWaitingForDebugger')).toHaveLength(1);
expect(targetManager._mainFrameId).toEqual('mainFrameId');
});
it('should autoattach to further unique sessions', async () => {
sendMock
.mockResponse('Target.getTargetInfo', {targetInfo}) // original, attach
.mockResponse('Target.getTargetInfo', {targetInfo}) // duplicate, no attach
.mockResponse('Target.getTargetInfo', {targetInfo: {...targetInfo, targetId: '1'}}) // unique, attach
.mockResponse('Target.getTargetInfo', {targetInfo: {...targetInfo, targetId: '2'}}) // unique, attach
.mockResponse('Network.enable')
.mockResponse('Target.setAutoAttach')
.mockResponse('Network.enable')
.mockResponse('Target.setAutoAttach')
.mockResponse('Network.enable')
.mockResponse('Target.setAutoAttach')
.mockResponse('Runtime.runIfWaitingForDebugger')
.mockResponse('Runtime.runIfWaitingForDebugger')
.mockResponse('Runtime.runIfWaitingForDebugger')
.mockResponse('Runtime.runIfWaitingForDebugger');
await targetManager.enable();
expect(sessionMock.on).toHaveBeenCalled();
const sessionListener = sessionMock.on.mock.calls.find(c => c[0] === 'sessionattached')?.[1];
// Original, attach.
expect(sendMock.findAllInvocations('Target.getTargetInfo')).toHaveLength(1);
expect(sendMock.findAllInvocations('Target.setAutoAttach')).toHaveLength(1);
// Duplicate, no attach.
await sessionListener(sessionMock);
expect(sendMock.findAllInvocations('Target.getTargetInfo')).toHaveLength(2);
expect(sendMock.findAllInvocations('Target.setAutoAttach')).toHaveLength(1);
// Unique, attach.
await sessionListener(sessionMock);
expect(sendMock.findAllInvocations('Target.getTargetInfo')).toHaveLength(3);
expect(sendMock.findAllInvocations('Target.setAutoAttach')).toHaveLength(2);
// Unique, attach.
await sessionListener(sessionMock);
expect(sendMock.findAllInvocations('Target.getTargetInfo')).toHaveLength(4);
expect(sendMock.findAllInvocations('Target.setAutoAttach')).toHaveLength(3);
// Four resumes because in finally clause, so runs regardless of uniqueness.
expect(sendMock.findAllInvocations('Runtime.runIfWaitingForDebugger')).toHaveLength(4);
});
it('should ignore errors while attaching to worker targets', async () => {
targetInfo.type = 'worker';
sendMock
.mockResponse('Target.getTargetInfo', {targetInfo})
.mockResponse('Network.enable', () => {
throw new Error('Cannot use Network.enable');
})
.mockResponse('Target.setAutoAttach');
await targetManager.enable();
const invocations = sendMock.findAllInvocations('Target.setAutoAttach');
expect(invocations).toHaveLength(0);
// Should still be resumed.
expect(sendMock.findAllInvocations('Runtime.runIfWaitingForDebugger')).toHaveLength(1);
});
it('should ignore targets that are not frames or web workers', async () => {
targetInfo.type = 'service_worker';
sendMock
.mockResponse('Target.getTargetInfo', {targetInfo})
.mockResponse('Target.setAutoAttach');
await targetManager.enable();
const invocations = sendMock.findAllInvocations('Target.setAutoAttach');
expect(invocations).toHaveLength(0);
// Should still be resumed.
expect(sendMock.findAllInvocations('Runtime.runIfWaitingForDebugger')).toHaveLength(1);
});
it('should listen to target before resuming', async () => {
let targetListeningAsserted = false;
// Intercept listener for all protocol events and ensure target is still paused.
sessionMock.on = /** @type {typeof sessionMock.on} */ (fnAny()
.mockImplementation(/** @param {string} eventName */ (eventName) => {
const getTargetInfoCount = sendMock.findAllInvocations('Target.getTargetInfo').length;
const setAutoAttachCount = sendMock.findAllInvocations('Target.setAutoAttach').length;
const resumeCount = sendMock.findAllInvocations('Runtime.runIfWaitingForDebugger').length;
// There may be many listeners for all protocol events, so just ensure this one occurred.
if (eventName === '*' &&
getTargetInfoCount === 1 && setAutoAttachCount === 0 && resumeCount === 0) {
targetListeningAsserted = true;
}
}));
sendMock
.mockResponse('Target.getTargetInfo', {targetInfo})
.mockResponse('Network.enable')
.mockResponse('Target.setAutoAttach');
expect(sendMock.findAllInvocations('Target.getTargetInfo')).toHaveLength(0);
expect(sendMock.findAllInvocations('Target.setAutoAttach')).toHaveLength(0);
expect(sendMock.findAllInvocations('Runtime.runIfWaitingForDebugger')).toHaveLength(0);
await targetManager.enable();
expect(targetListeningAsserted).toBe(true);
expect(sendMock.findAllInvocations('Target.getTargetInfo')).toHaveLength(1);
expect(sendMock.findAllInvocations('Target.setAutoAttach')).toHaveLength(1);
expect(sendMock.findAllInvocations('Runtime.runIfWaitingForDebugger')).toHaveLength(1);
});
it('should gracefully handle a target closing while attaching', async () => {
const targetClosedError = new Error('Target closed');
sendMock
.mockResponse('Target.getTargetInfo', {targetInfo})
.mockResponse('Network.enable')
.mockResponse('Target.setAutoAttach', () => Promise.reject(targetClosedError));
await targetManager.enable();
});
it('should throw other protocol errors while attaching', async () => {
const fatalError = new Error('Fatal error');
sendMock
.mockResponse('Target.getTargetInfo', {targetInfo})
.mockResponse('Network.enable')
.mockResponse('Target.setAutoAttach', () => Promise.reject(fatalError));
await expect(targetManager.enable()).rejects.toThrowError(
'Protocol error (Target.setAutoAttach): Fatal error');
// Should still attempt to resume target.
expect(sendMock.findAllInvocations('Runtime.runIfWaitingForDebugger')).toHaveLength(1);
});
it('should resume the target when finished', async () => {
targetInfo.type = 'service_worker';
sendMock.mockResponse('Target.getTargetInfo', {targetInfo});
await targetManager.enable();
const invocations = sendMock.findAllInvocations('Runtime.runIfWaitingForDebugger');
expect(invocations).toHaveLength(1);
});
it('should autoattach on main frame navigation', async () => {
sendMock
.mockResponse('Target.getTargetInfo', {targetInfo})
.mockResponse('Network.enable')
.mockResponse('Target.setAutoAttach')
.mockResponse('Target.setAutoAttach');
await targetManager.enable();
const onFrameNavigation = sessionMock.on.getListeners('Page.frameNavigated')[0];
onFrameNavigation({frame: {}}); // note the lack of a `parentId`
const invocations = sendMock.findAllInvocations('Target.setAutoAttach');
expect(invocations).toHaveLength(2);
});
it('should not autoattach on subframe navigation', async () => {
sendMock
.mockResponse('Target.getTargetInfo', {targetInfo})
.mockResponse('Network.enable')
.mockResponse('Target.setAutoAttach')
.mockResponse('Target.setAutoAttach');
await targetManager.enable();
const onFrameNavigation = sessionMock.on.getListeners('Page.frameNavigated')[0];
onFrameNavigation({frame: {parentId: 'root'}});
const invocations = sendMock.findAllInvocations('Target.setAutoAttach');
expect(invocations).toHaveLength(1);
});
it('should be idempotent', async () => {
sendMock
.mockResponse('Target.getTargetInfo', {targetInfo})
.mockResponse('Network.enable')
.mockResponse('Target.setAutoAttach');
await targetManager.enable();
await targetManager.enable();
await targetManager.enable();
const invocations = sendMock.findAllInvocations('Target.setAutoAttach');
expect(invocations).toHaveLength(1);
});
});
describe('.disable()', () => {
it('should uninstall listeners', async () => {
await targetManager.disable();
expect(sessionMock.off).toHaveBeenCalled();
});
});
describe('protocolevent emit', () => {
/** @param {string} sessionId */
function createCdpSession(sessionId) {
class MockCdpConnection extends EventEmitter {
constructor() {
super();
this._rawSend = fnAny();
}
}
const mockCdpConnection = new MockCdpConnection();
/** @type {import('puppeteer-core').CDPSession} */
// @ts-expect-error - close enough to the real thing.
const cdpSession = new CdpCDPSession(mockCdpConnection, '', sessionId);
return cdpSession;
}
it('should listen for and re-emit protocol events across sessions', async () => {
const rootSession = createCdpSession('root');
const rootTargetInfo = createTargetInfo();
// Still mock command responses at session level.
rootSession.send = createMockSendCommandFn()
.mockResponse('Page.enable')
.mockResponse('Page.getFrameTree', {frameTree: {frame: {id: ''}}})
.mockResponse('Runtime.enable')
.mockResponse('Target.getTargetInfo', {targetInfo: rootTargetInfo})
.mockResponse('Network.enable')
.mockResponse('Target.setAutoAttach')
.mockResponse('Runtime.runIfWaitingForDebugger');
const targetManager = new TargetManager(rootSession);
await targetManager.enable();
// Attach an iframe session.
const iframeSession = createCdpSession('iframe');
const iframeTargetInfo = createTargetInfo({type: 'iframe', targetId: 'iframe'});
// Still mock command responses at session level.
iframeSession.send = createMockSendCommandFn()
.mockResponse('Target.getTargetInfo', {targetInfo: iframeTargetInfo})
.mockResponse('Network.enable')
.mockResponse('Target.setAutoAttach')
.mockResponse('Runtime.runIfWaitingForDebugger');
rootSession.emit('sessionattached', iframeSession);
// Wait for iframe session to be attached.
await new Promise(resolve => setTimeout(resolve, 0));
const rootListener = fnAny();
const iframeListener = fnAny();
const allListener = fnAny();
rootSession.on('DOM.documentUpdated', rootListener);
iframeSession.on('Animation.animationCreated', iframeListener);
targetManager.on('protocolevent', allListener);
// @ts-expect-error - types for _onMessage are wrong.
rootSession._onMessage({method: 'DOM.documentUpdated'});
// @ts-expect-error - types for _onMessage are wrong.
rootSession._onMessage({method: 'Debugger.scriptParsed', params: {script: 'details'}});
// @ts-expect-error - types for _onMessage are wrong.
iframeSession._onMessage({method: 'Animation.animationCreated', params: {id: 'animated'}});
expect(rootListener).toHaveBeenCalledTimes(1);
expect(rootListener).toHaveBeenCalledWith(undefined);
expect(iframeListener).toHaveBeenCalledTimes(1);
expect(iframeListener).toHaveBeenCalledWith({id: 'animated'});
expect(allListener).toHaveBeenCalledTimes(3);
expect(allListener).toHaveBeenCalledWith({
method: 'DOM.documentUpdated',
params: undefined,
sessionId: 'root',
targetType: 'page',
});
expect(allListener).toHaveBeenCalledWith({
method: 'Debugger.scriptParsed',
params: {script: 'details'},
sessionId: 'root',
targetType: 'page',
});
expect(allListener).toHaveBeenCalledWith({
method: 'Animation.animationCreated',
params: {id: 'animated'},
sessionId: 'iframe',
targetType: 'iframe',
});
});
it('should stop listening for protocol events', async () => {
const rootSession = createCdpSession('root');
// Still mock command responses at session level.
rootSession.send = createMockSendCommandFn()
.mockResponse('Page.enable')
.mockResponse('Page.getFrameTree', {frameTree: {frame: {id: ''}}})
.mockResponse('Runtime.enable')
.mockResponse('Target.getTargetInfo', {targetInfo})
.mockResponse('Network.enable')
.mockResponse('Target.setAutoAttach')
.mockResponse('Runtime.runIfWaitingForDebugger');
const targetManager = new TargetManager(rootSession);
await targetManager.enable();
const allListener = fnAny();
targetManager.on('protocolevent', allListener);
// @ts-expect-error - types for _onMessage are wrong.
rootSession._onMessage({method: 'DOM.documentUpdated'});
expect(allListener).toHaveBeenCalled();
targetManager.off('protocolevent', allListener);
// @ts-expect-error - types for _onMessage are wrong.
rootSession._onMessage({method: 'DOM.documentUpdated'});
expect(allListener).toHaveBeenCalledTimes(1);
});
});
});