diff --git a/__tests__/unit/core-webhooks/conditions.test.ts b/__tests__/unit/core-webhooks/conditions.test.ts index 036e00e239..74b58758c5 100644 --- a/__tests__/unit/core-webhooks/conditions.test.ts +++ b/__tests__/unit/core-webhooks/conditions.test.ts @@ -22,6 +22,12 @@ describe("Conditions - between", () => { max: 2, }), ).toBeTrue(); + expect( + between("1.5", { + min: "1", + max: "2", + }), + ).toBeTrue(); }); it("should be false", () => { @@ -31,6 +37,12 @@ describe("Conditions - between", () => { max: 2, }), ).toBeFalse(); + expect( + between("3", { + min: "1", + max: "2", + }), + ).toBeFalse(); }); }); @@ -47,30 +59,46 @@ describe("Conditions - contains", () => { describe("Conditions - equal", () => { it("should be true", () => { expect(eq(1, 1)).toBeTrue(); + expect(eq("1", "1")).toBeTrue(); }); it("should be false", () => { expect(eq(1, 2)).toBeFalse(); + expect(eq("1", "2")).toBeFalse(); }); }); describe("Conditions - falsy", () => { it("should be true", () => { expect(falsy(false)).toBeTrue(); + expect(falsy("false")).toBeTrue(); + expect(falsy("FaLsE")).toBeTrue(); }); it("should be false", () => { expect(falsy(true)).toBeFalse(); + expect(falsy("true")).toBeFalse(); + expect(falsy("TrUe")).toBeFalse(); }); }); describe("Conditions - greater than", () => { it("should be true", () => { expect(gt(2, 1)).toBeTrue(); + expect(gt("2", "1")).toBeTrue(); + expect(gt("10", "2")).toBeTrue(); }); it("should be false", () => { expect(gt(1, 2)).toBeFalse(); + expect(gt("1", "2")).toBeFalse(); + expect(gt("2", "10")).toBeFalse(); + expect(gt(undefined, NaN)).toBeFalse(); + expect(gt(1, NaN)).toBeFalse(); + expect(gt(undefined, 1)).toBeFalse(); + expect(gt("null", "NaN")).toBeFalse(); + expect(gt("1", "NaN")).toBeFalse(); + expect(gt("null", "1")).toBeFalse(); }); }); @@ -78,20 +106,37 @@ describe("Conditions - greater than or equal", () => { it("should be true", () => { expect(gte(2, 1)).toBeTrue(); expect(gte(2, 2)).toBeTrue(); + expect(gte("2", "1")).toBeTrue(); + expect(gte("2", "2")).toBeTrue(); }); it("should be false", () => { expect(gte(1, 2)).toBeFalse(); + expect(gte("1", "2")).toBeFalse(); + expect(gt(undefined, NaN)).toBeFalse(); + expect(gt(1, NaN)).toBeFalse(); + expect(gt(undefined, 1)).toBeFalse(); + expect(gt("null", "NaN")).toBeFalse(); + expect(gt("1", "NaN")).toBeFalse(); + expect(gt("null", "1")).toBeFalse(); }); }); describe("Conditions - less than", () => { it("should be true", () => { expect(lt(1, 2)).toBeTrue(); + expect(lt("1", "2")).toBeTrue(); }); it("should be false", () => { expect(lt(2, 1)).toBeFalse(); + expect(lt("2", "1")).toBeFalse(); + expect(gt(undefined, NaN)).toBeFalse(); + expect(gt(1, NaN)).toBeFalse(); + expect(gt(undefined, 1)).toBeFalse(); + expect(gt("null", "NaN")).toBeFalse(); + expect(gt("1", "NaN")).toBeFalse(); + expect(gt("null", "1")).toBeFalse(); }); }); @@ -99,20 +144,31 @@ describe("Conditions - less than or equal", () => { it("should be true", () => { expect(lte(1, 2)).toBeTrue(); expect(lte(1, 1)).toBeTrue(); + expect(lte("1", "2")).toBeTrue(); + expect(lte("1", "1")).toBeTrue(); }); it("should be false", () => { expect(lte(2, 1)).toBeFalse(); + expect(lte("2", "1")).toBeFalse(); + expect(gt(undefined, NaN)).toBeFalse(); + expect(gt(1, NaN)).toBeFalse(); + expect(gt(undefined, 1)).toBeFalse(); + expect(gt("null", "NaN")).toBeFalse(); + expect(gt("1", "NaN")).toBeFalse(); + expect(gt("null", "1")).toBeFalse(); }); }); describe("Conditions - not equal", () => { it("should be true", () => { expect(ne(1, 2)).toBeTrue(); + expect(ne("1", "2")).toBeTrue(); }); it("should be false", () => { expect(ne(1, 1)).toBeFalse(); + expect(ne("1", "1")).toBeFalse(); }); }); @@ -124,6 +180,12 @@ describe("Conditions - not-between", () => { max: 2, }), ).toBeTrue(); + expect( + notBetween("3", { + min: "1", + max: "2", + }), + ).toBeTrue(); }); it("should be false", () => { @@ -133,6 +195,12 @@ describe("Conditions - not-between", () => { max: 2, }), ).toBeFalse(); + expect( + notBetween("1.5", { + min: "1", + max: "2", + }), + ).toBeFalse(); }); }); @@ -149,9 +217,13 @@ describe("Conditions - regexp", () => { describe("Conditions - truthy", () => { it("should be true", () => { expect(truthy(true)).toBeTrue(); + expect(truthy("true")).toBeTrue(); + expect(truthy("TrUe")).toBeTrue(); }); it("should be false", () => { expect(truthy(false)).toBeFalse(); + expect(truthy("false")).toBeFalse(); + expect(truthy("FaLsE")).toBeFalse(); }); }); diff --git a/packages/core-webhooks/src/conditions.ts b/packages/core-webhooks/src/conditions.ts index 764257076b..316934a91e 100644 --- a/packages/core-webhooks/src/conditions.ts +++ b/packages/core-webhooks/src/conditions.ts @@ -1,12 +1,16 @@ -export const between = (actual, expected): boolean => actual > expected.min && actual < expected.max; +import { Utils } from "@arkecosystem/crypto"; + +const toBoolean = (value): boolean => value.toString().toLowerCase().trim() === "true" ? true : false; + +export const between = (actual, expected): boolean => gt(actual, expected.min) && lt(actual, expected.max); export const contains = (actual, expected): boolean => actual.includes(expected); -export const eq = (actual, expected): boolean => actual === expected; -export const falsy = (actual): boolean => actual === false; -export const gt = (actual, expected): boolean => actual > expected; -export const gte = (actual, expected): boolean => actual >= expected; -export const lt = (actual, expected): boolean => actual < expected; -export const lte = (actual, expected): boolean => actual <= expected; -export const ne = (actual, expected): boolean => actual !== expected; +export const eq = (actual, expected): boolean => JSON.stringify(actual) === JSON.stringify(expected); +export const falsy = (actual): boolean => actual === false || !toBoolean(actual); +export const gt = (actual, expected): boolean => Utils.BigNumber.make(actual).gt(expected); +export const gte = (actual, expected): boolean => Utils.BigNumber.make(actual).gte(expected); +export const lt = (actual, expected): boolean => Utils.BigNumber.make(actual).lt(expected); +export const lte = (actual, expected): boolean => Utils.BigNumber.make(actual).lte(expected); +export const ne = (actual, expected): boolean => !eq(actual, expected); export const notBetween = (actual, expected): boolean => !between(actual, expected); export const regexp = (actual, expected): boolean => new RegExp(expected).test(actual); -export const truthy = (actual): boolean => actual === true; +export const truthy = (actual): boolean => actual === true || toBoolean(actual);