Skip to content

Commit

Permalink
test: add more adopted sockets tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alexey-yarmosh committed Nov 7, 2023
1 parent 855fcc7 commit dfe5051
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/lib/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Server> => {
await initRedis();
Expand All @@ -28,7 +29,6 @@ export const createServer = async (): Promise<Server> => {

reconnectProbes();

const { getWsServer } = await import('./ws/server.js');
const { getHttpServer } = await import('./http/server.js');

const httpServer = getHttpServer();
Expand Down
21 changes: 21 additions & 0 deletions test/tests/unit/probe/router.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<RemoteProbeSocket[]> = [ 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');
});
});
});
20 changes: 19 additions & 1 deletion test/tests/unit/ws/fetch-sockets.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit dfe5051

Please sign in to comment.