// SPDX-License-Identifier: Apache-2.0
import { ErrorCode } from "../../../../../core/error/BaseError";
import { createMock } from "@golevelup/ts-jest";
import { GetActiveAmortizationHoldHoldersQueryFixture } from "../../../../../../__tests__/fixtures/amortization/AmortizationFixture";
import { EvmAddressPropsFixture, AccountPropsFixture, ErrorMsgFixture } from "../../../../../../__tests__/fixtures/shared/DataFixture";
import { GetActiveAmortizationHoldHoldersQueryError } from "./error/GetActiveAmortizationHoldHoldersQueryError";
import { GetActiveAmortizationHoldHoldersQueryResponse, } from "./GetActiveAmortizationHoldHoldersQuery";
import { GetActiveAmortizationHoldHoldersQueryHandler } from "./GetActiveAmortizationHoldHoldersQueryHandler";
import EvmAddress from "../../../../../domain/context/contract/EvmAddress";
import Account from "../../../../../domain/context/account/Account";
describe("GetActiveAmortizationHoldHoldersQueryHandler", () => {
    let handler;
    let query;
    const queryAdapterServiceMock = createMock();
    const contractServiceMock = createMock();
    const accountServiceMock = createMock();
    const evmAddress = new EvmAddress(EvmAddressPropsFixture.create().value);
    const account = new Account(AccountPropsFixture.create());
    const errorMsg = ErrorMsgFixture.create().msg;
    beforeEach(() => {
        handler = new GetActiveAmortizationHoldHoldersQueryHandler(queryAdapterServiceMock, accountServiceMock, contractServiceMock);
        query = GetActiveAmortizationHoldHoldersQueryFixture.create();
    });
    afterEach(() => {
        jest.resetAllMocks();
    });
    describe("execute", () => {
        it("throws GetActiveAmortizationHoldHoldersQueryError 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.toBeInstanceOf(GetActiveAmortizationHoldHoldersQueryError);
            await expect(resultPromise).rejects.toMatchObject({
                message: expect.stringContaining(`An error occurred while querying active amortization hold holders: ${errorMsg}`),
                errorCode: ErrorCode.UncaughtQueryError,
            });
        });
        it("should successfully get active amortization hold holders", async () => {
            contractServiceMock.getContractEvmAddress.mockResolvedValueOnce(evmAddress);
            queryAdapterServiceMock.getActiveAmortizationHoldHolders.mockResolvedValue([evmAddress.toString()]);
            accountServiceMock.getAccountInfo.mockResolvedValueOnce(account);
            const result = await handler.execute(query);
            expect(result).toBeInstanceOf(GetActiveAmortizationHoldHoldersQueryResponse);
            expect(result.payload).toStrictEqual([account.id.toString()]);
            expect(contractServiceMock.getContractEvmAddress).toHaveBeenCalledTimes(1);
            expect(accountServiceMock.getAccountInfo).toHaveBeenCalledTimes(1);
            expect(queryAdapterServiceMock.getActiveAmortizationHoldHolders).toHaveBeenCalledTimes(1);
            expect(contractServiceMock.getContractEvmAddress).toHaveBeenCalledWith(query.securityId);
            expect(queryAdapterServiceMock.getActiveAmortizationHoldHolders).toHaveBeenCalledWith(evmAddress, query.amortizationId, query.start, query.end);
            expect(accountServiceMock.getAccountInfo).toHaveBeenCalledWith(evmAddress.toString());
        });
    });
});
