Skip to content

Commit f600d6a

Browse files
committed
include more test coverage
Signed-off-by: Simeon Nakov <[email protected]>
1 parent b11dd69 commit f600d6a

File tree

2 files changed

+108
-4
lines changed

2 files changed

+108
-4
lines changed

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,4 +227,53 @@ describe('SDKClientError', () => {
227227
const error = new SDKClientError({ status: Status.InvalidAccountId, message: 'Connection dropped' });
228228
expect(error.isConnectionDropped()).to.be.false;
229229
});
230+
231+
it('should handle status object with falsy _code property', () => {
232+
const errorWithZeroCode = { status: { _code: 0 }, message: 'Error with zero code' };
233+
const customMessage = 'Custom message for zero code';
234+
235+
const error = new SDKClientError(errorWithZeroCode, customMessage);
236+
237+
expect(error.message).to.equal(customMessage); // Should use custom message since _code is falsy
238+
expect(error.isValidNetworkError()).to.be.false; // Should be false since _code is falsy
239+
expect(error.status).to.equal(Status.Unknown); // Should default to Unknown
240+
});
241+
242+
it('should handle status object with null _code property', () => {
243+
const errorWithNullCode = { status: { _code: null }, message: 'Error with null code' };
244+
const customMessage = 'Custom message for null code';
245+
246+
const error = new SDKClientError(errorWithNullCode, customMessage);
247+
248+
expect(error.message).to.equal(customMessage);
249+
expect(error.isValidNetworkError()).to.be.false;
250+
expect(error.status).to.equal(Status.Unknown);
251+
});
252+
253+
it('should identify invalid contract ID when valid network error AND message contains InvalidContractId string', () => {
254+
const invalidContractMessage = `Some error containing ${Status.InvalidContractId.toString()} in the message`;
255+
const error = new SDKClientError({ status: Status.InsufficientTxFee, message: invalidContractMessage });
256+
257+
expect(error.isInvalidContractId()).to.be.true; // Should be true: valid network error AND message contains string
258+
expect(error.isValidNetworkError()).to.be.true;
259+
});
260+
261+
it('should handle empty error object with message parameter', () => {
262+
const error = new SDKClientError({}, 'Test message');
263+
264+
expect(error.message).to.equal('Test message');
265+
expect(error.isValidNetworkError()).to.be.false;
266+
expect(error.status).to.equal(Status.Unknown);
267+
});
268+
269+
it('should handle error object with status but no _code property', () => {
270+
const errorWithStatusButNoCode = { status: {}, message: 'Error message' };
271+
const customMessage = 'Custom error message';
272+
273+
const error = new SDKClientError(errorWithStatusButNoCode, customMessage);
274+
275+
expect(error.message).to.equal(customMessage); // Should use custom message since status._code is undefined
276+
expect(error.isValidNetworkError()).to.be.false;
277+
expect(error.status).to.equal(Status.Unknown);
278+
});
230279
});

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

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,11 @@ describe('Formatters', () => {
548548
const value = '0x7FFFFFFFFFFFFFFF';
549549
expect(weibarHexToTinyBarInt(value)).to.eq(922337203);
550550
});
551+
552+
it('should round up fractional weibar values to 1 tinybar', () => {
553+
const value = '0x1';
554+
expect(weibarHexToTinyBarInt(value)).to.eq(1);
555+
});
551556
});
552557

553558
describe('valid ethereum address', () => {
@@ -742,6 +747,16 @@ describe('Formatters', () => {
742747
const malformedHex = '0x08c379a0000000000000000000000000000000000000000000000000000000000000002';
743748
expect(decodeErrorMessage(malformedHex)).to.equal('');
744749
});
750+
751+
it('should handle non-hex message by returning as-is', () => {
752+
const nonHexMessage = 'Simple error message without hex';
753+
expect(decodeErrorMessage(nonHexMessage)).to.equal(nonHexMessage);
754+
});
755+
756+
it('should handle message that starts with 0x but is malformed', () => {
757+
const malformedMessage = '0xinvalid';
758+
expect(decodeErrorMessage(malformedMessage)).to.equal('');
759+
});
745760
});
746761
});
747762

@@ -895,7 +910,7 @@ describe('Formatters', () => {
895910
});
896911
});
897912

898-
describe('Additional Edge Cases', () => {
913+
describe('Additional Edge Cases - Targeted Coverage', () => {
899914
it('should handle getFunctionSelector with short input', () => {
900915
expect(getFunctionSelector('0x123')).to.eq('123');
901916
expect(getFunctionSelector('abc')).to.eq('abc');
@@ -920,22 +935,62 @@ describe('Formatters', () => {
920935
expect(toNullableBigNumber(123 as any)).to.equal(null);
921936
});
922937

923-
it('should handle formatTransactionId regex validation', () => {
938+
it('should handle formatTransactionId regex validation - precise failing cases', () => {
939+
// These should specifically fail the TRANSACTION_ID_REGEX: /\d{1}\.\d{1}\.\d{1,10}\@\d{1,10}\.\d{1,9}/
924940
expect(formatTransactionId('invalid-format')).to.eq(null);
925941
expect(formatTransactionId('0.0.2@')).to.eq(null);
926942
expect(formatTransactionId('@1234567890.123456789')).to.eq(null);
943+
expect(formatTransactionId('')).to.eq(null);
944+
expect(formatTransactionId('0.0.2-1234567890.123456789')).to.eq(null); // missing @
927945
});
928946

929-
it('should handle formatTransactionIdWithoutQueryParams with empty result', () => {
947+
it('should handle formatTransactionIdWithoutQueryParams with null formatTransactionId result', () => {
948+
// These should cause formatTransactionId to return null, which should make formatTransactionIdWithoutQueryParams return null
930949
expect(formatTransactionIdWithoutQueryParams('invalid?nonce=1')).to.eq(null);
931950
expect(formatTransactionIdWithoutQueryParams('?nonce=1')).to.eq(null);
951+
expect(formatTransactionIdWithoutQueryParams('')).to.eq(null);
932952
});
933953

934-
it('should handle parseNumericEnvVar error throwing scenarios', () => {
954+
it('should handle parseNumericEnvVar with completely invalid constant', () => {
935955
expect(() => parseNumericEnvVar('NONEXISTENT_VAR', 'NONEXISTENT_CONSTANT')).to.throw(
936956
Error,
937957
"Unable to parse numeric env var: 'NONEXISTENT_VAR', constant: 'NONEXISTENT_CONSTANT'",
938958
);
939959
});
960+
961+
it('should handle tinybarsToWeibars negative value error when allowNegativeValues is false', () => {
962+
expect(() => tinybarsToWeibars(-1, false)).to.throw(Error, 'Invalid value - cannot pass negative number');
963+
});
964+
965+
it('should handle tinybarsToWeibars excessive value error', () => {
966+
const excessiveValue = constants.TOTAL_SUPPLY_TINYBARS + 1;
967+
expect(() => tinybarsToWeibars(excessiveValue, false)).to.throw(
968+
Error,
969+
'Value cannot be more than the total supply of tinybars in the blockchain',
970+
);
971+
});
972+
973+
it('should test hashNumber function indirectly through numberTo0x', () => {
974+
// The hashNumber function is used internally by numberTo0x
975+
expect(numberTo0x(255)).to.equal('0xff');
976+
expect(numberTo0x(0)).to.equal('0x0');
977+
expect(numberTo0x(16)).to.equal('0x10');
978+
});
979+
980+
it('should test formatRequestIdMessage with pre-formatted request ID', () => {
981+
const preFormattedId = '[Request ID: test-id]';
982+
expect(formatRequestIdMessage(preFormattedId)).to.eq(preFormattedId);
983+
});
984+
985+
it('should test formatRequestIdMessage with unformatted request ID', () => {
986+
const unformattedId = 'simple-id';
987+
expect(formatRequestIdMessage(unformattedId)).to.eq('[Request ID: simple-id]');
988+
});
989+
990+
it('should test various falsy inputs for formatRequestIdMessage', () => {
991+
expect(formatRequestIdMessage(undefined)).to.eq('');
992+
expect(formatRequestIdMessage('')).to.eq('');
993+
expect(formatRequestIdMessage(null as any)).to.eq('');
994+
});
940995
});
941996
});

0 commit comments

Comments
 (0)