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

testkit: Enable browser tests log on terminal #1181

Merged
merged 2 commits into from
Feb 19, 2024
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
13 changes: 8 additions & 5 deletions packages/testkit-backend/src/channel/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ export default class WebSocketChannel extends Channel {
if (!this._ws) {
this._ws = new WebSocket(this._adddress)
this._ws.onmessage = ({ data: message }) => {
console.log(message)
console.debug('[WebSocketChannel] Received messsage', message)
console.debug('[WebSocketChannel] Received message', message)
const { messageType, contextId, data } = JSON.parse(message)

switch (messageType) {
Expand All @@ -45,12 +44,16 @@ export default class WebSocketChannel extends Channel {
}
}

writeResponse (contextId, response) {
writeResponse (contextId, response, skipLogging) {
if (this._ws) {
console.debug('[WebSocketChannel] Writing response', { contextId, response })
if (!skipLogging) {
console.debug('[WebSocketChannel] Writing response', { contextId, response })
}
return this._ws.send(this._serialize({ contextId, response }))
}
console.error('[WebSocketChannel] Websocket is not connected')
if (!skipLogging) {
console.error('[WebSocketChannel] Websocket is not connected')
}
}

_serialize (val) {
Expand Down
31 changes: 31 additions & 0 deletions packages/testkit-backend/src/console.remote.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { response } from './responses'

const originalConsole = console

export default {
install: (channel) => {
// eslint-disable-next-line no-global-assign
console = new Proxy({}, {
get: (_, method) => (...args) => {
originalConsole[method].apply(originalConsole, args)
channel.writeResponse(null, response('Console', {
method,
args
}), true)
}
})
},
handleConsole: (message) => {
if (message.response.name === 'Console') {
const { method, args } = message.response.data
args[0] = typeof args[0] === 'string' ? `[RemoteConsole] ${args[0]}` : args[0]
console[method].apply(console, args)
return true
}
return false
},
uninstall: () => {
StephenCathcart marked this conversation as resolved.
Show resolved Hide resolved
// eslint-disable-next-line no-global-assign
console = originalConsole
}
}
7 changes: 6 additions & 1 deletion packages/testkit-backend/src/controller/remote.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Controller from './interface'
import { WebSocketServer } from 'ws'
import { createServer } from 'http'
import { HttpStaticServer } from '../infrastructure'
import consoleRemote from '../console.remote'

/**
* RemoteController handles the requests by sending them a remote client.
Expand Down Expand Up @@ -81,7 +82,11 @@ export default class RemoteController extends Controller {
this._ws = ws
this._ws.on('message', safeRun(buffer => {
const message = JSON.parse(buffer.toString())
console.debug('[RemoteController] Received messsage', message)

if (consoleRemote.handleConsole(message)) {
return
}

const { contextId, response } = message
this._writeResponse(contextId, response)
}))
Expand Down
5 changes: 4 additions & 1 deletion packages/testkit-backend/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { getShouldRunTest } from './skipped-tests'
import { createGetFeatures } from './feature'
import * as REQUEST_HANDLERS from './request-handlers.js'
import * as RX_REQUEST_HANDLERS from './request-handlers-rx.js'
import remoteConsole from './console.remote.js'

const SUPPORTED_TLS = (() => {
if (tls.DEFAULT_MAX_VERSION) {
Expand Down Expand Up @@ -40,7 +41,9 @@ function main () {

const newChannel = () => {
if (channelType.toUpperCase() === 'WEBSOCKET') {
return new WebSocketChannel(new URL(`ws://localhost:${backendPort}`))
const channel = new WebSocketChannel(new URL(`ws://localhost:${backendPort}`))
remoteConsole.install(channel)
return channel
}
return new SocketChannel(backendPort)
}
Expand Down
2 changes: 1 addition & 1 deletion packages/testkit-backend/src/responses.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,6 @@ export function FakeTimeAck () {
return response('FakeTimeAck', {})
}

function response (name, data) {
export function response (name, data) {
return { name, data }
}