Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: jest tests #151

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ dist
.svelte-kit

### VisualStudioCode ###
.vscode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
Expand Down Expand Up @@ -268,3 +269,4 @@ next-env.d.ts

# E2E Chrome Extensions
extensions

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"),
jrwbabylonlab marked this conversation as resolved.
Show resolved Hide resolved
};
};

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);
jrwbabylonlab marked this conversation as resolved.
Show resolved Hide resolved
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,
jrwbabylonlab marked this conversation as resolved.
Show resolved Hide resolved
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),
jrwbabylonlab marked this conversation as resolved.
Show resolved Hide resolved
);
lastActivationHeight =
globalParams[globalParams.length - 1].activationHeight;
Expand Down