chore: extract CDPSession base class for easy mocking (#8950)

This commit is contained in:
Alex Rudenko 2022-09-15 15:50:12 +02:00 committed by GitHub
parent 633e7cfdf9
commit fa084bcdcd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 55 additions and 14 deletions

View File

@ -56,7 +56,7 @@ export class Connection extends EventEmitter {
#transport: ConnectionTransport; #transport: ConnectionTransport;
#delay: number; #delay: number;
#lastId = 0; #lastId = 0;
#sessions: Map<string, CDPSession> = new Map(); #sessions: Map<string, CDPSessionImpl> = new Map();
#closed = false; #closed = false;
#callbacks: Map<number, ConnectionCallback> = new Map(); #callbacks: Map<number, ConnectionCallback> = new Map();
#manuallyAttached = new Set<string>(); #manuallyAttached = new Set<string>();
@ -147,7 +147,7 @@ export class Connection extends EventEmitter {
const object = JSON.parse(message); const object = JSON.parse(message);
if (object.method === 'Target.attachedToTarget') { if (object.method === 'Target.attachedToTarget') {
const sessionId = object.params.sessionId; const sessionId = object.params.sessionId;
const session = new CDPSession( const session = new CDPSessionImpl(
this, this,
object.params.targetInfo.type, object.params.targetInfo.type,
sessionId sessionId
@ -310,6 +310,47 @@ export const CDPSessionEmittedEvents = {
* @public * @public
*/ */
export class CDPSession extends EventEmitter { export class CDPSession extends EventEmitter {
/**
* @internal
*/
constructor() {
super();
}
connection(): Connection | undefined {
throw new Error('Not implemented');
}
send<T extends keyof ProtocolMapping.Commands>(
method: T,
...paramArgs: ProtocolMapping.Commands[T]['paramsType']
): Promise<ProtocolMapping.Commands[T]['returnType']>;
send<T extends keyof ProtocolMapping.Commands>(): Promise<
ProtocolMapping.Commands[T]['returnType']
> {
throw new Error('Not implemented');
}
/**
* Detaches the cdpSession from the target. Once detached, the cdpSession object
* won't emit any events and can't be used to send messages.
*/
async detach(): Promise<void> {
throw new Error('Not implemented');
}
/**
* Returns the session's id.
*/
id(): string {
throw new Error('Not implemented');
}
}
/**
* @internal
*/
export class CDPSessionImpl extends CDPSession {
#sessionId: string; #sessionId: string;
#targetType: string; #targetType: string;
#callbacks: Map<number, ConnectionCallback> = new Map(); #callbacks: Map<number, ConnectionCallback> = new Map();
@ -325,11 +366,11 @@ export class CDPSession extends EventEmitter {
this.#sessionId = sessionId; this.#sessionId = sessionId;
} }
connection(): Connection | undefined { override connection(): Connection | undefined {
return this.#connection; return this.#connection;
} }
send<T extends keyof ProtocolMapping.Commands>( override send<T extends keyof ProtocolMapping.Commands>(
method: T, method: T,
...paramArgs: ProtocolMapping.Commands[T]['paramsType'] ...paramArgs: ProtocolMapping.Commands[T]['paramsType']
): Promise<ProtocolMapping.Commands[T]['returnType']> { ): Promise<ProtocolMapping.Commands[T]['returnType']> {
@ -386,7 +427,7 @@ export class CDPSession extends EventEmitter {
* Detaches the cdpSession from the target. Once detached, the cdpSession object * Detaches the cdpSession from the target. Once detached, the cdpSession object
* won't emit any events and can't be used to send messages. * won't emit any events and can't be used to send messages.
*/ */
async detach(): Promise<void> { override async detach(): Promise<void> {
if (!this.#connection) { if (!this.#connection) {
throw new Error( throw new Error(
`Session already detached. Most likely the ${ `Session already detached. Most likely the ${
@ -419,7 +460,7 @@ export class CDPSession extends EventEmitter {
/** /**
* Returns the session's id. * Returns the session's id.
*/ */
id(): string { override id(): string {
return this.#sessionId; return this.#sessionId;
} }
} }

View File

@ -15,7 +15,6 @@
*/ */
import {Protocol} from 'devtools-protocol'; import {Protocol} from 'devtools-protocol';
import {ProtocolMapping} from 'devtools-protocol/types/protocol-mapping.js';
import {assert} from '../util/assert.js'; import {assert} from '../util/assert.js';
import {EventEmitter} from './EventEmitter.js'; import {EventEmitter} from './EventEmitter.js';
import {Frame} from './Frame.js'; import {Frame} from './Frame.js';
@ -25,6 +24,7 @@ import {FetchRequestId, NetworkEventManager} from './NetworkEventManager.js';
import {debugError, isString} from './util.js'; import {debugError, isString} from './util.js';
import {DeferredPromise} from '../util/DeferredPromise.js'; import {DeferredPromise} from '../util/DeferredPromise.js';
import {createDebuggableDeferredPromise} from '../util/DebuggableDeferredPromise.js'; import {createDebuggableDeferredPromise} from '../util/DebuggableDeferredPromise.js';
import {CDPSession} from './Connection.js';
/** /**
* @public * @public
@ -66,13 +66,6 @@ export const NetworkManagerEmittedEvents = {
RequestFinished: Symbol('NetworkManager.RequestFinished'), RequestFinished: Symbol('NetworkManager.RequestFinished'),
} as const; } as const;
interface CDPSession extends EventEmitter {
send<T extends keyof ProtocolMapping.Commands>(
method: T,
...paramArgs: ProtocolMapping.Commands[T]['paramsType']
): Promise<ProtocolMapping.Commands[T]['returnType']>;
}
interface FrameManager { interface FrameManager {
frame(frameId: string): Frame | null; frame(frameId: string): Frame | null;
} }

View File

@ -26,6 +26,13 @@ import {HTTPResponse} from '../../lib/cjs/puppeteer/common/HTTPResponse.js';
class MockCDPSession extends EventEmitter { class MockCDPSession extends EventEmitter {
async send(): Promise<any> {} async send(): Promise<any> {}
connection() {
return undefined;
}
async detach() {}
id() {
return '1';
}
} }
describe('NetworkManager', () => { describe('NetworkManager', () => {