// SPDX-License-Identifier: Apache-2.0
import { createMock } from "@golevelup/ts-jest";
import { ErrorMsgFixture, EvmAddressPropsFixture, HederaIdPropsFixture, TransactionIdFixture, } from "../../../../../../../__tests__/fixtures/shared/DataFixture";
import EvmAddress from "../../../../../../domain/context/contract/EvmAddress";
import Account from "../../../../../../domain/context/account/Account";
import { SecurityRole } from "../../../../../../domain/context/security/SecurityRole";
import { SetNominalValueCommandResponse } from "./SetNominalValueCommand";
import { SetNominalValueCommandHandler } from "./SetNominalValueCommandHandler";
import { SetNominalValueCommandFixture } from "../../../../../../../__tests__/fixtures/nominalValue/NominalValueFixture";
import { SetNominalValueCommandError } from "./error/SetNominalValueCommandError";
import { ErrorCode } from "../../../../../../core/error/BaseError";
import { SecurityPaused } from "../../../../../../domain/context/security/error/operations/SecurityPaused";
import { NotGrantedRole } from "../../../../../../domain/context/security/error/operations/NotGrantedRole";
describe("SetNominalValueCommandHandler", () => {
    let handler;
    let command;
    const transactionServiceMock = createMock();
    const validationServiceMock = createMock();
    const accountServiceMock = createMock();
    const contractServiceMock = createMock();
    const evmAddress = new EvmAddress(EvmAddressPropsFixture.create().value);
    const account = new Account({
        id: HederaIdPropsFixture.create().value,
        evmAddress: EvmAddressPropsFixture.create().value,
    });
    const transactionId = TransactionIdFixture.create().id;
    const errorMsg = ErrorMsgFixture.create().msg;
    beforeEach(() => {
        handler = new SetNominalValueCommandHandler(accountServiceMock, transactionServiceMock, validationServiceMock, contractServiceMock);
        command = SetNominalValueCommandFixture.create();
    });
    afterEach(() => {
        jest.resetAllMocks();
    });
    describe("execute", () => {
        it("throws SetNominalValueCommandError when security is paused", async () => {
            const fakeError = new SecurityPaused();
            contractServiceMock.getContractEvmAddress.mockResolvedValueOnce(evmAddress);
            accountServiceMock.getCurrentAccount.mockReturnValue(account);
            validationServiceMock.checkPause.mockRejectedValue(fakeError);
            const resultPromise = handler.execute(command);
            await expect(resultPromise).rejects.toBeInstanceOf(SetNominalValueCommandError);
            await expect(resultPromise).rejects.toMatchObject({
                message: expect.stringContaining(`An error occurred while setting nominal value: ${fakeError.message}`),
                errorCode: ErrorCode.SecurityPaused,
            });
            expect(validationServiceMock.checkPause).toHaveBeenCalledWith(command.securityId);
        });
        it("throws SetNominalValueCommandError when account does not have NOMINAL_VALUE_ROLE", async () => {
            const fakeError = new NotGrantedRole(SecurityRole._NOMINAL_VALUE_ROLE);
            contractServiceMock.getContractEvmAddress.mockResolvedValueOnce(evmAddress);
            accountServiceMock.getCurrentAccount.mockReturnValue(account);
            validationServiceMock.checkPause.mockResolvedValue(undefined);
            validationServiceMock.checkRole.mockRejectedValue(fakeError);
            const resultPromise = handler.execute(command);
            await expect(resultPromise).rejects.toBeInstanceOf(SetNominalValueCommandError);
            await expect(resultPromise).rejects.toMatchObject({
                message: expect.stringContaining(`An error occurred while setting nominal value: ${fakeError.message}`),
                errorCode: ErrorCode.RoleNotAssigned,
            });
            expect(validationServiceMock.checkRole).toHaveBeenCalledWith(SecurityRole._NOMINAL_VALUE_ROLE, account.id.toString(), command.securityId);
        });
        it("throws SetNominalValueCommandError when command fails with uncaught error", async () => {
            const fakeError = new Error(errorMsg);
            contractServiceMock.getContractEvmAddress.mockRejectedValue(fakeError);
            const resultPromise = handler.execute(command);
            await expect(resultPromise).rejects.toBeInstanceOf(SetNominalValueCommandError);
            await expect(resultPromise).rejects.toMatchObject({
                message: expect.stringContaining(`An error occurred while setting nominal value: ${errorMsg}`),
                errorCode: ErrorCode.UncaughtCommandError,
            });
        });
        it("should successfully set nominal value", async () => {
            contractServiceMock.getContractEvmAddress.mockResolvedValueOnce(evmAddress);
            accountServiceMock.getCurrentAccount.mockReturnValue(account);
            validationServiceMock.checkPause.mockResolvedValue(undefined);
            validationServiceMock.checkRole.mockResolvedValue(undefined);
            transactionServiceMock.getHandler().setNominalValue.mockResolvedValue({
                id: transactionId,
            });
            const result = await handler.execute(command);
            expect(result).toBeInstanceOf(SetNominalValueCommandResponse);
            expect(result.payload).toBe(true);
            expect(result.transactionId).toBe(transactionId);
            expect(contractServiceMock.getContractEvmAddress).toHaveBeenCalledTimes(1);
            expect(validationServiceMock.checkPause).toHaveBeenCalledTimes(1);
            expect(validationServiceMock.checkRole).toHaveBeenCalledTimes(1);
            expect(accountServiceMock.getCurrentAccount).toHaveBeenCalledTimes(1);
            expect(transactionServiceMock.getHandler().setNominalValue).toHaveBeenCalledTimes(1);
            expect(validationServiceMock.checkPause).toHaveBeenCalledWith(command.securityId);
            expect(validationServiceMock.checkRole).toHaveBeenCalledWith(SecurityRole._NOMINAL_VALUE_ROLE, account.id.toString(), command.securityId);
            expect(contractServiceMock.getContractEvmAddress).toHaveBeenCalledWith(command.securityId);
            expect(transactionServiceMock.getHandler().setNominalValue).toHaveBeenCalledWith(evmAddress, command.nominalValue, command.nominalValueDecimals, command.securityId);
        });
    });
});
