@@ -365,6 +365,85 @@ func TestLoadMappingsFromFile_InvalidJSON(t *testing.T) {
365365 assert .Contains (t , err .Error (), "failed to parse mapping file" )
366366}
367367
368+ func TestResolveMatrixUser_SubNumbers (t * testing.T ) {
369+ // Test case 1: Resolve sub_number to matrix_id
370+ t .Run ("resolve sub_number to matrix_id" , func (t * testing.T ) {
371+ svc := NewMessageService (nil , nil )
372+ svc .SaveMapping (& models.MappingRequest {
373+ Number : "201" ,
374+ MatrixID : "@giacomo:example.com" ,
375+ RoomID : "!room1:example.com" ,
376+ UserName : "Giacomo Rossi" ,
377+ SubNumbers : []string {"3344" , "91201" },
378+ })
379+
380+ // Resolve using a sub_number
381+ result := svc .resolveMatrixUser ("91201" )
382+ assert .Equal (t , "@giacomo:example.com" , string (result ), "should resolve sub_number to matrix_id" )
383+ })
384+
385+ // Test case 2: Resolve main number to matrix_id
386+ t .Run ("resolve main number to matrix_id" , func (t * testing.T ) {
387+ svc := NewMessageService (nil , nil )
388+ svc .SaveMapping (& models.MappingRequest {
389+ Number : "202" ,
390+ MatrixID : "@mario:example.com" ,
391+ RoomID : "!room2:example.com" ,
392+ UserName : "Mario Bianchi" ,
393+ })
394+
395+ // Resolve using the main number
396+ result := svc .resolveMatrixUser ("202" )
397+ assert .Equal (t , "@mario:example.com" , string (result ), "should resolve main number to matrix_id" )
398+ })
399+
400+ // Test case 3: Resolve another sub_number
401+ t .Run ("resolve another sub_number" , func (t * testing.T ) {
402+ svc := NewMessageService (nil , nil )
403+ svc .SaveMapping (& models.MappingRequest {
404+ Number : "201" ,
405+ MatrixID : "@giacomo:example.com" ,
406+ RoomID : "!room1:example.com" ,
407+ UserName : "Giacomo Rossi" ,
408+ SubNumbers : []string {"3344" , "91201" },
409+ })
410+
411+ // Resolve using a different sub_number
412+ result := svc .resolveMatrixUser ("3344" )
413+ assert .Equal (t , "@giacomo:example.com" , string (result ), "should resolve any sub_number to matrix_id" )
414+ })
415+
416+ // Test case 4: Matrix ID passed directly
417+ t .Run ("matrix id passed directly" , func (t * testing.T ) {
418+ svc := NewMessageService (nil , nil )
419+ result := svc .resolveMatrixUser ("@test:example.com" )
420+ assert .Equal (t , "@test:example.com" , string (result ), "should return matrix_id as-is if it starts with @" )
421+ })
422+
423+ // Test case 5: No mapping found
424+ t .Run ("no mapping found" , func (t * testing.T ) {
425+ svc := NewMessageService (nil , nil )
426+ result := svc .resolveMatrixUser ("9999" )
427+ assert .Equal (t , "" , string (result ), "should return empty string if no mapping found" )
428+ })
429+
430+ // Test case 6: Case insensitivity
431+ t .Run ("case insensitive sub_number resolution" , func (t * testing.T ) {
432+ svc := NewMessageService (nil , nil )
433+ svc .SaveMapping (& models.MappingRequest {
434+ Number : "201" ,
435+ MatrixID : "@giacomo:example.com" ,
436+ RoomID : "!room1:example.com" ,
437+ UserName : "Giacomo Rossi" ,
438+ SubNumbers : []string {"3344" , "91201" },
439+ })
440+
441+ // Resolve with different case (though phone numbers are typically numeric)
442+ result := svc .resolveMatrixUser ("91201" )
443+ assert .Equal (t , "@giacomo:example.com" , string (result ), "should resolve case-insensitively" )
444+ })
445+ }
446+
368447func TestResolveMatrixIDToIdentifier_SubNumbers (t * testing.T ) {
369448 // Test case 1: Resolve via sub_number match
370449 // When a matrix_id matches one of the sub_numbers, the main number should be returned (not the sub_number)
0 commit comments