"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 LockFixture_1 = require("../../../../../__tests__/fixtures/lock/LockFixture");
const LockCommand_1 = require("../../../../app/usecase/command/security/operations/lock/LockCommand");
const ReleaseCommand_1 = require("../../../../app/usecase/command/security/operations/release/ReleaseCommand");
const BigDecimal_1 = __importDefault(require("../../../../domain/context/shared/BigDecimal"));
const LockedBalanceOfQuery_1 = require("../../../../app/usecase/query/security/lockedBalanceOf/LockedBalanceOfQuery");
const LockCountQuery_1 = require("../../../../app/usecase/query/security/lockCount/LockCountQuery");
const LocksIdQuery_1 = require("../../../../app/usecase/query/security/locksId/LocksIdQuery");
const GetLockQuery_1 = require("../../../../app/usecase/query/security/getLock/GetLockQuery");
describe("Lock", () => {
    let commandBusMock;
    let queryBusMock;
    let mirrorNodeMock;
    let lockRequest;
    let releaseRequest;
    let getLockedBalanceRequest;
    let getLockCountRequest;
    let getLocksIdRequest;
    let getLockRequest;
    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("lock", () => {
        lockRequest = new request_1.LockRequest(LockFixture_1.LockRequestFixture.create());
        const expectedResponse = {
            payload: true,
            transactionId: transactionId,
        };
        it("should lock successfully", async () => {
            commandBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await Security_1.default.lock(lockRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("LockRequest", lockRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new LockCommand_1.LockCommand(lockRequest.amount, lockRequest.targetId, lockRequest.securityId, lockRequest.expirationTimestamp));
            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.lock(lockRequest)).rejects.toThrow("Command execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("LockRequest", lockRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new LockCommand_1.LockCommand(lockRequest.amount, lockRequest.targetId, lockRequest.securityId, lockRequest.expirationTimestamp));
        });
        it("should throw error if securityId is invalid", async () => {
            lockRequest = new request_1.LockRequest({
                ...LockFixture_1.LockRequestFixture.create({
                    securityId: "invalid",
                }),
            });
            await expect(Security_1.default.lock(lockRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if targetId is invalid", async () => {
            lockRequest = new request_1.LockRequest({
                ...LockFixture_1.LockRequestFixture.create({
                    targetId: "invalid",
                }),
            });
            await expect(Security_1.default.lock(lockRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if amount is invalid", async () => {
            lockRequest = new request_1.LockRequest({
                ...LockFixture_1.LockRequestFixture.create({
                    amount: "invalid",
                }),
            });
            await expect(Security_1.default.lock(lockRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
    describe("release", () => {
        releaseRequest = new request_1.ReleaseRequest(LockFixture_1.ReleaseRequestFixture.create());
        const expectedResponse = {
            payload: true,
            transactionId: transactionId,
        };
        it("should release successfully", async () => {
            commandBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await Security_1.default.release(releaseRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("ReleaseRequest", releaseRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new ReleaseCommand_1.ReleaseCommand(releaseRequest.lockId, releaseRequest.targetId, releaseRequest.securityId));
            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.release(releaseRequest)).rejects.toThrow("Command execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("ReleaseRequest", releaseRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new ReleaseCommand_1.ReleaseCommand(releaseRequest.lockId, releaseRequest.targetId, releaseRequest.securityId));
        });
        it("should throw error if targetId is invalid", async () => {
            releaseRequest = new request_1.ReleaseRequest({
                ...LockFixture_1.ReleaseRequestFixture.create({
                    targetId: "invalid",
                }),
            });
            await expect(Security_1.default.release(releaseRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if securityId is invalid", async () => {
            releaseRequest = new request_1.ReleaseRequest({
                ...LockFixture_1.ReleaseRequestFixture.create({
                    securityId: "invalid",
                }),
            });
            await expect(Security_1.default.release(releaseRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
    describe("getLockedBalanceOf", () => {
        getLockedBalanceRequest = new request_1.GetLockedBalanceRequest(LockFixture_1.GetLockedBalanceRequestFixture.create());
        const expectedResponse = {
            payload: new BigDecimal_1.default(BigInt(0)),
        };
        it("should get locked balance of successfully", async () => {
            queryBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await Security_1.default.getLockedBalanceOf(getLockedBalanceRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("GetLockedBalanceRequest", getLockedBalanceRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new LockedBalanceOfQuery_1.LockedBalanceOfQuery(getLockedBalanceRequest.securityId, getLockedBalanceRequest.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.getLockedBalanceOf(getLockedBalanceRequest)).rejects.toThrow("Query execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("GetLockedBalanceRequest", getLockedBalanceRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new LockedBalanceOfQuery_1.LockedBalanceOfQuery(getLockedBalanceRequest.securityId, getLockedBalanceRequest.targetId));
        });
        it("should throw error if securityId is invalid", async () => {
            getLockedBalanceRequest = new request_1.GetLockedBalanceRequest({
                ...LockFixture_1.GetLockedBalanceRequestFixture.create({
                    securityId: "invalid",
                }),
            });
            await expect(Security_1.default.release(releaseRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if targetId is invalid", async () => {
            getLockedBalanceRequest = new request_1.GetLockedBalanceRequest({
                ...LockFixture_1.GetLockedBalanceRequestFixture.create({
                    targetId: "invalid",
                }),
            });
            await expect(Security_1.default.release(releaseRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
    describe("getLockCount", () => {
        getLockCountRequest = new request_1.GetLockCountRequest(LockFixture_1.GetLockCountRequestFixture.create());
        const expectedResponse = {
            payload: 1,
        };
        it("should get lock count successfully", async () => {
            queryBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await Security_1.default.getLockCount(getLockCountRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("GetLockCountRequest", getLockCountRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new LockCountQuery_1.LockCountQuery(getLockCountRequest.securityId, getLockCountRequest.targetId));
            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.getLockCount(getLockCountRequest)).rejects.toThrow("Query execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("GetLockCountRequest", getLockCountRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new LockCountQuery_1.LockCountQuery(getLockCountRequest.securityId, getLockCountRequest.targetId));
        });
        it("should throw error if securityId is invalid", async () => {
            getLockCountRequest = new request_1.GetLockCountRequest({
                ...LockFixture_1.GetLockCountRequestFixture.create({
                    securityId: "invalid",
                }),
            });
            await expect(Security_1.default.getLockCount(getLockCountRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if targetId is invalid", async () => {
            getLockCountRequest = new request_1.GetLockCountRequest({
                ...LockFixture_1.GetLockCountRequestFixture.create({
                    targetId: "invalid",
                }),
            });
            await expect(Security_1.default.getLockCount(getLockCountRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
    describe("getLocksId", () => {
        getLocksIdRequest = new request_1.GetLocksIdRequest(LockFixture_1.GetLocksIdRequestFixture.create());
        const expectedResponse = {
            payload: [BigInt(1)],
        };
        it("should get locks id successfully", async () => {
            queryBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await Security_1.default.getLocksId(getLocksIdRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("GetLocksIdRequest", getLocksIdRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new LocksIdQuery_1.LocksIdQuery(getLocksIdRequest.securityId, getLocksIdRequest.targetId, getLocksIdRequest.start, getLocksIdRequest.end));
            expect(result).toEqual([expectedResponse.payload[0].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.getLocksId(getLocksIdRequest)).rejects.toThrow("Query execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("GetLocksIdRequest", getLocksIdRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new LocksIdQuery_1.LocksIdQuery(getLocksIdRequest.securityId, getLocksIdRequest.targetId, getLocksIdRequest.start, getLocksIdRequest.end));
        });
        it("should throw error if securityId is invalid", async () => {
            getLocksIdRequest = new request_1.GetLocksIdRequest({
                ...LockFixture_1.GetLocksIdRequestFixture.create({
                    securityId: "invalid",
                }),
            });
            await expect(Security_1.default.getLockCount(getLockCountRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if targetId is invalid", async () => {
            getLocksIdRequest = new request_1.GetLocksIdRequest({
                ...LockFixture_1.GetLocksIdRequestFixture.create({
                    targetId: "invalid",
                }),
            });
            await expect(Security_1.default.getLockCount(getLockCountRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
    describe("getLock", () => {
        getLockRequest = new request_1.GetLockRequest(LockFixture_1.GetLockRequestFixture.create());
        const expectedResponse = {
            payload: LockFixture_1.LockFixture.create(),
        };
        it("should get lock successfully", async () => {
            queryBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await Security_1.default.getLock(getLockRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("GetLockRequest", getLockRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetLockQuery_1.GetLockQuery(getLockRequest.securityId, getLockRequest.targetId, getLockRequest.id));
            expect(result).toEqual(expect.objectContaining({
                id: expectedResponse.payload.id,
                amount: expectedResponse.payload.amount.toString(),
                expirationDate: expectedResponse.payload.expiredTimestamp.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.getLock(getLockRequest)).rejects.toThrow("Query execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("GetLockRequest", getLockRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetLockQuery_1.GetLockQuery(getLockRequest.securityId, getLockRequest.targetId, getLockRequest.id));
        });
        it("should throw error if securityId is invalid", async () => {
            getLockRequest = new request_1.GetLockRequest({
                ...LockFixture_1.GetLockRequestFixture.create({
                    securityId: "invalid",
                }),
            });
            await expect(Security_1.default.getLockCount(getLockCountRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if targetId is invalid", async () => {
            getLockRequest = new request_1.GetLockRequest({
                ...LockFixture_1.GetLockRequestFixture.create({
                    targetId: "invalid",
                }),
            });
            await expect(Security_1.default.getLockCount(getLockCountRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
});
