Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: improve vns utils test coverage to 100% #899

Merged
merged 8 commits into from
May 24, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,20 @@ describe('Vechain base signer tests', () => {
});

describe('resolveName(vnsName)', () => {
test('Should return null if provider is not set', async () => {
const signer = new VechainPrivateKeySigner(
Buffer.from(
'7f9290cc44c5fd2b95fe21d6ad6fe5fa9c177e1cd6f3b4c96a97b13e09eaa158',
'hex'
),
null
);

const name = 'test-sdk.vet';
const result = await signer.resolveName(name);
expect(result).toEqual(null);
});

test('Should use vnsUtils.resolveName() to resolve an address by name', async () => {
const signer = new VechainPrivateKeySigner(
Buffer.from(
Expand Down
82 changes: 82 additions & 0 deletions packages/network/tests/thor-client/transactions/fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,88 @@ const buildTransactionBodyClausesTestCases = [
reserved: { features: 1 }
}
}
},
{
description:
'Should not modify "to" part of clauses when no name is used',
clauses: [
{
to: '0x0000000000000000000000000000456E65726779',
value: '0',
data: '0x'
},
{
to: null,
value: '0',
data: '0x'
},
{
to: 'vtho.test-sdk.vet',
value: '0',
data: '0x'
}
],
options: {
gasPriceCoef: 255,
expiration: 1000,
isDelegated: true,
dependsOn:
'0x9140e36f05000508465fd55d70947b99a78c84b3afa5e068b955e366b560935f' // Any valid tx id
},
expected: {
solo: {
chainTag: 246,
clauses: [
{
data: '0x',
to: '0x0000000000000000000000000000456E65726779',
value: '0'
},
{
to: null,
data: '0x',
value: '0'
},
{
data: '0x',
to: '0x0000000000000000000000000000456E65726779',
value: '0'
}
],
dependsOn:
'0x9140e36f05000508465fd55d70947b99a78c84b3afa5e068b955e366b560935f',
expiration: 1000,
gas: 100046,
gasPriceCoef: 255,
reserved: { features: 1 }
},
testnet: {
chainTag: 39,
clauses: [
{
data: '0x',
to: '0x0000000000000000000000000000456E65726779',
value: '0'
},
{
to: null,
data: '0x',
value: '0'
},
{
data: '0x',
to: '0x0000000000000000000000000000456E65726779',
value: '0'
}
],
dependsOn:
'0x9140e36f05000508465fd55d70947b99a78c84b3afa5e068b955e366b560935f',
expiration: 1000,
gas: 100046,
gasPriceCoef: 255,
reserved: { features: 1 }
}
}
}
];

Expand Down
175 changes: 175 additions & 0 deletions packages/network/tests/utils/vns/lookups.mock.solo.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
import {
afterEach,
beforeEach,
describe,
expect,
jest,
test
} from '@jest/globals';
import { ThorClient, vnsUtils } from '../../../src';
import { testnetUrl } from '../../fixture';

/**
* vnsUtils vet.domains tests
*
* @group unit/utils/vnsUtils
*/
describe('vnsUtils', () => {
/**
* ThorClient instances
*/
let thorClient: ThorClient;

/**
* Init thor client and provider before each test
*/
beforeEach(() => {
thorClient = ThorClient.fromUrl(testnetUrl);
});

/**
* Destroy thor client after each test
*/
afterEach(() => {
thorClient.destroy();
});

describe('resolveNames(string[])', () => {
test('Should use the correct resolveUtils based on the passed thor client', async () => {
const names = ['test-sdk-1.vet', 'test-sdk-2.vet'];
const executeCall = jest.fn(async () => {
return await Promise.reject(new Error('error'));
});
jest.spyOn(
thorClient.contracts,
'executeCall'
).mockImplementationOnce(executeCall);

await expect(
vnsUtils.resolveNames(thorClient, names)
).rejects.toThrow();
expect(executeCall).toHaveBeenCalledWith(
'0xc403b8EA53F707d7d4de095f0A20bC491Cf2bc94',
'function getAddresses(string[] names) returns (address[] addresses)',
[names]
);
});

test('Should return null if genesisBlock can not be loaded', async () => {
// Mock the getGenesisBlock method to return null
jest.spyOn(
thorClient.blocks,
'getGenesisBlock'
).mockResolvedValueOnce(null);

const addresses = await vnsUtils.resolveNames(thorClient, [
'test-sdk.vet'
]);
expect(addresses).toEqual([null]);
});

test('Should return null if genesis has no matching configuration unknown', async () => {
jest.spyOn(
thorClient.blocks,
'getGenesisBlock'
).mockResolvedValueOnce({
number: 0,
id: '0x00000000c05a20fbca2bf6ae3affba6af4a74b800b585bf7a4988aba7aea0000', // different genesis id
size: 170,
parentID:
'0xffffffff00000000000000000000000000000000000000000000000000000000',
timestamp: 1526400000,
gasLimit: 10000000,
beneficiary: '0x0000000000000000000000000000000000000000',
gasUsed: 0,
totalScore: 0,
txsRoot:
'0x45b0cfc220ceec5b7c1c62c4d4193d38e4eba48e8815729ce75f9c0ab0e4c1c0',
txsFeatures: 0,
stateRoot:
'0x93de0ffb1f33bc0af053abc2a87c4af44594f5dcb1cb879dd823686a15d68550',
receiptsRoot:
'0x45b0cfc220ceec5b7c1c62c4d4193d38e4eba48e8815729ce75f9c0ab0e4c1c0',
signer: '0x0000000000000000000000000000000000000000',
isTrunk: true,
transactions: []
});

const addresses = await vnsUtils.resolveNames(thorClient, [
'test-sdk.vet'
]);
expect(addresses).toEqual([null]);
});
});

describe('lookupAddresses(string[])', () => {
test('Should use the correct resolveUtils based on the passed thor client', async () => {
const addresses = [
'0x0000000000000000000000000000456E65726779',
'0x625fCe8dd8E2C05e82e77847F3da06AF6e55A7AF'
];
const executeCall = jest.fn(async () => {
return await Promise.reject(new Error('error'));
});
jest.spyOn(
thorClient.contracts,
'executeCall'
).mockImplementationOnce(executeCall);

await expect(
vnsUtils.lookupAddresses(thorClient, addresses)
).rejects.toThrow();
expect(executeCall).toHaveBeenCalledWith(
'0xc403b8EA53F707d7d4de095f0A20bC491Cf2bc94',
'function getNames(address[] addresses) returns (string[] names)',
[addresses]
);
});

test('Should return null if genesisBlock can not be loaded', async () => {
// Mock the method to return null
jest.spyOn(
thorClient.blocks,
'getGenesisBlock'
).mockResolvedValueOnce(null);

const names = await vnsUtils.lookupAddresses(thorClient, [
'0x0000000000000000000000000000000000000000'
]);
expect(names).toEqual([null]);
});

test('Should return null if genesis has no matching configuration unknown', async () => {
jest.spyOn(
thorClient.blocks,
'getGenesisBlock'
).mockResolvedValueOnce({
number: 0,
id: '0x00000000c05a20fbca2bf6ae3affba6af4a74b800b585bf7a4988aba7aea0000', // different genesis id
size: 170,
parentID:
'0xffffffff00000000000000000000000000000000000000000000000000000000',
timestamp: 1526400000,
gasLimit: 10000000,
beneficiary: '0x0000000000000000000000000000000000000000',
gasUsed: 0,
totalScore: 0,
txsRoot:
'0x45b0cfc220ceec5b7c1c62c4d4193d38e4eba48e8815729ce75f9c0ab0e4c1c0',
txsFeatures: 0,
stateRoot:
'0x93de0ffb1f33bc0af053abc2a87c4af44594f5dcb1cb879dd823686a15d68550',
receiptsRoot:
'0x45b0cfc220ceec5b7c1c62c4d4193d38e4eba48e8815729ce75f9c0ab0e4c1c0',
signer: '0x0000000000000000000000000000000000000000',
isTrunk: true,
transactions: []
});

const names = await vnsUtils.lookupAddresses(thorClient, [
'0x0000000000000000000000000000000000000000'
]);
expect(names).toEqual([null]);
});
});
});
10 changes: 2 additions & 8 deletions packages/network/tests/utils/vns/lookups.unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* Vechain provider tests
*
* @group integration/providers/vechain-provider
*/
import {
afterEach,
beforeEach,
Expand All @@ -13,12 +8,11 @@ import {
} from '@jest/globals';
import { ThorClient, vnsUtils } from '../../../src';
import { testnetUrl } from '../../fixture';
// import { addressUtils, TESTNET_NETWORK } from '../../../../core';

/**
* vet.domains Name Service Utilities
* vnsUtils vet.domains tests
*
* @group unit/utils/vns
* @group unit/utils/vnsUtils
*/
describe('vnsUtils', () => {
/**
Expand Down