// SPDX-License-Identifier: Apache-2.0
import { createMock } from "@golevelup/ts-jest";
import { PauseRequest } from "../../request";
import { TransactionIdFixture } from "../../../../../__tests__/fixtures/shared/DataFixture";
import LogService from "../../../../app/service/log/LogService";
import ValidatedRequest from "../../../../core/validation/ValidatedArgs";
import { ValidationError } from "../../../../core/validation/ValidationError";
import Security from "../../../in/security/Security";
import { PauseRequestFixture } from "../../../../../__tests__/fixtures/pause/PauseFixture";
import { PauseCommand } from "../../../../app/usecase/command/security/operations/pause/PauseCommand";
import { IsPausedQuery } from "../../../../app/usecase/query/security/isPaused/IsPausedQuery";
describe("Pause", () => {
    let commandBusMock;
    let queryBusMock;
    let mirrorNodeMock;
    let handleValidationSpy;
    const transactionId = TransactionIdFixture.create().id;
    beforeEach(() => {
        commandBusMock = createMock();
        queryBusMock = createMock();
        mirrorNodeMock = createMock();
        handleValidationSpy = jest.spyOn(ValidatedRequest, "handleValidation");
        jest.spyOn(LogService, "logError").mockImplementation(() => { });
        Security.commandBus = commandBusMock;
        Security.queryBus = queryBusMock;
        Security.mirrorNode = mirrorNodeMock;
    });
    afterEach(() => {
        jest.resetAllMocks();
        jest.restoreAllMocks();
    });
    describe("pause", () => {
        const pauseRequest = new PauseRequest(PauseRequestFixture.create());
        const expectedResponse = {
            payload: true,
            transactionId: transactionId,
        };
        it("should pause successfully", async () => {
            commandBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await Security.pause(pauseRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("PauseRequest", pauseRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new PauseCommand(pauseRequest.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.pause(pauseRequest)).rejects.toThrow("Command execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("PauseRequest", pauseRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new PauseCommand(pauseRequest.securityId));
        });
        it("should throw error if securityId is invalid", async () => {
            const pauseRequest = new PauseRequest({
                ...PauseRequestFixture.create({
                    securityId: "invalid",
                }),
            });
            await expect(Security.pause(pauseRequest)).rejects.toThrow(ValidationError);
        });
    });
    describe("unpause", () => {
        const pauseRequest = new PauseRequest(PauseRequestFixture.create());
        const expectedResponse = {
            payload: true,
            transactionId: transactionId,
        };
        it("should unpause successfully", async () => {
            commandBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await Security.unpause(pauseRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("PauseRequest", pauseRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new PauseCommand(pauseRequest.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.unpause(pauseRequest)).rejects.toThrow("Command execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("PauseRequest", pauseRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new PauseCommand(pauseRequest.securityId));
        });
        it("should throw error if securityId is invalid", async () => {
            const pauseRequest = new PauseRequest({
                ...PauseRequestFixture.create({
                    securityId: "invalid",
                }),
            });
            await expect(Security.unpause(pauseRequest)).rejects.toThrow(ValidationError);
        });
    });
    describe("isPaused", () => {
        const pauseRequest = new PauseRequest(PauseRequestFixture.create());
        const expectedResponse = {
            payload: true,
        };
        it("should isPaused successfully", async () => {
            queryBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await Security.isPaused(pauseRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("PauseRequest", pauseRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new IsPausedQuery(pauseRequest.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.isPaused(pauseRequest)).rejects.toThrow("Query execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("PauseRequest", pauseRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new IsPausedQuery(pauseRequest.securityId));
        });
        it("should throw error if securityId is invalid", async () => {
            const pauseRequest = new PauseRequest({
                ...PauseRequestFixture.create({
                    securityId: "invalid",
                }),
            });
            await expect(Security.isPaused(pauseRequest)).rejects.toThrow(ValidationError);
        });
    });
});
