summaryrefslogtreecommitdiffstats
path: root/tests/test_article_scanner.py
blob: db8b683054dee3e3611b08cfc5358791e02091c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# 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"]