101 lines
2.6 KiB
Python
101 lines
2.6 KiB
Python
# tests/test_extractor.py
|
|
import pytest
|
|
from voice_gen.extractor import extract_text
|
|
|
|
|
|
def write_md(tmp_path, body: str):
|
|
p = tmp_path / "script.md"
|
|
p.write_text(body, encoding="utf-8")
|
|
return p
|
|
|
|
|
|
def test_plain_paragraph(tmp_path):
|
|
p = write_md(tmp_path, "Hello world.")
|
|
assert extract_text(p) == "Hello world."
|
|
|
|
|
|
def test_strips_yaml_frontmatter(tmp_path):
|
|
body = (
|
|
"---\n"
|
|
"title: hello\n"
|
|
"author: me\n"
|
|
"---\n"
|
|
"\n"
|
|
"Actual narration starts here.\n"
|
|
)
|
|
p = write_md(tmp_path, body)
|
|
assert extract_text(p) == "Actual narration starts here."
|
|
|
|
|
|
def test_strips_atx_headings(tmp_path):
|
|
body = "# Title\n\nFirst paragraph.\n\n## Sub\n\nSecond paragraph.\n"
|
|
p = write_md(tmp_path, body)
|
|
out = extract_text(p)
|
|
assert "Title" not in out
|
|
assert "Sub" not in out
|
|
assert "First paragraph." in out
|
|
assert "Second paragraph." in out
|
|
|
|
|
|
def test_strips_bold_italic_inline(tmp_path):
|
|
body = "This is **bold** and *italic* text."
|
|
p = write_md(tmp_path, body)
|
|
assert extract_text(p) == "This is bold and italic text."
|
|
|
|
|
|
def test_strips_unordered_list_markers(tmp_path):
|
|
body = "- First item\n- Second item\n- Third item\n"
|
|
p = write_md(tmp_path, body)
|
|
out = extract_text(p)
|
|
assert "First item" in out
|
|
assert "Second item" in out
|
|
assert "Third item" in out
|
|
assert "-" not in out.split("\n")[0]
|
|
|
|
|
|
def test_drops_fenced_code_blocks(tmp_path):
|
|
body = (
|
|
"Intro line.\n"
|
|
"\n"
|
|
"```python\n"
|
|
"print('do not read')\n"
|
|
"```\n"
|
|
"\n"
|
|
"Outro line.\n"
|
|
)
|
|
p = write_md(tmp_path, body)
|
|
out = extract_text(p)
|
|
assert "print" not in out
|
|
assert "do not read" not in out
|
|
assert "Intro line." in out
|
|
assert "Outro line." in out
|
|
|
|
|
|
def test_collapses_blank_lines(tmp_path):
|
|
body = "Para A.\n\n\n\nPara B.\n"
|
|
p = write_md(tmp_path, body)
|
|
out = extract_text(p)
|
|
assert out == "Para A.\nPara B."
|
|
|
|
|
|
def test_missing_file_raises(tmp_path):
|
|
with pytest.raises(FileNotFoundError):
|
|
extract_text(tmp_path / "nope.md")
|
|
|
|
|
|
def test_reads_utf16_le_with_bom(tmp_path):
|
|
"""PowerShell on Windows writes `>` redirection as UTF-16 LE WITH BOM.
|
|
|
|
The extractor must accept that, not just UTF-8.
|
|
"""
|
|
p = tmp_path / "script.md"
|
|
p.write_bytes(b"\xff\xfe" + "Hello world.\n".encode("utf-16-le"))
|
|
assert extract_text(p) == "Hello world."
|
|
|
|
|
|
def test_reads_utf8_bom_file(tmp_path):
|
|
"""Some Windows editors (Notepad, older VS Code) save UTF-8 with a BOM."""
|
|
p = tmp_path / "script.md"
|
|
p.write_bytes(b"\xef\xbb\xbfHello world.")
|
|
assert extract_text(p) == "Hello world."
|