Skip to content

Commit

Permalink
CI/CD: Enable ESLint Github Action (#104)
Browse files Browse the repository at this point in the history
Signed-off-by: Ivy Astrix <[email protected]>
  • Loading branch information
poi-son-ivy authored Feb 21, 2024
1 parent 67c04ae commit b87adbc
Show file tree
Hide file tree
Showing 14 changed files with 2,599 additions and 2,498 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/run-eslint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Lint Snap Code

on: [push, pull_request]

jobs:
eslint:
name: Run ESLint For Snap
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
name: Check out source code

- uses: actions/setup-node@v2
name: Set up Node.js
with:
node-version: '18'
cache: 'yarn'
cache-dependency-path: packages/hedera-wallet-snap/packages/snap

- name: Install Dependencies in Subdirectory
run: yarn install
working-directory: ./packages/hedera-wallet-snap/packages/snap

- name: Run ESLint in Subdirectory
run: yarn lint
working-directory: ./packages/hedera-wallet-snap/packages/snap
2 changes: 1 addition & 1 deletion packages/hedera-wallet-snap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"eslint-plugin-jest": "^27.4.0",
"eslint-plugin-jsdoc": "^46.8.1",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-prettier": "5.0.0",
"prettier": "^3.0.3",
"prettier-plugin-packagejson": "^2.4.5",
"typescript": "^5.2.2"
Expand Down
2 changes: 1 addition & 1 deletion packages/hedera-wallet-snap/packages/snap/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ module.exports = {
project: './tsconfig.eslint.json',
sourceType: 'module',
},
ignorePatterns: ['!.eslintrc.js', 'dist/', './postBuild.js'],
ignorePatterns: ['!.eslintrc.js', 'dist/', './postBuild.js','jest.config.js'],
};
2 changes: 1 addition & 1 deletion packages/hedera-wallet-snap/packages/snap/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ module.exports = {
testEnvironment: 'node',
testRegex: '.*\\.spec\\.ts$',
transformIgnorePatterns: [],
testTimeout: 120000
testTimeout: 120000,
};
1 change: 1 addition & 0 deletions packages/hedera-wallet-snap/packages/snap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"@metamask/snaps-ui": "^1.0.1",
"bignumber.js": "^9.1.1",
"ethers": "^6.3.0",
"jest-mock": "^29.7.0",
"lodash": "^4.17.21",
"normalize-url": "^8.0.0"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import {
type Client,
} from '@hashgraph/sdk';


import { CryptoUtils } from '../../../../utils/CryptoUtils';
import { Utils } from '../../../../utils/Utils';
import { TxReceipt } from '../../../../types/hedera';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ describe('CryptoUtils', () => {
});

it('should validate an Ethereum public key', () => {
const publicKey = '0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
const publicKey =
'0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
mockedIsHexString.mockReturnValue(true);

const result = CryptoUtils.isValidEthereumPublicKey(publicKey);
Expand Down Expand Up @@ -62,7 +63,9 @@ describe('CryptoUtils', () => {

it('should throw an error for invalid hex strings', () => {
const hexString = 'abc';
expect(() => CryptoUtils.hexToUInt8Array(hexString)).toThrow('Invalid hex string');
expect(() => CryptoUtils.hexToUInt8Array(hexString)).toThrow(
'Invalid hex string',
);
});
});
});
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-restricted-globals */
import { FetchUtils, FetchResponse } from '../FetchUtils';

global.fetch = jest.fn();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { providerErrors } from '@metamask/rpc-errors';
import { HederaUtils } from '../HederaUtils';

describe('HederaUtils', () => {
describe('getMirrorNodeFlagIfExists', () => {
it('returns empty string if no mirrorNodeUrl provided', () => {
Expand All @@ -23,7 +24,9 @@ describe('HederaUtils', () => {
});

it('returns true if externalAccount flag is set correctly', () => {
const params = { externalAccount: { accountIdOrEvmAddress: '0.0.123', curve: 'ED25519' } };
const params = {
externalAccount: { accountIdOrEvmAddress: '0.0.123', curve: 'ED25519' },
};
const result = HederaUtils.isExternalAccountFlagSet(params);
expect(result).toBe(true);
});
Expand All @@ -48,8 +51,9 @@ describe('HederaUtils', () => {
describe('isValidSignMessageRequest', () => {
it('throws an error if message parameter is missing', () => {
const params = {}; // Empty parameters
expect(() => HederaUtils.isValidSignMessageRequest(params))
.toThrow(providerErrors.unsupportedMethod().message);
expect(() => HederaUtils.isValidSignMessageRequest(params)).toThrow(
providerErrors.unsupportedMethod().message,
);
});

it('does not throw an error for valid message parameters', () => {
Expand All @@ -60,92 +64,120 @@ describe('HederaUtils', () => {
describe('isValidGetAccountInfoRequest', () => {
it('throws an error if accountId is invalid', () => {
const params = { accountId: 'invalid' };
expect(() => HederaUtils.isValidGetAccountInfoRequest(params))
.toThrow(providerErrors.unsupportedMethod().message);
expect(() => HederaUtils.isValidGetAccountInfoRequest(params)).toThrow(
providerErrors.unsupportedMethod().message,
);
});

it('does not throw an error for valid accountId', () => {
const params = { accountId: '0.0.12345' };
expect(() => HederaUtils.isValidGetAccountInfoRequest(params)).not.toThrow();
expect(() =>
HederaUtils.isValidGetAccountInfoRequest(params),
).not.toThrow();
});
});
describe('isValidGetTransactionsParams', () => {
it('throws an error if transactionId is invalid', () => {
const params = { transactionId: '' }; // Invalid empty string
expect(() => HederaUtils.isValidGetTransactionsParams(params))
.toThrow(providerErrors.unsupportedMethod().message);
expect(() => HederaUtils.isValidGetTransactionsParams(params)).toThrow(
providerErrors.unsupportedMethod().message,
);
});

it('does not throw an error for valid transactionId', () => {
const params = { transactionId: '0.0.12345@123456789' };
expect(() => HederaUtils.isValidGetTransactionsParams(params)).not.toThrow();
expect(() =>
HederaUtils.isValidGetTransactionsParams(params),
).not.toThrow();
});
});
describe('isValidAssociateTokensParams', () => {
it('throws an error if tokenIds array is empty', () => {
const params = { tokenIds: [] };
expect(() => HederaUtils.isValidAssociateTokensParams(params))
.toThrow(providerErrors.unsupportedMethod().message);
expect(() => HederaUtils.isValidAssociateTokensParams(params)).toThrow(
providerErrors.unsupportedMethod().message,
);
});

it('does not throw an error for valid tokenIds', () => {
const params = { tokenIds: ['0.0.123', '0.0.456'] };
expect(() => HederaUtils.isValidAssociateTokensParams(params)).not.toThrow();
expect(() =>
HederaUtils.isValidAssociateTokensParams(params),
).not.toThrow();
});
});
describe('isValidTransferCryptoParams', () => {
it('throws an error if transfers parameter is missing or empty', () => {
const emptyParams = {};
expect(() => HederaUtils.isValidTransferCryptoParams(emptyParams))
.toThrow('Invalid transferCrypto Params passed. "transfers" must be passed as a parameter');
expect(() =>
HederaUtils.isValidTransferCryptoParams(emptyParams),
).toThrow(
'Invalid transferCrypto Params passed. "transfers" must be passed as a parameter',
);

const paramsWithEmptyArray = { transfers: [] };
expect(() => HederaUtils.isValidTransferCryptoParams(paramsWithEmptyArray))
.toThrow('Invalid transferCrypto Params passed. "transfers" must be passed as a parameter');
expect(() =>
HederaUtils.isValidTransferCryptoParams(paramsWithEmptyArray),
).toThrow(
'Invalid transferCrypto Params passed. "transfers" must be passed as a parameter',
);
});

it('does not throw an error for valid transfers parameters', () => {
const validParams = {
transfers: [
{ to: '0.0.123', amount: 100, assetType: 'HBAR' }
]
transfers: [{ to: '0.0.123', amount: 100, assetType: 'HBAR' }],
};
expect(() => HederaUtils.isValidTransferCryptoParams(validParams)).not.toThrow();
expect(() =>
HederaUtils.isValidTransferCryptoParams(validParams),
).not.toThrow();
});
});
});
describe('isValidStakeHbarParams', () => {
it('throws an error if neither nodeId nor accountId are provided', () => {
const invalidParams = {};
expect(() => HederaUtils.isValidStakeHbarParams(invalidParams))
.toThrow('Invalid stakeHbar Params passed. Pass either "nodeId" or "accountId" as a parameter');
expect(() => HederaUtils.isValidStakeHbarParams(invalidParams)).toThrow(
'Invalid stakeHbar Params passed. Pass either "nodeId" or "accountId" as a parameter',
);
});

it('does not throw an error when valid nodeId is provided', () => {
const paramsWithNodeId = { nodeId: 0 };
expect(() => HederaUtils.isValidStakeHbarParams(paramsWithNodeId)).not.toThrow();
expect(() =>
HederaUtils.isValidStakeHbarParams(paramsWithNodeId),
).not.toThrow();
});

it('does not throw an error when valid accountId is provided', () => {
const paramsWithAccountId = { accountId: '0.0.12345' };
expect(() => HederaUtils.isValidStakeHbarParams(paramsWithAccountId)).not.toThrow();
expect(() =>
HederaUtils.isValidStakeHbarParams(paramsWithAccountId),
).not.toThrow();
});

// Test for invalid nodeId and accountId formats
});
describe('isValidDeleteAccountParams', () => {
it('throws an error if transferAccountId is missing or invalid', () => {
const invalidParams = {};
expect(() => HederaUtils.isValidDeleteAccountParams(invalidParams))
.toThrow('Invalid deleteAccount Params passed. "transferAccountId" must be passed as a parameter');
expect(() =>
HederaUtils.isValidDeleteAccountParams(invalidParams),
).toThrow(
'Invalid deleteAccount Params passed. "transferAccountId" must be passed as a parameter',
);

const invalidParams2 = { transferAccountId: '' };
expect(() => HederaUtils.isValidDeleteAccountParams(invalidParams2))
.toThrow('Invalid deleteAccount Params passed. "transferAccountId" is not a valid Account ID');
expect(() =>
HederaUtils.isValidDeleteAccountParams(invalidParams2),
).toThrow(
'Invalid deleteAccount Params passed. "transferAccountId" is not a valid Account ID',
);
});

it('does not throw an error for valid transferAccountId', () => {
const validParams = { transferAccountId: '0.0.12345' };
expect(() => HederaUtils.isValidDeleteAccountParams(validParams)).not.toThrow();
expect(() =>
HederaUtils.isValidDeleteAccountParams(validParams),
).not.toThrow();
});
});
});
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { StateUtils } from '../StateUtils';
import { WalletAccountState, WalletSnapState } from '../../types/state';

describe('StateUtils', () => {
describe('getEmptyAccountState', () => {
it('returns a deep clone of emptyAccountState', () => {
const state : WalletAccountState = StateUtils.getEmptyAccountState();
const state: WalletAccountState = StateUtils.getEmptyAccountState();
expect(state).toEqual({
keyStore: {
curve: 'ECDSA_SECP256K1',
Expand All @@ -17,14 +18,14 @@ describe('StateUtils', () => {

// Verify deep clone by mutation
state.keyStore.curve = 'ED25519';
const newState : WalletAccountState = StateUtils.getEmptyAccountState();
const newState: WalletAccountState = StateUtils.getEmptyAccountState();
expect(newState.keyStore.curve).toBe('ECDSA_SECP256K1');
});
});

describe('getInitialSnapState', () => {
it('returns a deep clone of initialSnapState', () => {
const snapState:WalletSnapState = StateUtils.getInitialSnapState();
const snapState: WalletSnapState = StateUtils.getInitialSnapState();
expect(snapState).toEqual({
currentAccount: {},
accountState: {},
Expand All @@ -41,7 +42,7 @@ describe('StateUtils', () => {

// Verify deep clone by mutation
snapState.snapConfig.dApp.disablePopups = true;
const newSnapState:WalletSnapState = StateUtils.getInitialSnapState();
const newSnapState: WalletSnapState = StateUtils.getInitialSnapState();
expect(newSnapState.snapConfig.dApp.disablePopups).toBe(false);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ describe('HederaWalletUtils', () => {
maxCost: 115.5, // (100 + 10) * 1.05
};

const result = TuumUtils.calculateHederaQueryFees(queryCost, serviceFeePercentage);
const result = TuumUtils.calculateHederaQueryFees(
queryCost,
serviceFeePercentage,
);

expect(result.serviceFeeToPay).toBeCloseTo(expected.serviceFeeToPay);
expect(result.maxCost).toBeCloseTo(expected.maxCost);
Expand All @@ -21,8 +24,8 @@ describe('HederaWalletUtils', () => {

describe('deductServiceFee', () => {
it('should call transferCrypto with correct parameters', async () => {
//note that jest is having issues with classes with nested includes
//since we're doing the instance refactor next going to leave this as-is
// note that jest is having issues with classes with nested includes
// since we're doing the instance refactor next going to leave this as-is
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('HederaWalletSnap', () => {

test('should convert a string representation of a timestamp to a UTC string', () => {
const timestamp = '1672444800'; // Equivalent to 2023-01-01T00:00:00Z
const expected = new Date(parseInt(timestamp) * 1000).toUTCString();
const expected = new Date(parseInt(timestamp, 10) * 1000).toUTCString();
expect(Utils.timestampToString(timestamp)).toBe(expected);
});

Expand Down
1 change: 1 addition & 0 deletions packages/hedera-wallet-snap/packages/snap/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
"removeComments": true
},
"files": ["./json-typings.d.ts"],
"exclude": ["./jest.config.js"],
"include": ["src/**/*"]
}
Loading

0 comments on commit b87adbc

Please sign in to comment.