import json import unittest from unittest.mock import AsyncMock, patch from app.vision.analyze import VisionService, format_user_message, format_vision_turn_hint from app.vision.preprocess import PreparedImage class VisionAnalyzeTests(unittest.TestCase): def test_format_user_message_with_fitness_hints(self) -> None: from app.vision.analyze import VisionResult result = VisionResult( parsed={ "description": "Экран тренировки бег", "document_type": "fitness_workout", "extracted_text": ["45 мин", "420 ккал"], "tables": [{"title": "Пульс", "rows": [["средний", "152"]]}], "fitness_hints": {"duration_min": 45, "active_calories": 420}, "confidence": "high", }, raw_content="{}", model="test-model", ) text = format_user_message("запиши тренировку", result) self.assertIn("[Скриншот: fitness_workout", text) self.assertIn("420 ккал", text) self.assertIn("Подпись: запиши тренировку", text) def test_run_async_analyze(self) -> None: import asyncio prepared = PreparedImage( jpeg_bytes=b"fakejpeg", width=100, height=100, original_bytes=200, compressed_bytes=100, ) payload = { "description": "Шаги за день", "document_type": "fitness_steps", "extracted_text": ["8432 шага"], "tables": [], "fitness_hints": {"steps": 8432}, "confidence": "high", "notes": "", } async def _run() -> None: service = VisionService() with patch.object( service.llm, "complete_vision", new=AsyncMock(return_value={"content": json.dumps(payload), "model": "vision-test", "usage": {}}), ): result = await service.analyze_prepared(prepared, user_hint="шаги") self.assertEqual(result.parsed["document_type"], "fitness_steps") self.assertEqual(result.parsed["fitness_hints"]["steps"], 8432) self.assertEqual(result.model, "vision-test") asyncio.run(_run()) def test_format_vision_turn_hint(self) -> None: self.assertEqual(format_vision_turn_hint("привет"), "") self.assertIn("не видишь", format_vision_turn_hint("[Скриншот: other, confidence=high]\nОписание: test")) self.assertIn("не видишь", format_vision_turn_hint("[Скриншот 2/3: other, confidence=high]\nОписание: test")) if __name__ == "__main__": unittest.main()