From 6756eef636662bb422dc6c32b850d582cfd44b93 Mon Sep 17 00:00:00 2001 From: David Totraev Date: Mon, 16 Sep 2024 17:05:07 +0500 Subject: [PATCH] fix: tests --- .vscode/launch.json | 18 +++ tests/helper/index.ts | 7 +- .../utils/delegations/createStakingTx.test.ts | 5 +- tests/utils/delegations/fee.test.ts | 115 +++++++++--------- .../utils/delegations/signUnbondingTx.test.ts | 64 ++++------ tests/utils/globalParams.test.ts | 2 +- 6 files changed, 101 insertions(+), 110 deletions(-) create mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 00000000..3b86a2ab --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,18 @@ +{ + "version": "1.0.0", + "configurations": [ + { + "type": "node", + "request": "launch", + "name": "Jest: current file", + //"env": { "NODE_ENV": "test" }, + "program": "${workspaceFolder}/node_modules/.bin/jest", + "args": ["${fileBasenameNoExtension}", "--config", "jest.config.ts"], + "console": "integratedTerminal", + "disableOptimisticBPs": true, + "windows": { + "program": "${workspaceFolder}/node_modules/jest/bin/jest" + } + } + ] +} diff --git a/tests/helper/index.ts b/tests/helper/index.ts index 6ec4ba49..980d5474 100644 --- a/tests/helper/index.ts +++ b/tests/helper/index.ts @@ -10,6 +10,7 @@ import ECPairFactory from "ecpair"; import { GlobalParamsVersion } from "@/app/types/globalParams"; import { createStakingTx } from "@/utils/delegations/signStakingTx"; import { getCurrentGlobalParamsVersion } from "@/utils/globalParams"; +import { getPublicKeyNoCoord } from "@/utils/wallet"; import { UTXO } from "@/utils/wallet/wallet_provider"; // Initialize the ECC library @@ -62,7 +63,7 @@ export class DataGenerator { return { keyPair, publicKey: pk, - noCoordPublicKey: pk.slice(2), + noCoordPublicKey: getPublicKeyNoCoord(pk).toString("hex"), }; }; @@ -113,9 +114,9 @@ export class DataGenerator { const unbondingTime = this.generateRandomUnbondingTime(stakingTerm); const tag = this.generateRandomTag().toString("hex"); - let minStakingTimeBlocks = Math.floor(Math.random() * 100); + let minStakingTimeBlocks = this.getRandomIntegerBetween(1, 100); let maxStakingTimeBlocks = - minStakingTimeBlocks + Math.floor(Math.random() * 1000); + minStakingTimeBlocks + this.getRandomIntegerBetween(1, 1000); if (isFixedTimelock) { maxStakingTimeBlocks = minStakingTimeBlocks; } diff --git a/tests/utils/delegations/createStakingTx.test.ts b/tests/utils/delegations/createStakingTx.test.ts index 486883f0..a85649bf 100644 --- a/tests/utils/delegations/createStakingTx.test.ts +++ b/tests/utils/delegations/createStakingTx.test.ts @@ -11,7 +11,6 @@ describe.each(testingNetworks)( const feeRate = DEFAULT_TEST_FEE_RATE; const { address: stakerTaprootAddress, scriptPubKey } = dataGen.getAddressAndScriptPubKey(randomStakerKeys.publicKey).taproot; - const randomParam = dataGen.generateRandomGlobalParams(isFixed); const randomStakingAmount = dataGen.getRandomIntegerBetween( randomParam.minStakingAmountSat, @@ -22,8 +21,8 @@ describe.each(testingNetworks)( randomParam.maxStakingTimeBlocks, ); const randomInputUTXOs = dataGen.generateRandomUTXOs( - randomStakingAmount + Math.floor(Math.random() * 100000000), - Math.floor(Math.random() * 10) + 1, + randomStakingAmount + dataGen.getRandomIntegerBetween(0, 100000000), + dataGen.getRandomIntegerBetween(1, 10), scriptPubKey, ); const testTermDescription = isFixed ? "fixed term" : "variable term"; diff --git a/tests/utils/delegations/fee.test.ts b/tests/utils/delegations/fee.test.ts index b1a46b82..746ee41c 100644 --- a/tests/utils/delegations/fee.test.ts +++ b/tests/utils/delegations/fee.test.ts @@ -2,63 +2,58 @@ import { txFeeSafetyCheck } from "@/utils/delegations/fee"; import { testingNetworks } from "../../helper"; -describe.each(testingNetworks)( - "txFeeSafetyCheck", - ({ networkName, dataGenerator }) => { - const feeRate = dataGenerator.generateRandomFeeRates(); - const globalParams = dataGenerator.generateGlobalPramsVersions( - dataGenerator.getRandomIntegerBetween(1, 10), - ); - const randomParam = - globalParams[ - dataGenerator.getRandomIntegerBetween(0, globalParams.length - 1) - ]; - const { signedPsbt } = dataGenerator.createRandomStakingPsbt( - globalParams, - randomParam.activationHeight + 1, - ); - const tx = signedPsbt.extractTransaction(); - - describe(`on ${networkName} - `, () => { - test("should not throw an error if the estimated fee is within the acceptable range", () => { - let estimatedFee = (tx.virtualSize() * feeRate) / 2 + 1; - expect(() => { - txFeeSafetyCheck(tx, feeRate, estimatedFee); - }).not.toThrow(); - - estimatedFee = tx.virtualSize() * feeRate * 2 - 1; - expect(() => { - txFeeSafetyCheck(tx, feeRate, estimatedFee); - }).not.toThrow(); - }); - - test("should throw an error if the estimated fee is too high", () => { - const estimatedFee = tx.virtualSize() * feeRate * 2 + 1; - - expect(() => { - txFeeSafetyCheck(tx, feeRate, estimatedFee); - }).toThrow("Estimated fee is too high"); - }); - - test("should throw an error if the estimated fee is too low", () => { - const estimatedFee = (tx.virtualSize() * feeRate) / 2 - 1; - - expect(() => { - txFeeSafetyCheck(tx, feeRate, estimatedFee); - }).toThrow("Estimated fee is too low"); - }); - - test("should not throw an error if the estimated fee is exactly within the boundary", () => { - let estimatedFee = (tx.virtualSize() * feeRate) / 2; - expect(() => { - txFeeSafetyCheck(tx, feeRate, estimatedFee); - }).not.toThrow(); - - estimatedFee = tx.virtualSize() * feeRate * 2; - expect(() => { - txFeeSafetyCheck(tx, feeRate, estimatedFee); - }).not.toThrow(); - }); - }); - }, -); +describe.each(testingNetworks)("txFeeSafetyCheck", ({ dataGenerator }) => { + const feeRate = dataGenerator.generateRandomFeeRates(); + const globalParams = dataGenerator.generateGlobalPramsVersions( + dataGenerator.getRandomIntegerBetween(1, 10), + ); + const randomParam = + globalParams[ + dataGenerator.getRandomIntegerBetween(0, globalParams.length - 1) + ]; + const { signedPsbt } = dataGenerator.createRandomStakingPsbt( + globalParams, + randomParam.activationHeight + 1, + ); + const tx = signedPsbt.extractTransaction(); + + test("should not throw an error if the estimated fee is within the acceptable range", () => { + let estimatedFee = (tx.virtualSize() * feeRate) / 2 + 1; + expect(() => { + txFeeSafetyCheck(tx, feeRate, estimatedFee); + }).not.toThrow(); + + estimatedFee = tx.virtualSize() * feeRate * 2 - 1; + expect(() => { + txFeeSafetyCheck(tx, feeRate, estimatedFee); + }).not.toThrow(); + }); + + test("should throw an error if the estimated fee is too high", () => { + const estimatedFee = tx.virtualSize() * feeRate * 2 + 1; + + expect(() => { + txFeeSafetyCheck(tx, feeRate, estimatedFee); + }).toThrow("Estimated fee is too high"); + }); + + test("should throw an error if the estimated fee is too low", () => { + const estimatedFee = (tx.virtualSize() * feeRate) / 2 - 1; + + expect(() => { + txFeeSafetyCheck(tx, feeRate, estimatedFee); + }).toThrow("Estimated fee is too low"); + }); + + test("should not throw an error if the estimated fee is exactly within the boundary", () => { + let estimatedFee = (tx.virtualSize() * feeRate) / 2; + expect(() => { + txFeeSafetyCheck(tx, feeRate, estimatedFee); + }).not.toThrow(); + + estimatedFee = tx.virtualSize() * feeRate * 2; + expect(() => { + txFeeSafetyCheck(tx, feeRate, estimatedFee); + }).not.toThrow(); + }); +}); diff --git a/tests/utils/delegations/signUnbondingTx.test.ts b/tests/utils/delegations/signUnbondingTx.test.ts index 315a53f1..77b300c9 100644 --- a/tests/utils/delegations/signUnbondingTx.test.ts +++ b/tests/utils/delegations/signUnbondingTx.test.ts @@ -1,9 +1,3 @@ -import { Psbt, Transaction } from "bitcoinjs-lib"; - -import { signUnbondingTx } from "@/utils/delegations/signUnbondingTx"; - -import { testingNetworks } from "../../helper"; - jest.mock("@/app/api/getUnbondingEligibility", () => ({ getUnbondingEligibility: jest.fn(), })); @@ -14,6 +8,15 @@ jest.mock("@/app/api/postUnbonding", () => ({ postUnbonding: jest.fn(), })); +import { Psbt, Transaction } from "bitcoinjs-lib"; + +import { getGlobalParams } from "@/app/api/getGlobalParams"; +import { getUnbondingEligibility } from "@/app/api/getUnbondingEligibility"; +import { postUnbonding } from "@/app/api/postUnbonding"; +import { signUnbondingTx } from "@/utils/delegations/signUnbondingTx"; + +import { testingNetworks } from "../../helper"; + describe("signUnbondingTx", () => { const { network, dataGenerator } = testingNetworks[0]; const randomTxId = dataGenerator.generateRandomTxId(); @@ -41,7 +44,8 @@ describe("signUnbondingTx", () => { dataGenerator.generateRandomKeyPair().noCoordPublicKey, stakingTx: { startHeight: randomStakingTxHeight, - timelock: signedPsbtTx.locktime, + timelock: 150, + //timelock: signedPsbtTx.locktime, txHex: signedPsbtTx.toHex(), outputIndex: 0, }, @@ -89,10 +93,7 @@ describe("signUnbondingTx", () => { }); it("should throw an error if the stakingTx is not eligible for unbonding", async () => { - const { - getUnbondingEligibility, - } = require("@/app/api/getUnbondingEligibility"); - getUnbondingEligibility.mockImplementationOnce(async () => { + (getUnbondingEligibility as any).mockImplementationOnce(async () => { return false; }); @@ -108,15 +109,10 @@ describe("signUnbondingTx", () => { }); it("should throw an error if global param is not loaded", async () => { - const { - getUnbondingEligibility, - } = require("@/app/api/getUnbondingEligibility"); - getUnbondingEligibility.mockImplementationOnce(async () => { + (getUnbondingEligibility as any).mockImplementationOnce(async () => { return true; }); - - const { getGlobalParams } = require("@/app/api/getGlobalParams"); - getGlobalParams.mockImplementationOnce(async () => { + (getGlobalParams as any).mockImplementationOnce(async () => { return []; }); @@ -132,15 +128,10 @@ describe("signUnbondingTx", () => { }); it("should throw an error if the current version is not found", async () => { - const { - getUnbondingEligibility, - } = require("@/app/api/getUnbondingEligibility"); - getUnbondingEligibility.mockImplementationOnce(async () => { + (getUnbondingEligibility as any).mockImplementationOnce(async () => { return true; }); - - const { getGlobalParams } = require("@/app/api/getGlobalParams"); - getGlobalParams.mockImplementationOnce(async () => { + (getGlobalParams as any).mockImplementationOnce(async () => { return randomGlobalParamsVersions; }); @@ -166,18 +157,12 @@ describe("signUnbondingTx", () => { }); it("should throw error if fail to signPsbtTx", async () => { - const { - getUnbondingEligibility, - } = require("@/app/api/getUnbondingEligibility"); - getUnbondingEligibility.mockImplementationOnce(async () => { + (getUnbondingEligibility as any).mockImplementationOnce(async () => { return true; }); - - const { getGlobalParams } = require("@/app/api/getGlobalParams"); - getGlobalParams.mockImplementationOnce(async () => { + (getGlobalParams as any).mockImplementationOnce(async () => { return randomGlobalParamsVersions; }); - mockedSignPsbtTx.mockRejectedValueOnce(new Error("oops!")); expect( @@ -192,20 +177,13 @@ describe("signUnbondingTx", () => { }); it("should return the signed unbonding transaction", async () => { - const { - getUnbondingEligibility, - } = require("@/app/api/getUnbondingEligibility"); - getUnbondingEligibility.mockImplementationOnce(async () => { + (getUnbondingEligibility as any).mockImplementationOnce(async () => { return true; }); - - const { getGlobalParams } = require("@/app/api/getGlobalParams"); - getGlobalParams.mockImplementationOnce(async () => { + (getGlobalParams as any).mockImplementationOnce(async () => { return randomGlobalParamsVersions; }); - - const { postUnbonding } = require("@/app/api/postUnbonding"); - postUnbonding.mockImplementationOnce(async () => { + (postUnbonding as any).mockImplementationOnce(async () => { return; }); diff --git a/tests/utils/globalParams.test.ts b/tests/utils/globalParams.test.ts index dceae9dd..2fd07f4b 100644 --- a/tests/utils/globalParams.test.ts +++ b/tests/utils/globalParams.test.ts @@ -11,7 +11,7 @@ describe("globalParams", () => { beforeEach(() => { globalParams = dataGenerator.generateGlobalPramsVersions( - dataGenerator.getRandomIntegerBetween(1, 100), + dataGenerator.getRandomIntegerBetween(2, 100), ); lastActivationHeight = globalParams[globalParams.length - 1].activationHeight;