from config import Config
from datetime import datetime, timedelta
from helpers import *
from models import Tracks


def test_fade_point():
    test_track_path = "testdata/isa.mp3"
    test_track_data = "testdata/isa.py"

    audio_segment = get_audio_segment(test_track_path)
    assert audio_segment

    fade_at = fade_point(audio_segment)

    # Get test data
    with open(test_track_data) as f:
        testdata = eval(f.read())

    # Volume detection can vary, so ± 1 second is OK
    assert fade_at < testdata['fade_at'] + 1000
    assert fade_at > testdata['fade_at'] - 1000


def test_get_tags():
    test_track_path = "testdata/mom.mp3"
    test_track_data = "testdata/mom.py"

    tags = get_tags(test_track_path)

    # Get test data
    with open(test_track_data) as f:
        testdata = eval(f.read())

    assert tags['artist'] == testdata['artist']
    assert tags['title'] == testdata['title']


def test_get_relative_date():
    assert get_relative_date(None) == "Never"
    today_at_10 = datetime.now().replace(hour=10, minute=0)
    today_at_11 = datetime.now().replace(hour=11, minute=0)
    assert get_relative_date(today_at_10, today_at_11) == "10:00"
    eight_days_ago = today_at_10 - timedelta(days=8)
    assert get_relative_date(eight_days_ago, today_at_11) == "1 week, 1 day ago"
    sixteen_days_ago = today_at_10 - timedelta(days=16)
    assert get_relative_date(
        sixteen_days_ago, today_at_11) == "2 weeks, 2 days ago"


def test_leading_silence():
    test_track_path = "testdata/isa.mp3"
    test_track_data = "testdata/isa.py"

    audio_segment = get_audio_segment(test_track_path)
    assert audio_segment

    silence_at = leading_silence(audio_segment)

    # Get test data
    with open(test_track_data) as f:
        testdata = eval(f.read())

    # Volume detection can vary, so ± 1 second is OK
    assert silence_at < testdata['leading_silence'] + 1000
    assert silence_at > testdata['leading_silence'] - 1000


def test_ms_to_mmss():
    assert ms_to_mmss(None) == "-"
    assert ms_to_mmss(59600) == "0:59"
    assert ms_to_mmss((5 * 60 * 1000) + 23000) == "5:23"
