"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    result["default"] = mod;
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const initTestEnvironment_1 = require("../../../../tests/utils/initTestEnvironment");
const Contracts_1 = require("../../../../Contracts");
const deployMockSystem_1 = require("../../../../utils/deploy/deployMockSystem");
const deployAndGetContract_1 = require("../../../../utils/solidity/deployAndGetContract");
const emptyAddress_1 = require("../../../../utils/constants/emptyAddress");
const Web3Utils = __importStar(require("web3-utils"));
describe('mocks', () => {
    const shared = {};
    const createManagerAndRegister = (contract, policy) => __awaiter(this, void 0, void 0, function* () {
        const contracts = yield deployMockSystem_1.deployMockSystem(shared.env, {
            policyManagerContract: Contracts_1.Contracts.PolicyManager,
        });
        yield contracts.policyManager.methods
            .register(shared.testPolicy, policy)
            .send({ from: shared.user, gas: 8000000 });
        return contracts.policyManager;
    });
    beforeAll(() => __awaiter(this, void 0, void 0, function* () {
        shared.env = yield initTestEnvironment_1.initTestEnvironment();
        shared.user = shared.env.wallet.address;
        shared.falsePolicy = yield deployAndGetContract_1.deployAndGetContract(shared.env, Contracts_1.Contracts.FalsePolicy);
        shared.truePolicy = yield deployAndGetContract_1.deployAndGetContract(shared.env, Contracts_1.Contracts.TruePolicy);
        shared.testPolicy = Web3Utils.sha3('testPolicy(address[4],uint256[2])').substring(0, 10);
        shared.dummyArgs = [
            shared.testPolicy,
            [emptyAddress_1.emptyAddress, emptyAddress_1.emptyAddress, emptyAddress_1.emptyAddress, emptyAddress_1.emptyAddress, emptyAddress_1.emptyAddress],
            [0, 0, 0],
            '0x0',
        ];
    }));
    it('Boolean policies', () => __awaiter(this, void 0, void 0, function* () {
        const res1 = yield shared.falsePolicy.methods
            .rule(...shared.dummyArgs)
            .call();
        const res2 = yield shared.truePolicy.methods
            .rule(...shared.dummyArgs)
            .call();
        expect(res1).toBe(false);
        expect(res2).toBe(true);
    }));
    it('Boolean policies on policy manager', () => __awaiter(this, void 0, void 0, function* () {
        const manager1 = yield createManagerAndRegister(Contracts_1.Contracts.PolicyManager, shared.falsePolicy.options.address);
        yield expect(manager1.methods.preValidate(...shared.dummyArgs).call()).rejects.toThrow('Rule evaluated to false');
        const manager2 = yield createManagerAndRegister(Contracts_1.Contracts.PolicyManager, shared.truePolicy.options.address);
        yield expect(manager2.methods.preValidate(...shared.dummyArgs).call()).resolves.not.toThrow();
    }));
});
