@@ -24,28 +24,31 @@ const mockInfoClient = {
2424const mockSubscriptionClient = {
2525 initialized : true ,
2626} ;
27- const mockTransport = {
27+ const mockWsTransport = {
2828 url : 'ws://mock' ,
2929 close : jest . fn ( ) . mockResolvedValue ( undefined ) ,
3030} ;
31+ const mockHttpTransport = {
32+ url : 'http://mock' ,
33+ } ;
3134
3235jest . mock ( '@nktkas/hyperliquid' , ( ) => ( {
3336 ExchangeClient : jest . fn ( ( ) => mockExchangeClient ) ,
3437 InfoClient : jest . fn ( ( ) => mockInfoClient ) ,
3538 SubscriptionClient : jest . fn ( ( ) => mockSubscriptionClient ) ,
36- WebSocketTransport : jest . fn ( ( ) => mockTransport ) ,
39+ WebSocketTransport : jest . fn ( ( ) => mockWsTransport ) ,
40+ HttpTransport : jest . fn ( ( ) => mockHttpTransport ) ,
3741} ) ) ;
3842
3943// Mock configuration
4044jest . mock ( '../constants/hyperLiquidConfig' , ( ) => ( {
41- getWebSocketEndpoint : jest . fn ( ( isTestnet : boolean ) =>
42- isTestnet
43- ? 'wss://api.hyperliquid-testnet.xyz/ws'
44- : 'wss://api.hyperliquid.xyz/ws' ,
45- ) ,
4645 HYPERLIQUID_TRANSPORT_CONFIG : {
47- reconnectAttempts : 5 ,
48- reconnectInterval : 1000 ,
46+ timeout : 10_000 ,
47+ keepAlive : { interval : 30_000 } ,
48+ reconnect : {
49+ maxRetries : 5 ,
50+ connectionTimeout : 10_000 ,
51+ } ,
4952 } ,
5053} ) ) ;
5154
@@ -92,7 +95,7 @@ describe('HyperLiquidClientService', () => {
9295 } ) ;
9396
9497 describe ( 'Client Initialization' , ( ) => {
95- it ( 'should initialize clients successfully' , ( ) => {
98+ it ( 'should initialize clients successfully with dual transports ' , ( ) => {
9699 service . initialize ( mockWallet ) ;
97100
98101 expect ( service . isInitialized ( ) ) . toBe ( true ) ;
@@ -102,23 +105,37 @@ describe('HyperLiquidClientService', () => {
102105 InfoClient,
103106 SubscriptionClient,
104107 WebSocketTransport,
108+ HttpTransport,
105109 } = require ( '@nktkas/hyperliquid' ) ;
106110
111+ // Verify HTTP transport uses isTestnet flag (SDK handles endpoint selection)
112+ expect ( HttpTransport ) . toHaveBeenCalledWith ( {
113+ isTestnet : false ,
114+ timeout : 10_000 ,
115+ } ) ;
116+
117+ // Verify WebSocket transport uses isTestnet flag (SDK handles endpoint selection)
107118 expect ( WebSocketTransport ) . toHaveBeenCalledWith ( {
108- url : 'wss://api.hyperliquid.xyz/ws' ,
109- reconnectAttempts : 5 ,
110- reconnectInterval : 1000 ,
111- reconnect : {
119+ isTestnet : false ,
120+ timeout : 10_000 ,
121+ keepAlive : { interval : 30_000 } ,
122+ reconnect : expect . objectContaining ( {
112123 WebSocket : expect . any ( Function ) ,
113- } ,
124+ } ) ,
114125 } ) ;
126+
127+ // ExchangeClient uses HTTP transport
115128 expect ( ExchangeClient ) . toHaveBeenCalledWith ( {
116129 wallet : mockWallet ,
117- transport : mockTransport ,
130+ transport : mockHttpTransport ,
118131 } ) ;
119- expect ( InfoClient ) . toHaveBeenCalledWith ( { transport : mockTransport } ) ;
132+
133+ // InfoClient uses HTTP transport
134+ expect ( InfoClient ) . toHaveBeenCalledWith ( { transport : mockHttpTransport } ) ;
135+
136+ // SubscriptionClient uses WebSocket transport
120137 expect ( SubscriptionClient ) . toHaveBeenCalledWith ( {
121- transport : mockTransport ,
138+ transport : mockWsTransport ,
122139 } ) ;
123140 } ) ;
124141
@@ -140,19 +157,28 @@ describe('HyperLiquidClientService', () => {
140157 const {
141158 ExchangeClient,
142159 WebSocketTransport,
160+ HttpTransport,
143161 } = require ( '@nktkas/hyperliquid' ) ;
144162
163+ // Verify testnet flag is passed (SDK auto-selects testnet endpoints)
164+ expect ( HttpTransport ) . toHaveBeenCalledWith ( {
165+ isTestnet : true ,
166+ timeout : 10_000 ,
167+ } ) ;
168+
145169 expect ( WebSocketTransport ) . toHaveBeenCalledWith ( {
146- url : 'wss://api.hyperliquid-testnet.xyz/ws' ,
147- reconnectAttempts : 5 ,
148- reconnectInterval : 1000 ,
149- reconnect : {
170+ isTestnet : true ,
171+ timeout : 10_000 ,
172+ keepAlive : { interval : 30_000 } ,
173+ reconnect : expect . objectContaining ( {
150174 WebSocket : expect . any ( Function ) ,
151- } ,
175+ } ) ,
152176 } ) ;
177+
178+ // ExchangeClient uses HTTP transport
153179 expect ( ExchangeClient ) . toHaveBeenCalledWith ( {
154180 wallet : mockWallet ,
155- transport : mockTransport ,
181+ transport : mockHttpTransport ,
156182 } ) ;
157183 } ) ;
158184 } ) ;
@@ -269,21 +295,24 @@ describe('HyperLiquidClientService', () => {
269295 service . initialize ( mockWallet ) ;
270296 } ) ;
271297
272- it ( 'should disconnect successfully' , async ( ) => {
298+ it ( 'should disconnect successfully and close only WebSocket transport ' , async ( ) => {
273299 await service . disconnect ( ) ;
274300
275- expect ( mockTransport . close ) . toHaveBeenCalled ( ) ;
301+ // Only WebSocket transport should be closed (HTTP is stateless)
302+ expect ( mockWsTransport . close ) . toHaveBeenCalled ( ) ;
276303 expect ( service . getSubscriptionClient ( ) ) . toBeUndefined ( ) ;
277304 } ) ;
278305
279306 it ( 'should handle disconnect errors gracefully' , async ( ) => {
280- mockTransport . close . mockRejectedValueOnce ( new Error ( 'Disconnect failed' ) ) ;
307+ mockWsTransport . close . mockRejectedValueOnce (
308+ new Error ( 'Disconnect failed' ) ,
309+ ) ;
281310
282311 // Should not throw, error is caught and logged
283312 await expect ( service . disconnect ( ) ) . resolves . not . toThrow ( ) ;
284313
285314 // Verify the error was attempted to be handled
286- expect ( mockTransport . close ) . toHaveBeenCalled ( ) ;
315+ expect ( mockWsTransport . close ) . toHaveBeenCalled ( ) ;
287316 } ) ;
288317
289318 it ( 'should clear all client references after disconnect' , async ( ) => {
@@ -348,7 +377,6 @@ describe('HyperLiquidClientService', () => {
348377 'HyperLiquid SDK clients initialized' ,
349378 expect . objectContaining ( {
350379 testnet : false ,
351- endpoint : 'wss://api.hyperliquid.xyz/ws' ,
352380 timestamp : expect . any ( String ) ,
353381 connectionState : 'connected' ,
354382 } ) ,
@@ -367,7 +395,6 @@ describe('HyperLiquidClientService', () => {
367395 'HyperLiquid: Disconnecting SDK clients' ,
368396 expect . objectContaining ( {
369397 isTestnet : false ,
370- endpoint : 'wss://api.hyperliquid.xyz/ws' ,
371398 timestamp : expect . any ( String ) ,
372399 } ) ,
373400 ) ;
@@ -381,9 +408,8 @@ describe('HyperLiquidClientService', () => {
381408 service . initialize ( mockWallet ) ;
382409
383410 expect ( DevLogger . log ) . toHaveBeenCalledWith (
384- 'HyperLiquid: Creating WebSocket transport ' ,
411+ 'HyperLiquid: Creating transports ' ,
385412 expect . objectContaining ( {
386- endpoint : 'wss://api.hyperliquid.xyz/ws' ,
387413 isTestnet : false ,
388414 timestamp : expect . any ( String ) ,
389415 } ) ,
0 commit comments