"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());
    });
};
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 lodash_1 = require("lodash");
const sinon_1 = __importDefault(require("sinon"));
const internal_1 = require("../../../../internal");
const mocks_1 = require("../../../../testUtils/mocks");
const types_1 = require("../../../../types");
const utils_1 = require("../../../../types/utils");
const Permissions_1 = require("../Permissions");
jest.mock('~/base/Procedure', require("../../../../testUtils/mocks/procedure").mockProcedureModule('~/base/Procedure'));
jest.mock('~/api/entities/CustomPermissionGroup', require("../../../../testUtils/mocks/entities").mockCustomPermissionGroupModule('~/api/entities/CustomPermissionGroup'));
jest.mock('~/api/entities/KnownPermissionGroup', require("../../../../testUtils/mocks/entities").mockKnownPermissionGroupModule('~/api/entities/KnownPermissionGroup'));
describe('Permissions class', () => {
    let ticker;
    let asset;
    let context;
    let target;
    let permissions;
    beforeAll(() => {
        mocks_1.entityMockUtils.initMocks();
        mocks_1.dsMockUtils.initMocks();
        mocks_1.procedureMockUtils.initMocks();
        ticker = 'SOME_ASSET';
        target = 'someDid';
    });
    beforeEach(() => {
        context = mocks_1.dsMockUtils.getContextInstance();
        asset = mocks_1.entityMockUtils.getAssetInstance({ ticker });
        permissions = new Permissions_1.Permissions(asset, context);
    });
    afterEach(() => {
        mocks_1.entityMockUtils.reset();
        mocks_1.dsMockUtils.reset();
        mocks_1.procedureMockUtils.reset();
    });
    afterAll(() => {
        mocks_1.entityMockUtils.cleanup();
        mocks_1.dsMockUtils.cleanup();
        mocks_1.procedureMockUtils.cleanup();
    });
    test('should extend namespace', () => {
        expect(Permissions_1.Permissions.prototype instanceof internal_1.Namespace).toBe(true);
    });
    describe('method: createGroup', () => {
        afterAll(() => {
            sinon_1.default.restore();
        });
        test('should prepare the procedure with the correct arguments and context, and return the resulting transaction queue', () => __awaiter(void 0, void 0, void 0, function* () {
            const args = {
                ticker: asset.ticker,
                permissions: { transactions: {} },
            };
            const expectedQueue = 'someQueue';
            mocks_1.procedureMockUtils
                .getPrepareStub()
                .withArgs({ args, transformer: undefined }, context)
                .resolves(expectedQueue);
            const queue = yield permissions.createGroup(args);
            expect(queue).toBe(expectedQueue);
        }));
    });
    describe('method: inviteAgent', () => {
        afterAll(() => {
            sinon_1.default.restore();
        });
        test('should prepare the procedure with the correct arguments and context, and return the resulting transaction queue', () => __awaiter(void 0, void 0, void 0, function* () {
            const args = {
                ticker: asset.ticker,
                target,
                permissions: { transactions: {} },
            };
            const expectedQueue = 'someQueue';
            mocks_1.procedureMockUtils
                .getPrepareStub()
                .withArgs({ args, transformer: undefined }, context)
                .resolves(expectedQueue);
            const queue = yield permissions.inviteAgent(args);
            expect(queue).toBe(expectedQueue);
        }));
    });
    describe('method: removeAgent', () => {
        afterAll(() => {
            sinon_1.default.restore();
        });
        test('should prepare the procedure with the correct arguments and context, and return the resulting transaction queue', () => __awaiter(void 0, void 0, void 0, function* () {
            const args = {
                ticker: asset.ticker,
                target,
            };
            const expectedQueue = 'someQueue';
            mocks_1.procedureMockUtils
                .getPrepareStub()
                .withArgs({ args, transformer: undefined }, context)
                .resolves(expectedQueue);
            const queue = yield permissions.removeAgent(args);
            expect(queue).toBe(expectedQueue);
        }));
    });
    describe('method: getGroup', () => {
        afterAll(() => {
            sinon_1.default.restore();
        });
        test('should retrieve a specific Custom Permission Group', () => __awaiter(void 0, void 0, void 0, function* () {
            mocks_1.entityMockUtils.configureMocks({
                customPermissionGroupOptions: {
                    ticker,
                },
            });
            const id = new bignumber_js_1.default(1);
            const result = yield permissions.getGroup({ id });
            expect(result).toEqual(mocks_1.entityMockUtils.getCustomPermissionGroupInstance({ id, ticker }));
        }));
        test('should throw an error if the Custom Permission Group does not exist', () => {
            const id = new bignumber_js_1.default(1);
            mocks_1.entityMockUtils.configureMocks({ customPermissionGroupOptions: { exists: false } });
            return expect(permissions.getGroup({ id })).rejects.toThrow('The Permission Group does not exist');
        });
        test('should retrieve a specific Known Permission Group', () => __awaiter(void 0, void 0, void 0, function* () {
            mocks_1.entityMockUtils.configureMocks({
                knownPermissionGroupOptions: {
                    ticker,
                },
            });
            const type = types_1.PermissionGroupType.Full;
            const result = yield permissions.getGroup({ type });
            expect(result).toEqual(mocks_1.entityMockUtils.getKnownPermissionGroupInstance({ type, ticker }));
        }));
    });
    describe('method: getGroups', () => {
        afterAll(() => {
            sinon_1.default.restore();
        });
        test('should retrieve all the permission groups of the Asset', () => __awaiter(void 0, void 0, void 0, function* () {
            const id = new bignumber_js_1.default(1);
            mocks_1.dsMockUtils.createQueryStub('externalAgents', 'groupPermissions', {
                entries: [
                    utils_1.tuple([mocks_1.dsMockUtils.createMockTicker(ticker), mocks_1.dsMockUtils.createMockU32(id.toNumber())], mocks_1.dsMockUtils.createMockOption(mocks_1.dsMockUtils.createMockExtrinsicPermissions())),
                ],
            });
            const { known, custom } = yield permissions.getGroups();
            expect(known.length).toEqual(4);
            expect(custom.length).toEqual(1);
            known.forEach(group => {
                expect(group instanceof internal_1.KnownPermissionGroup).toBe(true);
            });
            expect(custom[0] instanceof internal_1.CustomPermissionGroup).toBe(true);
        }));
    });
    describe('method: getAgents', () => {
        test('should retrieve a list of agent identities', () => __awaiter(void 0, void 0, void 0, function* () {
            const did = 'someDid';
            const otherDid = 'otherDid';
            const customId = new bignumber_js_1.default(1);
            mocks_1.dsMockUtils.createQueryStub('externalAgents', 'groupOfAgent', {
                entries: [
                    utils_1.tuple([mocks_1.dsMockUtils.createMockTicker(ticker), mocks_1.dsMockUtils.createMockIdentityId(did)], mocks_1.dsMockUtils.createMockOption(mocks_1.dsMockUtils.createMockAgentGroup('ExceptMeta'))),
                    utils_1.tuple([mocks_1.dsMockUtils.createMockTicker(ticker), mocks_1.dsMockUtils.createMockIdentityId(otherDid)], mocks_1.dsMockUtils.createMockOption(mocks_1.dsMockUtils.createMockAgentGroup('Full'))),
                    utils_1.tuple([mocks_1.dsMockUtils.createMockTicker(ticker), mocks_1.dsMockUtils.createMockIdentityId(did)], mocks_1.dsMockUtils.createMockOption(mocks_1.dsMockUtils.createMockAgentGroup('PolymeshV1Caa'))),
                    utils_1.tuple([mocks_1.dsMockUtils.createMockTicker(ticker), mocks_1.dsMockUtils.createMockIdentityId(otherDid)], mocks_1.dsMockUtils.createMockOption(mocks_1.dsMockUtils.createMockAgentGroup('PolymeshV1Pia'))),
                    utils_1.tuple([mocks_1.dsMockUtils.createMockTicker(ticker), mocks_1.dsMockUtils.createMockIdentityId(otherDid)], mocks_1.dsMockUtils.createMockOption(mocks_1.dsMockUtils.createMockAgentGroup({
                        Custom: mocks_1.dsMockUtils.createMockU32(customId.toNumber()),
                    }))),
                ],
            });
            const result = yield permissions.getAgents();
            expect(result.length).toEqual(5);
            for (const i in lodash_1.range(4)) {
                expect(result[i].group instanceof internal_1.KnownPermissionGroup).toEqual(true);
            }
            expect(result[4].group instanceof internal_1.CustomPermissionGroup).toEqual(true);
        }));
    });
});
//# sourceMappingURL=Permissions.js.map