"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 DataFixture_1 = require("../../../../../../../__tests__/fixtures/shared/DataFixture");
const BalancesOfAtSnapshotQueryError_1 = require("./error/BalancesOfAtSnapshotQueryError");
const BalancesOfAtSnapshotQuery_1 = require("./BalancesOfAtSnapshotQuery");
const BalancesOfAtSnapshotQueryHandler_1 = require("./BalancesOfAtSnapshotQueryHandler");
const EvmAddress_1 = __importDefault(require("../../../../../../domain/context/contract/EvmAddress"));
describe("BalancesOfAtSnapshotQueryHandler", () => {
    let handler;
    let query;
    const queryAdapterServiceMock = (0, ts_jest_1.createMock)();
    const contractServiceMock = (0, ts_jest_1.createMock)();
    const evmAddress = new EvmAddress_1.default(DataFixture_1.EvmAddressPropsFixture.create().value);
    const errorMsg = DataFixture_1.ErrorMsgFixture.create().msg;
    const mockBalances = [
        { holder: "0x1234567890123456789012345678901234567890", balance: BigInt(1000) },
        { holder: "0x0987654321098765432109876543210987654321", balance: BigInt(2000) },
    ];
    beforeEach(() => {
        handler = new BalancesOfAtSnapshotQueryHandler_1.BalancesOfAtSnapshotQueryHandler(queryAdapterServiceMock, contractServiceMock);
        query = new BalancesOfAtSnapshotQuery_1.BalancesOfAtSnapshotQuery("securityId", 1, 0, 10);
    });
    afterEach(() => {
        jest.resetAllMocks();
    });
    describe("execute", () => {
        it("throws BalancesOfAtSnapshotQueryError when query fails with uncaught error", async () => {
            const fakeError = new Error(errorMsg);
            contractServiceMock.getContractEvmAddress.mockRejectedValue(fakeError);
            const resultPromise = handler.execute(query);
            await expect(resultPromise).rejects.toThrow(BalancesOfAtSnapshotQueryError_1.BalancesOfAtSnapshotQueryError);
        });
        it("throws BalancesOfAtSnapshotQueryError when RPC query fails", async () => {
            const fakeError = new Error(errorMsg);
            contractServiceMock.getContractEvmAddress.mockResolvedValue(evmAddress);
            queryAdapterServiceMock.balancesOfAtSnapshot.mockRejectedValue(fakeError);
            const resultPromise = handler.execute(query);
            await expect(resultPromise).rejects.toThrow(BalancesOfAtSnapshotQueryError_1.BalancesOfAtSnapshotQueryError);
        });
        it("returns balances when query succeeds", async () => {
            contractServiceMock.getContractEvmAddress.mockResolvedValue(evmAddress);
            queryAdapterServiceMock.balancesOfAtSnapshot.mockResolvedValue(mockBalances);
            const result = await handler.execute(query);
            expect(result).toBeInstanceOf(BalancesOfAtSnapshotQuery_1.BalancesOfAtSnapshotQueryResponse);
            expect(result.payload).toEqual(mockBalances);
            expect(contractServiceMock.getContractEvmAddress).toHaveBeenCalledWith("securityId");
            expect(queryAdapterServiceMock.balancesOfAtSnapshot).toHaveBeenCalledWith(evmAddress, 1, 0, 10);
        });
        it("returns empty array when no balances found", async () => {
            contractServiceMock.getContractEvmAddress.mockResolvedValue(evmAddress);
            queryAdapterServiceMock.balancesOfAtSnapshot.mockResolvedValue([]);
            const result = await handler.execute(query);
            expect(result).toBeInstanceOf(BalancesOfAtSnapshotQuery_1.BalancesOfAtSnapshotQueryResponse);
            expect(result.payload).toEqual([]);
        });
    });
});
