Private
Public Access
0
0
Files
manual_slop/tests/test_twitter_threads_render.py
T
ed 2c680e23e4 feat(twitter_threads): gallery-dl media download + inline media embeds in Markdown
download_media now shells out to gallery-dl (handles X.com auth/403/video->mp4) instead of urllib, returns the downloaded files, + media_files_for_post glob helper + --cookies CLI. render_markdown embeds images with ![](...) and videos with an HTML <video controls> tag (were plain links). extract_corpus uses the package download_media (one download path). Tests rewritten for the gallery-dl API. Corpus: 4 threads, 33 media (32 img embeds + 1 video), 0 missing.
2026-07-05 19:08:43 -04:00

107 lines
4.1 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 '<video controls src="./media/123_vid1.mp4"></video>' 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