From 2e0c6dacd767b1cd8f4a1366d80c6ff4f29d3a44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emanuel=20Tesa=C5=99?= Date: Thu, 9 Nov 2023 11:56:51 +0100 Subject: [PATCH] Fix tests --- src/gas-price/index.ts | 1 + src/update-feeds/update-feeds.test.ts | 28 ++++++++++++++++++------- src/update-feeds/update-feeds.ts | 2 +- src/update-feeds/update-transactions.ts | 2 +- test/e2e/gas-price.feature.ts | 2 +- test/e2e/update-feeds.feature.ts | 2 +- 6 files changed, 25 insertions(+), 12 deletions(-) create mode 100644 src/gas-price/index.ts diff --git a/src/gas-price/index.ts b/src/gas-price/index.ts new file mode 100644 index 00000000..04ea12fa --- /dev/null +++ b/src/gas-price/index.ts @@ -0,0 +1 @@ +export * from './gas-price'; diff --git a/src/update-feeds/update-feeds.test.ts b/src/update-feeds/update-feeds.test.ts index a38f6a72..531e36b5 100644 --- a/src/update-feeds/update-feeds.test.ts +++ b/src/update-feeds/update-feeds.test.ts @@ -11,6 +11,7 @@ import * as utilsModule from '../utils'; import * as dapiDataRegistryModule from './dapi-data-registry'; import * as updateFeedsModule from './update-feeds'; +import * as updateTransactionModule from './update-transactions'; jest.mock('../state'); @@ -145,8 +146,8 @@ describe(updateFeedsModule.runUpdateFeed.name, () => { it('fetches other batches in a staggered way and logs errors', async () => { // Prepare the mocked contract so it returns three batches (of size 1) of dAPIs and the second batch fails to load. - const firstBatch = generateReadDapiWithIndexResponse(); - const thirdBatch = generateReadDapiWithIndexResponse(); + const firstDapi = generateReadDapiWithIndexResponse(); + const thirdDapi = generateReadDapiWithIndexResponse(); const dapiDataRegistry = generateMockDapiDataRegistry(); jest .spyOn(dapiDataRegistryModule, 'getDapiDataRegistry') @@ -154,10 +155,10 @@ describe(updateFeedsModule.runUpdateFeed.name, () => { dapiDataRegistry.interface.decodeFunctionResult.mockImplementation((_fn, value) => value); dapiDataRegistry.callStatic.tryMulticall.mockResolvedValueOnce({ successes: [true, true], - returndata: [[ethers.BigNumber.from(3)], firstBatch], + returndata: [[ethers.BigNumber.from(3)], firstDapi], }); dapiDataRegistry.callStatic.tryMulticall.mockResolvedValueOnce({ successes: [false], returndata: [] }); - dapiDataRegistry.callStatic.tryMulticall.mockResolvedValueOnce({ successes: [true], returndata: [thirdBatch] }); + dapiDataRegistry.callStatic.tryMulticall.mockResolvedValueOnce({ successes: [true], returndata: [thirdDapi] }); const sleepCalls = [] as number[]; const originalSleep = utilsModule.sleep; jest.spyOn(utilsModule, 'sleep').mockImplementation(async (ms) => { @@ -174,7 +175,7 @@ describe(updateFeedsModule.runUpdateFeed.name, () => { signedApiUrlStore: { '31337': { 'some-test-provider': ['url-one'] } }, signedApiStore: {}, gasPriceStore: { - '123': { + '31337': { 'some-test-provider': { gasPrices: [], sponsorLastUpdateTimestampMs: { @@ -185,7 +186,13 @@ describe(updateFeedsModule.runUpdateFeed.name, () => { }, }) ); - jest.spyOn(updateFeedsModule, 'getFeedsToUpdate').mockImplementation(() => []); + jest + .spyOn(updateFeedsModule, 'getFeedsToUpdate') + .mockImplementation(() => [ + allowPartial({ dapiInfo: firstDapi }), + allowPartial({ dapiInfo: thirdDapi }), + ]); + jest.spyOn(updateTransactionModule, 'updateFeeds').mockResolvedValue([null, null]); await updateFeedsModule.runUpdateFeed( 'provider-name', @@ -197,7 +204,7 @@ describe(updateFeedsModule.runUpdateFeed.name, () => { DapiDataRegistry: '0xDD78254f864F97f65e2d86541BdaEf88A504D2B2', }, }), - '123' + '31337' ); // Expect the contract to fetch the batches to be called with the correct stagger time. @@ -212,7 +219,7 @@ describe(updateFeedsModule.runUpdateFeed.name, () => { 'Failed to get active dAPIs batch', new Error('One of the multicalls failed') ); - expect(logger.debug).toHaveBeenCalledTimes(6); + expect(logger.debug).toHaveBeenCalledTimes(7); expect(logger.debug).toHaveBeenNthCalledWith(1, 'Fetching first batch of dAPIs batches'); expect(logger.debug).toHaveBeenNthCalledWith(2, 'Processing batch of active dAPIs', expect.anything()); expect(logger.debug).toHaveBeenNthCalledWith(3, 'Fetching batches of active dAPIs', { @@ -226,5 +233,10 @@ describe(updateFeedsModule.runUpdateFeed.name, () => { batchIndex: 2, }); expect(logger.debug).toHaveBeenNthCalledWith(6, 'Processing batch of active dAPIs', expect.anything()); + expect(logger.debug).toHaveBeenNthCalledWith(7, 'Finished processing batches of active dAPIs', { + batchesCount: 3, + errorCount: 4, + successCount: 0, + }); }); }); diff --git a/src/update-feeds/update-feeds.ts b/src/update-feeds/update-feeds.ts index c851ff35..e0fce453 100644 --- a/src/update-feeds/update-feeds.ts +++ b/src/update-feeds/update-feeds.ts @@ -5,7 +5,7 @@ import { range, size, zip } from 'lodash'; import { calculateMedian, checkUpdateConditions } from '../condition-check'; import type { Chain } from '../config/schema'; import { INT224_MAX, INT224_MIN } from '../constants'; -import { clearSponsorLastUpdateTimestampMs } from '../gas-price/gas-price'; +import { clearSponsorLastUpdateTimestampMs } from '../gas-price'; import { logger } from '../logger'; import { getStoreDataPoint } from '../signed-data-store'; import { getState, updateState } from '../state'; diff --git a/src/update-feeds/update-transactions.ts b/src/update-feeds/update-transactions.ts index c53e1799..c129ef06 100644 --- a/src/update-feeds/update-transactions.ts +++ b/src/update-feeds/update-transactions.ts @@ -2,7 +2,7 @@ import type { Api3ServerV1 } from '@api3/airnode-protocol-v1'; import { go } from '@api3/promise-utils'; import { ethers } from 'ethers'; -import { getAirseekerRecommendedGasPrice } from '../gas-price/gas-price'; +import { getAirseekerRecommendedGasPrice } from '../gas-price'; import { logger } from '../logger'; import { getState, updateState } from '../state'; import type { SignedData, ChainId, ProviderName } from '../types'; diff --git a/test/e2e/gas-price.feature.ts b/test/e2e/gas-price.feature.ts index 12432d78..4cba011c 100644 --- a/test/e2e/gas-price.feature.ts +++ b/test/e2e/gas-price.feature.ts @@ -6,7 +6,7 @@ import { multiplyGasPrice, initializeGasStore, clearExpiredStoreGasPrices, -} from '../../src/gas-price/gas-price'; +} from '../../src/gas-price'; import { getState, updateState } from '../../src/state'; import { init } from '../fixtures/mock-config'; diff --git a/test/e2e/update-feeds.feature.ts b/test/e2e/update-feeds.feature.ts index 1877c74d..cea39be2 100644 --- a/test/e2e/update-feeds.feature.ts +++ b/test/e2e/update-feeds.feature.ts @@ -1,7 +1,7 @@ import { ethers } from 'ethers'; import { omit } from 'lodash'; -import { initializeGasStore } from '../../src/gas-price/gas-price'; +import { initializeGasStore } from '../../src/gas-price'; import { logger } from '../../src/logger'; import * as stateModule from '../../src/state'; import { runUpdateFeed } from '../../src/update-feeds';