"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 Security_1 = __importDefault(require("../../../in/security/Security"));
const RecoveryAddressCommand_1 = require("../../../../app/usecase/command/security/operations/recoveryAddress/RecoveryAddressCommand");
const IsAddressRecoveredQuery_1 = require("../../../../app/usecase/query/security/recovery/IsAddressRecoveredQuery");
const RecoveryFixture_1 = require("../../../../../__tests__/fixtures/recovery/RecoveryFixture");
describe("Recovery", () => {
    let commandBusMock;
    let queryBusMock;
    let mirrorNodeMock;
    let recoveryAddressRequest;
    let isAddressRecoveredRequest;
    let handleValidationSpy;
    const transactionId = DataFixture_1.TransactionIdFixture.create().id;
    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(() => { });
        Security_1.default.commandBus = commandBusMock;
        Security_1.default.queryBus = queryBusMock;
        Security_1.default.mirrorNode = mirrorNodeMock;
    });
    afterEach(() => {
        jest.resetAllMocks();
        jest.restoreAllMocks();
    });
    describe("RecoveryAddress", () => {
        recoveryAddressRequest = new request_1.RecoveryAddressRequest(RecoveryFixture_1.RecoveryAddressRequestFixture.create());
        const expectedResponse = {
            payload: true,
            transactionId: transactionId,
        };
        it("should recover address successfully", async () => {
            commandBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await Security_1.default.recoveryAddress(recoveryAddressRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("RecoveryAddressRequest", recoveryAddressRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new RecoveryAddressCommand_1.RecoveryAddressCommand(recoveryAddressRequest.securityId, recoveryAddressRequest.lostWalletId, recoveryAddressRequest.newWalletId));
            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(Security_1.default.recoveryAddress(recoveryAddressRequest)).rejects.toThrow("Command execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("RecoveryAddressRequest", recoveryAddressRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new RecoveryAddressCommand_1.RecoveryAddressCommand(recoveryAddressRequest.securityId, recoveryAddressRequest.lostWalletId, recoveryAddressRequest.newWalletId));
        });
        it("should throw error if securityId is invalid", async () => {
            recoveryAddressRequest = new request_1.RecoveryAddressRequest({
                ...RecoveryFixture_1.RecoveryAddressRequestFixture.create({
                    securityId: "invalid",
                }),
            });
            await expect(Security_1.default.recoveryAddress(recoveryAddressRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if lostWalletId is invalid", async () => {
            recoveryAddressRequest = new request_1.RecoveryAddressRequest({
                ...RecoveryFixture_1.RecoveryAddressRequestFixture.create({
                    lostWalletId: "invalid",
                }),
            });
            await expect(Security_1.default.recoveryAddress(recoveryAddressRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if newWalletId is invalid", async () => {
            recoveryAddressRequest = new request_1.RecoveryAddressRequest({
                ...RecoveryFixture_1.RecoveryAddressRequestFixture.create({
                    newWalletId: "invalid",
                }),
            });
            await expect(Security_1.default.recoveryAddress(recoveryAddressRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
    describe("IsAddressRecovered", () => {
        isAddressRecoveredRequest = new request_1.IsAddressRecoveredRequest(RecoveryFixture_1.IsAddressRecoveredRequestFixture.create());
        const expectedResponse = {
            payload: true,
        };
        it("should get recovered status successfully", async () => {
            queryBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await Security_1.default.isAddressRecovered(isAddressRecoveredRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("IsAddressRecoveredRequest", isAddressRecoveredRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new IsAddressRecoveredQuery_1.IsAddressRecoveredQuery(isAddressRecoveredRequest.securityId, isAddressRecoveredRequest.targetId));
            expect(result).toEqual(expectedResponse.payload);
        });
        it("should throw an error if query execution fails", async () => {
            const error = new Error("Query execution failed");
            queryBusMock.execute.mockRejectedValue(error);
            await expect(Security_1.default.isAddressRecovered(isAddressRecoveredRequest)).rejects.toThrow("Query execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("IsAddressRecoveredRequest", isAddressRecoveredRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new IsAddressRecoveredQuery_1.IsAddressRecoveredQuery(isAddressRecoveredRequest.securityId, isAddressRecoveredRequest.targetId));
        });
        it("should throw error if securityId is invalid", async () => {
            isAddressRecoveredRequest = new request_1.IsAddressRecoveredRequest({
                ...RecoveryFixture_1.IsAddressRecoveredRequestFixture.create({
                    securityId: "invalid",
                }),
            });
            await expect(Security_1.default.isAddressRecovered(isAddressRecoveredRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if targetId is invalid", async () => {
            isAddressRecoveredRequest = new request_1.IsAddressRecoveredRequest({
                ...RecoveryFixture_1.IsAddressRecoveredRequestFixture.create({
                    targetId: "invalid",
                }),
            });
            await expect(Security_1.default.isAddressRecovered(isAddressRecoveredRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
});
