@@ -57,7 +57,7 @@ pub fn convert_https_permalink_to_matrix_uri(url_str: &str) -> Option<String> {
5757 // Room alias - Example: #room:example.org -> matrix:r/room:example.org
5858 format ! ( "matrix:r/{}" , & stripped[ 1 ..] )
5959 } else if stripped. starts_with ( '!' ) {
60- // Room ID - Example: !roomid:example.org -> matrix:roomid/roomid:example.org
60+ // Room ID - Example: !roomid:example.org -> matrix:roomid/! roomid:example.org
6161 format ! ( "matrix:roomid/{}" , stripped)
6262 } else if stripped. starts_with ( '@' ) {
6363 // User ID - Example: @user:example.org -> matrix:u/user:example.org
@@ -236,4 +236,72 @@ mod tests {
236236 "https://matrix.to"
237237 ) ;
238238 }
239+
240+ #[ test]
241+ fn test_room_alias_conversion ( ) {
242+ // Test room alias: #room:example.org
243+ let url = "https://links.example.org/#/#room:example.org" ;
244+ let result = convert_https_permalink_to_matrix_uri ( url) . unwrap ( ) ;
245+ assert_eq ! ( result, "matrix:r/room:example.org" ) ;
246+ }
247+
248+ #[ test]
249+ fn test_room_id_conversion ( ) {
250+ // Test room ID: !roomid:example.org
251+ let url = "https://custom.resolver/#/!roomid:example.org" ;
252+ let result = convert_https_permalink_to_matrix_uri ( url) . unwrap ( ) ;
253+ assert_eq ! ( result, "matrix:roomid/!roomid:example.org" ) ;
254+ }
255+
256+ #[ test]
257+ fn test_user_id_conversion ( ) {
258+ // Test user ID: @user:example.org
259+ let url = "https://matrix.to/#/@user:example.org" ;
260+ let result = convert_https_permalink_to_matrix_uri ( url) . unwrap ( ) ;
261+ assert_eq ! ( result, "matrix:u/user:example.org" ) ;
262+ }
263+
264+ #[ test]
265+ fn test_event_permalink_with_query ( ) {
266+ // Test with query parameters
267+ let url = "https://matrix.to/#/#room:example.org?via=server1.org&via=server2.org" ;
268+ let result = convert_https_permalink_to_matrix_uri ( url) . unwrap ( ) ;
269+ // Should preserve query parameters
270+ assert_eq ! ( result, "matrix:r/room:example.org?via=server1.org&via=server2.org" ) ;
271+ }
272+
273+ #[ test]
274+ fn test_invalid_permalink_no_conversion ( ) {
275+ // Test invalid permalink (no fragment)
276+ let url = "https://example.com/page" ;
277+ let result = convert_https_permalink_to_matrix_uri ( url) ;
278+ assert ! ( result. is_none( ) ) ;
279+
280+ // Test invalid permalink (fragment doesn't start with /)
281+ let url2 = "https://example.com/#anchor" ;
282+ let result2 = convert_https_permalink_to_matrix_uri ( url2) ;
283+ assert ! ( result2. is_none( ) ) ;
284+ }
285+
286+ #[ test]
287+ fn test_linkify_with_room_id ( ) {
288+ // Test linkify with room ID
289+ let text = "Join this room: https://matrix.to/#/!roomid:example.org" ;
290+ let result = linkify_outgoing_text_to_html ( text) . unwrap ( ) ;
291+
292+ // Should contain correct matrix: URI with ! preserved
293+ assert ! ( result. contains( r#"href="matrix:roomid/!roomid:example.org""# ) ) ;
294+ assert ! ( result. contains( "https://matrix.to/#/!roomid:example.org" ) ) ;
295+ }
296+
297+ #[ test]
298+ fn test_linkify_mixed_identifiers ( ) {
299+ // Test with different identifier types
300+ let text = "User https://matrix.to/#/@user:example.org room https://links.org/#/!room:example.org" ;
301+ let result = linkify_outgoing_text_to_html ( text) . unwrap ( ) ;
302+
303+ // Check both conversions
304+ assert ! ( result. contains( "matrix:u/user:example.org" ) ) ;
305+ assert ! ( result. contains( "matrix:roomid/!room:example.org" ) ) ;
306+ }
239307}
0 commit comments