"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 ValidatedArgs_1 = __importDefault(require("../../../../core/validation/ValidatedArgs"));
const ValidationError_1 = require("../../../../core/validation/ValidationError");
const FixedRate_1 = __importDefault(require("./FixedRate"));
const SetRateCommand_1 = require("../../../../app/usecase/command/interestRates/setRate/SetRateCommand");
const GetRateQuery_1 = require("../../../../app/usecase/query/interestRates/getRate/GetRateQuery");
const SetRateRequest_1 = __importDefault(require("../../request/interestRates/SetRateRequest"));
const GetRateRequest_1 = __importDefault(require("../../request/interestRates/GetRateRequest"));
describe("FixedRate", () => {
    let commandBusMock;
    let queryBusMock;
    let setRateRequest;
    let getRateRequest;
    let handleValidationSpy;
    const transactionId = DataFixture_1.TransactionIdFixture.create().id;
    const expectedSetRateResponse = {
        payload: true,
        transactionId: transactionId,
    };
    const expectedGetRateResponse = {
        rate: "5.5",
        decimals: 8,
    };
    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(() => { });
        FixedRate_1.default.commandBus = commandBusMock;
        FixedRate_1.default.queryBus = queryBusMock;
    });
    afterEach(() => {
        jest.resetAllMocks();
        jest.restoreAllMocks();
    });
    describe("setRate", () => {
        beforeEach(() => {
            setRateRequest = new SetRateRequest_1.default({
                securityId: "0x1234567890123456789012345678901234567890",
                rate: "5.5",
                rateDecimals: 8,
            });
        });
        it("should set rate successfully", async () => {
            commandBusMock.execute.mockResolvedValue(expectedSetRateResponse);
            const result = await FixedRate_1.default.setRate(setRateRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("SetRateRequest", setRateRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new SetRateCommand_1.SetRateCommand(setRateRequest.securityId, setRateRequest.rate, setRateRequest.rateDecimals));
            expect(result).toEqual(expectedSetRateResponse);
        });
        it("should throw an error if command execution fails", async () => {
            const error = new Error("Command execution failed");
            commandBusMock.execute.mockRejectedValue(error);
            await expect(FixedRate_1.default.setRate(setRateRequest)).rejects.toThrow(error);
            expect(handleValidationSpy).toHaveBeenCalledWith("SetRateRequest", setRateRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new SetRateCommand_1.SetRateCommand(setRateRequest.securityId, setRateRequest.rate, setRateRequest.rateDecimals));
        });
        it("should throw validation error", async () => {
            const validationError = new ValidationError_1.ValidationError("Validation failed", []);
            handleValidationSpy.mockImplementation(() => {
                throw validationError;
            });
            await expect(FixedRate_1.default.setRate(setRateRequest)).rejects.toThrow("Validation failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("SetRateRequest", setRateRequest);
            expect(commandBusMock.execute).not.toHaveBeenCalled();
        });
    });
    describe("getRate", () => {
        beforeEach(() => {
            getRateRequest = new GetRateRequest_1.default({
                securityId: "0x1234567890123456789012345678901234567890",
            });
        });
        it("should get rate successfully", async () => {
            queryBusMock.execute.mockResolvedValue(expectedGetRateResponse);
            const result = await FixedRate_1.default.getRate(getRateRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("GetRateRequest", getRateRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetRateQuery_1.GetRateQuery(getRateRequest.securityId));
            expect(result).toEqual(expectedGetRateResponse);
        });
        it("should throw an error if query execution fails", async () => {
            const error = new Error("Query execution failed");
            queryBusMock.execute.mockRejectedValue(error);
            await expect(FixedRate_1.default.getRate(getRateRequest)).rejects.toThrow(error);
            expect(handleValidationSpy).toHaveBeenCalledWith("GetRateRequest", getRateRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetRateQuery_1.GetRateQuery(getRateRequest.securityId));
        });
        it("should throw validation error", async () => {
            const validationError = new ValidationError_1.ValidationError("Validation failed", []);
            handleValidationSpy.mockImplementation(() => {
                throw validationError;
            });
            await expect(FixedRate_1.default.getRate(getRateRequest)).rejects.toThrow("Validation failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("GetRateRequest", getRateRequest);
            expect(queryBusMock.execute).not.toHaveBeenCalled();
        });
    });
});
