Skip to content

Commit

Permalink
test: update synced-probe-list tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alexey-yarmosh committed Apr 11, 2024
1 parent e93273f commit ed045fa
Showing 1 changed file with 44 additions and 16 deletions.
60 changes: 44 additions & 16 deletions test/tests/unit/ws/synced-probe-list.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ import { expect } from 'chai';

import type { WsServerNamespace } from '../../../../src/lib/ws/server.js';
import { SyncedProbeList } from '../../../../src/lib/ws/synced-probe-list.js';
import { type AdoptedProbe, AdoptedProbes } from '../../../../src/lib/adopted-probes.js';
import type { Probe } from '../../../../src/probe/types.js';
import { getRegionByCountry } from '../../../../src/lib/location/location.js';
import { getRedisClient } from '../../../../src/lib/redis/client.js';
import { ProbeOverride } from '../../../../src/lib/probe-override.js';
import { AdminData } from '../../../../src/lib/admin-data.js';

describe('SyncedProbeList', () => {
const sandbox = sinon.createSandbox();
Expand Down Expand Up @@ -36,9 +34,7 @@ describe('SyncedProbeList', () => {
},
} as unknown as WsServerNamespace;

const adoptedProbes = sandbox.createStubInstance(AdoptedProbes);
const adminData = new AdminData(sandbox.stub() as any);
const probeOverride = new ProbeOverride(adoptedProbes, adminData);
const probeOverride = sandbox.createStubInstance(ProbeOverride);

let syncedProbeList: SyncedProbeList;

Expand All @@ -47,9 +43,8 @@ describe('SyncedProbeList', () => {
redisJsonGet.callThrough();
redisPExpire.callThrough();
localFetchSocketsStub.resolves([]);
adoptedProbes.getUpdatedLocation.callThrough();
adoptedProbes.getUpdatedTags.callThrough();
adoptedProbes.getUpdatedProbes.callThrough();
probeOverride.addAdminData.returnsArg(0);
probeOverride.addAdoptedData.returnsArg(0);

syncedProbeList = new SyncedProbeList(redisClient, ioNamespace, probeOverride);
});
Expand Down Expand Up @@ -282,17 +277,17 @@ describe('SyncedProbeList', () => {
expect(syncedProbeList.getProbes()).to.be.empty;
});

it('applies adoption data to getProbes()/fetchProbes() but not to getRawProbes()', async () => {
const sockets = [
{ data: { probe: { client: 'A', location: { ...location }, tags: [], ipAddress: '1.1.1.1' } } },
{ data: { probe: { client: 'B', location: { ...location }, tags: [] } } },
];
it('applies adoption data to getProbes()/fetchProbes() but not to getRawProbes()/getProbesWithAdminData()', async () => {
const probe1 = { client: 'A', location: { ...location }, tags: [], ipAddress: '1.1.1.1' } as unknown as Probe;
const probe2 = { client: 'B', location: { ...location }, tags: [] } as unknown as Probe;
const sockets = [{ data: { probe: probe1 } }, { data: { probe: probe2 } }];

const tags = [{ type: 'user', value: 'u-name-tag1' }] as Probe['tags'];

const tags = [{ type: 'user', value: 'u-name-tag1' }] as AdoptedProbe['tags'];
const adoptedProbe = { tags } as AdoptedProbe;
const adoptedProbe = { tags } as Probe;

localFetchSocketsStub.resolves(sockets);
adoptedProbes.getByIp.withArgs('1.1.1.1').returns(adoptedProbe);
probeOverride.addAdoptedData.returns([ adoptedProbe, probe2 ]);

const fetchedProbesPromise = syncedProbeList.fetchProbes();
clock.tick(1);
Expand All @@ -307,10 +302,43 @@ describe('SyncedProbeList', () => {
expect(fetchedProbes[0]).to.deep.include({ tags });
expect(fetchedProbes[1]).not.to.deep.include({ tags });

expect(syncedProbeList.getProbesWithAdminData()[0]).not.to.deep.include({ tags });
expect(syncedProbeList.getProbesWithAdminData()[1]).not.to.deep.include({ tags });

expect(syncedProbeList.getRawProbes()[0]).not.to.deep.include({ tags });
expect(syncedProbeList.getRawProbes()[1]).not.to.deep.include({ tags });
});

it('applies admin location override data to getProbes()/fetchProbes()/getProbesWithAdminData() but not to getRawProbes()', async () => {
const probe1 = { client: 'A', location: { ...location }, tags: [], ipAddress: '1.1.1.1' } as unknown as Probe;
const probe2 = { client: 'B', location: { ...location }, tags: [] } as unknown as Probe;
const sockets = [{ data: { probe: probe1 } }, { data: { probe: probe2 } }];

const updatedProbe = { probe1, location: { ...probe1.location, city: 'Miami' } } as unknown as Probe;

localFetchSocketsStub.resolves(sockets);
probeOverride.addAdminData.returns([ updatedProbe, probe2 ]);

const fetchedProbesPromise = syncedProbeList.fetchProbes();
clock.tick(1);

await syncedProbeList.sync();
const fetchedProbes = await fetchedProbesPromise;

expect(localFetchSocketsStub.callCount).to.equal(1);
expect(syncedProbeList.getProbes()[0]?.location.city).to.deep.equal('Miami');
expect(syncedProbeList.getProbes()[1]?.location.city).to.deep.equal('The New York City');

expect(fetchedProbes[0]?.location.city).to.deep.equal('Miami');
expect(fetchedProbes[1]?.location.city).to.deep.equal('The New York City');

expect(syncedProbeList.getProbesWithAdminData()[0]?.location.city).to.deep.equal('Miami');
expect(syncedProbeList.getProbesWithAdminData()[1]?.location.city).to.deep.equal('The New York City');

expect(syncedProbeList.getRawProbes()[0]?.location.city).to.deep.equal('The New York City');
expect(syncedProbeList.getRawProbes()[1]?.location.city).to.deep.equal('The New York City');
});

it('resolves fetchProbes() only after new data arrives', async () => {
const fetchedProbesPromise = syncedProbeList.fetchProbes();

Expand Down

0 comments on commit ed045fa

Please sign in to comment.