Skip to content

Commit

Permalink
chore: extract CDPSession base class for easy mocking (#8950)
Browse files Browse the repository at this point in the history
  • Loading branch information
OrKoN committed Sep 15, 2022
1 parent 633e7cf commit fa084bc
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 14 deletions.
53 changes: 47 additions & 6 deletions src/common/Connection.ts
Expand Up @@ -56,7 +56,7 @@ export class Connection extends EventEmitter {
#transport: ConnectionTransport;
#delay: number;
#lastId = 0;
#sessions: Map<string, CDPSession> = new Map();
#sessions: Map<string, CDPSessionImpl> = new Map();
#closed = false;
#callbacks: Map<number, ConnectionCallback> = new Map();
#manuallyAttached = new Set<string>();
Expand Down Expand Up @@ -147,7 +147,7 @@ export class Connection extends EventEmitter {
const object = JSON.parse(message);
if (object.method === 'Target.attachedToTarget') {
const sessionId = object.params.sessionId;
const session = new CDPSession(
const session = new CDPSessionImpl(
this,
object.params.targetInfo.type,
sessionId
Expand Down Expand Up @@ -310,6 +310,47 @@ export const CDPSessionEmittedEvents = {
* @public
*/
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;
#targetType: string;
#callbacks: Map<number, ConnectionCallback> = new Map();
Expand All @@ -325,11 +366,11 @@ export class CDPSession extends EventEmitter {
this.#sessionId = sessionId;
}

connection(): Connection | undefined {
override connection(): Connection | undefined {
return this.#connection;
}

send<T extends keyof ProtocolMapping.Commands>(
override send<T extends keyof ProtocolMapping.Commands>(
method: T,
...paramArgs: ProtocolMapping.Commands[T]['paramsType']
): Promise<ProtocolMapping.Commands[T]['returnType']> {
Expand Down Expand Up @@ -386,7 +427,7 @@ export class CDPSession extends EventEmitter {
* 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> {
override async detach(): Promise<void> {
if (!this.#connection) {
throw new Error(
`Session already detached. Most likely the ${
Expand Down Expand Up @@ -419,7 +460,7 @@ export class CDPSession extends EventEmitter {
/**
* Returns the session's id.
*/
id(): string {
override id(): string {
return this.#sessionId;
}
}
Expand Down
9 changes: 1 addition & 8 deletions src/common/NetworkManager.ts
Expand Up @@ -15,7 +15,6 @@
*/

import {Protocol} from 'devtools-protocol';
import {ProtocolMapping} from 'devtools-protocol/types/protocol-mapping.js';
import {assert} from '../util/assert.js';
import {EventEmitter} from './EventEmitter.js';
import {Frame} from './Frame.js';
Expand All @@ -25,6 +24,7 @@ import {FetchRequestId, NetworkEventManager} from './NetworkEventManager.js';
import {debugError, isString} from './util.js';
import {DeferredPromise} from '../util/DeferredPromise.js';
import {createDebuggableDeferredPromise} from '../util/DebuggableDeferredPromise.js';
import {CDPSession} from './Connection.js';

/**
* @public
Expand Down Expand Up @@ -66,13 +66,6 @@ export const NetworkManagerEmittedEvents = {
RequestFinished: Symbol('NetworkManager.RequestFinished'),
} 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 {
frame(frameId: string): Frame | null;
}
Expand Down
7 changes: 7 additions & 0 deletions test/src/NetworkManager.spec.ts
Expand Up @@ -26,6 +26,13 @@ import {HTTPResponse} from '../../lib/cjs/puppeteer/common/HTTPResponse.js';

class MockCDPSession extends EventEmitter {
async send(): Promise<any> {}
connection() {
return undefined;
}
async detach() {}
id() {
return '1';
}
}

describe('NetworkManager', () => {
Expand Down

0 comments on commit fa084bc

Please sign in to comment.