from pathlib import Path

import pytest

from resume_agent.documents import DocumentError, read_document, read_document_bytes
from resume_agent.web import html_to_text


def test_reads_plain_text(tmp_path: Path) -> None:
    resume = tmp_path / "resume.md"
    resume.write_text("# Ada\nBuilt reliable systems.", encoding="utf-8")
    assert "Built reliable systems" in read_document(resume)


def test_rejects_unknown_document_type(tmp_path: Path) -> None:
    resume = tmp_path / "resume.exe"
    resume.write_text("not a resume", encoding="utf-8")
    with pytest.raises(DocumentError, match="Supported resume formats"):
        read_document(resume)


def test_reads_uploaded_text_bytes() -> None:
    assert read_document_bytes("resume.txt", b"Built reliable systems.") == (
        "Built reliable systems."
    )


def test_rejects_empty_uploaded_text() -> None:
    with pytest.raises(DocumentError, match="No readable text"):
        read_document_bytes("resume.md", b"")


def test_html_to_text_removes_scripts() -> None:
    text = html_to_text("<h1>Engineer</h1><script>malicious()</script><p>Build APIs</p>")
    assert "Engineer" in text
    assert "Build APIs" in text
    assert "malicious" not in text
