"use strict";
// SPDX-License-Identifier: Apache-2.0
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const ts_jest_1 = require("@golevelup/ts-jest");
const DataFixture_1 = require("../../../../../../../../__tests__/fixtures/shared/DataFixture");
const CreateExternalBlackListMockCommandHandler_js_1 = require("./CreateExternalBlackListMockCommandHandler.js");
const CreateExternalBlackListMockCommand_js_1 = require("./CreateExternalBlackListMockCommand.js");
const Account_1 = __importDefault(require("../../../../../../../domain/context/account/Account"));
const CreateExternalBlackListMockCommandError_js_1 = require("./error/CreateExternalBlackListMockCommandError.js");
const BaseError_1 = require("../../../../../../../core/error/BaseError");
describe("CreateExternalBlackListMockCommandHandler", () => {
    let handler;
    const transactionServiceMock = (0, ts_jest_1.createMock)();
    const mirrorNodeAdapterMock = (0, ts_jest_1.createMock)();
    const account = new Account_1.default({
        id: DataFixture_1.HederaIdPropsFixture.create().value,
        evmAddress: DataFixture_1.EvmAddressPropsFixture.create().value,
    });
    const transactionId = DataFixture_1.TransactionIdFixture.create().id;
    const errorMsg = DataFixture_1.ErrorMsgFixture.create().msg;
    beforeEach(() => {
        handler = new CreateExternalBlackListMockCommandHandler_js_1.CreateExternalBlackListMockCommandHandler(mirrorNodeAdapterMock, transactionServiceMock);
    });
    afterEach(() => {
        jest.resetAllMocks();
    });
    describe("execute", () => {
        describe("error cases", () => {
            it("throws CreateExternalBlackListMockCommandError when command fails with uncaught error", async () => {
                const fakeError = new Error(errorMsg);
                transactionServiceMock.getHandler().createExternalBlackListMock.mockRejectedValue(fakeError);
                const resultPromise = handler.execute();
                await expect(resultPromise).rejects.toBeInstanceOf(CreateExternalBlackListMockCommandError_js_1.CreateExternalBlackListMockCommandError);
                await expect(resultPromise).rejects.toMatchObject({
                    message: expect.stringContaining(`An error occurred while creating external blacklist: ${errorMsg}`),
                    errorCode: BaseError_1.ErrorCode.UncaughtCommandError,
                });
            });
        });
        describe("success cases", () => {
            it("should successfully create black list mock if return an Id", async () => {
                mirrorNodeAdapterMock.getAccountInfo.mockResolvedValueOnce(account);
                transactionServiceMock.getHandler().createExternalBlackListMock.mockResolvedValue({
                    id: transactionId,
                });
                transactionServiceMock.getTransactionResult.mockResolvedValue(account.evmAddress);
                const result = await handler.execute();
                expect(result).toBeInstanceOf(CreateExternalBlackListMockCommand_js_1.CreateExternalBlackListMockCommandResponse);
                expect(result.payload).toBe(account.id.toString());
                expect(transactionServiceMock.getTransactionResult).toHaveBeenCalledTimes(1);
                expect(transactionServiceMock.getTransactionResult).toHaveBeenCalledWith({
                    res: { id: transactionId },
                    className: CreateExternalBlackListMockCommandHandler_js_1.CreateExternalBlackListMockCommandHandler.name,
                    position: 0,
                    numberOfResultsItems: 1,
                    isContractCreation: true,
                });
                expect(mirrorNodeAdapterMock.getAccountInfo).toHaveBeenCalledTimes(1);
                expect(transactionServiceMock.getHandler().createExternalBlackListMock).toHaveBeenCalledTimes(1);
                expect(mirrorNodeAdapterMock.getAccountInfo).toHaveBeenCalledWith(account.evmAddress);
                expect(transactionServiceMock.getHandler().createExternalBlackListMock).toHaveBeenCalledWith();
            });
            it("should successfully create black list mock if return an address", async () => {
                mirrorNodeAdapterMock.getAccountInfo.mockResolvedValueOnce(account);
                transactionServiceMock.getHandler().createExternalBlackListMock.mockResolvedValue(account.evmAddress);
                const result = await handler.execute();
                expect(result).toBeInstanceOf(CreateExternalBlackListMockCommand_js_1.CreateExternalBlackListMockCommandResponse);
                expect(result.payload).toBe(account.id.toString());
                expect(mirrorNodeAdapterMock.getAccountInfo).toHaveBeenCalledTimes(1);
                expect(transactionServiceMock.getHandler().createExternalBlackListMock).toHaveBeenCalledTimes(1);
                expect(mirrorNodeAdapterMock.getAccountInfo).toHaveBeenCalledWith(account.evmAddress);
                expect(transactionServiceMock.getHandler().createExternalBlackListMock).toHaveBeenCalledWith();
            });
        });
    });
});
