Skip to content

Commit

Permalink
test: fix unit tests for synchronization and network
Browse files Browse the repository at this point in the history
  • Loading branch information
semeano committed Jan 23, 2025
1 parent afd204e commit 05da067
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,29 @@ import { mergeOps } from "@ledgerhq/coin-framework/bridge/jsHelpers";
import { AptosAPI } from "../../api";
import { getAccountShape } from "../../bridge/synchronisation";

jest.mock("@ledgerhq/coin-framework/account");
jest.mock("@ledgerhq/coin-framework/account", () => {
const originalModule = jest.requireActual("@ledgerhq/coin-framework/account");
const partialMockedModule = Object.keys(originalModule).reduce(
(pre: { [key: string]: jest.Mock }, methodName) => {
pre[methodName] = jest.fn();
return pre;
},
{} as { [key: string]: jest.Mock },
);
return {
...partialMockedModule,
// mock all except these
decodeAccountId: originalModule.decodeAccountId,
};
});

const mockedEncodeAccountId = jest.mocked(encodeAccountId);

jest.mock("../../api");
let mockedAptosAPI: jest.Mocked<any>;

jest.mock("@ledgerhq/coin-framework/bridge/jsHelpers");
jest.mock("invariant", () => jest.fn());

describe("getAccountShape", () => {
beforeEach(() => {
Expand Down Expand Up @@ -121,7 +137,7 @@ describe("getAccountShape", () => {
{} as SyncConfig,
);

expect(account.xpub).toEqual("");
expect(account.xpub).toEqual("1");
expect(mockedEncodeAccountId).toHaveBeenCalledTimes(1);
expect(mockedAptosAPI).toHaveBeenCalledTimes(1);
expect(mockGetAccountSpy).toHaveBeenCalledWith("address", undefined);
Expand Down Expand Up @@ -217,7 +233,7 @@ describe("getAccountShape", () => {
{} as SyncConfig,
);

expect(account.xpub).toEqual("");
expect(account.xpub).toEqual("1");
expect(mockedEncodeAccountId).toHaveBeenCalledTimes(1);
expect(mockedAptosAPI).toHaveBeenCalledTimes(1);
expect(mockGetAccountSpy).toHaveBeenCalledWith("address", undefined);
Expand Down Expand Up @@ -283,7 +299,74 @@ describe("getAccountShape", () => {
{} as SyncConfig,
);

expect(account.xpub).toEqual("");
expect(account.xpub).toEqual("1");
expect(mockedEncodeAccountId).toHaveBeenCalledTimes(1);
expect(mockedAptosAPI).toHaveBeenCalledTimes(1);
expect(mockGetAccountSpy).toHaveBeenCalledWith("address", 1);
});

it("get publicKey from rest", async () => {
const mockGetAccountInfo = jest.fn().mockImplementation(async () => ({
balance: BigInt(0),
transactions: [],
blockHeight: 0,
}));
mockedAptosAPI.mockImplementation(() => ({
getAccountInfo: mockGetAccountInfo,
}));
const mockGetAccountSpy = jest.spyOn({ getAccount: mockGetAccountInfo }, "getAccount");

const account = await getAccountShape(
{
id: "1",
address: "address",
currency: getCryptoCurrencyById("aptos"),
derivationMode: "",
index: 0,
xpub: "address",
derivationPath: "",
deviceId: "1",
initialAccount: {
id: "1:1:1:1:1",
seedIdentifier: "1",
derivationMode: "",
index: 0,
freshAddress: "address",
freshAddressPath: "",
used: true,
balance: BigInt(10),
spendableBalance: BigInt(10),
creationDate: new Date(),
blockHeight: 0,
currency: getCryptoCurrencyById("aptos"),
t: 1,
operations: [
{
id: "1",
hash: "hash",
type: "OUT",
value: BigInt(10),
fee: BigInt(0),
blockHeight: 0,
blockHash: "blockHash",
accountId: "1",
senders: ["sender"],
recipients: ["recipient"],
date: new Date(),
extra: { version: 1 },
},
],
pendingOperations: [],
lastSyncDate: new Date(),
balanceHistoryCache: {},
swapHistory: [],
},
rest: { publicKey: "restPublicKey" },
} as unknown as AccountShapeInfo<Account>,
{} as SyncConfig,
);

expect(account.xpub).toEqual("restPublicKey");
expect(mockedEncodeAccountId).toHaveBeenCalledTimes(1);
expect(mockedAptosAPI).toHaveBeenCalledTimes(1);
expect(mockGetAccountSpy).toHaveBeenCalledWith("address", 1);
Expand Down
10 changes: 5 additions & 5 deletions libs/coin-modules/coin-aptos/src/__tests__/network/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ import {
} from "@aptos-labs/ts-sdk";

jest.mock("@aptos-labs/ts-sdk", () => {
const originalAptosLabs = jest.requireActual("@aptos-labs/ts-sdk");
const partialMockedAptosLabs = Object.keys(originalAptosLabs).reduce(
const originalModule = jest.requireActual("@aptos-labs/ts-sdk");
const partialMockedModule = Object.keys(originalModule).reduce(
(pre: { [key: string]: jest.Mock }, methodName) => {
pre[methodName] = jest.fn();
return pre;
},
{} as { [key: string]: jest.Mock },
);
return {
...partialMockedAptosLabs,
...partialMockedModule,
// mock all except these
AccountAddress: originalAptosLabs.AccountAddress,
Hex: originalAptosLabs.Hex,
AccountAddress: originalModule.AccountAddress,
Hex: originalModule.Hex,
};
});
let mockedGenerateSigningMessageForTransaction: jest.Mocked<any>;
Expand Down

2 comments on commit 05da067

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Bot] Testing with 'Nitrogen' ($0.00) ⏲ 1416ms

What is the bot and how does it work? Everything is documented here!

1 critical spec errors

Spec Aptos failed!

Error: speculos process failure. vnc_server: readall: connection closed

Details of the 0 mutations

Spec Aptos (failed)


Portfolio ($0.00) – Details of the 1 currencies
Spec (accounts) State Remaining Runs (est) funds?
Aptos (0) 0 ops , 🤷‍♂️ ``

Performance ⏲ 1416ms

Time spent for each spec: (total across mutations)

Spec (accounts) preload scan re-sync tx status sign op broadcast test destination test
TOTAL N/A N/A N/A N/A N/A N/A N/A N/A
Aptos (0) N/A N/A N/A N/A N/A N/A N/A N/A

What is the bot and how does it work? Everything is documented here!

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Bot] Testing with 'Nitrogen' 💰 1 miss funds ($4.78) ⏲ 3.9s

💰 1 specs may miss funds: Aptos

What is the bot and how does it work? Everything is documented here!

⚠️ 1 spec hints
  • Spec Aptos:
    • No mutation were found possible. Yet there are funds in the accounts, please investigate.
Details of the 0 mutations

Spec Aptos (2)

Spec Aptos found 2 Aptos accounts. Will use Aptos 0.6.10 on nanoSP 1.1.2
undefined: 0.573619 APT (1ops) (0xc9ce406c17508f44ed2f1dbd62e0a0363a4335afe2e77dd9d174fffb346293be on 44'/637'/0'/0/0) #0 js:2:aptos:11fd81d9637768b091212591ba1090840b14d3349265e9ea734657c14a0f0b49:
undefined: 0 APT (0ops) (0x4a0939b013ec25060d431f21f1e21a332d03cd3361ba478f86daa541ff176556 on 44'/637'/1'/0/0) #1 js:2:aptos:6e66925078c1e3c2c59078c9292aa9a77ffeb2358e55f709c6184e12f23a123f:

Portfolio ($4.78) – Details of the 1 currencies
Spec (accounts) State Remaining Runs (est) funds?
Aptos (2) 1 ops , 0.573619 APT ($4.78) 0xc9ce406c17508f44ed2f1dbd62e0a0363a4335afe2e77dd9d174fffb346293be
undefined: 0.573619 APT (1ops) (0xc9ce406c17508f44ed2f1dbd62e0a0363a4335afe2e77dd9d174fffb346293be on 44'/637'/0'/0/0) #0 js:2:aptos:11fd81d9637768b091212591ba1090840b14d3349265e9ea734657c14a0f0b49:
undefined: 0 APT (0ops) (0x4a0939b013ec25060d431f21f1e21a332d03cd3361ba478f86daa541ff176556 on 44'/637'/1'/0/0) #1 js:2:aptos:6e66925078c1e3c2c59078c9292aa9a77ffeb2358e55f709c6184e12f23a123f:
Performance ⏲ 3.9s

Time spent for each spec: (total across mutations)

Spec (accounts) preload scan re-sync tx status sign op broadcast test destination test
TOTAL 1.01ms 1829ms 0.40ms N/A N/A N/A N/A N/A
Aptos (1) 1.01ms 1829ms 0.40ms N/A N/A N/A N/A N/A

What is the bot and how does it work? Everything is documented here!

Please sign in to comment.