Skip to content

Commit

Permalink
test: improve cli calls
Browse files Browse the repository at this point in the history
  • Loading branch information
abdurrahman-ledger committed Dec 6, 2024
1 parent df6ba89 commit fc04d9f
Show file tree
Hide file tree
Showing 9 changed files with 196 additions and 381 deletions.
35 changes: 6 additions & 29 deletions apps/ledger-live-desktop/tests/fixtures/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,7 @@ import { captureArtifacts } from "tests/utils/allureUtils";
import { randomUUID } from "crypto";
import { AppInfos } from "@ledgerhq/live-common/e2e/enum/AppInfos";
import { lastValueFrom, Observable } from "rxjs";
import { commandCLI } from "tests/utils/cliUtils";
import { registerSpeculosTransport } from "@ledgerhq/live-cli/src/live-common-setup";
import { activateLedgerSync } from "@ledgerhq/live-common/e2e/speculos";

type Command<T extends (...args: any) => Observable<any> | Promise<any> | string> = {
command: T;
args: Parameters<T>[0]; // Infer the first argument type
output?: (output: any) => void;
};

type TestFixtures = {
lang: string;
Expand All @@ -38,7 +30,7 @@ type TestFixtures = {
featureFlags: OptionalFeatureMap;
simulateCamera: string;
app: Application;
cliCommands: Command<(typeof commandCLI)[keyof typeof commandCLI]>[];
cliCommands?: ((appjsonPath: string) => Observable<unknown> | Promise<unknown> | string)[];
};

const IS_NOT_MOCK = process.env.MOCK == "0";
Expand Down Expand Up @@ -121,31 +113,16 @@ export const test = base.extend<TestFixtures>({
setEnv("SPECULOS_API_PORT", device?.ports.apiPort?.toString());
setEnv("MOCK", "");

if (cliCommands) {
if (cliCommands?.length) {
registerSpeculosTransport(device?.ports.apiPort);
for (const command of cliCommands) {
if (command.args && "appjson" in command.args) {
command.args.appjson = `${userdataDestinationPath}/app.json`;
}

const resultPromise = handleResult(command.command(command.args as any));

if (command.args && "getKeyRingTree" in command.args) {
await activateLedgerSync();
}

const result = await resultPromise;
command?.output?.(result);
for (const cmd of cliCommands) {
const promise = await cmd(`${userdataDestinationPath}/app.json`);
const result =
promise instanceof Observable ? await lastValueFrom(promise) : await promise;
console.log("CLI result: ", result);
}
}
}
async function handleResult(result: Promise<any> | Observable<any> | string): Promise<any> {
if (result instanceof Observable) {
return lastValueFrom(result); // Converts Observable to Promise
}
return result; // Return Promise directly
}

// default environment variables
env = Object.assign(
Expand Down
29 changes: 13 additions & 16 deletions apps/ledger-live-desktop/tests/specs/speculos/delegate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Account } from "@ledgerhq/live-common/e2e/enum/Account";
import { Delegate } from "../../models/Delegate";
import { addTmsLink } from "tests/utils/allureUtils";
import { getDescription } from "../../utils/customJsonReporter";
import { commandCLI } from "tests/utils/cliUtils";
import { CLI } from "tests/utils/cliUtils";
import { isRunningInScheduledWorkflow } from "tests/utils/githubUtils";

const e2eDelegationAccounts = [
Expand Down Expand Up @@ -59,14 +59,13 @@ test.describe("Delegate flows", () => {
userdata: "skip-onboarding",
speculosApp: account.delegate.account.currency.speculosApp,
cliCommands: [
{
command: commandCLI.liveData,
args: {
(appjsonPath: string) => {
return CLI.liveData({
currency: account.delegate.account.currency.ticker,
index: account.delegate.account.index,
add: true,
appjson: "",
},
appjson: appjsonPath,
});
},
],
});
Expand Down Expand Up @@ -114,14 +113,13 @@ test.describe("Delegate flows", () => {
userdata: "skip-onboarding",
speculosApp: validator.delegate.account.currency.speculosApp,
cliCommands: [
{
command: commandCLI.liveData,
args: {
(appjsonPath: string) => {
return CLI.liveData({
currency: validator.delegate.account.currency.ticker,
index: validator.delegate.account.index,
add: true,
appjson: "",
},
appjson: appjsonPath,
});
},
],
});
Expand Down Expand Up @@ -157,14 +155,13 @@ test.describe("Delegate flows", () => {
userdata: "skip-onboarding",
speculosApp: delegateAccount.account.currency.speculosApp,
cliCommands: [
{
command: commandCLI.liveData,
args: {
(appjsonPath: string) => {
return CLI.liveData({
currency: delegateAccount.account.currency.ticker,
index: delegateAccount.account.index,
add: true,
appjson: "",
},
appjson: appjsonPath,
});
},
],
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { test } from "../../fixtures/common";
import { Account } from "@ledgerhq/live-common/e2e/enum/Account";
import { addTmsLink } from "tests/utils/allureUtils";
import { getDescription } from "../../utils/customJsonReporter";
import { commandCLI } from "tests/utils/cliUtils";
import { CLI } from "tests/utils/cliUtils";

const accounts = [
{ account: Account.BTC_NATIVE_SEGWIT_1, xrayTicket: "B2CQA-2548" },
Expand All @@ -24,14 +24,13 @@ for (const account of accounts) {
test.use({
userdata: "skip-onboarding",
cliCommands: [
{
command: commandCLI.liveData,
args: {
(appjsonPath: string) => {
return CLI.liveData({
currency: account.account.currency.currencyId,
index: account.account.index,
appjson: "",
add: true,
},
appjson: appjsonPath,
});
},
],
speculosApp: account.account.currency.speculosApp,
Expand Down
74 changes: 42 additions & 32 deletions apps/ledger-live-desktop/tests/specs/speculos/ledgerSync.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,65 @@ import { test } from "../../fixtures/common";
import { AppInfos } from "@ledgerhq/live-common/e2e/enum/AppInfos";
import { addTmsLink } from "tests/utils/allureUtils";
import { getDescription } from "../../utils/customJsonReporter";
import { commandCLI } from "tests/utils/cliUtils";
import { CLI } from "tests/utils/cliUtils";
import { activateLedgerSync } from "@ledgerhq/live-common/e2e/speculos";

const app: AppInfos = AppInfos.LS;

test.describe(`[${app.name}] Sync Accounts`, () => {
const ledgerKeyRingProtocolArgs = {
getKeyRingTree: true,
pubKey: undefined,
privateKey: undefined,
pubKey: "",
privateKey: "",
};
const ledgerSyncPushDataArgs = {
pubKey: undefined,
privateKey: undefined,
rootId: undefined,
walletSyncEncryptionKey: undefined,
applicationPath: undefined,
rootId: "",
walletSyncEncryptionKey: "",
applicationPath: "",
push: true,
data: '{"accounts":[{"id":"mock:1:dogecoin:0.790010769447963:","currencyId":"dogecoin","index":1,"seedIdentifier":"mock","derivationMode":"","freshAddress":"1uVnrWAzycYqKUXSuNXt3XSjJ8"},{"id":"mock:1:bitcoin_gold:0.8027791663782486:","currencyId":"bitcoin_gold","index":1,"seedIdentifier":"mock","derivationMode":"","freshAddress":"1Y5T8JQqBKUS7cXbxUYCR4wg3YSbV9R"}],"accountNames":{"mock:1:dogecoin:0.790010769447963:":"Renamed Dogecoin 2","mock:1:bitcoin_gold:0.8027791663782486:":"Bitcoin Gold 2"}}',
};

async function initializeLedgerKeyRingProtocol() {
return CLI.ledgerKeyRingProtocol({ initMemberCredentials: true }).then(output => {
if (output && typeof output !== "string" && "pubkey" in output) {
ledgerKeyRingProtocolArgs.pubKey = output.pubkey;
ledgerKeyRingProtocolArgs.privateKey = output.privatekey;
}
return output;
});
}

async function initializeLedgerSync() {
const output = CLI.ledgerKeyRingProtocol({
getKeyRingTree: true,
...ledgerKeyRingProtocolArgs,
}).then(out => {
if (out && typeof out !== "string" && "rootId" in out) {
ledgerSyncPushDataArgs.rootId = out.rootId;
ledgerSyncPushDataArgs.walletSyncEncryptionKey = out.walletSyncEncryptionKey;
ledgerSyncPushDataArgs.applicationPath = out.applicationPath;
}
return out;
});
await activateLedgerSync();
return output;
}

test.use({
userdata: "skip-onboarding",
speculosApp: app,
cliCommands: [
{
command: commandCLI.ledgerKeyRingProtocol,
args: {
initMemberCredentials: true,
},
output: output => {
ledgerKeyRingProtocolArgs.pubKey = output.pubkey;
ledgerKeyRingProtocolArgs.privateKey = output.privatekey;

ledgerSyncPushDataArgs.pubKey = output.pubkey;
ledgerSyncPushDataArgs.privateKey = output.privatekey;
},
async () => {
return initializeLedgerKeyRingProtocol();
},
{
command: commandCLI.ledgerKeyRingProtocol,
args: ledgerKeyRingProtocolArgs,
output: output => {
ledgerSyncPushDataArgs.rootId = output.rootId;
ledgerSyncPushDataArgs.walletSyncEncryptionKey = output.walletSyncEncryptionKey;
ledgerSyncPushDataArgs.applicationPath = output.applicationPath;
},
async () => {
return initializeLedgerSync();
},
{
command: commandCLI.ledgerSync,
args: ledgerSyncPushDataArgs,
async () => {
return CLI.ledgerSync({
...ledgerKeyRingProtocolArgs,
...ledgerSyncPushDataArgs,
});
},
],
});
Expand Down
38 changes: 17 additions & 21 deletions apps/ledger-live-desktop/tests/specs/speculos/nft.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Nft } from "@ledgerhq/live-common/e2e/enum/Nft";
import { Fee } from "@ledgerhq/live-common/e2e/enum/Fee";
import { addTmsLink } from "tests/utils/allureUtils";
import { getDescription } from "../../utils/customJsonReporter";
import { commandCLI } from "tests/utils/cliUtils";
import { CLI } from "tests/utils/cliUtils";
import invariant from "invariant";

test.describe("send NFT to ENS address", () => {
Expand All @@ -19,14 +19,13 @@ test.describe("send NFT to ENS address", () => {
test.use({
userdata: "skip-onboarding",
cliCommands: [
{
command: commandCLI.liveData,
args: {
(appjsonPath: string) => {
return CLI.liveData({
currency: transaction.accountToDebit.currency.currencyId,
index: transaction.accountToDebit.index,
appjson: "",
add: true,
},
appjson: appjsonPath,
});
},
],
speculosApp: transaction.accountToDebit.currency.speculosApp,
Expand Down Expand Up @@ -67,14 +66,13 @@ test.describe("The user can see his NFT floor price", () => {
test.use({
userdata: "skip-onboarding",
cliCommands: [
{
command: commandCLI.liveData,
args: {
(appjsonPath: string) => {
return CLI.liveData({
currency: account.currency.currencyId,
index: account.index,
appjson: "",
add: true,
},
appjson: appjsonPath,
});
},
],
speculosApp: account.currency.speculosApp,
Expand Down Expand Up @@ -117,14 +115,13 @@ for (const account of accounts) {
test.use({
userdata: "skip-onboarding",
cliCommands: [
{
command: commandCLI.liveData,
args: {
(appjsonPath: string) => {
return CLI.liveData({
currency: account.account.currency.currencyId,
index: account.account.index,
appjson: "",
add: true,
},
appjson: appjsonPath,
});
},
],
speculosApp: account.account.currency.speculosApp,
Expand Down Expand Up @@ -153,14 +150,13 @@ for (const account of accounts) {
test.use({
userdata: "skip-onboarding",
cliCommands: [
{
command: commandCLI.liveData,
args: {
(appjsonPath: string) => {
return CLI.liveData({
currency: account.account.currency.currencyId,
index: account.account.index,
appjson: "",
add: true,
},
appjson: appjsonPath,
});
},
],
speculosApp: account.account.currency.speculosApp,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { test } from "../../fixtures/common";
import { Account } from "@ledgerhq/live-common/e2e/enum/Account";
import { addTmsLink } from "tests/utils/allureUtils";
import { getDescription } from "../../utils/customJsonReporter";
import { commandCLI } from "tests/utils/cliUtils";
import { CLI } from "tests/utils/cliUtils";

const accounts = [
{ account: Account.BTC_NATIVE_SEGWIT_1, xrayTicket: "B2CQA-2559, B2CQA-2687" },
Expand All @@ -24,14 +24,13 @@ for (const account of accounts) {
userdata: "skip-onboarding",
speculosApp: account.account.currency.speculosApp,
cliCommands: [
{
command: commandCLI.liveData,
args: {
(appjsonPath: string) => {
return CLI.liveData({
currency: account.account.currency.currencyId,
index: account.account.index,
appjson: "",
add: true,
},
appjson: appjsonPath,
});
},
],
});
Expand Down Expand Up @@ -82,14 +81,13 @@ test.describe("Receive", () => {
userdata: "skip-onboarding",
speculosApp: account.currency.speculosApp,
cliCommands: [
{
command: commandCLI.liveData,
args: {
(appjsonPath: string) => {
return CLI.liveData({
currency: account.currency.currencyId,
index: account.index,
add: true,
appjson: "",
},
appjson: appjsonPath,
});
},
],
});
Expand Down
Loading

0 comments on commit fc04d9f

Please sign in to comment.