""" Tests for backfeed of responses to crossposts (v2.13.0, ADR-073) Silo clients are tested against fake APIs (httpx.MockTransport). The worker, storage and pages are tested with a fake client that returns whatever a test puts in it. """ import io import json from urllib.parse import parse_qs import httpx import pytest from PIL import Image from starpunk import avatars, backfeed, create_app, syndication from starpunk.backfeed import Harvest, Response, facets_html, interval_for, text_html from starpunk.database import get_db from starpunk.notes import create_note from starpunk.syndication import BlueskyClient, MastodonClient, SyndicationError, ThreadsClient SITE = "https://blog.example.com" GTS = "https://social.example" COPY = f"{GTS}/@phil/statuses/01COPY" # --- Fakes -------------------------------------------------------------------- class FakeClient: """Stands in for a silo client: returns the harvest a test sets""" config_token = "token" def __init__(self): self.harvest = Harvest([]) self.error = None self.calls = [] def responses(self, remote_id, remote_url, cap=1000): self.calls.append(remote_id) if self.error: raise self.error return self.harvest def maintain(self): pass def like(n, bio="", **kw): return Response(kind="like", source_url=f"{COPY}#liked-by-{n}", remote_id=str(n), author_id=str(n), author_name=f"Liker {n}", author_url=f"{GTS}/@l{n}", author_bio=bio, **kw) def reply(n, text="Nice!"): return Response(kind="reply", source_url=f"{GTS}/@r{n}/statuses/{n}", remote_id=str(n), author_id=f"r{n}", author_name=f"Replier {n}", author_url=f"{GTS}/@r{n}", content_html=f"

{text}

", published="2026-11-10T12:00:00Z") def resolve(host, *args, **kwargs): return [(2, 1, 6, "", ("93.184.216.34", 0))] def png(color="red", size=(200, 120)): buf = io.BytesIO() Image.new("RGB", size, color).save(buf, "PNG") return buf.getvalue() class FakeWeb: """Avatar images by URL (requests are pinned to an IP; Host names the site)""" def __init__(self): self.files = {} def __call__(self, request): url = f"{request.url.scheme}://{request.headers['host']}{request.url.raw_path.decode()}" if url in self.files: return httpx.Response(200, headers={"content-type": "image/png"}, content=self.files[url]) return httpx.Response(404) @pytest.fixture def web(monkeypatch): fake = FakeWeb() monkeypatch.setattr("starpunk.webmention_ssrf.TRANSPORT", httpx.MockTransport(fake)) monkeypatch.setattr("starpunk.webmention_ssrf.socket.getaddrinfo", resolve) return fake @pytest.fixture def client(): return FakeClient() @pytest.fixture def app(tmp_path, client, web): data = tmp_path / "data" app = create_app({ "TESTING": True, "SITE_URL": SITE, "ADMIN_ME": "https://example.com", "SESSION_SECRET": "test-secret", "DATA_PATH": data, "NOTES_PATH": data / "notes", "DATABASE_PATH": data / "starpunk.db", "GOTOSOCIAL_URL": GTS, "GOTOSOCIAL_ACCESS_TOKEN": "token", "BACKFEED_ENABLED": True, }) syndication._client_cache[GTS] = client backfeed.denied.clear() yield app syndication._client_cache.clear() backfeed.denied.clear() def crossposted(app, age_days=0, content="Hello"): """A published note with a synced GoToSocial copy; returns (note, syndication id)""" with app.app_context(): note = create_note(content=content, published=True) db = get_db(app) if age_days: db.execute("UPDATE notes SET created_at = datetime('now', ?) WHERE id = ?", (f"-{age_days} days", note.id)) cursor = db.execute( "INSERT INTO syndications (note_id, target, enabled, remote_id, remote_url, state) " "VALUES (?, 'gotosocial', 1, '01COPY', ?, 'synced')", (note.id, COPY)) db.commit() return note, cursor.lastrowid def rows(app, note_id): with app.app_context(): return [dict(r) for r in get_db(app).execute( "SELECT * FROM webmentions WHERE target_note_id = ? ORDER BY source_url", (note_id,))] def run(app): with app.app_context(): return backfeed.process_due() # --- Schedule and text ------------------------------------------------------------ def test_schedule_slows_with_age_and_stops_at_cutoff(): day = 24 * 3600 assert interval_for(3600, 90) == 15 * 60 assert interval_for(2 * day, 90) == 3600 assert interval_for(10 * day, 90) == day assert interval_for(60 * day, 90) == 7 * day assert interval_for(91 * day, 90) is None def test_text_and_facets_become_safe_html(): assert text_html("hi \nsee https://a.example/x") == \ '

hi <b>
see https://a.example/x

' text = "café: example.com/long…" start = len("café: ".encode()) facets = [{"index": {"byteStart": start, "byteEnd": len(text.encode())}, "features": [{"$type": "app.bsky.richtext.facet#link", "uri": "https://example.com/long/path"}]}] assert facets_html(text, facets) == \ '

café: example.com/long…

' assert facets_html("", []) == "" # --- Worker and storage ------------------------------------------------------------- def test_check_stores_responses_as_webmentions(app, client): note, sid = crossposted(app) client.harvest = Harvest([like(1), reply(2, "Great post")]) assert run(app) == 1 stored = rows(app, note.id) assert [r["mention_type"] for r in stored] == ["like", "reply"] assert all(r["origin"] == "gotosocial" and r["validation_status"] == "valid" and r["moderation_status"] == "approved" for r in stored) assert stored[1]["content_snippet"] == "

Great post

" assert stored[0]["target_url"] == f"{SITE}/note/{note.slug}" with app.app_context(): s = get_db(app).execute("SELECT * FROM syndications WHERE id = ?", (sid,)).fetchone() assert s["backfeed_checked_at"] and s["backfeed_next_at"] and s["backfeed_error"] is None assert run(app) == 0, "not due again for 15 minutes" def test_repeat_check_updates_rows_and_keeps_moderation(app, client): note, sid = crossposted(app) client.harvest = Harvest([like(1), like(2)]) run(app) with app.app_context(): db = get_db(app) db.execute("UPDATE webmentions SET moderation_status = 'hidden' WHERE source_url LIKE '%-1'") db.execute("UPDATE syndications SET backfeed_next_at = datetime('now')") db.commit() client.harvest = Harvest([like(1), like(2)]) run(app) stored = rows(app, note.id) assert len(stored) == 2 assert [r["moderation_status"] for r in stored] == ["hidden", "approved"] def test_withdrawn_responses_drop_off_and_come_back(app, client): note, _ = crossposted(app) client.harvest = Harvest([like(1), reply(2)]) run(app) def recheck(harvest): with app.app_context(): get_db(app).execute("UPDATE syndications SET backfeed_next_at = datetime('now')") get_db(app).commit() client.harvest = harvest run(app) return {r["source_url"]: (r["validation_status"], r["validation_error"]) for r in rows(app, note.id)} # An incomplete list (cap reached, say) removes nothing assert all(v == ("valid", None) for v in recheck(Harvest([like(1)], complete=False)).values()) state = recheck(Harvest([like(1)])) assert state[reply(2).source_url] == ("invalid", "No longer on GoToSocial") assert recheck(Harvest([like(1), reply(2)]))[reply(2).source_url] == ("valid", None) def test_people_who_opt_out_are_skipped_and_removed(app, client): note, _ = crossposted(app) client.harvest = Harvest([like(1), like(2)]) run(app) with app.app_context(): get_db(app).execute("UPDATE syndications SET backfeed_next_at = datetime('now')") get_db(app).commit() client.harvest = Harvest([like(1, bio="

Please #NoBridge thanks

"), like(2), like(3, opted_out=True)]) run(app) assert [r["source_url"] for r in rows(app, note.id)] == [like(2).source_url] def test_admin_delete_is_a_tombstone_the_next_check_keeps(app, client): from starpunk.webmention_queries import count_webmentions, delete_webmentions, list_webmentions note, _ = crossposted(app) client.harvest = Harvest([like(1)]) run(app) with app.test_request_context(): wm_id = rows(app, note.id)[0]["id"] assert delete_webmentions([wm_id]) == 1 get_db(app).execute("UPDATE syndications SET backfeed_next_at = datetime('now')") get_db(app).commit() run(app) assert rows(app, note.id)[0]["moderation_status"] == "deleted" with app.test_request_context(): assert list_webmentions()[0] == [] and count_webmentions() == 0 assert len(list_webmentions(status_filter="deleted")[0]) == 1 def test_old_notes_wait_for_a_backfill(app, client): old, sid = crossposted(app, age_days=200) client.harvest = Harvest([reply(1)]) assert run(app) == 0 with app.app_context(): assert backfeed.start_backfill() == 1 assert backfeed.status()["remaining"] == 1 assert run(app) == 1 assert len(rows(app, old.id)) == 1 with app.app_context(): status = backfeed.status() s = get_db(app).execute("SELECT * FROM syndications WHERE id = ?", (sid,)).fetchone() assert status["remaining"] == 0 and status["finished"] assert s["backfeed_backfill"] == 0 and s["backfeed_next_at"] is None, "past the cutoff: no more checks" def test_backfill_is_paced(app, client): crossposted(app, age_days=200) crossposted(app, age_days=300) with app.app_context(): backfeed.start_backfill() assert run(app) == 1 assert run(app) == 0, "the next backfill check waits a few seconds" with app.app_context(): get_db(app).execute("UPDATE site_state SET value = datetime('now', '-10 seconds') " "WHERE key = 'backfeed_backfill_last'") get_db(app).commit() assert run(app) == 1 def test_routine_checks_skip_likes_reposts_and_unpublished(app, client): with app.app_context(): liked = create_note(content="", published=True, like_of="https://other.example/post") draft = create_note(content="Draft", published=False) db = get_db(app) for note in (liked, draft): db.execute("INSERT INTO syndications (note_id, target, enabled, remote_id, remote_url, state) " "VALUES (?, 'gotosocial', 1, 'x', 'https://s.example/x', 'synced')", (note.id,)) db.commit() assert run(app) == 0 and client.calls == [] def test_refused_credential_stops_that_silo(app, client): crossposted(app) error = SyndicationError("GET /api/v1/statuses/01COPY/favourited_by: HTTP 403 forbidden", permanent=True) error.status = 403 client.error = error run(app) assert "gotosocial" in backfeed.denied and "read:statuses" in backfeed.denied["gotosocial"] client.error = None assert run(app) == 0, "not retried until restart" def test_failures_back_off(app, client): _, sid = crossposted(app) client.error = SyndicationError("HTTP 502 bad gateway") run(app) with app.app_context(): s = get_db(app).execute("SELECT * FROM syndications WHERE id = ?", (sid,)).fetchone() assert s["backfeed_attempts"] == 1 and "502" in s["backfeed_error"] assert run(app) == 0 def test_disabled_does_nothing(app, client): crossposted(app) app.config["BACKFEED_ENABLED"] = False assert run(app) == 0 and client.calls == [] # --- Pages ------------------------------------------------------------------------------ def test_note_page_shows_backfed_responses_quotes_and_counts(app, client, web): note, sid = crossposted(app) web.files["https://cdn.example/a.png"] = png() quote = Response(kind="quote", source_url="https://bsky.app/profile/q.example/post/3k", remote_id="at://did:plc:q/app.bsky.feed.post/3k", author_name="Quoter", author_url="https://bsky.app/profile/q.example", content_html="

Look at this

") client.harvest = Harvest([like(1, author_photo="https://cdn.example/a.png"), reply(2), quote]) run(app) with app.app_context(): get_db(app).execute("UPDATE syndications SET likes_count = 12, reposts_count = 1 WHERE id = ?", (sid,)) get_db(app).commit() page = app.test_client().get(f"/note/{note.slug}").get_data(as_text=True) assert "via GoToSocial" in page assert "1 Quote" in page and "Look at this" in page # Likes and reposts are a counts line (v2.13.2): named likes plus the silo's anonymous count assert '13' in page and "+12 on GoToSocial" in page assert '1' in page assert f'' in page assert f'Like on ; rel="next"'}) if path == "/api/v1/statuses/01COPY/reblogged_by": return httpx.Response(200, json=[account("2")]) if path == "/api/v1/statuses/01COPY/context": status = lambda n, parent="01COPY", vis="public", who=None: { "id": n, "url": f"{GTS}/@u/statuses/{n}", "uri": f"{GTS}/u/{n}", "in_reply_to_id": parent, "visibility": vis, "content": f"

reply {n}

", "created_at": "2026-11-10T00:00:00Z", "account": who or account("r" + n)} return httpx.Response(200, json={"ancestors": [], "descendants": [ status("10"), status("11", vis="unlisted"), status("12", parent="10"), status("13", who=account("me")), status("14", parent="12", who=account("me")), status("15", parent="13")]}) return httpx.Response(404) def test_mastodon_client_reads_likes_boosts_and_public_reply_threads(): gts = MastodonClient(GTS, "t", http=httpx.Client(transport=httpx.MockTransport(mastodon_api))) harvest = gts.responses("01COPY", COPY) assert harvest.complete got = [(r.kind, r.source_url) for r in harvest.responses] # 11 isn't public; 13 is our own thread continuing, so 15 under it is left out too. # 12 answers 10, and our 14 answers 12 (v2.20.2) assert got == [("like", f"{COPY}#liked-by-1"), ("like", f"{COPY}#liked-by-3"), ("repost", f"{COPY}#reposted-by-2"), ("reply", f"{GTS}/@u/statuses/10"), ("reply", f"{GTS}/@u/statuses/12"), ("reply", f"{GTS}/@u/statuses/14")] replies = {r.remote_id: r for r in harvest.responses if r.kind == "reply"} assert replies["10"].content_html == "

reply 10

" assert [(replies[i].parent_id, replies[i].own) for i in ("10", "12", "14")] == \ [(None, False), ("10", False), ("12", True)] assert gts.responses("01COPY", COPY, cap=1).complete is False def test_bluesky_client_reads_likes_reposts_quotes_and_replies(): me, post = "did:plc:me", "at://did:plc:me/app.bsky.feed.post/abc" def api(request): nsid = request.url.path.removeprefix("/xrpc/") query = parse_qs(request.url.query.decode()) profile = lambda d, **kw: {"did": d, "handle": f"{d[8:]}.example", "displayName": d[8:].title(), **kw} view = lambda d, rkey, text: {"uri": f"at://{d}/app.bsky.feed.post/{rkey}", "author": profile(d), "record": {"text": text, "createdAt": "2026-11-10T00:00:00Z"}} if nsid == "com.atproto.server.createSession": return httpx.Response(200, json={"did": me, "handle": "me.example", "accessJwt": "a", "refreshJwt": "r"}) if nsid == "app.bsky.feed.getLikes": if query.get("cursor"): return httpx.Response(200, json={"likes": [{"actor": profile("did:plc:carol")}]}) return httpx.Response(200, json={"cursor": "c1", "likes": [ {"actor": profile("did:plc:alice"), "createdAt": "2026-11-10T01:00:00Z"}, {"actor": profile("did:plc:shy", labels=[{"val": "!no-unauthenticated"}])}, {"actor": profile(me)}]}) if nsid == "app.bsky.feed.getRepostedBy": return httpx.Response(200, json={"repostedBy": [profile("did:plc:bob")]}) if nsid == "app.bsky.feed.getQuotes": return httpx.Response(200, json={"posts": [view("did:plc:dan", "q1", "Quoting this")]}) if nsid == "app.bsky.feed.getPostThread": return httpx.Response(200, json={"thread": {"post": {}, "replies": [ {"post": view("did:plc:eve", "r1", "A reply")}, {"$type": "app.bsky.feed.defs#blockedPost", "uri": "at://x"}]}}) if nsid == "app.bsky.actor.getProfiles": return httpx.Response(200, json={"profiles": [ profile("did:plc:dan", description="no thanks #nobot"), profile("did:plc:eve")]}) return httpx.Response(400, json={"error": "Unknown"}) bsky = BlueskyClient("https://bsky.example", "me.example", "pw", http=httpx.Client(transport=httpx.MockTransport(api))) harvest = bsky.responses(post, "https://bsky.app/profile/me.example/post/abc") by = {(r.kind, r.author_id): r for r in harvest.responses} assert set(by) == {("like", "did:plc:alice"), ("like", "did:plc:shy"), ("like", "did:plc:carol"), ("repost", "did:plc:bob"), ("quote", "did:plc:dan"), ("reply", "did:plc:eve")} assert by[("like", "did:plc:shy")].opted_out assert by[("quote", "did:plc:dan")].author_bio == "no thanks #nobot" assert by[("reply", "did:plc:eve")].source_url == "https://bsky.app/profile/eve.example/post/r1" assert by[("like", "did:plc:alice")].source_url.endswith("#liked-by-did:plc:alice") assert harvest.complete def test_threads_client_reads_replies_and_counts(): def api(request): path = request.url.path params = parse_qs(request.url.query.decode()) if path.endswith("/me"): return httpx.Response(200, json={"id": "42", "username": "me"}) if path.endswith("/111/conversation"): # The whole conversation, newest first, as Meta returns it (v2.20.2) assert "profile_picture_url" in params["fields"][0] and "replied_to" in params["fields"][0] to = lambda i: {"replied_to": {"id": i}} return httpx.Response(200, json={"data": [ {"id": "6", "text": "Ha", "username": "ann", "permalink": "https://www.threads.com/@ann/post/C", "timestamp": "2026-11-10T02:00:00+0000", "hide_status": "NOT_HUSHED", **to("5")}, {"id": "5", "text": "Yes", "username": "me", "permalink": "https://www.threads.com/@me/post/B", "timestamp": "2026-11-10T01:00:00+0000", **to("1")}, {"id": "7", "text": "under mine", "username": "bo", "permalink": "p", "hide_status": "NOT_HUSHED", **to("4")}, {"id": "1", "text": "Hi", "username": "ann", "permalink": "https://www.threads.com/@ann/post/A", "timestamp": "2026-11-10T00:00:00+0000", "hide_status": "NOT_HUSHED", **to("111")}, {"id": "2", "text": "private", **to("111")}, {"id": "3", "text": "hidden", "username": "x", "permalink": "p", "hide_status": "HIDDEN", **to("111")}, {"id": "4", "text": "mine", "username": "me", "permalink": "p", **to("111")}]}) if path.endswith("/111/insights"): assert params["metric"] == ["likes,reposts,quotes"] return httpx.Response(200, json={"data": [ {"name": "likes", "values": [{"value": 12}]}, {"name": "reposts", "values": [{"value": 3}]}, {"name": "quotes", "values": [{"value": 0}]}]}) return httpx.Response(404, json={"error": {"code": 100}}) threads = ThreadsClient("tok", http=httpx.Client(transport=httpx.MockTransport(api))) harvest = threads.responses("111", "https://www.threads.com/@me/post/X") # Our own reply to our post continues the thread (4, and 7 under it, are left out); # our reply to ann (5) and her answer (6) are kept, oldest first assert [(r.author_name, r.content_html, r.parent_id, r.own) for r in harvest.responses] == [ ("ann", "

Hi

", None, False), ("me", "

Yes

", "1", True), ("ann", "

Ha

", "5", False)] assert harvest.counts == {"likes": 12, "reposts": 3, "quotes": 0} # --- Migration --------------------------------------------------------------------------- def test_migration_020_keeps_existing_webmentions(tmp_path): import sqlite3 from pathlib import Path from starpunk.database.schema import INITIAL_SCHEMA_SQL from starpunk.migrations import execute_in_transaction migrations = Path(__file__).parent.parent / "migrations" conn = sqlite3.connect(tmp_path / "old.db") conn.execute("PRAGMA foreign_keys=ON") conn.executescript(INITIAL_SCHEMA_SQL) for name in ("010_add_webmentions.sql", "012_add_syndications.sql", "014_webmention_source_details.sql"): matches = list(migrations.glob(name.split("_")[0] + "_*.sql")) conn.execute("BEGIN") execute_in_transaction(conn, matches[0].read_text()) conn.commit() conn.execute("INSERT INTO notes (slug, file_path, published) VALUES ('n', 'n.md', 1)") conn.execute("INSERT INTO webmentions (source_url, target_url, target_note_id, mention_type, " "validation_status, moderation_status, source_published) " "VALUES ('https://a.example/r', 'https://b.example/note/n', 1, 'reply', 'valid', 'hidden', " "'2026-01-01')") conn.commit() conn.execute("BEGIN") execute_in_transaction(conn, (migrations / "020_backfeed.sql").read_text()) conn.commit() row = conn.execute("SELECT mention_type, moderation_status, source_published, origin, target_note_id " "FROM webmentions").fetchone() assert row == ("reply", "hidden", "2026-01-01", "webmention", 1) conn.execute("INSERT INTO webmentions (source_url, target_url, mention_type, origin) " "VALUES ('https://q.example', 'https://b.example/note/n', 'quote', 'bluesky')") columns = {r[1] for r in conn.execute("PRAGMA table_info(syndications)")} assert {"backfeed_next_at", "backfeed_backfill", "likes_count"} <= columns assert conn.execute("PRAGMA foreign_key_check").fetchall() == [] # --- Admin ---------------------------------------------------------------------------------- def test_admin_backfill_and_check_now(app, client): from starpunk.auth import create_session app.config.update(ADMIN_ME="https://example.com", CSRF_ENABLED=False) note, sid = crossposted(app, age_days=200) with app.test_request_context(): token = create_session("https://example.com") browser = app.test_client() browser.set_cookie("starpunk_session", token) page = browser.get("/admin/webmentions").get_data(as_text=True) assert "No backfill has been run" in page and "Backfill responses" in page response = browser.post("/admin/backfeed/backfill", follow_redirects=True) assert "Backfill started: 1 crosspost(s) to check" in response.get_data(as_text=True) assert "1 crosspost(s) left to check" in browser.get("/admin/webmentions").get_data(as_text=True) edit = browser.get(f"/admin/edit/{note.id}").get_data(as_text=True) assert "Responses not checked yet" in edit and "Check now" in edit browser.post(f"/admin/syndication/{sid}/check") with app.app_context(): s = get_db(app).execute("SELECT backfeed_next_at FROM syndications WHERE id = ?", (sid,)).fetchone() assert s["backfeed_next_at"] backfeed.denied["gotosocial"] = "Needs a GoToSocial token with read:statuses" assert "Needs a GoToSocial token" in browser.get("/admin/log").get_data(as_text=True) assert "Response checks have errors" in browser.get("/admin/webmentions").get_data(as_text=True) def test_threads_avatar_kept_while_its_copy_exists(app, client, web): note, _ = crossposted(app) with app.app_context(): get_db(app).execute("UPDATE syndications SET target = 'threads'") get_db(app).commit() syndication._client_cache["threads"] = client app.config["THREADS_ACCESS_TOKEN"] = "token" web.files["https://cdn.example/p.jpg?sig=1"] = png() first = reply(1) first.author_photo = "https://cdn.example/p.jpg?sig=1" client.harvest = Harvest([first]) run(app) with app.app_context(): get_db(app).execute("UPDATE syndications SET backfeed_next_at = datetime('now')") get_db(app).commit() again = reply(1) again.author_photo = "https://cdn.example/p.jpg?sig=2" # not fetchable: would be a refetch client.harvest = Harvest([again]) run(app) assert rows(app, note.id)[0]["author_photo"] == "https://cdn.example/p.jpg?sig=1" def test_avatars_from_own_gotosocial_and_bluesky_pds(app, web, monkeypatch): """v2.13.1: own GoToSocial may resolve to a private address; Bluesky CDN avatars come from the PDS""" did, cid = "did:plc:abc123", "bafkreiabc" web.files[f"{GTS}/fileserver/a/avatar.png"] = png() web.files[f"https://plc.directory/{did}"] = json.dumps( {"service": [{"id": "#atproto_pds", "serviceEndpoint": "https://pds.example"}]}).encode() web.files[f"https://pds.example/xrpc/com.atproto.sync.getBlob?did={did}&cid={cid}"] = png("blue") real_resolve = resolve monkeypatch.setattr("starpunk.webmention_ssrf.socket.getaddrinfo", lambda host, *a, **k: [(2, 1, 6, "", ("172.20.0.5", 0))] if host == "social.example" else real_resolve(host)) data = app.config["DATA_PATH"] with app.app_context(): assert avatars.fetch(data, f"{GTS}/fileserver/a/avatar.png", SITE) assert avatars.fetch(data, f"https://cdn.bsky.app/img/avatar/plain/{did}/{cid}@jpeg", SITE) # Other private addresses stay blocked web.files["https://internal.example/a.png"] = png() monkeypatch.setattr("starpunk.webmention_ssrf.socket.getaddrinfo", lambda host, *a, **k: [(2, 1, 6, "", ("10.0.0.5", 0))]) assert not avatars.fetch(data, "https://internal.example/a.png", SITE) def test_own_gotosocial_redirect_to_remote_avatar_is_followed(app, web, monkeypatch): """v2.13.2: GoToSocial redirects uncached remote media to the original server""" class Redirecting(FakeWeb): def __call__(self, request): url = f"{request.url.scheme}://{request.headers['host']}{request.url.raw_path.decode()}" if url == f"{GTS}/fileserver/x/avatar.jpeg": return httpx.Response(302, headers={"location": "https://remote.example/avatar.jpeg"}) return super().__call__(request) fake = Redirecting() fake.files["https://remote.example/avatar.jpeg"] = png() monkeypatch.setattr("starpunk.webmention_ssrf.TRANSPORT", httpx.MockTransport(fake)) with app.app_context(): assert avatars.fetch(app.config["DATA_PATH"], f"{GTS}/fileserver/x/avatar.jpeg", SITE) def test_people_who_opt_out_are_counted_not_named(app, client): """v2.13.3: likes and reposts by people who opted out add to the totals anonymously""" note, sid = crossposted(app) shy = like(5, opted_out=True) client.harvest = Harvest([like(1), shy, like(6, bio="#nobot"), Response(kind="repost", source_url=f"{COPY}#reposted-by-7", remote_id="7", author_name="Quiet", author_bio="#nobridge")]) run(app) with app.app_context(): s = get_db(app).execute("SELECT likes_count, reposts_count FROM syndications WHERE id = ?", (sid,)).fetchone() assert (s["likes_count"], s["reposts_count"]) == (2, 1) assert [r["source_url"] for r in rows(app, note.id)] == [like(1).source_url] page = app.test_client().get(f"/note/{note.slug}").get_data(as_text=True) assert '3' in page and "+2 on GoToSocial" in page assert "Liker 5" not in page and "Quiet" not in page def test_home_page_shows_counts(app, client): """v2.13.4: lists of notes show like, repost and reply counts, linking to the responses""" note, sid = crossposted(app) with app.app_context(): quiet = create_note(content="Nobody saw this", published=True) client.harvest = Harvest([like(1), like(2), reply(3)]) run(app) with app.app_context(): get_db(app).execute("UPDATE syndications SET likes_count = 1, reposts_count = 2 WHERE id = ?", (sid,)) get_db(app).commit() page = app.test_client().get("/").get_data(as_text=True) assert f'href="{SITE}/note/{note.slug}#responses"' in page assert 'title="3 likes"' in page and 'title="2 reposts"' in page and 'title="1 reply"' in page assert f"/note/{quiet.slug}#responses" not in page assert 'id="responses"' in app.test_client().get(f"/note/{note.slug}").get_data(as_text=True) def test_quote_counts_include_anonymous_ones(app, client): """v2.14.2: quotes have a count, including those without names (Threads, opted out)""" note, sid = crossposted(app) client.harvest = Harvest([Response(kind="quote", source_url="https://bsky.app/profile/q/post/1", remote_id="at://q/1", author_name="Quoter", content_html="

Look

")]) run(app) with app.app_context(): get_db(app).execute("UPDATE syndications SET quotes_count = 2 WHERE id = ?", (sid,)) get_db(app).commit() page = app.test_client().get(f"/note/{note.slug}").get_data(as_text=True) quote = page.split('response-count-quote')[1].split('\n')[0] assert '3' in quote and "2 on GoToSocial" in quote assert "Quoter" in page, "the named quote is listed under Responses" home = app.test_client().get("/").get_data(as_text=True) assert 'title="3 quotes"' in home # --- Reply threads (v2.20.2) ------------------------------------------------------ def test_thread_members_follow_replies_to_replies(): from starpunk.backfeed import thread_members posts = [ # newest first, as Threads lists them {"id": "c", "parent": "b", "own": False}, {"id": "b", "parent": "a", "own": True}, {"id": "x", "parent": "self", "own": False}, {"id": "self", "parent": "COPY", "own": True}, {"id": "a", "parent": "COPY", "own": False}, {"id": "loop", "parent": "loop", "own": False}, ] assert thread_members(posts, "COPY") == {"a": None, "b": "a", "c": "b"} def test_bluesky_client_reads_nested_replies(): me, post = "did:plc:me", "at://did:plc:me/app.bsky.feed.post/abc" def view(did, rkey, text, at): return {"uri": f"at://{did}/app.bsky.feed.post/{rkey}", "indexedAt": at, "author": {"did": did, "handle": did.rsplit(":", 1)[1] + ".example"}, "record": {"text": text, "createdAt": at}} def api(request): nsid = request.url.path.removeprefix("/xrpc/") if nsid == "com.atproto.server.createSession": return httpx.Response(200, json={"did": me, "handle": "me.example", "accessJwt": "a", "refreshJwt": "r"}) if nsid in ("app.bsky.feed.getLikes", "app.bsky.feed.getRepostedBy", "app.bsky.feed.getQuotes"): return httpx.Response(200, json={"likes": [], "repostedBy": [], "posts": []}) if nsid == "app.bsky.feed.getPostThread": assert int(dict(request.url.params)["depth"]) > 1 return httpx.Response(200, json={"thread": {"post": {}, "replies": [ {"post": view("did:plc:eve", "r1", "First", "2026-11-10T01:00:00Z"), "replies": [ {"post": view(me, "r2", "Thanks", "2026-11-10T02:00:00Z"), "replies": [ {"post": view("did:plc:eve", "r3", "Anytime", "2026-11-10T03:00:00Z")}]}]}, {"post": view(me, "self", "More from me", "2026-11-10T00:30:00Z")}]}}) if nsid == "app.bsky.actor.getProfiles": return httpx.Response(200, json={"profiles": []}) return httpx.Response(400, json={"error": "Unknown"}) bsky = BlueskyClient("https://bsky.example", "me.example", "pw", http=httpx.Client(transport=httpx.MockTransport(api))) replies = [(r.remote_id.rsplit("/", 1)[1], r.parent_id and r.parent_id.rsplit("/", 1)[1], r.own) for r in bsky.responses(post, "https://bsky.app/profile/me.example/post/abc").responses] assert replies == [("r1", None, False), ("r2", "r1", True), ("r3", "r2", False)] def test_reply_threads_are_stored_and_shown_nested(app, client): app.config["WEBMENTION_ENABLED"] = True note, _ = crossposted(app) first, mine, answer = reply(1, "It keeps coming up"), reply(2, "That is terrifying"), reply(3, "To be fair") mine.parent_id, mine.own, mine.author_name = "1", True, "philskents" answer.parent_id = "2" client.harvest = Harvest([first, mine, answer]) run(app) stored = {r["remote_id"]: r for r in rows(app, note.id)} assert [stored[i]["parent_remote_id"] for i in "123"] == [None, "1", "2"] assert stored["2"]["notified_at"], "our own reply is no news to us" assert not stored["1"]["notified_at"] page = app.test_client().get(f"/note/{note.slug}").data.decode() assert "3 Replies" in page # Each answer sits inside the reply it answers outer = page.index("It keeps coming up") assert outer < page.index('class="webmention-thread"') < page.index("That is terrifying") \ < page.index("To be fair") assert page.count('class="webmention-thread"') == 2 import mf2py items = mf2py.parse(doc=page, url=f"{SITE}/note/{note.slug}")["items"] entry = next(i for i in items if "h-entry" in i["type"]) (comment,) = entry["properties"]["comment"] (nested,) = comment["properties"]["comment"] assert "terrifying" in json.dumps(nested) and "To be fair" in json.dumps(nested["properties"]["comment"])