Skip to content

Commit 99de17e

Browse files
Revert "test: split Solana WebSocket Mock setup logic from Websocket Mocks (#35552)"
This reverts commit 44307c4.
1 parent ac99dd4 commit 99de17e

File tree

6 files changed

+82
-128
lines changed

6 files changed

+82
-128
lines changed

test/e2e/helpers.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const {
2626
getServerMochaToBackground,
2727
} = require('./background-socket/server-mocha-to-background');
2828
const LocalWebSocketServer = require('./websocket-server').default;
29-
const { setupSolanaWebsocketMocks } = require('./websocket-solana-mocks');
29+
const { setSolanaWebsocketMocks } = require('./websocket-solana-mocks');
3030

3131
const tinyDelayMs = 200;
3232
const regularDelayMs = tinyDelayMs * 2;
@@ -128,10 +128,7 @@ async function withFixtures(options, testSuite) {
128128
ethConversionInUsd,
129129
monConversionInUsd,
130130
manifestFlags,
131-
withSolanaWebSocket = {
132-
server: false,
133-
mocks: [],
134-
},
131+
withSolanaWebSocket = false,
135132
} = options;
136133

137134
// Normalize localNodeOptions
@@ -264,10 +261,12 @@ async function withFixtures(options, testSuite) {
264261
}
265262
}
266263

267-
if (withSolanaWebSocket.server) {
264+
if (withSolanaWebSocket) {
268265
localWebSocketServer = LocalWebSocketServer.getServerInstance();
269266
localWebSocketServer.start();
270-
await setupSolanaWebsocketMocks(withSolanaWebSocket.mocks);
267+
// All specs use the same ws mocks.
268+
// If we need custom ws mocks we can expand logic for supporting custom ws mocks like with http
269+
await setSolanaWebsocketMocks();
271270
}
272271

273272
const { mockedEndpoint, getPrivacyReport } = await setupMocking(
@@ -455,7 +454,7 @@ async function withFixtures(options, testSuite) {
455454
})(),
456455
);
457456

458-
if (withSolanaWebSocket.server) {
457+
if (withSolanaWebSocket) {
459458
shutdownTasks.push(localWebSocketServer.stopAndCleanup());
460459
}
461460

test/e2e/mock-e2e.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ const privateHostMatchers = [
143143
* @param {object} options - Network mock options.
144144
* @param {string} options.chainId - The chain ID used by the default configured network.
145145
* @param {string} options.ethConversionInUsd - The USD conversion rate for ETH.
146-
* @param {object} withSolanaWebSocket - Solana WebSocket configuration with server flag and mocks function
146+
* @param {boolean} withSolanaWebSocket - If we want to re-route all the ws requests to our Solana Local WS server
147147
* @returns {Promise<SetupMockReturn>}
148148
*/
149149
async function setupMocking(
@@ -946,7 +946,7 @@ async function setupMocking(
946946
* Solana Websocket
947947
* Setup HTTP intercept for WebSocket handshake requests
948948
*/
949-
if (withSolanaWebSocket.server) {
949+
if (withSolanaWebSocket) {
950950
await server
951951
.forAnyWebSocket()
952952
.matching((req) =>

test/e2e/tests/solana/common-solana.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import { ACCOUNT_TYPE } from '../../constants';
1111
import { loginWithBalanceValidation } from '../../page-objects/flows/login.flow';
1212
import { mockProtocolSnap } from '../../mock-response-data/snaps/snap-binary-mocks';
1313
import AssetListPage from '../../page-objects/pages/home/asset-list';
14-
import { DEFAULT_SOLANA_WS_MOCKS } from './mocks/websocketDefaultMocks';
1514

1615
const SOLANA_URL_REGEX_MAINNET =
1716
/^https:\/\/solana-(mainnet|devnet)\.infura\.io\/v3*/u;
@@ -1612,10 +1611,7 @@ export async function withSolanaAccountSnap(
16121611
fixtures: fixtures.build(),
16131612
title,
16141613
dapp: true,
1615-
withSolanaWebSocket: {
1616-
server: true,
1617-
mocks: DEFAULT_SOLANA_WS_MOCKS,
1618-
},
1614+
withSolanaWebSocket: true,
16191615
manifestFlags: {
16201616
// This flag is used to enable/disable the remote mode for the carousel
16211617
// component, which will impact to the slides count.

test/e2e/tests/solana/mocks/websocketDefaultMocks.ts

Lines changed: 0 additions & 65 deletions
This file was deleted.

test/e2e/tests/solana/web-socket-connection.spec.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,14 @@ import HeaderNavbar from '../../page-objects/pages/header-navbar';
88
import AccountListPage from '../../page-objects/pages/account-list-page';
99
import FixtureBuilder from '../../fixture-builder';
1010
import LocalWebSocketServer from '../../websocket-server';
11-
import { DEFAULT_SOLANA_WS_MOCKS } from './mocks/websocketDefaultMocks';
1211

1312
describe('Solana Web Socket', function (this: Suite) {
1413
it('a websocket connection is open when MetaMask full view is open', async function () {
1514
await withFixtures(
1615
{
1716
fixtures: new FixtureBuilder().build(),
1817
title: this.test?.fullTitle(),
19-
withSolanaWebSocket: {
20-
server: true,
21-
mocks: DEFAULT_SOLANA_WS_MOCKS,
22-
},
18+
withSolanaWebSocket: true,
2319
manifestFlags: {
2420
remoteFeatureFlags: {
2521
addSolanaAccount: true,
@@ -54,10 +50,7 @@ describe('Solana Web Socket', function (this: Suite) {
5450
{
5551
fixtures: new FixtureBuilder().build(),
5652
title: this.test?.fullTitle(),
57-
withSolanaWebSocket: {
58-
server: true,
59-
mocks: DEFAULT_SOLANA_WS_MOCKS,
60-
},
53+
withSolanaWebSocket: true,
6154
manifestFlags: {
6255
remoteFeatureFlags: {
6356
addSolanaAccount: true,
@@ -102,10 +95,7 @@ describe('Solana Web Socket', function (this: Suite) {
10295
{
10396
fixtures: new FixtureBuilder().build(),
10497
title: this.test?.fullTitle(),
105-
withSolanaWebSocket: {
106-
server: true,
107-
mocks: DEFAULT_SOLANA_WS_MOCKS,
108-
},
98+
withSolanaWebSocket: true,
10999
manifestFlags: {
110100
remoteFeatureFlags: {
111101
addSolanaAccount: true,

test/e2e/websocket-solana-mocks.ts

Lines changed: 69 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
// eslint-disable-next-line @typescript-eslint/no-shadow
22
import { WebSocket } from 'ws';
33
import LocalWebSocketServer from './websocket-server';
4-
import { WebSocketMessageMock } from './tests/solana/mocks/websocketDefaultMocks';
54

65
/**
7-
* Sets up Solana WebSocket mocks with configurable message handlers
8-
*
9-
* @param mocks - Array of message mock configurations
6+
* WebSocket Solana mocks
7+
* This function should be called after the WebSocket server is started
108
*/
11-
export async function setupSolanaWebsocketMocks(
12-
mocks: WebSocketMessageMock[] = [],
13-
): Promise<void> {
9+
export async function setSolanaWebsocketMocks(): Promise<void> {
1410
const localWebSocketServer = LocalWebSocketServer.getServerInstance();
1511
const wsServer = localWebSocketServer.getServer();
1612

@@ -22,34 +18,72 @@ export async function setupSolanaWebsocketMocks(
2218
socket.on('message', (data) => {
2319
const message = data.toString();
2420
console.log('Message received from client:', message);
25-
26-
// Check each mock configuration
27-
for (const mock of mocks) {
28-
const includes = Array.isArray(mock.messageIncludes)
29-
? mock.messageIncludes
30-
: [mock.messageIncludes];
31-
32-
// Check if all required strings are included in the message
33-
const matches = includes.every((includeStr) =>
34-
message.includes(includeStr),
35-
);
36-
37-
if (matches) {
38-
if (mock.logMessage) {
39-
console.log(mock.logMessage);
40-
}
41-
42-
const delay = mock.delay || 500;
43-
setTimeout(() => {
44-
socket.send(JSON.stringify(mock.response));
45-
console.log(
46-
`Simulated message sent to the client for: ${includes.join(' + ')}`,
47-
);
48-
}, delay);
49-
50-
// Break after first match to avoid multiple responses
51-
break;
52-
}
21+
if (message.includes('signatureSubscribe')) {
22+
console.log('Signature subscribe message received from client');
23+
setTimeout(() => {
24+
socket.send(
25+
JSON.stringify({
26+
jsonrpc: '2.0',
27+
result: 8648699534240963,
28+
id: '1',
29+
}),
30+
);
31+
console.log('Simulated message sent to the client');
32+
}, 500); // Delay the message by 500ms
33+
}
34+
if (message.includes('accountSubscribe')) {
35+
console.log('Account subscribe message received from client');
36+
setTimeout(() => {
37+
socket.send(
38+
JSON.stringify({
39+
jsonrpc: '2.0',
40+
result:
41+
'b07ebf7caf2238a9b604d4dfcaf1934280fcd347d6eded62bc0def6cbb767d11',
42+
id: '1',
43+
}),
44+
);
45+
console.log(
46+
'Simulated message for accountSubscribe sent to the client',
47+
);
48+
}, 500); // Delay the message by 500ms
49+
}
50+
if (
51+
message.includes('programSubscribe') &&
52+
message.includes('TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA')
53+
) {
54+
console.log('Program subscribe message received from client');
55+
setTimeout(() => {
56+
socket.send(
57+
JSON.stringify({
58+
jsonrpc: '2.0',
59+
result:
60+
'568eafd45635c108d0d426361143de125a841628a58679f5a024cbab9a20b41c',
61+
id: '1',
62+
}),
63+
);
64+
console.log(
65+
'Simulated message for programSubscribe Token2022 sent to the client',
66+
);
67+
}, 500); // Delay the message by 500ms
68+
}
69+
if (
70+
message.includes('programSubscribe') &&
71+
message.includes('TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb')
72+
) {
73+
console.log('Program subscribe message received from client');
74+
setTimeout(() => {
75+
socket.send(
76+
JSON.stringify({
77+
jsonrpc: '2.0',
78+
result:
79+
'f33dd9975158af47bf16c7f6062a73191d4595c59cfec605d5a51e25c65ffb51',
80+
id: '1',
81+
}),
82+
);
83+
console.log(
84+
'Simulated message for programSubscribe sent to the client',
85+
);
86+
}, 500); // Delay the message by 500ms
5387
}
5488
});
5589
});

0 commit comments

Comments
 (0)