"use strict";
// SPDX-License-Identifier: Apache-2.0
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 DataFixture_1 = require("../../../../__tests__/fixtures/shared/DataFixture");
const LogService_1 = __importDefault(require("../../../app/service/log/LogService"));
const ExternalPausesManagement_1 = __importDefault(require("./ExternalPausesManagement"));
const request_1 = require("../request");
const ExternalPausesFixture_1 = require("../../../../__tests__/fixtures/externalPauses/ExternalPausesFixture");
const UpdateExternalPausesCommand_1 = require("../../../app/usecase/command/security/externalPauses/updateExternalPauses/UpdateExternalPausesCommand");
const AddExternalPauseCommand_1 = require("../../../app/usecase/command/security/externalPauses/addExternalPause/AddExternalPauseCommand");
const RemoveExternalPauseCommand_1 = require("../../../app/usecase/command/security/externalPauses/removeExternalPause/RemoveExternalPauseCommand");
const IsExternalPauseQuery_1 = require("../../../app/usecase/query/security/externalPauses/isExternalPause/IsExternalPauseQuery");
const GetExternalPausesCountQuery_1 = require("../../../app/usecase/query/security/externalPauses/getExternalPausesCount/GetExternalPausesCountQuery");
const GetExternalPausesMembersQuery_1 = require("../../../app/usecase/query/security/externalPauses/getExternalPausesMembers/GetExternalPausesMembersQuery");
const SetPausedMockCommand_1 = require("../../../app/usecase/command/security/externalPauses/mock/setPaused/SetPausedMockCommand");
const CreateExternalPauseMockCommand_1 = require("../../../app/usecase/command/security/externalPauses/mock/createExternalPauseMock/CreateExternalPauseMockCommand");
const IsPausedMockQuery_1 = require("../../../app/usecase/query/security/externalPauses/mock/isPaused/IsPausedMockQuery");
const ValidatedArgs_1 = __importDefault(require("../../../core/validation/ValidatedArgs"));
const ValidationError_1 = require("../../../core/validation/ValidationError");
describe("ExternalPausesManagement", () => {
    let commandBusMock;
    let queryBusMock;
    let updateExternalPausesRequest;
    let addExternalPauseRequest;
    let removeExternalPauseRequest;
    let isExternalPauseRequest;
    let getExternalPausesCountRequest;
    let getExternalPausesMembersRequest;
    let setPausedMockRequest;
    let isPausedMockRequest;
    let handleValidationSpy;
    const transactionId = DataFixture_1.TransactionIdFixture.create().id;
    const hederaId = DataFixture_1.HederaIdPropsFixture.create().value;
    const expectedResponse = {
        payload: true,
        transactionId: transactionId,
    };
    beforeEach(() => {
        commandBusMock = (0, ts_jest_1.createMock)();
        queryBusMock = (0, ts_jest_1.createMock)();
        handleValidationSpy = jest.spyOn(ValidatedArgs_1.default, "handleValidation");
        jest.spyOn(LogService_1.default, "logError").mockImplementation(() => { });
        ExternalPausesManagement_1.default.commandBus = commandBusMock;
        ExternalPausesManagement_1.default.queryBus = queryBusMock;
    });
    afterEach(() => {
        jest.resetAllMocks();
        jest.restoreAllMocks();
    });
    describe("updateExternalPauses", () => {
        updateExternalPausesRequest = new request_1.UpdateExternalPausesRequest(ExternalPausesFixture_1.UpdateExternalPausesRequestFixture.create());
        it("should update external pause successfully", async () => {
            commandBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await ExternalPausesManagement_1.default.updateExternalPauses(updateExternalPausesRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("UpdateExternalPausesRequest", updateExternalPausesRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new UpdateExternalPausesCommand_1.UpdateExternalPausesCommand(updateExternalPausesRequest.securityId, updateExternalPausesRequest.externalPausesAddresses, updateExternalPausesRequest.actives));
            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(ExternalPausesManagement_1.default.updateExternalPauses(updateExternalPausesRequest)).rejects.toThrow("Command execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("UpdateExternalPausesRequest", updateExternalPausesRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new UpdateExternalPausesCommand_1.UpdateExternalPausesCommand(updateExternalPausesRequest.securityId, updateExternalPausesRequest.externalPausesAddresses, updateExternalPausesRequest.actives));
        });
        it("should throw error if securityId is invalid", async () => {
            updateExternalPausesRequest = new request_1.UpdateExternalPausesRequest({
                ...ExternalPausesFixture_1.UpdateExternalPausesRequestFixture.create({
                    securityId: "invalid",
                }),
            });
            await expect(ExternalPausesManagement_1.default.updateExternalPauses(updateExternalPausesRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if externalPausesAddresses array is invalid", async () => {
            updateExternalPausesRequest = new request_1.UpdateExternalPausesRequest({
                ...ExternalPausesFixture_1.UpdateExternalPausesRequestFixture.create({
                    externalPausesAddresses: ["invalid"],
                }),
            });
            await expect(ExternalPausesManagement_1.default.updateExternalPauses(updateExternalPausesRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if externalPausesAddresses and active array are different", async () => {
            updateExternalPausesRequest = new request_1.UpdateExternalPausesRequest({
                ...ExternalPausesFixture_1.UpdateExternalPausesRequestFixture.create({
                    actives: [true, false],
                }),
            });
            await expect(ExternalPausesManagement_1.default.updateExternalPauses(updateExternalPausesRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
    describe("addExternalPause", () => {
        addExternalPauseRequest = new request_1.AddExternalPauseRequest(ExternalPausesFixture_1.AddExternalPauseRequestFixture.create());
        it("should add external pause successfully", async () => {
            commandBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await ExternalPausesManagement_1.default.addExternalPause(addExternalPauseRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("AddExternalPauseRequest", addExternalPauseRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new AddExternalPauseCommand_1.AddExternalPauseCommand(addExternalPauseRequest.securityId, addExternalPauseRequest.externalPauseAddress));
            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(ExternalPausesManagement_1.default.addExternalPause(addExternalPauseRequest)).rejects.toThrow("Command execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("AddExternalPauseRequest", addExternalPauseRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new AddExternalPauseCommand_1.AddExternalPauseCommand(addExternalPauseRequest.securityId, addExternalPauseRequest.externalPauseAddress));
        });
        it("should throw error if securityId is invalid", async () => {
            addExternalPauseRequest = new request_1.AddExternalPauseRequest({
                ...ExternalPausesFixture_1.AddExternalPauseRequestFixture.create({
                    securityId: "invalid",
                }),
            });
            await expect(ExternalPausesManagement_1.default.addExternalPause(addExternalPauseRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if externalPauseAddress is invalid", async () => {
            addExternalPauseRequest = new request_1.AddExternalPauseRequest({
                ...ExternalPausesFixture_1.AddExternalPauseRequestFixture.create({
                    externalPauseAddress: "invalid",
                }),
            });
            await expect(ExternalPausesManagement_1.default.addExternalPause(addExternalPauseRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
    describe("removeExternalPause", () => {
        removeExternalPauseRequest = new request_1.RemoveExternalPauseRequest(ExternalPausesFixture_1.RemoveExternalPauseRequestFixture.create());
        it("should remove external pause successfully", async () => {
            commandBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await ExternalPausesManagement_1.default.removeExternalPause(removeExternalPauseRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("RemoveExternalPauseRequest", removeExternalPauseRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new RemoveExternalPauseCommand_1.RemoveExternalPauseCommand(removeExternalPauseRequest.securityId, removeExternalPauseRequest.externalPauseAddress));
            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(ExternalPausesManagement_1.default.removeExternalPause(removeExternalPauseRequest)).rejects.toThrow("Command execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("RemoveExternalPauseRequest", removeExternalPauseRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new RemoveExternalPauseCommand_1.RemoveExternalPauseCommand(removeExternalPauseRequest.securityId, removeExternalPauseRequest.externalPauseAddress));
        });
        it("should throw error if securityId is invalid", async () => {
            removeExternalPauseRequest = new request_1.RemoveExternalPauseRequest({
                ...ExternalPausesFixture_1.RemoveExternalPauseRequestFixture.create({
                    securityId: "invalid",
                }),
            });
            await expect(ExternalPausesManagement_1.default.removeExternalPause(removeExternalPauseRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if externalPauseAddress is invalid", async () => {
            removeExternalPauseRequest = new request_1.RemoveExternalPauseRequest({
                ...ExternalPausesFixture_1.RemoveExternalPauseRequestFixture.create({
                    externalPauseAddress: "invalid",
                }),
            });
            await expect(ExternalPausesManagement_1.default.removeExternalPause(removeExternalPauseRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
    describe("isExternalPause", () => {
        isExternalPauseRequest = new request_1.IsExternalPauseRequest(ExternalPausesFixture_1.IsExternalPauseQueryFixture.create());
        const expectedQueryResponse = {
            payload: true,
        };
        it("should check if is external pause successfully", async () => {
            queryBusMock.execute.mockResolvedValue(expectedQueryResponse);
            const result = await ExternalPausesManagement_1.default.isExternalPause(isExternalPauseRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("IsExternalPauseRequest", isExternalPauseRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new IsExternalPauseQuery_1.IsExternalPauseQuery(isExternalPauseRequest.securityId, isExternalPauseRequest.externalPauseAddress));
            expect(result).toEqual(expectedQueryResponse.payload);
        });
        it("should throw an error if query execution fails", async () => {
            const error = new Error("Query execution failed");
            queryBusMock.execute.mockRejectedValue(error);
            await expect(ExternalPausesManagement_1.default.isExternalPause(isExternalPauseRequest)).rejects.toThrow("Query execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("IsExternalPauseRequest", isExternalPauseRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new IsExternalPauseQuery_1.IsExternalPauseQuery(isExternalPauseRequest.securityId, isExternalPauseRequest.externalPauseAddress));
        });
        it("should throw error if securityId is invalid", async () => {
            isExternalPauseRequest = new request_1.IsExternalPauseRequest({
                ...ExternalPausesFixture_1.IsExternalPauseQueryFixture.create({
                    securityId: "invalid",
                }),
            });
            await expect(ExternalPausesManagement_1.default.isExternalPause(isExternalPauseRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if externalPauseAddress is invalid", async () => {
            isExternalPauseRequest = new request_1.IsExternalPauseRequest({
                ...ExternalPausesFixture_1.IsExternalPauseQueryFixture.create({
                    externalPauseAddress: "invalid",
                }),
            });
            await expect(ExternalPausesManagement_1.default.isExternalPause(isExternalPauseRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
    describe("getExternalPausesCount", () => {
        getExternalPausesCountRequest = new request_1.GetExternalPausesCountRequest(ExternalPausesFixture_1.GetExternalPausesCountQueryFixture.create());
        const expectedQueryResponse = {
            payload: 1,
        };
        it("should get external pauses count successfully", async () => {
            queryBusMock.execute.mockResolvedValue(expectedQueryResponse);
            const result = await ExternalPausesManagement_1.default.getExternalPausesCount(getExternalPausesCountRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("GetExternalPausesCountRequest", getExternalPausesCountRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetExternalPausesCountQuery_1.GetExternalPausesCountQuery(getExternalPausesCountRequest.securityId));
            expect(result).toEqual(expectedQueryResponse.payload);
        });
        it("should throw an error if query execution fails", async () => {
            const error = new Error("Query execution failed");
            queryBusMock.execute.mockRejectedValue(error);
            await expect(ExternalPausesManagement_1.default.getExternalPausesCount(getExternalPausesCountRequest)).rejects.toThrow("Query execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("GetExternalPausesCountRequest", getExternalPausesCountRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetExternalPausesCountQuery_1.GetExternalPausesCountQuery(getExternalPausesCountRequest.securityId));
        });
        it("should throw error if securityId is invalid", async () => {
            getExternalPausesCountRequest = new request_1.GetExternalPausesCountRequest({
                ...ExternalPausesFixture_1.GetExternalPausesCountQueryFixture.create({
                    securityId: "invalid",
                }),
            });
            await expect(ExternalPausesManagement_1.default.getExternalPausesCount(getExternalPausesCountRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
    describe("getExternalPausesMembers", () => {
        getExternalPausesMembersRequest = new request_1.GetExternalPausesMembersRequest(ExternalPausesFixture_1.GetExternalPausesMembersQueryFixture.create());
        const expectedQueryResponse = {
            payload: [DataFixture_1.HederaIdPropsFixture.create().value],
        };
        it("should get external pauses members successfully", async () => {
            queryBusMock.execute.mockResolvedValue(expectedQueryResponse);
            const result = await ExternalPausesManagement_1.default.getExternalPausesMembers(getExternalPausesMembersRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("GetExternalPausesMembersRequest", getExternalPausesMembersRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetExternalPausesMembersQuery_1.GetExternalPausesMembersQuery(getExternalPausesMembersRequest.securityId, getExternalPausesMembersRequest.start, getExternalPausesMembersRequest.end));
            expect(result).toEqual(expectedQueryResponse.payload);
        });
        it("should throw an error if query execution fails", async () => {
            const error = new Error("Query execution failed");
            queryBusMock.execute.mockRejectedValue(error);
            await expect(ExternalPausesManagement_1.default.getExternalPausesMembers(getExternalPausesMembersRequest)).rejects.toThrow("Query execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("GetExternalPausesMembersRequest", getExternalPausesMembersRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetExternalPausesMembersQuery_1.GetExternalPausesMembersQuery(getExternalPausesMembersRequest.securityId, getExternalPausesMembersRequest.start, getExternalPausesMembersRequest.end));
        });
        it("should throw error if securityId is invalid", async () => {
            getExternalPausesMembersRequest = new request_1.GetExternalPausesMembersRequest({
                ...ExternalPausesFixture_1.GetExternalPausesMembersQueryFixture.create({
                    securityId: "invalid",
                }),
            });
            await expect(ExternalPausesManagement_1.default.getExternalPausesMembers(getExternalPausesMembersRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if start is negative", async () => {
            getExternalPausesMembersRequest = new request_1.GetExternalPausesMembersRequest({
                ...ExternalPausesFixture_1.GetExternalPausesMembersQueryFixture.create({
                    start: -1,
                }),
            });
            await expect(ExternalPausesManagement_1.default.getExternalPausesMembers(getExternalPausesMembersRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if end is negative", async () => {
            getExternalPausesMembersRequest = new request_1.GetExternalPausesMembersRequest({
                ...ExternalPausesFixture_1.GetExternalPausesMembersQueryFixture.create({
                    end: -1,
                }),
            });
            await expect(ExternalPausesManagement_1.default.getExternalPausesMembers(getExternalPausesMembersRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
    describe("setPausedMock", () => {
        setPausedMockRequest = new request_1.SetPausedMockRequest(ExternalPausesFixture_1.SetPausedMockCommandFixture.create());
        it("should add to external pause successfully", async () => {
            commandBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await ExternalPausesManagement_1.default.setPausedMock(setPausedMockRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("SetPausedMockRequest", setPausedMockRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new SetPausedMockCommand_1.SetPausedMockCommand(setPausedMockRequest.contractId, setPausedMockRequest.paused));
            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(ExternalPausesManagement_1.default.setPausedMock(setPausedMockRequest)).rejects.toThrow("Command execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("SetPausedMockRequest", setPausedMockRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new SetPausedMockCommand_1.SetPausedMockCommand(setPausedMockRequest.contractId, setPausedMockRequest.paused));
        });
        it("should throw error if securityId is invalid", async () => {
            setPausedMockRequest = new request_1.SetPausedMockRequest({
                ...ExternalPausesFixture_1.SetPausedMockCommandFixture.create({
                    contractId: "invalid",
                }),
            });
            await expect(ExternalPausesManagement_1.default.setPausedMock(setPausedMockRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
    describe("createMock", () => {
        const expectedCommandResponse = {
            payload: hederaId,
        };
        it("should create external pause mock successfully", async () => {
            commandBusMock.execute.mockResolvedValue(expectedCommandResponse);
            const result = await ExternalPausesManagement_1.default.createMock();
            expect(commandBusMock.execute).toHaveBeenCalledWith(new CreateExternalPauseMockCommand_1.CreateExternalPauseMockCommand());
            expect(result).toEqual(expectedCommandResponse.payload);
        });
        it("should throw an error if command execution fails", async () => {
            const error = new Error("Command execution failed");
            commandBusMock.execute.mockRejectedValue(error);
            await expect(ExternalPausesManagement_1.default.createMock()).rejects.toThrow("Command execution failed");
            expect(commandBusMock.execute).toHaveBeenCalledWith(new CreateExternalPauseMockCommand_1.CreateExternalPauseMockCommand());
        });
    });
    describe("isPausedMock", () => {
        isPausedMockRequest = new request_1.IsPausedMockRequest(ExternalPausesFixture_1.IsPausedMockRequestFixture.create());
        const expectedQueryResponse = {
            payload: true,
        };
        it("should check if address is in external pause successfully", async () => {
            queryBusMock.execute.mockResolvedValue(expectedQueryResponse);
            const result = await ExternalPausesManagement_1.default.isPausedMock(isPausedMockRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("IsPausedMockRequest", isPausedMockRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new IsPausedMockQuery_1.IsPausedMockQuery(isPausedMockRequest.contractId));
            expect(result).toEqual(expectedQueryResponse.payload);
        });
        it("should throw an error if query execution fails", async () => {
            const error = new Error("Query execution failed");
            queryBusMock.execute.mockRejectedValue(error);
            await expect(ExternalPausesManagement_1.default.isPausedMock(isPausedMockRequest)).rejects.toThrow("Query execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("IsPausedMockRequest", isPausedMockRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new IsPausedMockQuery_1.IsPausedMockQuery(isPausedMockRequest.contractId));
        });
        it("should throw error if contractId is invalid", async () => {
            isPausedMockRequest = new request_1.IsPausedMockRequest({
                ...ExternalPausesFixture_1.IsPausedMockRequestFixture.create({
                    contractId: "invalid",
                }),
            });
            await expect(ExternalPausesManagement_1.default.isPausedMock(isPausedMockRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
});
