"""Tests : réécriture de playlists HLS (proxy) et sélection de variante.""" from app.routers.proxy import _rewrite_playlist from app.services.downloads import _best_variant, _parse_ffmpeg_time MASTER = """#EXTM3U #EXT-X-STREAM-INF:BANDWIDTH=400000,RESOLUTION=640x360 low/index.m3u8 #EXT-X-STREAM-INF:BANDWIDTH=900000,RESOLUTION=1280x720 https://cdn.example.com/high/index.m3u8 """ MEDIA = """#EXTM3U #EXT-X-KEY:METHOD=AES-128,URI="https://cdn.example.com/key.bin",IV=0xabc #EXTINF:6.0, seg-1.ts #EXTINF:6.0, https://cdn.example.com/seg-2.ts """ def test_best_variant_picks_highest_bandwidth(): url = _best_variant(MASTER, "https://site.example.com/master.m3u8") assert url == "https://cdn.example.com/high/index.m3u8" def test_best_variant_none_for_media_playlist(): assert _best_variant(MEDIA, "https://x/") is None def test_parse_ffmpeg_time(): assert _parse_ffmpeg_time("00:01:30.50") == 90.5 assert _parse_ffmpeg_time("01:00:00.00") == 3600.0 def test_rewrite_playlist_routes_everything_through_proxy(): out = _rewrite_playlist(MEDIA, "https://cdn.example.com/hls/master.m3u8", "https://ref/") assert "/api/proxy?url=" in out # URI relative des segments résolue contre l'URL de base assert "seg-1.ts" in out and "https%3A%2F%2Fcdn.example.com%2Fhls%2Fseg-1.ts" in out # URI absolue conservée mais proxiée assert "seg-2.ts" in out # La clé AES dans l'attribut URI="..." est aussi réécrite assert 'URI="/api/proxy?url=' in out # Le referer est propagé aux URLs proxiées assert "ref=https%3A%2F%2Fref%2F" in out def test_rewrite_playlist_without_referer(): out = _rewrite_playlist("#EXTM3U\nseg.ts\n", "https://cdn.example.com/m.m3u8", None) assert "ref=" not in out assert out.startswith("#EXTM3U")