"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 bignumber_js_1 = __importDefault(require("bignumber.js"));
const types_1 = require("../../../polkadot/types");
const sinon_1 = __importDefault(require("sinon"));
const quitCustody_1 = require("../quitCustody");
const procedureUtilsModule = __importStar(require("../utils"));
const internal_1 = require("../../../internal");
const mocks_1 = require("../../../testUtils/mocks");
const types_2 = require("../../../types");
const utilsConversionModule = __importStar(require("../../../utils/conversion"));
jest.mock('~/api/entities/NumberedPortfolio', require("../../../testUtils/mocks/entities").mockNumberedPortfolioModule('~/api/entities/NumberedPortfolio'));
describe('quitCustody procedure', () => {
    const id = new bignumber_js_1.default(1);
    const did = 'someDid';
    let mockContext;
    let portfolioIdToMeshPortfolioIdStub;
    let portfolioLikeToPortfolioIdStub;
    let assertPortfolioExistsStub;
    beforeAll(() => {
        mocks_1.dsMockUtils.initMocks();
        mocks_1.procedureMockUtils.initMocks();
        mocks_1.entityMockUtils.initMocks();
        portfolioIdToMeshPortfolioIdStub = sinon_1.default.stub(utilsConversionModule, 'portfolioIdToMeshPortfolioId');
        portfolioLikeToPortfolioIdStub = sinon_1.default.stub(utilsConversionModule, 'portfolioLikeToPortfolioId');
        assertPortfolioExistsStub = sinon_1.default.stub(procedureUtilsModule, 'assertPortfolioExists');
    });
    beforeEach(() => {
        mockContext = mocks_1.dsMockUtils.getContextInstance();
        mocks_1.entityMockUtils.configureMocks({
            numberedPortfolioOptions: {
                isOwnedBy: true,
            },
        });
        assertPortfolioExistsStub.returns(true);
    });
    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 current Identity is the Portfolio owner', () => __awaiter(void 0, void 0, void 0, function* () {
        const portfolio = new internal_1.NumberedPortfolio({ id, did }, mockContext);
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext, {
            portfolioId: { did, number: id },
        });
        let error;
        try {
            yield quitCustody_1.prepareQuitCustody.call(proc, {
                portfolio,
            });
        }
        catch (err) {
            error = err;
        }
        expect(error.message).toBe('The Portfolio owner cannot quit custody');
    }));
    test('should add a quit portfolio custody transaction to the queue', () => __awaiter(void 0, void 0, void 0, function* () {
        const portfolio = mocks_1.entityMockUtils.getNumberedPortfolioInstance({
            id,
            did,
            isOwnedBy: false,
        });
        const rawMeshPortfolioId = mocks_1.dsMockUtils.createMockPortfolioId({
            did: mocks_1.dsMockUtils.createMockIdentityId(did),
            kind: mocks_1.dsMockUtils.createMockPortfolioKind({
                User: mocks_1.dsMockUtils.createMockU64(id),
            }),
        });
        const portfolioId = { did, number: id };
        portfolioIdToMeshPortfolioIdStub.withArgs(portfolioId, mockContext).returns(rawMeshPortfolioId);
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext, {
            portfolioId,
        });
        const transaction = mocks_1.dsMockUtils.createTxStub('portfolio', 'quitPortfolioCustody');
        yield quitCustody_1.prepareQuitCustody.call(proc, {
            portfolio,
        });
        const addTransactionStub = mocks_1.procedureMockUtils.getAddTransactionStub();
        sinon_1.default.assert.calledWith(addTransactionStub, { transaction, args: [rawMeshPortfolioId] });
    }));
    describe('getAuthorization', () => {
        test('should return the appropriate roles and permissions', () => {
            let portfolioId = { did, number: id };
            let proc = mocks_1.procedureMockUtils.getInstance(mockContext, {
                portfolioId,
            });
            let boundFunc = quitCustody_1.getAuthorization.bind(proc);
            let portfolio = mocks_1.entityMockUtils.getNumberedPortfolioInstance({ id, did });
            let args = {
                portfolio,
            };
            expect(boundFunc(args)).toEqual({
                roles: [{ type: types_2.RoleType.PortfolioCustodian, portfolioId }],
                permissions: {
                    transactions: [types_1.TxTags.portfolio.QuitPortfolioCustody],
                    portfolios: [expect.objectContaining({ owner: expect.objectContaining({ did }), id })],
                    assets: [],
                },
            });
            portfolioId = { did };
            proc = mocks_1.procedureMockUtils.getInstance(mockContext, {
                portfolioId,
            });
            boundFunc = quitCustody_1.getAuthorization.bind(proc);
            portfolio = mocks_1.entityMockUtils.getDefaultPortfolioInstance(portfolioId);
            args = {
                portfolio,
            };
            expect(boundFunc(args)).toEqual({
                roles: [{ type: types_2.RoleType.PortfolioCustodian, portfolioId }],
                permissions: {
                    transactions: [types_1.TxTags.portfolio.QuitPortfolioCustody],
                    portfolios: [expect.objectContaining({ owner: expect.objectContaining({ did }) })],
                    assets: [],
                },
            });
        });
    });
    describe('prepareStorage', () => {
        test('should return the portfolio id', () => __awaiter(void 0, void 0, void 0, function* () {
            const portfolio = new internal_1.NumberedPortfolio({ id, did }, mockContext);
            const portfolioId = { did, number: id };
            const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
            const boundFunc = quitCustody_1.prepareStorage.bind(proc);
            portfolioLikeToPortfolioIdStub.withArgs(portfolio).returns(portfolioId);
            const result = yield boundFunc({ portfolio });
            expect(result).toEqual({
                portfolioId,
            });
        }));
    });
});
//# sourceMappingURL=quitCustody.js.map