Skip to content

Commit 8fa49fb

Browse files
committed
Add tests
Signed-off-by: Luis Mastrangelo <[email protected]>
1 parent 98841f9 commit 8fa49fb

File tree

4 files changed

+61
-15
lines changed

4 files changed

+61
-15
lines changed

packages/relay/src/lib/relay.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,9 +336,9 @@ export class Relay {
336336
const operator = this.clientMain.operatorAccountId!.toString();
337337
const balance = BigInt(await this.ethImpl.getBalance(operator, 'latest', {} as RequestDetails));
338338
if (balance === BigInt(0)) {
339-
throw new Error(`Operator account \`${operator}\` has no balance`);
339+
throw new Error(`Operator account '${operator}' has no balance`);
340340
} else {
341-
this.logger.info(`Operator account \`${operator}\` has balance: ${balance}`);
341+
this.logger.info(`Operator account '${operator}' has balance: ${balance}`);
342342
}
343343
}
344344
}

packages/relay/tests/lib/eth/eth-helpers.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,14 @@ export function generateEthTestEnv(fixedFeeHistory = false) {
3636
const logger = pino({ level: 'silent' });
3737
const registry = new Registry();
3838
const cacheService = CacheService.getInstance(CACHE_LEVEL.L1, registry);
39-
// @ts-ignore
4039
const mirrorNodeInstance = new MirrorNodeClient(
4140
ConfigService.get('MIRROR_NODE_URL'),
4241
logger.child({ name: `mirror-node` }),
4342
registry,
4443
cacheService,
4544
);
4645

47-
// @ts-ignore
4846
const restMock = new MockAdapter(mirrorNodeInstance.getMirrorNodeRestInstance(), { onNoMatch: 'throwException' });
49-
// @ts-ignore
5047
const web3Mock = new MockAdapter(mirrorNodeInstance.getMirrorNodeWeb3Instance(), { onNoMatch: 'throwException' });
5148

5249
const duration = constants.HBAR_RATE_LIMIT_DURATION;

packages/relay/tests/lib/eth/eth_getBalance.spec.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22

33
import { expect, use } from 'chai';
44
import chaiAsPromised from 'chai-as-promised';
5-
import sinon from 'sinon';
65

76
import { numberTo0x } from '../../../dist/formatters';
8-
import { SDKClient } from '../../../src/lib/clients';
97
import constants from '../../../src/lib/constants';
108
import { RequestDetails } from '../../../src/lib/types';
119
import { buildCryptoTransferTransaction, overrideEnvsInMochaDescribe } from '../../helpers';
@@ -29,12 +27,9 @@ import { balancesByAccountIdByTimestampURL, generateEthTestEnv } from './eth-hel
2927

3028
use(chaiAsPromised);
3129

32-
let sdkClientStub: sinon.SinonStubbedInstance<SDKClient>;
33-
let getSdkClientStub: sinon.SinonStub;
34-
3530
describe('@ethGetBalance using MirrorNode', async function () {
3631
this.timeout(10000);
37-
const { restMock, hapiServiceInstance, ethImpl, cacheService } = generateEthTestEnv();
32+
const { restMock, ethImpl, cacheService } = generateEthTestEnv();
3833

3934
const requestDetails = new RequestDetails({ requestId: 'eth_getBalanceTest', ipAddress: '0.0.0.0' });
4035

@@ -45,13 +40,10 @@ describe('@ethGetBalance using MirrorNode', async function () {
4540
await cacheService.clear(requestDetails);
4641
restMock.reset();
4742

48-
sdkClientStub = sinon.createStubInstance(SDKClient);
49-
getSdkClientStub = sinon.stub(hapiServiceInstance, 'getSDKClient').returns(sdkClientStub);
5043
restMock.onGet('network/fees').reply(200, JSON.stringify(DEFAULT_NETWORK_FEES));
5144
});
5245

5346
this.afterEach(() => {
54-
getSdkClientStub.restore();
5547
restMock.resetHandlers();
5648
});
5749

packages/relay/tests/lib/relay.spec.ts

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
// SPDX-License-Identifier: Apache-2.0
22

3+
import MockAdapter from 'axios-mock-adapter';
34
import chai, { expect } from 'chai';
45
import chaiAsPromised from 'chai-as-promised';
56
import fs from 'fs';
67
import pino from 'pino';
78
import { Registry } from 'prom-client';
89
import sinon from 'sinon';
9-
import { CacheService } from '../../src/lib/services/cacheService/cacheService';
1010

1111
import { Relay } from '../../src';
12+
import { CacheService } from '../../src/lib/services/cacheService/cacheService';
1213
import { overrideEnvsInMochaDescribe, withOverriddenEnvsInMochaTest } from '../helpers';
1314

1415
chai.use(chaiAsPromised);
@@ -103,4 +104,60 @@ describe('Relay', () => {
103104
});
104105
});
105106
});
107+
108+
describe('ensureOperatorHasBalance', function () {
109+
withOverriddenEnvsInMochaTest({ READ_ONLY: true }, () => {
110+
it('should never throw', async function () {
111+
await expect(relay.ensureOperatorHasBalance()).to.not.be.rejectedWith();
112+
});
113+
});
114+
115+
withOverriddenEnvsInMochaTest({ READ_ONLY: false }, () => {
116+
let restMock: MockAdapter;
117+
let operatorId: string;
118+
119+
beforeEach(() => {
120+
const mirrorNodeInstance = relay.mirrorClient().getMirrorNodeRestInstance();
121+
restMock = new MockAdapter(mirrorNodeInstance, { onNoMatch: 'throwException' });
122+
// @ts-expect-error: Property 'clientMain' is private and only accessible within class 'Relay'
123+
const clientMain = relay.clientMain;
124+
operatorId = clientMain.operatorAccountId!.toString();
125+
});
126+
127+
afterEach(() => {
128+
restMock.restore();
129+
});
130+
131+
it('should not throw when operator has balance', async function () {
132+
const balance = {
133+
account: operatorId,
134+
balance: {
135+
balance: 99960581137,
136+
},
137+
};
138+
restMock.onGet(`accounts/${operatorId}?limit=100`).reply(200, JSON.stringify(balance));
139+
await expect(relay.ensureOperatorHasBalance()).to.not.be.rejectedWith();
140+
});
141+
142+
it('should throw when operator has no balance', async function () {
143+
const balance = {
144+
account: operatorId,
145+
balance: {
146+
balance: 0,
147+
},
148+
};
149+
restMock.onGet(`accounts/${operatorId}?limit=100`).reply(200, JSON.stringify(balance));
150+
151+
const message = `Operator account '${operatorId}' has no balance`;
152+
await expect(relay.ensureOperatorHasBalance()).to.be.rejectedWith(message);
153+
});
154+
155+
it('should throw when operator has not been found', async function () {
156+
restMock.onGet(`accounts/${operatorId}?limit=100`).reply(404, JSON.stringify({}));
157+
158+
const message = `Operator account '${operatorId}' has no balance`;
159+
await expect(relay.ensureOperatorHasBalance()).to.be.rejectedWith(message);
160+
});
161+
});
162+
});
106163
});

0 commit comments

Comments
 (0)