"use strict";
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __read = (this && this.__read) || function (o, n) {
    var m = typeof Symbol === "function" && o[Symbol.iterator];
    if (!m) return o;
    var i = m.call(o), r, ar = [], e;
    try {
        while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
    }
    catch (error) { e = { error: error }; }
    finally {
        try {
            if (r && !r.done && (m = i["return"])) m.call(i);
        }
        finally { if (e) throw e.error; }
    }
    return ar;
};
var __spread = (this && this.__spread) || function () {
    for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
    return ar;
};
Object.defineProperty(exports, "__esModule", { value: true });
var ethers_1 = require("ethers");
var valid_transition_1 = require("../../src/valid-transition");
var walletA = ethers_1.Wallet.createRandom();
var walletB = ethers_1.Wallet.createRandom();
var channel = {
    chainId: '0x1',
    channelNonce: 0,
    participants: [walletA.address, walletB.address],
};
var baseFromState = {
    turnNum: 0,
    isFinal: false,
    appDefinition: ethers_1.ethers.constants.AddressZero,
    appData: '0x00',
    outcome: [{ assetHolderAddress: ethers_1.ethers.constants.AddressZero, allocationItems: [] }],
    channel: channel,
    challengeDuration: 1,
};
var baseToState = __assign(__assign({}, baseFromState), { turnNum: baseFromState.turnNum + 1 });
var valid = [
    [baseFromState, baseToState]
];
var incorrectTurnNumCases = [
    [/turnNum must increment by one/, {}, { turnNum: baseToState.turnNum + 2 }],
    [/turnNum must increment by one/, {}, { turnNum: baseToState.turnNum + 3 }],
];
var changedAConstantCases = [
    [/chainId must not change/, {}, { channel: __assign(__assign({}, baseToState.channel), { chainId: '0x2' }) }],
    [/participants must not change/, {}, { channel: __assign(__assign({}, baseToState.channel), { participants: [baseToState.channel[0]] }) }],
    [/channelNonce must not change/, {}, { channel: __assign(__assign({}, baseToState.channel), { channelNonce: baseToState.channel.channelNonce + 1 }) }],
    [/appDefinition must not change/, {}, { appDefinition: channel.participants[0] }],
    [/challengeDuration must not change/, {}, { challengeDuration: baseToState.challengeDuration + 1 }],
];
var changedOutcomeWhenToIsFinal = [
    [/Outcome change verboten/, {}, { isFinal: true, outcome: [] }],
];
var fromFinalToNotFinal = [
    [/isFinal retrograde/, { isFinal: true }, {}],
];
var changedOutcomeDuringSetup = [
    [/Outcome change forbidden/, { turnNum: 1 }, { turnNum: 2, outcome: [] },],
];
var changedAppDataDuringSetup = [
    [/appData change forbidden/, { turnNum: 1 }, { turnNum: 2, appData: '0x02' }],
];
var commonCase = [
    [{ turnNum: 6 }, { turnNum: 7 }],
];
function expectStatus(fromOverrides, toOverrides, status) {
    var from = __assign(__assign({}, baseFromState), fromOverrides);
    var to = __assign(__assign({}, baseToState), toOverrides);
    expect(valid_transition_1.requireValidProtocolTransition(from, to)).toEqual(status);
}
function expectError(fromOverrides, toOverrides, pattern) {
    var from = __assign(__assign({}, baseFromState), fromOverrides);
    var to = __assign(__assign({}, baseToState), toOverrides);
    expect(function () { return valid_transition_1.requireValidProtocolTransition(from, to); }).toThrow(pattern);
}
describe('getValidProtocolTransitionStatus', function () {
    it.each(valid)('Returns Status.True for valid cases ', function (from, to) {
        expectStatus(from, to, valid_transition_1.Status.True);
    });
    it.each(__spread(incorrectTurnNumCases, changedAConstantCases, changedOutcomeWhenToIsFinal, fromFinalToNotFinal, changedOutcomeDuringSetup, changedAppDataDuringSetup))('Correctly throws error matching %p', function (pattern, from, to) {
        expectError(from, to, pattern);
    });
    it.each(__spread(commonCase))('Returns Status.NeedToCheckApp for otherwise valid cases ', function (from, to) {
        expectStatus(from, to, valid_transition_1.Status.NeedToCheckApp);
    });
});
//# sourceMappingURL=valid-transition.test.js.map