@@ -237,65 +237,59 @@ describe('Redis Proxy API', () => {
237237 } ) ;
238238 } ) ;
239239
240- test ( 'Redis client connection with graceful timeout ' , async ( ) => {
241- // Test with Redis client but with better timeout handling for CI
240+ test ( 'Redis client connection and command execution ' , async ( ) => {
241+ // Test with real Redis client - this should work properly
242242 const client = createClient ( {
243243 socket : {
244244 host : '127.0.0.1' ,
245- port : proxy . config . listenPort ,
246- connectTimeout : 1000 ,
247- commandTimeout : 1000
245+ port : proxy . config . listenPort
248246 }
249247 } ) ;
250248
251- let connected = false ;
252-
253- try {
254- // Wrap the entire test in a timeout
255- await Promise . race ( [
256- ( async ( ) => {
257- await client . connect ( ) ;
258- connected = true ;
259- console . log ( 'Redis client connected successfully' ) ;
249+ await client . connect ( ) ;
250+ console . log ( 'Redis client connected successfully' ) ;
260251
261- // Give the connection a moment to be registered
262- await new Promise ( resolve => setTimeout ( resolve , 100 ) ) ;
252+ // Give the connection a moment to be registered
253+ await new Promise ( resolve => setTimeout ( resolve , 100 ) ) ;
263254
264- // Verify connection is tracked
265- const statsRes = await app . request ( '/stats' ) ;
266- const stats = await statsRes . json ( ) ;
267- expect ( stats . activeConnections ) . toBe ( 1 ) ;
255+ // Verify connection is tracked
256+ const statsRes = await app . request ( '/stats' ) ;
257+ const stats = await statsRes . json ( ) ;
258+ expect ( stats . activeConnections ) . toBe ( 1 ) ;
259+ expect ( stats . totalConnections ) . toBeGreaterThanOrEqual ( 1 ) ;
260+ expect ( stats . connections . length ) . toBe ( 1 ) ;
268261
269- // Try to send a command with timeout
270- try {
271- const result = await Promise . race ( [
272- client . sendCommand ( [ 'PING' ] ) ,
273- new Promise ( ( _ , reject ) => setTimeout ( ( ) => reject ( new Error ( 'Command timeout' ) ) , 500 ) )
274- ] ) ;
275- console . log ( ' Redis command result:' , result ) ;
276- } catch ( cmdErr ) {
277- console . log ( 'Redis command failed (expected):' , cmdErr ) ;
278- // This is expected with our mock server
279- }
280- } ) ( ) ,
281- new Promise ( ( _ , reject ) => setTimeout ( ( ) => reject ( new Error ( 'Test timeout' ) ) , 2000 ) )
282- ] ) ;
283- } catch ( err ) {
284- console . log ( 'Redis client test failed (may be expected in CI):' , err ) ;
285- // In CI, this test might fail due to Redis client issues, which is okay
286- // The important thing is that we don't hang
287- } finally {
288- if ( connected ) {
289- try {
290- await client . disconnect ( ) ;
291- } catch ( err ) {
292- console . log ( 'Disconnect error (expected):' , err ) ;
293- }
294- }
295- }
262+ // Get connection ID
263+ const connectionsRes = await app . request ( '/connections' ) ;
264+ const connectionsResult = await connectionsRes . json ( ) ;
265+ expect ( connectionsResult . connectionIds . length ) . toBe ( 1 ) ;
266+ const connectionId = connectionsResult . connectionIds [ 0 ] ;
267+
268+ // Test sending a command through Redis client
269+ const result = await client . sendCommand ( [ 'FOO' ] ) ;
270+ expect ( result ) . toBe ( 'BAR' ) ;
271+
272+ // Test API endpoint to send data to this connection
273+ const pingCommand = Buffer . from ( '*1\r\n$4\r\nPING\r\n' ) . toString ( 'base64' ) ;
274+ const sendRes = await app . request ( `/send-to-client/ ${ connectionId } ?encoding=base64` , {
275+ method : 'POST' ,
276+ body : pingCommand ,
277+ } ) ;
278+
279+ expect ( sendRes . status ) . toBe ( 200 ) ;
280+ const sendResult = await sendRes . json ( ) ;
281+ expect ( sendResult . success ) . toBe ( true ) ;
282+ expect ( sendResult . connectionId ) . toBe ( connectionId ) ;
283+
284+ // Clean up
285+ await client . disconnect ( ) ;
286+
287+ // Give the proxy a moment to detect the disconnection
288+ await new Promise ( resolve => setTimeout ( resolve , 100 ) ) ;
296289
297- // Always verify we can still query stats after test
290+ // Verify the connection is no longer active
298291 const finalStatsRes = await app . request ( '/stats' ) ;
299- expect ( finalStatsRes . status ) . toBe ( 200 ) ;
292+ const finalStats = await finalStatsRes . json ( ) ;
293+ expect ( finalStats . activeConnections ) . toBe ( 0 ) ;
300294 } ) ;
301295} ) ;
0 commit comments