11import GenesysCloudWebrtSdk , { IExtendedMediaSession , utils } from "../../src" ;
22import { StatsAggregator } from "../../src/stats-aggregator"
3- import { requestApiWithRetry } from "../../src/utils" ;
43import { MockSession , SimpleMockSdk } from "../test-utils" ;
54
65describe ( 'StatsAggregator' , ( ) => {
@@ -160,6 +159,10 @@ describe('StatsAggregator', () => {
160159 } ) ;
161160
162161 describe ( 'sendStats' , ( ) => {
162+ afterEach ( ( ) => {
163+ jest . restoreAllMocks ( ) ;
164+ } ) ;
165+
163166 it ( 'should call requestApi' , ( ) => {
164167 const requestApiSpy = jest . spyOn ( utils , 'requestApi' ) . mockResolvedValue ( null ) ;
165168 const mockSession = new MockSession ( ) as unknown as IExtendedMediaSession ;
@@ -171,6 +174,10 @@ describe('StatsAggregator', () => {
171174 averageJitter : 0.1 ,
172175 estimatedAverageMos : 5
173176 }
177+ const participant = {
178+ calls : [ { id : "testCall" } ]
179+ } ;
180+ mockSession . pcParticipant = participant as any ;
174181
175182 statsAggregator [ 'sendStats' ] ( rtpStats , new Date ( ) ) ;
176183
@@ -185,5 +192,49 @@ describe('StatsAggregator', () => {
185192 // This should actually check it
186193 expect ( statsArgument [ 'dateCreated' ] ) . toBeTruthy ( ) ;
187194 } ) ;
195+
196+ it ( 'should not call requestApi if there is no participant' , ( ) => {
197+ const requestApiSpy = jest . spyOn ( utils , 'requestApi' ) . mockResolvedValue ( null ) ;
198+ const mockSession = new MockSession ( ) as unknown as IExtendedMediaSession ;
199+ const sdk = new SimpleMockSdk ( ) as unknown as GenesysCloudWebrtSdk ;
200+ const statsAggregator = new StatsAggregator ( mockSession , sdk ) ;
201+ const rtpStats = {
202+ packetsReceived : 5 ,
203+ packetsSent : 5 ,
204+ averageJitter : 0.1 ,
205+ estimatedAverageMos : 5
206+ }
207+
208+ statsAggregator [ 'sendStats' ] ( rtpStats , new Date ( ) ) ;
209+
210+ expect ( requestApiSpy ) . not . toHaveBeenCalled ( ) ;
211+ } ) ;
212+
213+ it ( 'should not call requestApi if there are no calls for the participant' , ( ) => {
214+ const requestApiSpy = jest . spyOn ( utils , 'requestApi' ) . mockResolvedValue ( null ) ;
215+ const mockSession = new MockSession ( ) as unknown as IExtendedMediaSession ;
216+ const sdk = new SimpleMockSdk ( ) as unknown as GenesysCloudWebrtSdk ;
217+ const statsAggregator = new StatsAggregator ( mockSession , sdk ) ;
218+ const rtpStats = {
219+ packetsReceived : 5 ,
220+ packetsSent : 5 ,
221+ averageJitter : 0.1 ,
222+ estimatedAverageMos : 5
223+ }
224+
225+ const participantA = {
226+ calls : [ ]
227+ } ;
228+ mockSession . pcParticipant = participantA as any ;
229+ statsAggregator [ 'sendStats' ] ( rtpStats , new Date ( ) ) ;
230+
231+ expect ( requestApiSpy ) . not . toHaveBeenCalled ( ) ;
232+
233+ const participantB = { } ;
234+ mockSession . pcParticipant = participantB as any ;
235+ statsAggregator [ 'sendStats' ] ( rtpStats , new Date ( ) ) ;
236+
237+ expect ( requestApiSpy ) . not . toHaveBeenCalled ( ) ;
238+ } ) ;
188239 } ) ;
189240} ) ;
0 commit comments