diff --git a/tools/erc-repository-indexer/erc-contract-indexer/tests/unit/services/byteCodeAnalyzer.test.ts b/tools/erc-repository-indexer/erc-contract-indexer/tests/unit/services/byteCodeAnalyzer.test.ts index 28f775bc6..1176a63d3 100644 --- a/tools/erc-repository-indexer/erc-contract-indexer/tests/unit/services/byteCodeAnalyzer.test.ts +++ b/tools/erc-repository-indexer/erc-contract-indexer/tests/unit/services/byteCodeAnalyzer.test.ts @@ -36,12 +36,14 @@ describe('ByteCodeAnalyzer', () => { const mockContractCallResponse = testConstants.MOCK_CONTRACT_CALL_RESPONSE; const mockValidMirrorNodeUrl = 'mock-mirror-node.com'; const mockValidMirrorNodeUrlWeb3 = 'mock-mirror-node-web3.com'; + const mockScanningLimit = 39; beforeEach(() => { byteCodeAnalyzer = new ByteCodeAnalyzer(); contractScannerService = new ContractScannerService( mockValidMirrorNodeUrl, - mockValidMirrorNodeUrlWeb3 + mockValidMirrorNodeUrlWeb3, + mockScanningLimit ); }); diff --git a/tools/erc-repository-indexer/erc-contract-indexer/tests/unit/services/config.test.ts b/tools/erc-repository-indexer/erc-contract-indexer/tests/unit/services/config.test.ts index c9a47ac76..6db2f56b6 100644 --- a/tools/erc-repository-indexer/erc-contract-indexer/tests/unit/services/config.test.ts +++ b/tools/erc-repository-indexer/erc-contract-indexer/tests/unit/services/config.test.ts @@ -179,4 +179,40 @@ describe('ConfigService', () => { expect(startingPoint).toBe(mockStartingPoint); expect(mockRetrieveNextPointer).toHaveBeenCalled(); }); + + it('should return default value, 100, if SCAN_CONTRACT_LIMIT is undefined', async () => { + process.env.HEDERA_NETWORK = mockValidHederaNetwork; + process.env.MIRROR_NODE_URL = mockValidMirrorNodeUrl; + delete process.env.SCAN_CONTRACT_LIMIT; + + const configService = new ConfigService(); + + expect(configService.getScanContractLimit()).toEqual(100); + }); + + it('should return dynamic SCAN_CONTRACT_LIMIT value', async () => { + process.env.HEDERA_NETWORK = mockValidHederaNetwork; + process.env.MIRROR_NODE_URL = mockValidMirrorNodeUrl; + + const expectedLimit = 36; + process.env.SCAN_CONTRACT_LIMIT = expectedLimit.toString(); + + const configService = new ConfigService(); + + expect(configService.getScanContractLimit()).toEqual(expectedLimit); + }); + + it('should throw an error if SCAN_CONTRACT_LIMIT is set to invalid values', async () => { + process.env.HEDERA_NETWORK = mockValidHederaNetwork; + process.env.MIRROR_NODE_URL = mockValidMirrorNodeUrl; + + const invalidLimits = ['-3', '369', 'not a number']; + invalidLimits.forEach((limit) => { + process.env.SCAN_CONTRACT_LIMIT = limit; + + expect(() => { + configService = new ConfigService(); + }).toThrow(/SCAN_CONTRACT_LIMIT Is Not Properly Configured/); + }); + }); }); diff --git a/tools/erc-repository-indexer/erc-contract-indexer/tests/unit/services/contractScanner.test.ts b/tools/erc-repository-indexer/erc-contract-indexer/tests/unit/services/contractScanner.test.ts index 60f392101..1a8c75b79 100644 --- a/tools/erc-repository-indexer/erc-contract-indexer/tests/unit/services/contractScanner.test.ts +++ b/tools/erc-repository-indexer/erc-contract-indexer/tests/unit/services/contractScanner.test.ts @@ -42,6 +42,7 @@ const mockedHelper = Helper as jest.Mocked; describe('ContractScannerService', () => { const mockValidMirrorNodeUrl = 'mock-mirror-node.com'; const mockValidMirrorNodeUrlWeb3 = 'mock-mirror-node-web3.com'; + const mockScanningLimit = 39; let contractScannerService: ContractScannerService; @@ -53,7 +54,8 @@ describe('ContractScannerService', () => { contractScannerService = new ContractScannerService( mockValidMirrorNodeUrl, - mockValidMirrorNodeUrlWeb3 + mockValidMirrorNodeUrlWeb3, + mockScanningLimit ); }); diff --git a/tools/erc-repository-indexer/erc-contract-indexer/tests/unit/utils/helper.spec.ts b/tools/erc-repository-indexer/erc-contract-indexer/tests/unit/utils/helper.spec.ts new file mode 100644 index 000000000..69ad16ab4 --- /dev/null +++ b/tools/erc-repository-indexer/erc-contract-indexer/tests/unit/utils/helper.spec.ts @@ -0,0 +1,50 @@ +/*- + * + * Hedera Smart Contracts + * + * Copyright (C) 2024 Hedera Hashgraph, LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +import { Helper } from '../../../src/utils/helper'; + +describe('Helper', () => { + describe('buildUrl', () => { + const mockNext = + '/api/v1/contracts?limit=100&order=asc&contract.id=gt:0.0.5294198'; + const mockScanningLimit = 39; + + it('Should build a default next url', () => { + const expectedDefaultNext = '/api/v1/contracts?limit=100&order=asc'; + const defaultNext = Helper.buildUrl(null); + expect(defaultNext).toEqual(expectedDefaultNext); + }); + + it('Should return next link if provided', () => { + const nextLink = Helper.buildUrl(mockNext); + expect(nextLink).toEqual(mockNext); + }); + + it('Should return next link modified with scanningLimit if provided', () => { + const expectedNextLink = mockNext.replace( + '100', + mockScanningLimit.toString() + ); + + const nextLink = Helper.buildUrl(mockNext, mockScanningLimit); + expect(nextLink).toEqual(expectedNextLink); + }); + }); +});