"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 request_1 = require("../../request");
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 CorporateActions_1 = __importDefault(require("./CorporateActions"));
const CorporateActionsFixture_1 = require("../../../../../__tests__/fixtures/corporateActions/CorporateActionsFixture");
const ActionContentHashExistsQuery_1 = require("../../../../app/usecase/query/security/actionContentHashExists/ActionContentHashExistsQuery");
describe("Corporate Actions", () => {
    let queryBusMock;
    let actionContentHashExistsRequest;
    let handleValidationSpy;
    beforeEach(() => {
        queryBusMock = (0, ts_jest_1.createMock)();
        handleValidationSpy = jest.spyOn(ValidatedArgs_1.default, "handleValidation");
        jest.spyOn(LogService_1.default, "logError").mockImplementation(() => { });
        CorporateActions_1.default.queryBus = queryBusMock;
    });
    afterEach(() => {
        jest.resetAllMocks();
        jest.restoreAllMocks();
    });
    describe("ActionContentHashExists", () => {
        actionContentHashExistsRequest = new request_1.ActionContentHashExistsRequest(CorporateActionsFixture_1.ActionContentHashExistsRequestFixture.create());
        const expectedResponse = {
            payload: true,
        };
        it("should check action content hash exist successfully", async () => {
            queryBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await CorporateActions_1.default.actionContentHashExists(actionContentHashExistsRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("ActionContentHashExistsRequest", actionContentHashExistsRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new ActionContentHashExistsQuery_1.ActionContentHashExistsQuery(actionContentHashExistsRequest.securityId, actionContentHashExistsRequest.contentHash));
            expect(result).toEqual(expectedResponse.payload);
        });
        it("should throw an error if query execution fails", async () => {
            const error = new Error("Query execution failed");
            queryBusMock.execute.mockRejectedValue(error);
            await expect(CorporateActions_1.default.actionContentHashExists(actionContentHashExistsRequest)).rejects.toThrow("Query execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("ActionContentHashExistsRequest", actionContentHashExistsRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new ActionContentHashExistsQuery_1.ActionContentHashExistsQuery(actionContentHashExistsRequest.securityId, actionContentHashExistsRequest.contentHash));
        });
        it("should throw error if securityId is invalid", async () => {
            actionContentHashExistsRequest = new request_1.ActionContentHashExistsRequest({
                ...CorporateActionsFixture_1.ActionContentHashExistsRequestFixture.create({
                    securityId: "invalid",
                }),
            });
            await expect(CorporateActions_1.default.actionContentHashExists(actionContentHashExistsRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if content hash is invalid", async () => {
            actionContentHashExistsRequest = new request_1.ActionContentHashExistsRequest({
                ...CorporateActionsFixture_1.ActionContentHashExistsRequestFixture.create({
                    contentHash: "invalid",
                }),
            });
            await expect(CorporateActions_1.default.actionContentHashExists(actionContentHashExistsRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
    describe("getCorporateAction", () => {
        it("should get corporate action successfully", async () => {
            const fixtureData = CorporateActionsFixture_1.GetCorporateActionRequestFixture.create();
            const request = new request_1.GetCorporateActionRequest({
                securityId: fixtureData.securityId,
                corporateActionId: fixtureData.corporateActionId,
            });
            const expectedResponse = CorporateActionsFixture_1.GetCorporateActionResponseFixture.create();
            queryBusMock.execute.mockResolvedValue({ payload: expectedResponse });
            const result = await CorporateActions_1.default.getCorporateAction(request);
            expect(result).toEqual(expectedResponse);
            expect(result.actionType).toBe(expectedResponse.actionType);
            expect(result.actionTypeId).toBe(expectedResponse.actionTypeId);
            expect(result.data).toBe(expectedResponse.data);
            expect(result.isDisabled).toBe(expectedResponse.isDisabled);
        });
        it("should throw ValidationError for invalid securityId", async () => {
            const invalidRequest = new request_1.GetCorporateActionRequest({
                securityId: "invalid-id",
                corporateActionId: "0x" + "a".repeat(64),
            });
            await expect(CorporateActions_1.default.getCorporateAction(invalidRequest)).rejects.toThrow();
        });
        it("should throw ValidationError for invalid corporateActionId", async () => {
            const invalidRequest = new request_1.GetCorporateActionRequest({
                securityId: "0.0.12345",
                corporateActionId: "invalid-bytes32",
            });
            await expect(CorporateActions_1.default.getCorporateAction(invalidRequest)).rejects.toThrow();
        });
    });
    describe("getCorporateActions", () => {
        it("should get corporate actions successfully", async () => {
            const fixtureData = CorporateActionsFixture_1.GetCorporateActionsRequestFixture.create();
            const request = new request_1.GetCorporateActionsRequest({
                securityId: fixtureData.securityId,
                start: fixtureData.start,
                end: fixtureData.end,
            });
            const expectedResponse = CorporateActionsFixture_1.GetCorporateActionsResponseFixture.create();
            queryBusMock.execute.mockResolvedValue({ payload: expectedResponse });
            const result = await CorporateActions_1.default.getCorporateActions(request);
            expect(result).toEqual(expectedResponse);
            expect(result.actionTypes).toEqual(expectedResponse.actionTypes);
            expect(result.actionTypeIds).toEqual(expectedResponse.actionTypeIds);
            expect(result.datas).toEqual(expectedResponse.datas);
            expect(result.isDisabled).toEqual(expectedResponse.isDisabled);
        });
        it("should throw ValidationError for invalid securityId", async () => {
            const invalidRequest = new request_1.GetCorporateActionsRequest({
                securityId: "invalid-id",
                start: 0,
                end: 10,
            });
            await expect(CorporateActions_1.default.getCorporateActions(invalidRequest)).rejects.toThrow();
        });
        it("should throw ValidationError for invalid start", async () => {
            const invalidRequest = new request_1.GetCorporateActionsRequest({
                securityId: "0.0.12345",
                start: -1,
                end: 10,
            });
            await expect(CorporateActions_1.default.getCorporateActions(invalidRequest)).rejects.toThrow();
        });
        it("should throw ValidationError for invalid end", async () => {
            const invalidRequest = new request_1.GetCorporateActionsRequest({
                securityId: "0.0.12345",
                start: 0,
                end: 0,
            });
            await expect(CorporateActions_1.default.getCorporateActions(invalidRequest)).rejects.toThrow();
        });
    });
    describe("getCorporateActionsByType", () => {
        it("should get corporate actions by type successfully", async () => {
            const fixtureData = CorporateActionsFixture_1.GetCorporateActionsByTypeRequestFixture.create();
            const request = new request_1.GetCorporateActionsByTypeRequest({
                securityId: fixtureData.securityId,
                actionType: fixtureData.actionType,
                start: fixtureData.start,
                end: fixtureData.end,
            });
            const expectedResponse = CorporateActionsFixture_1.GetCorporateActionsResponseFixture.create();
            queryBusMock.execute.mockResolvedValue({ payload: expectedResponse });
            const result = await CorporateActions_1.default.getCorporateActionsByType(request);
            expect(result).toEqual(expectedResponse);
            expect(result.actionTypes).toEqual(expectedResponse.actionTypes);
            expect(result.actionTypeIds).toEqual(expectedResponse.actionTypeIds);
            expect(result.datas).toEqual(expectedResponse.datas);
            expect(result.isDisabled).toEqual(expectedResponse.isDisabled);
        });
        it("should throw ValidationError for invalid securityId", async () => {
            const invalidRequest = new request_1.GetCorporateActionsByTypeRequest({
                securityId: "invalid-id",
                actionType: "0x" + "a".repeat(64),
                start: 0,
                end: 10,
            });
            await expect(CorporateActions_1.default.getCorporateActionsByType(invalidRequest)).rejects.toThrow();
        });
        it("should throw ValidationError for invalid actionType", async () => {
            const invalidRequest = new request_1.GetCorporateActionsByTypeRequest({
                securityId: "0.0.12345",
                actionType: "invalid-bytes32",
                start: 0,
                end: 10,
            });
            await expect(CorporateActions_1.default.getCorporateActionsByType(invalidRequest)).rejects.toThrow();
        });
        it("should throw ValidationError for invalid start", async () => {
            const invalidRequest = new request_1.GetCorporateActionsByTypeRequest({
                securityId: "0.0.12345",
                actionType: "0x" + "a".repeat(64),
                start: -1,
                end: 10,
            });
            await expect(CorporateActions_1.default.getCorporateActionsByType(invalidRequest)).rejects.toThrow();
        });
        it("should throw ValidationError for invalid end", async () => {
            const invalidRequest = new request_1.GetCorporateActionsByTypeRequest({
                securityId: "0.0.12345",
                actionType: "0x" + "a".repeat(64),
                start: 0,
                end: 0,
            });
            await expect(CorporateActions_1.default.getCorporateActionsByType(invalidRequest)).rejects.toThrow();
        });
    });
});
