Skip to content

Commit

Permalink
feat: add rpc send transfer test
Browse files Browse the repository at this point in the history
  • Loading branch information
alter-eggo committed Dec 27, 2024
1 parent 34d6e0d commit 1d1d446
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 1 deletion.
13 changes: 13 additions & 0 deletions tests/mocks/mock-bitcoin-tx.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import type { Page } from '@playwright/test';

import { BITCOIN_API_BASE_URL_TESTNET3 } from '@leather.io/models';

import { mockMainnetNsTransactionsTestAccount } from './mock-utxos';

export async function mockTestAccountBtcBroadcastTransaction(page: Page) {
await page.route(`${BITCOIN_API_BASE_URL_TESTNET3}/tx`, route =>
route.fulfill({
body: mockMainnetNsTransactionsTestAccount[0].txid,
})
);
}
3 changes: 2 additions & 1 deletion tests/mocks/mock-utxos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ export const mockUtxosListWithRunes = [
value: 546,
},
];
const mockMainnetNsTransactionsTestAccount = [

export const mockMainnetNsTransactionsTestAccount = [
{
txid: '58d44000884f0ba4cdcbeb1ac082e6c802d300c16b0d3251738e8cf6a57397ce',
version: 2,
Expand Down
87 changes: 87 additions & 0 deletions tests/specs/rpc-send-transfer/rpc-send-transfer.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { BrowserContext, Page } from '@playwright/test';
import { TEST_TESTNET_ACCOUNT_2_BTC_ADDRESS } from '@tests/mocks/constants';
import { mockTestAccountBtcBroadcastTransaction } from '@tests/mocks/mock-bitcoin-tx';

import { RpcSendTransferParams } from '@leather.io/rpc';

import { test } from '../../fixtures/fixtures';

const baseParams = {
recipients: [
{
address: TEST_TESTNET_ACCOUNT_2_BTC_ADDRESS,
amount: '800',
},
{
address: TEST_TESTNET_ACCOUNT_2_BTC_ADDRESS,
amount: '900',
},
],
network: 'testnet',
};

test.describe('Send transfer (RPC)', () => {
test.beforeEach(async ({ extensionId, globalPage, onboardingPage, page }) => {
await globalPage.setupAndUseApiCalls(extensionId);
await onboardingPage.signInWithTestAccount(extensionId);
await page.goto('localhost:3000', { waitUntil: 'networkidle' });
});

function clickActionButton(context: BrowserContext) {
return async (buttonToPress: 'Cancel' | 'Approve') => {
const popup = await context.waitForEvent('page');
await popup.waitForTimeout(1000);
const btn = popup.locator(`text="${buttonToPress}"`);
await btn.click();
};
}

async function mockPopupRequests(context: BrowserContext) {
const popup = await context.waitForEvent('page');
await mockTestAccountBtcBroadcastTransaction(popup);
}

function openSendTransfer(page: Page) {
return async (params: RpcSendTransferParams) =>
page.evaluate(
async params =>
(window as any).LeatherProvider?.request('sendTransfer', {
...params,
}).catch((e: unknown) => e),
{ ...params }
);
}

test('that the request can be broadcasted', async ({ page, context }) => {
void mockPopupRequests(context);

const [result] = await Promise.all([
openSendTransfer(page)(baseParams),
clickActionButton(context)('Approve'),
]);

delete result.id;

test.expect(result).toEqual({
jsonrpc: '2.0',
result: { txid: '58d44000884f0ba4cdcbeb1ac082e6c802d300c16b0d3251738e8cf6a57397ce' },
});
});

test('that the request can be cancelled', async ({ page, context }) => {
const [result] = await Promise.all([
openSendTransfer(page)(baseParams),
clickActionButton(context)('Cancel'),
]);

delete result.id;

test.expect(result).toEqual({
jsonrpc: '2.0',
error: {
code: 4001,
message: 'User rejected signing the transaction',
},
});
});
});

0 comments on commit 1d1d446

Please sign in to comment.