@@ -288,6 +288,38 @@ describe('user info service', () => {
288288 expect ( user ) . toHaveProperty ( 'timezone' , 'Europe/Paris' )
289289 } )
290290
291+ it ( 'returns user info but without language/timezone if common settings is enabled but common settings service returns no values' , async ( ) => {
292+ const configWithCommon = {
293+ ...config ,
294+ features : { common_settings : { enabled : true } }
295+ } as unknown as Config
296+
297+ const mockTwakeDBNoSettings = {
298+ ...twakeDBMock ,
299+ get : jest . fn ( ) . mockImplementation ( async ( table , _fields , query ) => {
300+ if ( table === 'usersettings' && query . matrix_id != null ) {
301+ return [ ]
302+ }
303+ return [ ]
304+ } )
305+ }
306+
307+ const svc = new UserInfoService (
308+ userDb ,
309+ mockTwakeDBNoSettings as unknown as TwakeDB ,
310+ matrixDBMock as unknown as MatrixDB ,
311+ configWithCommon ,
312+ logger
313+ )
314+
315+ const user = await svc . get ( '@dwho:docker.localhost' )
316+
317+ expect ( user ) . not . toBeNull ( )
318+ expect ( user ) . toHaveProperty ( 'display_name' , 'Dr Who' )
319+ expect ( user ) . not . toHaveProperty ( 'language' )
320+ expect ( user ) . not . toHaveProperty ( 'timezone' )
321+ } )
322+
291323 it ( 'propagates avatar_url as avatar when present' , async ( ) => {
292324 ; ( matrixDBMock . get as jest . Mock ) . mockResolvedValueOnce ( [
293325 { displayname : 'Dr Who' , avatar_url : 'http://example.com/avatar.png' }
@@ -323,8 +355,8 @@ describe('user info service', () => {
323355 getSpy . mockRestore ( )
324356 } )
325357
326- it ( 'passes the exact matrix id to the usersettings query' , async ( ) => {
327- // Create a service instance with the common‑ settings feature turned ON
358+ it ( 'passes the exact matrix id to the usersettings query (when common settings enabled) ' , async ( ) => {
359+ // Create a service instance with the common- settings feature turned ON
328360 const cfg = {
329361 ...config ,
330362 features : { common_settings : { enabled : true } }
@@ -338,16 +370,16 @@ describe('user info service', () => {
338370 logger
339371 )
340372
341- // spy on the DB mock ( twakeDBMock.get is already a jest.fn )
342- const spy = jest . spyOn ( twakeDBMock , 'get' )
373+ twakeDBMock . get . mockClear ( )
374+
343375 await svc . get ( '@dwho:docker.localhost' )
344- expect ( spy ) . toHaveBeenCalledWith ( 'usersettings' , [ '*' ] , {
376+
377+ expect ( twakeDBMock . get ) . toHaveBeenCalledWith ( 'usersettings' , [ '*' ] , {
345378 matrix_id : '@dwho:docker.localhost'
346379 } )
347- spy . mockRestore ( )
348380 } )
349381
350- it ( 're‑ throws a wrapped error when an internal query fails' , async ( ) => {
382+ it ( 're- throws a wrapped error when an internal query fails' , async ( ) => {
351383 const brokenMatrix = {
352384 get : jest . fn ( ) . mockRejectedValue ( new Error ( 'boom' ) )
353385 } as unknown as MatrixDB
@@ -365,21 +397,29 @@ describe('user info service', () => {
365397 } )
366398
367399 it ( 'honors the env var FEATURE_COMMON_SETTINGS_ENABLED' , async ( ) => {
400+ // Set the environment variable BEFORE creating the service instance
368401 process . env . FEATURE_COMMON_SETTINGS_ENABLED = 'true'
402+
369403 const cfg = {
370404 ...config ,
371405 features : { common_settings : { enabled : false } } as any
372406 }
407+
408+ // New service instance created AFTER env var is set
373409 const svc = new UserInfoService (
374410 userDb ,
375411 twakeDBMock as unknown as TwakeDB ,
376412 matrixDBMock as unknown as MatrixDB ,
377413 cfg as unknown as Config ,
378414 logger
379415 )
416+
380417 const user = await svc . get ( '@dwho:docker.localhost' )
418+ expect ( user ) . not . toBeNull ( )
381419 expect ( user ) . toHaveProperty ( 'language' , 'fr' )
382420 expect ( user ) . toHaveProperty ( 'timezone' , 'Europe/Paris' )
421+
422+ // Clean up the environment variable
383423 delete process . env . FEATURE_COMMON_SETTINGS_ENABLED
384424 } )
385425
@@ -419,12 +459,4 @@ describe('user info service', () => {
419459 expect ( user ?. sn ) . toBe ( 'One' )
420460 expect ( user ?. givenName ) . toBe ( 'Alpha' )
421461 } )
422-
423- it ( 'passes the exact matrix id to the usersettings query' , async ( ) => {
424- const spy = jest . spyOn ( twakeDBMock , 'get' )
425- await service . get ( '@dwho:docker.localhost' )
426- expect ( spy ) . toHaveBeenCalledWith ( 'usersettings' , [ '*' ] , {
427- matrix_id : '@dwho:docker.localhost'
428- } )
429- } )
430462} )
0 commit comments