From 068b698de87174956087a9e749c2ecfdf59f5b75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antonio=20Barc=C3=A9los?= Date: Mon, 19 Feb 2024 14:28:41 +0100 Subject: [PATCH 1/2] testkit: Enable browser tests log on terminal Introduce a Console message in the protocol between the LocalController on Browser and the RemoteController in Node for enabling the logs and other console messages to be printed in terminal and be available in artifacts. This feature should help debugging the errors in the testing pipeline. --- packages/testkit-backend/src/channel/websocket.js | 13 ++++++++----- packages/testkit-backend/src/controller/remote.js | 7 ++++++- packages/testkit-backend/src/index.js | 5 ++++- packages/testkit-backend/src/responses.js | 2 +- 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/packages/testkit-backend/src/channel/websocket.js b/packages/testkit-backend/src/channel/websocket.js index 26c248433..1e0e3eeec 100644 --- a/packages/testkit-backend/src/channel/websocket.js +++ b/packages/testkit-backend/src/channel/websocket.js @@ -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) { @@ -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) { diff --git a/packages/testkit-backend/src/controller/remote.js b/packages/testkit-backend/src/controller/remote.js index 66cf1bf25..ccf8fcc3f 100644 --- a/packages/testkit-backend/src/controller/remote.js +++ b/packages/testkit-backend/src/controller/remote.js @@ -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. @@ -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) })) diff --git a/packages/testkit-backend/src/index.js b/packages/testkit-backend/src/index.js index feaae6274..224e40f3d 100644 --- a/packages/testkit-backend/src/index.js +++ b/packages/testkit-backend/src/index.js @@ -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) { @@ -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) } diff --git a/packages/testkit-backend/src/responses.js b/packages/testkit-backend/src/responses.js index 3d3405fa3..c6d67a3c4 100644 --- a/packages/testkit-backend/src/responses.js +++ b/packages/testkit-backend/src/responses.js @@ -160,6 +160,6 @@ export function FakeTimeAck () { return response('FakeTimeAck', {}) } -function response (name, data) { +export function response (name, data) { return { name, data } } From 9369dbe732f45856fc0bd790afcf27e727074438 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antonio=20Barc=C3=A9los?= Date: Mon, 19 Feb 2024 15:04:04 +0100 Subject: [PATCH 2/2] Push missing file --- .../testkit-backend/src/console.remote.js | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 packages/testkit-backend/src/console.remote.js diff --git a/packages/testkit-backend/src/console.remote.js b/packages/testkit-backend/src/console.remote.js new file mode 100644 index 000000000..3889dc3e7 --- /dev/null +++ b/packages/testkit-backend/src/console.remote.js @@ -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: () => { + // eslint-disable-next-line no-global-assign + console = originalConsole + } +}