Private
Public Access
0
0
Files
manual_slop/tests/test_twitter_threads_render.py
T
ed ef98f6d1a1 feat(twitter_threads): Result[T] + render_markdown.py (YAML front-matter + per-post sections + media links)
Added standalone Result[T] (frozen+slots, ok/err classmethods, is_ok) to error_types.py per canonical AND-over-OR error_handling.md (not the video_analysis _Ok|_Err sum type). render_markdown renders YAML front-matter from root post, per-post ## sections with reply markers, quote blockquotes, and ./media/<name> links; media_names mapping keeps render decoupled from download_media. TDD: red (no Result) -> green (7 render + 9 types passed).
2026-07-05 17:17:18 -04:00

107 lines
4.0 KiB
Python

"""Tests for scripts.twitter_threads.render_markdown and Result contract."""
from __future__ import annotations
from pathlib import Path
import pytest
from scripts.twitter_threads.error_types import ErrorInfo, make_error, PostMetrics, PostData, Result, ThreadData
from scripts.twitter_threads.render_markdown import render_markdown
def _post(post_id: str, text: str, reply_to_id: str | None = None, quote_of_id: str | None = None, view_count: int = 0, timestamp: str = "2024-02-01T00:00:00Z", handle: str = "NOTimothyLottes", author: str = "Tim") -> PostData:
metrics = PostMetrics(reply_count=0, repost_count=0, like_count=0, view_count=view_count)
return PostData(post_id=post_id, author=author, handle=handle, text=text, timestamp=timestamp, media_urls=(), reply_to_id=reply_to_id, quote_of_id=quote_of_id, metrics=metrics)
def test_result_ok_and_err() -> None:
r = Result.ok("x")
assert r.is_ok is True
assert r.data == "x"
e = make_error("K", "s", "d")
r2 = Result.err(e)
assert r2.is_ok is False
assert r2.error is e
def test_render_single_post(tmp_path: Path) -> None:
root = _post("123", "Hello world", view_count=5)
thread = ThreadData(root_post_id="123", posts=(root,), source_url="https://x.com/x/x/123")
out = tmp_path / "thread.md"
result = render_markdown(thread, {}, out)
assert result.is_ok
assert result.data == out
assert out.exists()
text = out.read_text(encoding="utf-8")
assert text.startswith("---")
assert "@NOTimothyLottes" in text
assert "post_id:" in text
assert "post_count: 1" in text
assert "view_count: 5" in text
assert "Hello world" in text
def test_render_thread(tmp_path: Path) -> None:
p1 = _post("1", "first post")
p2 = _post("2", "second post", reply_to_id="1")
p3 = _post("3", "third post", reply_to_id="2")
thread = ThreadData(root_post_id="1", posts=(p1, p2, p3), source_url="https://x.com/x/x/1")
out = tmp_path / "thread.md"
result = render_markdown(thread, {}, out)
assert result.is_ok
text = out.read_text(encoding="utf-8")
i1 = text.index("## Post 1")
i2 = text.index("## Post 2")
i3 = text.index("## Post 3")
assert i1 < i2 < i3
assert "reply to Post 1" in text
assert "reply to Post 2" in text
def test_render_quote_tweet(tmp_path: Path) -> None:
root = _post("100", "this is a quote", quote_of_id="999")
thread = ThreadData(root_post_id="100", posts=(root,), source_url="https://x.com/x/x/100")
out = tmp_path / "thread.md"
result = render_markdown(thread, {}, out)
assert result.is_ok
text = out.read_text(encoding="utf-8")
quote_lines = [line for line in text.splitlines() if line.startswith("> ")]
assert any("Quoting" in line and "999" in line for line in quote_lines)
def test_render_media_links(tmp_path: Path) -> None:
root = _post("123", "media post")
thread = ThreadData(root_post_id="123", posts=(root,), source_url="https://x.com/x/x/123")
out = tmp_path / "thread.md"
media_names = {"123": ["123_img1.jpg", "123_img2.jpg", "123_vid1.mp4"]}
result = render_markdown(thread, media_names, out)
assert result.is_ok
text = out.read_text(encoding="utf-8")
assert "[Media 1](./media/123_img1.jpg)" in text
assert "[Media 2](./media/123_img2.jpg)" in text
assert "[Media 3](./media/123_vid1.mp4)" in text
def test_render_view_count_null(tmp_path: Path) -> None:
root = _post("123", "hello", view_count=None)
thread = ThreadData(root_post_id="123", posts=(root,), source_url="https://x.com/x/x/123")
out = tmp_path / "thread.md"
result = render_markdown(thread, {}, out)
assert result.is_ok
text = out.read_text(encoding="utf-8")
assert "view_count: null" in text
def test_render_title_truncated(tmp_path: Path) -> None:
text100 = "A" * 100
root = _post("123", text100)
thread = ThreadData(root_post_id="123", posts=(root,), source_url="https://x.com/x/x/123")
out = tmp_path / "thread.md"
result = render_markdown(thread, {}, out)
assert result.is_ok
text = out.read_text(encoding="utf-8")
h1_lines = [line for line in text.splitlines() if line.startswith("# @")]
assert h1_lines, "expected an H1 line starting with '# @'"
h1 = h1_lines[0]
assert "A" * 80 in h1
assert "A" * 81 not in h1