"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 ERC1400Fixture_1 = require("../../../../../__tests__/fixtures/erc1400/ERC1400Fixture");
const SetMaxSupplyCommand_1 = require("../../../../app/usecase/command/security/operations/cap/SetMaxSupplyCommand");
const GetMaxSupplyQuery_1 = require("../../../../app/usecase/query/security/cap/getMaxSupply/GetMaxSupplyQuery");
describe("Supply", () => {
    let commandBusMock;
    let queryBusMock;
    let mirrorNodeMock;
    let setMaxSupplyRequest;
    let getMaxSupplyRequest;
    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("setMaxSupply", () => {
        setMaxSupplyRequest = new request_1.SetMaxSupplyRequest(ERC1400Fixture_1.SetMaxSupplyRequestFixture.create());
        const expectedResponse = {
            payload: true,
            transactionId: transactionId,
        };
        it("should set max supply successfully", async () => {
            commandBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await Security_1.default.setMaxSupply(setMaxSupplyRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("SetMaxSupplyRequest", setMaxSupplyRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new SetMaxSupplyCommand_1.SetMaxSupplyCommand(setMaxSupplyRequest.maxSupply, setMaxSupplyRequest.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.setMaxSupply(setMaxSupplyRequest)).rejects.toThrow("Command execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("SetMaxSupplyRequest", setMaxSupplyRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new SetMaxSupplyCommand_1.SetMaxSupplyCommand(setMaxSupplyRequest.maxSupply, setMaxSupplyRequest.securityId));
        });
        it("should throw error if securityId is invalid", async () => {
            setMaxSupplyRequest = new request_1.SetMaxSupplyRequest({
                ...ERC1400Fixture_1.SetMaxSupplyRequestFixture.create({
                    securityId: "invalid",
                }),
            });
            await expect(Security_1.default.setMaxSupply(setMaxSupplyRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if maxSupply is invalid", async () => {
            setMaxSupplyRequest = new request_1.SetMaxSupplyRequest({
                ...ERC1400Fixture_1.SetMaxSupplyRequestFixture.create({
                    maxSupply: "invalid",
                }),
            });
            await expect(Security_1.default.setMaxSupply(setMaxSupplyRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
    describe("getMaxSupply", () => {
        getMaxSupplyRequest = new request_1.GetMaxSupplyRequest(ERC1400Fixture_1.GetMaxSupplyRequestFixture.create());
        const expectedResponse = {
            payload: new BigDecimal_1.default(BigInt(1)),
        };
        it("should get max supply successfully", async () => {
            queryBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await Security_1.default.getMaxSupply(getMaxSupplyRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("GetMaxSupplyRequest", getMaxSupplyRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetMaxSupplyQuery_1.GetMaxSupplyQuery(getMaxSupplyRequest.securityId));
            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.getMaxSupply(getMaxSupplyRequest)).rejects.toThrow("Query execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("GetMaxSupplyRequest", getMaxSupplyRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetMaxSupplyQuery_1.GetMaxSupplyQuery(getMaxSupplyRequest.securityId));
        });
        it("should throw error if securityId is invalid", async () => {
            getMaxSupplyRequest = new request_1.GetMaxSupplyRequest({
                ...ERC1400Fixture_1.GetMaxSupplyRequestFixture.create({
                    securityId: "invalid",
                }),
            });
            await expect(Security_1.default.getMaxSupply(getMaxSupplyRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
});
