diff --git a/backend/ads/tests.py b/backend/ads/tests.py index f3506c0..f822c5d 100644 --- a/backend/ads/tests.py +++ b/backend/ads/tests.py @@ -102,3 +102,52 @@ class AdEvaluationAPITests(APITestCase): self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) self.assertFalse(AdEvaluation.objects.filter(id=self.eval1.id).exists()) + +from unittest.mock import MagicMock +from ads.tasks import extract_and_parse_json, call_llm_with_structured_fallback, AdEvaluationResult + +class AdTasksHelperTests(APITestCase): + def test_extract_and_parse_json_raw(self): + raw = '{"is_flagged": true, "reason": "مناسب است", "confidence": 0.9}' + res = extract_and_parse_json(raw) + self.assertTrue(res['is_flagged']) + self.assertEqual(res['reason'], 'مناسب است') + + def test_extract_and_parse_json_markdown_fence(self): + raw = '```json\n{\n "is_flagged": false,\n "reason": "نامناسب",\n "confidence": 0.2\n}\n```' + res = extract_and_parse_json(raw) + self.assertFalse(res['is_flagged']) + + def test_extract_and_parse_json_with_preamble(self): + raw = 'Here is the response:\n```json\n{"is_flagged": true, "reason": "خوب", "confidence": 0.8}\n```\nHope this helps!' + res = extract_and_parse_json(raw) + self.assertTrue(res['is_flagged']) + + def test_call_llm_fallback_on_markdown(self): + mock_client = MagicMock() + # beta parse raises exception (simulating markdown parse failure) + mock_client.beta.chat.completions.parse.side_effect = Exception("Invalid JSON markdown") + + # standard completion returns markdown + mock_message = MagicMock() + mock_message.content = '```json\n{"is_flagged": true, "reason": "پرچم گذاری شد", "confidence": 0.95}\n```' + mock_choice = MagicMock() + mock_choice.message = mock_message + mock_completion = MagicMock() + mock_completion.choices = [mock_choice] + mock_client.chat.completions.create.return_value = mock_completion + + res = call_llm_with_structured_fallback( + client=mock_client, + model_name="openrouter/free", + system_instruction="sys", + user_content="user", + pydantic_cls=AdEvaluationResult, + max_tokens=2500, + timeout=35 + ) + self.assertTrue(res.is_flagged) + self.assertEqual(res.reason, "پرچم گذاری شد") + self.assertEqual(res.confidence, 0.95) + +