From a049a01aeb2b09edaaf46411bb764c14a607b343 Mon Sep 17 00:00:00 2001 From: Val Lorentz Date: Tue, 14 Mar 2023 21:24:06 +0100 Subject: [PATCH] Client: move socket connection out of the constructor It will make it easier to write tests for what used to be in the connect() method --- server/client.ts | 33 +++++++++++++++++++++++--------- server/clientManager.ts | 1 + server/plugins/inputs/connect.ts | 2 +- server/server.ts | 3 ++- test/tests/customhighlights.ts | 1 + 5 files changed, 29 insertions(+), 11 deletions(-) diff --git a/server/client.ts b/server/client.ts index 049c0744..c34ab0e3 100644 --- a/server/client.ts +++ b/server/client.ts @@ -180,8 +180,16 @@ class Client { this.registerPushSubscription(session, session.pushSubscription, true); } }); + } - (client.config.networks || []).forEach((network) => client.connect(network, true)); + connect() { + const client = this; + + if (client.networks.length !== 0) { + throw new Error(`${client.name} is already connected`); + } + + (client.config.networks || []).forEach((network) => client.connectToNetwork(network, true)); // Networks are stored directly in the client object // We don't need to keep it in the config object @@ -192,7 +200,7 @@ class Client { // Networks are created instantly, but to reduce server load on startup // We randomize the IRC connections and channel log loading - let delay = manager.clients.length * 500; + let delay = client.manager.clients.length * 500; client.networks.forEach((network) => { setTimeout(() => { network.channels.forEach((channel) => channel.loadMessages(client, network)); @@ -205,7 +213,7 @@ class Client { delay += 1000 + Math.floor(Math.random() * 1000); }); - client.fileHash = manager.getDataToSave(client).newHash; + client.fileHash = client.manager.getDataToSave(client).newHash; } } @@ -242,12 +250,10 @@ class Client { return false; } - connect(args: Record, isStartup = false) { + networkFromConfig(args: Record): Network { const client = this; - let channels: Chan[] = []; - // Get channel id for lobby before creating other channels for nicer ids - const lobbyChannelId = client.idChan++; + let channels: Chan[] = []; if (Array.isArray(args.channels)) { let badName = false; @@ -295,7 +301,7 @@ class Client { } // TODO; better typing for args - const network = new Network({ + return new Network({ uuid: args.uuid, name: String( args.name || (Config.values.lockNetwork ? Config.values.defaults.name : "") || "" @@ -323,6 +329,15 @@ class Client { proxyUsername: String(args.proxyUsername || ""), proxyPassword: String(args.proxyPassword || ""), }); + } + + connectToNetwork(args: Record, isStartup = false) { + const client = this; + + // Get channel id for lobby before creating other channels for nicer ids + const lobbyChannelId = client.idChan++; + + const network = this.networkFromConfig(args); // Set network lobby channel id network.getLobby().id = lobbyChannelId; @@ -363,7 +378,7 @@ class Client { if (!isStartup) { client.save(); - channels.forEach((channel) => channel.loadMessages(client, network)); + network.channels.forEach((channel) => channel.loadMessages(client, network)); } } diff --git a/server/clientManager.ts b/server/clientManager.ts index d32081e3..85055d88 100644 --- a/server/clientManager.ts +++ b/server/clientManager.ts @@ -145,6 +145,7 @@ class ClientManager { } } else { client = new Client(this, name, userConfig); + client.connect(); this.clients.push(client); } diff --git a/server/plugins/inputs/connect.ts b/server/plugins/inputs/connect.ts index 8ba60c20..85262e57 100644 --- a/server/plugins/inputs/connect.ts +++ b/server/plugins/inputs/connect.ts @@ -39,7 +39,7 @@ const input: PluginInputHandler = function (network, chan, cmd, args) { } const host = args[0]; - this.connect({host, port, tls}); + this.connectToNetwork({host, port, tls}); return true; }; diff --git a/server/server.ts b/server/server.ts index e5b005dd..5ecdea92 100644 --- a/server/server.ts +++ b/server/server.ts @@ -485,7 +485,7 @@ function initializeClient( data.commands = null; data.ignoreList = null; - client.connect(data); + client.connectToNetwork(data); } }); @@ -948,6 +948,7 @@ function performAuthentication(this: Socket, data) { if (Config.values.public) { client = new Client(manager!); + client.connect(); manager!.clients.push(client); socket.on("disconnect", function () { diff --git a/test/tests/customhighlights.ts b/test/tests/customhighlights.ts index d9a8cfd4..30f84c01 100644 --- a/test/tests/customhighlights.ts +++ b/test/tests/customhighlights.ts @@ -27,6 +27,7 @@ describe("Custom highlights", function () { }, } as any ); + client.connect(); logInfoStub.restore(); expect(userLoadedLog).to.equal("User test loaded\n");