"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 SetCouponCommand_1 = require("./SetCouponCommand");
const SetCouponCommandHandler_1 = require("./SetCouponCommandHandler");
const BigDecimal_1 = __importDefault(require("../../../../../../domain/context/shared/BigDecimal"));
const BondFixture_1 = require("../../../../../../../__tests__/fixtures/bond/BondFixture");
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 SetCouponCommandError_1 = require("./error/SetCouponCommandError");
const BaseError_1 = require("../../../../../../core/error/BaseError");
describe("SetCouponCommandHandler", () => {
    let handler;
    let command;
    const transactionServiceMock = (0, ts_jest_1.createMock)();
    const contractServiceMock = (0, ts_jest_1.createMock)();
    const evmAddress = new EvmAddress_1.default(DataFixture_1.EvmAddressPropsFixture.create().value);
    const transactionId = DataFixture_1.TransactionIdFixture.create().id;
    const couponId = DataFixture_1.CouponIdFixture.create().id;
    const errorMsg = DataFixture_1.ErrorMsgFixture.create().msg;
    beforeEach(() => {
        handler = new SetCouponCommandHandler_1.SetCouponCommandHandler(transactionServiceMock, contractServiceMock);
        command = BondFixture_1.SetCouponCommandFixture.create();
    });
    afterEach(() => {
        jest.resetAllMocks();
    });
    describe("execute", () => {
        describe("error cases", () => {
            it("throws SetCouponCommandError when command fails with uncaught error", async () => {
                const fakeError = new Error(errorMsg);
                contractServiceMock.getContractEvmAddress.mockRejectedValue(fakeError);
                const resultPromise = handler.execute(command);
                await expect(resultPromise).rejects.toBeInstanceOf(SetCouponCommandError_1.SetCouponCommandError);
                await expect(resultPromise).rejects.toMatchObject({
                    message: expect.stringContaining(`An error occurred while setting the coupon: ${errorMsg}`),
                    errorCode: BaseError_1.ErrorCode.UncaughtCommandError,
                });
            });
        });
        describe("success cases", () => {
            it("successfully sets coupon", async () => {
                setupContractEvmAddressMock();
                setupSuccessfulTransactionMock();
                setupSuccesfulTransactionResultMock();
                const result = await handler.execute(command);
                expectSuccessfulResponse(result);
                expectTransactionServiceCall(command, evmAddress);
                expect(transactionServiceMock.getHandler().setCoupon).toHaveBeenCalledTimes(1);
                expect(transactionServiceMock.getTransactionResult).toHaveBeenCalledTimes(1);
            });
        });
    });
    function setupContractEvmAddressMock() {
        contractServiceMock.getContractEvmAddress.mockResolvedValue(evmAddress);
    }
    function setupSuccessfulTransactionMock() {
        transactionServiceMock.getHandler().setCoupon.mockResolvedValue({
            id: transactionId,
            response: { couponID: couponId },
        });
    }
    function setupSuccesfulTransactionResultMock() {
        transactionServiceMock.getTransactionResult.mockResolvedValue(couponId);
    }
    function expectSuccessfulResponse(result) {
        expect(result).toBeInstanceOf(SetCouponCommand_1.SetCouponCommandResponse);
        expect(result.payload).toBe(parseInt(couponId, 16));
        expect(result.transactionId).toBe(transactionId);
    }
    function expectTransactionServiceCall(command, expectedAddress) {
        expect(transactionServiceMock.getHandler().setCoupon).toHaveBeenCalledWith(expectedAddress, BigDecimal_1.default.fromString(command.recordDate), BigDecimal_1.default.fromString(command.executionDate), BigDecimal_1.default.fromString(command.rate), BigDecimal_1.default.fromString(command.startDate), BigDecimal_1.default.fromString(command.endDate), BigDecimal_1.default.fromString(command.fixingDate), command.rateStatus, command.address);
        expect(transactionServiceMock.getTransactionResult).toHaveBeenCalledWith(expect.objectContaining({
            res: {
                id: transactionId,
                response: { couponID: couponId },
            },
            result: couponId,
            className: SetCouponCommandHandler_1.SetCouponCommandHandler.name,
            position: 0,
            numberOfResultsItems: 1,
        }));
    }
});
