|
| 1 | +import json |
| 2 | +from unittest.mock import MagicMock, patch |
| 3 | + |
| 4 | +from core.llm_generator.llm_generator import LLMGenerator |
| 5 | + |
| 6 | + |
| 7 | +class DummyMessage: |
| 8 | + def __init__(self, content): |
| 9 | + self.content = content |
| 10 | + |
| 11 | + |
| 12 | +class DummyResponse: |
| 13 | + def __init__(self, content): |
| 14 | + self.message = DummyMessage(content) |
| 15 | + |
| 16 | + |
| 17 | +def make_json_response(language, output): |
| 18 | + return json.dumps({"Language Type": language, "Your Reasoning": "...", "Your Output": output}) |
| 19 | + |
| 20 | + |
| 21 | +@patch("core.llm_generator.llm_generator.ModelManager.get_default_model_instance") |
| 22 | +def test_generate_conversation_name_enforces_persian(mock_get_model): |
| 23 | + # A Persian input containing Persian-specific character 'پ' |
| 24 | + persian_query = "سلام، چطوری؟ پ" # contains 'پ' |
| 25 | + |
| 26 | + # First model response: misdetected as Arabic and returns Arabic title |
| 27 | + first_resp = DummyResponse(make_json_response("Arabic", "مرحبا")) |
| 28 | + # Second response (after retry): returns a Persian title with Persian-specific chars |
| 29 | + second_resp = DummyResponse(make_json_response("Persian", "عنوان پِرس")) |
| 30 | + |
| 31 | + model_instance = MagicMock() |
| 32 | + model_instance.invoke_llm.side_effect = [first_resp, second_resp] |
| 33 | + |
| 34 | + mock_get_model.return_value = model_instance |
| 35 | + |
| 36 | + name = LLMGenerator.generate_conversation_name("tenant1", persian_query) |
| 37 | + |
| 38 | + # The final name should come from the Persian response (contains Persian-specific char 'پ') |
| 39 | + assert "پ" in name |
| 40 | + # Ensure the model was invoked at least twice (retry occurred) |
| 41 | + assert model_instance.invoke_llm.call_count >= 2 |
| 42 | + |
| 43 | + |
| 44 | +@patch("core.llm_generator.llm_generator.ModelManager.get_default_model_instance") |
| 45 | +def test_generate_conversation_name_translation_fallback(mock_get_model): |
| 46 | + # Persian query |
| 47 | + persian_query = "این یک تست است پ" |
| 48 | + |
| 49 | + # Model returns non-Persian outputs consistently |
| 50 | + non_persian_resp = DummyResponse(make_json_response("Arabic", "مرحبا")) |
| 51 | + |
| 52 | + # Translate response (last call) returns Persian translation |
| 53 | + translate_resp = DummyResponse("عنوان ترجمه شده پ") |
| 54 | + |
| 55 | + model_instance = MagicMock() |
| 56 | + # First two calls return non-persian results; third call is translation |
| 57 | + model_instance.invoke_llm.side_effect = [non_persian_resp, non_persian_resp, translate_resp] |
| 58 | + |
| 59 | + mock_get_model.return_value = model_instance |
| 60 | + |
| 61 | + name = LLMGenerator.generate_conversation_name("tenant1", persian_query) |
| 62 | + |
| 63 | + # Final name should contain Persian character 'پ' from translation fallback |
| 64 | + assert "پ" in name |
| 65 | + assert model_instance.invoke_llm.call_count >= 3 |
0 commit comments