import asyncio import uuid from unittest.mock import AsyncMock, patch import pytest from fastapi import HTTPException from pydantic import ValidationError from api.v1.ppt.endpoints.presentation import generate_presentation_sync from models.generate_presentation_request import GeneratePresentationRequest from models.presentation_and_path import PresentationPathAndEditPath class FakeAsyncSession: async def get(self, *_args, **_kwargs): return None def add(self, *_args, **_kwargs): return None def add_all(self, *_args, **_kwargs): return None async def commit(self): return None class TestPresentationGenerationAPI: def test_generate_presentation_export_as_pdf(self): request = GeneratePresentationRequest( content="Create a presentation about artificial intelligence and machine learning", n_slides=5, language="English", export_as="pdf", template="general", ) response_payload = PresentationPathAndEditPath( presentation_id=uuid.uuid4(), path="/tmp/exports/test.pdf", edit_path="/presentation?id=test", ) with patch( "api.v1.ppt.endpoints.presentation.generate_presentation_handler", new=AsyncMock(return_value=response_payload), ) as mock_handler: response = asyncio.run( generate_presentation_sync(request, sql_session=FakeAsyncSession()) ) assert response == response_payload mock_handler.assert_awaited_once() def test_generate_presentation_export_as_pptx(self): request = GeneratePresentationRequest( content="Create a presentation about artificial intelligence and machine learning", n_slides=5, language="English", export_as="pptx", template="general", ) response_payload = PresentationPathAndEditPath( presentation_id=uuid.uuid4(), path="/tmp/exports/test.pptx", edit_path="/presentation?id=test", ) with patch( "api.v1.ppt.endpoints.presentation.generate_presentation_handler", new=AsyncMock(return_value=response_payload), ) as mock_handler: response = asyncio.run( generate_presentation_sync(request, sql_session=FakeAsyncSession()) ) assert response == response_payload mock_handler.assert_awaited_once() def test_generate_presentation_with_no_content(self): with pytest.raises(ValidationError): GeneratePresentationRequest.model_validate( { "n_slides": 5, "language": "English", "export_as": "pdf", "template": "general", } ) def test_generate_presentation_with_n_slides_less_than_one(self): request = GeneratePresentationRequest( content="Create a presentation about artificial intelligence and machine learning", n_slides=0, language="English", export_as="pdf", template="general", ) with pytest.raises(HTTPException) as exc: asyncio.run( generate_presentation_sync(request, sql_session=FakeAsyncSession()) ) assert exc.value.status_code == 400 assert exc.value.detail == "Number of slides must be greater than 0" def test_generate_presentation_with_invalid_export_type(self): with pytest.raises(ValidationError): GeneratePresentationRequest.model_validate( { "content": "Create a presentation about artificial intelligence and machine learning", "n_slides": 5, "language": "English", "export_as": "invalid_type", "template": "general", } )