@@ -158,6 +158,7 @@ describe('ReportPortal javascript client', () => {
158158 endpoint : 'https://rp.us/api/v1' ,
159159 project : 'tst' ,
160160 } ) ;
161+ jest . spyOn ( client , 'fetchServerInfo' ) . mockResolvedValue ( { } ) ;
161162 jest . spyOn ( client . statistics , 'trackEvent' ) . mockImplementation ( ) ;
162163
163164 await client . triggerStatisticsEvent ( ) ;
@@ -218,6 +219,101 @@ describe('ReportPortal javascript client', () => {
218219 } ) ,
219220 ) ;
220221 } ) ;
222+
223+ it ( 'should fetch server info and set instanceID before tracking event' , async ( ) => {
224+ const client = new RPClient ( {
225+ apiKey : 'startLaunchTest' ,
226+ endpoint : 'https://rp.us/api/v1' ,
227+ project : 'tst' ,
228+ } ) ;
229+ const serverInfoResponse = {
230+ extensions : {
231+ result : {
232+ 'server.details.instance' : 'test-instance-123' ,
233+ } ,
234+ } ,
235+ } ;
236+ jest . spyOn ( client , 'fetchServerInfo' ) . mockResolvedValue ( serverInfoResponse ) ;
237+ jest . spyOn ( client . statistics , 'trackEvent' ) . mockImplementation ( ) ;
238+ jest . spyOn ( client . statistics , 'setInstanceID' ) ;
239+
240+ await client . triggerStatisticsEvent ( ) ;
241+
242+ expect ( client . fetchServerInfo ) . toHaveBeenCalled ( ) ;
243+ expect ( client . statistics . setInstanceID ) . toHaveBeenCalledWith ( 'test-instance-123' ) ;
244+ expect ( client . statistics . trackEvent ) . toHaveBeenCalled ( ) ;
245+ } ) ;
246+
247+ it ( 'should still track event with not_set instanceID if fetchServerInfo fails' , async ( ) => {
248+ const client = new RPClient ( {
249+ apiKey : 'startLaunchTest' ,
250+ endpoint : 'https://rp.us/api/v1' ,
251+ project : 'tst' ,
252+ } ) ;
253+ jest . spyOn ( client , 'fetchServerInfo' ) . mockRejectedValue ( new Error ( 'Network error' ) ) ;
254+ jest . spyOn ( client . statistics , 'trackEvent' ) . mockImplementation ( ) ;
255+ jest . spyOn ( client . statistics , 'setInstanceID' ) ;
256+
257+ await client . triggerStatisticsEvent ( ) ;
258+
259+ expect ( client . statistics . setInstanceID ) . toHaveBeenCalledWith ( 'not_set' ) ;
260+ expect ( client . statistics . trackEvent ) . toHaveBeenCalled ( ) ;
261+ } ) ;
262+
263+ it ( 'should not set instanceID if server info does not contain it' , async ( ) => {
264+ const client = new RPClient ( {
265+ apiKey : 'startLaunchTest' ,
266+ endpoint : 'https://rp.us/api/v1' ,
267+ project : 'tst' ,
268+ } ) ;
269+ jest . spyOn ( client , 'fetchServerInfo' ) . mockResolvedValue ( { } ) ;
270+ jest . spyOn ( client . statistics , 'trackEvent' ) . mockImplementation ( ) ;
271+ jest . spyOn ( client . statistics , 'setInstanceID' ) ;
272+
273+ await client . triggerStatisticsEvent ( ) ;
274+
275+ expect ( client . statistics . setInstanceID ) . not . toHaveBeenCalled ( ) ;
276+ expect ( client . statistics . trackEvent ) . toHaveBeenCalled ( ) ;
277+ } ) ;
278+ } ) ;
279+
280+ describe ( 'getServerInfoUrl' , ( ) => {
281+ it ( 'should return correct info URL for v1 endpoint' , ( ) => {
282+ const client = new RPClient ( {
283+ apiKey : 'test' ,
284+ project : 'test' ,
285+ endpoint : 'https://rp.us/api/v1' ,
286+ } ) ;
287+
288+ expect ( client . getServerInfoUrl ( ) ) . toBe ( 'https://rp.us/api/info' ) ;
289+ } ) ;
290+
291+ it ( 'should return correct info URL for v2 endpoint' , ( ) => {
292+ const client = new RPClient ( {
293+ apiKey : 'test' ,
294+ project : 'test' ,
295+ endpoint : 'https://rp.us/api/v2' ,
296+ } ) ;
297+
298+ expect ( client . getServerInfoUrl ( ) ) . toBe ( 'https://rp.us/api/info' ) ;
299+ } ) ;
300+ } ) ;
301+
302+ describe ( 'fetchServerInfo' , ( ) => {
303+ it ( 'should call restClient.request with correct URL' , async ( ) => {
304+ const client = new RPClient ( {
305+ apiKey : 'test' ,
306+ project : 'test' ,
307+ endpoint : 'https://rp.us/api/v1' ,
308+ } ) ;
309+ const serverInfo = { extensions : { result : { } } } ;
310+ jest . spyOn ( client . restClient , 'request' ) . mockResolvedValue ( serverInfo ) ;
311+
312+ const result = await client . fetchServerInfo ( ) ;
313+
314+ expect ( client . restClient . request ) . toHaveBeenCalledWith ( 'GET' , 'https://rp.us/api/info' , { } ) ;
315+ expect ( result ) . toEqual ( serverInfo ) ;
316+ } ) ;
221317 } ) ;
222318
223319 describe ( 'startLaunch' , ( ) => {
0 commit comments