"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 Security_1 = __importDefault(require("../../../in/security/Security"));
const ERC1400Fixture_1 = require("../../../../../__tests__/fixtures/erc1400/ERC1400Fixture");
const SecurityFixture_1 = require("../../../../../__tests__/fixtures/shared/SecurityFixture");
const GetSecurityQuery_1 = require("../../../../app/usecase/query/security/get/GetSecurityQuery");
const SecurityFixture_2 = require("../../../../../__tests__/fixtures/security/SecurityFixture");
const DataFixture_1 = require("../../../../../__tests__/fixtures/shared/DataFixture");
const GetSecurityHoldersQuery_1 = require("../../../../app/usecase/query/security/security/getSecurityHolders/GetSecurityHoldersQuery");
const GetTotalSecurityHoldersQuery_1 = require("../../../../app/usecase/query/security/security/getTotalSecurityHolders/GetTotalSecurityHoldersQuery");
describe("Info", () => {
    let commandBusMock;
    let queryBusMock;
    let mirrorNodeMock;
    let getSecurityDetailsRequest;
    let getSecurityHoldersRequest;
    let getTotalSecurityHoldersRequest;
    let handleValidationSpy;
    const transactionId = DataFixture_1.TransactionIdFixture.create().id;
    beforeEach(() => {
        commandBusMock = (0, ts_jest_1.createMock)();
        queryBusMock = (0, ts_jest_1.createMock)();
        mirrorNodeMock = (0, ts_jest_1.createMock)();
        handleValidationSpy = jest.spyOn(ValidatedArgs_1.default, "handleValidation");
        jest.spyOn(LogService_1.default, "logError").mockImplementation(() => { });
        Security_1.default.commandBus = commandBusMock;
        Security_1.default.queryBus = queryBusMock;
        Security_1.default.mirrorNode = mirrorNodeMock;
    });
    afterEach(() => {
        jest.resetAllMocks();
        jest.restoreAllMocks();
    });
    describe("getInfo", () => {
        getSecurityDetailsRequest = new request_1.GetSecurityDetailsRequest(ERC1400Fixture_1.GetSecurityDetailsRequestFixture.create());
        const expectedResponse = {
            security: SecurityFixture_1.SecurityPropsFixture.create(),
        };
        it("should get security info successfully", async () => {
            queryBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await Security_1.default.getInfo(getSecurityDetailsRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("GetSecurityDetailsRequest", getSecurityDetailsRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetSecurityQuery_1.GetSecurityQuery(getSecurityDetailsRequest.securityId));
            expect(result).toEqual(expect.objectContaining({
                name: expectedResponse.security.name,
                symbol: expectedResponse.security.symbol,
                isin: expectedResponse.security.isin,
                type: expectedResponse.security.type,
                decimals: expectedResponse.security.decimals,
                isWhiteList: expectedResponse.security.isWhiteList,
                isControllable: expectedResponse.security.isControllable,
                isMultiPartition: expectedResponse.security.isMultiPartition,
                totalSupply: expectedResponse.security.totalSupply?.toString(),
                maxSupply: expectedResponse.security.maxSupply?.toString(),
                diamondAddress: expectedResponse.security.diamondAddress?.toString(),
                evmDiamondAddress: expectedResponse.security.evmDiamondAddress?.toString(),
                paused: expectedResponse.security.paused,
                regulation: expectedResponse.security.regulation,
                isCountryControlListWhiteList: expectedResponse.security.isCountryControlListWhiteList,
                countries: expectedResponse.security.countries,
                info: expectedResponse.security.info,
            }));
        });
        it("should throw an error if query execution fails", async () => {
            const error = new Error("Query execution failed");
            queryBusMock.execute.mockRejectedValue(error);
            await expect(Security_1.default.getInfo(getSecurityDetailsRequest)).rejects.toThrow("Query execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("GetSecurityDetailsRequest", getSecurityDetailsRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetSecurityQuery_1.GetSecurityQuery(getSecurityDetailsRequest.securityId));
        });
        it("should throw error if securityId is invalid", async () => {
            getSecurityDetailsRequest = new request_1.GetSecurityDetailsRequest({
                ...ERC1400Fixture_1.GetSecurityDetailsRequestFixture.create({
                    securityId: "invalid",
                }),
            });
            await expect(Security_1.default.getInfo(getSecurityDetailsRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
    describe("getSecurityHolders", () => {
        getSecurityHoldersRequest = new request_1.GetSecurityHoldersRequest(SecurityFixture_2.GetSecurityHoldersRequestFixture.create());
        it("should get token holders at snapshot successfully", async () => {
            const expectedResponse = {
                payload: [transactionId],
            };
            queryBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await Security_1.default.getSecurityHolders(getSecurityHoldersRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith(request_1.GetSecurityHoldersRequest.name, getSecurityHoldersRequest);
            expect(queryBusMock.execute).toHaveBeenCalledTimes(1);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetSecurityHoldersQuery_1.GetSecurityHoldersQuery(getSecurityHoldersRequest.securityId, getSecurityHoldersRequest.start, getSecurityHoldersRequest.end));
            expect(result).toStrictEqual(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(Security_1.default.getSecurityHolders(getSecurityHoldersRequest)).rejects.toThrow("Query execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith(request_1.GetSecurityHoldersRequest.name, getSecurityHoldersRequest);
            expect(queryBusMock.execute).toHaveBeenCalledTimes(1);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetSecurityHoldersQuery_1.GetSecurityHoldersQuery(getSecurityHoldersRequest.securityId, getSecurityHoldersRequest.start, getSecurityHoldersRequest.end));
        });
        it("should throw error if securityId is invalid", async () => {
            getSecurityHoldersRequest = new request_1.GetSecurityHoldersRequest({
                ...SecurityFixture_2.GetSecurityHoldersRequestFixture.create(),
                securityId: "invalid",
            });
            await expect(Security_1.default.getSecurityHolders(getSecurityHoldersRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if start is invalid", async () => {
            getSecurityHoldersRequest = new request_1.GetSecurityHoldersRequest({
                ...SecurityFixture_2.GetSecurityHoldersRequestFixture.create(),
                start: -1,
            });
            await expect(Security_1.default.getSecurityHolders(getSecurityHoldersRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if end is invalid", async () => {
            getSecurityHoldersRequest = new request_1.GetSecurityHoldersRequest({
                ...SecurityFixture_2.GetSecurityHoldersRequestFixture.create(),
                end: -1,
            });
            await expect(Security_1.default.getSecurityHolders(getSecurityHoldersRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
    describe("getTotalSecurityHolders", () => {
        getTotalSecurityHoldersRequest = new request_1.GetTotalSecurityHoldersRequest(SecurityFixture_2.GetTotalSecurityHoldersRequestFixture.create());
        it("should get total security holders successfully", async () => {
            const expectedResponse = {
                payload: 1,
            };
            queryBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await Security_1.default.getTotalSecurityHolders(getTotalSecurityHoldersRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith(request_1.GetTotalSecurityHoldersRequest.name, getTotalSecurityHoldersRequest);
            expect(queryBusMock.execute).toHaveBeenCalledTimes(1);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetTotalSecurityHoldersQuery_1.GetTotalSecurityHoldersQuery(getTotalSecurityHoldersRequest.securityId));
            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(Security_1.default.getTotalSecurityHolders(getTotalSecurityHoldersRequest)).rejects.toThrow("Query execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith(request_1.GetTotalSecurityHoldersRequest.name, getTotalSecurityHoldersRequest);
            expect(queryBusMock.execute).toHaveBeenCalledTimes(1);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetTotalSecurityHoldersQuery_1.GetTotalSecurityHoldersQuery(getTotalSecurityHoldersRequest.securityId));
        });
        it("should throw error if securityId is invalid", async () => {
            getTotalSecurityHoldersRequest = new request_1.GetTotalSecurityHoldersRequest({
                ...SecurityFixture_2.GetTotalSecurityHoldersRequestFixture.create(),
                securityId: "invalid",
            });
            await expect(Security_1.default.getTotalSecurityHolders(getTotalSecurityHoldersRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
});
