diff --git a/test/tests/unit/adopted-probes.test.ts b/test/tests/unit/adopted-probes.test.ts index c2b80b20..babdefb6 100644 --- a/test/tests/unit/adopted-probes.test.ts +++ b/test/tests/unit/adopted-probes.test.ts @@ -27,7 +27,7 @@ describe('AdoptedProbes', () => { sandbox.restore(); }); - it('startSync method should sync the data and start regular syncs', async () => { + it('syncDashboardData method should sync the data', async () => { const adoptedProbes = new AdoptedProbes(sqlStub as unknown as Knex, fetchSocketsStub); selectStub.resolves([{ ip: '1.1.1.1', uuid: '1-1-1-1-1', lastSyncDate: '1970-01-01' }]); @@ -39,7 +39,8 @@ describe('AdoptedProbes', () => { expect(sqlStub.callCount).to.equal(1); expect(sqlStub.args[0]).deep.equal([ 'adopted_probes' ]); expect(selectStub.callCount).to.equal(1); - expect(selectStub.args[0]).deep.equal([ 'ip', 'uuid', 'lastSyncDate' ]); + + expect(selectStub.args[0]).deep.equal([ 'ip', 'uuid', 'lastSyncDate', 'status', 'version', 'country', 'city', 'latitude', 'longitude', 'asn', 'network' ]); }); it('class should update uuid if it is wrong', async () => { @@ -80,7 +81,7 @@ describe('AdoptedProbes', () => { expect(deleteStub.callCount).to.equal(0); }); - it('class should do nothing if adopted probe was not found and lastSyncDate > 30 days away', async () => { + it('class should delete adoption if adopted probe was not found and lastSyncDate > 30 days away', async () => { const adoptedProbes = new AdoptedProbes(sqlStub as unknown as Knex, fetchSocketsStub); selectStub.resolves([{ ip: '1.1.1.1', uuid: '1-1-1-1-1', lastSyncDate: '1969-11-15' }]); fetchSocketsStub.resolves([]); @@ -91,7 +92,6 @@ describe('AdoptedProbes', () => { expect(whereStub.args[0]).to.deep.equal([{ ip: '1.1.1.1' }]); expect(updateStub.callCount).to.equal(0); expect(deleteStub.callCount).to.equal(1); - expect(deleteStub.callCount).to.equal(1); }); it('class should do nothing if probe data is actual', async () => { @@ -144,4 +144,103 @@ describe('AdoptedProbes', () => { expect(updateStub.callCount).to.equal(0); expect(deleteStub.callCount).to.equal(0); }); + + it('class should update probe meta info if it is outdated', async () => { + const adoptedProbes = new AdoptedProbes(sqlStub as unknown as Knex, fetchSocketsStub); + selectStub.resolves([{ + ip: '1.1.1.1', + uuid: '1-1-1-1-1', + lastSyncDate: '1970-01-01', + status: 'ready', + version: '0.26.0', + country: 'IE', + city: 'Dublin', + latitude: 53.3331, + longitude: -6.2489, + asn: 16509, + network: 'Amazon.com, Inc.', + }]); + + fetchSocketsStub.resolves([{ + data: { + probe: { + ipAddress: '1.1.1.1', + uuid: '1-1-1-1-1', + status: 'initializing', + version: '0.27.0', + nodeVersion: 'v18.17.0', + location: { + continent: 'EU', + region: 'Northern Europe', + country: 'GB', + city: 'London', + asn: 20473, + latitude: 53.3331, + longitude: -6.2489, + network: 'The Constant Company, LLC', + }, + }, + }, + }]); + + await adoptedProbes.syncDashboardData(); + + expect(whereStub.callCount).to.equal(1); + expect(whereStub.args[0]).to.deep.equal([{ ip: '1.1.1.1' }]); + expect(updateStub.callCount).to.equal(1); + + expect(updateStub.args[0]).to.deep.equal([{ + status: 'initializing', + version: '0.27.0', + country: 'GB', + city: 'London', + asn: 20473, + network: 'The Constant Company, LLC', + }]); + }); + + it('class should update probe meta info if it is outdated', async () => { + const adoptedProbes = new AdoptedProbes(sqlStub as unknown as Knex, fetchSocketsStub); + selectStub.resolves([{ + ip: '1.1.1.1', + uuid: '1-1-1-1-1', + lastSyncDate: '1970-01-01', + status: 'ready', + version: '0.26.0', + country: 'IE', + city: 'Dublin', + latitude: 53.3331, + longitude: -6.2489, + asn: 16509, + network: 'Amazon.com, Inc.', + }]); + + fetchSocketsStub.resolves([{ + data: { + probe: { + ipAddress: '1.1.1.1', + uuid: '1-1-1-1-1', + status: 'ready', + version: '0.26.0', + nodeVersion: 'v18.17.0', + location: { + continent: 'EU', + region: 'Northern Europe', + country: 'IE', + city: 'Dublin', + asn: 16509, + latitude: 53.3331, + longitude: -6.2489, + network: 'Amazon.com, Inc.', + }, + }, + }, + }]); + + await adoptedProbes.syncDashboardData(); + + expect(whereStub.callCount).to.equal(0); + expect(updateStub.callCount).to.equal(0); + expect(deleteStub.callCount).to.equal(0); + }); });