"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 request_1 = require("../request");
const DataFixture_1 = require("../../../../__tests__/fixtures/shared/DataFixture");
const LogService_1 = __importDefault(require("../../../app/service/log/LogService"));
const ValidatedArgs_1 = __importDefault(require("../../../core/validation/ValidatedArgs"));
const ValidationError_1 = require("../../../core/validation/ValidationError");
const Management_1 = __importDefault(require("./Management"));
const ManagementFixture_1 = require("../../../../__tests__/fixtures/management/ManagementFixture");
const updateConfigVersionCommand_1 = require("../../../app/usecase/command/management/updateConfigVersion/updateConfigVersionCommand");
const updateConfigCommand_1 = require("../../../app/usecase/command/management/updateConfig/updateConfigCommand");
const updateResolverCommand_1 = require("../../../app/usecase/command/management/updateResolver/updateResolverCommand");
const ContractId_1 = __importDefault(require("../../../domain/context/contract/ContractId"));
const DiamondConfiguration_1 = require("../../../domain/context/security/DiamondConfiguration");
const GetConfigInfoQuery_1 = require("../../../app/usecase/query/management/GetConfigInfoQuery");
describe("Management", () => {
    let commandBusMock;
    let queryBusMock;
    let mirrorNodeMock;
    let updateConfigVersionRequest;
    let updateConfigRequest;
    let getConfigInfoRequest;
    let updateResolverRequest;
    let handleValidationSpy;
    const transactionId = DataFixture_1.TransactionIdFixture.create().id;
    const resolverAddress = DataFixture_1.HederaIdPropsFixture.create().value;
    const contractViewModel = {
        id: resolverAddress,
        evmAddress: DataFixture_1.EvmAddressPropsFixture.create().value,
    };
    const expectedResponse = {
        payload: true,
        transactionId: transactionId,
    };
    beforeEach(() => {
        commandBusMock = (0, ts_jest_1.createMock)();
        queryBusMock = (0, ts_jest_1.createMock)();
        mirrorNodeMock = (0, ts_jest_1.createMock)();
        handleValidationSpy = jest.spyOn(ValidatedArgs_1.default, "handleValidation");
        jest.spyOn(LogService_1.default, "logError").mockImplementation(() => { });
        Management_1.default.commandBus = commandBusMock;
        Management_1.default.queryBus = queryBusMock;
        Management_1.default.mirrorNode = mirrorNodeMock;
    });
    afterEach(() => {
        jest.resetAllMocks();
        jest.restoreAllMocks();
    });
    describe("updateConfigVersion", () => {
        updateConfigVersionRequest = new request_1.UpdateConfigVersionRequest(ManagementFixture_1.UpdateConfigVersionRequestFixture.create());
        it("should update config version successfully", async () => {
            commandBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await Management_1.default.updateConfigVersion(updateConfigVersionRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("UpdateConfigVersionRequest", updateConfigVersionRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new updateConfigVersionCommand_1.UpdateConfigVersionCommand(updateConfigVersionRequest.configVersion, updateConfigVersionRequest.securityId));
            expect(result).toEqual(expectedResponse);
        });
        it("should throw an error if command execution fails", async () => {
            const error = new Error("Command execution failed");
            commandBusMock.execute.mockRejectedValue(error);
            await expect(Management_1.default.updateConfigVersion(updateConfigVersionRequest)).rejects.toThrow("Command execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("UpdateConfigVersionRequest", updateConfigVersionRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new updateConfigVersionCommand_1.UpdateConfigVersionCommand(updateConfigVersionRequest.configVersion, updateConfigVersionRequest.securityId));
        });
        it("should throw error if securityId is invalid", async () => {
            updateConfigVersionRequest = new request_1.UpdateConfigVersionRequest({
                ...ManagementFixture_1.UpdateConfigVersionRequestFixture.create({
                    securityId: "invalid",
                }),
            });
            await expect(Management_1.default.updateConfigVersion(updateConfigVersionRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if configVersion is invalid", async () => {
            updateConfigVersionRequest = new request_1.UpdateConfigVersionRequest({
                ...ManagementFixture_1.UpdateConfigVersionRequestFixture.create({
                    configVersion: "invalid",
                }),
            });
            await expect(Management_1.default.updateConfigVersion(updateConfigVersionRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
    describe("updateConfig", () => {
        updateConfigRequest = new request_1.UpdateConfigRequest(ManagementFixture_1.UpdateConfigRequestFixture.create());
        it("should update config successfully", async () => {
            commandBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await Management_1.default.updateConfig(updateConfigRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("UpdateConfigRequest", updateConfigRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new updateConfigCommand_1.UpdateConfigCommand(updateConfigRequest.configId, updateConfigRequest.configVersion, updateConfigRequest.securityId));
            expect(result).toEqual(expectedResponse);
        });
        it("should throw an error if command execution fails", async () => {
            const error = new Error("Command execution failed");
            commandBusMock.execute.mockRejectedValue(error);
            await expect(Management_1.default.updateConfig(updateConfigRequest)).rejects.toThrow("Command execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("UpdateConfigRequest", updateConfigRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new updateConfigCommand_1.UpdateConfigCommand(updateConfigRequest.configId, updateConfigRequest.configVersion, updateConfigRequest.securityId));
        });
        it("should throw error if securityId is invalid", async () => {
            updateConfigRequest = new request_1.UpdateConfigRequest({
                ...ManagementFixture_1.UpdateConfigRequestFixture.create({
                    securityId: "invalid",
                }),
            });
            await expect(Management_1.default.updateConfig(updateConfigRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if configId is invalid", async () => {
            updateConfigRequest = new request_1.UpdateConfigRequest({
                ...ManagementFixture_1.UpdateConfigRequestFixture.create({
                    configId: "invalid",
                }),
            });
            await expect(Management_1.default.updateConfig(updateConfigRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if configVersion is invalid", async () => {
            updateConfigRequest = new request_1.UpdateConfigRequest({
                ...ManagementFixture_1.UpdateConfigRequestFixture.create({
                    configVersion: "invalid",
                }),
            });
            await expect(Management_1.default.updateConfig(updateConfigRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
    describe("updateResolver", () => {
        updateResolverRequest = new request_1.UpdateResolverRequest(ManagementFixture_1.UpdateResolverRequestFixture.create());
        it("should update resolver successfully", async () => {
            commandBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await Management_1.default.updateResolver(updateResolverRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("UpdateResolverRequest", updateResolverRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new updateResolverCommand_1.UpdateResolverCommand(updateResolverRequest.configVersion, updateResolverRequest.securityId, updateResolverRequest.configId, new ContractId_1.default(updateResolverRequest.resolver)));
            expect(result).toEqual(expectedResponse);
        });
        it("should throw an error if command execution fails", async () => {
            const error = new Error("Command execution failed");
            commandBusMock.execute.mockRejectedValue(error);
            await expect(Management_1.default.updateResolver(updateResolverRequest)).rejects.toThrow("Command execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("UpdateResolverRequest", updateResolverRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new updateResolverCommand_1.UpdateResolverCommand(updateResolverRequest.configVersion, updateResolverRequest.securityId, updateResolverRequest.configId, new ContractId_1.default(updateResolverRequest.resolver)));
        });
        it("should throw error if securityId is invalid", async () => {
            updateResolverRequest = new request_1.UpdateResolverRequest({
                ...ManagementFixture_1.UpdateResolverRequestFixture.create({
                    securityId: "invalid",
                }),
            });
            await expect(Management_1.default.updateResolver(updateResolverRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if configId is invalid", async () => {
            updateResolverRequest = new request_1.UpdateResolverRequest({
                ...ManagementFixture_1.UpdateResolverRequestFixture.create({
                    configId: "invalid",
                }),
            });
            await expect(Management_1.default.updateResolver(updateResolverRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if configVersion is invalid", async () => {
            updateResolverRequest = new request_1.UpdateResolverRequest({
                ...ManagementFixture_1.UpdateResolverRequestFixture.create({
                    configVersion: "invalid",
                }),
            });
            await expect(Management_1.default.updateResolver(updateResolverRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if resolver is invalid", async () => {
            updateResolverRequest = new request_1.UpdateResolverRequest({
                ...ManagementFixture_1.UpdateResolverRequestFixture.create({
                    resolver: "invalid",
                }),
            });
            await expect(Management_1.default.updateResolver(updateResolverRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
    describe("getConfigInfo", () => {
        getConfigInfoRequest = new request_1.GetConfigInfoRequest(ManagementFixture_1.GetConfigInfoRequestFixture.create());
        const expectedResponse = {
            payload: new DiamondConfiguration_1.DiamondConfiguration(resolverAddress, "1", 1),
        };
        it("should get config info successfully", async () => {
            queryBusMock.execute.mockResolvedValue(expectedResponse);
            mirrorNodeMock.getContractInfo.mockResolvedValue(contractViewModel);
            const result = await Management_1.default.getConfigInfo(getConfigInfoRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("GetConfigInfoRequest", getConfigInfoRequest);
            expect(queryBusMock.execute).toHaveBeenCalledTimes(1);
            expect(mirrorNodeMock.getContractInfo).toHaveBeenCalledTimes(1);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetConfigInfoQuery_1.GetConfigInfoQuery(getConfigInfoRequest.securityId));
            expect(mirrorNodeMock.getContractInfo).toHaveBeenCalledWith(resolverAddress);
            expect(result).toEqual(expect.objectContaining({
                resolverAddress: resolverAddress,
                configId: "1",
                configVersion: 1,
            }));
        });
        it("should throw an error if query execution fails", async () => {
            const error = new Error("Query execution failed");
            queryBusMock.execute.mockRejectedValue(error);
            await expect(Management_1.default.getConfigInfo(getConfigInfoRequest)).rejects.toThrow("Query execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("GetConfigInfoRequest", getConfigInfoRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetConfigInfoQuery_1.GetConfigInfoQuery(getConfigInfoRequest.securityId));
        });
        it("should throw error if securityId is invalid", async () => {
            getConfigInfoRequest = new request_1.GetConfigInfoRequest({
                ...ManagementFixture_1.GetConfigInfoRequestFixture.create({
                    securityId: "invalid",
                }),
            });
            await expect(Management_1.default.getConfigInfo(getConfigInfoRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
});
