"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 TransactionService_1 = __importDefault(require("../../../../service/transaction/TransactionService"));
const ConnectCommandHandler_1 = require("./ConnectCommandHandler");
const ConnectCommand_1 = require("./ConnectCommand");
const NetworkFixture_1 = require("../../../../../../__tests__/fixtures/network/NetworkFixture");
const Account_1 = __importDefault(require("../../../../../domain/context/account/Account"));
const DataFixture_1 = require("../../../../../../__tests__/fixtures/shared/DataFixture");
const ConnectCommandError_1 = require("./error/ConnectCommandError");
const BaseError_1 = require("../../../../../core/error/BaseError");
describe("ConnectCommandHandler", () => {
    let handler;
    let command;
    const transactionAdapterMock = (0, ts_jest_1.createMock)();
    const account = new Account_1.default(DataFixture_1.AccountPropsFixture.create());
    const errorMsg = DataFixture_1.ErrorMsgFixture.create().msg;
    beforeEach(() => {
        handler = new ConnectCommandHandler_1.ConnectCommandHandler();
        command = NetworkFixture_1.ConnectCommandFixture.create();
        transactionAdapterMock.register.mockResolvedValue({ account });
        jest.spyOn(TransactionService_1.default, "getHandlerClass").mockReturnValue(transactionAdapterMock);
    });
    afterEach(() => {
        jest.resetAllMocks();
    });
    describe("execute", () => {
        describe("error cases", () => {
            it("throws ConnectCommandError when command fails with uncaught error", async () => {
                const fakeError = new Error(errorMsg);
                transactionAdapterMock.register.mockRejectedValue(fakeError);
                const resultPromise = handler.execute(command);
                await expect(resultPromise).rejects.toBeInstanceOf(ConnectCommandError_1.ConnectCommandError);
                await expect(resultPromise).rejects.toMatchObject({
                    message: expect.stringContaining(`An error occurred while connecting to network: ${errorMsg}`),
                    errorCode: BaseError_1.ErrorCode.UncaughtCommandError,
                });
            });
        });
        describe("success cases", () => {
            it("should successfully connect with custodial settings", async () => {
                command = NetworkFixture_1.ConnectCommandFixture.omit("HWCSettings").create();
                const result = await handler.execute(command);
                expect(result).toBeInstanceOf(ConnectCommand_1.ConnectCommandResponse);
                expect(transactionAdapterMock.register).toHaveBeenCalledTimes(1);
                expect(transactionAdapterMock.register).toHaveBeenCalledWith(command.custodialSettings, false);
                expect(result.walletType).toEqual(command.wallet);
                expect(result.payload).toEqual({ account });
            });
            it("should successfully connect with HWC settings", async () => {
                command = NetworkFixture_1.ConnectCommandFixture.omit("custodialSettings").create();
                const result = await handler.execute(command);
                expect(result).toBeInstanceOf(ConnectCommand_1.ConnectCommandResponse);
                expect(transactionAdapterMock.register).toHaveBeenCalledTimes(1);
                expect(transactionAdapterMock.register).toHaveBeenCalledWith(command.HWCSettings, false);
                expect(result.walletType).toEqual(command.wallet);
                expect(result.payload).toEqual({ account });
            });
            it("should successfully connect without custodial settings and HWC", async () => {
                command = NetworkFixture_1.ConnectCommandFixture.omit("HWCSettings").create();
                command = { ...command, custodialSettings: undefined };
                const result = await handler.execute(command);
                expect(result).toBeInstanceOf(ConnectCommand_1.ConnectCommandResponse);
                expect(transactionAdapterMock.register).toHaveBeenCalledTimes(1);
                expect(transactionAdapterMock.register).toHaveBeenCalledWith(command.account, false);
                expect(result.walletType).toEqual(command.wallet);
                expect(result.payload).toEqual({ account });
            });
        });
    });
});
