import unittest from unittest.mock import patch from training_coach.models import CheckIn, ParsedCheckIn from training_coach.parser_service import parser_backend, parse_check_in_with_configured_backend class ParserServiceTest(unittest.TestCase): def test_parser_backend_defaults_to_ollama_locally(self): with patch.dict("os.environ", {}, clear=True): self.assertEqual(parser_backend(), "ollama") def test_parser_backend_defaults_to_transformers_on_space(self): with patch.dict("os.environ", {"SPACE_ID": "LucasMah/app"}, clear=True): self.assertEqual(parser_backend(), "llama_cpp") def test_parser_backend_env_override_wins(self): with patch.dict( "os.environ", {"SPACE_ID": "LucasMah/app", "PARSER_BACKEND": "ollama"}, clear=True, ): self.assertEqual(parser_backend(), "ollama") def test_configured_parser_uses_ollama_backend(self): parsed = ParsedCheckIn(check_in=CheckIn(raw_text="60 min")) with patch.dict("os.environ", {"PARSER_BACKEND": "ollama"}, clear=True): with patch( "training_coach.parser_service.parse_check_in_with_ollama", return_value=parsed, ) as parse_ollama: result = parse_check_in_with_configured_backend("60 min") self.assertEqual(result, parsed) parse_ollama.assert_called_once_with("60 min", model_name="qwen3:1.7B") def test_configured_parser_uses_llama_cpp_backend(self): parsed = ParsedCheckIn(check_in=CheckIn(raw_text="60 min")) with patch.dict("os.environ", {"PARSER_BACKEND": "llama_cpp"}, clear=True): with patch( "training_coach.parser_service.parse_check_in_with_llama_cpp", return_value=parsed, ) as parse_llama_cpp: result = parse_check_in_with_configured_backend("60 min") self.assertEqual(result, parsed) parse_llama_cpp.assert_called_once_with("60 min") def test_configured_parser_uses_transformers_backend(self): parsed = ParsedCheckIn(check_in=CheckIn(raw_text="60 min")) with patch.dict("os.environ", {"PARSER_BACKEND": "transformers"}, clear=True): with patch( "training_coach.parser_service.parse_check_in_with_model", return_value=parsed, ) as parse_transformers: result = parse_check_in_with_configured_backend("60 min") self.assertEqual(result, parsed) parse_transformers.assert_called_once_with("60 min") if __name__ == "__main__": unittest.main()