Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: extract CDPSession base class for easy mocking #8950

Merged
merged 1 commit into from
Sep 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
53 changes: 47 additions & 6 deletions src/common/Connection.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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