"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 DisconnectCommandHandler_1 = require("./DisconnectCommandHandler");
const DisconnectCommand_1 = require("./DisconnectCommand");
const Injectable_1 = __importDefault(require("../../../../../core/injectable/Injectable"));
const DataFixture_1 = require("../../../../../../__tests__/fixtures/shared/DataFixture");
const DisconnectCommandError_1 = require("./error/DisconnectCommandError");
const BaseError_1 = require("../../../../../core/error/BaseError");
describe("DisconnectCommandHandler", () => {
    let handler;
    const transactionAdapterMock = (0, ts_jest_1.createMock)();
    const errorMsg = DataFixture_1.ErrorMsgFixture.create().msg;
    beforeEach(() => {
        handler = new DisconnectCommandHandler_1.DisconnectCommandHandler();
        transactionAdapterMock.stop.mockResolvedValue(true);
        jest.spyOn(Injectable_1.default, "resolveTransactionHandler").mockReturnValue(transactionAdapterMock);
    });
    afterEach(() => {
        jest.resetAllMocks();
    });
    describe("execute", () => {
        describe("error cases", () => {
            it("throws DisconnectCommandError when command fails with uncaught error", async () => {
                const fakeError = new Error(errorMsg);
                transactionAdapterMock.stop.mockRejectedValue(fakeError);
                const resultPromise = handler.execute();
                await expect(resultPromise).rejects.toBeInstanceOf(DisconnectCommandError_1.DisconnectCommandError);
                await expect(resultPromise).rejects.toMatchObject({
                    message: expect.stringContaining(`An error occurred while disconnecting from network: ${errorMsg}`),
                    errorCode: BaseError_1.ErrorCode.UncaughtCommandError,
                });
            });
        });
        describe("success cases", () => {
            it("should successfully disconnect", async () => {
                const result = await handler.execute();
                expect(result).toBeInstanceOf(DisconnectCommand_1.DisconnectCommandResponse);
                expect(transactionAdapterMock.stop).toHaveBeenCalledTimes(1);
                expect(result.payload).toEqual(true);
            });
        });
    });
});
