@@ -14,6 +14,11 @@ V6L11BWkpzGXSW4Hv43qa+GSYOD2QU68Mb59oSk2OB+BtOLpJofmbGEGgvmwyCI9
1414MwIDAQAB
1515-----END PUBLIC KEY-----` ;
1616
17+ type IncomingSocket = {
18+ connection : "open" | "close" ;
19+ ws : WebSocket ;
20+ } ;
21+
1722describe ( "TunnelServer" , ( ) => {
1823 let server : TunnelServer ;
1924 const port = 51515 ;
@@ -33,7 +38,7 @@ describe("TunnelServer", () => {
3338 * }
3439 */
3540 const jwtToken = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJsZW5zLXVzZXIiLCJncm91cHMiOlsiZGV2Il0sImlhdCI6MTUxNjIzOTAyMiwiY2x1c3RlcklkIjoiYTAyNmU1MGQtZjliNC00YWE4LWJhMDItYzk3MjJmN2YwNjYzIiwiYXVkIjoiaHR0cDovL2xvY2FsaG9zdC9ib3JlZC9hMDI2ZTUwZC1mOWI0LTRhYTgtYmEwMi1jOTcyMmY3ZjA2NjMifQ.jkTbX_O8UWbYdCRiTv4NEgDkewEOB9QrLOHOm_Ox8BKt7DC4696bbdOwVn_VHist0g6889ms0m8Nr_RKW5BW90ItAsfDx_0cp34_WKPuMBeXYxkfAEabBbhjATfrW1IUTVtV9R_qQ71nbqlhY9UudByfETI8CanjbDP7QYZCxmVCf2HvRML3h6mS1tqHmqZvjRAHY-cFmO8qa6xLp2c1vFMxuCoSZGoGIqoNPaLKIVBbDdjxzOEjO__gQX6ksUZxsHOy13iBre8gbBVi85lhkSCZa9OtXDEAICqsrlpHZvxIYqYMgBNG0YY4sVvvDGJgDxxTyWn8lphKrZyWWtNvjw" ;
36-
41+
3742 /**
3843 * {
3944 * "sub": "a026e50d-f9b4-4aa8-ba02-c9722f7f0663",
@@ -55,10 +60,7 @@ describe("TunnelServer", () => {
5560 const sleep = ( amount : number ) => new Promise ( ( resolve ) => setTimeout ( resolve , amount ) ) ;
5661 const get = async ( path : string , headers ?: Headers ) => got ( `http://localhost:${ port } ${ path } ` , { throwHttpErrors : false , headers } ) ;
5762
58- const incomingSocket = ( type = "agent" , headers : { [ key : string ] : string } = { } , keepOpen = 10 , close = true , endpoint = "connect" ) : Promise < {
59- connection : "open" | "close" ;
60- ws : WebSocket ;
61- } > => {
63+ const incomingSocket = ( type = "agent" , headers : { [ key : string ] : string } = { } , keepOpen = 10 , close = true , endpoint = "connect" ) : Promise < IncomingSocket > => {
6264 return new Promise ( ( resolve , reject ) => {
6365 const ws = new WebSocket ( `http://localhost:${ port } /${ type } /${ endpoint } ` , {
6466 headers
@@ -122,7 +124,7 @@ describe("TunnelServer", () => {
122124
123125 const agents = server . getAgentsForClusterId ( "a026e50d-f9b4-4aa8-ba02-c9722f7f0663" ) ;
124126
125- agents . push ( new Agent ( ws as any , "rsa-public-key" , server ) ) ;
127+ agents . push ( new Agent ( ws as any , "rsa-public-key" , server , "test-id" ) ) ;
126128
127129 const res = await get ( "/client/public-key" , { "Authorization" : `Bearer ${ jwtToken } ` } ) ;
128130
@@ -451,7 +453,7 @@ describe("TunnelServer", () => {
451453 await expect ( connect ( ) ) . resolves . toHaveProperty ( "connection" , "open" ) ;
452454 } ) ;
453455
454- it ( "sends empty presence json to client presence socket when socket is open" , async ( ) => {
456+ it ( "sends empty presence json to client presence socket when socket is open" , async ( done ) => {
455457 expect . assertions ( 1 ) ;
456458
457459 const presence = await incomingSocket ( "client" , {
@@ -463,17 +465,14 @@ describe("TunnelServer", () => {
463465 "presence" : {
464466 "userIds" : [ ]
465467 }
466- } )
467- ) ;
468+ } ) ) ;
469+ presence . ws . close ( ) ;
470+ done ( ) ;
468471 } ;
469-
470- await sleep ( 200 ) ; //waits until first message was sent
471-
472- presence . ws . close ( ) ;
473472 } ) ;
474473
475474
476- it ( "sends presence json to client presence socket when socket is open and clients are already connected" , async ( ) => {
475+ it ( "sends presence json to client presence socket when socket is open and clients are already connected" , async ( done ) => {
477476 expect . assertions ( 1 ) ;
478477
479478 const agent = await incomingSocket ( "agent" , {
@@ -489,22 +488,22 @@ describe("TunnelServer", () => {
489488 } , undefined , false , "presence" ) ;
490489
491490 presence . ws . onmessage = ( message ) => {
492- expect ( message . data ) . toBe ( JSON . stringify ( {
491+ expect ( message . data ) . toBe ( JSON . stringify ( {
493492 "presence" : {
494493 "userIds" : [ "lens-user" ]
495494 }
496495 } )
497496 ) ;
498- } ;
499497
500- await sleep ( 200 ) ; //waits until first message was sent
498+ presence . ws . close ( ) ;
499+ client . ws . close ( ) ;
500+ agent . ws . close ( ) ;
501501
502- presence . ws . close ( ) ;
503- client . ws . close ( ) ;
504- agent . ws . close ( ) ;
502+ done ( ) ;
503+ } ;
505504 } ) ;
506505
507- it ( "sends userIds per agent to client presence socket after agent and client connected" , async ( ) => {
506+ it ( "sends userIds per agent to client presence socket after agent and client connected" , async ( done ) => {
508507 expect . assertions ( 1 ) ;
509508
510509 const presence = await incomingSocket ( "client" , {
@@ -513,31 +512,34 @@ describe("TunnelServer", () => {
513512
514513 await sleep ( 200 ) ; //waits until first message was sent
515514
515+ let agent : IncomingSocket | null = null ;
516+ let client : IncomingSocket | null = null ;
517+
516518 presence . ws . onmessage = ( message ) => {
517- expect ( message . data ) . toBe ( JSON . stringify ( {
519+ console . log ( "message" , message . data ) ;
520+ expect ( message . data ) . toBe ( JSON . stringify ( {
518521 "presence" : {
519522 "userIds" : [ "lens-user" ]
520523 }
521- } )
522- ) ;
524+ } ) ) ;
525+
526+ presence . ws . close ( ) ;
527+ client ?. ws . close ( ) ;
528+ agent ?. ws . close ( ) ;
529+
530+ done ( ) ;
523531 } ;
524532
525- const agent = await incomingSocket ( "agent" , {
533+ agent = await incomingSocket ( "agent" , {
526534 "Authorization" : `Bearer ${ agentJwtToken } `
527535 } , undefined , false ) ;
528536
529- const client = await incomingSocket ( "client" , {
537+ client = await incomingSocket ( "client" , {
530538 "Authorization" : `Bearer ${ jwtToken } `
531539 } , undefined , false ) ;
532-
533- await sleep ( 100 ) ; //waits until on "ClientConnected" message was sent
534-
535- presence . ws . close ( ) ;
536- client . ws . close ( ) ;
537- agent . ws . close ( ) ;
538540 } ) ;
539541
540- it ( "sends empty presence json to client presence socket after agent and client connected and disconnected" , async ( ) => {
542+ it ( "sends empty presence json to client presence socket after agent and client connected and disconnected" , async ( done ) => {
541543 expect . assertions ( 1 ) ;
542544
543545 const presence = await incomingSocket ( "client" , {
@@ -555,21 +557,18 @@ describe("TunnelServer", () => {
555557 } , undefined , false ) ;
556558
557559 presence . ws . onmessage = ( message ) => {
558- console . log ( message . data ) ;
559- expect ( message . data ) . toBe ( JSON . stringify ( {
560+ expect ( message . data ) . toBe ( JSON . stringify ( {
560561 "presence" : {
561562 "userIds" : [ ]
562563 }
563- } )
564- ) ;
564+ } ) ) ;
565+
566+ presence . ws . close ( ) ;
567+ done ( ) ;
565568 } ;
566569
567570 agent . ws . close ( ) ;
568571 client . ws . close ( ) ;
569-
570- await sleep ( 200 ) ; //waits until on "ClientDisconnected" message was received
571-
572- presence . ws . close ( ) ;
573572 } ) ;
574573 } ) ;
575574 } ) ;
0 commit comments