11import { beforeEach , describe , expect , it , vi } from 'vitest' ;
2+ import type { MatrixClient } from '$types/matrix-sdk' ;
23
34const tauriApi = vi . hoisted ( ( ) => ( {
45 isTauri : vi . fn < ( ) => boolean > ( ) ,
@@ -15,7 +16,7 @@ const mediaTransport = vi.hoisted(() => ({
1516vi . mock ( '@tauri-apps/api/core' , ( ) => tauriApi ) ;
1617vi . mock ( './mediaTransport' , ( ) => mediaTransport ) ;
1718
18- const { rewriteAuthenticatedMediaUrl } = await import ( './matrix' ) ;
19+ const { mxcUrlToHttp , rewriteAuthenticatedMediaUrl } = await import ( './matrix' ) ;
1920
2021describe ( 'rewriteAuthenticatedMediaUrl' , ( ) => {
2122 beforeEach ( ( ) => {
@@ -59,6 +60,36 @@ describe('rewriteAuthenticatedMediaUrl', () => {
5960 ) ;
6061 } ) ;
6162
63+ it . each ( [
64+ '/_matrix/media/v3/download/example.org/abc123' ,
65+ '/_matrix/media/v3/thumbnail/example.org/abc123?width=96&height=96&method=crop' ,
66+ '/_matrix/media/r0/download/example.org/abc123' ,
67+ '/_matrix/media/r0/thumbnail/example.org/abc123?width=96&height=96&method=crop' ,
68+ ] ) ( 'rewrites legacy media paths under Tauri: %s' , ( path ) => {
69+ tauriApi . isTauri . mockReturnValue ( true ) ;
70+ const url = `https://matrix.example.org${ path } ` ;
71+ const separator = url . includes ( '?' ) ? '&' : '?' ;
72+ expect ( rewriteAuthenticatedMediaUrl ( url ) ) . toBe (
73+ `sable-media://${ url } ${ separator } __sable_media_cache=2&__sable_media_session=%40user%3Aexample.com`
74+ ) ;
75+ } ) ;
76+
77+ it ( 'does not rewrite unrelated Matrix media URLs' , ( ) => {
78+ tauriApi . isTauri . mockReturnValue ( true ) ;
79+ const url = 'https://matrix.example.org/_matrix/media/v3/config' ;
80+ expect ( rewriteAuthenticatedMediaUrl ( url ) ) . toBe ( url ) ;
81+ expect ( tauriApi . convertFileSrc ) . not . toHaveBeenCalled ( ) ;
82+ } ) ;
83+
84+ it . each ( [
85+ 'https://example.org/avatar.png?next=/_matrix/media/v3/download/example.org/abc123' ,
86+ 'https://example.org/avatar.png#/_matrix/media/r0/thumbnail/example.org/abc123' ,
87+ ] ) ( 'does not rewrite a media path only present in query or hash: %s' , ( url ) => {
88+ tauriApi . isTauri . mockReturnValue ( true ) ;
89+ expect ( rewriteAuthenticatedMediaUrl ( url ) ) . toBe ( url ) ;
90+ expect ( tauriApi . convertFileSrc ) . not . toHaveBeenCalled ( ) ;
91+ } ) ;
92+
6293 it ( 'passes through already-rewritten sable-media:// URLs' , ( ) => {
6394 tauriApi . isTauri . mockReturnValue ( true ) ;
6495 const url =
@@ -69,3 +100,17 @@ describe('rewriteAuthenticatedMediaUrl', () => {
69100 expect ( tauriApi . convertFileSrc ) . not . toHaveBeenCalled ( ) ;
70101 } ) ;
71102} ) ;
103+
104+ describe ( 'mxcUrlToHttp' , ( ) => {
105+ it ( 'rewrites SDK legacy media URLs under Tauri without useAuthentication' , ( ) => {
106+ tauriApi . isTauri . mockReturnValue ( true ) ;
107+ const legacyUrl = 'https://matrix.example.org/_matrix/media/v3/download/example.org/video' ;
108+ const mx = {
109+ mxcUrlToHttp : vi . fn < ( ) => string > ( ( ) => legacyUrl ) ,
110+ } as unknown as MatrixClient ;
111+
112+ expect ( mxcUrlToHttp ( mx , 'mxc://example.org/video' , false ) ) . toBe (
113+ `sable-media://${ legacyUrl } ?__sable_media_cache=2&__sable_media_session=%40user%3Aexample.com`
114+ ) ;
115+ } ) ;
116+ } ) ;
0 commit comments