"""
Tests for yt-dlp_alexwlchan.
"""

import importlib
import os

from pymediainfo import MediaInfo
import pytest

yt_dlp_alexwlchan = importlib.import_module("yt-dlp_alexwlchan")

download_video = yt_dlp_alexwlchan.download_video
normalise_url = yt_dlp_alexwlchan.normalise_url


def test_youtube_video() -> None:
    """
    Download a YouTube video and check we get the expected output.
    """
    video_info = download_video("https://www.youtube.com/watch?v=TUQaGhPdlxs")

    assert (
        video_info["title"]
        == '"new york city, manhattan, people" - Free Public Domain Video'
    )
    assert os.path.exists(video_info["video_path"])
    assert os.path.exists(video_info["thumbnail_path"])
    assert video_info["subtitle_path"] is None
    assert os.path.exists(video_info["uploader"]["avatar_path"])

    assert video_info["id"] == "TUQaGhPdlxs"
    assert video_info["date_uploaded"] == "2022-03-25T01:10:38Z"

    assert video_info["video_path"].name.endswith(" [TUQaGhPdlxs].mp4")


def test_youtube_path_is_cleaned_up() -> None:
    """
    Paths of YouTube videos get cleaned up during the download.
    """
    video = download_video("https://www.youtube.com/shorts/eso8JB7q0a0")
    assert (
        video["title"] == "3D Printing Everyday for 365 Days 176/365  "
        "#stem #3dprinting #3dprint #ideas #useful"
    )
    assert (
        os.path.basename(video["video_path"]) == "3D Printing Everyday for 365 Days "
        "176-365 stem 3dprinting 3dprint ideas useful [eso8JB7q0a0].mp4"
    )


def test_youtube_video_does_not_http429() -> None:
    """
    Download a YouTube video without any subtitles or spoken audio.
    """
    video = download_video("https://www.youtube.com/watch?v=0N1_0SUGlDQ")
    assert video["subtitle_path"] is None


def test_youtube_video_with_automatic_subtitles() -> None:
    """
    Download a YouTube video with automatic subtitles.
    """
    video = download_video("https://www.youtube.com/shorts/hyGluE562oA")
    assert video["subtitle_path"] is not None


def test_youtube_ignores_ai_upscaling() -> None:
    """
    Downloaded a YouTube video ignores AI upscaled versions.
    """
    # This is a video uploaded in 2009, and the highest resolution is
    # 640x480, but as of October 2025, YouTube have started offering
    # AI upscaled versions.
    video = download_video("https://www.youtube.com/watch?v=0N1_0SUGlDQ")

    media_info = MediaInfo.parse(video["video_path"])
    video_track = next(tr for tr in media_info.tracks if tr.track_type == "Video")
    assert (video_track.width, video_track.height) == (640, 480)


def test_instagram_video() -> None:
    """
    Download an Instagram video and check we get the expected output.
    """
    video_info = download_video("https://www.instagram.com/reel/DMWY8KkOS0n/")

    assert os.path.exists(video_info["video_path"])
    assert os.path.exists(video_info["thumbnail_path"])
    assert video_info["subtitle_path"] is None

    assert video_info["uploader"]["id"] == "52716733233"
    assert video_info["uploader"]["name"] == "Public Domain Gems"
    assert (
        video_info["uploader"]["url"] == "https://www.instagram.com/publicdomaingems/"
    )
    assert os.path.exists(video_info["uploader"]["avatar_path"])

    assert video_info["id"] == "DMWY8KkOS0n"
    assert video_info["date_uploaded"] == "2025-07-21T00:34:41Z"

    assert video_info["video_path"].name.endswith(" [DMWY8KkOS0n].mp4")


@pytest.mark.parametrize(
    "url, expected",
    [
        (
            "https://www.instagram.com/reel/DMWY8KkOS0n/",
            "https://www.instagram.com/reel/DMWY8KkOS0n/",
        ),
        (
            "https://www.youtube.com/shorts/hyGluE562oA",
            "https://www.youtube.com/shorts/hyGluE562oA",
        ),
        (
            "https://www.youtube.com/watch?v=0N1_0SUGlDQ",
            "https://www.youtube.com/watch?v=0N1_0SUGlDQ",
        ),
        (
            "https://www.youtube.com/watch?v=0N1_0SUGlDQ&app=desktop&list=LL&index=43",
            "https://www.youtube.com/watch?v=0N1_0SUGlDQ",
        ),
    ],
)
def test_normalise_url(url: str, expected: str) -> None:
    """
    Tests for `normalise_url`.
    """
    assert normalise_url(url) == expected
