"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
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());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const sinon_1 = __importDefault(require("sinon"));
const quitSubsidy_1 = require("../quitSubsidy");
const polkadot_1 = require("../../../polkadot");
const mocks_1 = require("../../../testUtils/mocks");
const utilsConversionModule = __importStar(require("../../../utils/conversion"));
jest.mock('~/api/entities/Subsidy', require("../../../testUtils/mocks/entities").mockSubsidyModule('~/api/entities/Subsidy'));
describe('quitSubsidy procedure', () => {
    let mockContext;
    let beneficiary;
    let subsidizer;
    let subsidy;
    let stringToAccountIdStub;
    let args;
    beforeAll(() => {
        mocks_1.dsMockUtils.initMocks();
        mocks_1.procedureMockUtils.initMocks();
        mocks_1.entityMockUtils.initMocks();
        stringToAccountIdStub = sinon_1.default.stub(utilsConversionModule, 'stringToAccountId');
    });
    beforeEach(() => {
        mockContext = mocks_1.dsMockUtils.getContextInstance();
        subsidy = mocks_1.entityMockUtils.getSubsidyInstance();
        subsidizer = mocks_1.entityMockUtils.getAccountInstance({ address: 'subsidizer' });
        beneficiary = mocks_1.entityMockUtils.getAccountInstance({ address: 'beneficiary' });
        args = { subsidy };
    });
    afterEach(() => {
        mocks_1.entityMockUtils.reset();
        mocks_1.procedureMockUtils.reset();
        mocks_1.dsMockUtils.reset();
    });
    afterAll(() => {
        mocks_1.procedureMockUtils.cleanup();
        mocks_1.dsMockUtils.cleanup();
    });
    test('should throw an error if the Subsidy does not exist', () => __awaiter(void 0, void 0, void 0, function* () {
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        let error;
        try {
            yield quitSubsidy_1.prepareQuitSubsidy.call(proc, {
                subsidy: mocks_1.entityMockUtils.getSubsidyInstance({ exists: false }),
            });
        }
        catch (err) {
            error = err;
        }
        expect(error.message).toBe('The Subsidy no longer exists');
    }));
    test('should add a transaction to the queue', () => __awaiter(void 0, void 0, void 0, function* () {
        const addTransactionStub = mocks_1.procedureMockUtils.getAddTransactionStub();
        const removePayingKeyTransaction = mocks_1.dsMockUtils.createTxStub('relayer', 'removePayingKey');
        const rawBeneficiaryAccountId = mocks_1.dsMockUtils.createMockAccountId('beneficiary');
        const rawSubsidizerAccountId = mocks_1.dsMockUtils.createMockAccountId('subsidizer');
        stringToAccountIdStub.withArgs('beneficiary', mockContext).returns(rawBeneficiaryAccountId);
        stringToAccountIdStub.withArgs('subsidizer', mockContext).returns(rawSubsidizerAccountId);
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        yield quitSubsidy_1.prepareQuitSubsidy.call(proc, args);
        sinon_1.default.assert.calledWith(addTransactionStub, sinon_1.default.match({
            transaction: removePayingKeyTransaction,
            args: [rawBeneficiaryAccountId, rawSubsidizerAccountId],
        }));
    }));
    describe('getAuthorization', () => {
        test('should return the appropriate roles and permissions', () => __awaiter(void 0, void 0, void 0, function* () {
            const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
            const boundFunc = quitSubsidy_1.getAuthorization.bind(proc);
            let result = yield boundFunc(args);
            expect(result).toEqual({
                roles: 'Only the subsidizer or the beneficiary are allowed to quit a Subsidy',
                permissions: {
                    transactions: [polkadot_1.TxTags.relayer.RemovePayingKey],
                },
            });
            mockContext.getCurrentAccount.onSecondCall().returns(subsidizer);
            result = yield boundFunc(args);
            expect(result).toEqual({
                roles: true,
                permissions: {
                    transactions: [polkadot_1.TxTags.relayer.RemovePayingKey],
                },
            });
            mockContext.getCurrentAccount.onThirdCall().returns(beneficiary);
            result = yield boundFunc(args);
            expect(result).toEqual({
                roles: true,
                permissions: {
                    transactions: [polkadot_1.TxTags.relayer.RemovePayingKey],
                },
            });
        }));
    });
});
//# sourceMappingURL=quitSubsidy.js.map