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: adding command log to protocol #26387

Merged
merged 9 commits into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/app/src/runner/event-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,7 @@ export class EventManager {
this._interceptStudio(displayProps)

this.reporterBus.emit('reporter:log:add', displayProps)
Cypress.backend('protocol:command:log:added', { ...displayProps, timestamp: displayProps.wallClockStartedAt })
})

Cypress.on('log:changed', (log) => {
Expand All @@ -505,6 +506,7 @@ export class EventManager {
this._interceptStudio(displayProps)

this.reporterBus.emit('reporter:log:state:changed', displayProps)
Cypress.backend('protocol:command:log:changed', { ...displayProps, timestamp: new Date().toJSON() })
})

// TODO: MOVE BACK INTO useEventManager. Verify this works
Expand Down
3 changes: 1 addition & 2 deletions packages/reporter/src/attempts/attempt-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,7 @@ export default class Attempt {
addLog = (props: LogProps) => {
switch (props.instrument) {
case 'command': {
// @ts-ignore satisfied by CommandProps
if (props.sessionInfo) {
if ((props as CommandProps).sessionInfo) {
this._addSession(props as unknown as SessionProps) // add sessionInstrumentPanel details
}

Expand Down
3 changes: 1 addition & 2 deletions packages/reporter/src/instruments/instrument-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ export interface InstrumentProps {
displayName?: string
name?: string
message?: string
// agent / route / session - instrument panel log type
// parent / child / system - command log type
type?: 'agent' | 'parent' | 'child' | 'system' | 'route' | 'session'
type?: 'parent' | 'child' | 'system'
testCurrentRetry?: number
state: TestState
referencesAlias?: Alias
Expand Down
24 changes: 16 additions & 8 deletions packages/server/lib/cloud/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ class ProtocolManagerImpl implements ProtocolManager {
return
}

debug('connecting to browser for new spec')

await this.protocol?.connectToBrowser(cdpClient)
}

Expand Down Expand Up @@ -93,8 +91,6 @@ class ProtocolManagerImpl implements ProtocolManager {
return
}

debug('after spec')

this.protocol?.afterSpec()
}

Expand All @@ -103,8 +99,6 @@ class ProtocolManagerImpl implements ProtocolManager {
return
}

debug('before test %O', test)

this.protocol?.beforeTest(test)
}

Expand All @@ -113,10 +107,24 @@ class ProtocolManagerImpl implements ProtocolManager {
return
}

debug('after test %O', test)

this.protocol?.afterTest(test)
}

commandLogAdded (log: any) {
if (!this.protocolEnabled()) {
return
}

this.protocol?.commandLogAdded(log)
}

commandLogChanged (log: any): void {
if (!this.protocolEnabled()) {
return
}

this.protocol?.commandLogChanged(log)
}
}

export default ProtocolManagerImpl
4 changes: 4 additions & 0 deletions packages/server/lib/socket-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,10 @@ export class SocketBase {
return this.protocolManager?.beforeTest(args[0])
case 'protocol:test:after:run':
return this.protocolManager?.afterTest(args[0])
case 'protocol:command:log:added':
return this.protocolManager?.commandLogAdded(args[0])
case 'protocol:command:log:changed':
return this.protocolManager?.commandLogChanged(args[0])
default:
throw new Error(`You requested a backend event we cannot handle: ${eventName}`)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,19 @@ const AppCaptureProtocol = class {
this.beforeSpec = this.beforeSpec.bind(this)
this.afterSpec = this.afterSpec.bind(this)
this.beforeTest = this.beforeTest.bind(this)
this.commandLogAdded = this.commandLogAdded.bind(this)
this.commandLogChanged = this.commandLogChanged.bind(this)
}

connectToBrowser ({
target,
host,
port,
}) {
connectToBrowser (cdpClient) {
return Promise.resolve()
}

addRunnables (runnables) {}

beforeSpec (spec) {}

afterSpec (spec) {}

beforeTest (test) {}
commandLogAdded (log) {}
commandLogChanged (log) {}
}

module.exports = {
Expand Down
126 changes: 79 additions & 47 deletions packages/server/test/unit/cloud/protocol_spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { proxyquire } from '../../spec_helper'
import path from 'path'
import os from 'os'
import type { AppCaptureProtocolInterface, ProtocolManager as ProtocolManagerInterface } from '@packages/types'

const mockDb = sinon.stub()
const mockDatabase = sinon.stub().returns(mockDb)
Expand All @@ -10,54 +11,39 @@ const { default: ProtocolManager } = proxyquire('../lib/cloud/protocol', {
})

describe('lib/cloud/protocol', () => {
beforeEach(() => {
let protocolManager: ProtocolManagerInterface
let protocol: AppCaptureProtocolInterface

beforeEach(async () => {
process.env.CYPRESS_LOCAL_PROTOCOL_PATH = path.join(__dirname, '..', '..', 'support', 'fixtures', 'cloud', 'protocol', 'test-protocol.js')

const protocolManager = new ProtocolManager()

await protocolManager.setupProtocol()

protocol = (protocolManager as any).protocol
})

afterEach(() => {
delete process.env.CYPRESS_LOCAL_PROTOCOL_PATH
})

it('should be able to setup the protocol', async () => {
const protocolManager = new ProtocolManager()

await protocolManager.setupProtocol()

const protocol = (protocolManager as any).protocol

expect(protocolManager.protocolEnabled()).to.be.true
expect(protocol.Debug).not.to.be.undefined
expect((protocol as any).Debug).not.to.be.undefined
})

it('should be able to connect to the browser', async () => {
const protocolManager = new ProtocolManager()

await protocolManager.setupProtocol()

const protocol = (protocolManager as any).protocol
const mockCdpClient = sinon.stub()

sinon.stub(protocol, 'connectToBrowser').resolves()

await protocolManager.connectToBrowser({
target: 'target',
host: 'host',
port: 1234,
})
await protocolManager.connectToBrowser(mockCdpClient as any)

expect(protocol.connectToBrowser).to.be.calledWith({
target: 'target',
host: 'host',
port: 1234,
})
expect(protocol.connectToBrowser).to.be.calledWith(mockCdpClient)
})

it('should be able to initialize a new spec', async () => {
const protocolManager = new ProtocolManager()

await protocolManager.setupProtocol()

const protocol = (protocolManager as any).protocol

sinon.stub(protocol, 'beforeSpec')

protocolManager.beforeSpec({
Expand All @@ -72,12 +58,6 @@ describe('lib/cloud/protocol', () => {
})

it('should be able to initialize a new test', async () => {
const protocolManager = new ProtocolManager()

await protocolManager.setupProtocol()

const protocol = (protocolManager as any).protocol

sinon.stub(protocol, 'beforeTest')

protocolManager.beforeTest({
Expand All @@ -94,12 +74,6 @@ describe('lib/cloud/protocol', () => {
})

it('should be able to clean up after a spec', async () => {
const protocolManager = new ProtocolManager()

await protocolManager.setupProtocol()

const protocol = (protocolManager as any).protocol

sinon.stub(protocol, 'afterSpec')

protocolManager.afterSpec()
Expand All @@ -108,12 +82,6 @@ describe('lib/cloud/protocol', () => {
})

it('should be able to add runnables', async () => {
const protocolManager = new ProtocolManager()

await protocolManager.setupProtocol()

const protocol = (protocolManager as any).protocol

sinon.stub(protocol, 'addRunnables')

const rootRunnable = {
Expand All @@ -135,4 +103,68 @@ describe('lib/cloud/protocol', () => {

expect(protocol.addRunnables).to.be.calledWith(rootRunnable)
})

it('should be able to add a command log', async () => {
sinon.stub(protocol, 'commandLogAdded')

const log = {
id: 'log-https://example.cypress.io-17',
alias: 'getComment',
aliasType: 'route',
displayName: 'xhr',
event: true,
hookId: 'r4',
instrument: 'command',
message: '',
method: 'GET',
name: 'request',
renderProps: {},
state: 'pending',
testId: 'r4',
timeout: 0,
type: 'parent',
url: 'https://jsonplaceholder.cypress.io/comments/1',
wallClockStartedAt: '2023-03-30T21:58:08.456Z',
testCurrentRetry: 0,
hasSnapshot: false,
hasConsoleProps: true,
timestamp: '2023-03-30T21:58:08.457Z',
}

protocolManager.commandLogAdded(log)

expect(protocol.commandLogAdded).to.be.calledWith(log)
})

it('should be able to change a command log', async () => {
sinon.stub(protocol, 'commandLogChanged')

const log = {
id: 'log-https://example.cypress.io-17',
alias: 'getComment',
aliasType: 'route',
displayName: 'xhr',
event: true,
hookId: 'r4',
instrument: 'command',
message: '',
method: 'GET',
name: 'request',
renderProps: {},
state: 'pending',
testId: 'r4',
timeout: 0,
type: 'parent',
url: 'https://jsonplaceholder.cypress.io/comments/1',
wallClockStartedAt: '2023-03-30T21:58:08.456Z',
testCurrentRetry: 0,
hasSnapshot: false,
hasConsoleProps: true,
timestamp: '2023-03-30T21:58:08.457Z',
}

protocolManager.commandLogChanged(log)

expect(protocol.commandLogChanged).to.be.calledWith(log)
})
})
4 changes: 4 additions & 0 deletions packages/types/src/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export interface AppCaptureProtocolInterface {
afterSpec (): void
beforeTest(test: Record<string, any>): void
afterTest(test: Record<string, any>): void
commandLogAdded (log: any): void
commandLogChanged (log: any): void
}

export interface ProtocolManager {
Expand All @@ -31,4 +33,6 @@ export interface ProtocolManager {
afterSpec (): void
beforeTest(test: Record<string, any>): void
afterTest(test: Record<string, any>): void
commandLogAdded (log: any): void
commandLogChanged (log: any): void
}