Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add rpc send transfer test #6047

Merged
merged 1 commit into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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',
},
});
});
});
Loading