// 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 { ErrorCode } from "../../../../../../core/error/BaseError";
import { TransferCommandFixture } from "../../../../../../../__tests__/fixtures/transfer/TransferFixture";
import { Security } from "../../../../../../domain/context/security/Security";
import { SecurityPropsFixture } from "../../../../../../../__tests__/fixtures/shared/SecurityFixture";
import BigDecimal from "../../../../../../domain/context/shared/BigDecimal";
import { TransferCommandHandler } from "./TransferCommandHandler";
import { TransferCommandResponse } from "./TransferCommand";
import { TransferCommandError } from "./error/TransferCommandError";
import Account from "../../../../../../domain/context/account/Account";
describe("TransferCommandHandler", () => {
    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 errorMsg = ErrorMsgFixture.create().msg;
    const security = new Security(SecurityPropsFixture.create());
    const account = new Account(AccountPropsFixture.create());
    beforeEach(() => {
        handler = new TransferCommandHandler(securityServiceMock, transactionServiceMock, accountServiceMock, validationServiceMock, contractServiceMock);
        const commandRaw = TransferCommandFixture.create();
        /* eslint-disable-next-line @typescript-eslint/no-unused-vars */
        const { deadline, nounce, signature, sourceId, ...commandFiltered } = commandRaw;
        command = commandFiltered;
    });
    afterEach(() => {
        jest.resetAllMocks();
    });
    describe("execute", () => {
        describe("error cases", () => {
            it("throws TransferCommandError 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(TransferCommandError);
                await expect(resultPromise).rejects.toMatchObject({
                    message: expect.stringContaining(`An error occurred while transferring tokens: ${errorMsg}`),
                    errorCode: ErrorCode.UncaughtCommandError,
                });
            });
        });
        describe("success cases", () => {
            it("should successfully transfer", async () => {
                contractServiceMock.getContractEvmAddress.mockResolvedValue(evmAddress);
                accountServiceMock.getAccountEvmAddress.mockResolvedValue(evmAddress);
                accountServiceMock.getCurrentAccount.mockReturnValue(account);
                securityServiceMock.get.mockResolvedValue(security);
                transactionServiceMock.getHandler().transfer.mockResolvedValue({
                    id: transactionId,
                });
                const result = await handler.execute(command);
                expect(result).toBeInstanceOf(TransferCommandResponse);
                expect(result.transactionId).toBe(transactionId);
                expect(contractServiceMock.getContractEvmAddress).toHaveBeenCalledTimes(1);
                expect(contractServiceMock.getContractEvmAddress).toHaveBeenCalledWith(command.securityId);
                expect(accountServiceMock.getAccountEvmAddress).toHaveBeenCalledTimes(1);
                expect(accountServiceMock.getAccountEvmAddress).toHaveBeenNthCalledWith(1, command.targetId);
                expect(validationServiceMock.checkCanTransfer).toHaveBeenCalledTimes(1);
                expect(validationServiceMock.checkCanTransfer).toHaveBeenCalledWith(command.securityId, command.targetId, command.amount, account.id.toString());
                expect(transactionServiceMock.getHandler().transfer).toHaveBeenCalledTimes(1);
                expect(transactionServiceMock.getHandler().transfer).toHaveBeenCalledWith(evmAddress, evmAddress, BigDecimal.fromString(command.amount, security.decimals), command.securityId);
            });
        });
    });
});
