diff --git a/clients/validator/package.json b/clients/validator/package.json index 5a8c9d9709..25aae110f1 100644 --- a/clients/validator/package.json +++ b/clients/validator/package.json @@ -6,7 +6,7 @@ "main": "./dist/index.js", "types": "dist/index.d.ts", "scripts": { - "test": "jest --coverage --verbose false", + "test": "jest --verbose false", "lint": "eslint src", "lint:fix": "eslint src --fix", "docs": "typedoc --out docs src/index.ts" diff --git a/clients/validator/tests/integration/client.int.test.ts b/clients/validator/tests/integration/client.int.test.ts deleted file mode 100644 index ca4b1ffd92..0000000000 --- a/clients/validator/tests/integration/client.int.test.ts +++ /dev/null @@ -1,73 +0,0 @@ -import validatorClient from "../../src/index"; -import { config } from "../test-utils/config"; - -let client: validatorClient; -let mnemonic: string; - -beforeEach(async () => { - mnemonic = validatorClient.randomMnemonic(); - client = await validatorClient.connect( - mnemonic, - config.NYMD_URL, - config.VALIDATOR_API, - config.CURRENCY_DENOM, - config.MIXNET_CONTRACT, - config.VESTING_CONTRACT - ); -}); - -describe("perform a few non expensive network calls with the validator client", () => { - test.skip("get all cached mixnodes", async () => { - try { - const response = await client.getCachedMixnodes(); - - //expect all mixnodes to have their owner address - response.forEach((mixnodeDetails) => { - expect(mixnodeDetails.owner).toHaveLength(43); - }); - } catch (error) { - throw error; - } - }); - - test.skip("get minimium pledge amount for a mixnode", async () => { - try { - const response = await client.minimumMixnodePledge(); - - expect(response.amount).toBe("100000000"); - expect(response.denom).toBe(config.CURRENCY_DENOM); - } catch (error) { - throw error; - } - }); - - test.skip("get minimium gateway pledge amount", async () => { - try { - const response = await client.minimumGatewayPledge(); - - expect(response.amount).toBe("100000000"); - expect(response.denom).toBe(config.CURRENCY_DENOM as string); - } catch (error) { - throw error; - } - }); - - test.skip("ensure the correct mixnet address is being passed", () => { - try { - //should supply the given value from the client init - const mixnet_contract = client.mixnetContract; - expect(mixnet_contract).toStrictEqual(config.MIXNET_CONTRACT); - } catch (error) { - throw error; - } - }); - - test.skip("ensure the correct vesting address is being passed", () => { - try { - const vesting_contract = client.vestingContract; - expect(vesting_contract).toStrictEqual(config.VESTING_CONTRACT); - } catch (error) { - throw error; - } - }); -}); \ No newline at end of file diff --git a/clients/validator/tests/integration/integration.e2e.test.ts b/clients/validator/tests/integration/integration.e2e.test.ts index 721f10d9b6..47d6c61b0d 100644 --- a/clients/validator/tests/integration/integration.e2e.test.ts +++ b/clients/validator/tests/integration/integration.e2e.test.ts @@ -1,7 +1,7 @@ import validator from "../../src/index"; import { ExecuteResult } from "@cosmjs/cosmwasm-stargate"; -import { Coin } from "@cosmjs/proto-signing"; import { config } from "../test-utils/config"; +import {buildCoin, buildWallet, profitPercentage} from "../test-utils/utils" import { Gateway, GatewayOwnershipResponse, @@ -26,7 +26,7 @@ beforeEach(async () => { }); describe("long running e2e tests", () => { - test.only("token transfer", async () => { + test.skip("token transfer", async () => { try { //make sure there's enough balance in the wallet @@ -61,7 +61,7 @@ describe("long running e2e tests", () => { } }); - test.only("update mixnode profit percentage", async () => { + test.skip("update mixnode profit percentage", async () => { const nodeIdentity = config.MIXNODE_IDENTITY; const profitPercent = profitPercentage(); @@ -81,7 +81,7 @@ describe("long running e2e tests", () => { expect(ownsMixNode.mixnode?.mix_node.profit_margin_percent).toStrictEqual(profitPercent); }); - test.only("unbond and bond mixnode", async () => { + test.skip("unbond and bond mixnode", async () => { try { await validatorClient.unbondMixNode("auto", "unbond-mixnode"); @@ -161,7 +161,7 @@ describe("long running e2e tests", () => { expect(ownsGateway.address).toStrictEqual(config.USER_WALLET_ADDRESS); }); - test.only("delegate to mixnode, then undelegate", async () => { + test.skip("delegate to mixnode, then undelegate", async () => { const pledge = buildCoin("100000000", config.CURRENCY_DENOM) @@ -171,6 +171,7 @@ describe("long running e2e tests", () => { pledge, "auto" ); + //todo - we can assert the events for responses response.logs.forEach((log) => { console.log(log.events); console.log(log.log); @@ -186,8 +187,7 @@ describe("long running e2e tests", () => { "auto" ); - //see output of events - //remove shortly + //todo - we can assert the events for responses unbond.logs.forEach((logs) => { logs.events.forEach((events) => { console.log(events.type); @@ -199,27 +199,3 @@ describe("long running e2e tests", () => { } }); }); - -const profitPercentage = (): number => { - return Math.floor(Math.random() * 100) + 1; -}; - - -const buildCoin = (amount: string, denomination: string): Coin => { - return { - denom: `u${denomination}`, - amount: amount, - }; -}; - -const buildWallet = async (): Promise => { - let mnemonic = validator.randomMnemonic(); - - const randomAddress = await validator.mnemonicToAddress( - mnemonic, - config.NETWORK_BECH - ); - console.log(randomAddress); - return randomAddress; - }; - diff --git a/clients/validator/tests/integration/signing.client.test.ts b/clients/validator/tests/integration/signing.client.test.ts deleted file mode 100644 index 8db61578f5..0000000000 --- a/clients/validator/tests/integration/signing.client.test.ts +++ /dev/null @@ -1,55 +0,0 @@ -import SigningClient from "../../src/signing-client"; -import validator from "../../src/index"; -import { CosmWasmClient } from "@cosmjs/cosmwasm-stargate"; -import { config } from "../test-utils/config"; - -let cosmWasmClient: CosmWasmClient; -let mnemonic: string; - -beforeEach(async () => { - cosmWasmClient = await SigningClient.connect(config.NYMD_URL); -}); - -describe("peform basic network checks with the client", () => { - test.skip("retrieve a newly created users balance should be empty", async () => { - try { - mnemonic = validator.randomMnemonic(); - - const randomAddress = await validator.mnemonicToAddress( - mnemonic, - config.NETWORK_BECH - ); - - const balance = await cosmWasmClient.getBalance( - randomAddress, - config.CURRENCY_DENOM - ); - - expect(balance.denom).toStrictEqual(config.CURRENCY_DENOM); - expect(balance.amount).toStrictEqual("0"); - } catch (error) { - throw error; - } - }); - - test.skip("get the chain id of the network", async () => { - try { - const chainId = await cosmWasmClient.getChainId(); - expect(chainId).toStrictEqual(config.CHAIN_ID); - } catch (error) { - throw error; - } - }); - - test.skip("get height then search for it by it's block", async () => { - try { - const height = await cosmWasmClient.getHeight(); - const block = await cosmWasmClient.getBlock(height); - - expect(block.header.chainId).toStrictEqual(config.CHAIN_ID); - expect(block.header.height).toStrictEqual(height); - } catch (error) { - throw error; - } - }); -}); \ No newline at end of file diff --git a/clients/validator/tests/integration/validator.api.querier.test.ts b/clients/validator/tests/integration/validator.api.querier.test.ts deleted file mode 100644 index 65befe9eb5..0000000000 --- a/clients/validator/tests/integration/validator.api.querier.test.ts +++ /dev/null @@ -1,26 +0,0 @@ -import ValidatorApiQuerier from "../../src/validator-api-querier"; -import { config } from "../test-utils/config"; - -let client: ValidatorApiQuerier; - -beforeEach(() => { - client = new ValidatorApiQuerier(config.VALIDATOR_API); -}); - -describe("init the validator api querier", () => { - test.skip("get rewarded mixnodes", async () => { - try { - //all mixnodes will have their owners address - let response = await client.getRewardedMixnodes(); - - console.log(response); - - //this is dependany on config and network amend shortly - response.forEach((Node) => { - expect(Node.owner.length).toStrictEqual(43); - }); - } catch (error) { - throw error; - } - }); -}); diff --git a/clients/validator/tests/mocks/cosmwasm.mock.client.test.ts b/clients/validator/tests/mocks/cosmwasm.mock.client.test.ts new file mode 100644 index 0000000000..cdb95f956e --- /dev/null +++ b/clients/validator/tests/mocks/cosmwasm.mock.client.test.ts @@ -0,0 +1,46 @@ +import { Mock, Times } from "moq.ts"; +import { Block, BlockHeader } from "@cosmjs/stargate"; +import { CosmWasmClient } from "@cosmjs/cosmwasm-stargate"; + +describe("implement cosmwasm client test", () => { + test.only("get height of a block then search for it", async () => { + let height = Promise.resolve(200); + + let blockHeader = { + version: { + block: "200", + app: "testing", + }, + height: 200, + chainId: "nym", + time: "today", + }; + + let block = Promise.resolve({ + header: blockHeader, + id: "test", + txs: [], + }); + + const getheight = new Mock() + .setup((nym) => nym.getHeight()) + .returns(height); + + const getblock = new Mock() + .setup((nym) => nym.getBlock(200)) + .returns(block); + + let heightC = getheight.object(); + let blockC = getblock.object(); + + let executeHeight = await heightC.getHeight(); + let executeBlock = await blockC.getBlock(200); + + getheight.verify((nym) => nym.getHeight(), Times.Exactly(1)); + getblock.verify((nym) => nym.getBlock(200), Times.Exactly(1)); + + expect(executeHeight).toStrictEqual(await height); + expect(executeBlock.header.height).toStrictEqual(await height); + expect(executeBlock.header.chainId).toStrictEqual("nym"); + }); +}); \ No newline at end of file diff --git a/clients/validator/tests/mocks/nymd.mock.test.ts b/clients/validator/tests/mocks/nymd.mock.test.ts index 8f6da6af31..b1e8fc5d1b 100644 --- a/clients/validator/tests/mocks/nymd.mock.test.ts +++ b/clients/validator/tests/mocks/nymd.mock.test.ts @@ -2,13 +2,9 @@ import { Mock, Times } from "moq.ts"; import { INymdQuery } from "../../src/query-client"; describe("nym-client mocks", () => { - beforeAll(() => {}); - - afterEach(() => {}); - - test.only("nymd mocks", async () => { + test.only("gets interval rewarding percent", async () => { let contract = "mixnet_contract"; - let response = Promise.resolve(Number(200)); + let response = Promise.resolve(Number(2)); const client = new Mock() .setup((nym) => nym.getIntervalRewardPercent(contract)) diff --git a/clients/validator/tests/mocks/validator.client.mock.test.ts b/clients/validator/tests/mocks/validator.client.mock.test.ts new file mode 100644 index 0000000000..7abe889f87 --- /dev/null +++ b/clients/validator/tests/mocks/validator.client.mock.test.ts @@ -0,0 +1,187 @@ +import { Coin } from "@cosmjs/proto-signing"; +import { Mock, Times } from "moq.ts"; +import ValidatorClient from "../../src/index"; +import { DeliverTxResponse, logs } from "@cosmjs/stargate"; +import { Gateway, MixNode } from "../../src/types"; +import { ExecuteResult } from "@cosmjs/cosmwasm-stargate"; +import { config } from "../test-utils/config"; +import { buildWallet, buildCoin, profitPercentage } from "../test-utils/utils"; +import { promiseExecuteResult } from "../test-utils/expectedResults"; + +describe("mock validator client tests", () => { + test.skip("token transfer", async () => { + + //arrange + //todo -- add more here + let recipientAddress = "nymt14ev4p8qaa7ayr06cg3z7y2u2kxc9a8f4h9gkch"; + let sender = "nymt1cv59jumgvz2chn7ffst8tzvnapqzp282m5vat2"; + + const coin = buildCoin("50000", "nymt"); + + let transaction = Promise.resolve({ + code: 0, + height: 1208302, + rawLog: "[]", + transactionHash: + "9C7BF465AB5CAB0D62446CBB251CF89CD173A640C5DE8DBC14A4BB950916114E", + gasUsed: 65042, + gasWanted: 67977, + }); + + console.log(transaction); + + let mockClient = new Mock() + .setup((nym) => nym.send(recipientAddress, [coin], "auto", "test")).returns(transaction); + + let token = mockClient.object(); + + //act + let response = await token.send(recipientAddress, [coin], "auto", "test"); + + //assert + mockClient.verify(cl => cl.send(recipientAddress, [coin], "auto"), Times.Exactly(1)); + }); + + test.only("bond mixnode test", async () => { + //arrange + let ownerSignature = "ownersignature"; + let coin = buildCoin("50000", "nymt"); + let expectedResult = promiseExecuteResult(); + + const profitPercent = profitPercentage(); + + const mixnode = { + host: "1.1.1.1", + mix_port: 1789, + verloc_port: 1790, + http_api_port: 8080, + identity_key: "identity", + sphinx_key: "identity", + version: "0.12.1", + profit_margin_percent: profitPercent, + }; + + let client = new Mock() + .setup((client) => + client.bondMixNode(mixnode, ownerSignature, coin, "auto") + ) + .returns(expectedResult); + + let mixnodeBond = client.object(); + + //act + let response = await mixnodeBond.bondMixNode( + mixnode, + ownerSignature, + coin, + "auto" + ); + client.verify((cl) => + cl.bondMixNode(mixnode, ownerSignature, coin, "auto") + ); + + //assert + expect(response.logs[0].log).toStrictEqual("test"); + expect(response.transactionHash).toStrictEqual( + "9C7BF465AB5CAB0D62446CBB251CF89CD173A640C5DE8DBC14A4BB950916114E" + ); + }); + + test.only("un-bond mixnode", async () => { + //arrange + let expectedResult = promiseExecuteResult(); + + let client = new Mock() + .setup((client) => client.unbondMixNode("auto")) + .returns(expectedResult); + + let unbondNode = client.object(); + + //act + let response = await unbondNode.unbondMixNode("auto"); + client.verify((cl) => cl.unbondMixNode("auto")); + + //assert + expect(response.logs[0].log).toStrictEqual("test"); + expect(response.transactionHash).toStrictEqual( + "9C7BF465AB5CAB0D62446CBB251CF89CD173A640C5DE8DBC14A4BB950916114E" + ); + }); + + + test.only("bond gateway", async () => { + //arrange + let expectedResult = promiseExecuteResult(); + let ownerSignature = "ownersigntature"; + let coin = buildCoin("50000", "nymt"); + + const gateway = { + host: '1.2.3.4', + mix_port: 1789, + clients_port: 9000, + version: "0.12.1", + sphinx_key: "sphinx_key", + identity_key: "identity_key", + location: "earth" + }; + + let client = new Mock() + .setup((client) => client.bondGateway(gateway, ownerSignature, coin, "auto", "memo")) + .returns(expectedResult); + + let mock = client.object(); + + //act + let response = await mock.bondGateway(gateway, ownerSignature, coin, "auto", "memo"); + client.verify((cl) => cl.bondGateway(gateway, ownerSignature, coin, "auto", "memo")); + + //assert + expect(response.logs[0].log).toStrictEqual("test"); + expect(response.transactionHash).toStrictEqual( + "9C7BF465AB5CAB0D62446CBB251CF89CD173A640C5DE8DBC14A4BB950916114E" + ); + }); + + test.only("unbond gateway", async () => { + //arrange + let expectedResult = promiseExecuteResult(); + let client = new Mock() + .setup((client) => client.unbondGateway()) + .returns(expectedResult); + + let mock = client.object(); + + //act + let response = await mock.unbondGateway(); + client.verify((cl) => cl.unbondGateway()); + + //assert + expect(response.logs[0].log).toStrictEqual("test"); + expect(response.transactionHash).toStrictEqual( + "9C7BF465AB5CAB0D62446CBB251CF89CD173A640C5DE8DBC14A4BB950916114E" + ); + }); + + test.only("retrieve a newly created account and the balance should be empty", async () => { + let nymWallet = await buildWallet(); + + let coin = Promise.resolve({ + denom: `${config.CURRENCY_DENOM}`, + amount: "0", + }); + + let client = new Mock() + .setup((nym) => nym.getBalance(nymWallet)) + .returns(coin); + + let obj = client.object(); + + let execute = await obj.getBalance(nymWallet); + + client.verify((nym) => nym.getBalance(nymWallet), Times.Exactly(1)); + + expect(execute).toStrictEqual(await coin); + }); +}); + + diff --git a/clients/validator/tests/test-utils/expectedResults.ts b/clients/validator/tests/test-utils/expectedResults.ts new file mode 100644 index 0000000000..02b382ad7a --- /dev/null +++ b/clients/validator/tests/test-utils/expectedResults.ts @@ -0,0 +1,15 @@ +import { ExecuteResult } from "@cosmjs/cosmwasm-stargate"; +import { logs } from "@cosmjs/stargate"; + +export const promiseExecuteResult = (): Promise => { + let log = { + msg_index: 0, + log: "test", + events: [], + }; + return Promise.resolve({ + logs: [log], + transactionHash: + "9C7BF465AB5CAB0D62446CBB251CF89CD173A640C5DE8DBC14A4BB950916114E", + }); + }; \ No newline at end of file diff --git a/clients/validator/tests/test-utils/utils.ts b/clients/validator/tests/test-utils/utils.ts index 723d12bc7e..a70ef7b8db 100644 --- a/clients/validator/tests/test-utils/utils.ts +++ b/clients/validator/tests/test-utils/utils.ts @@ -1,4 +1,8 @@ // timer actions + +import ValidatorClient, { Coin } from "../../src"; +import { config } from "./config"; + // Store current time as `start` export const now = (eventName = null) => { if (eventName) { @@ -15,4 +19,28 @@ export const now = (eventName = null) => { } return duration; }; + + export const profitPercentage = (): number => { + return Math.floor(Math.random() * 100) + 1; + }; + + + export const buildCoin = (amount: string, denomination: string): Coin => { + return { + denom: `u${denomination}`, + amount: amount, + }; + }; + + export const buildWallet = async (): Promise => { + let mnemonic = ValidatorClient.randomMnemonic(); + + const randomAddress = await ValidatorClient.buildWallet( + mnemonic, + config.NETWORK_BECH + ); + let accountdetails = await randomAddress.getAccounts(); + let nymWallet = accountdetails[0].address; + return nymWallet; + }; \ No newline at end of file