"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 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 Security_1 = __importDefault(require("../../../in/security/Security"));
const PauseFixture_1 = require("../../../../../__tests__/fixtures/pause/PauseFixture");
const PauseCommand_1 = require("../../../../app/usecase/command/security/operations/pause/PauseCommand");
const IsPausedQuery_1 = require("../../../../app/usecase/query/security/isPaused/IsPausedQuery");
describe("Pause", () => {
    let commandBusMock;
    let queryBusMock;
    let mirrorNodeMock;
    let handleValidationSpy;
    const transactionId = DataFixture_1.TransactionIdFixture.create().id;
    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("pause", () => {
        const pauseRequest = new request_1.PauseRequest(PauseFixture_1.PauseRequestFixture.create());
        const expectedResponse = {
            payload: true,
            transactionId: transactionId,
        };
        it("should pause successfully", async () => {
            commandBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await Security_1.default.pause(pauseRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("PauseRequest", pauseRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new PauseCommand_1.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_1.default.pause(pauseRequest)).rejects.toThrow("Command execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("PauseRequest", pauseRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new PauseCommand_1.PauseCommand(pauseRequest.securityId));
        });
        it("should throw error if securityId is invalid", async () => {
            const pauseRequest = new request_1.PauseRequest({
                ...PauseFixture_1.PauseRequestFixture.create({
                    securityId: "invalid",
                }),
            });
            await expect(Security_1.default.pause(pauseRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
    describe("unpause", () => {
        const pauseRequest = new request_1.PauseRequest(PauseFixture_1.PauseRequestFixture.create());
        const expectedResponse = {
            payload: true,
            transactionId: transactionId,
        };
        it("should unpause successfully", async () => {
            commandBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await Security_1.default.unpause(pauseRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("PauseRequest", pauseRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new PauseCommand_1.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_1.default.unpause(pauseRequest)).rejects.toThrow("Command execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("PauseRequest", pauseRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new PauseCommand_1.PauseCommand(pauseRequest.securityId));
        });
        it("should throw error if securityId is invalid", async () => {
            const pauseRequest = new request_1.PauseRequest({
                ...PauseFixture_1.PauseRequestFixture.create({
                    securityId: "invalid",
                }),
            });
            await expect(Security_1.default.unpause(pauseRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
    describe("isPaused", () => {
        const pauseRequest = new request_1.PauseRequest(PauseFixture_1.PauseRequestFixture.create());
        const expectedResponse = {
            payload: true,
        };
        it("should isPaused successfully", async () => {
            queryBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await Security_1.default.isPaused(pauseRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("PauseRequest", pauseRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new IsPausedQuery_1.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_1.default.isPaused(pauseRequest)).rejects.toThrow("Query execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("PauseRequest", pauseRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new IsPausedQuery_1.IsPausedQuery(pauseRequest.securityId));
        });
        it("should throw error if securityId is invalid", async () => {
            const pauseRequest = new request_1.PauseRequest({
                ...PauseFixture_1.PauseRequestFixture.create({
                    securityId: "invalid",
                }),
            });
            await expect(Security_1.default.isPaused(pauseRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
});
