"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
// eslint-disable-next-line @typescript-eslint/no-var-requires
const truffleAssert = require("truffle-assertions");
const TokenConservationWrapper = artifacts.require("TokenConservationWrapper");
contract("TokenConservation", () => __awaiter(void 0, void 0, void 0, function* () {
    describe("checkTokenConservation()", () => {
        it("checks successfully the token conservation", () => __awaiter(void 0, void 0, void 0, function* () {
            const tokenConservation = yield TokenConservationWrapper.new();
            const testArray = [1, 0, 0];
            yield tokenConservation.checkTokenConservationTest(testArray);
        }));
        it("throws, if token conservation does not hold", () => __awaiter(void 0, void 0, void 0, function* () {
            const tokenConservation = yield TokenConservationWrapper.new();
            const testArray = [0, 1];
            yield truffleAssert.reverts(tokenConservation.checkTokenConservationTest(testArray));
        }));
        it("throws, if token conservation does not hold", () => __awaiter(void 0, void 0, void 0, function* () {
            const tokenConservation = yield TokenConservationWrapper.new();
            const testArray = [-1];
            yield truffleAssert.reverts(tokenConservation.checkTokenConservationTest(testArray), "Token conservation at 0 must be positive");
        }));
    });
    describe("checkPriceOrdering()", () => {
        it("returns false when unordered", () => __awaiter(void 0, void 0, void 0, function* () {
            const tokenConservation = yield TokenConservationWrapper.new();
            assert.equal(yield tokenConservation.checkPriceOrdering([1, 0]), false, "Failed on [1, 0]");
            assert.equal(yield tokenConservation.checkPriceOrdering([1, 3, 2]), false, "Failed on [1, 3, 2]");
        }));
        it("returns false when not unique", () => __awaiter(void 0, void 0, void 0, function* () {
            const tokenConservation = yield TokenConservationWrapper.new();
            assert.equal(yield tokenConservation.checkPriceOrdering([1, 1]), false, "Failed on [1, 1]");
        }));
        it("returns true when ordered", () => __awaiter(void 0, void 0, void 0, function* () {
            const tokenConservation = yield TokenConservationWrapper.new();
            assert.equal(yield tokenConservation.checkPriceOrdering([0, 1]), true, "Failed on [0, 1]");
            assert.equal(yield tokenConservation.checkPriceOrdering([1, 2, 3]), true, "Failed on [1, 2, 3]");
        }));
    });
    describe("updateTokenConservation()", () => {
        it("calculates the updated tokenConservation array", () => __awaiter(void 0, void 0, void 0, function* () {
            const tokenConservation = yield TokenConservationWrapper.new();
            const testArray = [0, 0, 0, 0];
            const tokenIdsForPrice = [1, 2, 3];
            const buyToken = 2;
            const sellToken = 1;
            const buyAmount = 10;
            const sellAmount = 3;
            const updatedArray = (yield tokenConservation.updateTokenConservationTest(testArray, buyToken, sellToken, tokenIdsForPrice, buyAmount, sellAmount)).map((a) => a.toNumber());
            const expectedArray = [0, 3, -10, 0];
            assert.deepEqual(updatedArray, expectedArray);
            const secondUpdatedArray = (yield tokenConservation.updateTokenConservationTest(testArray, 2, 3, tokenIdsForPrice, 1, 2)).map((a) => a.toNumber());
            assert.deepEqual([0, 0, -1, 2], secondUpdatedArray);
        }));
        it("throws, if findPriceIndex does not find the token, as it is not supplied", () => __awaiter(void 0, void 0, void 0, function* () {
            const tokenConservation = yield TokenConservationWrapper.new();
            const testArray = [0, 0, 0];
            const tokenIdsForPrice = [0, 1];
            const buyToken = 2;
            const sellToken = 1;
            const buyAmount = 10;
            const sellAmount = 3;
            yield truffleAssert.reverts(tokenConservation.updateTokenConservationTest(testArray, buyToken, sellToken, tokenIdsForPrice, buyAmount, sellAmount));
        }));
    });
}));
