From dfe50511983b066cf7ea9ebd62ad1e17767f38e8 Mon Sep 17 00:00:00 2001 From: Alexey Yarmosh Date: Tue, 7 Nov 2023 12:27:10 +0300 Subject: [PATCH] test: add more adopted sockets tests --- src/lib/server.ts | 2 +- test/tests/unit/probe/router.test.ts | 21 +++++++++++++++++++++ test/tests/unit/ws/fetch-sockets.test.ts | 20 +++++++++++++++++++- 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/src/lib/server.ts b/src/lib/server.ts index 4bc65a51..81749970 100644 --- a/src/lib/server.ts +++ b/src/lib/server.ts @@ -8,6 +8,7 @@ import { populateMemList as populateIpWhiteList } from './geoip/whitelist.js'; import { populateCitiesList } from './geoip/city-approximation.js'; import { adoptedProbes } from './adopted-probes.js'; import { reconnectProbes } from './ws/helper/reconnect-probes.js'; +import { getWsServer } from './ws/server.js'; export const createServer = async (): Promise => { await initRedis(); @@ -28,7 +29,6 @@ export const createServer = async (): Promise => { reconnectProbes(); - const { getWsServer } = await import('./ws/server.js'); const { getHttpServer } = await import('./http/server.js'); const httpServer = getHttpServer(); diff --git a/test/tests/unit/probe/router.test.ts b/test/tests/unit/probe/router.test.ts index 9b9adc05..d6399ecd 100644 --- a/test/tests/unit/probe/router.test.ts +++ b/test/tests/unit/probe/router.test.ts @@ -819,5 +819,26 @@ describe('probe router', () => { expect(probes.length).to.equal(0); }); + + it('should return match for user tag', async () => { + const socket = await buildSocket(String(Date.now()), location); + socket.data.probe.tags = [ + ...socket.data.probe.tags, + { type: 'user', value: 'u-jimaek-dashboardtag' }, + ]; + + const sockets: DeepPartial = [ socket ]; + + const locations: Location[] = [ + { tags: [ 'u-jimaek-dashboardtag' ] }, + ]; + + fetchSocketsMock.resolves(sockets as never); + + const probes = await router.findMatchingProbes(locations, 100); + + expect(probes.length).to.equal(1); + expect(probes[0]!.location.country).to.equal('GB'); + }); }); }); diff --git a/test/tests/unit/ws/fetch-sockets.test.ts b/test/tests/unit/ws/fetch-sockets.test.ts index 456f5d3c..08649c1a 100644 --- a/test/tests/unit/ws/fetch-sockets.test.ts +++ b/test/tests/unit/ws/fetch-sockets.test.ts @@ -19,8 +19,8 @@ describe('fetchSockets', () => { }); beforeEach(async () => { - ({ fetchSockets } = await import('../../../../src/lib/ws/fetch-sockets.js')); sinon.resetHistory(); + ({ fetchSockets } = await import('../../../../src/lib/ws/fetch-sockets.js')); }); after(() => { @@ -162,6 +162,24 @@ describe('fetchSockets', () => { ]); }); + it('should return same socket object if there is no adoption data', async () => { + const socket1 = { id: '1', data: { probe: { ipAddress: '' } } }; + const socket2 = { id: '2', data: { probe: { ipAddress: '' } } }; + fetchRawSockets.resolves([ socket1, socket2 ]); + getByIp.returns(undefined); + const result = await fetchSockets(); + expect(result[0]).to.equal(socket1); + expect(result[1]).to.equal(socket2); + }); + + it('should return same socket object if adoption data is not edited', async () => { + const socket = { id: '3', data: { probe: { ipAddress: '' } } }; + fetchRawSockets.resolves([ socket ]); + getByIp.returns({ isCustomCity: false, tags: [] }); + const result = await fetchSockets(); + expect(result[0]).to.equal(socket); + }); + it('multiple calls to fetchSockets should result in one socket.io fetchSockets call', async () => { expect(fetchRawSockets.callCount).to.equal(0);