@@ -30,6 +30,8 @@ const VALID_OPTIONS: TelemetryClientInitOptions = {
3030const VALID_CONNECTION_STRING =
3131 'InstrumentationKey=abc;IngestionEndpoint=https://example.com' ;
3232
33+ const TEST_USER_ID = 'user-guid-1234' ;
34+
3335/**
3436 * `TelemetryClient.initialize` reads `CONNECTION_STRING` from `./constants`
3537 * at call time. Each test that needs to drive a specific value re-mocks
@@ -166,6 +168,8 @@ describe('TelemetryClient.track', () => {
166168 baseUrl : 'https://example.com' ,
167169 orgName : 'org' ,
168170 tenantName : 'tenant' ,
171+ orgId : 'org-guid' ,
172+ tenantId : 'tenant-guid' ,
169173 clientId : 'client-1' ,
170174 redirectUri : 'https://example.com/cb' ,
171175 } ,
@@ -180,16 +184,36 @@ describe('TelemetryClient.track', () => {
180184 ApplicationName : 'TestService' ,
181185 Version : '1.2.3' ,
182186 Service : 'Service.Method' ,
183- CloudUrl : 'https://example.com/org/tenant' ,
187+ CloudUrl : 'https://example.com/org-guid /tenant-guid ' ,
184188 CloudOrganizationName : 'org' ,
185189 CloudTenantName : 'tenant' ,
190+ CloudOrganizationId : 'org-guid' ,
191+ CloudTenantId : 'tenant-guid' ,
186192 CloudClientId : 'client-1' ,
187193 CloudRedirectUri : 'https://example.com/cb' ,
188194 custom : 'value' ,
189195 } ) ;
190196 expect ( typeof logRecord . timestamp ) . toBe ( 'number' ) ;
191197 } ) ;
192198
199+ it ( 'builds CloudUrl from org/tenant names when the ids are absent' , async ( ) => {
200+ const client = await clientWithConnectionString ( VALID_CONNECTION_STRING ) ;
201+
202+ client . initialize ( {
203+ ...VALID_OPTIONS ,
204+ context : {
205+ baseUrl : 'https://example.com' ,
206+ orgName : 'org' ,
207+ tenantName : 'tenant' ,
208+ } ,
209+ } ) ;
210+
211+ client . track ( 'Some.Event' ) ;
212+
213+ const [ logRecord ] = mocks . emit . mock . calls [ 0 ] ;
214+ expect ( logRecord . attributes . CloudUrl ) . toBe ( 'https://example.com/org/tenant' ) ;
215+ } ) ;
216+
193217 it ( 'falls back to the eventName as the body when no display name is given' , async ( ) => {
194218 const client = await clientWithConnectionString ( VALID_CONNECTION_STRING ) ;
195219 client . initialize ( VALID_OPTIONS ) ;
@@ -210,9 +234,12 @@ describe('TelemetryClient.track', () => {
210234 const [ logRecord ] = mocks . emit . mock . calls [ 0 ] ;
211235 expect ( logRecord . attributes . CloudOrganizationName ) . toBe ( '' ) ;
212236 expect ( logRecord . attributes . CloudTenantName ) . toBe ( '' ) ;
237+ expect ( logRecord . attributes . CloudOrganizationId ) . toBe ( '' ) ;
238+ expect ( logRecord . attributes . CloudTenantId ) . toBe ( '' ) ;
213239 expect ( logRecord . attributes . CloudUrl ) . toBe ( '' ) ;
214240 expect ( logRecord . attributes . CloudClientId ) . toBe ( '' ) ;
215241 expect ( logRecord . attributes . CloudRedirectUri ) . toBe ( '' ) ;
242+ expect ( logRecord . attributes . CloudUserId ) . toBe ( '' ) ;
216243 } ) ;
217244
218245 it ( 'reports the event name as the Service attribute and respects the explicit display name' , async ( ) => {
@@ -227,6 +254,45 @@ describe('TelemetryClient.track', () => {
227254 } ) ;
228255} ) ;
229256
257+ describe ( 'TelemetryClient.setUserId' , ( ) => {
258+ it ( 'populates CloudUserId on subsequent events' , async ( ) => {
259+ const client = await clientWithConnectionString ( VALID_CONNECTION_STRING ) ;
260+ client . initialize ( VALID_OPTIONS ) ;
261+
262+ client . setUserId ( TEST_USER_ID ) ;
263+ client . track ( 'Some.Event' ) ;
264+
265+ const [ logRecord ] = mocks . emit . mock . calls [ 0 ] ;
266+ expect ( logRecord . attributes . CloudUserId ) . toBe ( TEST_USER_ID ) ;
267+ } ) ;
268+
269+ it ( 'reports CloudUserId as UNKNOWN on events emitted before a user id is set' , async ( ) => {
270+ const client = await clientWithConnectionString ( VALID_CONNECTION_STRING ) ;
271+ client . initialize ( VALID_OPTIONS ) ;
272+
273+ client . track ( 'Before.UserId' ) ;
274+ client . setUserId ( TEST_USER_ID ) ;
275+ client . track ( 'After.UserId' ) ;
276+
277+ const [ beforeRecord ] = mocks . emit . mock . calls [ 0 ] ;
278+ const [ afterRecord ] = mocks . emit . mock . calls [ 1 ] ;
279+ expect ( beforeRecord . attributes . CloudUserId ) . toBe ( '' ) ;
280+ expect ( afterRecord . attributes . CloudUserId ) . toBe ( TEST_USER_ID ) ;
281+ } ) ;
282+
283+ it ( 'keeps the previously set user id when called with an empty value' , async ( ) => {
284+ const client = await clientWithConnectionString ( VALID_CONNECTION_STRING ) ;
285+ client . initialize ( VALID_OPTIONS ) ;
286+
287+ client . setUserId ( TEST_USER_ID ) ;
288+ client . setUserId ( '' ) ;
289+ client . track ( 'Some.Event' ) ;
290+
291+ const [ logRecord ] = mocks . emit . mock . calls [ 0 ] ;
292+ expect ( logRecord . attributes . CloudUserId ) . toBe ( TEST_USER_ID ) ;
293+ } ) ;
294+ } ) ;
295+
230296describe ( 'TelemetryClient.getDefaultEventName' , ( ) => {
231297 it ( 'returns undefined before initialize' , ( ) => {
232298 const client = new TelemetryClient ( ) ;
0 commit comments