Skip to content

Commit

Permalink
feat: enhance block explorer utility tests with edge cases and multip…
Browse files Browse the repository at this point in the history
…le explorers (#5147)

### Description
- Added comprehensive tests for block explorer utilities, covering edge
cases such as chains without block explorers and chains without API
URLs.
- Implemented tests for handling multiple block explorers, ensuring
correct explorer selection by index.
- Included tests for special chain names and proper URL handling,
particularly with trailing slashes.
<!--
What's included in this PR?
-->

### Drive-by changes

<!--
Are there any minor or drive-by changes also included?
-->

### Related issues

<!--
- Fixes #[issue number here]
-->

### Backward compatibility

<!--
Are these changes backward compatible? Are there any infrastructure
implications, e.g. changes that would prohibit deploying older commits
using this infra tooling?

Yes/No
-->

### Testing
All new tests have been implemented and verified for correctness.
<!--
What kind of testing have these changes undergone?

None/Manual/Unit Tests
-->
  • Loading branch information
tiendn authored Jan 11, 2025
1 parent 5765587 commit 585404d
Showing 1 changed file with 119 additions and 0 deletions.
119 changes: 119 additions & 0 deletions typescript/sdk/src/metadata/blockExplorer.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { expect } from 'chai';

import { ProtocolType } from '@hyperlane-xyz/utils';

import {
test1,
testCosmosChain,
Expand All @@ -8,6 +10,7 @@ import {

import {
getExplorerAddressUrl,
getExplorerApi,
getExplorerApiUrl,
getExplorerBaseUrl,
getExplorerTxUrl,
Expand Down Expand Up @@ -52,4 +55,120 @@ describe('Block explorer utils', () => {
);
});
});

describe('Edge cases', () => {
const emptyChain = {
protocol: ProtocolType.Ethereum,
name: 'empty',
domainId: 1,
chainId: 1,
rpcUrls: [{ http: 'https://empty.test' }],
};

const chainWithoutApi = {
protocol: ProtocolType.Ethereum,
name: 'noapi',
chainId: 1,
domainId: 1,
rpcUrls: [{ http: 'https://noapi.test' }],
blockExplorers: [
{
name: 'test',
url: 'https://test.com',
apiUrl: '',
},
],
};

it('handles chain without block explorers', () => {
expect(getExplorerBaseUrl(emptyChain)).to.be.null;
expect(getExplorerApi(emptyChain)).to.be.null;
expect(getExplorerTxUrl(emptyChain, '0x123')).to.be.null;
expect(getExplorerAddressUrl(emptyChain, '0x123')).to.be.null;
});

it('handles chain without api url', () => {
expect(getExplorerBaseUrl(chainWithoutApi)).to.equal('https://test.com/');
expect(getExplorerApi(chainWithoutApi)).to.be.null;
});
});

describe('Multiple block explorers', () => {
const multiExplorerChain = {
protocol: ProtocolType.Ethereum,
name: 'multi',
domainId: 1,
chainId: 1,
rpcUrls: [{ http: 'https://multi.test' }],
blockExplorers: [
{
name: 'first',
url: 'https://first.com',
apiUrl: 'https://api.first.com',
apiKey: 'key1',
},
{
name: 'second',
url: 'https://second.com',
apiUrl: 'https://api.second.com',
apiKey: 'key2',
},
],
};

it('uses correct explorer by index', () => {
expect(getExplorerBaseUrl(multiExplorerChain, 1)).to.equal(
'https://second.com/',
);
expect(getExplorerApiUrl(multiExplorerChain, 1)).to.equal(
'https://api.second.com/?apikey=key2',
);
});
});

describe('Special chain names with different common paths', () => {
const nautilusChain = {
protocol: ProtocolType.Ethereum,
name: 'nautilus',
chainId: 1,
domainId: 1,
rpcUrls: [{ http: 'https://nautilus.test' }],
blockExplorers: [
{
name: 'nautilus',
url: 'https://nautilus.com',
apiUrl: 'https://api.nautilus.com',
},
],
};

it('uses correct transaction path for special chains', () => {
expect(getExplorerTxUrl(nautilusChain, '0x123')).to.equal(
'https://nautilus.com/transaction/0x123',
);
});
});

describe('URL handling', () => {
const chainWithTrailingSlash = {
protocol: ProtocolType.Ethereum,
name: 'test',
domainId: 1,
chainId: 1,
rpcUrls: [{ http: 'https://test.chain' }],
blockExplorers: [
{
name: 'test',
url: 'https://test.com/',
apiUrl: 'https://api.test.com',
},
],
};

it('handles trailing slashes correctly', () => {
expect(getExplorerTxUrl(chainWithTrailingSlash, '0x123')).to.equal(
'https://test.com/tx/0x123',
);
});
});
});

0 comments on commit 585404d

Please sign in to comment.