"""Anonymous feed contract: private presentation, durable accountability.""" from __future__ import annotations import pytest from sqlalchemy.ext.asyncio import AsyncSession from app.core.exceptions import ValidationError from app.domains.feed.models import ANONYMITY_ANONYMOUS from app.domains.feed.service import FeedService @pytest.mark.asyncio async def test_anonymous_post_hides_author_and_forces_public_text_only( db_session: AsyncSession, employee_user, ) -> None: author, _ = employee_user await db_session.refresh(author, attribute_names=["org_memberships", "user_roles"]) svc = FeedService(db_session) post, created = await svc.create_user_post( author=author, body="a sensitive question", visibility="department", category="general", mentioned_person_ids=[], attachments=[], author_visibility=ANONYMITY_ANONYMOUS, ) assert created is True assert post.visibility == "public" assert post.author_person_id == str(author.id) # audit / ownership survive rows, _ = await svc.list_posts(viewer=author, tab="anonymous") assert post.id in [row.id for row in rows] [dto] = await svc.hydrate_posts([post], viewer=author) assert dto["author"] is None assert dto["author_visibility"] == ANONYMITY_ANONYMOUS with pytest.raises(ValidationError, match="cannot include attachments"): await svc.create_user_post( author=author, body="not permitted", visibility="public", category="general", mentioned_person_ids=[], attachments=[{"s3_key": "feed_post_images/x/y"}], author_visibility=ANONYMITY_ANONYMOUS, ) @pytest.mark.asyncio async def test_comments_on_anonymous_post_are_always_anonymous( db_session: AsyncSession, employee_user, ) -> None: author, _ = employee_user await db_session.refresh(author, attribute_names=["org_memberships", "user_roles"]) svc = FeedService(db_session) post, _ = await svc.create_user_post( author=author, body="anonymous post", visibility="public", category="general", mentioned_person_ids=[], attachments=[], author_visibility=ANONYMITY_ANONYMOUS, ) comment = await svc.add_comment( post_id=post.id, author=author, body="reply", mentioned_person_ids=[], reply_to_comment_id=None, author_visibility="named", ) assert comment.author_visibility == ANONYMITY_ANONYMOUS [dto] = await svc.list_comments(post_id=post.id, viewer=author) assert dto["author"] is None