@ledgerhq/coin-mina
Version:
129 lines • 6.37 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const bignumber_js_1 = require("bignumber.js");
const rxjs_1 = require("rxjs");
const operation_1 = require("@ledgerhq/coin-framework/operation");
const signOperation_1 = require("./signOperation");
const buildTransaction_1 = require("./buildTransaction");
const common_logic_1 = require("../common-logic");
const fixtures_1 = require("../test/fixtures");
// Mock dependencies
jest.mock("@ledgerhq/coin-framework/operation");
jest.mock("./buildTransaction");
jest.mock("../common-logic");
describe("signOperation", () => {
// Mock reset before each test
beforeEach(() => {
jest.clearAllMocks();
// Setup mocks
operation_1.encodeOperationId.mockReturnValue("mock_operation_id");
buildTransaction_1.buildTransaction.mockResolvedValue(mockUnsignedTransaction);
common_logic_1.reEncodeRawSignature.mockReturnValue("encoded_signature");
});
// Use fixtures instead of defining mock data locally
const mockAccount = (0, fixtures_1.createMockAccount)();
const mockTransaction = (0, fixtures_1.createMockTransaction)();
const mockUnsignedTransaction = (0, fixtures_1.createMockUnsignedTransaction)(mockAccount, mockTransaction);
const mockSignerContext = (0, fixtures_1.createMockSignerContext)();
describe("buildOptimisticOperation", () => {
it("should build operation with correct values", () => {
const result = (0, signOperation_1.buildOptimisticOperation)(mockAccount, mockTransaction, new bignumber_js_1.BigNumber(10));
expect(result).toEqual({
id: "mock_operation_id",
hash: "",
type: "OUT",
value: new bignumber_js_1.BigNumber(1010), // amount + fee
fee: new bignumber_js_1.BigNumber(10),
blockHash: null,
blockHeight: null,
senders: [mockAccount.freshAddress],
recipients: [mockTransaction.recipient],
accountId: mockAccount.id,
date: expect.any(Date),
extra: {
memo: "test memo",
accountCreationFee: "0",
},
});
expect(operation_1.encodeOperationId).toHaveBeenCalledWith(mockAccount.id, "", "OUT");
});
it("should subtract account creation fee from value when present", () => {
const txWithCreationFee = (0, fixtures_1.createMockTransaction)({
fees: {
fee: new bignumber_js_1.BigNumber(10),
accountCreationFee: new bignumber_js_1.BigNumber(5),
},
});
const result = (0, signOperation_1.buildOptimisticOperation)(mockAccount, txWithCreationFee, new bignumber_js_1.BigNumber(10));
// Value should be amount + fee - accountCreationFee = 1000 + 10 - 5 = 1005
expect(result.value).toEqual(new bignumber_js_1.BigNumber(1005));
expect(result.extra.accountCreationFee).toBe("5");
});
});
describe("buildSignOperation", () => {
it("should emit correct events and return signed operation", done => {
const signOperation = (0, signOperation_1.buildSignOperation)(mockSignerContext);
const events = [];
const observable = signOperation({
account: mockAccount,
transaction: mockTransaction,
deviceId: fixtures_1.mockDeviceId,
});
observable.subscribe({
next: event => {
events.push(event.type);
if (event.type === "signed") {
const { operation, signature } = event.signedOperation;
// Check operation
expect(operation).toEqual(expect.objectContaining({
id: "mock_operation_id",
type: "OUT",
value: new bignumber_js_1.BigNumber(1010),
fee: new bignumber_js_1.BigNumber(10),
senders: [mockAccount.freshAddress],
recipients: [mockTransaction.recipient],
}));
// Check signature
const parsedSignature = JSON.parse(signature);
expect(parsedSignature).toEqual({
transaction: mockUnsignedTransaction,
signature: "encoded_signature",
});
}
},
complete: () => {
// Check all events were emitted in the correct order
expect(events).toEqual([
"device-signature-requested",
"device-signature-granted",
"signed",
]);
done();
},
error: error => done(error),
});
});
it("should build transaction with correct parameters", async () => {
const signOperation = (0, signOperation_1.buildSignOperation)(mockSignerContext);
const observable = signOperation({
account: mockAccount,
transaction: mockTransaction,
deviceId: fixtures_1.mockDeviceId,
});
await (0, rxjs_1.firstValueFrom)(observable.pipe());
expect(buildTransaction_1.buildTransaction).toHaveBeenCalledWith(mockAccount, mockTransaction);
});
it("should call signer with correct parameters", async () => {
const mockSignerContextSpy = (0, fixtures_1.createMockSignerContext)();
const signOperation = (0, signOperation_1.buildSignOperation)(mockSignerContextSpy);
const observable = signOperation({
account: mockAccount,
transaction: mockTransaction,
deviceId: fixtures_1.mockDeviceId,
});
await (0, rxjs_1.firstValueFrom)(observable.pipe());
expect(mockSignerContextSpy).toHaveBeenCalledWith(fixtures_1.mockDeviceId, expect.any(Function));
});
});
});
//# sourceMappingURL=signOperation.test.js.map