from govcrawler.storage.attachments import parse_disposition_filename, safe_filename


def test_plain_filename():
    assert parse_disposition_filename('attachment; filename="notice.pdf"') == "notice.pdf"


def test_rfc5987_utf8():
    h = "attachment; filename*=UTF-8''%E5%85%AC%E5%91%8A.pdf"
    assert parse_disposition_filename(h) == "公告.pdf"


def test_regular_filename_url_encoded_utf8():
    h = 'attachment; filename="%E5%85%AC%E5%91%8A%E5%85%A8%E6%96%87.pdf"'
    assert parse_disposition_filename(h) == "公告全文.pdf"


def test_missing_header_returns_none():
    assert parse_disposition_filename(None) is None
    assert parse_disposition_filename("") is None


def test_safe_filename_strips_illegal():
    assert "/" not in safe_filename("a/b.pdf")
    assert "\\" not in safe_filename("a\\b.pdf")


def test_safe_filename_keeps_chinese():
    out = safe_filename("公告全文.pdf")
    assert "公告" in out
