from __future__ import annotations import os import sys import unittest sys.path.insert(0, os.path.dirname(os.path.dirname(__file__))) from analytics.model_voice import build_hr_model_voice class TestModelVoice(unittest.TestCase): def test_hr_model_voice_uses_ranked_reason_candidates(self) -> None: row = { "model_voice_reason_candidates": [ { "category": "arsenal", "direction": "supportive", "magnitude": 0.012, "signed_magnitude": 0.012, "template_key": "arsenal_favorable", "template_inputs": {}, }, { "category": "pitcher", "direction": "caution", "magnitude": 0.008, "signed_magnitude": -0.008, "template_key": "pitcher_suppresses_hr", "template_inputs": {"pitcher_name": "Ace Arm"}, }, ] } voice = build_hr_model_voice(row) self.assertIn("arsenal mix fits his power profile", voice["model_voice"].lower()) self.assertIn("pitcher profile suppresses home-run damage", voice["model_voice"].lower()) self.assertTrue(voice["model_voice"].endswith(".")) self.assertEqual(voice["model_voice_primary_reason"], "The arsenal mix fits his power profile") self.assertEqual(voice["model_voice_caveat"], "The pitcher profile suppresses home-run damage") def test_hr_model_voice_changes_with_different_reasons(self) -> None: favorable = build_hr_model_voice( { "model_voice_reason_candidates": [ { "category": "environment", "direction": "supportive", "magnitude": 0.010, "signed_magnitude": 0.010, "template_key": "weather_supportive", "template_inputs": {}, } ] } ) caution = build_hr_model_voice( { "model_voice_reason_candidates": [ { "category": "platoon", "direction": "caution", "magnitude": 0.009, "signed_magnitude": -0.009, "template_key": "platoon_disadvantage", "template_inputs": {}, } ] } ) self.assertNotEqual(favorable["model_voice"], caution["model_voice"]) self.assertIn("weather conditions", favorable["model_voice"].lower()) self.assertIn("same-handed", caution["model_voice"].lower()) if __name__ == "__main__": unittest.main()