// 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 Account from "../../../../../../domain/context/account/Account";
import { SecurityRole } from "../../../../../../domain/context/security/SecurityRole";
import { RevokeKycCommandFixture } from "../../../../../../../__tests__/fixtures/kyc/KycFixture";
import { RevokeKycCommandHandler } from "./RevokeKycCommandHandler";
import { RevokeKycCommandResponse } from "./RevokeKycCommand";
import { RevokeKycCommandError } from "./error/RevokeKycCommandError";
import { ErrorCode } from "../../../../../../core/error/BaseError";
describe("RevokeKycCommandHandler", () => {
    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(AccountPropsFixture.create());
    const transactionId = TransactionIdFixture.create().id;
    const errorMsg = ErrorMsgFixture.create().msg;
    beforeEach(() => {
        handler = new RevokeKycCommandHandler(accountServiceMock, contractServiceMock, transactionServiceMock, validationServiceMock);
        command = RevokeKycCommandFixture.create();
    });
    afterEach(() => {
        jest.resetAllMocks();
    });
    describe("execute", () => {
        describe("error cases", () => {
            it("throws RevokeKycCommandError 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(RevokeKycCommandError);
                await expect(resultPromise).rejects.toMatchObject({
                    message: expect.stringContaining(`An error occurred while revoking KYC: ${errorMsg}`),
                    errorCode: ErrorCode.UncaughtCommandError,
                });
            });
        });
        describe("success cases", () => {
            it("should successfully revoke kyc", async () => {
                accountServiceMock.getCurrentAccount.mockReturnValue(account);
                contractServiceMock.getContractEvmAddress.mockResolvedValue(evmAddress);
                accountServiceMock.getAccountEvmAddress.mockResolvedValue(evmAddress);
                transactionServiceMock.getHandler().revokeKyc.mockResolvedValue({
                    id: transactionId,
                });
                const result = await handler.execute(command);
                expect(result).toBeInstanceOf(RevokeKycCommandResponse);
                expect(result.payload).toBe(true);
                expect(result.transactionId).toBe(transactionId);
                expect(contractServiceMock.getContractEvmAddress).toHaveBeenCalledTimes(1);
                expect(accountServiceMock.getAccountEvmAddress).toHaveBeenCalledTimes(1);
                expect(transactionServiceMock.getHandler().revokeKyc).toHaveBeenCalledTimes(1);
                expect(validationServiceMock.checkPause).toHaveBeenCalledWith(command.securityId);
                expect(validationServiceMock.checkRole).toHaveBeenCalledWith(SecurityRole._KYC_ROLE, account.id.toString(), command.securityId);
                expect(contractServiceMock.getContractEvmAddress).toHaveBeenNthCalledWith(1, command.securityId);
                expect(transactionServiceMock.getHandler().revokeKyc).toHaveBeenCalledWith(evmAddress, evmAddress, command.securityId);
            });
        });
    });
});
