from pathlib import Path

from pdf_to_md_local.dependencies import find_tesseract, parse_tesseract_languages, workspace_tessdata_prefix


def test_parse_tesseract_languages_detects_vietnamese():
    output = 'List of available languages in "tessdata/" (3):\neng\nosd\nvie\n'
    assert parse_tesseract_languages(output) == {"eng", "osd", "vie"}


def test_find_tesseract_uses_common_windows_install_path(monkeypatch, tmp_path):
    tesseract = tmp_path / "Tesseract-OCR" / "tesseract.exe"
    tesseract.parent.mkdir()
    tesseract.write_text("", encoding="utf-8")

    monkeypatch.setattr("pdf_to_md_local.dependencies.shutil.which", lambda name: None)
    monkeypatch.setattr("pdf_to_md_local.dependencies.COMMON_WINDOWS_TESSERACT_PATHS", (tesseract,))

    assert find_tesseract() == str(tesseract)


def test_workspace_tessdata_prefix_is_returned_when_vietnamese_data_exists(tmp_path, monkeypatch):
    tessdata = tmp_path / "tools" / "tessdata"
    tessdata.mkdir(parents=True)
    (tessdata / "vie.traineddata").write_text("", encoding="utf-8")
    monkeypatch.setattr("pdf_to_md_local.dependencies.project_root", lambda: tmp_path)

    assert workspace_tessdata_prefix() == str(tessdata)
