mirror of
https://github.com/barkeser2002/turkanime-gui.git
synced 2026-11-24 20:46:18 +03:00
Bütün sayfalar web'e geçtiği için `gui/qt/pages/` ve `gui/qt/widgets.py` silindi. Sayfalardan bağımsız kalan parçalar yerine taşındı: - İndirme yöneticisi `gui/qt/indirme.py` - Künye yardımcıları `gui/web/kunye.py` (Qt'siz) - Otomatik eşleştirme `gui/web/eslestirme.py` (Qt'siz) - Keşif verisi `gui/web/uclar_kesif.py` Eski sayfa testleri, sayfaların çağırdığı uçlara (`AyarlarUclari`, `DetayUclari`, `KesifUclari`, `IzlemeUclari`) ve web sürücüsüyle DOM'a taşındı. Kaynak başına akordiyon için yeni `test_web_bolumler.py` yazıldı. Izgara yerleşimi artık CSS grid ve tarayıcının gerçek ölçüleriyle sınanıyor. Taşıma sırasında bulunan iki hata: - Arşiv durumu okunamayınca (`PermissionError`) sayfa yanlış bir mesaj gösteriyordu: "indirme klasörüne yazılamıyor". Artık asıl sebep yazılıyor. - İzleme Listem'de elle başlatılan senkron bitince durum satırı "sürüyor"da kalıyordu. Girişten sonraki kendiliğinden senkron ise, değişiklik olmasa bile bildirim gösteriyordu. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015orra9NXZHpRa7PJyxoyR2
611 lines
24 KiB
Python
611 lines
24 KiB
Python
"""İndirme kuyruğu: ayarların gerçekten okunması, iptal, yeniden deneme, geçmiş.
|
||
|
||
Buradaki testlerin çoğu bir hatayı kilitliyor: Qt tarafı `ayarlar.json`'daki
|
||
"aria2c kullan", "max resolution", "paralel indirme sayisi" gibi ayarları hiç
|
||
okumuyordu — kullanıcı ayarı değiştiriyor, hiçbir şey olmuyordu.
|
||
|
||
Ağa çıkılmaz, gerçek yt-dlp/aria2c çalıştırılmaz: `Bolum`/`Video` sahteleri
|
||
`best_video`/`indir` çağrılarını kaydeder.
|
||
"""
|
||
from __future__ import annotations
|
||
|
||
import os
|
||
import threading
|
||
import time
|
||
|
||
import pytest
|
||
|
||
from turkanime_api.gui.qt.indirme import (
|
||
BITMIS_DURUMLAR, DURUM_BEKLIYOR, DURUM_HATA, DURUM_INDIRILIYOR, DURUM_IPTAL,
|
||
DURUM_TAMAMLANDI, MAX_DENEME, DownloadManager,
|
||
)
|
||
|
||
|
||
# ── Sahteler ─────────────────────────────────────────────────────────────────
|
||
class SahteAnime:
|
||
def __init__(self, slug="naruto-test", title="Naruto Test"):
|
||
self.slug = slug
|
||
self.title = title
|
||
|
||
|
||
class SahteVideo:
|
||
"""`objects.Video` yerine: indirme çağrılarını kaydeder, ağa çıkmaz."""
|
||
|
||
def __init__(self, bolum, player="GDRIVE", hata: Exception | None = None):
|
||
self.bolum = bolum
|
||
self.player = player
|
||
self.ydl_opts = {}
|
||
self._hata = hata
|
||
self.deneme = 0
|
||
self.cikti = []
|
||
|
||
def indir(self, callback=None, output=""):
|
||
self.deneme += 1
|
||
self.cikti.append(output)
|
||
if self._hata is not None:
|
||
raise self._hata
|
||
if callback:
|
||
callback({"status": "downloading", "downloaded_bytes": 512,
|
||
"total_bytes": 1024, "speed": 256})
|
||
callback({"status": "finished"})
|
||
|
||
def duzelt(self):
|
||
"""Yeniden deneme testinde işi başarıya çevir."""
|
||
self._hata = None
|
||
|
||
|
||
class BlokeVideo:
|
||
"""İptal gelene kadar ilerleme yayan sahte indirme."""
|
||
|
||
def __init__(self, bolum):
|
||
self.bolum = bolum
|
||
self.player = "GDRIVE"
|
||
self.ydl_opts = {}
|
||
self.basladi = threading.Event()
|
||
self.hook_sayisi = 0
|
||
|
||
def indir(self, callback=None, output=""):
|
||
self.basladi.set()
|
||
for i in range(20000):
|
||
callback({"status": "downloading", "downloaded_bytes": i,
|
||
"total_bytes": 20000})
|
||
# Hook istisna fırlatırsa sayaç ARTMAZ: "hook durdu mu?" ölçümü.
|
||
self.hook_sayisi += 1
|
||
time.sleep(0.005)
|
||
callback({"status": "finished"})
|
||
|
||
|
||
class SahteBolum:
|
||
def __init__(self, video=None, slug="naruto-test-1-bolum", anime=None):
|
||
self.slug = slug
|
||
self.anime = anime if anime is not None else SahteAnime()
|
||
self.video = video if video is not None else SahteVideo(self)
|
||
if getattr(self.video, "bolum", None) is None:
|
||
self.video.bolum = self
|
||
self.best_kwargs = None
|
||
|
||
def best_video(self, **kwargs):
|
||
self.best_kwargs = kwargs
|
||
return self.video
|
||
|
||
|
||
def _entry(bolum, title="Naruto Test 1. Bölüm"):
|
||
return {"title": title, "obj": bolum}
|
||
|
||
|
||
@pytest.fixture
|
||
def manager(qtbot, izole_ev):
|
||
"""Her test kendi yöneticisiyle çalışsın; işler testler arasında sızmasın.
|
||
|
||
`izole_ev` şart: indirme gerçek `gecmis.json`'a yazıyor VE okuyor. Yalnızca
|
||
yazımı geri almak yetmiyordu — "geçmişe yazılmadı" iddiaları kullanıcının
|
||
dosyasında aynı bölüm zaten varsa yalancı kırmızı veriyordu.
|
||
"""
|
||
mgr = DownloadManager()
|
||
yield mgr
|
||
mgr.cancel_all()
|
||
|
||
|
||
def _bekle(qtbot, manager, task_id, timeout=10000):
|
||
qtbot.waitUntil(lambda: manager.durum(task_id) in BITMIS_DURUMLAR,
|
||
timeout=timeout)
|
||
|
||
|
||
# ── Ayarlar gerçekten okunuyor mu? ───────────────────────────────────────────
|
||
def test_max_resolution_ve_aday_sayisi_best_video_a_gidiyor(qtbot, manager, ayarla,
|
||
tmp_path):
|
||
""""max resolution" ve "1080p aday sayısı" `best_video`'ya paslanmalı."""
|
||
ayarla(**{"max resolution": False, "1080p aday sayısı": 3})
|
||
bolum = SahteBolum()
|
||
|
||
task_id = manager.enqueue(_entry(bolum), output=str(tmp_path))
|
||
_bekle(qtbot, manager, task_id)
|
||
|
||
assert bolum.best_kwargs == {"by_res": False, "early_subset": 3}
|
||
|
||
|
||
def test_max_resolution_acikken_by_res_true(qtbot, manager, ayarla, tmp_path):
|
||
ayarla(**{"max resolution": True, "1080p aday sayısı": 12})
|
||
bolum = SahteBolum()
|
||
|
||
task_id = manager.enqueue(_entry(bolum), output=str(tmp_path))
|
||
_bekle(qtbot, manager, task_id)
|
||
|
||
assert bolum.best_kwargs == {"by_res": True, "early_subset": 12}
|
||
|
||
|
||
def test_aria2c_ayari_aria2c_yolunu_seciyor(qtbot, manager, ayarla, monkeypatch,
|
||
tmp_path):
|
||
""""aria2c kullan" açıkken `cli_tools.indir_aria2c` kullanılmalı."""
|
||
import turkanime_api.cli.cli_tools as cli_tools
|
||
|
||
cagrilar = []
|
||
monkeypatch.setattr(cli_tools, "indir_aria2c",
|
||
lambda video, callback, output: cagrilar.append(output) or True)
|
||
|
||
ayarla(**{"aria2c kullan": True})
|
||
bolum = SahteBolum()
|
||
|
||
task_id = manager.enqueue(_entry(bolum), output=str(tmp_path))
|
||
_bekle(qtbot, manager, task_id)
|
||
|
||
assert manager.durum(task_id) == DURUM_TAMAMLANDI
|
||
assert cagrilar == [str(tmp_path)]
|
||
assert bolum.video.deneme == 0, "aria2c seçilince yt-dlp yolu çalışmamalı"
|
||
|
||
|
||
def test_aria2c_kapaliyken_yt_dlp_kullaniliyor(qtbot, manager, ayarla, monkeypatch,
|
||
tmp_path):
|
||
import turkanime_api.cli.cli_tools as cli_tools
|
||
|
||
cagrilar = []
|
||
monkeypatch.setattr(cli_tools, "indir_aria2c",
|
||
lambda *a, **k: cagrilar.append(k) or True)
|
||
|
||
ayarla(**{"aria2c kullan": False})
|
||
bolum = SahteBolum()
|
||
|
||
task_id = manager.enqueue(_entry(bolum), output=str(tmp_path))
|
||
_bekle(qtbot, manager, task_id)
|
||
|
||
assert cagrilar == []
|
||
assert bolum.video.deneme == 1
|
||
|
||
|
||
def test_aria2c_alucard_player_icin_atlaniyor(qtbot, manager, ayarla, monkeypatch,
|
||
tmp_path):
|
||
"""ALUCARD linkleri yt-dlp impersonate oturumuna bağlı; aria2c taşıyamıyor."""
|
||
import turkanime_api.cli.cli_tools as cli_tools
|
||
|
||
cagrilar = []
|
||
monkeypatch.setattr(cli_tools, "indir_aria2c",
|
||
lambda *a, **k: cagrilar.append(k) or True)
|
||
|
||
ayarla(**{"aria2c kullan": True})
|
||
bolum = SahteBolum()
|
||
bolum.video.player = "ALUCARD(BETA)"
|
||
|
||
task_id = manager.enqueue(_entry(bolum), output=str(tmp_path))
|
||
_bekle(qtbot, manager, task_id)
|
||
|
||
assert cagrilar == []
|
||
assert bolum.video.deneme == 1
|
||
|
||
|
||
def test_aria2c_basarisiz_donerse_hata(qtbot, manager, ayarla, monkeypatch, tmp_path):
|
||
"""`indir_aria2c` False dönerse başarı sayılmamalı."""
|
||
import turkanime_api.cli.cli_tools as cli_tools
|
||
|
||
monkeypatch.setattr(cli_tools, "indir_aria2c", lambda *a, **k: False)
|
||
ayarla(**{"aria2c kullan": True})
|
||
|
||
task_id = manager.enqueue(_entry(SahteBolum()), output=str(tmp_path))
|
||
_bekle(qtbot, manager, task_id)
|
||
|
||
assert manager.durum(task_id) == DURUM_HATA
|
||
|
||
|
||
def test_paralel_indirme_sayisi_havuz_boyutunu_belirliyor(manager, ayarla,
|
||
monkeypatch, tmp_path):
|
||
""""paralel indirme sayisi" sabit 3 yerine havuz boyutunu belirlemeli."""
|
||
import turkanime_api.gui.qt.indirme as dl_mod
|
||
from turkanime_api.gui.qt.workers import long_task_pool
|
||
|
||
monkeypatch.setattr(dl_mod, "run_bg", lambda *a, **k: None) # iş başlamasın
|
||
|
||
# Farklı bölümler: aynı bölüm ikinci kez kuyruğa girmiyor (bkz.
|
||
# test_ayni_bolum_ikinci_kez_kuyruga_girmiyor).
|
||
ayarla(**{"paralel indirme sayisi": 6})
|
||
manager.enqueue(_entry(SahteBolum(slug="naruto-test-1-bolum")), output=str(tmp_path))
|
||
assert long_task_pool().maxThreadCount() == 6
|
||
|
||
ayarla(**{"paralel indirme sayisi": 2})
|
||
manager.enqueue(_entry(SahteBolum(slug="naruto-test-2-bolum")), output=str(tmp_path))
|
||
assert long_task_pool().maxThreadCount() == 2
|
||
|
||
|
||
def test_bozuk_paralel_ayari_varsayilana_dusuyor(ayarla):
|
||
from turkanime_api.gui.qt import prefs
|
||
|
||
ayarla(**{"paralel indirme sayisi": 0})
|
||
assert prefs.oku().paralel == prefs.VARSAYILAN_PARALEL
|
||
ayarla(**{"paralel indirme sayisi": "abc"})
|
||
assert prefs.oku().paralel == prefs.VARSAYILAN_PARALEL
|
||
|
||
|
||
# ── İptal ────────────────────────────────────────────────────────────────────
|
||
def test_calisan_indirme_iptal_edilebiliyor(qtbot, manager, ayarla, tmp_path):
|
||
"""İptal gerçekten durdurmalı: hook `return` etseydi indirme sürerdi."""
|
||
ayarla(**{"aria2c kullan": False})
|
||
bolum = SahteBolum()
|
||
video = BlokeVideo(bolum)
|
||
bolum.video = video
|
||
|
||
task_id = manager.enqueue(_entry(bolum), output=str(tmp_path))
|
||
assert video.basladi.wait(10), "indirme hiç başlamadı"
|
||
manager.cancel(task_id)
|
||
_bekle(qtbot, manager, task_id)
|
||
|
||
assert manager.durum(task_id) == DURUM_IPTAL
|
||
sayac = video.hook_sayisi
|
||
time.sleep(0.15)
|
||
assert video.hook_sayisi == sayac, "hook iptalden sonra da çalışıyor"
|
||
|
||
|
||
def test_bekleyen_is_aninda_iptal_ediliyor(manager, ayarla, monkeypatch, tmp_path):
|
||
"""Havuz doluysa iş dakikalarca başlamaz; iptal geri bildirimi beklememeli."""
|
||
import turkanime_api.gui.qt.indirme as dl_mod
|
||
|
||
monkeypatch.setattr(dl_mod, "run_bg", lambda *a, **k: None)
|
||
ayarla(**{"aria2c kullan": False})
|
||
|
||
task_id = manager.enqueue(_entry(SahteBolum()), output=str(tmp_path))
|
||
assert manager.durum(task_id) == DURUM_BEKLIYOR
|
||
assert manager.cancel(task_id) is True
|
||
assert manager.durum(task_id) == DURUM_IPTAL
|
||
# İkinci iptal yok sayılmalı (çift "bitti" raporu üretmemeli).
|
||
assert manager.cancel(task_id) is False
|
||
|
||
|
||
def test_cancel_all_bekleyen_isleri_kapatiyor(manager, ayarla, monkeypatch, tmp_path):
|
||
import turkanime_api.gui.qt.indirme as dl_mod
|
||
|
||
monkeypatch.setattr(dl_mod, "run_bg", lambda *a, **k: None)
|
||
ayarla(**{"aria2c kullan": False})
|
||
|
||
ids = [manager.enqueue(_entry(SahteBolum(slug=f"naruto-test-{i}-bolum")),
|
||
output=str(tmp_path))
|
||
for i in range(3)]
|
||
assert manager.cancel_all() == 3
|
||
assert all(manager.durum(i) == DURUM_IPTAL for i in ids)
|
||
assert manager.active_ids() == []
|
||
|
||
|
||
# ── Aynı bölüm iki kez kuyruğa girmiyor ────────────────────────────────────
|
||
@pytest.fixture
|
||
def baslamayan(monkeypatch):
|
||
"""`run_bg` sahte: işler "bekliyor"da kalsın, çağrılar sayılsın."""
|
||
import turkanime_api.gui.qt.indirme as dl_mod
|
||
cagrilar = []
|
||
monkeypatch.setattr(dl_mod, "run_bg", lambda *a, **k: cagrilar.append(a))
|
||
return cagrilar
|
||
|
||
|
||
def test_ayni_bolum_ikinci_kez_kuyruga_girmiyor(manager, ayarla, baslamayan, tmp_path):
|
||
"""ESKİ HATA: `enqueue` her çağrıda yeni iş kuruyordu; iki yt-dlp aynı
|
||
dosyaya yazıyor, ikisi de "tamamlandı" diyor, dosya bozuk çıkıyordu."""
|
||
eklenen = []
|
||
manager.added.connect(lambda tid, _t: eklenen.append(tid))
|
||
bolum = SahteBolum()
|
||
|
||
ilk = manager.enqueue(_entry(bolum), output=str(tmp_path))
|
||
ikinci = manager.enqueue(_entry(bolum), output=str(tmp_path))
|
||
|
||
assert ikinci == ilk
|
||
assert manager.active_ids() == [ilk]
|
||
assert len(baslamayan) == 1 and eklenen == [ilk]
|
||
assert manager.kuyruktaki_is(_entry(bolum), output=str(tmp_path)) == ilk
|
||
|
||
|
||
def test_ayni_hedefe_giden_baska_nesne_de_ayni_is(manager, ayarla, baslamayan, tmp_path):
|
||
"""Anahtar nesne değil hedef yol: yeniden getirilmiş liste yeni nesne verir."""
|
||
ilk = manager.enqueue(_entry(SahteBolum()), output=str(tmp_path))
|
||
assert manager.enqueue(_entry(SahteBolum()), output=str(tmp_path)) == ilk
|
||
|
||
baska_bolum = manager.enqueue(_entry(SahteBolum(slug="naruto-test-2-bolum")),
|
||
output=str(tmp_path))
|
||
baska_klasor = manager.enqueue(_entry(SahteBolum()), output=str(tmp_path / "b"))
|
||
assert len({ilk, baska_bolum, baska_klasor}) == 3
|
||
|
||
|
||
@pytest.mark.parametrize("bitir", ["iptal", "hata", "tamam"])
|
||
def test_bitmis_isten_sonra_yeniden_kuyruga_alinabiliyor(manager, ayarla, baslamayan,
|
||
tmp_path, bitir):
|
||
from turkanime_api.gui.qt import indirme as dl_mod
|
||
|
||
bolum = SahteBolum()
|
||
ilk = manager.enqueue(_entry(bolum), output=str(tmp_path))
|
||
is_ = manager._jobs[ilk]
|
||
if bitir == "iptal":
|
||
manager.cancel(ilk)
|
||
else:
|
||
manager._bitir(is_, bitir == "tamam",
|
||
dl_mod.DURUM_TAMAMLANDI if bitir == "tamam" else DURUM_HATA)
|
||
|
||
yeni = manager.enqueue(_entry(bolum), output=str(tmp_path))
|
||
assert yeni is not None and yeni != ilk
|
||
|
||
|
||
def test_yeniden_dene_ayni_bolum_kuyruktayken_ikinci_is_acmiyor(
|
||
manager, ayarla, baslamayan, tmp_path):
|
||
bolum = SahteBolum()
|
||
ilk = manager.enqueue(_entry(bolum), output=str(tmp_path))
|
||
manager.cancel(ilk)
|
||
ikinci = manager.enqueue(_entry(bolum), output=str(tmp_path))
|
||
|
||
assert manager.retry(ilk) is None, "aynı dosyaya ikinci yazıcı olmamalı"
|
||
assert manager.active_ids() == [ikinci]
|
||
|
||
|
||
# ── Yeniden deneme ───────────────────────────────────────────────────────────
|
||
def test_basarisiz_is_otomatik_bir_kez_tekrarlaniyor(qtbot, manager, ayarla, tmp_path):
|
||
ayarla(**{"aria2c kullan": False})
|
||
bolum = SahteBolum(video=None)
|
||
bolum.video = SahteVideo(bolum, hata=RuntimeError("bağlantı koptu"))
|
||
|
||
task_id = manager.enqueue(_entry(bolum), output=str(tmp_path))
|
||
_bekle(qtbot, manager, task_id)
|
||
|
||
assert manager.durum(task_id) == DURUM_HATA
|
||
assert bolum.video.deneme == MAX_DENEME
|
||
|
||
|
||
class AdresliVideo(SahteVideo):
|
||
"""Adresi olan sahte: kuyruk başarısız adresi `atla`ya ekleyebilsin."""
|
||
|
||
def __init__(self, bolum, url, hata=None, part_yolu=None):
|
||
super().__init__(bolum, hata=hata)
|
||
self.url = url
|
||
self.part_yolu = part_yolu
|
||
self.part_vardi = None
|
||
|
||
def indir(self, callback=None, output=""):
|
||
if self.part_yolu is not None:
|
||
self.part_vardi = os.path.exists(self.part_yolu)
|
||
super().indir(callback, output)
|
||
|
||
|
||
def test_ikinci_deneme_baska_adayi_istiyor_yarim_dosyayi_siliyor(
|
||
qtbot, manager, ayarla, tmp_path):
|
||
"""ESKİ HATA: ikinci deneme AYNI adresi yeniden indiriyordu. Aday
|
||
değişince eski akışın `.part`'ı da silinmeli: yt-dlp ona ekleme yapar ve
|
||
iki farklı akışın baytları tek dosyada birleşirdi."""
|
||
ayarla(**{"aria2c kullan": False})
|
||
bolum = SahteBolum()
|
||
part = tmp_path / "naruto-test" / "naruto-test-1-bolum.mp4.part"
|
||
part.parent.mkdir(parents=True)
|
||
part.write_bytes(b"eski akis")
|
||
a = AdresliVideo(bolum, "https://a.test/v.mp4", hata=RuntimeError("HTTP 403"))
|
||
b = AdresliVideo(bolum, "https://b.test/v.mp4", part_yolu=str(part))
|
||
cagrilar = []
|
||
|
||
def best_video(**kwargs):
|
||
cagrilar.append(kwargs)
|
||
return b if kwargs.get("atla") else a
|
||
bolum.best_video = best_video
|
||
|
||
task_id = manager.enqueue(_entry(bolum), output=str(tmp_path))
|
||
_bekle(qtbot, manager, task_id)
|
||
|
||
assert manager.durum(task_id) == DURUM_TAMAMLANDI
|
||
assert len(cagrilar) == 2
|
||
assert set(cagrilar[1]["atla"]) == {"https://a.test/v.mp4"}
|
||
assert (a.deneme, b.deneme) == (1, 1)
|
||
assert b.part_vardi is False, "yarım dosya ikinci aday başlamadan silinmeli"
|
||
|
||
|
||
def test_baska_aday_yoksa_ayni_video_yeniden_deneniyor(qtbot, manager, ayarla, tmp_path):
|
||
"""Geçici 5xx için: aday kalmadıysa aynı adres, `.part` korunarak."""
|
||
ayarla(**{"aria2c kullan": False})
|
||
bolum = SahteBolum()
|
||
part = tmp_path / "naruto-test" / "naruto-test-1-bolum.mp4.part"
|
||
part.parent.mkdir(parents=True)
|
||
part.write_bytes(b"yarim")
|
||
a = AdresliVideo(bolum, "https://a.test/v.mp4", hata=RuntimeError("HTTP 503"))
|
||
bolum.best_video = lambda **k: None if k.get("atla") else a
|
||
|
||
task_id = manager.enqueue(_entry(bolum), output=str(tmp_path))
|
||
_bekle(qtbot, manager, task_id)
|
||
|
||
assert a.deneme == MAX_DENEME
|
||
assert part.exists(), "aynı adres yeniden denenirken .part silinmemeli"
|
||
|
||
|
||
def test_yeniden_dene_basarisiz_isi_kuyruga_aliyor(qtbot, manager, ayarla, tmp_path):
|
||
ayarla(**{"aria2c kullan": False})
|
||
bolum = SahteBolum(video=None)
|
||
bolum.video = SahteVideo(bolum, hata=RuntimeError("bağlantı koptu"))
|
||
|
||
task_id = manager.enqueue(_entry(bolum), output=str(tmp_path))
|
||
_bekle(qtbot, manager, task_id)
|
||
assert manager.durum(task_id) == DURUM_HATA
|
||
|
||
bolum.video.duzelt()
|
||
assert manager.retry(task_id) == task_id
|
||
_bekle(qtbot, manager, task_id)
|
||
|
||
assert manager.durum(task_id) == DURUM_TAMAMLANDI
|
||
assert bolum.video.deneme == MAX_DENEME + 1
|
||
|
||
|
||
def test_tamamlanmis_is_yeniden_denenmiyor(qtbot, manager, ayarla, tmp_path):
|
||
ayarla(**{"aria2c kullan": False})
|
||
bolum = SahteBolum()
|
||
|
||
task_id = manager.enqueue(_entry(bolum), output=str(tmp_path))
|
||
_bekle(qtbot, manager, task_id)
|
||
|
||
assert manager.retry(task_id) is None
|
||
assert bolum.video.deneme == 1
|
||
|
||
|
||
def test_video_bulunamazsa_hata(qtbot, manager, ayarla, tmp_path):
|
||
ayarla(**{"aria2c kullan": False})
|
||
bolum = SahteBolum()
|
||
bolum.best_video = lambda **k: None
|
||
|
||
task_id = manager.enqueue(_entry(bolum), output=str(tmp_path))
|
||
_bekle(qtbot, manager, task_id)
|
||
|
||
assert manager.durum(task_id) == DURUM_HATA
|
||
|
||
|
||
# ── Durum akışı ve geçmiş ────────────────────────────────────────────────────
|
||
def test_durum_akisi_bekliyor_indiriliyor_tamamlandi(qtbot, manager, ayarla, tmp_path):
|
||
ayarla(**{"aria2c kullan": False})
|
||
goruldu = []
|
||
manager.state.connect(lambda _id, durum: goruldu.append(durum))
|
||
|
||
task_id = manager.enqueue(_entry(SahteBolum()), output=str(tmp_path))
|
||
_bekle(qtbot, manager, task_id)
|
||
qtbot.waitUntil(lambda: DURUM_TAMAMLANDI in goruldu, timeout=5000)
|
||
|
||
assert goruldu[:3] == [DURUM_BEKLIYOR, DURUM_INDIRILIYOR, DURUM_TAMAMLANDI]
|
||
|
||
|
||
def test_indirme_bitince_gecmise_yaziliyor(qtbot, manager, ayarla, tmp_path):
|
||
"""Qt tarafı `set_gecmis`'i hiç çağırmıyordu; rozetler bu kayda bakıyor."""
|
||
from turkanime_api.cli.dosyalar import Dosyalar
|
||
|
||
ayarla(**{"aria2c kullan": False})
|
||
bolum = SahteBolum(slug="naruto-test-7-bolum")
|
||
|
||
task_id = manager.enqueue(_entry(bolum), output=str(tmp_path))
|
||
_bekle(qtbot, manager, task_id)
|
||
|
||
indirildi = Dosyalar().gecmis["indirildi"]
|
||
assert "naruto-test-7-bolum" in indirildi.get("naruto-test", [])
|
||
|
||
|
||
def test_basarisiz_indirme_gecmise_yazilmiyor(qtbot, manager, ayarla, tmp_path):
|
||
from turkanime_api.cli.dosyalar import Dosyalar
|
||
|
||
ayarla(**{"aria2c kullan": False})
|
||
bolum = SahteBolum(slug="naruto-test-9-bolum", video=None)
|
||
bolum.video = SahteVideo(bolum, hata=RuntimeError("olmadı"))
|
||
|
||
task_id = manager.enqueue(_entry(bolum), output=str(tmp_path))
|
||
_bekle(qtbot, manager, task_id)
|
||
|
||
indirildi = Dosyalar().gecmis["indirildi"]
|
||
assert "naruto-test-9-bolum" not in indirildi.get("naruto-test", [])
|
||
|
||
|
||
# ── Sayfanın satır tablosu (web: `uclar_indirme.IndirmeUclari`) ─────────────
|
||
def _uclar(manager):
|
||
import json
|
||
from turkanime_api.gui.web.kopru import Kopru
|
||
from turkanime_api.gui.web.uclar_indirme import IndirmeUclari
|
||
kopru = Kopru()
|
||
olaylar: list = []
|
||
kopru.olay.connect(lambda ad, js: olaylar.append((ad, json.loads(js))))
|
||
uclar = IndirmeUclari(kopru, manager, oynat=lambda e: None,
|
||
klasor_ac=lambda yol: True, indirme_dizini=lambda: "/tmp")
|
||
return uclar, olaylar
|
||
|
||
|
||
def _satir(uclar, tid):
|
||
return next(s for s in uclar.indirmeler()["satirlar"] if s["id"] == tid)
|
||
|
||
|
||
def test_satir_durumlari_ve_yeniden_denemede_temizlik(qtbot, manager):
|
||
"""Yeniden denenen satırda eski hata metni ve ilerleme kalmamalı."""
|
||
uclar, olaylar = _uclar(manager)
|
||
manager.added.emit("dl1", "Naruto 1")
|
||
manager.state.emit("dl1", DURUM_INDIRILIYOR)
|
||
manager.progress.emit("dl1", 30, "3 MB / 10 MB")
|
||
manager.state.emit("dl1", DURUM_HATA)
|
||
manager.finished.emit("dl1", False, "hata: bağlantı koptu")
|
||
satir = _satir(uclar, "dl1")
|
||
assert (satir["durum"], satir["mesaj"], satir["ok"]) == (DURUM_HATA, "hata: bağlantı koptu", False)
|
||
|
||
manager.state.emit("dl1", DURUM_BEKLIYOR)
|
||
satir = _satir(uclar, "dl1")
|
||
assert (satir["yuzde"], satir["mesaj"], satir["detay"]) == (0, "", "")
|
||
# Her durum değişikliği sayfaya hemen gidiyor.
|
||
assert [o for o, _v in olaylar].count("indirme_satir") >= 4
|
||
|
||
|
||
def test_ilerleme_toplu_ve_seyrek_gidiyor(qtbot, manager):
|
||
uclar, olaylar = _uclar(manager)
|
||
manager.added.emit("dl1", "Naruto 1")
|
||
for yuzde in range(0, 41, 2):
|
||
manager.progress.emit("dl1", yuzde, f"%{yuzde}")
|
||
assert _satir(uclar, "dl1")["yuzde"] == 40 # tablo hemen güncel
|
||
qtbot.waitUntil(lambda: any(o == "indirme_ilerleme" for o, _v in olaylar), timeout=2000)
|
||
ilerlemeler = [v for o, v in olaylar if o == "indirme_ilerleme"]
|
||
assert len(ilerlemeler) == 1, "21 kanca çağrısı tek toplu olaya inmeli"
|
||
assert ilerlemeler[0]["satirlar"] == [{"id": "dl1", "yuzde": 40, "detay": "%40"}]
|
||
|
||
|
||
def test_tamamlananlari_temizle(qtbot, manager):
|
||
uclar, olaylar = _uclar(manager)
|
||
manager.added.emit("dl1", "Naruto 1")
|
||
manager.added.emit("dl2", "Naruto 2")
|
||
manager.state.emit("dl1", DURUM_TAMAMLANDI)
|
||
manager.finished.emit("dl1", True, DURUM_TAMAMLANDI)
|
||
assert _satir(uclar, "dl1")["yuzde"] == 100
|
||
assert uclar.indirme_toplu("temizle") == {"adet": 1}
|
||
assert [s["id"] for s in uclar.indirmeler()["satirlar"]] == ["dl2"]
|
||
assert ("indirme_silindi", {"idler": ["dl1"]}) in olaylar
|
||
|
||
|
||
def test_toplu_iptal_yoneticiye_gidiyor(qtbot, manager, monkeypatch):
|
||
uclar, _ = _uclar(manager)
|
||
cagrildi = []
|
||
monkeypatch.setattr(manager, "cancel_all", lambda: cagrildi.append(True) or 2)
|
||
assert uclar.indirme_toplu("iptal") == {"adet": 2}
|
||
assert cagrildi == [True]
|
||
with pytest.raises(ValueError):
|
||
uclar.indirme_toplu("yok-boyle")
|
||
|
||
|
||
def test_satir_iptali_yoneticiye_bagli(qtbot, manager, monkeypatch, ayarla, tmp_path):
|
||
import turkanime_api.gui.qt.indirme as dl_mod
|
||
|
||
monkeypatch.setattr(dl_mod, "run_bg", lambda *a, **k: None)
|
||
uclar, _ = _uclar(manager)
|
||
task_id = manager.enqueue(_entry(SahteBolum()), output=str(tmp_path))
|
||
assert uclar.indirme_eylem(task_id, "iptal") is True
|
||
assert manager.durum(task_id) == DURUM_IPTAL
|
||
assert _satir(uclar, task_id)["durum"] == DURUM_IPTAL
|
||
with pytest.raises(ValueError):
|
||
uclar.indirme_eylem("yok", "iptal")
|
||
|
||
|
||
# ── Yok edilmiş pencere ──────────────────────────────────────────────────────
|
||
def test_yok_edilmis_yonetici_sinyal_yayinca_cokmuyor(qtbot, izole_ev, capfd):
|
||
"""Pencere kapandıktan sonra koşmaya devam eden indirme sessizce bırakılmalı.
|
||
|
||
Ölçüldü: `_run` ilk `state.emit`'inde
|
||
`RuntimeError: Internal C++ object (DownloadManager) already deleted`
|
||
fırlatıyor, `_Task` bunu yakalayıp konsola traceback basıyor ve iş
|
||
"indiriliyor" durumunda takılı kalıyordu.
|
||
"""
|
||
import shiboken6
|
||
from PySide6.QtWidgets import QWidget
|
||
|
||
pencere = QWidget()
|
||
mgr = DownloadManager(pencere)
|
||
mgr._basla = lambda job: None # işi elle tetikleyeceğiz
|
||
task_id = mgr.enqueue(_entry(SahteBolum(slug="naruto-test-5-bolum")),
|
||
output=str(izole_ev))
|
||
|
||
shiboken6.delete(pencere) # pencere gerçekten yıkıldı
|
||
capfd.readouterr() # önceki gürültüyü at
|
||
|
||
mgr._run(task_id) # istisna DIŞARI sızmamalı
|
||
|
||
cikti = capfd.readouterr()
|
||
assert "RuntimeError" not in (cikti.out + cikti.err)
|