"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 SetNetworkRequest_1 = __importDefault(require("../request/network/SetNetworkRequest"));
const SetConfigurationRequest_1 = __importDefault(require("../request/management/SetConfigurationRequest"));
const ts_jest_1 = require("@golevelup/ts-jest");
const LogService_1 = __importDefault(require("../../../app/service/log/LogService"));
const ValidatedArgs_1 = __importDefault(require("../../../core/validation/ValidatedArgs"));
const Network_1 = __importDefault(require("./Network"));
const SetConfigurationCommand_1 = require("../../../app/usecase/command/network/setConfiguration/SetConfigurationCommand");
const SetNetworkCommand_1 = require("../../../app/usecase/command/network/setNetwork/SetNetworkCommand");
const DataFixture_1 = require("../../../../__tests__/fixtures/shared/DataFixture");
describe("Network", () => {
    let commandBusMock;
    let transactionServiceMock;
    let networkServiceMock;
    let setNetworkRequest;
    let setConfigurationRequest;
    let handleValidationSpy;
    const mockFactoryAddress = DataFixture_1.HederaIdPropsFixture.create().value;
    const mockResolverAddress = DataFixture_1.HederaIdPropsFixture.create().value;
    const mockEnvironment = "testnet";
    const mockMirrorNode = "mirror.node";
    const mockRpcNode = "rpc.node";
    const mockConsensusNodes = "consensus.nodes";
    beforeEach(() => {
        commandBusMock = (0, ts_jest_1.createMock)();
        transactionServiceMock = (0, ts_jest_1.createMock)();
        handleValidationSpy = jest.spyOn(ValidatedArgs_1.default, "handleValidation");
        networkServiceMock = (0, ts_jest_1.createMock)({
            configuration: {
                factoryAddress: mockFactoryAddress,
                resolverAddress: mockResolverAddress,
            },
            environment: mockEnvironment,
        });
        jest.spyOn(LogService_1.default, "logError").mockImplementation(() => { });
        setNetworkRequest = new SetNetworkRequest_1.default({
            environment: mockEnvironment,
            mirrorNode: mockMirrorNode,
            rpcNode: mockRpcNode,
            consensusNodes: mockConsensusNodes,
        });
        setConfigurationRequest = new SetConfigurationRequest_1.default({
            factoryAddress: mockFactoryAddress,
            resolverAddress: mockResolverAddress,
        });
        Network_1.default.commandBus = commandBusMock;
        Network_1.default.transactionService = transactionServiceMock;
        Network_1.default.networkService = networkServiceMock;
    });
    afterEach(() => {
        jest.resetAllMocks();
        jest.restoreAllMocks();
    });
    describe("setConfig", () => {
        it("should validate request and execute SetConfigurationCommand", async () => {
            const expectedResponse = {
                factoryAddress: mockFactoryAddress,
                resolverAddress: mockResolverAddress,
            };
            commandBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await Network_1.default.setConfig(setConfigurationRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("SetConfigurationRequest", setConfigurationRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new SetConfigurationCommand_1.SetConfigurationCommand(mockFactoryAddress, mockResolverAddress));
            expect(result).toEqual(expectedResponse);
        });
    });
    describe("getFactoryAddress", () => {
        it("should return factory address from networkService", async () => {
            const result = await Network_1.default.getFactoryAddress();
            expect(result).toBe(mockFactoryAddress);
        });
        it("should return empty string if configuration is undefined", async () => {
            networkServiceMock.configuration = undefined;
            const result = await Network_1.default.getFactoryAddress();
            expect(result).toBe("");
        });
    });
    describe("getResolverAddress", () => {
        it("should return resolver address from networkService", async () => {
            const result = await Network_1.default.getResolverAddress();
            expect(result).toBe(mockResolverAddress);
        });
        it("should return empty string if configuration is undefined", async () => {
            networkServiceMock.configuration = undefined;
            const result = await Network_1.default.getResolverAddress();
            expect(result).toBe("");
        });
    });
    describe("getNetwork", () => {
        it("should return environment from networkService", async () => {
            const result = await Network_1.default.getNetwork();
            expect(result).toBe(mockEnvironment);
        });
    });
    describe("isNetworkRecognized", () => {
        it("should return true if environment is recognized", async () => {
            const result = await Network_1.default.isNetworkRecognized();
            expect(result).toBe(true);
        });
        it("should return false if environment is unrecognized", async () => {
            networkServiceMock.environment = "unrecognized";
            const result = await Network_1.default.isNetworkRecognized();
            expect(result).toBe(false);
        });
    });
    describe("setNetwork", () => {
        it("should validate request and execute SetNetworkCommand", async () => {
            const expectedResponse = {
                environment: mockEnvironment,
                mirrorNode: mockMirrorNode,
                rpcNode: mockRpcNode,
                consensusNodes: mockConsensusNodes,
            };
            commandBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await Network_1.default.setNetwork(setNetworkRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("SetNetworkRequest", setNetworkRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new SetNetworkCommand_1.SetNetworkCommand(mockEnvironment, mockMirrorNode, mockRpcNode, mockConsensusNodes));
            expect(result).toEqual(expectedResponse);
        });
    });
});
