"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 ValidatedArgs_1 = __importDefault(require("../../../core/validation/ValidatedArgs"));
const ValidationError_1 = require("../../../core/validation/ValidationError");
const LogService_1 = __importDefault(require("../../../app/service/log/LogService"));
const GetLatestKpiDataRequest_1 = __importDefault(require("../request/kpis/GetLatestKpiDataRequest"));
const GetMinDateRequest_1 = __importDefault(require("../request/kpis/GetMinDateRequest"));
const IsCheckPointDateRequest_1 = __importDefault(require("../request/kpis/IsCheckPointDateRequest"));
const GetLatestKpiDataQuery_1 = require("../../../app/usecase/query/interestRates/getLatestKpiData/GetLatestKpiDataQuery");
const GetMinDateQuery_1 = require("../../../app/usecase/query/kpis/getMinDate/GetMinDateQuery");
const IsCheckPointDateQuery_1 = require("../../../app/usecase/query/kpis/isCheckPointDate/IsCheckPointDateQuery");
const Kpis_1 = __importDefault(require("./Kpis"));
describe("Kpis", () => {
    let queryBusMock;
    let getLatestKpiDataRequest;
    let getMinDateRequest;
    let isCheckPointDateRequest;
    let handleValidationSpy;
    const mockSecurityId = "0.0.123456";
    const mockFrom = "1000";
    const mockTo = "2000";
    const mockKpi = "test-kpi";
    const mockDate = 1234567890;
    const mockProject = "0x1234567890123456789012345678901234567890";
    const mockValue = "12345";
    const mockExists = true;
    const mockMinDate = 1000000;
    const mockIsCheckPoint = true;
    beforeEach(() => {
        queryBusMock = (0, ts_jest_1.createMock)();
        handleValidationSpy = jest.spyOn(ValidatedArgs_1.default, "handleValidation");
        jest.spyOn(LogService_1.default, "logError").mockImplementation(() => { });
        Kpis_1.default.queryBus = queryBusMock;
    });
    afterEach(() => {
        jest.resetAllMocks();
        jest.restoreAllMocks();
    });
    describe("getLatestKpiData", () => {
        beforeEach(() => {
            getLatestKpiDataRequest = new GetLatestKpiDataRequest_1.default({
                securityId: mockSecurityId,
                from: mockFrom,
                to: mockTo,
                kpi: mockKpi,
            });
        });
        it("should get latest kpi data successfully", async () => {
            const expectedResponse = new GetLatestKpiDataQuery_1.GetLatestKpiDataQueryResponse(mockValue, mockExists);
            queryBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await Kpis_1.default.getLatestKpiData(getLatestKpiDataRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("GetLatestKpiDataRequest", getLatestKpiDataRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetLatestKpiDataQuery_1.GetLatestKpiDataQuery(getLatestKpiDataRequest.securityId, BigInt(getLatestKpiDataRequest.from), BigInt(getLatestKpiDataRequest.to), getLatestKpiDataRequest.kpi));
            expect(result).toEqual({ value: mockValue, exists: mockExists });
        });
        it("should throw an error if query execution fails", async () => {
            const error = new Error("Query execution failed");
            queryBusMock.execute.mockRejectedValue(error);
            await expect(Kpis_1.default.getLatestKpiData(getLatestKpiDataRequest)).rejects.toThrow("Query execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("GetLatestKpiDataRequest", getLatestKpiDataRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetLatestKpiDataQuery_1.GetLatestKpiDataQuery(getLatestKpiDataRequest.securityId, BigInt(getLatestKpiDataRequest.from), BigInt(getLatestKpiDataRequest.to), getLatestKpiDataRequest.kpi));
        });
        it("should throw error if securityId is invalid", async () => {
            getLatestKpiDataRequest = new GetLatestKpiDataRequest_1.default({
                securityId: "",
                from: mockFrom,
                to: mockTo,
                kpi: mockKpi,
            });
            await expect(Kpis_1.default.getLatestKpiData(getLatestKpiDataRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if kpi is invalid", async () => {
            getLatestKpiDataRequest = new GetLatestKpiDataRequest_1.default({
                securityId: mockSecurityId,
                from: mockFrom,
                to: mockTo,
                kpi: "",
            });
            await expect(Kpis_1.default.getLatestKpiData(getLatestKpiDataRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
    describe("getMinDate", () => {
        beforeEach(() => {
            getMinDateRequest = new GetMinDateRequest_1.default({ securityId: mockSecurityId });
        });
        it("should get min date successfully", async () => {
            const expectedResponse = new GetMinDateQuery_1.GetMinDateQueryResponse(mockMinDate);
            queryBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await Kpis_1.default.getMinDate(getMinDateRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("GetMinDateRequest", getMinDateRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetMinDateQuery_1.GetMinDateQuery(getMinDateRequest.securityId));
            expect(result).toEqual(mockMinDate);
        });
        it("should throw an error if query execution fails", async () => {
            const error = new Error("Query execution failed");
            queryBusMock.execute.mockRejectedValue(error);
            await expect(Kpis_1.default.getMinDate(getMinDateRequest)).rejects.toThrow("Query execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("GetMinDateRequest", getMinDateRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetMinDateQuery_1.GetMinDateQuery(getMinDateRequest.securityId));
        });
        it("should throw error if securityId is invalid", async () => {
            getMinDateRequest = new GetMinDateRequest_1.default({
                securityId: "",
            });
            await expect(Kpis_1.default.getMinDate(getMinDateRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
    describe("isCheckPointDate", () => {
        beforeEach(() => {
            isCheckPointDateRequest = new IsCheckPointDateRequest_1.default({
                securityId: mockSecurityId,
                date: mockDate,
                project: mockProject,
            });
        });
        it("should check if date is checkpoint successfully", async () => {
            const expectedResponse = new IsCheckPointDateQuery_1.IsCheckPointDateQueryResponse(mockIsCheckPoint);
            queryBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await Kpis_1.default.isCheckPointDate(isCheckPointDateRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("IsCheckPointDateRequest", isCheckPointDateRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new IsCheckPointDateQuery_1.IsCheckPointDateQuery(isCheckPointDateRequest.securityId, BigInt(isCheckPointDateRequest.date), isCheckPointDateRequest.project));
            expect(result).toEqual(mockIsCheckPoint);
        });
        it("should throw an error if query execution fails", async () => {
            const error = new Error("Query execution failed");
            queryBusMock.execute.mockRejectedValue(error);
            await expect(Kpis_1.default.isCheckPointDate(isCheckPointDateRequest)).rejects.toThrow("Query execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("IsCheckPointDateRequest", isCheckPointDateRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new IsCheckPointDateQuery_1.IsCheckPointDateQuery(isCheckPointDateRequest.securityId, BigInt(isCheckPointDateRequest.date), isCheckPointDateRequest.project));
        });
        it("should throw error if securityId is invalid", async () => {
            isCheckPointDateRequest = new IsCheckPointDateRequest_1.default({
                securityId: "",
                date: mockDate,
                project: mockProject,
            });
            await expect(Kpis_1.default.isCheckPointDate(isCheckPointDateRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if project is invalid", async () => {
            isCheckPointDateRequest = new IsCheckPointDateRequest_1.default({
                securityId: mockSecurityId,
                date: mockDate,
                project: "",
            });
            await expect(Kpis_1.default.isCheckPointDate(isCheckPointDateRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
});
