summaryrefslogtreecommitdiffstats
path: root/tests/test_frontmatter.py
blob: 2257a8aaa4d44bd7ec9b6d84d731ca970f5b1ee6 (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
import pytest
from pathlib import Path
import tempfile
from core.frontmatter import parse_frontmatter, write_frontmatter

SAMPLE_MD = """\
+++
title = "Test Article"
type = "Tech"
draft = false
tags = ["linux", "python"]
+++

# Body

Some content here.
"""

def test_parse_returns_frontmatter_dict(tmp_path):
    f = tmp_path / "index.md"
    f.write_text(SAMPLE_MD)
    fm, body = parse_frontmatter(f)
    assert fm["title"] == "Test Article"
    assert fm["type"] == "Tech"
    assert fm["draft"] is False
    assert "linux" in fm["tags"]

def test_parse_returns_body(tmp_path):
    f = tmp_path / "index.md"
    f.write_text(SAMPLE_MD)
    fm, body = parse_frontmatter(f)
    assert "# Body" in body
    assert "Some content here." in body

def test_write_preserves_format(tmp_path):
    f = tmp_path / "index.md"
    f.write_text(SAMPLE_MD)
    fm, body = parse_frontmatter(f)
    fm["title"] = "Updated Title"
    write_frontmatter(f, fm, body)
    fm2, _ = parse_frontmatter(f)
    assert fm2["title"] == "Updated Title"
    assert fm2["type"] == "Tech"

def test_parse_raises_on_missing_delimiters(tmp_path):
    f = tmp_path / "index.md"
    f.write_text("# No frontmatter\n\nJust body.")
    with pytest.raises(ValueError, match="frontmatter"):
        parse_frontmatter(f)