// SPDX-License-Identifier: Apache-2.0
import { createMock } from "@golevelup/ts-jest";
import { ErrorMsgFixture, EvmAddressPropsFixture } from "../../../../../../../__tests__/fixtures/shared/DataFixture";
import { ErrorCode } from "../../../../../../core/error/BaseError";
import EvmAddress from "../../../../../../domain/context/contract/EvmAddress";
import { GetMaxSupplyByPartitionQueryFixture } from "../../../../../../../__tests__/fixtures/erc1400/ERC1400Fixture";
import { SecurityPropsFixture } from "../../../../../../../__tests__/fixtures/shared/SecurityFixture";
import { Security } from "../../../../../../domain/context/security/Security";
import BigDecimal from "../../../../../../domain/context/shared/BigDecimal";
import { GetMaxSupplyByPartitionQueryHandler } from "./GetMaxSupplyByPartitionQueryHandler";
import { GetMaxSupplyByPartitionQueryResponse } from "./GetMaxSupplyByPartitionQuery";
import { GetMaxSupplyByPartitionQueryError } from "./error/GetMaxSupplyByPartitionQueryError";
describe("GetMaxSupplyByPartitionQueryHandler", () => {
    let handler;
    let query;
    const queryAdapterServiceMock = createMock();
    const contractServiceMock = createMock();
    const securityServiceMock = createMock();
    const evmAddress = new EvmAddress(EvmAddressPropsFixture.create().value);
    const security = new Security(SecurityPropsFixture.create());
    const amount = BigInt(1);
    const errorMsg = ErrorMsgFixture.create().msg;
    beforeEach(() => {
        handler = new GetMaxSupplyByPartitionQueryHandler(securityServiceMock, queryAdapterServiceMock, contractServiceMock);
        query = GetMaxSupplyByPartitionQueryFixture.create();
    });
    afterEach(() => {
        jest.resetAllMocks();
    });
    describe("execute", () => {
        it("throws GetMaxSupplyQueryError when query fails with uncaught error", async () => {
            const fakeError = new Error(errorMsg);
            securityServiceMock.get.mockRejectedValue(fakeError);
            const resultPromise = handler.execute(query);
            await expect(resultPromise).rejects.toBeInstanceOf(GetMaxSupplyByPartitionQueryError);
            await expect(resultPromise).rejects.toMatchObject({
                message: expect.stringContaining(`An error occurred while querying max supply by partition: ${errorMsg}`),
                errorCode: ErrorCode.UncaughtQueryError,
            });
        });
        it("should successfully get max supply by partition", async () => {
            securityServiceMock.get.mockResolvedValueOnce(security);
            contractServiceMock.getContractEvmAddress.mockResolvedValueOnce(evmAddress);
            queryAdapterServiceMock.getMaxSupplyByPartition.mockResolvedValueOnce(amount);
            const result = await handler.execute(query);
            expect(result).toBeInstanceOf(GetMaxSupplyByPartitionQueryResponse);
            expect(result.payload).toStrictEqual(BigDecimal.fromStringFixed(amount.toString(), security.decimals));
            expect(securityServiceMock.get).toHaveBeenCalledTimes(1);
            expect(contractServiceMock.getContractEvmAddress).toHaveBeenCalledTimes(1);
            expect(securityServiceMock.get).toHaveBeenCalledWith(query.securityId);
            expect(contractServiceMock.getContractEvmAddress).toHaveBeenCalledWith(query.securityId);
            expect(queryAdapterServiceMock.getMaxSupplyByPartition).toHaveBeenCalledWith(evmAddress, query.partitionId);
        });
    });
});
