"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 });
require("reflect-metadata");
const tsyringe_1 = require("tsyringe");
const GetInterestRateQueryHandler_1 = require("./GetInterestRateQueryHandler");
const GetInterestRateQuery_1 = require("./GetInterestRateQuery");
const RPCQueryAdapter_1 = require("../../../../../port/out/rpc/RPCQueryAdapter");
const AccountService_1 = __importDefault(require("../../../../service/account/AccountService"));
const EvmAddress_1 = __importDefault(require("../../../../../domain/context/contract/EvmAddress"));
const GetInterestRateQueryError_1 = require("./error/GetInterestRateQueryError");
describe("GetInterestRateQueryHandler", () => {
    let handler;
    let mockQueryAdapter;
    let mockAccountService;
    beforeEach(() => {
        mockQueryAdapter = {
            getInterestRate: jest.fn(),
        };
        mockAccountService = {
            getAccountEvmAddress: jest.fn(),
        };
        tsyringe_1.container.registerInstance(RPCQueryAdapter_1.RPCQueryAdapter, mockQueryAdapter);
        tsyringe_1.container.registerInstance(AccountService_1.default, mockAccountService);
        handler = tsyringe_1.container.resolve(GetInterestRateQueryHandler_1.GetInterestRateQueryHandler);
    });
    it("should return interest rate data successfully", async () => {
        // Arrange
        const securityId = "0.0.123456";
        const evmAddress = new EvmAddress_1.default("0x1234567890123456789012345678901234567890");
        const mockResult = [
            1000n,
            500n,
            100n,
            1640995200n,
            400n,
            50n,
            30n,
            2n,
        ];
        mockAccountService.getAccountEvmAddress.mockResolvedValue(evmAddress);
        mockQueryAdapter.getInterestRate.mockResolvedValue(mockResult);
        const query = new GetInterestRateQuery_1.GetInterestRateQuery(securityId);
        // Act
        const result = await handler.execute(query);
        // Assert
        expect(result).toBeInstanceOf(GetInterestRateQuery_1.GetInterestRateQueryResponse);
        expect(result.maxRate).toBe("1000");
        expect(result.baseRate).toBe("500");
        expect(result.minRate).toBe("100");
        expect(result.startPeriod).toBe("1640995200");
        expect(result.startRate).toBe("400");
        expect(result.missedPenalty).toBe("50");
        expect(result.reportPeriod).toBe("30");
        expect(result.rateDecimals).toBe(2);
        expect(mockAccountService.getAccountEvmAddress).toHaveBeenCalledWith(securityId);
        expect(mockQueryAdapter.getInterestRate).toHaveBeenCalledWith(evmAddress);
    });
    it("should throw GetInterestRateQueryError when service fails", async () => {
        // Arrange
        const securityId = "0.0.123456";
        const error = new Error("Service error");
        mockAccountService.getAccountEvmAddress.mockRejectedValue(error);
        const query = new GetInterestRateQuery_1.GetInterestRateQuery(securityId);
        // Act & Assert
        await expect(handler.execute(query)).rejects.toThrow(GetInterestRateQueryError_1.GetInterestRateQueryError);
    });
});
