import pytest
from datetime import datetime, timedelta
from src.utils import parse_relative_expression, parse_date_string, sanitize_filename

def test_parse_relative_expression_days():
    # Test T, T+n, T-n
    result = parse_relative_expression("T")
    assert result is not None
    
    result_plus = parse_relative_expression("T+5")
    assert result_plus is not None
    
    result_minus = parse_relative_expression("T-3")
    assert result_minus is not None

def test_parse_relative_expression_invalid():
    assert parse_relative_expression("abc") is None
    assert parse_relative_expression("123") is None
    assert parse_relative_expression("T++1") is None

def test_parse_date_string_formats():
    # Test various ISO and common formats
    dt = parse_date_string("2024-11-08")
    assert dt.year == 2024
    assert dt.month == 11
    assert dt.day == 8
    
    dt = parse_date_string("08-11-2024")
    assert dt.year == 2024
    assert dt.month == 11
    assert dt.day == 8

def test_sanitize_filename_basic():
    assert sanitize_filename("test.txt") == "test.txt"
    assert sanitize_filename("test space.txt") == "test space.txt"
    assert sanitize_filename("test/path.txt") == "path.txt"

def test_sanitize_filename_unsafe_chars():
    # Test removing unsafe characters
    assert sanitize_filename("test$(id).txt") == "test.txt"
    assert sanitize_filename("test${env}.txt") == "test.txt"
    assert ".." not in sanitize_filename("test..txt")
    assert sanitize_filename("test;rm -rf.txt") == "testrm -rf.txt"

def test_sanitize_filename_empty_or_invalid():
    with pytest.raises(ValueError):
        sanitize_filename("")
    
    with pytest.raises(ValueError):
        sanitize_filename("../../../")
