"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 LogService_1 = __importDefault(require("../../../../app/service/log/LogService"));
const ValidatedArgs_1 = __importDefault(require("../../../../core/validation/ValidatedArgs"));
const ValidationError_1 = require("../../../../core/validation/ValidationError");
const AccountFixture_1 = require("../../../../../__tests__/fixtures/account/AccountFixture");
const Security_1 = __importDefault(require("../../../in/security/Security"));
const BalanceOfQuery_1 = require("../../../../app/usecase/query/security/balanceof/BalanceOfQuery");
describe("Balance", () => {
    let commandBusMock;
    let queryBusMock;
    let mirrorNodeMock;
    let getAccountBalanceRequest;
    let handleValidationSpy;
    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("getBalanceOf", () => {
        getAccountBalanceRequest = new request_1.GetAccountBalanceRequest(AccountFixture_1.GetAccountBalanceRequestFixture.create());
        const expectedResponse = {
            payload: BigInt(1),
        };
        it("should get balance of successfully", async () => {
            queryBusMock.execute.mockResolvedValue(expectedResponse);
            const result = await Security_1.default.getBalanceOf(getAccountBalanceRequest);
            expect(handleValidationSpy).toHaveBeenCalledWith("GetAccountBalanceRequest", getAccountBalanceRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new BalanceOfQuery_1.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_1.default.getBalanceOf(getAccountBalanceRequest)).rejects.toThrow("Query execution failed");
            expect(handleValidationSpy).toHaveBeenCalledWith("GetAccountBalanceRequest", getAccountBalanceRequest);
            expect(queryBusMock.execute).toHaveBeenCalledWith(new BalanceOfQuery_1.BalanceOfQuery(getAccountBalanceRequest.securityId, getAccountBalanceRequest.targetId));
        });
        it("should throw error if securityId is invalid", async () => {
            getAccountBalanceRequest = new request_1.GetAccountBalanceRequest({
                ...AccountFixture_1.GetAccountBalanceRequestFixture.create({
                    securityId: "invalid",
                }),
            });
            await expect(Security_1.default.getBalanceOf(getAccountBalanceRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
        it("should throw error if targetId is invalid", async () => {
            getAccountBalanceRequest = new request_1.GetAccountBalanceRequest({
                ...AccountFixture_1.GetAccountBalanceRequestFixture.create({
                    targetId: "invalid",
                }),
            });
            await expect(Security_1.default.getBalanceOf(getAccountBalanceRequest)).rejects.toThrow(ValidationError_1.ValidationError);
        });
    });
});
