"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 NetworkFixture_1 = require("../../../../../../__tests__/fixtures/network/NetworkFixture");
const SetNetworkCommandHandler_1 = require("./SetNetworkCommandHandler");
const SetNetworkCommand_1 = require("./SetNetworkCommand");
const Injectable_1 = __importDefault(require("../../../../../core/injectable/Injectable"));
const DataFixture_1 = require("../../../../../../__tests__/fixtures/shared/DataFixture");
const SetNetworkCommandError_1 = require("./error/SetNetworkCommandError");
const BaseError_1 = require("../../../../../core/error/BaseError");
describe("SetNetworkCommandHandler", () => {
    let handler;
    let command;
    const networkServiceMock = (0, ts_jest_1.createMock)();
    const mirrorNodeAdapterMock = (0, ts_jest_1.createMock)();
    const queryAdapterMock = (0, ts_jest_1.createMock)();
    const errorMsg = DataFixture_1.ErrorMsgFixture.create().msg;
    beforeEach(() => {
        handler = new SetNetworkCommandHandler_1.SetNetworkCommandHandler(networkServiceMock, mirrorNodeAdapterMock);
        command = NetworkFixture_1.SetNetworkCommandFixture.create();
        jest.spyOn(Injectable_1.default, "resolve").mockReturnValue(queryAdapterMock);
    });
    afterEach(() => {
        jest.resetAllMocks();
    });
    describe("execute", () => {
        describe("error cases", () => {
            it("throws SetNetworkCommandError when command fails with uncaught error", async () => {
                const fakeError = new Error(errorMsg);
                jest.spyOn(mirrorNodeAdapterMock, "set").mockImplementation(() => {
                    throw fakeError;
                });
                const resultPromise = handler.execute(command);
                await expect(resultPromise).rejects.toBeInstanceOf(SetNetworkCommandError_1.SetNetworkCommandError);
                await expect(resultPromise).rejects.toMatchObject({
                    message: expect.stringContaining(`An error occurred while setting network: ${errorMsg}`),
                    errorCode: BaseError_1.ErrorCode.UncaughtCommandError,
                });
            });
        });
        describe("success cases", () => {
            it("should successfully set network", async () => {
                const result = await handler.execute(command);
                expect(result).toBeInstanceOf(SetNetworkCommand_1.SetNetworkCommandResponse);
                expect(result.environment).toEqual(command.environment);
                expect(result.mirrorNode).toEqual(command.mirrorNode);
                expect(result.rpcNode).toEqual(command.rpcNode);
                expect(result.consensusNodes).toEqual(command.consensusNodes);
            });
        });
    });
});
