"""Gestion des chaînes suivies."""
from __future__ import annotations
import psycopg
from ..outils.base import channels_in_db, seed_videos
from ..outils.site import CH, ORIGIN, login
def test_channel_management(app_client, clean_db):
login(app_client)
h = {"Origin": ORIGIN}
def post(path, data):
return app_client.post(path, data=data, headers=h, follow_redirects=False).headers["location"]
third = "UC" + "c" * 22
# Ajout : commentaires de 1er niveau par défaut, ou avec les réponses
assert post("/channels", {"channel": CH}) == "/channels?msg=created"
assert post("/channels", {"channel": f"https://www.youtube.com/channel/{third}/shorts",
"comment_scope": "all"}) == "/channels?msg=created"
fourth = "UC" + "e" * 22
assert post("/channels", {"channel": fourth, "comment_scope": "n'importe quoi"}) == "/channels?msg=created"
rows = channels_in_db(clean_db["owner"])
assert (rows[CH][:2], rows[third][:2], rows[fourth][:2]) == (("top", True), ("all", True), ("top", True))
post(f"/channels/{fourth}/delete", {})
# Erreurs de saisie
assert post("/channels", {"channel": CH}) == "/channels?msg=duplicate"
assert post("/channels", {"channel": "@machaine"}) == "/channels?msg=handle"
assert post("/channels", {"channel": "https://www.youtube.com/@machaine"}) == "/channels?msg=handle"
assert post("/channels", {"channel": "UC123"}) == "/channels?msg=invalid_channel"
# Changer les commentaires relevés ou mettre en pause ne relance pas la vérification
with psycopg.connect(clean_db["owner"], autocommit=True) as conn:
conn.execute("UPDATE shortstats.channel SET checked_at = now(), youtube_title = 'Nom vérifié'")
assert post(f"/channels/{CH}", {"comment_scope": "all", "active": "on"}) == "/channels?msg=updated"
assert post(f"/channels/{third}", {"comment_scope": "top"}) == "/channels?msg=updated" # case décochée
rows = channels_in_db(clean_db["owner"])
assert rows[CH][:3] == ("all", True, "Nom vérifié") and rows[CH][3] is not None
assert rows[third][:3] == ("top", False, "Nom vérifié") and rows[third][3] is not None
# Réactiver une chaîne relance sa vérification
post(f"/channels/{third}", {"comment_scope": "top", "active": "on"})
assert channels_in_db(clean_db["owner"])[third][3] is None
# Page : identifiants, état, réglage des commentaires ; plus de commentaire épinglé
page = app_client.get("/channels").text
assert third in page and "vérification en cours" in page
assert "épinglé" not in page and 'name="author"' not in page
assert '' in page
assert '' in page
assert f'href="https://www.youtube.com/channel/{third}/shorts"' in page
# Suppression : refusée si des vidéos existent, acceptée sinon
seed_videos(clean_db["owner"], n=2)
assert post(f"/channels/{CH}/delete", {}) == "/channels?msg=has_videos"
assert post(f"/channels/{third}/delete", {}) == "/channels?msg=deleted"
assert post("/channels/pas-un-id/delete", {}) == "/channels?msg=missing"
assert set(channels_in_db(clean_db["owner"])) == {CH}