// SPDX-License-Identifier: Apache-2.0
import { ErrorCode } from "../../../../../../core/error/BaseError";
import EvmAddress from "../../../../../../domain/context/contract/EvmAddress";
import { createMock } from "@golevelup/ts-jest";
import { GetCouponFromOrderedListAtQueryFixture } from "../../../../../../../__tests__/fixtures/bond/BondFixture";
import { ErrorMsgFixture, EvmAddressPropsFixture } from "../../../../../../../__tests__/fixtures/shared/DataFixture";
import { GetCouponFromOrderedListAtQueryError } from "./error/GetCouponFromOrderedListAtQueryError";
import { GetCouponFromOrderedListAtQueryResponse, } from "./GetCouponFromOrderedListAtQuery";
import { GetCouponFromOrderedListAtQueryHandler } from "./GetCouponFromOrderedListAtQueryHandler";
describe("GetCouponFromOrderedListAtQueryHandler", () => {
    let handler;
    let query;
    const queryAdapterServiceMock = createMock();
    const accountServiceMock = createMock();
    const evmAddress = new EvmAddress(EvmAddressPropsFixture.create().value);
    const errorMsg = ErrorMsgFixture.create().msg;
    beforeEach(() => {
        handler = new GetCouponFromOrderedListAtQueryHandler(queryAdapterServiceMock, accountServiceMock);
        query = GetCouponFromOrderedListAtQueryFixture.create();
    });
    afterEach(() => {
        jest.resetAllMocks();
    });
    describe("execute", () => {
        it("throws GetCouponFromOrderedListAtQueryError when query fails with uncaught error", async () => {
            const fakeError = new Error(errorMsg);
            accountServiceMock.getAccountEvmAddress.mockRejectedValue(fakeError);
            const resultPromise = handler.execute(query);
            await expect(resultPromise).rejects.toBeInstanceOf(GetCouponFromOrderedListAtQueryError);
            await expect(resultPromise).rejects.toMatchObject({
                message: expect.stringContaining(`An error occurred while querying coupon from ordered list at position: ${errorMsg}`),
                errorCode: ErrorCode.UncaughtQueryError,
            });
        });
        it("should successfully get coupon holders", async () => {
            accountServiceMock.getAccountEvmAddress.mockResolvedValueOnce(evmAddress);
            queryAdapterServiceMock.getCouponFromOrderedListAt.mockResolvedValue(1);
            const result = await handler.execute(query);
            expect(result).toBeInstanceOf(GetCouponFromOrderedListAtQueryResponse);
            expect(result.couponId).toStrictEqual(1);
            expect(accountServiceMock.getAccountEvmAddress).toHaveBeenCalledTimes(1);
            expect(queryAdapterServiceMock.getCouponFromOrderedListAt).toHaveBeenCalledTimes(1);
            expect(accountServiceMock.getAccountEvmAddress).toHaveBeenCalledWith(query.securityId);
            expect(queryAdapterServiceMock.getCouponFromOrderedListAt).toHaveBeenCalledWith(evmAddress, query.pos);
        });
    });
});
