Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
totraev committed Sep 16, 2024
1 parent 76c60ca commit e6fb940
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 110 deletions.
17 changes: 17 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"version": "1.0.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Jest: current file",
"program": "${workspaceFolder}/node_modules/.bin/jest",
"args": ["${fileBasenameNoExtension}", "--config", "jest.config.ts"],
"console": "integratedTerminal",
"disableOptimisticBPs": true,
"windows": {
"program": "${workspaceFolder}/node_modules/jest/bin/jest"
}
}
]
}
7 changes: 4 additions & 3 deletions tests/helper/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -62,7 +63,7 @@ export class DataGenerator {
return {
keyPair,
publicKey: pk,
noCoordPublicKey: pk.slice(2),
noCoordPublicKey: getPublicKeyNoCoord(pk).toString("hex"),
};
};

Expand Down Expand Up @@ -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;
}
Expand Down
5 changes: 2 additions & 3 deletions tests/utils/delegations/createStakingTx.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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";
Expand Down
115 changes: 55 additions & 60 deletions tests/utils/delegations/fee.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
64 changes: 21 additions & 43 deletions tests/utils/delegations/signUnbondingTx.test.ts
Original file line number Diff line number Diff line change
@@ -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(),
}));
Expand All @@ -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();
Expand Down Expand Up @@ -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,
},
Expand Down Expand Up @@ -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;
});

Expand All @@ -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 [];
});

Expand All @@ -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;
});

Expand All @@ -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(
Expand All @@ -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;
});

Expand Down
2 changes: 1 addition & 1 deletion tests/utils/globalParams.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe("globalParams", () => {

beforeEach(() => {
globalParams = dataGenerator.generateGlobalPramsVersions(
dataGenerator.getRandomIntegerBetween(1, 100),
dataGenerator.getRandomIntegerBetween(2, 100),
);
lastActivationHeight =
globalParams[globalParams.length - 1].activationHeight;
Expand Down

0 comments on commit e6fb940

Please sign in to comment.