From d34b964cc225f460ecf452bb80caf52552c251f4 Mon Sep 17 00:00:00 2001 From: SamTV12345 <40429738+SamTV12345@users.noreply.github.com> Date: Fri, 8 Mar 2024 18:50:29 +0100 Subject: [PATCH] Fixed frontend tests. (#6210) * Fixed frontend tests. * Use old socket io syntax. * uSE ESM: * Remove padvar. * Remove cypress. --- src/node/db/DB.ts | 4 ++-- src/node/handler/PadMessageHandler.ts | 1 - src/node/hooks/express/socketio.ts | 27 ++++++++++------------- src/package.json | 3 +-- src/tests/frontend/specs/collab_client.js | 6 ++--- 5 files changed, 18 insertions(+), 23 deletions(-) diff --git a/src/node/db/DB.ts b/src/node/db/DB.ts index 1daab8dd3..542da6735 100644 --- a/src/node/db/DB.ts +++ b/src/node/db/DB.ts @@ -21,9 +21,9 @@ * limitations under the License. */ -const ueberDB = require('ueberdb2'); +import ueberDB from 'ueberdb2'; const settings = require('../utils/Settings'); -const log4js = require('log4js'); +import log4js from 'log4js'; const stats = require('../stats') const logger = log4js.getLogger('ueberDB'); diff --git a/src/node/handler/PadMessageHandler.ts b/src/node/handler/PadMessageHandler.ts index 221af33d2..bbeef736a 100644 --- a/src/node/handler/PadMessageHandler.ts +++ b/src/node/handler/PadMessageHandler.ts @@ -957,7 +957,6 @@ const handleClientReady = async (socket:any, message: typeof ChatMessage) => { rev: pad.getHeadRevisionNumber(), time: currentTime, }, - socketTransportProtocols: settings.socketTransportProtocols, colorPalette: authorManager.getColorPalette(), clientIp: '127.0.0.1', userColor: authorColorId, diff --git a/src/node/hooks/express/socketio.ts b/src/node/hooks/express/socketio.ts index 896d0779f..961e2cfd3 100644 --- a/src/node/hooks/express/socketio.ts +++ b/src/node/hooks/express/socketio.ts @@ -2,12 +2,12 @@ import {ArgsExpressType} from "../../types/ArgsExpressType"; -const events = require('events'); +import events from 'events'; const express = require('../express'); -const log4js = require('log4js'); +import log4js from 'log4js'; const proxyaddr = require('proxy-addr'); const settings = require('../../utils/Settings'); -import {Server} from 'socket.io' +import {Server, Socket} from 'socket.io' const socketIORouter = require('../../handler/SocketIORouter'); const hooks = require('../../../static/js/pluginfw/hooks'); const padMessageHandler = require('../../handler/PadMessageHandler'); @@ -17,7 +17,7 @@ const logger = log4js.getLogger('socket.io'); const sockets = new Set(); const socketsEvents = new events.EventEmitter(); -exports.expressCloseServer = async () => { +export const expressCloseServer = async () => { if (io == null) return; logger.info('Closing socket.io engine...'); // Close the socket.io engine to disconnect existing clients and reject new clients. Don't call @@ -48,7 +48,7 @@ exports.expressCloseServer = async () => { logger.info('All socket.io clients have disconnected'); }; -exports.socketSessionMiddleware = (args: any) => (socket: any, next: Function) => { +const socketSessionMiddleware = (args: any) => (socket: any, next: Function) => { const req = socket.request; // Express sets req.ip but socket.io does not. Replicate Express's behavior here. if (req.ip == null) { @@ -65,19 +65,16 @@ exports.socketSessionMiddleware = (args: any) => (socket: any, next: Function) = express.sessionMiddleware(req, {}, next); }; -exports.expressCreateServer = (hookName:string, args:ArgsExpressType, cb:Function) => { +export const expressCreateServer = (hookName:string, args:ArgsExpressType, cb:Function) => { // init socket.io and redirect all requests to the MessageHandler // there shouldn't be a browser that isn't compatible to all // transports in this list at once // e.g. XHR is disabled in IE by default, so in IE it should use jsonp-polling - io = new Server({ + io = new Server(args.server,{ transports: settings.socketTransportProtocols, - }) - - io.attach(args.server, { cookie: false, maxHttpBufferSize: settings.socketIo.maxHttpBufferSize, - }); + }) io.on('connection', (socket:any) => { sockets.add(socket); @@ -92,11 +89,11 @@ exports.expressCreateServer = (hookName:string, args:ArgsExpressType, cb:Functio }); }); - io.use(exports.socketSessionMiddleware(args)); - + io.use(socketSessionMiddleware(args)); + // Temporary workaround so all clients go through middleware and handle connection - io.of('/pluginfw/installer').use(exports.socketSessionMiddleware(args)) - io.of('/settings').use(exports.socketSessionMiddleware(args)) + io.of('/pluginfw/installer').use(socketSessionMiddleware(args)) + io.of('/settings').use(socketSessionMiddleware(args)) io.use((socket:any, next:Function) => { socket.conn.on('packet', (packet:string) => { diff --git a/src/package.json b/src/package.json index cc6f2e884..d680d767a 100644 --- a/src/package.json +++ b/src/package.json @@ -88,7 +88,6 @@ "@types/sinon": "^17.0.3", "@types/supertest": "^6.0.2", "@types/underscore": "^1.11.15", - "cypress": "^13.6.6", "eslint": "^8.57.0", "eslint-config-etherpad": "^3.0.22", "etherpad-cli-client": "^3.0.1", @@ -114,7 +113,7 @@ }, "scripts": { "lint": "eslint .", - "test": "mocha --import=tsx --timeout 120000 --recursive tests/backend/specs/**.ts tests/backend/specs/**/*.ts", + "test": "mocha --import=tsx --timeout 120000 --recursive tests/backend/specs/**.ts tests/backend/specs/**/*.ts ../node_modules/ep_*/static/tests/backend/specs", "test-container": "mocha --import=tsx --timeout 5000 tests/container/specs/api", "dev": "node --import tsx node/server.ts", "prod": "node --import tsx node/server.ts", diff --git a/src/tests/frontend/specs/collab_client.js b/src/tests/frontend/specs/collab_client.js index 307e37908..9cc943e73 100644 --- a/src/tests/frontend/specs/collab_client.js +++ b/src/tests/frontend/specs/collab_client.js @@ -36,8 +36,8 @@ describe('Messages in the COLLABROOM', function () { // User 1 starts sending a change to the server. let sendStarted; const finishSend = (() => { - const socketJsonObj = helper.padChrome$.window.pad.socket.json; - const sendBackup = socketJsonObj.send; + const socketJsonObj = helper.padChrome$.window.pad.socket; + const sendBackup = socketJsonObj.emit; let startSend; sendStarted = new Promise((resolve) => { startSend = resolve; }); let finishSend; @@ -46,7 +46,7 @@ describe('Messages in the COLLABROOM', function () { startSend(); sendP.then(() => { socketJsonObj.send = sendBackup; - socketJsonObj.send(...args); + socketJsonObj.send('message', ...args); }); }; return finishSend;