"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 ContractId_1 = __importDefault(require("../../../domain/context/contract/ContractId"));
const Factory_1 = __importDefault(require("./Factory"));
const FactoryFixture_1 = require("../../../../__tests__/fixtures/factory/FactoryFixture");
const RegulationFixture_1 = require("../../../../__tests__/fixtures/shared/RegulationFixture");
const GetRegulationDetailsQuery_1 = require("../../../app/usecase/query/factory/get/GetRegulationDetailsQuery");
describe("Factory", () => {
    let queryBusMock;
    let networkServiceMock;
    let getRegulationDetailsRequest;
    let handleValidationSpy;
    const factoryAddress = DataFixture_1.HederaIdPropsFixture.create().value;
    beforeEach(() => {
        queryBusMock = (0, ts_jest_1.createMock)();
        handleValidationSpy = jest.spyOn(ValidatedArgs_1.default, "handleValidation");
        networkServiceMock = (0, ts_jest_1.createMock)({
            configuration: {
                factoryAddress: factoryAddress,
            },
        });
        jest.spyOn(LogService_1.default, "logError").mockImplementation(() => { });
        Factory_1.default.queryBus = queryBusMock;
        Factory_1.default.networkService = networkServiceMock;
    });
    afterEach(() => {
        jest.resetAllMocks();
        jest.restoreAllMocks();
    });
    describe("getRegulationDetails", () => {
        getRegulationDetailsRequest = new request_1.GetRegulationDetailsRequest(FactoryFixture_1.GetRegulationDetailsRequestFixture.create());
        it("should get regulation details successfully", async () => {
            const expectedResponse = {
                regulation: RegulationFixture_1.RegulationFixture.create(),
            };
            queryBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await Factory_1.default.getRegulationDetails(getRegulationDetailsRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("GetRegulationDetailsRequest", getRegulationDetailsRequest);
            expect(queryBusMock.execute).toHaveBeenCalledTimes(1);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetRegulationDetailsQuery_1.GetRegulationDetailsQuery(getRegulationDetailsRequest.regulationType, getRegulationDetailsRequest.regulationSubType, new ContractId_1.default(factoryAddress)));
            expect(result).toEqual(expectedResponse.regulation);
        });
        it("should throw an error if query execution fails", async () => {
            const error = new Error("Query execution failed");
            queryBusMock.execute.mockRejectedValue(error);
            await expect(Factory_1.default.getRegulationDetails(getRegulationDetailsRequest)).rejects.toThrow("Query execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("GetRegulationDetailsRequest", getRegulationDetailsRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new GetRegulationDetailsQuery_1.GetRegulationDetailsQuery(getRegulationDetailsRequest.regulationType, getRegulationDetailsRequest.regulationSubType, new ContractId_1.default(factoryAddress)));
        });
        it("should throw error if regulationType is invalid", async () => {
            getRegulationDetailsRequest = new request_1.GetRegulationDetailsRequest({
                ...FactoryFixture_1.GetRegulationDetailsRequestFixture.create(),
                regulationType: 100,
            });
            await expect(Factory_1.default.getRegulationDetails(getRegulationDetailsRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if regulationSubType is invalid", async () => {
            getRegulationDetailsRequest = new request_1.GetRegulationDetailsRequest({
                ...FactoryFixture_1.GetRegulationDetailsRequestFixture.create(),
                regulationSubType: 100,
            });
            await expect(Factory_1.default.getRegulationDetails(getRegulationDetailsRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
});
