"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 EvmAddress_1 = __importDefault(require("../../../../../domain/context/contract/EvmAddress"));
const BigDecimal_1 = __importDefault(require("../../../../../domain/context/shared/BigDecimal"));
const BaseError_1 = require("../../../../../core/error/BaseError");
const SetInterestRateCommandHandler_1 = require("./SetInterestRateCommandHandler");
const SetInterestRateCommand_1 = require("./SetInterestRateCommand");
const SetInterestRateCommandError_1 = require("./error/SetInterestRateCommandError");
describe("SetInterestRateCommandHandler", () => {
    let handler;
    let command;
    const transactionServiceMock = (0, ts_jest_1.createMock)();
    const validationServiceMock = (0, ts_jest_1.createMock)();
    const contractServiceMock = (0, ts_jest_1.createMock)();
    const accountServiceMock = (0, ts_jest_1.createMock)();
    const handlerMock = (0, ts_jest_1.createMock)();
    const evmAddress = new EvmAddress_1.default(DataFixture_1.EvmAddressPropsFixture.create().value);
    const transactionId = DataFixture_1.TransactionIdFixture.create().id;
    const errorMsg = DataFixture_1.ErrorMsgFixture.create().msg;
    beforeEach(() => {
        handler = new SetInterestRateCommandHandler_1.SetInterestRateCommandHandler(transactionServiceMock, contractServiceMock, validationServiceMock, accountServiceMock);
        command = new SetInterestRateCommand_1.SetInterestRateCommand("0x1234567890123456789012345678901234567890", "10.5", "5.5", "1.5", "1640995200", "4.5", "2.5", "30", 8);
        transactionServiceMock.getHandler.mockReturnValue(handlerMock);
        contractServiceMock.getContractEvmAddress.mockResolvedValue(evmAddress);
        validationServiceMock.checkPause.mockResolvedValue(undefined);
    });
    afterAll(() => {
        jest.resetAllMocks();
    });
    describe("execute", () => {
        describe("error cases", () => {
            it("throws SetInterestRateCommandError when checkPause fails", async () => {
                const fakeError = new Error(errorMsg);
                validationServiceMock.checkPause.mockRejectedValue(fakeError);
                const resultPromise = handler.execute(command);
                await expect(resultPromise).rejects.toBeInstanceOf(SetInterestRateCommandError_1.SetInterestRateCommandError);
                await expect(resultPromise).rejects.toMatchObject({
                    message: expect.stringContaining(`Error setting interest rate: ${errorMsg}`),
                    errorCode: BaseError_1.ErrorCode.UncaughtCommandError,
                });
                expect(validationServiceMock.checkPause).toHaveBeenCalledWith(command.securityId);
            });
            it("throws SetInterestRateCommandError when getContractEvmAddress fails", async () => {
                const fakeError = new Error(errorMsg);
                validationServiceMock.checkPause.mockResolvedValue(undefined);
                contractServiceMock.getContractEvmAddress.mockRejectedValue(fakeError);
                const resultPromise = handler.execute(command);
                await expect(resultPromise).rejects.toBeInstanceOf(SetInterestRateCommandError_1.SetInterestRateCommandError);
                await expect(resultPromise).rejects.toMatchObject({
                    message: expect.stringContaining(`Error setting interest rate: ${errorMsg}`),
                    errorCode: BaseError_1.ErrorCode.UncaughtCommandError,
                });
                expect(validationServiceMock.checkPause).toHaveBeenCalledWith(command.securityId);
                expect(contractServiceMock.getContractEvmAddress).toHaveBeenCalledWith(command.securityId);
            });
            it("throws SetInterestRateCommandError when setInterestRate fails", async () => {
                const fakeError = new Error(errorMsg);
                validationServiceMock.checkPause.mockResolvedValue(undefined);
                handlerMock.setInterestRate.mockRejectedValue(fakeError);
                const resultPromise = handler.execute(command);
                await expect(resultPromise).rejects.toBeInstanceOf(SetInterestRateCommandError_1.SetInterestRateCommandError);
                await expect(resultPromise).rejects.toMatchObject({
                    message: expect.stringContaining(`Error setting interest rate: ${errorMsg}`),
                    errorCode: BaseError_1.ErrorCode.UncaughtCommandError,
                });
                expect(validationServiceMock.checkPause).toHaveBeenCalledWith(command.securityId);
                expect(contractServiceMock.getContractEvmAddress).toHaveBeenCalledWith(command.securityId);
                expect(handlerMock.setInterestRate).toHaveBeenCalledWith(evmAddress, BigDecimal_1.default.fromString(command.maxRate, command.rateDecimals), BigDecimal_1.default.fromString(command.baseRate, command.rateDecimals), BigDecimal_1.default.fromString(command.minRate, command.rateDecimals), BigDecimal_1.default.fromString(command.startPeriod), BigDecimal_1.default.fromString(command.startRate, command.rateDecimals), BigDecimal_1.default.fromString(command.missedPenalty, command.rateDecimals), BigDecimal_1.default.fromString(command.reportPeriod), command.rateDecimals, command.securityId);
            });
            it("throws SetInterestRateCommandError when command fails with uncaught error", async () => {
                const fakeError = new Error(errorMsg);
                validationServiceMock.checkPause.mockResolvedValue(undefined);
                validationServiceMock.checkRole.mockResolvedValue(undefined);
                contractServiceMock.getContractEvmAddress.mockResolvedValue(evmAddress);
                // Mock the transaction handler to throw an error
                const mockHandler = (0, ts_jest_1.createMock)();
                mockHandler.setInterestRate.mockRejectedValue(fakeError);
                transactionServiceMock.getHandler.mockReturnValue(mockHandler);
                const resultPromise = handler.execute(command);
                await expect(resultPromise).rejects.toBeInstanceOf(SetInterestRateCommandError_1.SetInterestRateCommandError);
                await expect(resultPromise).rejects.toMatchObject({
                    message: expect.stringContaining(`Error setting interest rate: ${errorMsg}`),
                    errorCode: BaseError_1.ErrorCode.UncaughtCommandError,
                });
            });
        });
        describe("success cases", () => {
            it("should successfully set interest rate", async () => {
                const transactionResponse = {
                    id: transactionId,
                    error: undefined,
                };
                validationServiceMock.checkPause.mockResolvedValue(undefined);
                handlerMock.setInterestRate.mockResolvedValue(transactionResponse);
                const result = await handler.execute(command);
                expect(validationServiceMock.checkPause).toHaveBeenCalledWith(command.securityId);
                expect(contractServiceMock.getContractEvmAddress).toHaveBeenCalledWith(command.securityId);
                expect(handlerMock.setInterestRate).toHaveBeenCalledWith(evmAddress, BigDecimal_1.default.fromString(command.maxRate, command.rateDecimals), BigDecimal_1.default.fromString(command.baseRate, command.rateDecimals), BigDecimal_1.default.fromString(command.minRate, command.rateDecimals), BigDecimal_1.default.fromString(command.startPeriod), BigDecimal_1.default.fromString(command.startRate, command.rateDecimals), BigDecimal_1.default.fromString(command.missedPenalty, command.rateDecimals), BigDecimal_1.default.fromString(command.reportPeriod), command.rateDecimals, command.securityId);
                expect(result).toEqual(new SetInterestRateCommand_1.SetInterestRateCommandResponse(transactionResponse.error === undefined, transactionResponse.id));
            });
            it("should successfully set interest rate with error in response", async () => {
                const transactionResponse = {
                    id: transactionId,
                    error: "Some error",
                };
                validationServiceMock.checkPause.mockResolvedValue(undefined);
                handlerMock.setInterestRate.mockResolvedValue(transactionResponse);
                const result = await handler.execute(command);
                expect(validationServiceMock.checkPause).toHaveBeenCalledWith(command.securityId);
                expect(contractServiceMock.getContractEvmAddress).toHaveBeenCalledWith(command.securityId);
                expect(handlerMock.setInterestRate).toHaveBeenCalledWith(evmAddress, BigDecimal_1.default.fromString(command.maxRate, command.rateDecimals), BigDecimal_1.default.fromString(command.baseRate, command.rateDecimals), BigDecimal_1.default.fromString(command.minRate, command.rateDecimals), BigDecimal_1.default.fromString(command.startPeriod), BigDecimal_1.default.fromString(command.startRate, command.rateDecimals), BigDecimal_1.default.fromString(command.missedPenalty, command.rateDecimals), BigDecimal_1.default.fromString(command.reportPeriod), command.rateDecimals, command.securityId);
                expect(result).toEqual(new SetInterestRateCommand_1.SetInterestRateCommandResponse(transactionResponse.error === undefined, transactionResponse.id));
                expect(result.payload).toBe(false);
            });
        });
    });
});
