""" Tests for crossposting to Bluesky (starpunk/syndication.py, v2.2.0) A fake PDS (httpx.MockTransport) records what StarPunk sends. """ import io import json 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 ( BlueskyClient, bluesky_facets, get_note_syndications, plain_text, process_due, select_targets, ) SERVICE = "https://bsky.example" PDS = "https://pds.example" DID = "did:plc:abc123" class FakePDS: def __init__(self): self.records = {} self.blobs = [] self.calls = [] self.logins = 0 self.expire_next = False self.lose_next_reply = False self.fail_next = None def _json(self, code, body): return httpx.Response(code, json=body) def __call__(self, request: httpx.Request) -> httpx.Response: nsid = request.url.path.removeprefix("/xrpc/") self.calls.append((request.url.host, nsid)) if nsid == "com.atproto.server.createSession": body = json.loads(request.content) if body["password"] != "app-pass": return self._json(401, {"error": "AuthenticationRequired"}) self.logins += 1 return self._json(200, { "did": DID, "handle": "phil.example", "accessJwt": f"access{self.logins}", "refreshJwt": "refresh", "didDoc": {"service": [{"id": "#atproto_pds", "serviceEndpoint": PDS}]}, }) if nsid == "com.atproto.server.refreshSession": self.logins += 1 return self._json(200, {"did": DID, "handle": "phil.example", "accessJwt": f"access{self.logins}", "refreshJwt": "refresh"}) assert request.url.host == "pds.example" if request.headers.get("Authorization") != f"Bearer access{self.logins}" or self.expire_next: self.expire_next = False return self._json(400, {"error": "ExpiredToken"}) if self.fail_next: code, self.fail_next = self.fail_next, None return self._json(code, {"error": "Nope"}) if nsid == "com.atproto.repo.uploadBlob": self.blobs.append(len(request.content)) assert len(request.content) <= 1_000_000 return self._json(200, {"blob": {"$type": "blob", "ref": {"$link": f"b{len(self.blobs)}"}, "mimeType": request.headers["Content-Type"], "size": len(request.content)}}) if nsid == "com.atproto.repo.createRecord": body = json.loads(request.content) uri = f"at://{DID}/app.bsky.feed.post/r{len(self.records) + 1}" self.records[uri] = body["record"] if self.lose_next_reply: self.lose_next_reply = False raise httpx.ReadTimeout("lost reply") return self._json(200, {"uri": uri, "cid": "c"}) if nsid == "com.atproto.repo.listRecords": return self._json(200, {"records": [{"uri": u, "value": v} for u, v in self.records.items()]}) if nsid == "com.atproto.repo.deleteRecord": body = json.loads(request.content) self.records.pop(f"at://{body['repo']}/{body['collection']}/{body['rkey']}", None) return self._json(200, {}) return self._json(500, {"error": "unknown"}) @pytest.fixture def pds(): return FakePDS() class FakeWeb: """Other people's pages, for link cards: url -> (status, headers, body)""" def __init__(self): self.pages = {} self.fetched = [] def __call__(self, request): url = str(request.url) self.fetched.append(url) status, headers, body = self.pages.get(url, (404, {}, b"nope")) return httpx.Response(status, headers=headers, content=body) @pytest.fixture def web(): return FakeWeb() def routed(pds, web): return httpx.MockTransport( lambda r: pds(r) if r.url.host in ("bsky.example", "pds.example") else web(r)) @pytest.fixture def app(tmp_path, pds, web): 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", "BLUESKY_HANDLE": "phil.example", "BLUESKY_APP_PASSWORD": "app-pass", "BLUESKY_SERVICE": SERVICE, }) syndication._client_cache[f"bluesky:{SERVICE}:phil.example"] = BlueskyClient( SERVICE, "phil.example", "app-pass", http=httpx.Client(transport=routed(pds, web)) ) yield app syndication._client_cache.clear() def publish(app, content="Hello Bluesky", tags=None): with app.test_request_context(): note = create_note(content=content, published=True, tags=tags) select_targets(note.id, ["bluesky"]) process_due() return note def only_record(pds): (record,) = pds.records.values() return record def row(app, note): with app.app_context(): (r,) = get_note_syndications(note.id) return r class TestText: def test_markdown_becomes_plain_text(self): md = "# Title\n\nSome *emphasis* and a [link](https://x.example).\n\n- one\n- two\n\n" assert plain_text(md) == "Title\n\nSome emphasis and a link.\n\n• one\n• two" def test_facets_use_utf8_byte_offsets(self): text = "Café ☕ https://blog.example.com/note/x #IndieWeb" link, tag = bluesky_facets(text) raw = text.encode() assert raw[link["index"]["byteStart"]:link["index"]["byteEnd"]] == b"https://blog.example.com/note/x" assert raw[tag["index"]["byteStart"]:tag["index"]["byteEnd"]] == b"#IndieWeb" assert tag["features"][0]["tag"] == "IndieWeb" def test_fragment_in_url_is_not_a_tag(self): facets = bluesky_facets("see https://x.example/page#section and #42 too") assert [f["features"][0]["$type"] for f in facets] == ["app.bsky.richtext.facet#link"] class TestLifecycle: def test_publish_posts_with_link_card(self, app, pds): note = publish(app, content="Hello **Bluesky**", tags=["IndieWeb"]) record = only_record(pds) permalink = f"https://blog.example.com/note/{note.slug}" assert record["text"] == f"Hello Bluesky\n\n{permalink}\n\n#IndieWeb" assert record["embed"]["external"]["uri"] == permalink kinds = {f["features"][0]["$type"] for f in record["facets"]} assert kinds == {"app.bsky.richtext.facet#link", "app.bsky.richtext.facet#tag"} r = row(app, note) assert r["state"] == "synced" assert r["remote_id"] == f"at://{DID}/app.bsky.feed.post/r1" assert r["remote_url"] == "https://bsky.app/profile/phil.example/post/r1" def test_long_note_fits_300(self, app, pds): note = publish(app, content="word " * 500) text = only_record(pds)["text"] assert len(text) <= 300 assert text.endswith(f"https://blog.example.com/note/{note.slug}") def test_edit_leaves_post_alone(self, app, pds): note = publish(app, content="first") with app.test_request_context(): update_note(id=note.id, content="second") process_due() assert only_record(pds)["text"].startswith("first") assert row(app, note)["state"] == "synced" def test_delete_and_unpublish_remove_post(self, app, pds): note = publish(app) with app.test_request_context(): update_note(id=note.id, published=False) process_due() assert pds.records == {} update_note(id=note.id, published=True) process_due() assert len(pds.records) == 1 delete_note(id=note.id) process_due() assert pds.records == {} class TestSession: def test_logs_in_once_and_uses_pds(self, app, pds): publish(app, content="one") publish(app, content="two") assert pds.logins == 1 assert ("bsky.example", "com.atproto.server.createSession") in pds.calls assert ("pds.example", "com.atproto.repo.createRecord") in pds.calls def test_expired_token_is_refreshed(self, app, pds): publish(app, content="one") pds.expire_next = True publish(app, content="two") assert len(pds.records) == 2 assert ("bsky.example", "com.atproto.server.refreshSession") in pds.calls def test_bad_password_fails_permanently(self, app, pds): syndication._client_cache[f"bluesky:{SERVICE}:phil.example"].password = "wrong" note = publish(app) r = row(app, note) assert r["state"] == "failed" and "401" in r["last_error"] def test_lost_reply_does_not_double_post(self, app, pds): pds.lose_next_reply = True note = publish(app) assert row(app, note)["state"] == "pending" with app.test_request_context(): from starpunk.syndication import retry retry(row(app, note)["id"]) # retry() resets attempts; simulate the scheduled retry instead from starpunk.database import get_db get_db(app).execute("UPDATE syndications SET attempts = 1") get_db(app).commit() process_due() assert len(pds.records) == 1 assert row(app, note)["state"] == "synced" def test_server_error_retries(self, app, pds): publish(app, content="warm up") # log in first pds.fail_next = 502 note = publish(app, content="later") assert row(app, note)["state"] == "pending" class TestMedia: def test_large_image_is_shrunk_with_alt_and_aspect_ratio(self, app, pds): from starpunk.media import attach_media_to_note, save_media import os img = Image.frombytes("RGB", (2400, 1800), os.urandom(2400 * 1800 * 3)) buf = io.BytesIO() img.save(buf, format="PNG") assert buf.tell() > 1_000_000 with app.test_request_context(): note = create_note(content="noisy", published=True) media = save_media(buf.getvalue(), "noise.png") attach_media_to_note(note.id, [media["id"]], ["Static"]) select_targets(note.id, ["bluesky"]) process_due() record = only_record(pds) (image,) = record["embed"]["images"] assert record["embed"]["$type"] == "app.bsky.embed.images" assert image["alt"] == "Static" assert image["aspectRatio"]["width"] > 0 ARTICLE = "https://news.example/story" ARTICLE_HTML = b"""