// SPDX-License-Identifier: Apache-2.0
import { createMock } from "@golevelup/ts-jest";
import { SetNameRequest, SetSymbolRequest } from "../../request";
import { TransactionIdFixture } from "../../../../../__tests__/fixtures/shared/DataFixture";
import LogService from "../../../../app/service/log/LogService";
import ValidatedRequest from "../../../../core/validation/ValidatedArgs";
import { ValidationError } from "../../../../core/validation/ValidationError";
import Security from "../../../in/security/Security";
import { SetNameRequestFixture, SetSymbolRequestFixture } from "../../../../../__tests__/fixtures/tokenMetadata/TokenMetadataFixture";
import { SetNameCommand } from "../../../../app/usecase/command/security/operations/tokenMetadata/setName/SetNameCommand";
import { SetSymbolCommand } from "../../../../app/usecase/command/security/operations/tokenMetadata/setSymbol/SetSymbolCommand";
describe("Token Metadata", () => {
    let commandBusMock;
    let queryBusMock;
    let mirrorNodeMock;
    let setNameRequest;
    let setSymbolRequest;
    let handleValidationSpy;
    const transactionId = TransactionIdFixture.create().id;
    beforeEach(() => {
        commandBusMock = createMock();
        queryBusMock = createMock();
        mirrorNodeMock = createMock();
        handleValidationSpy = jest.spyOn(ValidatedRequest, "handleValidation");
        jest.spyOn(LogService, "logError").mockImplementation(() => { });
        Security.commandBus = commandBusMock;
        Security.queryBus = queryBusMock;
        Security.mirrorNode = mirrorNodeMock;
    });
    afterEach(() => {
        jest.resetAllMocks();
        jest.restoreAllMocks();
    });
    describe("SetName", () => {
        setNameRequest = new SetNameRequest(SetNameRequestFixture.create());
        const expectedResponse = {
            payload: true,
            transactionId: transactionId,
        };
        it("should set name successfully", async () => {
            commandBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await Security.setName(setNameRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("SetNameRequest", setNameRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new SetNameCommand(setNameRequest.securityId, setNameRequest.name));
            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.setName(setNameRequest)).rejects.toThrow("Command execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("SetNameRequest", setNameRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new SetNameCommand(setNameRequest.securityId, setNameRequest.name));
        });
        it("should throw error if securityId is invalid", async () => {
            setNameRequest = new SetNameRequest({
                ...SetNameRequestFixture.create({
                    securityId: "invalid",
                }),
            });
            await expect(Security.setName(setNameRequest)).rejects.toThrow(ValidationError);
        });
        it("should throw error if name is empty", async () => {
            setNameRequest = new SetNameRequest({
                ...SetNameRequestFixture.create({
                    name: "",
                }),
            });
            await expect(Security.setName(setNameRequest)).rejects.toThrow(ValidationError);
        });
    });
    describe("SetSymbol", () => {
        setSymbolRequest = new SetSymbolRequest(SetSymbolRequestFixture.create());
        const expectedResponse = {
            payload: true,
            transactionId: transactionId,
        };
        it("should set symbol successfully", async () => {
            commandBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await Security.setSymbol(setSymbolRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("SetSymbolRequest", setSymbolRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new SetSymbolCommand(setSymbolRequest.securityId, setSymbolRequest.symbol));
            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.setSymbol(setSymbolRequest)).rejects.toThrow("Command execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("SetSymbolRequest", setSymbolRequest);
            expect(commandBusMock.execute).toHaveBeenCalledWith(new SetSymbolCommand(setSymbolRequest.securityId, setSymbolRequest.symbol));
        });
        it("should throw error if securityId is invalid", async () => {
            setSymbolRequest = new SetSymbolRequest({
                ...SetSymbolRequestFixture.create({
                    securityId: "invalid",
                }),
            });
            await expect(Security.setSymbol(setSymbolRequest)).rejects.toThrow(ValidationError);
        });
        it("should throw error if symbol is empty", async () => {
            setSymbolRequest = new SetSymbolRequest({
                ...SetSymbolRequestFixture.create({
                    symbol: "",
                }),
            });
            await expect(Security.setSymbol(setSymbolRequest)).rejects.toThrow(ValidationError);
        });
    });
});
