"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const chai_1 = require("chai");
const web3_1 = __importDefault(require("web3"));
const src_1 = require("../../src");
// eslint-disable-next-line @typescript-eslint/no-var-requires
const stdout = require("test-console").stdout;
// eslint-disable-next-line @typescript-eslint/no-var-requires
const stderr = require("test-console").stderr;
// eslint-disable-next-line @typescript-eslint/no-var-requires
const chai = require("chai");
// eslint-disable-next-line @typescript-eslint/no-var-requires
chai.use(require("chai-as-promised"));
describe("Utils tests ", () => {
    it("should return '0x0' if no tx", () => {
        const expected = "0x0";
        const res = (0, src_1.unPrefix0x)("");
        (0, chai_1.expect)(res).to.be.equal(expected);
    });
    it("should return false if no tx", () => {
        const res = (0, src_1.isPrefixed0x)("");
        (0, chai_1.expect)(res).to.be.false;
    });
    it("should return undefined", () => {
        const expected = undefined;
        const res = (0, src_1.toNumber)(undefined);
        (0, chai_1.expect)(res).to.be.equal(expected);
    });
    it("should return undefined", () => {
        const expected = undefined;
        const res = (0, src_1.toNumber)(null);
        (0, chai_1.expect)(res).to.be.equal(expected);
    });
    it("should return number", () => {
        const expected = 1;
        const res = (0, src_1.toNumber)(expected);
        (0, chai_1.expect)(res).to.be.equal(expected);
    });
    it("should return number", () => {
        const expected = 1;
        const res = (0, src_1.toNumber)(web3_1.default.utils.toBN(1));
        (0, chai_1.expect)(res).to.be.equal(expected);
    });
    it("should return snakecase", () => {
        const expected = "this-is-my-project";
        const res = (0, src_1.camelToSnakeCase)("thisIsMyProject");
        (0, chai_1.expect)(res).to.be.equal(expected);
    });
    it("should return snakecase from camelcase", () => {
        const expected = "this:is:my:project";
        const res = (0, src_1.camelToSnakeCase)("thisIsMyProject", ":");
        (0, chai_1.expect)(res).to.be.equal(expected);
    });
    it("should return snakecase", () => {
        const expected = { "first:argument": "thisIsMyProject", "second:argument": 2, 3: { "deep:argument": "deepProject" } };
        const obj = { firstArgument: "thisIsMyProject", secondArgument: 2, 3: { deepArgument: "deepProject" } };
        const res = (0, src_1.toSnakeCase)(obj, ":");
        (0, chai_1.expect)(res).to.be.eql(expected);
    });
    it("should return snakecase", () => {
        const expected = { "first-argument": "thisIsMyProject", "second-argument": 2, 3: { "deep-argument": "deepProject" } };
        const obj = { firstArgument: "thisIsMyProject", secondArgument: 2, 3: { deepArgument: "deepProject" } };
        const res = (0, src_1.toSnakeCase)(obj);
        (0, chai_1.expect)(res).to.be.eql(expected);
    });
    it("should return error", () => {
        const fn = () => {
            return (0, src_1.toBN)(1.0194e21, false);
        };
        (0, chai_1.expect)(fn).to.throw(Error);
    });
    it("should return 0", () => {
        const expected = web3_1.default.utils.toBN(0);
        const res = (0, src_1.toBN)(1.0194e21, true);
        (0, chai_1.expect)(res).to.be.eql(expected);
    });
    it("should return BN", () => {
        const expected = web3_1.default.utils.toBN(1);
        const res = (0, src_1.toBN)(expected);
        (0, chai_1.expect)(res).to.be.eql(expected);
    });
    it("should return hex", () => {
        (0, chai_1.expect)((0, src_1.toHex)("2022")).to.be.eq("0x7e6");
    });
    it("should return hex 2", () => {
        (0, chai_1.expect)((0, src_1.toHex)(2022)).to.be.eq("0x7e6");
    });
    it("should return hex 3", () => {
        const bn = web3_1.default.utils.toBN((0, src_1.toHex)(2022));
        (0, chai_1.expect)((0, src_1.toHex)(bn)).to.be.eq("0x7e6");
    });
    it("should return defaultMccLoggingObject", () => {
        const expected = {
            mode: "develop",
            loggingCallback: src_1.defaultLoggingCallback,
            warningCallback: src_1.defaultWarningCallback,
            exceptionCallback: src_1.defaultExceptionCallback,
        };
        const res = (0, src_1.defaultMccLoggingObject)();
        (0, chai_1.expect)(res).to.be.eql(expected);
    });
    it("should return fillWithDefault with empty object", () => {
        const expected = {
            mode: "develop",
            loggingCallback: src_1.defaultLoggingCallback,
            warningCallback: src_1.defaultWarningCallback,
            exceptionCallback: src_1.defaultExceptionCallback,
        };
        const res = (0, src_1.fillWithDefault)({});
        (0, chai_1.expect)(res).to.be.eql(expected);
    });
    it("should return fillWithDefault with set mode", () => {
        const expected = {
            mode: "off",
            loggingCallback: src_1.defaultLoggingCallback,
            warningCallback: src_1.defaultWarningCallback,
            exceptionCallback: src_1.defaultExceptionCallback,
        };
        const res = (0, src_1.fillWithDefault)({ mode: "off" });
        (0, chai_1.expect)(res).to.be.eql(expected);
    });
    it("should return fillWithDefault with set callbacks", () => {
        function justLikeDefaultLoggingCallback(message) {
            console.log(message);
        }
        function justLikeDefaultWarningCallback(message) {
            console.log(message);
        }
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        function justLikeDefaultExceptionCallback(error, message) {
            console.log(message);
            console.error(error);
            if (error.stack) {
                console.error(error.stack);
            }
        }
        const expected = {
            mode: "off",
            loggingCallback: justLikeDefaultLoggingCallback,
            warningCallback: justLikeDefaultWarningCallback,
            exceptionCallback: justLikeDefaultExceptionCallback,
        };
        const res = (0, src_1.fillWithDefault)({
            mode: "off",
            loggingCallback: justLikeDefaultLoggingCallback,
            warningCallback: justLikeDefaultWarningCallback,
            exceptionCallback: justLikeDefaultExceptionCallback,
        });
        (0, chai_1.expect)(res).to.be.eql(expected);
    });
    it("should eventually return ", async () => {
        await (0, chai_1.expect)((0, src_1.sleepMs)(1000)).to.eventually.be.fulfilled;
    });
    it("should return ", async () => {
        const n = 123;
        const res = (0, src_1.getSimpleRandom)(n);
        (0, chai_1.expect)(res).not.to.equal(n);
    });
    describe("Functions with console.log()", () => {
        it("should log", () => {
            const output = stdout.inspectSync(() => {
                (0, src_1.defaultLoggingCallback)("New message");
            });
            (0, chai_1.expect)(output).to.be.eql(["New message\n"]);
        });
        it("should log", () => {
            const output = stdout.inspectSync(() => {
                (0, src_1.defaultWarningCallback)("New message");
            });
            (0, chai_1.expect)(output).to.be.eql(["New message\n"]);
        });
        it("should log", () => {
            const output = stdout.inspectSync(() => {
                (0, src_1.defaultExceptionCallback)({}, "New message");
            });
            const outputErr = stderr.inspectSync(() => {
                (0, src_1.defaultExceptionCallback)({ stack: "New stack" }, "New message");
            });
            (0, chai_1.expect)(output).to.be.eql(["New message\n"]);
            (0, chai_1.expect)(outputErr[1]).to.be.equal("New stack\n");
            (0, chai_1.expect)(outputErr[0]).contains("New stack");
        });
    });
});
//# sourceMappingURL=utils.test.js.map