import { genericPrepareTransaction } from "../prepareTransaction";
import { getAlpacaApi } from "../alpaca";
import { transactionToIntent } from "../utils";
import BigNumber from "bignumber.js";
jest.mock("../alpaca", () => ({
    getAlpacaApi: jest.fn(),
}));
jest.mock("../utils", () => ({
    transactionToIntent: jest.fn(),
}));
describe("genericPrepareTransaction", () => {
    const network = "testnet";
    const kind = "local";
    const account = {
        id: "test-account",
        address: "0xabc",
    };
    const baseTransaction = {
        amount: new BigNumber(100_000),
        fees: new BigNumber(500),
        recipient: "0xrecipient",
    };
    const txIntent = { mock: "intent" };
    beforeEach(() => {
        jest.clearAllMocks();
        transactionToIntent.mockReturnValue(txIntent);
    });
    it("updates fees if they differ", async () => {
        const newFee = new BigNumber(700);
        getAlpacaApi.mockReturnValue({
            estimateFees: jest.fn().mockResolvedValue({ value: newFee }),
        });
        const prepareTransaction = genericPrepareTransaction(network, kind);
        const result = await prepareTransaction(account, { ...baseTransaction });
        expect(result.fees.toString()).toBe(newFee.toString());
        expect(transactionToIntent).toHaveBeenCalledWith(account, expect.objectContaining(baseTransaction));
    });
    it("returns original transaction if fees are the same", async () => {
        const sameFee = baseTransaction.fees;
        getAlpacaApi.mockReturnValue({
            estimateFees: jest.fn().mockResolvedValue({ value: sameFee }),
        });
        const prepareTransaction = genericPrepareTransaction(network, kind);
        const result = await prepareTransaction(account, baseTransaction);
        expect(result).toBe(baseTransaction);
    });
    it("sets fee if original fees are undefined", async () => {
        const newFee = new BigNumber(1234);
        getAlpacaApi.mockReturnValue({
            estimateFees: jest.fn().mockResolvedValue({ value: newFee }),
        });
        const txWithoutFees = { ...baseTransaction, fees: undefined };
        const prepareTransaction = genericPrepareTransaction(network, kind);
        const result = await prepareTransaction(account, txWithoutFees);
        expect(result.fees.toString()).toBe(newFee.toString());
        expect(result).not.toBe(txWithoutFees);
    });
    it("returns original if fees are BigNumber-equal but different instance", async () => {
        const sameValue = new BigNumber(baseTransaction.fees.toString()); // different instance
        getAlpacaApi.mockReturnValue({
            estimateFees: jest.fn().mockResolvedValue({ value: sameValue }),
        });
        const prepareTransaction = genericPrepareTransaction(network, kind);
        const result = await prepareTransaction(account, baseTransaction);
        expect(result).toBe(baseTransaction); // still same reference
    });
});
//# sourceMappingURL=prepareTransaction.test.js.map