@@ -20,12 +20,107 @@ test("connects to Jellyfish Server", async ({ page: firstPage, context }) => {
20
20
const firstClientId = await joinRoomAndAddTrack ( firstPage , roomId ) ;
21
21
const secondClientId = await joinRoomAndAddTrack ( secondPage , roomId ) ;
22
22
23
- await assertThatOtherIsSeen ( firstPage , secondClientId ) ;
24
- await assertThatOtherIsSeen ( secondPage , firstClientId ) ;
23
+ await assertThatOtherIsSeen ( firstPage , [ secondClientId ] ) ;
24
+ await assertThatOtherIsSeen ( secondPage , [ firstClientId ] ) ;
25
25
26
26
await Promise . all ( [ assertThatOtherVideoIsPlaying ( firstPage ) , assertThatOtherVideoIsPlaying ( secondPage ) ] ) ;
27
27
} ) ;
28
28
29
+ test ( "properly sees 8 other peers" , async ( { page, context } ) => {
30
+ const pages = [ page , ...( await Promise . all ( [ ...Array ( 8 ) ] . map ( ( ) => context . newPage ( ) ) ) ) ] ;
31
+
32
+ const roomRequest = await page . request . post ( "http://localhost:5002/room" ) ;
33
+ const roomId = ( await roomRequest . json ( ) ) . data . room . id as string ;
34
+
35
+ const peerIds = await Promise . all (
36
+ pages . map ( async ( page ) => {
37
+ await page . goto ( "/" ) ;
38
+ return await joinRoomAndAddTrack ( page , roomId ) ;
39
+ } ) ,
40
+ ) ;
41
+
42
+ await Promise . all (
43
+ pages . map ( async ( page , idx ) => {
44
+ await assertThatOtherIsSeen (
45
+ page ,
46
+ peerIds . filter ( ( id ) => id !== peerIds [ idx ] ) ,
47
+ ) ;
48
+ await assertThatOtherVideoIsPlaying ( page ) ;
49
+ } ) ,
50
+ ) ;
51
+ } ) ;
52
+
53
+ test ( "see peers just in the same room" , async ( { page, context } ) => {
54
+ const [ p1r1 , p2r1 , p1r2 , p2r2 ] = [ page , ...( await Promise . all ( [ ...Array ( 3 ) ] . map ( ( ) => context . newPage ( ) ) ) ) ] ;
55
+ const [ firstRoomPages , secondRoomPages ] = [
56
+ [ p1r1 , p2r1 ] ,
57
+ [ p1r2 , p2r2 ] ,
58
+ ] ;
59
+
60
+ const firstRoomRequest = await page . request . post ( "http://localhost:5002/room" ) ;
61
+ const secondRoomRequest = await page . request . post ( "http://localhost:5002/room" ) ;
62
+ const firstRoomId = ( await firstRoomRequest . json ( ) ) . data . room . id as string ;
63
+ const secondRoomId = ( await secondRoomRequest . json ( ) ) . data . room . id as string ;
64
+
65
+ const firstRoomPeerIds = await Promise . all (
66
+ firstRoomPages . map ( async ( page ) => {
67
+ await page . goto ( "/" ) ;
68
+ return await joinRoomAndAddTrack ( page , firstRoomId ) ;
69
+ } ) ,
70
+ ) ;
71
+
72
+ const secondRoomPeerIds = await Promise . all (
73
+ secondRoomPages . map ( async ( page ) => {
74
+ await page . goto ( "/" ) ;
75
+ return await joinRoomAndAddTrack ( page , secondRoomId ) ;
76
+ } ) ,
77
+ ) ;
78
+
79
+ await Promise . all (
80
+ firstRoomPages . map ( async ( page , idx ) => {
81
+ await assertThatOtherIsSeen (
82
+ page ,
83
+ firstRoomPeerIds . filter ( ( id ) => id !== firstRoomPeerIds [ idx ] ) ,
84
+ ) ;
85
+ await expect ( assertThatOtherIsSeen ( page , secondRoomPeerIds ) ) . rejects . toThrow ( ) ;
86
+ await assertThatOtherVideoIsPlaying ( page ) ;
87
+ } ) ,
88
+ ) ;
89
+
90
+ await Promise . all (
91
+ secondRoomPages . map ( async ( page , idx ) => {
92
+ await assertThatOtherIsSeen (
93
+ page ,
94
+ secondRoomPeerIds . filter ( ( id ) => id !== secondRoomPeerIds [ idx ] ) ,
95
+ ) ;
96
+ await expect ( assertThatOtherIsSeen ( page , firstRoomPeerIds ) ) . rejects . toThrow ( ) ;
97
+ await assertThatOtherVideoIsPlaying ( page ) ;
98
+ } ) ,
99
+ ) ;
100
+ } ) ;
101
+
102
+ test ( "throws an error if joining room at max capacity" , async ( { page, context } ) => {
103
+ const [ page1 , page2 , overflowingPage ] = [ page , ...( await Promise . all ( [ ...Array ( 2 ) ] . map ( ( ) => context . newPage ( ) ) ) ) ] ;
104
+
105
+ const roomRequest = await page . request . post ( "http://localhost:5002/room" , { data : { maxPeers : 2 } } ) ;
106
+ const roomId = ( await roomRequest . json ( ) ) . data . room . id as string ;
107
+
108
+ await Promise . all (
109
+ [ page1 , page2 ] . map ( async ( page ) => {
110
+ await page . goto ( "/" ) ;
111
+ return await joinRoomAndAddTrack ( page , roomId ) ;
112
+ } ) ,
113
+ ) ;
114
+
115
+ await overflowingPage . goto ( "/" ) ;
116
+ await expect ( joinRoomAndAddTrack ( overflowingPage , roomId ) ) . rejects . toEqual ( {
117
+ status : 503 ,
118
+ response : {
119
+ errors : `Reached peer limit in room ${ roomId } ` ,
120
+ } ,
121
+ } ) ;
122
+ } ) ;
123
+
29
124
async function joinRoomAndAddTrack ( page : Page , roomId : string ) : Promise < string > {
30
125
const peerRequest = await page . request . post ( "http://localhost:5002/room/" + roomId + "/peer" , {
31
126
data : {
@@ -35,28 +130,35 @@ async function joinRoomAndAddTrack(page: Page, roomId: string): Promise<string>
35
130
} ,
36
131
} ,
37
132
} ) ;
38
- const {
39
- peer : { id : peerId } ,
40
- token : peerToken ,
41
- } = ( await peerRequest . json ( ) ) . data ;
42
133
43
- await page . getByLabel ( "Peer Token" ) . fill ( peerToken ) ;
44
- await page . getByLabel ( "Peer name" ) . fill ( peerId ) ;
45
- await page . getByRole ( "button" , { name : "Connect" , exact : true } ) . click ( ) ;
134
+ try {
135
+ const {
136
+ peer : { id : peerId } ,
137
+ token : peerToken ,
138
+ } = ( await peerRequest . json ( ) ) . data ;
46
139
47
- await expect ( page . locator ( "#local-track-video" ) ) . toBeVisible ( ) ;
48
- await page . locator ( "#add-track-btn" ) . click ( ) ;
140
+ await page . getByLabel ( "Peer Token" ) . fill ( peerToken ) ;
141
+ await page . getByLabel ( "Peer name" ) . fill ( peerId ) ;
142
+ await page . getByRole ( "button" , { name : "Connect" , exact : true } ) . click ( ) ;
49
143
50
- return peerId ;
144
+ await expect ( page . locator ( "#local-track-video" ) ) . toBeVisible ( ) ;
145
+ await page . locator ( "#add-track-btn" ) . click ( ) ;
146
+
147
+ return peerId ;
148
+ } catch ( e ) {
149
+ throw { status : peerRequest . status ( ) , response : await peerRequest . json ( ) } ;
150
+ }
51
151
}
52
152
53
- async function assertThatOtherIsSeen ( page : Page , otherClientId : string ) {
153
+ async function assertThatOtherIsSeen ( page : Page , otherClientsId : string [ ] ) {
54
154
const remotePeersContainer = page . locator ( "#remote-peers-container" ) ;
55
155
await expect ( remotePeersContainer ) . toBeVisible ( ) ;
56
- const otherClientCard = remotePeersContainer . locator ( `css=[data-peer-id="${ otherClientId } "]` ) ;
57
- await expect ( otherClientCard ) . toBeVisible ( ) ;
58
- await expect ( otherClientCard ) . toContainText ( `Client: ${ otherClientId } ` ) ;
59
- await expect ( otherClientCard . locator ( "video" ) ) . toBeVisible ( ) ;
156
+ for ( const peerId of otherClientsId ) {
157
+ const peerCard = remotePeersContainer . locator ( `css=[data-peer-id="${ peerId } "]` ) ;
158
+ await expect ( peerCard ) . toBeVisible ( ) ;
159
+ await expect ( peerCard ) . toContainText ( `Client: ${ peerId } ` ) ;
160
+ await expect ( peerCard . locator ( "video" ) ) . toBeVisible ( ) ;
161
+ }
60
162
}
61
163
62
164
async function assertThatOtherVideoIsPlaying ( page : Page ) {
@@ -73,10 +175,14 @@ async function assertThatOtherVideoIsPlaying(page: Page) {
73
175
74
176
const client = ( window as unknown as { client : JellyfishClient < unknown , unknown > } ) . client ;
75
177
const peerConnection = ( client as unknown as { webrtc : { connection : RTCPeerConnection } } ) . webrtc . connection ;
76
- const firstMeasure = await getDecodedFrames ( ) ;
77
- await sleep ( 200 ) ;
78
- const secondMeasure = await getDecodedFrames ( ) ;
79
- return secondMeasure > firstMeasure ;
178
+
179
+ for ( let _retryNum = 0 ; _retryNum < 5 ; _retryNum ++ ) {
180
+ const firstMeasure = await getDecodedFrames ( ) ;
181
+ await sleep ( 250 ) ;
182
+ const secondMeasure = await getDecodedFrames ( ) ;
183
+ if ( secondMeasure > firstMeasure ) return true ;
184
+ }
185
+ return false ;
80
186
} ) ;
81
187
expect ( playing ) . toBe ( true ) ;
82
188
}
0 commit comments