Skip to content

Commit

Permalink
chore: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shifty11 committed May 1, 2024
1 parent f1794be commit f56bc77
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 3 deletions.
7 changes: 6 additions & 1 deletion common/protocol/src/methods/checks/isEndkeyReached.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Validator, standardizeError } from "../..";
import { standardizeError, Validator } from "../..";

/**
isEndkeyReached checks if the end key is reached.
Expand All @@ -8,6 +8,11 @@ export async function isEndkeyReached(
toKey: string
): Promise<boolean> {
try {
// No further checks are needed if the current_key is empty
if (!this.pool.data!.current_key) {
return false;
}

// Check if the current_key or to_key equals the end_key
if (
this.pool.data!.current_key === this.pool.data!.end_key ||
Expand Down
93 changes: 93 additions & 0 deletions common/protocol/test/checks/is_endkey_reached.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { register } from "prom-client";
import { Logger } from "tslog";

import { Validator } from "../../src";
import { isEndkeyReached, setupMetrics } from "../../src/methods";
import { genesis_pool } from "../mocks/constants";
import { TestRuntime } from "../mocks/runtime.mock";

/*
TEST CASES - isEndkeyReached
* assert isEndkeyReached with empty current_key
* assert isEndkeyReached with end_key == current_key
* assert isEndkeyReached with toKey == end_key
* assert isEndkeyReached is not reached
*/

describe("isEndkeyReached", () => {
let v: Validator;

beforeEach(() => {
v = new Validator(new TestRuntime());

// mock logger
v.logger = new Logger();

v.logger.info = jest.fn();
v.logger.debug = jest.fn();
v.logger.warn = jest.fn();
v.logger.error = jest.fn();
v.logger.fatal = jest.fn();

v.pool = {
...genesis_pool,
} as any;

setupMetrics.call(v);
});

afterEach(() => {
// reset prometheus
register.clear();
});

test("assert isEndkeyReached with empty current_key", async () => {
// ARRANGE
v.pool.data!.current_key = "";
v.pool.data!.end_key = "0";

// ACT
const result = await isEndkeyReached.call(v, "");

// ASSERT
expect(result).toBeFalsy();
});

test("assert isEndkeyReached with end_key == current_key", async () => {
// ARRANGE
v.pool.data!.current_key = "0";
v.pool.data!.end_key = "0";

// ACT
const result = await isEndkeyReached.call(v, "");

// ASSERT
expect(result).toBeTruthy();
});

test("assert isEndkeyReached with toKey == end_key", async () => {
// ARRANGE
v.pool.data!.current_key = "0";
v.pool.data!.end_key = "1";

// ACT
const result = await isEndkeyReached.call(v, "1");

// ASSERT
expect(result).toBeTruthy();
});

test("assert isEndkeyReached is not reached", async () => {
// ARRANGE
v.pool.data!.current_key = "0";
v.pool.data!.end_key = "1";

// ACT
const result = await isEndkeyReached.call(v, "0");

// ASSERT
expect(result).toBeFalsy();
});
});
14 changes: 13 additions & 1 deletion common/protocol/test/checks/is_pool_active.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Logger } from "tslog";
import { Validator } from "../../src/index";
import { setupMetrics, isPoolActive } from "../../src/methods";
import { isPoolActive, setupMetrics } from "../../src/methods";
import { register } from "prom-client";
import { TestRuntime } from "../mocks/runtime.mock";
import { genesis_pool } from "../mocks/constants";
Expand All @@ -13,6 +13,7 @@ TEST CASES - isPoolActive
* assert if pool status is POOL_STATUS_ACTIVE
* assert if pool status is POOL_STATUS_NO_FUNDS
* assert if pool status is POOL_STATUS_DISABLED
* assert if pool status is POOL_STATUS_END_KEY_REACHED
* assert if pool status is POOL_STATUS_NOT_ENOUGH_DELEGATION
* assert if pool status is POOL_STATUS_UPGRADING
* assert if pool status is POOL_STATUS_UNSPECIFIED
Expand Down Expand Up @@ -80,6 +81,17 @@ describe("isPoolActive", () => {
expect(result).toBeFalsy();
});

test("assert if pool status is POOL_STATUS_END_KEY_REACHED", async () => {
// ARRANGE
v.pool.status = PoolStatus.POOL_STATUS_END_KEY_REACHED;

// ACT
const result = isPoolActive.call(v);

// ASSERT
expect(result).toBeFalsy();
});

test("assert if pool status is POOL_STATUS_NOT_ENOUGH_DELEGATION", async () => {
// ARRANGE
v.pool.status = PoolStatus.POOL_STATUS_NOT_ENOUGH_DELEGATION;
Expand Down
1 change: 1 addition & 0 deletions common/protocol/test/mocks/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const genesis_pool = {
logo: "ar://9FJDam56yBbmvn8rlamEucATH5UcYqSBw468rlCXn8E",
config: "ar://bYvlKiVLb1YY0Gx4mjUhpWBkztqmA4uEN97kTaZtBfY",
start_key: "0",
end_key: "",
current_key: "",
current_summary: "",
current_index: "0",
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5218,7 +5218,7 @@ esutils@^2.0.2:
resolved "https://registry.yarnpkg.com/esutils/-/esutils-2.0.3.tgz#74d2eb4de0b8da1293711910d50775b9b710ef64"
integrity sha512-kVscqXk4OCp68SZ0dkgEKVi6/8ij300KBWTJq32P/dYeWTSwK41WyTxalN1eRmA5Z9UU/LX9D7FWSmV9SAYx6g==

ethers@^5.5.1:
ethers@^5.5.1, ethers@^5.6.5:
version "5.7.2"
resolved "https://registry.yarnpkg.com/ethers/-/ethers-5.7.2.tgz#3a7deeabbb8c030d4126b24f84e525466145872e"
integrity sha512-wswUsmWo1aOK8rR7DIKiWSw9DbLWe6x98Jrn8wcTflTVvaXhAMaB5zGAXy0GYQEQp9iO1iSHWVyARQm11zUtyg==
Expand Down

0 comments on commit f56bc77

Please sign in to comment.