"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const types_1 = require("@polkadot/types");
const chai_1 = require("../../chai");
const factory_1 = require("../../../src/factory");
const types_2 = require("../../../src/types");
const utils_1 = require("../../../src/utils");
describe("Encoding", () => {
    let registry;
    const createH256Le = (hash) => {
        return new (registry.createClass("H256Le"))(registry, hash);
    };
    before(() => {
        registry = new types_1.TypeRegistry();
        registry.register(factory_1.getAPITypes());
    });
    it("should encode / decode same block hash as H256Le", () => {
        const blockHashHexLE = "0x9067166e896765258f6636a082abad6953f17a0e8dc21fc4f85648ceeedbda69";
        const blockHash = createH256Le(blockHashHexLE);
        return chai_1.assert.equal(blockHash.toHex(), blockHashHexLE);
    });
    it("should strip prefix", () => {
        const blockHashHexBEWithPrefix = "0x5499ac3ca3ddf563ace6b6a56ec2e8bdc5f796bef249445c36d90a69d0757d4c";
        const blockHashHexBEWithoutPrefix = "5499ac3ca3ddf563ace6b6a56ec2e8bdc5f796bef249445c36d90a69d0757d4c";
        chai_1.assert.equal(utils_1.stripHexPrefix(blockHashHexBEWithPrefix), blockHashHexBEWithoutPrefix);
        chai_1.assert.equal(utils_1.stripHexPrefix(blockHashHexBEWithoutPrefix), blockHashHexBEWithoutPrefix);
    });
    it("should reverse endianness from le to be", () => {
        const blockHashHexLE = "0x9067166e896765258f6636a082abad6953f17a0e8dc21fc4f85648ceeedbda69";
        const blockHashHexBE = "0x69dadbeece4856f8c41fc28d0e7af15369adab82a036668f256567896e166790";
        const blockHash = createH256Le(blockHashHexLE);
        const result = utils_1.uint8ArrayToString(utils_1.reverseEndianness(blockHash));
        return chai_1.assert.equal(result, utils_1.stripHexPrefix(blockHashHexBE));
    });
    it("should reverse endianness hex", () => {
        const blockHashHexLE = "0x9067166e896765258f6636a082abad6953f17a0e8dc21fc4f85648ceeedbda69";
        const blockHashHexBE = "0x69dadbeece4856f8c41fc28d0e7af15369adab82a036668f256567896e166790";
        return chai_1.assert.equal(utils_1.reverseEndiannessHex(blockHashHexLE), utils_1.stripHexPrefix(blockHashHexBE));
    });
    describe("parseRedeemRequestStatus", () => {
        const buildMockStatus = (status) => ({
            isPending: status === "Pending",
            isCompleted: status === "Completed",
            isReimbursed: status === "Reimbursed",
            isRetried: status === "Retried",
            type: status,
        });
        const buildMockRedeemRequest = (status, opentime, period) => {
            return {
                period: registry.createType("u32", period),
                opentime: registry.createType("u32", opentime),
                status: status,
            };
        };
        const assertEqualPretty = (expected, actual) => {
            chai_1.assert.equal(actual, expected, `Expected '${types_2.RedeemStatus[expected]}' but was '${types_2.RedeemStatus[actual]}'`);
        };
        it("should correctly parse completed status", () => {
            const mockRequest = buildMockRedeemRequest(buildMockStatus("Completed"));
            const expectedStatus = types_2.RedeemStatus.Completed;
            const actualStatus = utils_1.parseRedeemRequestStatus(mockRequest, 42, 42);
            assertEqualPretty(actualStatus, expectedStatus);
        });
        it("should correctly parse reimbursed status", () => {
            const mockRequest = buildMockRedeemRequest(buildMockStatus("Reimbursed"));
            const expectedStatus = types_2.RedeemStatus.Reimbursed;
            const actualStatus = utils_1.parseRedeemRequestStatus(mockRequest, 42, 42);
            assertEqualPretty(actualStatus, expectedStatus);
        });
        it("should correctly parse retried status", () => {
            const mockRequest = buildMockRedeemRequest(buildMockStatus("Retried"));
            const expectedStatus = types_2.RedeemStatus.Retried;
            const actualStatus = utils_1.parseRedeemRequestStatus(mockRequest, 42, 42);
            assertEqualPretty(actualStatus, expectedStatus);
        });
        describe("should correctly parse expired status", () => {
            const currentBlock = 42;
            const opentimeBlock = 1;
            // pending status internally, but expect expired due to blocks elapsed
            const mockInternalPendingStatus = buildMockStatus("Pending");
            const expectedStatus = types_2.RedeemStatus.Expired;
            it("when global redeem period is greater than request period and less than opentime + period", () => {
                const globalRedeemPeriod = currentBlock - 5;
                const requestPeriod = globalRedeemPeriod - 3;
                // preconditions
                chai_1.assert.isAbove(globalRedeemPeriod, requestPeriod, "Precondition failed: fix test setup");
                chai_1.assert.isBelow(globalRedeemPeriod, currentBlock, "Precondition failed: fix test setup");
                const mockRequest = buildMockRedeemRequest(mockInternalPendingStatus, opentimeBlock, requestPeriod);
                const actualStatus = utils_1.parseRedeemRequestStatus(mockRequest, globalRedeemPeriod, currentBlock);
                assertEqualPretty(actualStatus, expectedStatus);
            });
            it("when request period is greater than global redeem period and less than opentime + period", () => {
                const requestPeriod = currentBlock - 3;
                const globalRedeemPeriod = requestPeriod - 5;
                // preconditions
                chai_1.assert.isAbove(requestPeriod, globalRedeemPeriod, "Precondition failed: fix test setup");
                chai_1.assert.isBelow(requestPeriod, currentBlock, "Precondition failed: fix test setup");
                const mockRequest = buildMockRedeemRequest(mockInternalPendingStatus, opentimeBlock, requestPeriod);
                const actualStatus = utils_1.parseRedeemRequestStatus(mockRequest, globalRedeemPeriod, currentBlock);
                assertEqualPretty(actualStatus, expectedStatus);
            });
        });
        describe("should correctly parse pending status", () => {
            const currentBlock = 42;
            const opentimeBlock = 1;
            const mockInternalPendingStatus = buildMockStatus("Pending");
            const expectedStatus = types_2.RedeemStatus.PendingWithBtcTxNotFound;
            it("when opentime + period is greater than current block cont", () => {
                const globalRedeemPeriod = 50;
                const requestPeriod = 25;
                // preconditions
                chai_1.assert.isTrue(opentimeBlock + Math.max(requestPeriod, globalRedeemPeriod) > currentBlock, "Precondition failed: fix test setup");
                const mockRequest = buildMockRedeemRequest(mockInternalPendingStatus, opentimeBlock, requestPeriod);
                const actualStatus = utils_1.parseRedeemRequestStatus(mockRequest, globalRedeemPeriod, currentBlock);
                assertEqualPretty(actualStatus, expectedStatus);
            });
            it("when opentime + period is equal to current block count", () => {
                const globalRedeemPeriod = currentBlock - opentimeBlock;
                // anything less than above
                const requestPeriod = globalRedeemPeriod - 1;
                // preconditions
                chai_1.assert.isTrue(opentimeBlock + Math.max(requestPeriod, globalRedeemPeriod) == currentBlock, "Precondition failed: fix test setup");
                const mockRequest = buildMockRedeemRequest(mockInternalPendingStatus, opentimeBlock, requestPeriod);
                const actualStatus = utils_1.parseRedeemRequestStatus(mockRequest, globalRedeemPeriod, currentBlock);
                assertEqualPretty(actualStatus, expectedStatus);
            });
        });
    });
});
//# sourceMappingURL=encoding.test.js.map