""" Tests for crossposting to Threads (starpunk/syndication.py, v2.6.0) A fake Graph API (httpx.MockTransport) records what StarPunk sends. """ import io import json import time from urllib.parse import parse_qsl import httpx import pytest from PIL import Image from starpunk import create_app, syndication from starpunk.notes import create_note, delete_note, update_note from starpunk.syndication import ( THREADS_REFRESH_EVERY, ThreadsClient, get_note_syndications, process_due, select_targets, threads_length, threads_topic, ) API = "https://graph.threads.example/v1.0" UID = "1789" class FakeThreads: def __init__(self): self.containers = {} self.posts = {} self.calls = [] self.token = "tok1" self.refreshes = 0 self.refresh_fails = False self.pending_polls = 0 self.lose_next_publish = False self.fail_next = None self.fail_next_publish = False def _json(self, code, body): return httpx.Response(code, json=body) def _error(self, code, message, err_code, subcode=None): error = {"message": message, "type": "OAuthException", "code": err_code} if subcode: error["error_subcode"] = subcode return self._json(code, {"error": error}) def __call__(self, request: httpx.Request) -> httpx.Response: params = dict(request.url.params) if request.content: params.update(parse_qsl(request.content.decode())) path = request.url.path self.calls.append((request.method, path, params)) if path == "/refresh_access_token": assert params["grant_type"] == "th_refresh_token" if self.refresh_fails or params.get("access_token") != self.token: return self._error(400, "token too new", 190) self.refreshes += 1 self.token = f"tok{self.refreshes + 1}" return self._json(200, {"access_token": self.token, "token_type": "bearer", "expires_in": 5184000}) if params.get("access_token") != self.token: return self._error(400, "Invalid OAuth access token", 190) if self.fail_next: code, err, self.fail_next = *self.fail_next, None return self._error(code, "nope", err) path = path.removeprefix("/v1.0/") if path == "me": return self._json(200, {"id": UID, "username": "phil"}) if path == f"{UID}/threads" and request.method == "POST": cid = f"c{len(self.containers) + 1}" self.containers[cid] = params return self._json(200, {"id": cid}) if path == f"{UID}/threads" and request.method == "GET": return self._json(200, {"data": [ {"id": pid, "text": p["text"], "permalink": f"https://www.threads.com/@phil/post/{pid}"} for pid, p in self.posts.items()]}) if path == f"{UID}/threads_publish" and self.fail_next_publish: self.fail_next_publish = False return self._error(400, "Fatal", 24, 4279009) if path == f"{UID}/threads_publish": container = self.containers[params["creation_id"]] pid = f"p{len(self.posts) + 1}" self.posts[pid] = container if self.lose_next_publish: self.lose_next_publish = False raise httpx.ReadTimeout("lost reply") return self._json(200, {"id": pid}) if path in self.containers and request.method == "GET": if self.pending_polls: self.pending_polls -= 1 return self._json(200, {"status": "IN_PROGRESS"}) return self._json(200, {"status": "FINISHED"}) if path in self.posts and request.method == "GET": return self._json(200, {"id": path, "permalink": f"https://www.threads.com/@phil/post/{path}"}) if request.method == "DELETE": if self.posts.pop(path, None) is None: return self._error(400, "Object does not exist", 100, 33) return self._json(200, {"success": True, "deleted_id": path}) return self._json(500, {"error": {"message": "unknown", "code": 2}}) @pytest.fixture def api(): return FakeThreads() def make_client(app, api, token="tok1"): return ThreadsClient(token, app.config["DATA_PATH"] / "threads_token.json", api=API, http=httpx.Client(transport=httpx.MockTransport(api)), sleep=lambda s: None) @pytest.fixture def app(tmp_path, api): data = tmp_path / "data" app = create_app({ "TESTING": True, "SITE_URL": "https://blog.example.com", "ADMIN_ME": "https://example.com", "SESSION_SECRET": "test-secret", "DATA_PATH": data, "NOTES_PATH": data / "notes", "DATABASE_PATH": data / "starpunk.db", "THREADS_ACCESS_TOKEN": "tok1", "THREADS_USERNAME": "phil", }) syndication._client_cache["threads"] = make_client(app, api) yield app syndication._client_cache.clear() def publish(app, content="Hello Threads", tags=None): with app.test_request_context(): note = create_note(content=content, published=True, tags=tags) select_targets(note.id, ["threads"]) process_due() return note def only_post(api): (post,) = api.posts.values() return post def row(app, note): with app.app_context(): (r,) = get_note_syndications(note.id) return r def image_bytes(fmt="JPEG", size=(800, 600)): buf = io.BytesIO() Image.new("RGB", size, (200, 30, 30)).save(buf, format=fmt) return buf.getvalue() class TestText: def test_emoji_count_as_bytes(self): assert threads_length("abc") == 3 assert threads_length("☕") == 3 def test_topic_is_first_usable_tag(self): assert threads_topic(["...", "Indie.Web & Co", "x"]) == "IndieWeb Co" assert threads_topic([]) == "" class TestLifecycle: def test_publish_text_post(self, app, api): note = publish(app, content="Hello **Threads**", tags=["IndieWeb", "Second"]) post = only_post(api) permalink = f"https://blog.example.com/note/{note.slug}" assert post["media_type"] == "TEXT" assert post["text"] == f"Hello Threads\n\n{permalink}" assert "link_attachment" not in post # Threads previews the first link itself assert post["topic_tag"] == "IndieWeb" r = row(app, note) assert r["state"] == "synced" assert r["remote_id"] == "p1" assert r["remote_url"] == "https://www.threads.com/@phil/post/p1" def test_long_note_fits_500(self, app, api): note = publish(app, content="☕ word " * 300) text = only_post(api)["text"] assert threads_length(text) <= 500 assert text.endswith(f"https://blog.example.com/note/{note.slug}") def test_waits_for_container(self, app, api): api.pending_polls = 2 note = publish(app) assert row(app, note)["state"] == "synced" def test_edit_leaves_post_alone(self, app, api): note = publish(app, content="first") with app.test_request_context(): update_note(id=note.id, content="second") process_due() assert only_post(api)["text"].startswith("first") assert row(app, note)["state"] == "synced" def test_delete_and_unpublish_remove_post(self, app, api): note = publish(app) with app.test_request_context(): update_note(id=note.id, published=False) process_due() assert api.posts == {} update_note(id=note.id, published=True) process_due() assert len(api.posts) == 1 delete_note(id=note.id) process_due() assert api.posts == {} def test_post_deleted_on_threads_is_fine(self, app, api): note = publish(app) api.posts.clear() with app.test_request_context(): delete_note(id=note.id) process_due() assert row(app, note)["state"] == "synced" class TestErrors: def test_lost_reply_does_not_double_post(self, app, api): api.lose_next_publish = True note = publish(app) assert row(app, note)["state"] == "pending" with app.test_request_context(): from starpunk.database import get_db get_db(app).execute("UPDATE syndications SET next_attempt_at = datetime('now')") get_db(app).commit() process_due() assert len(api.posts) == 1 assert row(app, note)["state"] == "synced" def test_publish_fatal_is_retried(self, app, api): api.fail_next_publish = True note = publish(app) r = row(app, note) assert r["state"] == "pending" and "Fatal" in r["last_error"] and "subcode 4279009" in r["last_error"] with app.test_request_context(): from starpunk.database import get_db get_db(app).execute("UPDATE syndications SET next_attempt_at = datetime('now')") get_db(app).commit() process_due() assert len(api.posts) == 1 assert row(app, note)["state"] == "synced" def test_bad_token_fails_permanently(self, app, api): api.token = "other" note = publish(app) r = row(app, note) assert r["state"] == "failed" and "Invalid OAuth" in r["last_error"] assert "tok1" not in r["last_error"] def test_rate_limit_retries(self, app, api): publish(app, content="warm up") api.fail_next = (400, 4) # Graph "application request limit reached" note = publish(app, content="later") assert row(app, note)["state"] == "pending" class TestLinks: def test_markdown_link_comes_first_for_the_preview(self, app, api): note = publish(app, content="Read [this story](https://news.example/story) now") text = only_post(api)["text"] assert text.startswith("Read this story (https://news.example/story) now") assert text.index("https://news.example/story") < text.index(f"/note/{note.slug}") def test_stays_within_five_links(self, app, api): import re links = " ".join(f"[l{i}](https://l{i}.example)" for i in range(7)) publish(app, content=f"Lots: {links}") text = only_post(api)["text"] assert len(set(re.findall(r"https?://[^\s)]+", text))) == 5 assert "l6" in text # the extra links keep their text class TestMedia: def _note_with(self, app, images): from starpunk.media import attach_media_to_note, save_media with app.test_request_context(): note = create_note(content="pics", published=True) ids = [save_media(data, name)["id"] for data, name in images] attach_media_to_note(note.id, ids, [f"alt {i}" for i in range(len(ids))]) select_targets(note.id, ["threads"]) process_due() return note def test_single_image(self, app, api): self._note_with(app, [(image_bytes(), "a.jpg")]) post = only_post(api) assert post["media_type"] == "IMAGE" assert post["image_url"].startswith("https://blog.example.com/media/") assert post["alt_text"] == "alt 0" assert "link_attachment" not in post def test_carousel(self, app, api): self._note_with(app, [(image_bytes(), "a.jpg"), (image_bytes("PNG"), "b.png")]) post = only_post(api) assert post["media_type"] == "CAROUSEL" children = [api.containers[c] for c in post["children"].split(",")] assert [c["is_carousel_item"] for c in children] == ["true", "true"] assert [c["alt_text"] for c in children] == ["alt 0", "alt 1"] def test_unsupported_format_is_left_off(self, app, api): self._note_with(app, [(image_bytes("WEBP"), "a.webp")]) assert only_post(api)["media_type"] == "TEXT" class TestToken: def test_refreshes_daily_and_saves(self, app, api): client = syndication._client_cache["threads"] with app.app_context(): client.maintain() assert api.refreshes == 1 client.maintain() # same day: nothing assert api.refreshes == 1 saved = json.loads((app.config["DATA_PATH"] / "threads_token.json").read_text()) assert saved["token"] == "tok2" # A restart picks up the saved token assert make_client(app, api)._token == "tok2" publish(app) assert len(api.posts) == 1 def test_other_worker_refresh_is_reused(self, app, api): first, second = make_client(app, api), make_client(app, api) with app.app_context(): first.maintain() second.maintain() assert api.refreshes == 1 assert second._token == "tok2" def test_refresh_due_after_a_day(self, app, api, monkeypatch): client = syndication._client_cache["threads"] with app.app_context(): client.maintain() later = time.time() + THREADS_REFRESH_EVERY + 1 monkeypatch.setattr(syndication.time, "time", lambda: later) client.maintain() assert api.refreshes == 2 def test_failed_refresh_keeps_token(self, app, api): api.refresh_fails = True client = syndication._client_cache["threads"] with app.app_context(): client.maintain() assert client._token == "tok1" publish(app) assert len(api.posts) == 1 def test_new_config_token_replaces_saved_one(self, app, api): with app.app_context(): syndication._client_cache["threads"].maintain() api.token = "fresh" assert make_client(app, api, token="fresh")._token == "fresh" def test_worker_runs_upkeep(self, app, api): with app.app_context(): process_due() assert api.refreshes == 1 def test_all_targets_listed_for_micropub(tmp_path): data = tmp_path / "d" app = create_app({ "TESTING": True, "SITE_URL": "https://b.example", "ADMIN_ME": "https://e.example", "SESSION_SECRET": "x", "DATA_PATH": data, "NOTES_PATH": data / "notes", "DATABASE_PATH": data / "s.db", "GOTOSOCIAL_URL": "https://social.example", "GOTOSOCIAL_ACCESS_TOKEN": "t", "BLUESKY_HANDLE": "phil.example", "BLUESKY_APP_PASSWORD": "p", "THREADS_ACCESS_TOKEN": "x", "THREADS_USERNAME": "@phil", }) with app.app_context(): from starpunk.syndication import get_targets, resolve_targets assert set(get_targets()) == {"gotosocial", "bluesky", "threads"} assert resolve_targets(["https://www.threads.com/@phil"]) == ["threads"] syndication._client_cache.clear() class TestLikesReposts: PAGE = "https://other.example/post/1" def _post(self, app, monkeypatch, content="", **kind): monkeypatch.setattr("starpunk.reply_context.fetch_reply_context", lambda *a, **k: None) with app.test_request_context(): note = create_note(content=content, published=True, **kind) select_targets(note.id, ["threads"]) process_due() return note def test_like_is_not_crossposted(self, app, api, monkeypatch): note = self._post(app, monkeypatch, like_of=self.PAGE) assert api.posts == {} r = row(app, note) assert r["state"] == "synced" and r["remote_id"] is None def test_repost_posts_link(self, app, api, monkeypatch): self._post(app, monkeypatch, repost_of=self.PAGE) post = only_post(api) assert post["text"].startswith("🔁") and self.PAGE in post["text"] assert "link_attachment" not in post def test_repost_with_comment_keeps_comment(self, app, api, monkeypatch): note = self._post(app, monkeypatch, content="So **true**", repost_of=self.PAGE) assert only_post(api)["text"].startswith("So true") with app.test_request_context(): delete_note(id=note.id) process_due() assert api.posts == {}