"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 AccountFixture_1 = require("../../../../../../__tests__/fixtures/account/AccountFixture");
const DataFixture_1 = require("../../../../../../__tests__/fixtures/shared/DataFixture");
const BaseError_1 = require("../../../../../core/error/BaseError");
const GetAccountInfoQueryHandler_1 = require("./GetAccountInfoQueryHandler");
const GetAccountInfoQuery_1 = require("./GetAccountInfoQuery");
const Account_1 = __importDefault(require("../../../../../domain/context/account/Account"));
const GetAccountInfoQueryError_1 = require("./error/GetAccountInfoQueryError");
describe("GetAccountInfoQueryHandler", () => {
    let handler;
    let query;
    const mirrorNodeAdapterMock = (0, ts_jest_1.createMock)();
    const errorMsg = DataFixture_1.ErrorMsgFixture.create().msg;
    const account = new Account_1.default(AccountFixture_1.AccountPropsFixture.create());
    beforeEach(() => {
        handler = new GetAccountInfoQueryHandler_1.GetAccountInfoQueryHandler(mirrorNodeAdapterMock);
        query = AccountFixture_1.GetAccountInfoQueryFixture.create();
    });
    afterEach(() => {
        jest.resetAllMocks();
    });
    describe("execute", () => {
        it("throws GetAccountInfoQueryError when query fails with uncaught error", async () => {
            const fakeError = new Error(errorMsg);
            mirrorNodeAdapterMock.getAccountInfo.mockRejectedValue(fakeError);
            const resultPromise = handler.execute(query);
            await expect(resultPromise).rejects.toBeInstanceOf(GetAccountInfoQueryError_1.GetAccountInfoQueryError);
            await expect(resultPromise).rejects.toMatchObject({
                message: expect.stringContaining(`An error occurred while querying account info: ${errorMsg}`),
                errorCode: BaseError_1.ErrorCode.UncaughtQueryError,
            });
        });
        it("should successfully get account info", async () => {
            mirrorNodeAdapterMock.getAccountInfo.mockResolvedValueOnce(account);
            const result = await handler.execute(query);
            expect(result).toBeInstanceOf(GetAccountInfoQuery_1.GetAccountInfoQueryResponse);
            expect(result.account).toBe(account);
            expect(mirrorNodeAdapterMock.getAccountInfo).toHaveBeenCalledTimes(1);
            expect(mirrorNodeAdapterMock.getAccountInfo).toHaveBeenCalledWith(query.id);
        });
    });
});
