"""Integration tests for the content generation and distribution flow. Covers: organization → content creation → distribution schedule → status tracking. """ import uuid import pytest from sqlalchemy import select from app.models.organization import Organization from app.models.content import Content from app.models.distribution import DistributionSchedule from tests.fixtures.auth import _to_uuid class TestContentDistributionFlow: """Test content creation through distribution.""" @pytest.mark.asyncio async def test_content_to_distribution_flow(self, async_session, test_user): """Test content creation through distribution scheduling.""" # 1. Create organization (required FK for Content and DistributionSchedule) org = Organization( name="Test Org", slug=f"test-org-{uuid.uuid4().hex[:8]}", plan="free", ) async_session.add(org) await async_session.commit() await async_session.refresh(org) # 2. Create content under the organization content = Content( organization_id=org.id, title="GEO优化文章", body="这是一篇关于AI搜索优化的文章正文。", content_type="article", status="draft", target_platforms=["wenxin", "kimi"], created_by=test_user.id, ) async_session.add(content) await async_session.commit() await async_session.refresh(content) # 3. Create distribution schedule for the content schedule = DistributionSchedule( organization_id=org.id, content_title=content.title, content_id=content.id, platforms=[ {"platform": "wenxin", "platform_name": "文心一言", "status": "pending"}, {"platform": "kimi", "platform_name": "Kimi", "status": "pending"}, ], status="pending", created_by=test_user.id, ) async_session.add(schedule) await async_session.commit() await async_session.refresh(schedule) # 4. Verify the chain: org → content → distribution assert content.organization_id == org.id assert schedule.content_id == content.id assert schedule.organization_id == org.id assert len(schedule.platforms) == 2 @pytest.mark.asyncio async def test_content_status_transitions(self, async_session, test_user): """Test content status transitions: draft → published.""" org = Organization( name="Status Org", slug=f"status-org-{uuid.uuid4().hex[:8]}", plan="free", ) async_session.add(org) await async_session.commit() await async_session.refresh(org) content = Content( organization_id=org.id, title="Status Test Article", body="Content body", content_type="article", status="draft", created_by=test_user.id, ) async_session.add(content) await async_session.commit() await async_session.refresh(content) assert content.status == "draft" # Transition to published content.status = "published" await async_session.commit() await async_session.refresh(content) assert content.status == "published" @pytest.mark.asyncio async def test_distribution_schedule_status_tracking(self, async_session, test_user): """Test distribution schedule status tracking: pending → completed.""" org = Organization( name="Dist Org", slug=f"dist-org-{uuid.uuid4().hex[:8]}", plan="free", ) async_session.add(org) await async_session.commit() await async_session.refresh(org) content = Content( organization_id=org.id, title="Distribution Test", body="Body text", content_type="article", status="published", created_by=test_user.id, ) async_session.add(content) await async_session.commit() await async_session.refresh(content) schedule = DistributionSchedule( organization_id=org.id, content_title=content.title, content_id=content.id, platforms=[ {"platform": "wenxin", "status": "pending"}, ], status="pending", created_by=test_user.id, ) async_session.add(schedule) await async_session.commit() await async_session.refresh(schedule) assert schedule.status == "pending" # Mark as completed schedule.status = "completed" await async_session.commit() await async_session.refresh(schedule) assert schedule.status == "completed" # Verify it's still linked to the content result = await async_session.execute( select(DistributionSchedule).where( DistributionSchedule.content_id == content.id ) ) schedules = result.scalars().all() assert len(schedules) == 1 assert schedules[0].status == "completed"