"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 Injectable_1 = __importDefault(require("../../../core/injectable/Injectable"));
const ts_jest_1 = require("@golevelup/ts-jest");
const TransactionService_1 = __importDefault(require("./TransactionService"));
const SetCouponCommandHandler_1 = require("../../usecase/command/bond/coupon/set/SetCouponCommandHandler");
const EmptyResponse_1 = require("./error/EmptyResponse");
const DataFixture_1 = require("../../../../__tests__/fixtures/shared/DataFixture");
const InvalidResponse_1 = require("../../../core/error/InvalidResponse");
const _1 = require("@faker-js/faker/.");
const CreateEquityCommandHandler_1 = require("../../usecase/command/equity/create/CreateEquityCommandHandler");
const CreateBondCommandHandler_1 = require("../../usecase/command/bond/create/CreateBondCommandHandler");
const Constants_1 = require("../../../core/Constants");
describe("TransactioNService", () => {
    let service;
    const position = 1;
    const numberOfResultsItems = 2;
    const mirrorNodeAdapterMock = (0, ts_jest_1.createMock)();
    const transactionResponse = DataFixture_1.TransactionResponseFixture.create();
    const result = _1.faker.number.int({ min: 1, max: 999 }).toString();
    beforeEach(() => {
        jest.spyOn(Injectable_1.default, "resolve").mockReturnValue(mirrorNodeAdapterMock);
        service = new TransactionService_1.default();
    });
    afterEach(() => {
        jest.resetAllMocks();
    });
    describe("getTransactionResult", () => {
        describe("error cases", () => {
            it("should throw an error when transaction response id is missing", async () => {
                const response = {
                    id: undefined,
                };
                await expect(service.getTransactionResult({
                    res: response,
                    className: SetCouponCommandHandler_1.SetCouponCommandHandler.name,
                    position,
                    numberOfResultsItems,
                })).rejects.toThrow(EmptyResponse_1.EmptyResponse);
            });
            it("should throw an error when transaction response is empty", async () => {
                mirrorNodeAdapterMock.getContractResults.mockResolvedValue(null);
                await expect(service.getTransactionResult({
                    res: transactionResponse,
                    className: SetCouponCommandHandler_1.SetCouponCommandHandler.name,
                    position,
                    numberOfResultsItems,
                })).rejects.toThrow(InvalidResponse_1.InvalidResponse);
            });
        });
        describe("success cases", () => {
            it("should retrieve transaction result from event data", async () => {
                await expect(service.getTransactionResult({
                    res: transactionResponse,
                    result,
                    className: SetCouponCommandHandler_1.SetCouponCommandHandler.name,
                    position,
                    numberOfResultsItems,
                })).resolves.toBe(result);
            });
            it("should retrieve transaction result from mirror node", async () => {
                const results = ["1", result];
                mirrorNodeAdapterMock.getContractResults.mockResolvedValue(results);
                await expect(service.getTransactionResult({
                    res: transactionResponse,
                    className: SetCouponCommandHandler_1.SetCouponCommandHandler.name,
                    position,
                    numberOfResultsItems,
                })).resolves.toBe(results[position]);
                expect(mirrorNodeAdapterMock.getContractResults).toHaveBeenCalledWith(transactionResponse.id, numberOfResultsItems);
                expect(mirrorNodeAdapterMock.getContractResults).toHaveBeenCalledTimes(1);
            });
            it("should retrieve transaction result for CreateEquityCommandHandler with formatted address", async () => {
                const rawResult = "0".repeat(Constants_1.BYTES_32_LENGTH - Constants_1.ADDRESS_LENGTH + 2) + "1234567890abcdef";
                const expectedResult = "0x1234567890abcdef";
                const results = ["1", rawResult];
                mirrorNodeAdapterMock.getContractResults.mockResolvedValue(results);
                await expect(service.getTransactionResult({
                    res: transactionResponse,
                    className: CreateEquityCommandHandler_1.CreateEquityCommandHandler.name,
                    position,
                    numberOfResultsItems,
                })).resolves.toBe(expectedResult);
                expect(mirrorNodeAdapterMock.getContractResults).toHaveBeenCalledWith(transactionResponse.id, numberOfResultsItems);
                expect(mirrorNodeAdapterMock.getContractResults).toHaveBeenCalledTimes(1);
            });
            it("should retrieve transaction result for CreateBondCommandHandler with formatted address", async () => {
                const rawResult = "0".repeat(Constants_1.BYTES_32_LENGTH - Constants_1.ADDRESS_LENGTH + 2) + "abcdef1234567890";
                const expectedResult = "0xabcdef1234567890";
                const results = ["1", rawResult];
                mirrorNodeAdapterMock.getContractResults.mockResolvedValue(results);
                await expect(service.getTransactionResult({
                    res: transactionResponse,
                    className: CreateBondCommandHandler_1.CreateBondCommandHandler.name,
                    position,
                    numberOfResultsItems,
                })).resolves.toBe(expectedResult);
                expect(mirrorNodeAdapterMock.getContractResults).toHaveBeenCalledWith(transactionResponse.id, numberOfResultsItems);
                expect(mirrorNodeAdapterMock.getContractResults).toHaveBeenCalledTimes(1);
            });
        });
    });
});
