"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 IssueFixture_1 = require("../../../../../__tests__/fixtures/issue/IssueFixture");
const IssueCommand_1 = require("../../../../app/usecase/command/security/operations/issue/IssueCommand");
const BatchMintCommand_1 = require("../../../../app/usecase/command/security/operations/batch/batchMint/BatchMintCommand");
const MintCommand_1 = require("../../../../app/usecase/command/security/operations/mint/MintCommand");
const BatchFixture_1 = require("../../../../../__tests__/fixtures/batch/BatchFixture");
const MintFixture_1 = require("../../../../../__tests__/fixtures/mint/MintFixture");
describe("Issue", () => {
    let commandBusMock;
    let queryBusMock;
    let mirrorNodeMock;
    let issueRequest;
    let mintRequest;
    let batchMintRequest;
    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("issue", () => {
        issueRequest = new request_1.IssueRequest(IssueFixture_1.IssueRequestFixture.create());
        const expectedResponse = {
            payload: true,
            transactionId: transactionId,
        };
        it("should issue successfully", async () => {
            commandBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await Security_1.default.issue(issueRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("IssueRequest", issueRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new IssueCommand_1.IssueCommand(issueRequest.amount, issueRequest.targetId, issueRequest.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.issue(issueRequest)).rejects.toThrow("Command execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("IssueRequest", issueRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new IssueCommand_1.IssueCommand(issueRequest.amount, issueRequest.targetId, issueRequest.securityId));
        });
        it("should throw error if securityId is invalid", async () => {
            issueRequest = new request_1.IssueRequest({
                ...IssueFixture_1.IssueRequestFixture.create({
                    securityId: "invalid",
                }),
            });
            await expect(Security_1.default.issue(issueRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if targetId is invalid", async () => {
            issueRequest = new request_1.IssueRequest({
                ...IssueFixture_1.IssueRequestFixture.create({
                    targetId: "invalid",
                }),
            });
            await expect(Security_1.default.issue(issueRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if amount is invalid", async () => {
            issueRequest = new request_1.IssueRequest({
                ...IssueFixture_1.IssueRequestFixture.create({
                    amount: "invalid",
                }),
            });
            await expect(Security_1.default.issue(issueRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
    describe("mint", () => {
        mintRequest = new request_1.MintRequest(MintFixture_1.MintRequestFixture.create());
        const expectedResponse = {
            payload: true,
            transactionId: transactionId,
        };
        it("should mint successfully", async () => {
            commandBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await Security_1.default.mint(mintRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("MintRequest", mintRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new MintCommand_1.MintCommand(mintRequest.securityId, mintRequest.targetId, mintRequest.amount));
            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.mint(mintRequest)).rejects.toThrow("Command execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("MintRequest", mintRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new MintCommand_1.MintCommand(mintRequest.securityId, mintRequest.targetId, mintRequest.amount));
        });
        it("should throw error if securityId is invalid", async () => {
            mintRequest = new request_1.MintRequest({
                ...MintFixture_1.MintRequestFixture.create({
                    securityId: "invalid",
                }),
            });
            await expect(Security_1.default.mint(mintRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if targetId is invalid", async () => {
            mintRequest = new request_1.MintRequest({
                ...MintFixture_1.MintRequestFixture.create({
                    targetId: "invalid",
                }),
            });
            await expect(Security_1.default.mint(mintRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if amount is invalid", async () => {
            mintRequest = new request_1.MintRequest({
                ...MintFixture_1.MintRequestFixture.create({
                    amount: "invalid",
                }),
            });
            await expect(Security_1.default.mint(mintRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
    describe("BatchMint", () => {
        batchMintRequest = new request_1.BatchMintRequest(BatchFixture_1.BatchMintRequestFixture.create());
        const expectedResponse = new BatchMintCommand_1.BatchMintResponse(true, transactionId);
        it("should batch mint sucessfully", async () => {
            commandBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await Security_1.default.batchMint(batchMintRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("BatchMintRequest", batchMintRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new BatchMintCommand_1.BatchMintCommand(batchMintRequest.securityId, batchMintRequest.amountList, batchMintRequest.toList));
            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.batchMint(batchMintRequest)).rejects.toThrow("Command execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("BatchMintRequest", batchMintRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new BatchMintCommand_1.BatchMintCommand(batchMintRequest.securityId, batchMintRequest.amountList, batchMintRequest.toList));
        });
        it("should throw error if securityId is invalid", async () => {
            batchMintRequest = new request_1.BatchMintRequest({
                ...BatchFixture_1.BatchMintRequestFixture.create({
                    securityId: "invalid",
                }),
            });
            await expect(Security_1.default.batchMint(batchMintRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if amountList is empty", async () => {
            batchMintRequest = new request_1.BatchMintRequest({
                ...BatchFixture_1.BatchMintRequestFixture.create({
                    amountList: [],
                }),
            });
            await expect(Security_1.default.batchMint(batchMintRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if toList is empty", async () => {
            batchMintRequest = new request_1.BatchMintRequest({
                ...BatchFixture_1.BatchMintRequestFixture.create({
                    toList: [],
                }),
            });
            await expect(Security_1.default.batchMint(batchMintRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if list lengths are not equal", async () => {
            batchMintRequest = new request_1.BatchMintRequest({
                ...BatchFixture_1.BatchMintRequestFixture.create({
                    toList: [DataFixture_1.HederaIdPropsFixture.create().value, DataFixture_1.HederaIdPropsFixture.create().value],
                }),
            });
            await expect(Security_1.default.batchMint(batchMintRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
});
