"use strict";
// SPDX-License-Identifier: Apache-2.0
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (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 () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const ts_jest_1 = require("@golevelup/ts-jest");
const request_1 = require("../../request");
const DataFixture_1 = require("../../../../../__tests__/fixtures/shared/DataFixture");
const LogService_1 = __importDefault(require("../../../../app/service/log/LogService"));
const ValidatedArgs_1 = __importDefault(require("../../../../core/validation/ValidatedArgs"));
const ValidationError_1 = require("../../../../core/validation/ValidationError");
const Account_1 = __importDefault(require("../../../../domain/context/account/Account"));
const AccountFixture_1 = require("../../../../../__tests__/fixtures/account/AccountFixture");
const Security_1 = __importStar(require("../../../in/security/Security"));
const ControlListFixture_1 = require("../../../../../__tests__/fixtures/controlList/ControlListFixture");
const AddToControlListCommand_1 = require("../../../../app/usecase/command/security/operations/addToControlList/AddToControlListCommand");
const RemoveFromControlListCommand_1 = require("../../../../app/usecase/command/security/operations/removeFromControlList/RemoveFromControlListCommand");
const IsInControlListQuery_1 = require("../../../../app/usecase/query/account/controlList/IsInControlListQuery");
const GetControlListCountQuery_1 = require("../../../../app/usecase/query/security/controlList/getControlListCount/GetControlListCountQuery");
const GetControlListMembersQuery_1 = require("../../../../app/usecase/query/security/controlList/getControlListMembers/GetControlListMembersQuery");
const GetControlListTypeQuery_1 = require("../../../../app/usecase/query/security/controlList/getControlListType/GetControlListTypeQuery");
describe("Control List", () => {
    let commandBusMock;
    let queryBusMock;
    let mirrorNodeMock;
    let getControlListCountRequest;
    let getControlListMembersRequest;
    let getControlListTypeRequest;
    let handleValidationSpy;
    const transactionId = DataFixture_1.TransactionIdFixture.create().id;
    const account = new Account_1.default(AccountFixture_1.AccountPropsFixture.create());
    beforeEach(() => {
        commandBusMock = (0, ts_jest_1.createMock)();
        queryBusMock = (0, ts_jest_1.createMock)();
        mirrorNodeMock = (0, ts_jest_1.createMock)();
        handleValidationSpy = jest.spyOn(ValidatedArgs_1.default, "handleValidation");
        jest.spyOn(LogService_1.default, "logError").mockImplementation(() => { });
        Security_1.default.commandBus = commandBusMock;
        Security_1.default.queryBus = queryBusMock;
        Security_1.default.mirrorNode = mirrorNodeMock;
    });
    afterEach(() => {
        jest.resetAllMocks();
        jest.restoreAllMocks();
    });
    describe("addToControlList", () => {
        const controlListRequest = new request_1.ControlListRequest(ControlListFixture_1.ControlListRequestFixture.create());
        const expectedResponse = {
            payload: true,
            transactionId: transactionId,
        };
        it("should add to control list successfully", async () => {
            commandBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await Security_1.default.addToControlList(controlListRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("ControlListRequest", controlListRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new AddToControlListCommand_1.AddToControlListCommand(controlListRequest.targetId, controlListRequest.securityId));
            expect(result).toEqual(expectedResponse);
        });
        it("should throw an error if command execution fails", async () => {
            const error = new Error("Command execution failed");
            commandBusMock.execute.mockRejectedValue(error);
            await expect(Security_1.default.addToControlList(controlListRequest)).rejects.toThrow("Command execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("ControlListRequest", controlListRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new AddToControlListCommand_1.AddToControlListCommand(controlListRequest.targetId, controlListRequest.securityId));
        });
        it("should throw error if securityId is invalid", async () => {
            const controlListRequest = new request_1.ControlListRequest({
                ...ControlListFixture_1.ControlListRequestFixture.create({
                    securityId: "invalid",
                }),
            });
            await expect(Security_1.default.addToControlList(controlListRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if targetId is invalid", async () => {
            const controlListRequest = new request_1.ControlListRequest({
                ...ControlListFixture_1.ControlListRequestFixture.create({
                    targetId: "invalid",
                }),
            });
            await expect(Security_1.default.addToControlList(controlListRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
    describe("removeFromControlList", () => {
        const controlListRequest = new request_1.ControlListRequest(ControlListFixture_1.ControlListRequestFixture.create());
        const expectedResponse = {
            payload: true,
            transactionId: transactionId,
        };
        it("should remove from control list successfully", async () => {
            commandBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await Security_1.default.removeFromControlList(controlListRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("ControlListRequest", controlListRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new RemoveFromControlListCommand_1.RemoveFromControlListCommand(controlListRequest.targetId, controlListRequest.securityId));
            expect(result).toEqual(expectedResponse);
        });
        it("should throw an error if command execution fails", async () => {
            const error = new Error("Command execution failed");
            commandBusMock.execute.mockRejectedValue(error);
            await expect(Security_1.default.removeFromControlList(controlListRequest)).rejects.toThrow("Command execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("ControlListRequest", controlListRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new RemoveFromControlListCommand_1.RemoveFromControlListCommand(controlListRequest.targetId, controlListRequest.securityId));
        });
        it("should throw error if securityId is invalid", async () => {
            const controlListRequest = new request_1.ControlListRequest({
                ...ControlListFixture_1.ControlListRequestFixture.create({
                    securityId: "invalid",
                }),
            });
            await expect(Security_1.default.removeFromControlList(controlListRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if targetId is invalid", async () => {
            const controlListRequest = new request_1.ControlListRequest({
                ...ControlListFixture_1.ControlListRequestFixture.create({
                    targetId: "invalid",
                }),
            });
            await expect(Security_1.default.removeFromControlList(controlListRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
    describe("isAccountInControlList", () => {
        const controlListRequest = new request_1.ControlListRequest(ControlListFixture_1.ControlListRequestFixture.create());
        const expectedResponse = {
            payload: true,
        };
        it("should get is account in control list successfully", async () => {
            queryBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await Security_1.default.isAccountInControlList(controlListRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("ControlListRequest", controlListRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new IsInControlListQuery_1.IsInControlListQuery(controlListRequest.securityId, controlListRequest.targetId));
            expect(result).toEqual(expectedResponse.payload);
        });
        it("should throw an error if query execution fails", async () => {
            const error = new Error("Query execution failed");
            queryBusMock.execute.mockRejectedValue(error);
            await expect(Security_1.default.isAccountInControlList(controlListRequest)).rejects.toThrow("Query execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("ControlListRequest", controlListRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new IsInControlListQuery_1.IsInControlListQuery(controlListRequest.securityId, controlListRequest.targetId));
        });
        it("should throw error if securityId is invalid", async () => {
            const controlListRequest = new request_1.ControlListRequest({
                ...ControlListFixture_1.ControlListRequestFixture.create({
                    securityId: "invalid",
                }),
            });
            await expect(Security_1.default.isAccountInControlList(controlListRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if targetId is invalid", async () => {
            const controlListRequest = new request_1.ControlListRequest({
                ...ControlListFixture_1.ControlListRequestFixture.create({
                    targetId: "invalid",
                }),
            });
            await expect(Security_1.default.isAccountInControlList(controlListRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
    describe("getControlListCount", () => {
        getControlListCountRequest = new request_1.GetControlListCountRequest(ControlListFixture_1.GetControlListCountRequestFixture.create());
        const expectedResponse = {
            payload: 1,
        };
        it("should get control list count successfully", async () => {
            queryBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await Security_1.default.getControlListCount(getControlListCountRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("GetControlListCountRequest", getControlListCountRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetControlListCountQuery_1.GetControlListCountQuery(getControlListCountRequest.securityId));
            expect(result).toEqual(expectedResponse.payload);
        });
        it("should throw an error if query execution fails", async () => {
            const error = new Error("Query execution failed");
            queryBusMock.execute.mockRejectedValue(error);
            await expect(Security_1.default.getControlListCount(getControlListCountRequest)).rejects.toThrow("Query execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("GetControlListCountRequest", getControlListCountRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetControlListCountQuery_1.GetControlListCountQuery(getControlListCountRequest.securityId));
        });
        it("should throw error if securityId is invalid", async () => {
            getControlListCountRequest = new request_1.GetControlListCountRequest({
                ...ControlListFixture_1.GetControlListCountRequestFixture.create({
                    securityId: "invalid",
                }),
            });
            await expect(Security_1.default.getControlListCount(getControlListCountRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
    describe("getControlListMembers", () => {
        getControlListMembersRequest = new request_1.GetControlListMembersRequest(ControlListFixture_1.GetControlListMembersRequestFixture.create());
        const expectedResponse = {
            payload: [account.id.toString()],
        };
        it("should get control list members successfully", async () => {
            queryBusMock.execute.mockResolvedValue(expectedResponse);
            mirrorNodeMock.getAccountInfo.mockResolvedValue(account);
            const result = await Security_1.default.getControlListMembers(getControlListMembersRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("GetControlListMembersRequest", getControlListMembersRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetControlListMembersQuery_1.GetControlListMembersQuery(getControlListMembersRequest.securityId, getControlListMembersRequest.start, getControlListMembersRequest.end));
            expect(mirrorNodeMock.getAccountInfo).toHaveBeenCalledWith(account.id.toString());
            expect(result).toEqual(expectedResponse.payload);
        });
        it("should throw an error if query execution fails", async () => {
            const error = new Error("Query execution failed");
            queryBusMock.execute.mockRejectedValue(error);
            await expect(Security_1.default.getControlListMembers(getControlListMembersRequest)).rejects.toThrow("Query execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("GetControlListMembersRequest", getControlListMembersRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetControlListMembersQuery_1.GetControlListMembersQuery(getControlListMembersRequest.securityId, getControlListMembersRequest.start, getControlListMembersRequest.end));
        });
        it("should throw error if securityId is invalid", async () => {
            getControlListMembersRequest = new request_1.GetControlListMembersRequest({
                ...ControlListFixture_1.GetControlListMembersRequestFixture.create({
                    securityId: "invalid",
                }),
            });
            await expect(Security_1.default.getControlListMembers(getControlListMembersRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
    describe("getControlListType", () => {
        getControlListTypeRequest = new request_1.GetControlListTypeRequest(ControlListFixture_1.GetControlListTypeRequestFixture.create());
        const expectedResponse = {
            payload: Security_1.SecurityControlListType.BLACKLIST,
        };
        it("should get control list type successfully", async () => {
            queryBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await Security_1.default.getControlListType(getControlListTypeRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("GetControlListTypeRequest", getControlListTypeRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetControlListTypeQuery_1.GetControlListTypeQuery(getControlListTypeRequest.securityId));
            expect(result).toEqual(expectedResponse.payload);
        });
        it("should throw an error if query execution fails", async () => {
            const error = new Error("Query execution failed");
            queryBusMock.execute.mockRejectedValue(error);
            await expect(Security_1.default.getControlListType(getControlListTypeRequest)).rejects.toThrow("Query execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("GetControlListTypeRequest", getControlListTypeRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetControlListTypeQuery_1.GetControlListTypeQuery(getControlListTypeRequest.securityId));
        });
        it("should throw error if securityId is invalid", async () => {
            getControlListTypeRequest = new request_1.GetControlListTypeRequest({
                ...ControlListFixture_1.GetControlListTypeRequestFixture.create({
                    securityId: "invalid",
                }),
            });
            await expect(Security_1.default.getControlListType(getControlListCountRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
});
