Files
StarPunk/tests/test_reply_context_fetch.py
T
PhilandClaude Opus 5.5 d73dd0a5b0 fix: Fetch reply contexts again (v2.10.2)
fetch_source returns (content, content_type, final_url) since v2.7.0, but
fetch_reply_context unpacked two values, so every new reply/like/repost
context was stored as an error. Parse with the final URL so relative links
resolve. Adds a test that runs the real fetch path.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-03 12:42:19 -07:00

51 lines
2.0 KiB
Python

"""
Reply contexts are fetched for real (v2.10.2)
v2.7.0 made fetch_source return (content, content_type, final_url) but
fetch_reply_context still unpacked two values, so every new reply, like and
repost stored fetch_status='error'. Other tests stub fetch_reply_context out;
this one runs it through the mocked network.
"""
import sqlite3
import httpx
from starpunk.reply_context import fetch_reply_context
PUBLIC = [(2, 1, 6, "", ("93.184.216.34", 0))]
PAGE = """<html><head><title>Their site</title></head><body>
<article class="h-entry">
<h1 class="p-name">A post worth replying to</h1>
<a class="p-author h-card" href="/about">Ada Example</a>
<time class="dt-published" datetime="2026-11-01T10:00:00Z">Nov 1</time>
<div class="e-content"><p>Some thoughts.</p></div>
</article></body></html>"""
def test_reply_context_is_fetched_and_stored(app, monkeypatch):
def site(request):
if request.url.path == "/old":
return httpx.Response(301, headers={"Location": "https://their.example/posts/1"})
return httpx.Response(200, html=PAGE)
monkeypatch.setattr("starpunk.webmention_ssrf.socket.getaddrinfo", lambda *a, **k: PUBLIC)
monkeypatch.setattr("starpunk.webmention_ssrf.TRANSPORT", httpx.MockTransport(site))
with app.app_context():
context = fetch_reply_context(app.config["DATABASE_PATH"], "https://their.example/old",
app.config["SITE_URL"])
assert context["fetch_status"] == "fetched", context
assert context["title"] == "A post worth replying to"
assert context["author_name"] == "Ada Example"
# Relative links resolve against the URL after the redirect
assert context["author_url"] == "https://their.example/about"
assert "Some thoughts" in (context["content_snippet"] or "")
row = sqlite3.connect(app.config["DATABASE_PATH"]).execute(
"SELECT fetch_status FROM reply_contexts WHERE url = ?", ("https://their.example/old",)
).fetchone()
assert row == ("fetched",)