From 29bbd600829898614243c7aa73b5297cc4b7309b Mon Sep 17 00:00:00 2001 From: Benjamin Levesque <14175665+benjlevesque@users.noreply.github.com> Date: Mon, 27 Nov 2023 16:22:12 +0100 Subject: [PATCH] test: reduce flaky tests --- README.md | 12 --- packages/payment-detection/README.md | 16 +--- .../test/eth/etherscan-fixtures.ts | 84 +++++++++++++++++++ .../test/eth/info-retriever.test.ts | 74 +++++++++------- .../test/payment/erc777-stream.test.ts | 25 ------ 5 files changed, 129 insertions(+), 82 deletions(-) create mode 100644 packages/payment-detection/test/eth/etherscan-fixtures.ts diff --git a/README.md b/README.md index a43678d72f..9944fffd15 100644 --- a/README.md +++ b/README.md @@ -78,18 +78,6 @@ yarn run lint ### Test -Disable API tests OR define all required explorer API keys. - -```bash -export DISABLE_API_TESTS=1 -# OR -export EXPLORER_API_KEY_MAINNET= -export EXPLORER_API_KEY_RINKEBY= -export EXPLORER_API_KEY_FUSE= -export EXPLORER_API_KEY_MATIC= -export EXPLORER_API_KEY_FANTOM= -``` - Test all the packages in the monorepo. ```bash diff --git a/packages/payment-detection/README.md b/packages/payment-detection/README.md index 63404465ef..4279b72e6d 100644 --- a/packages/payment-detection/README.md +++ b/packages/payment-detection/README.md @@ -67,18 +67,8 @@ yarn codegen # Test -The ETH `InfoRetriever` tests require explorer API keys. Before running the -payment-detection tests, define `DISABLE_API_TESTS` or define all required -explorer API keys. - -```bash -export DISABLE_API_TESTS=1 -# OR -export EXPLORER_API_KEY_MAINNET= -export EXPLORER_API_KEY_RINKEBY= -export EXPLORER_API_KEY_FUSE= -export EXPLORER_API_KEY_MATIC= -export EXPLORER_API_KEY_FANTOM= - yarn run test + +``` + ``` diff --git a/packages/payment-detection/test/eth/etherscan-fixtures.ts b/packages/payment-detection/test/eth/etherscan-fixtures.ts new file mode 100644 index 0000000000..281719e5d9 --- /dev/null +++ b/packages/payment-detection/test/eth/etherscan-fixtures.ts @@ -0,0 +1,84 @@ +import { BigNumber } from 'ethers'; + +export default [ + { + hash: '0x74d5dafdfaa023583d8bb6993a873babd403a05b2286e556e2617801b130cb8e', + type: 0, + accessList: null, + blockHash: '0xcd58c67561d7b94447e8e9d313f5e7c343f9fe5606961f4f33142bafea005cd5', + blockNumber: 9081997, + transactionIndex: 12, + confirmations: 9581715, + from: '0xc12F17Da12cd01a9CDBB216949BA0b41A6Ffc4EB', + gasPrice: BigNumber.from('0x04a817c800'), + gasLimit: BigNumber.from('0x5288'), + to: '0xc12F17Da12cd01a9CDBB216949BA0b41A6Ffc4EB', + value: BigNumber.from('0x05'), + nonce: 17, + data: '0xa93299ed2555d098', + creates: null, + chainId: 0, + timestamp: 1575964424, + }, + { + hash: '0x0b53c5296a7b286fef52336529f3934584fea116725d1fe4c59552e926229059', + type: 0, + accessList: null, + blockHash: '0x8535994e0a366ccfc61f5f925ce59404346569342bda1d80ccedfa6fed24c21a', + blockNumber: 9082338, + transactionIndex: 151, + confirmations: 9581374, + from: '0xc12F17Da12cd01a9CDBB216949BA0b41A6Ffc4EB', + gasPrice: BigNumber.from('0x02540be400'), + gasLimit: BigNumber.from('0x5288'), + to: '0xc12F17Da12cd01a9CDBB216949BA0b41A6Ffc4EB', + value: BigNumber.from('0x21'), + nonce: 18, + data: '0x9649a1a4dd5854ed', + creates: null, + chainId: 0, + timestamp: 1575969371, + }, + + { + hash: '0x06d95c3889dcd974106e82fa27358549d9392d6fee6ea14fe1acedadc1013114', + type: 0, + accessList: null, + blockHash: '0x74705d798896a8dfefd3057170f95e6b3a5a05f735440c761cd6f17f4aabd422', + blockNumber: 10013330, + transactionIndex: 120, + confirmations: 8650382, + from: '0x74Ef019C1E9F11366c5c8DC4Ab556C16fe13B51F', + gasPrice: BigNumber.from('0x059682f000'), + gasLimit: BigNumber.from('0x5428'), + to: '0xc12F17Da12cd01a9CDBB216949BA0b41A6Ffc4EB', + value: BigNumber.from('0x02dd231b00'), + nonce: 205, + data: '0xc19da4923539c37f', + creates: null, + chainId: 0, + timestamp: 1588776378, + }, + + // polygonscan + + { + hash: '0x50af07756eb07bb0eb29943cb7206d8359c829aef7f6dad50f61b488c2790c1c', + type: 0, + accessList: null, + blockHash: '0x186a3f7a4a49ef302223bc72cd45a07f01a7c0c6bf1fc3127fc97de7879874f5', + blockNumber: 17182533, + transactionIndex: 108, + confirmations: 33274506, + from: '0x4E64C2d06d19D13061e62E291b2C4e9fe5679b93', + gasPrice: BigNumber.from('0x02f4d4f484'), + gasLimit: BigNumber.from('0x5288'), + to: '0x4E64C2d06d19D13061e62E291b2C4e9fe5679b93', + value: BigNumber.from('0x038d7ea4c68000'), + nonce: 11, + data: '0x5f05d421a5ed3558', + creates: null, + chainId: 0, + timestamp: 1627050587, + }, +]; diff --git a/packages/payment-detection/test/eth/info-retriever.test.ts b/packages/payment-detection/test/eth/info-retriever.test.ts index 7ddb7c4d67..e076bbe902 100644 --- a/packages/payment-detection/test/eth/info-retriever.test.ts +++ b/packages/payment-detection/test/eth/info-retriever.test.ts @@ -1,6 +1,8 @@ import { PaymentTypes } from '@requestnetwork/types'; import { EthInputDataInfoRetriever } from '../../src/eth/info-retriever'; import PaymentReferenceCalculator from '../../src/payment-reference-calculator'; +import etherscanFixtures from './etherscan-fixtures'; +import { providers } from 'ethers'; describe('api/eth/info-retriever', () => { // In this test, we're looking this transaction: @@ -45,38 +47,36 @@ describe('api/eth/info-retriever', () => { await expect(infoRetreiver.getTransferEvents()).rejects.toThrowError(); }); - // Utility for conditionally skipping tests - const describeIf = ( - condition: any, - ...args: [string | number | Function | jest.FunctionLike, jest.EmptyFunction] - ) => (condition ? describe(...args) : describe.skip(...args)); - // Skip tests if build is from external fork or API tests are disabled // External forks cannot access secret API keys - describeIf(!process.env.CIRCLE_PR_NUMBER && !process.env.DISABLE_API_TESTS, 'Multichain', () => { + describe('Multichain retriever', () => { + beforeAll(() => { + jest + .spyOn(providers.EtherscanProvider.prototype, 'getHistory') + .mockResolvedValue(etherscanFixtures as any); + }); // TODO temporary disable xDAI, CELO, Sokol, and Goerli // FIXME: API-based checks should run nightly and be mocked for CI - [ - 'mainnet', - // 'rinkeby', - // 'goerli', - // 'xdai', - // 'sokol', - 'fuse', - //'celo', - 'matic', - 'fantom', - ].forEach((network) => { - it(`Can get the balance on ${network}`, async () => { - const retriever = new EthInputDataInfoRetriever( - '0xc12F17Da12cd01a9CDBB216949BA0b41A6Ffc4EB', - PaymentTypes.EVENTS_NAMES.PAYMENT, - network, - '9649a1a4dd5854ed', - process.env[`EXPLORER_API_KEY_${network.toUpperCase()}`], - ); - await expect(retriever.getTransferEvents()).resolves.not.toThrow(); - }); + + it(`Can get the balance with the Multichain retriever`, async () => { + const retriever = new EthInputDataInfoRetriever( + '0xc12F17Da12cd01a9CDBB216949BA0b41A6Ffc4EB', + PaymentTypes.EVENTS_NAMES.PAYMENT, + 'mainnet', + '9649a1a4dd5854ed', + ); + await expect(retriever.getTransferEvents()).resolves.toMatchObject([ + { + amount: '33', + name: 'payment', + parameters: { + block: 9082338, + confirmations: 9581374, + txHash: '0x0b53c5296a7b286fef52336529f3934584fea116725d1fe4c59552e926229059', + }, + timestamp: 1575969371, + }, + ]); }); it('can detect a MATIC payment to self', async () => { @@ -93,12 +93,22 @@ describe('api/eth/info-retriever', () => { PaymentTypes.EVENTS_NAMES.PAYMENT, 'matic', paymentReference, - process.env[`EXPLORER_API_KEY_MATIC`], + 'AU9R1APE9478EN12K35SE8EAYDZCTIE2GE', ); - const events = await infoRetriever.getTransferEvents(); - expect(events).toHaveLength(1); - expect(events[0].amount).toBe('1000000000000000'); + const events = await infoRetriever.getTransferEvents(); + expect(events).toMatchObject([ + { + amount: '1000000000000000', + name: 'payment', + parameters: { + block: 17182533, + confirmations: 33274506, + txHash: '0x50af07756eb07bb0eb29943cb7206d8359c829aef7f6dad50f61b488c2790c1c', + }, + timestamp: 1627050587, + }, + ]); }); }); }); diff --git a/packages/payment-processor/test/payment/erc777-stream.test.ts b/packages/payment-processor/test/payment/erc777-stream.test.ts index 58c04bbce3..6feaa83764 100644 --- a/packages/payment-processor/test/payment/erc777-stream.test.ts +++ b/packages/payment-processor/test/payment/erc777-stream.test.ts @@ -92,31 +92,6 @@ describe('erc777-stream', () => { }); }); - describe('Superfluid framework', () => { - it.each([ - { network: 'goerli' }, - { network: 'matic' }, - // { network: 'xdai' }, - { network: 'optimism' }, - { network: 'avalanche' }, - { network: 'arbitrum-one' }, - ] as Array<{ network: CurrencyTypes.EvmChainName }>)( - 'Should initialize superfluid framework on $network', - async ({ network }) => { - const provider = getDefaultProvider(network); - const networkValidRequest = { - ...validRequest, - currencyInfo: { - ...validRequest.currencyInfo, - network, - }, - }; - const sf = await getSuperFluidFramework(networkValidRequest, provider); - expect(sf).toBeDefined(); - }, - ); - }); - describe('encodePayErc20FeeRequest (used to pay and swap to pay)', () => { it('should throw an error if the request is not erc777', async () => { const request = deepCopy(validRequest) as ClientTypes.IRequestData;