# tests/test_article_scanner.py
from pathlib import Path
from core.article_scanner import scan_articles

def _make_article(base: Path, lang: str, slug: str, fm_extra: str = "") -> Path:
    p = base / "content" / lang / "articles" / slug
    p.mkdir(parents=True)
    (p / "index.md").write_text(f"+++\ntitle = \"{slug}\"\ntype = \"Tech\"\n{fm_extra}+++\n\nBody.\n")
    return p / "index.md"

def test_scan_finds_articles(tmp_path):
    _make_article(tmp_path, "it", "primo-post")
    _make_article(tmp_path, "en", "first-post")
    articles = scan_articles(tmp_path)
    slugs = [a.slug for a in articles]
    assert "primo-post" in slugs
    assert "first-post" in slugs

def test_scan_detects_translation_pair(tmp_path):
    _make_article(tmp_path, "it", "shared-slug")
    _make_article(tmp_path, "en", "shared-slug")
    articles = scan_articles(tmp_path)
    it_art = next(a for a in articles if a.lang == "it" and a.slug == "shared-slug")
    en_art = next(a for a in articles if a.lang == "en" and a.slug == "shared-slug")
    assert it_art.has_translation is True
    assert it_art.translation_path == en_art.path
    assert en_art.has_translation is True

def test_scan_detects_missing_translation(tmp_path):
    _make_article(tmp_path, "it", "solo-italiano")
    articles = scan_articles(tmp_path)
    art = next(a for a in articles if a.slug == "solo-italiano")
    assert art.has_translation is False
    assert art.translation_path is None

def test_scan_tolerates_corrupt_frontmatter(tmp_path):
    p = tmp_path / "content" / "it" / "articles" / "broken"
    p.mkdir(parents=True)
    (p / "index.md").write_text("no frontmatter delimiters here\n")
    articles = scan_articles(tmp_path)
    art = next(a for a in articles if a.slug == "broken")
    assert art.frontmatter == {}

def test_scan_parses_frontmatter(tmp_path):
    _make_article(tmp_path, "it", "con-fm", 'tags = ["linux"]\n')
    articles = scan_articles(tmp_path)
    art = next(a for a in articles if a.slug == "con-fm")
    assert art.frontmatter["title"] == "con-fm"
    assert "linux" in art.frontmatter["tags"]
