|
1 | 1 | /** biome-ignore-all lint/suspicious/noExplicitAny: test code */
|
| 2 | +/** biome-ignore-all lint/suspicious/noShadowRestrictedNames: test code */ |
2 | 3 | import { type ConnectionMode, type IKeyManager, type IKVStore, type KeyPair, type SessionRequest, SessionStore, WebSocketTransport } from "@metamask/mobile-wallet-protocol-core";
|
3 | 4 | import { DappClient, type OtpRequiredPayload } from "@metamask/mobile-wallet-protocol-dapp-client";
|
4 | 5 | import { WalletClient } from "@metamask/mobile-wallet-protocol-wallet-client";
|
5 | 6 | import { decrypt, encrypt, PrivateKey } from "eciesjs";
|
| 7 | +import { type Proxy, Toxiproxy } from "toxiproxy-node-client"; |
6 | 8 | import * as t from "vitest";
|
7 | 9 | import WebSocket from "ws";
|
8 | 10 |
|
9 | 11 | const RELAY_URL = "ws://localhost:8000/connection/websocket";
|
| 12 | +const PROXY_RELAY_URL = "ws://localhost:8001/connection/websocket"; |
| 13 | +const TOXIPROXY_URL = "http://localhost:8474"; |
10 | 14 |
|
11 | 15 | class InMemoryKVStore implements IKVStore {
|
12 | 16 | private store = new Map<string, string>();
|
@@ -69,6 +73,12 @@ async function connectClients(dappClient: DappClient, walletClient: WalletClient
|
69 | 73 | await Promise.all([dappConnectPromise, walletConnectPromise]);
|
70 | 74 | }
|
71 | 75 |
|
| 76 | +// Helper to assert that a promise does NOT resolve within a given time |
| 77 | +async function assertPromiseNotResolve(promise: Promise<unknown>, timeout: number, message: string) { |
| 78 | + const timeoutPromise = new Promise((_, reject) => setTimeout(() => reject(new Error(message)), timeout)); |
| 79 | + await t.expect(Promise.race([promise, timeoutPromise])).rejects.toThrow(message); |
| 80 | +} |
| 81 | + |
72 | 82 | t.describe("E2E Integration Test", () => {
|
73 | 83 | let dappClient: DappClient;
|
74 | 84 | let walletClient: WalletClient;
|
@@ -199,4 +209,114 @@ t.describe("E2E Integration Test", () => {
|
199 | 209 | await resumedDappClient.sendRequest(testPayload);
|
200 | 210 | await t.expect(messagePromise).resolves.toEqual(testPayload);
|
201 | 211 | });
|
| 212 | + |
| 213 | +}); |
| 214 | + |
| 215 | +t.describe("E2E Integration Test via Proxy", () => { |
| 216 | + let dappClient: DappClient; |
| 217 | + let walletClient: WalletClient; |
| 218 | + let dappKvStore: InMemoryKVStore; |
| 219 | + let walletKvStore: InMemoryKVStore; |
| 220 | + let dappSessionStore: SessionStore; |
| 221 | + let walletSessionStore: SessionStore; |
| 222 | + |
| 223 | + // Toxiproxy setup |
| 224 | + let toxiproxy: Toxiproxy; |
| 225 | + let proxy: Proxy; |
| 226 | + const proxyConfig = { |
| 227 | + listen: "0.0.0.0:8001", |
| 228 | + upstream: "centrifugo:8000", |
| 229 | + }; |
| 230 | + |
| 231 | + t.beforeAll(async () => { |
| 232 | + toxiproxy = new Toxiproxy(TOXIPROXY_URL); |
| 233 | + try { |
| 234 | + proxy = await toxiproxy.get("centrifugo_proxy"); |
| 235 | + await proxy.remove(); |
| 236 | + } catch { |
| 237 | + // Proxy doesn't exist, which is fine |
| 238 | + } |
| 239 | + proxy = await toxiproxy.createProxy({ |
| 240 | + name: "centrifugo_proxy", |
| 241 | + ...proxyConfig, |
| 242 | + }); |
| 243 | + }); |
| 244 | + |
| 245 | + t.beforeEach(async () => { |
| 246 | + // Ensure the proxy is enabled before each test |
| 247 | + await proxy.update({ ...proxyConfig, enabled: true }); |
| 248 | + |
| 249 | + dappKvStore = new InMemoryKVStore(); |
| 250 | + walletKvStore = new InMemoryKVStore(); |
| 251 | + dappSessionStore = new SessionStore(dappKvStore); |
| 252 | + walletSessionStore = new SessionStore(walletKvStore); |
| 253 | + |
| 254 | + // DApp connects directly, Wallet connects through proxy |
| 255 | + const dappTransport = await WebSocketTransport.create({ url: RELAY_URL, kvstore: dappKvStore, websocket: WebSocket }); |
| 256 | + const walletTransport = await WebSocketTransport.create({ url: PROXY_RELAY_URL, kvstore: walletKvStore, websocket: WebSocket }); |
| 257 | + const keyManager = new KeyManager(); |
| 258 | + |
| 259 | + dappClient = new DappClient({ transport: dappTransport, sessionstore: dappSessionStore, keymanager: keyManager }); |
| 260 | + walletClient = new WalletClient({ transport: walletTransport, sessionstore: walletSessionStore, keymanager: keyManager }); |
| 261 | + }); |
| 262 | + |
| 263 | + t.afterEach(async () => { |
| 264 | + // Reset proxy state after each test |
| 265 | + if (proxy) { |
| 266 | + await proxy.update({ ...proxyConfig, enabled: true }); |
| 267 | + } |
| 268 | + try { |
| 269 | + await dappClient?.disconnect(); |
| 270 | + } catch (e) { |
| 271 | + console.error("Error disconnecting dappClient:", e); |
| 272 | + } |
| 273 | + try { |
| 274 | + await walletClient?.disconnect(); |
| 275 | + } catch (e) { |
| 276 | + console.error("Error disconnecting walletClient:", e); |
| 277 | + } |
| 278 | + }); |
| 279 | + |
| 280 | + t.test( |
| 281 | + "should recover from a one-sided stale connection and receive queued messages after reconnect", |
| 282 | + async () => { |
| 283 | + // 1. Establish a normal connection and exchange a message to confirm it works |
| 284 | + await connectClients(dappClient, walletClient, "trusted"); |
| 285 | + const initialMessage = { step: "initial_message" }; |
| 286 | + const initialMessagePromise = new Promise((resolve) => walletClient.once("message", resolve)); |
| 287 | + await dappClient.sendRequest(initialMessage); |
| 288 | + await t.expect(initialMessagePromise).resolves.toEqual(initialMessage); |
| 289 | + t.expect((walletClient as any).state).toBe("CONNECTED"); |
| 290 | + |
| 291 | + // 2. Create a ONE-SIDED network partition using toxiproxy. |
| 292 | + // This cuts off the Wallet's connection but leaves the DApp's connection intact. |
| 293 | + await proxy.update({ ...proxyConfig, enabled: false }); |
| 294 | + |
| 295 | + // 3. Dapp sends a message. Its transport is still live, so it successfully publishes to the relay. |
| 296 | + // The Wallet, however, is now on a stale connection and should NOT receive it. |
| 297 | + const missedMessage = { step: "missed_message" }; |
| 298 | + const missedMessagePromise = new Promise((resolve) => walletClient.once("message", resolve)); |
| 299 | + await dappClient.sendRequest(missedMessage); |
| 300 | + |
| 301 | + // Assert that the message is NOT received by the wallet within 2 seconds. |
| 302 | + await assertPromiseNotResolve(missedMessagePromise, 2000, "Wallet incorrectly received message during network partition."); |
| 303 | + |
| 304 | + // 4. Restore the network path and trigger the wallet's reconnect logic. |
| 305 | + await proxy.update({ ...proxyConfig, enabled: true }); |
| 306 | + await walletClient.reconnect(); |
| 307 | + |
| 308 | + // 5. The wallet should now re-establish its connection. The transport's recovery logic |
| 309 | + // should fetch the missed message from the channel's history. |
| 310 | + const receivedMissedMessage = await missedMessagePromise; |
| 311 | + t.expect(receivedMissedMessage).toEqual(missedMessage); |
| 312 | + t.expect((walletClient as any).state).toBe("CONNECTED"); |
| 313 | + |
| 314 | + // 6. Send a final message to confirm the live connection is fully restored and working. |
| 315 | + const finalMessage = { step: "final_message" }; |
| 316 | + const finalMessagePromise = new Promise((resolve) => walletClient.once("message", resolve)); |
| 317 | + await dappClient.sendRequest(finalMessage); |
| 318 | + await t.expect(finalMessagePromise).resolves.toEqual(finalMessage); |
| 319 | + }, |
| 320 | + 15000, |
| 321 | + ); |
202 | 322 | });
|
0 commit comments