// SPDX-License-Identifier: Apache-2.0
import { createMock } from "@golevelup/ts-jest";
import { AccountPropsFixture, ErrorMsgFixture, EvmAddressPropsFixture, TransactionIdFixture, } from "../../../../../../../../__tests__/fixtures/shared/DataFixture";
import EvmAddress from "../../../../../../../domain/context/contract/EvmAddress";
import { ClearingCreateHoldByPartitionCommandFixture } from "../../../../../../../../__tests__/fixtures/clearing/ClearingFixture";
import { SecurityPropsFixture } from "../../../../../../../../__tests__/fixtures/shared/SecurityFixture";
import { Security } from "../../../../../../../domain/context/security/Security";
import BigDecimal from "../../../../../../../domain/context/shared/BigDecimal";
import { faker } from "@faker-js/faker/.";
import { OperatorClearingCreateHoldByPartitionCommandHandler } from "./OperatorClearingCreateHoldByPartitionCommandHandler";
import { OperatorClearingCreateHoldByPartitionCommandResponse, } from "./OperatorClearingCreateHoldByPartitionCommand";
import Account from "../../../../../../../domain/context/account/Account";
import { OperatorClearingCreateHoldByPartitionCommandError } from "./error/OperatorClearingCreateHoldByPartitionCommandError";
import { ErrorCode } from "../../../../../../../core/error/BaseError";
describe("ClearingTransferFromByPartitionCommandHandler", () => {
    let handler;
    let command;
    const transactionServiceMock = createMock();
    const validationServiceMock = createMock();
    const accountServiceMock = createMock();
    const contractServiceMock = createMock();
    const securityServiceMock = createMock();
    const evmAddress = new EvmAddress(EvmAddressPropsFixture.create().value);
    const transactionId = TransactionIdFixture.create().id;
    const security = new Security(SecurityPropsFixture.create());
    const account = new Account(AccountPropsFixture.create());
    const errorMsg = ErrorMsgFixture.create().msg;
    const clearingId = faker.string.hexadecimal({
        length: 64,
        prefix: "0x",
    });
    beforeEach(() => {
        handler = new OperatorClearingCreateHoldByPartitionCommandHandler(securityServiceMock, accountServiceMock, transactionServiceMock, validationServiceMock, contractServiceMock);
        const commandRaw = ClearingCreateHoldByPartitionCommandFixture.create();
        /* eslint-disable-next-line @typescript-eslint/no-unused-vars */
        const { deadline, nonce, signature, ...commandFiltered } = commandRaw;
        command = commandFiltered;
    });
    afterEach(() => {
        jest.resetAllMocks();
    });
    describe("execute", () => {
        describe("error cases", () => {
            it("throws OperatorClearingCreateHoldByPartitionCommandError 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(OperatorClearingCreateHoldByPartitionCommandError);
                await expect(resultPromise).rejects.toMatchObject({
                    message: expect.stringContaining(`An error occurred while executing operator clearing create hold operation: ${errorMsg}`),
                    errorCode: ErrorCode.UncaughtCommandError,
                });
            });
        });
        describe("success cases", () => {
            it("should successfully create clearing create hold by operator", async () => {
                contractServiceMock.getContractEvmAddress.mockResolvedValue(evmAddress);
                accountServiceMock.getAccountEvmAddress.mockResolvedValue(evmAddress);
                accountServiceMock.getAccountEvmAddressOrNull.mockResolvedValue(evmAddress);
                accountServiceMock.getCurrentAccount.mockReturnValue(account);
                securityServiceMock.get.mockResolvedValue(security);
                transactionServiceMock.getHandler().operatorClearingCreateHoldByPartition.mockResolvedValue({
                    id: transactionId,
                });
                transactionServiceMock.getTransactionResult.mockResolvedValue(clearingId);
                const result = await handler.execute(command);
                expect(result).toBeInstanceOf(OperatorClearingCreateHoldByPartitionCommandResponse);
                expect(result.payload).toBe(parseInt(clearingId));
                expect(result.transactionId).toBe(transactionId);
                expect(contractServiceMock.getContractEvmAddress).toHaveBeenCalledTimes(1);
                expect(contractServiceMock.getContractEvmAddress).toHaveBeenNthCalledWith(1, command.securityId);
                expect(accountServiceMock.getAccountEvmAddress).toHaveBeenCalledTimes(2);
                expect(accountServiceMock.getAccountEvmAddress).toHaveBeenNthCalledWith(1, command.escrowId);
                expect(accountServiceMock.getAccountEvmAddress).toHaveBeenNthCalledWith(2, command.sourceId);
                expect(accountServiceMock.getAccountEvmAddressOrNull).toHaveBeenCalledTimes(1);
                expect(accountServiceMock.getAccountEvmAddressOrNull).toHaveBeenNthCalledWith(1, command.targetId);
                expect(validationServiceMock.checkOperator).toHaveBeenCalledTimes(1);
                expect(validationServiceMock.checkOperator).toHaveBeenCalledWith(command.securityId, command.partitionId, account.id.toString(), command.sourceId);
                expect(validationServiceMock.checkBalance).toHaveBeenCalledTimes(1);
                expect(validationServiceMock.checkBalance).toHaveBeenCalledWith(command.securityId, command.sourceId, BigDecimal.fromString(command.amount, security.decimals));
                expect(validationServiceMock.checkDecimals).toHaveBeenCalledTimes(1);
                expect(validationServiceMock.checkDecimals).toHaveBeenCalledWith(security, command.amount);
                expect(validationServiceMock.checkPause).toHaveBeenCalledTimes(1);
                expect(validationServiceMock.checkPause).toHaveBeenCalledWith(command.securityId);
                expect(validationServiceMock.checkClearingActivated).toHaveBeenCalledTimes(1);
                expect(validationServiceMock.checkClearingActivated).toHaveBeenCalledWith(command.securityId);
                expect(transactionServiceMock.getHandler().operatorClearingCreateHoldByPartition).toHaveBeenCalledTimes(1);
                expect(transactionServiceMock.getHandler().operatorClearingCreateHoldByPartition).toHaveBeenCalledWith(evmAddress, command.partitionId, evmAddress, BigDecimal.fromString(command.amount, security.decimals), evmAddress, evmAddress, BigDecimal.fromString(command.clearingExpirationDate.substring(0, 10)), BigDecimal.fromString(command.holdExpirationDate.substring(0, 10)), command.securityId);
                expect(transactionServiceMock.getTransactionResult).toHaveBeenCalledTimes(1);
                expect(transactionServiceMock.getTransactionResult).toHaveBeenCalledWith({
                    res: { id: transactionId },
                    className: OperatorClearingCreateHoldByPartitionCommandHandler.name,
                    position: 1,
                    numberOfResultsItems: 2,
                });
            });
        });
    });
});
