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

chore: update the 'sendMessage' method in the 'DeeplinkProtocolService' class #10304

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
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import handleBatchRpcResponse from '../handlers/handleBatchRpcResponse';
import handleCustomRpcCalls from '../handlers/handleCustomRpcCalls';
import DevLogger from '../utils/DevLogger';
import DeeplinkProtocolService from './DeeplinkProtocolService';
import AppConstants from '../../AppConstants';
import { DappClient } from '../AndroidSDK/dapp-sdk-types';

jest.mock('../SDKConnect');
jest.mock('../../../core/Engine');
Expand Down Expand Up @@ -134,7 +136,9 @@ describe('DeeplinkProtocolService', () => {

it('should handle batch RPC responses', async () => {
const mockChainRPCs = [{ id: '1' }];
const mockMessage = { data: { id: '1', error: null } };
const mockMessage = {
data: { id: '1', accounts: [], chainId: '0x1', error: null },
};
service.batchRPCManager.getById = jest
.fn()
.mockReturnValue(mockChainRPCs);
Expand Down Expand Up @@ -165,7 +169,14 @@ describe('DeeplinkProtocolService', () => {
});

it('should handle error in message data', async () => {
const mockMessage = { data: { id: '1', error: new Error('Test error') } };
const mockMessage = {
data: {
id: '1',
accounts: [],
chainId: '0x1',
error: new Error('Test error'),
},
};
const openDeeplinkSpy = jest.spyOn(service, 'openDeeplink');

service.currentClientId = 'client1';
Expand Down Expand Up @@ -204,7 +215,14 @@ describe('DeeplinkProtocolService', () => {

it('should handle non-final batch RPC response and error in message data', async () => {
const mockChainRPCs = [{ id: '1' }];
const mockMessage = { data: { id: '1', error: new Error('Test error') } };
const mockMessage = {
data: {
id: '1',
accounts: [],
chainId: '0x1',
error: new Error('Test error'),
},
};
const devLoggerSpy = jest.spyOn(DevLogger, 'log');
const openDeeplinkSpy = jest.spyOn(service, 'openDeeplink');
service.batchRPCManager.getById = jest
Expand Down Expand Up @@ -322,6 +340,122 @@ describe('DeeplinkProtocolService', () => {
});
});

describe('handleConnectionEventAsync', () => {
let clientInfo: DappClient;

let params: {
dappPublicKey: string;
url: string;
scheme: string;
channelId: string;
originatorInfo?: string;
request?: string;
};

beforeEach(() => {
clientInfo = {
clientId: 'client1',
originatorInfo: {
url: 'test.com',
title: 'Test',
platform: 'test',
dappId: 'dappId',
},
connected: false,
validUntil: Date.now(),
scheme: 'scheme1',
};
params = {
dappPublicKey: 'key',
url: 'url',
scheme: 'scheme1',
channelId: 'client1',
originatorInfo: Buffer.from(
JSON.stringify({
originatorInfo: {
url: 'test.com',
title: 'Test',
platform: 'test',
dappId: 'dappId',
},
}),
).toString('base64'),
request: JSON.stringify({ id: '1', method: 'test', params: [] }),
};

// Mocking methods
service.checkPermission = jest.fn().mockResolvedValue(null);
service.setupBridge = jest.fn();
service.sendMessage = jest.fn().mockResolvedValue(null);
service.processDappRpcRequest = jest.fn().mockResolvedValue(null);
service.openDeeplink = jest.fn().mockResolvedValue(null);

(Engine.context as unknown) = {
PermissionController: {
requestPermissions: jest.fn().mockResolvedValue(null),
},
KeyringController: {
unlock: jest.fn().mockResolvedValue(null),
isUnlocked: jest.fn().mockReturnValue(true),
},
PreferencesController: {
state: {
selectedAddress: '0xAddress',
},
},
};
});

it('should setup a new client bridge if the connection does not exist', async () => {
await service.handleConnectionEventAsync({ clientInfo, params });
expect(service.checkPermission).toHaveBeenCalledWith({
originatorInfo: clientInfo.originatorInfo,
channelId: clientInfo.clientId,
});
expect(service.setupBridge).toHaveBeenCalledWith(clientInfo);
expect(SDKConnect.getInstance().addDappConnection).toHaveBeenCalledWith({
id: clientInfo.clientId,
lastAuthorized: expect.any(Number),
origin: AppConstants.MM_SDK.IOS_SDK,
originatorInfo: clientInfo.originatorInfo,
otherPublicKey: service.dappPublicKeyByClientId[clientInfo.clientId],
validUntil: expect.any(Number),
scheme: clientInfo.scheme,
});
});

it('should update existing client connection and process request if exists', async () => {
service.connections[clientInfo.clientId] = clientInfo;
await service.handleConnectionEventAsync({ clientInfo, params });
expect(service.processDappRpcRequest).toHaveBeenCalledWith(params);
});

it('should send error message if connection event fails', async () => {
(service.checkPermission as jest.Mock).mockRejectedValue(
new Error('Permission error'),
);
await service.handleConnectionEventAsync({ clientInfo, params });
expect(service.sendMessage).toHaveBeenCalledWith({
data: {
error: new Error('Permission error'),
jsonrpc: '2.0',
},
name: 'metamask-provider',
});
expect(service.openDeeplink).toHaveBeenCalledWith({
message: {
data: {
error: new Error('Permission error'),
jsonrpc: '2.0',
},
name: 'metamask-provider',
},
clientId: '',
scheme: clientInfo.scheme,
});
});
});

describe('processDappRpcRequest', () => {
it('should process a dapp RPC request', async () => {
const params = {
Expand Down
Loading
Loading