// SPDX-License-Identifier: Apache-2.0
import { createMock } from "@golevelup/ts-jest";
import { GetAccountBalanceRequest } from "../../request";
import LogService from "../../../../app/service/log/LogService";
import ValidatedRequest from "../../../../core/validation/ValidatedArgs";
import { ValidationError } from "../../../../core/validation/ValidationError";
import { GetAccountBalanceRequestFixture } from "../../../../../__tests__/fixtures/account/AccountFixture";
import Security from "../../../in/security/Security";
import { BalanceOfQuery } from "../../../../app/usecase/query/security/balanceof/BalanceOfQuery";
describe("Balance", () => {
    let commandBusMock;
    let queryBusMock;
    let mirrorNodeMock;
    let getAccountBalanceRequest;
    let handleValidationSpy;
    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("getBalanceOf", () => {
        getAccountBalanceRequest = new GetAccountBalanceRequest(GetAccountBalanceRequestFixture.create());
        const expectedResponse = {
            payload: BigInt(1),
        };
        it("should get balance of successfully", async () => {
            queryBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await Security.getBalanceOf(getAccountBalanceRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("GetAccountBalanceRequest", getAccountBalanceRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new BalanceOfQuery(getAccountBalanceRequest.securityId, getAccountBalanceRequest.targetId));
            expect(result).toEqual(expect.objectContaining({
                value: expectedResponse.payload.toString(),
            }));
        });
        it("should throw an error if query execution fails", async () => {
            const error = new Error("Query execution failed");
            queryBusMock.execute.mockRejectedValue(error);
            await expect(Security.getBalanceOf(getAccountBalanceRequest)).rejects.toThrow("Query execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("GetAccountBalanceRequest", getAccountBalanceRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new BalanceOfQuery(getAccountBalanceRequest.securityId, getAccountBalanceRequest.targetId));
        });
        it("should throw error if securityId is invalid", async () => {
            getAccountBalanceRequest = new GetAccountBalanceRequest({
                ...GetAccountBalanceRequestFixture.create({
                    securityId: "invalid",
                }),
            });
            await expect(Security.getBalanceOf(getAccountBalanceRequest)).rejects.toThrow(ValidationError);
        });
        it("should throw error if targetId is invalid", async () => {
            getAccountBalanceRequest = new GetAccountBalanceRequest({
                ...GetAccountBalanceRequestFixture.create({
                    targetId: "invalid",
                }),
            });
            await expect(Security.getBalanceOf(getAccountBalanceRequest)).rejects.toThrow(ValidationError);
        });
    });
});
