"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const rxjs_1 = require("rxjs");
const operators_1 = require("rxjs/operators");
const signOperation_1 = require("../signOperation");
const errors_1 = require("@ledgerhq/errors");
const alpaca_1 = require("../alpaca");
const utils_1 = require("../utils");
jest.mock("../alpaca", () => ({
    getAlpacaApi: jest.fn(),
}));
jest.mock("../utils", () => ({
    buildOptimisticOperation: jest.fn(),
    transactionToIntent: jest.fn(),
}));
describe("genericSignOperation", () => {
    const network = "xrp";
    const kind = "local";
    const mockSignerContext = jest.fn();
    const mockSigner = {
        getAddress: jest.fn(),
        signTransaction: jest.fn(),
    };
    const account = {
        freshAddressPath: "44'/144'/0'/0/0",
        address: "rTestAddress",
    };
    const transaction = {
        amount: 100000n,
        fees: 500n,
        tag: 1234,
    };
    const deviceId = "mockDevice";
    const txIntent = {
        memo: {
            type: "map",
            memos: new Map(),
        },
    };
    const unsignedTx = "unsignedTx";
    const signedTx = "signedTx";
    const pubKey = "pubKey";
    beforeEach(() => {
        jest.clearAllMocks();
        alpaca_1.getAlpacaApi.mockReturnValue({
            craftTransaction: jest.fn().mockResolvedValue(unsignedTx),
            getAccountInfo: jest.fn().mockResolvedValue(pubKey),
            combine: jest.fn().mockResolvedValue(signedTx),
        });
        utils_1.transactionToIntent.mockReturnValue(txIntent);
        utils_1.buildOptimisticOperation.mockReturnValue({ id: "mock-op" });
        mockSigner.getAddress.mockResolvedValue({ publicKey: pubKey });
        mockSigner.signTransaction.mockResolvedValue("sig");
        mockSignerContext.mockImplementation(async (_deviceId, cb) => cb(mockSigner));
    });
    it("emits full sign operation flow", async () => {
        const signOperation = (0, signOperation_1.genericSignOperation)(network, kind)(mockSignerContext);
        const observable = signOperation({ account, transaction, deviceId });
        const events = await (0, rxjs_1.lastValueFrom)(observable.pipe((0, operators_1.toArray)()));
        expect(events[0]).toEqual({ type: "device-signature-requested" });
        expect(events[1]).toEqual({ type: "device-signature-granted" });
        expect(events[2]).toEqual({
            type: "signed",
            signedOperation: {
                operation: { id: "mock-op" },
                signature: signedTx,
            },
        });
        expect(utils_1.transactionToIntent).toHaveBeenCalledWith(account, transaction);
        expect(txIntent.memo.memos.get("destinationTag")).toBe("1234");
    });
    it("throws FeeNotLoaded if fees are missing", async () => {
        const txWithoutFees = { ...transaction };
        delete txWithoutFees.fees;
        const signOperation = (0, signOperation_1.genericSignOperation)(network, kind)(mockSignerContext);
        const observable = signOperation({ account, transaction: txWithoutFees, deviceId });
        await expect(observable.toPromise()).rejects.toThrow(errors_1.FeeNotLoaded);
    });
});
//# sourceMappingURL=signOperation.test.js.map