"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 AddAgentCommand_1 = require("../../../../app/usecase/command/security/operations/agent/addAgent/AddAgentCommand");
const RemoveAgentCommand_1 = require("../../../../app/usecase/command/security/operations/agent/removeAgent/RemoveAgentCommand");
const AgentFixture_1 = require("../../../../../__tests__/fixtures/agent/AgentFixture");
describe("Agent", () => {
    let commandBusMock;
    let queryBusMock;
    let mirrorNodeMock;
    let addAgentRequest;
    let removeAgentRequest;
    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("AddAgentRequest", () => {
        addAgentRequest = new request_1.AddAgentRequest(AgentFixture_1.AddAgentRequestFixture.create());
        const expectedResponse = {
            payload: true,
            transactionId: transactionId,
        };
        it("should add agent successfully", async () => {
            commandBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await Security_1.default.addAgent(addAgentRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("AddAgentRequest", addAgentRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new AddAgentCommand_1.AddAgentCommand(addAgentRequest.securityId, addAgentRequest.agentId));
            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.addAgent(addAgentRequest)).rejects.toThrow("Command execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("AddAgentRequest", addAgentRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new AddAgentCommand_1.AddAgentCommand(addAgentRequest.securityId, addAgentRequest.agentId));
        });
        it("should throw error if securityId is invalid", async () => {
            addAgentRequest = new request_1.AddAgentRequest({
                ...AgentFixture_1.AddAgentRequestFixture.create({
                    securityId: "invalid",
                }),
            });
            await expect(Security_1.default.addAgent(addAgentRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if agentId is invalid", async () => {
            addAgentRequest = new request_1.AddAgentRequest({
                ...AgentFixture_1.AddAgentRequestFixture.create({
                    agentId: "invalid",
                }),
            });
            await expect(Security_1.default.addAgent(addAgentRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
    describe("RemoveAgentRequest", () => {
        removeAgentRequest = new request_1.RemoveAgentRequest(AgentFixture_1.RemoveAgentRequestFixture.create());
        const expectedResponse = {
            payload: true,
            transactionId: transactionId,
        };
        it("should add agent successfully", async () => {
            commandBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await Security_1.default.removeAgent(removeAgentRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("RemoveAgentRequest", removeAgentRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new RemoveAgentCommand_1.RemoveAgentCommand(removeAgentRequest.securityId, removeAgentRequest.agentId));
            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.removeAgent(removeAgentRequest)).rejects.toThrow("Command execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("RemoveAgentRequest", removeAgentRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new RemoveAgentCommand_1.RemoveAgentCommand(removeAgentRequest.securityId, removeAgentRequest.agentId));
        });
        it("should throw error if securityId is invalid", async () => {
            removeAgentRequest = new request_1.RemoveAgentRequest({
                ...AgentFixture_1.RemoveAgentRequestFixture.create({
                    securityId: "invalid",
                }),
            });
            await expect(Security_1.default.removeAgent(removeAgentRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if agentId is invalid", async () => {
            removeAgentRequest = new request_1.RemoveAgentRequest({
                ...AgentFixture_1.RemoveAgentRequestFixture.create({
                    agentId: "invalid",
                }),
            });
            await expect(Security_1.default.removeAgent(removeAgentRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
});
