"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 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 Security_1 = __importDefault(require("../../../in/security/Security"));
const BigDecimal_1 = __importDefault(require("../../../../domain/context/shared/BigDecimal"));
const BatchFreezePartialTokensCommand_1 = require("../../../../app/usecase/command/security/operations/batch/batchFreezePartialTokens/BatchFreezePartialTokensCommand");
const BatchSetAddressFrozenCommand_1 = require("../../../../app/usecase/command/security/operations/batch/batchSetAddressFrozen/BatchSetAddressFrozenCommand");
const BatchUnfreezePartialTokensCommand_1 = require("../../../../app/usecase/command/security/operations/batch/batchUnfreezePartialTokens/BatchUnfreezePartialTokensCommand");
const FreezePartialTokensCommand_1 = require("../../../../app/usecase/command/security/operations/freeze/freezePartialTokens/FreezePartialTokensCommand");
const UnfreezePartialTokensCommand_1 = require("../../../../app/usecase/command/security/operations/freeze/unfreezePartialTokens/UnfreezePartialTokensCommand");
const GetFrozenPartialTokensQuery_1 = require("../../../../app/usecase/query/security/freeze/getFrozenPartialTokens/GetFrozenPartialTokensQuery");
const BatchFixture_1 = require("../../../../../__tests__/fixtures/batch/BatchFixture");
const FreezeFixture_1 = require("../../../../../__tests__/fixtures/freeze/FreezeFixture");
const SetAddressFrozenCommand_1 = require("../../../../app/usecase/command/security/operations/freeze/setAddressFrozen/SetAddressFrozenCommand");
describe("Freeze", () => {
    let commandBusMock;
    let queryBusMock;
    let mirrorNodeMock;
    let setAddressFrozenRequest;
    let freezePartialTokensRequest;
    let unfreezePartialTokensRequest;
    let getFrozenPartialTokensRequest;
    let batchSetAddressFrozenRequest;
    let batchFreezePartialTokensRequest;
    let batchUnfreezePartialTokensRequest;
    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("SetAddressFrozen", () => {
        setAddressFrozenRequest = new request_1.SetAddressFrozenRequest(FreezeFixture_1.SetAddressFrozenRequestFixture.create());
        const expectedResponse = new SetAddressFrozenCommand_1.SetAddressFrozenCommandResponse(true, transactionId);
        it("should freeze address sucessfully", async () => {
            commandBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await Security_1.default.setAddressFrozen(setAddressFrozenRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("SetAddressFrozenRequest", setAddressFrozenRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new SetAddressFrozenCommand_1.SetAddressFrozenCommand(setAddressFrozenRequest.securityId, setAddressFrozenRequest.status, setAddressFrozenRequest.targetId));
            expect(result).toEqual(expectedResponse);
        });
        it("should throw an error if command execution fails", async () => {
            const error = new Error("Command execution failed");
            commandBusMock.execute.mockRejectedValue(error);
            await expect(Security_1.default.setAddressFrozen(setAddressFrozenRequest)).rejects.toThrow("Command execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("SetAddressFrozenRequest", setAddressFrozenRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new SetAddressFrozenCommand_1.SetAddressFrozenCommand(setAddressFrozenRequest.securityId, setAddressFrozenRequest.status, setAddressFrozenRequest.targetId));
        });
        it("should throw error if securityId is invalid", async () => {
            setAddressFrozenRequest = new request_1.SetAddressFrozenRequest({
                ...FreezeFixture_1.SetAddressFrozenRequestFixture.create({
                    securityId: "invalid",
                }),
            });
            await expect(Security_1.default.setAddressFrozen(setAddressFrozenRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if targetId is empty", async () => {
            setAddressFrozenRequest = new request_1.SetAddressFrozenRequest({
                ...FreezeFixture_1.SetAddressFrozenRequestFixture.create({
                    targetId: "",
                }),
            });
            await expect(Security_1.default.setAddressFrozen(setAddressFrozenRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if status is string", async () => {
            setAddressFrozenRequest = new request_1.SetAddressFrozenRequest({
                ...FreezeFixture_1.SetAddressFrozenRequestFixture.create({
                    status: "",
                }),
            });
            await expect(Security_1.default.setAddressFrozen(setAddressFrozenRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
    describe("FreezePartialTokens", () => {
        freezePartialTokensRequest = new request_1.FreezePartialTokensRequest(FreezeFixture_1.FreezePartialTokensRequestFixture.create());
        const expectedResponse = new FreezePartialTokensCommand_1.FreezePartialTokensResponse(true, transactionId);
        it("should freeze partial tokens sucessfully", async () => {
            commandBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await Security_1.default.freezePartialTokens(freezePartialTokensRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("FreezePartialTokensRequest", freezePartialTokensRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new FreezePartialTokensCommand_1.FreezePartialTokensCommand(freezePartialTokensRequest.securityId, freezePartialTokensRequest.amount, freezePartialTokensRequest.targetId));
            expect(result).toEqual(expectedResponse);
        });
        it("should throw an error if command execution fails", async () => {
            const error = new Error("Command execution failed");
            commandBusMock.execute.mockRejectedValue(error);
            await expect(Security_1.default.freezePartialTokens(freezePartialTokensRequest)).rejects.toThrow("Command execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("FreezePartialTokensRequest", freezePartialTokensRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new FreezePartialTokensCommand_1.FreezePartialTokensCommand(freezePartialTokensRequest.securityId, freezePartialTokensRequest.amount, freezePartialTokensRequest.targetId));
        });
        it("should throw error if securityId is invalid", async () => {
            freezePartialTokensRequest = new request_1.FreezePartialTokensRequest({
                ...FreezeFixture_1.FreezePartialTokensRequestFixture.create({
                    securityId: "invalid",
                }),
            });
            await expect(Security_1.default.freezePartialTokens(freezePartialTokensRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if targetId is empty", async () => {
            freezePartialTokensRequest = new request_1.FreezePartialTokensRequest({
                ...FreezeFixture_1.FreezePartialTokensRequestFixture.create({
                    targetId: "",
                }),
            });
            await expect(Security_1.default.freezePartialTokens(freezePartialTokensRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
    describe("UnfreezePartialTokens", () => {
        unfreezePartialTokensRequest = new request_1.UnfreezePartialTokensRequest(FreezeFixture_1.FreezePartialTokensRequestFixture.create());
        const expectedResponse = new UnfreezePartialTokensCommand_1.UnfreezePartialTokensResponse(true, transactionId);
        it("should unfreeze partial tokens sucessfully", async () => {
            commandBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await Security_1.default.unfreezePartialTokens(unfreezePartialTokensRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("UnfreezePartialTokensRequest", unfreezePartialTokensRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new UnfreezePartialTokensCommand_1.UnfreezePartialTokensCommand(unfreezePartialTokensRequest.securityId, unfreezePartialTokensRequest.amount, unfreezePartialTokensRequest.targetId));
            expect(result).toEqual(expectedResponse);
        });
        it("should throw an error if command execution fails", async () => {
            const error = new Error("Command execution failed");
            commandBusMock.execute.mockRejectedValue(error);
            await expect(Security_1.default.unfreezePartialTokens(unfreezePartialTokensRequest)).rejects.toThrow("Command execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("UnfreezePartialTokensRequest", unfreezePartialTokensRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new UnfreezePartialTokensCommand_1.UnfreezePartialTokensCommand(unfreezePartialTokensRequest.securityId, unfreezePartialTokensRequest.amount, unfreezePartialTokensRequest.targetId));
        });
        it("should throw error if securityId is invalid", async () => {
            unfreezePartialTokensRequest = new request_1.UnfreezePartialTokensRequest({
                ...FreezeFixture_1.UnfreezePartialTokensRequestFixture.create({
                    securityId: "invalid",
                }),
            });
            await expect(Security_1.default.unfreezePartialTokens(unfreezePartialTokensRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if targetId is empty", async () => {
            unfreezePartialTokensRequest = new request_1.UnfreezePartialTokensRequest({
                ...FreezeFixture_1.UnfreezePartialTokensRequestFixture.create({
                    targetId: "",
                }),
            });
            await expect(Security_1.default.unfreezePartialTokens(unfreezePartialTokensRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
    describe("getFrozenPartialTokens", () => {
        getFrozenPartialTokensRequest = new request_1.GetFrozenPartialTokensRequest(FreezeFixture_1.GetFrozenPartialTokensQueryFixture.create());
        const expectedResponse = {
            payload: new BigDecimal_1.default(BigInt(1)),
        };
        it("should get hold count for by partition successfully", async () => {
            queryBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await Security_1.default.getFrozenPartialTokens(getFrozenPartialTokensRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("GetFrozenPartialTokensRequest", getFrozenPartialTokensRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetFrozenPartialTokensQuery_1.GetFrozenPartialTokensQuery(getFrozenPartialTokensRequest.securityId, getFrozenPartialTokensRequest.targetId));
            expect(result).toEqual(expect.objectContaining({
                value: expectedResponse.payload.toString(),
            }));
        });
        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.getFrozenPartialTokens(getFrozenPartialTokensRequest)).rejects.toThrow("Query execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("GetFrozenPartialTokensRequest", getFrozenPartialTokensRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetFrozenPartialTokensQuery_1.GetFrozenPartialTokensQuery(getFrozenPartialTokensRequest.securityId, getFrozenPartialTokensRequest.targetId));
        });
        it("should throw error if securityId is invalid", async () => {
            getFrozenPartialTokensRequest = new request_1.GetFrozenPartialTokensRequest({
                ...FreezeFixture_1.GetFrozenPartialTokensQueryFixture.create({
                    securityId: "invalid",
                }),
            });
            await expect(Security_1.default.getFrozenPartialTokens(getFrozenPartialTokensRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if targetId is invalid", async () => {
            getFrozenPartialTokensRequest = new request_1.GetFrozenPartialTokensRequest({
                ...FreezeFixture_1.GetFrozenPartialTokensQueryFixture.create({
                    targetId: "invalid",
                }),
            });
            await expect(Security_1.default.getFrozenPartialTokens(getFrozenPartialTokensRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
    describe("BatchSetAddressFrozen", () => {
        batchSetAddressFrozenRequest = new request_1.BatchSetAddressFrozenRequest(BatchFixture_1.BatchSetAddressFrozenRequestFixture.create());
        const expectedResponse = new BatchSetAddressFrozenCommand_1.BatchSetAddressFrozenResponse(true, transactionId);
        it("should batch set address frozen sucessfully", async () => {
            commandBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await Security_1.default.batchSetAddressFrozen(batchSetAddressFrozenRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("BatchSetAddressFrozenRequest", batchSetAddressFrozenRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new BatchSetAddressFrozenCommand_1.BatchSetAddressFrozenCommand(batchSetAddressFrozenRequest.securityId, batchSetAddressFrozenRequest.freezeList, batchSetAddressFrozenRequest.targetList));
            expect(result).toEqual(expectedResponse);
        });
        it("should throw an error if command execution fails", async () => {
            const error = new Error("Command execution failed");
            commandBusMock.execute.mockRejectedValue(error);
            await expect(Security_1.default.batchSetAddressFrozen(batchSetAddressFrozenRequest)).rejects.toThrow("Command execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("BatchSetAddressFrozenRequest", batchSetAddressFrozenRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new BatchSetAddressFrozenCommand_1.BatchSetAddressFrozenCommand(batchSetAddressFrozenRequest.securityId, batchSetAddressFrozenRequest.freezeList, batchSetAddressFrozenRequest.targetList));
        });
        it("should throw error if securityId is invalid", async () => {
            batchSetAddressFrozenRequest = new request_1.BatchSetAddressFrozenRequest({
                ...BatchFixture_1.BatchSetAddressFrozenRequestFixture.create({
                    securityId: "invalid",
                }),
            });
            await expect(Security_1.default.batchSetAddressFrozen(batchSetAddressFrozenRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if freezeList is empty", async () => {
            batchSetAddressFrozenRequest = new request_1.BatchSetAddressFrozenRequest({
                ...BatchFixture_1.BatchSetAddressFrozenRequestFixture.create({
                    freezeList: [],
                }),
            });
            await expect(Security_1.default.batchSetAddressFrozen(batchSetAddressFrozenRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if targetList is empty", async () => {
            batchSetAddressFrozenRequest = new request_1.BatchSetAddressFrozenRequest({
                ...BatchFixture_1.BatchSetAddressFrozenRequestFixture.create({
                    targetList: [],
                }),
            });
            await expect(Security_1.default.batchSetAddressFrozen(batchSetAddressFrozenRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if list lengths are not equal", async () => {
            batchSetAddressFrozenRequest = new request_1.BatchSetAddressFrozenRequest({
                ...BatchFixture_1.BatchSetAddressFrozenRequestFixture.create({
                    targetList: [DataFixture_1.HederaIdPropsFixture.create().value, DataFixture_1.HederaIdPropsFixture.create().value],
                }),
            });
            await expect(Security_1.default.batchSetAddressFrozen(batchSetAddressFrozenRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
    describe("BatchFreezePartialTokens", () => {
        batchFreezePartialTokensRequest = new request_1.BatchFreezePartialTokensRequest(BatchFixture_1.BatchFreezePartialTokensRequestFixture.create());
        const expectedResponse = new BatchFreezePartialTokensCommand_1.BatchFreezePartialTokensResponse(true, transactionId);
        it("should batch freeze partial tokens sucessfully", async () => {
            commandBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await Security_1.default.batchFreezePartialTokens(batchFreezePartialTokensRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("BatchFreezePartialTokensRequest", batchFreezePartialTokensRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new BatchFreezePartialTokensCommand_1.BatchFreezePartialTokensCommand(batchFreezePartialTokensRequest.securityId, batchFreezePartialTokensRequest.amountList, batchFreezePartialTokensRequest.targetList));
            expect(result).toEqual(expectedResponse);
        });
        it("should throw an error if command execution fails", async () => {
            const error = new Error("Command execution failed");
            commandBusMock.execute.mockRejectedValue(error);
            await expect(Security_1.default.batchFreezePartialTokens(batchFreezePartialTokensRequest)).rejects.toThrow("Command execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("BatchFreezePartialTokensRequest", batchFreezePartialTokensRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new BatchFreezePartialTokensCommand_1.BatchFreezePartialTokensCommand(batchFreezePartialTokensRequest.securityId, batchFreezePartialTokensRequest.amountList, batchFreezePartialTokensRequest.targetList));
        });
        it("should throw error if securityId is invalid", async () => {
            batchFreezePartialTokensRequest = new request_1.BatchFreezePartialTokensRequest({
                ...BatchFixture_1.BatchFreezePartialTokensRequestFixture.create({
                    securityId: "invalid",
                }),
            });
            await expect(Security_1.default.batchFreezePartialTokens(batchFreezePartialTokensRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if amountList is empty", async () => {
            batchFreezePartialTokensRequest = new request_1.BatchFreezePartialTokensRequest({
                ...BatchFixture_1.BatchFreezePartialTokensRequestFixture.create({
                    amountList: [],
                }),
            });
            await expect(Security_1.default.batchFreezePartialTokens(batchFreezePartialTokensRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if targetList is empty", async () => {
            batchFreezePartialTokensRequest = new request_1.BatchFreezePartialTokensRequest({
                ...BatchFixture_1.BatchFreezePartialTokensRequestFixture.create({
                    targetList: [],
                }),
            });
            await expect(Security_1.default.batchFreezePartialTokens(batchFreezePartialTokensRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if list lengths are not equal", async () => {
            batchFreezePartialTokensRequest = new request_1.BatchFreezePartialTokensRequest({
                ...BatchFixture_1.BatchFreezePartialTokensRequestFixture.create({
                    targetList: [DataFixture_1.HederaIdPropsFixture.create().value, DataFixture_1.HederaIdPropsFixture.create().value],
                }),
            });
            await expect(Security_1.default.batchFreezePartialTokens(batchFreezePartialTokensRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
    describe("BatchUnfreezePartialTokens", () => {
        batchUnfreezePartialTokensRequest = new request_1.BatchUnfreezePartialTokensRequest(BatchFixture_1.BatchUnfreezePartialTokensRequestFixture.create());
        const expectedResponse = new BatchUnfreezePartialTokensCommand_1.BatchUnfreezePartialTokensResponse(true, transactionId);
        it("should batch freeze partial tokens sucessfully", async () => {
            commandBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await Security_1.default.batchUnfreezePartialTokens(batchUnfreezePartialTokensRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("BatchUnfreezePartialTokensRequest", batchUnfreezePartialTokensRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new BatchUnfreezePartialTokensCommand_1.BatchUnfreezePartialTokensCommand(batchUnfreezePartialTokensRequest.securityId, batchUnfreezePartialTokensRequest.amountList, batchUnfreezePartialTokensRequest.targetList));
            expect(result).toEqual(expectedResponse);
        });
        it("should throw an error if command execution fails", async () => {
            const error = new Error("Command execution failed");
            commandBusMock.execute.mockRejectedValue(error);
            await expect(Security_1.default.batchUnfreezePartialTokens(batchUnfreezePartialTokensRequest)).rejects.toThrow("Command execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("BatchUnfreezePartialTokensRequest", batchUnfreezePartialTokensRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new BatchUnfreezePartialTokensCommand_1.BatchUnfreezePartialTokensCommand(batchUnfreezePartialTokensRequest.securityId, batchUnfreezePartialTokensRequest.amountList, batchUnfreezePartialTokensRequest.targetList));
        });
        it("should throw error if securityId is invalid", async () => {
            batchUnfreezePartialTokensRequest = new request_1.BatchUnfreezePartialTokensRequest({
                ...BatchFixture_1.BatchUnfreezePartialTokensRequestFixture.create({
                    securityId: "invalid",
                }),
            });
            await expect(Security_1.default.batchUnfreezePartialTokens(batchUnfreezePartialTokensRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if amountList is empty", async () => {
            batchUnfreezePartialTokensRequest = new request_1.BatchUnfreezePartialTokensRequest({
                ...BatchFixture_1.BatchUnfreezePartialTokensRequestFixture.create({
                    amountList: [],
                }),
            });
            await expect(Security_1.default.batchUnfreezePartialTokens(batchUnfreezePartialTokensRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if targetList is empty", async () => {
            batchUnfreezePartialTokensRequest = new request_1.BatchUnfreezePartialTokensRequest({
                ...BatchFixture_1.BatchUnfreezePartialTokensRequestFixture.create({
                    targetList: [],
                }),
            });
            await expect(Security_1.default.batchUnfreezePartialTokens(batchUnfreezePartialTokensRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if list lengths are not equal", async () => {
            batchUnfreezePartialTokensRequest = new request_1.BatchUnfreezePartialTokensRequest({
                ...BatchFixture_1.BatchUnfreezePartialTokensRequestFixture.create({
                    targetList: [DataFixture_1.HederaIdPropsFixture.create().value, DataFixture_1.HederaIdPropsFixture.create().value],
                }),
            });
            await expect(Security_1.default.batchUnfreezePartialTokens(batchUnfreezePartialTokensRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
});
