// SPDX-License-Identifier: Apache-2.0
import { createMock } from "@golevelup/ts-jest";
import { ErrorMsgFixture, EvmAddressPropsFixture, TransactionIdFixture } from "../../../../../../../__tests__/fixtures/shared/DataFixture";
import EvmAddress from "../../../../../../domain/context/contract/EvmAddress";
import { ErrorCode } from "../../../../../../core/error/BaseError";
import { GrantRoleCommandFixture } from "../../../../../../../__tests__/fixtures/role/RoleFixture";
import { RevokeRoleCommandHandler } from "./RevokeRoleCommandHandler";
import { RevokeRoleCommandResponse } from "./RevokeRoleCommand";
import { RevokeRoleCommandError } from "./error/RevokeRoleCommandError";
describe("RevokeRoleCommandHandler", () => {
    let handler;
    let command;
    const transactionServiceMock = createMock();
    const validationServiceMock = createMock();
    const accountServiceMock = createMock();
    const contractServiceMock = createMock();
    const evmAddress = new EvmAddress(EvmAddressPropsFixture.create().value);
    const transactionId = TransactionIdFixture.create().id;
    const errorMsg = ErrorMsgFixture.create().msg;
    beforeEach(() => {
        handler = new RevokeRoleCommandHandler(accountServiceMock, transactionServiceMock, validationServiceMock, contractServiceMock);
        command = GrantRoleCommandFixture.create();
    });
    afterEach(() => {
        jest.resetAllMocks();
    });
    describe("execute", () => {
        describe("error cases", () => {
            it("throws RevokeRoleCommandError 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(RevokeRoleCommandError);
                await expect(resultPromise).rejects.toMatchObject({
                    message: expect.stringContaining(`An error occurred while revoking roles: ${errorMsg}`),
                    errorCode: ErrorCode.UncaughtCommandError,
                });
            });
        });
        describe("success cases", () => {
            it("should successfully revoke role", async () => {
                contractServiceMock.getContractEvmAddress.mockResolvedValue(evmAddress);
                accountServiceMock.getAccountEvmAddress.mockResolvedValue(evmAddress);
                transactionServiceMock.getHandler().revokeRole.mockResolvedValue({
                    id: transactionId,
                });
                const result = await handler.execute(command);
                expect(result).toBeInstanceOf(RevokeRoleCommandResponse);
                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.checkPause).toHaveBeenCalledTimes(1);
                expect(validationServiceMock.checkPause).toHaveBeenCalledWith(command.securityId);
                expect(transactionServiceMock.getHandler().revokeRole).toHaveBeenCalledTimes(1);
                expect(transactionServiceMock.getHandler().revokeRole).toHaveBeenCalledWith(evmAddress, evmAddress, command.role, command.securityId);
            });
        });
    });
});
