Skip to content

Commit

Permalink
chore: improve open API and add tests, ref leather-io/issues#5800
Browse files Browse the repository at this point in the history
  • Loading branch information
pete-watters committed Sep 3, 2024
1 parent 6b9566a commit e16c9d6
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 12 deletions.
41 changes: 30 additions & 11 deletions src/background/messaging/rpc-methods/open.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { RpcErrorCode } from '@btckit/types';

import { RouteUrls } from '@shared/route-urls';
import { OpenRequest } from '@shared/rpc/methods/open';
import { makeRpcErrorResponse } from '@shared/rpc/rpc-methods';
import { makeRpcErrorResponse, makeRpcSuccessResponse } from '@shared/rpc/rpc-methods';

import {
listenForPopupClose,
Expand All @@ -13,18 +13,37 @@ import { trackRpcRequestSuccess } from '../rpc-message-handler';

export async function rpcOpen(message: OpenRequest, port: chrome.runtime.Port) {
const { urlParams, tabId } = makeSearchParamsWithDefaults(port, [['requestId', message.id]]);
const { id } = await triggerRequestWindowOpen(RouteUrls.Home, urlParams);
void trackRpcRequestSuccess({ endpoint: message.method });

listenForPopupClose({
tabId,
id,
response: makeRpcErrorResponse('open', {
try {
const { id } = await triggerRequestWindowOpen(RouteUrls.Home, urlParams);
void trackRpcRequestSuccess({ endpoint: message.method });

chrome.tabs.sendMessage(
tabId,
makeRpcSuccessResponse('open', {
id: message.id,
result: { message: 'Success' },
})
);

listenForPopupClose({
tabId,
id,
response: makeRpcErrorResponse('open', {
id: message.id,
error: {
code: RpcErrorCode.USER_REJECTION,
message: 'User rejected request to open wallet',
},
}),
});
} catch (error) {
makeRpcErrorResponse('open', {
id: message.id,
error: {
code: RpcErrorCode.USER_REJECTION,
message: 'User rejected request to open wallet',
code: RpcErrorCode.SERVER_ERROR,
message: 'Request to open wallet failed',
},
}),
});
});
}
}
14 changes: 14 additions & 0 deletions src/shared/rpc/methods/open-wallet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { DefineRpcMethod, RpcRequest, RpcResponse } from '@btckit/types';
import * as yup from 'yup';

const rpcOpenWalletParamsSchema = yup.object().shape({
url: yup.string(),
});

type OpenWalletRequestParams = yup.InferType<typeof rpcOpenWalletParamsSchema>;

export type OpenWalletRequest = RpcRequest<'openWallet', OpenWalletRequestParams>;

type OpenWalletResponse = RpcResponse<Response>;

export type OpenWallet = DefineRpcMethod<OpenWalletRequest, OpenWalletResponse>;
2 changes: 1 addition & 1 deletion src/shared/rpc/methods/open.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ type OpenRequestParams = yup.InferType<typeof rpcOpenParamsSchema>;

export type OpenRequest = RpcRequest<'open', OpenRequestParams>;

type OpenResponse = RpcResponse<Response>;
type OpenResponse = RpcResponse<{ message: string }>;

export type Open = DefineRpcMethod<OpenRequest, OpenResponse>;
72 changes: 72 additions & 0 deletions tests/specs/rpc-open/open.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import type { BrowserContext, Page } from '@playwright/test';
import { TEST_PASSWORD } from '@tests/mocks/constants';
import { HomePageSelectors } from '@tests/selectors/home.selectors';

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

const successResponse = {
jsonrpc: '2.0',
result: {
message: 'Success',
},
};

async function interceptRequestPopup(context: BrowserContext) {
return context.waitForEvent('page');
}

async function assertWalletHomeOpens(popup: Page) {
await popup.getByTestId(HomePageSelectors.HomePageContainer).waitFor();
// await page.getByTestId(HomePageSelectors.HomePageContainer).waitFor();
const button = popup.getByTestId(HomePageSelectors.FundAccountBtn);
await test.expect(button).toBeVisible();
// await button.click();
}
async function initiateOpen(page: Page) {
return page.evaluate(async () => (window as any).LeatherProvider?.request('open'));
}

test.describe('Rpc: Open', () => {
test.beforeEach(async ({ extensionId, globalPage, onboardingPage }) => {
await globalPage.setupAndUseApiCalls(extensionId);

await onboardingPage.signInWithTestAccount(extensionId);
});

test('the wallet opens successfully', async ({ page, context }) => {
await page.goto('localhost:3000');
const openPromise = await initiateOpen(page);

const popup = await interceptRequestPopup(context);
await assertWalletHomeOpens(popup);

const result = await openPromise;
if (!result) throw new Error('Expected result');
const { id, ...payloadWithoutId } = result;

await test.expect(payloadWithoutId).toEqual(successResponse);
});

test('the promise rejects when user closes the popup window', async ({ page, context }) => {
await page.goto('localhost:3000');
const openPromise = initiateOpen(page);
const popup = await interceptRequestPopup(context);
await popup.close();
await test.expect(openPromise).rejects.toThrow();

Check failure on line 55 in tests/specs/rpc-open/open.spec.ts

View workflow job for this annotation

GitHub Actions / Shard 5 of 10

[chromium] › specs/rpc-open/open.spec.ts:50:3 › Rpc: Open › the promise rejects when user closes the popup window

1) [chromium] › specs/rpc-open/open.spec.ts:50:3 › Rpc: Open › the promise rejects when user closes the popup window Error: expect(received).rejects.toThrow() Received promise resolved instead of rejected Resolved to value: {"id": "12818001-6337-4999-8963-da11d43e7fee", "jsonrpc": "2.0", "result": {"message": "Success"}} 53 | const popup = await interceptRequestPopup(context); 54 | await popup.close(); > 55 | await test.expect(openPromise).rejects.toThrow(); | ^ 56 | }); 57 | 58 | test('it forces user to unlock wallet when wallet is locked', async ({ at /home/runner/work/extension/extension/tests/specs/rpc-open/open.spec.ts:55:44

Check failure on line 55 in tests/specs/rpc-open/open.spec.ts

View workflow job for this annotation

GitHub Actions / Shard 5 of 10

[chromium] › specs/rpc-open/open.spec.ts:50:3 › Rpc: Open › the promise rejects when user closes the popup window

1) [chromium] › specs/rpc-open/open.spec.ts:50:3 › Rpc: Open › the promise rejects when user closes the popup window Retry #1 ─────────────────────────────────────────────────────────────────────────────────────── Error: expect(received).rejects.toThrow() Received promise resolved instead of rejected Resolved to value: {"id": "07acf4b0-9b11-4503-9e72-5315cd833ab1", "jsonrpc": "2.0", "result": {"message": "Success"}} 53 | const popup = await interceptRequestPopup(context); 54 | await popup.close(); > 55 | await test.expect(openPromise).rejects.toThrow(); | ^ 56 | }); 57 | 58 | test('it forces user to unlock wallet when wallet is locked', async ({ at /home/runner/work/extension/extension/tests/specs/rpc-open/open.spec.ts:55:44

Check failure on line 55 in tests/specs/rpc-open/open.spec.ts

View workflow job for this annotation

GitHub Actions / Shard 5 of 10

[chromium] › specs/rpc-open/open.spec.ts:50:3 › Rpc: Open › the promise rejects when user closes the popup window

1) [chromium] › specs/rpc-open/open.spec.ts:50:3 › Rpc: Open › the promise rejects when user closes the popup window Retry #2 ─────────────────────────────────────────────────────────────────────────────────────── Error: expect(received).rejects.toThrow() Received promise resolved instead of rejected Resolved to value: {"id": "edb44f43-d1f2-4030-bd9a-3ea21e9223d7", "jsonrpc": "2.0", "result": {"message": "Success"}} 53 | const popup = await interceptRequestPopup(context); 54 | await popup.close(); > 55 | await test.expect(openPromise).rejects.toThrow(); | ^ 56 | }); 57 | 58 | test('it forces user to unlock wallet when wallet is locked', async ({ at /home/runner/work/extension/extension/tests/specs/rpc-open/open.spec.ts:55:44
});

test('it forces user to unlock wallet when wallet is locked', async ({
homePage,
page,
context,
}) => {
await homePage.lock();
await page.goto('localhost:3000');
const openPromise = initiateOpen(page);
const popup = await interceptRequestPopup(context);
await popup.getByRole('textbox').fill(TEST_PASSWORD);
await popup.getByRole('button', { name: 'Continue' }).click();
await assertWalletHomeOpens(popup);
await test.expect(openPromise).resolves.toMatchObject(successResponse);
});
});

0 comments on commit e16c9d6

Please sign in to comment.