import pytest
from youtube import watch_formats


class TestCodecName:
    def test_avc_returns_h264(self):
        assert watch_formats.codec_name('avc1.64001F') == 'h264'

    def test_av01_returns_av1(self):
        assert watch_formats.codec_name('av01.0.05M.08') == 'av1'

    def test_vp9_returns_vp(self):
        assert watch_formats.codec_name('vp9') == 'vp'

    def test_unknown_returns_unknown(self):
        assert watch_formats.codec_name('unknown_codec') == 'unknown'


class TestVideoQualityString:
    def test_with_vcodec(self):
        fmt = {'vcodec': 'avc1', 'width': 1920, 'height': 1080, 'fps': 30}
        assert watch_formats.video_quality_string(fmt) == '1920x1080 30fps'

    def test_with_vcodec_no_fps(self):
        fmt = {'vcodec': 'avc1', 'width': 1280, 'height': 720}
        assert watch_formats.video_quality_string(fmt) == '1280x720'

    def test_with_acodec_only(self):
        fmt = {'acodec': 'mp4a.40.2'}
        assert watch_formats.video_quality_string(fmt) == 'audio only'

    def test_empty(self):
        fmt = {}
        assert watch_formats.video_quality_string(fmt) == '?'


class TestShortVideoQualityString:
    def test_with_fps(self):
        fmt = {'quality': 1080, 'fps': 60, 'vcodec': 'av01.0.05M.08'}
        assert watch_formats.short_video_quality_string(fmt) == '1080p60 AV1'

    def test_h264(self):
        fmt = {'quality': 720, 'fps': 30, 'vcodec': 'avc1.64001E'}
        assert watch_formats.short_video_quality_string(fmt) == '720p30 h264'


class TestAudioQualityString:
    def test_with_bitrate(self):
        fmt = {'acodec': 'mp4a.40.2', 'audio_bitrate': 128}
        assert watch_formats.audio_quality_string(fmt) == '128k'

    def test_with_sample_rate(self):
        fmt = {'acodec': 'mp4a.40.2', 'audio_bitrate': 128, 'audio_sample_rate': 44100}
        assert watch_formats.audio_quality_string(fmt) == '128k 44.1kHz'

    def test_video_only(self):
        fmt = {'vcodec': 'avc1'}
        assert watch_formats.audio_quality_string(fmt) == 'video only'


class TestFormatBytes:
    def test_none(self):
        assert watch_formats.format_bytes(None) == 'N/A'

    def test_bytes(self):
        assert watch_formats.format_bytes(512) == '512.00B'

    def test_kibibytes(self):
        assert watch_formats.format_bytes(1024) == '1.00KiB'

    def test_mebibytes(self):
        assert watch_formats.format_bytes(1048576) == '1.00MiB'