from signspeak.llm import ( deterministic_speech_from_intent, enforce_intent_consistency, extract_json_object, normalize_llm_output, safe_json_loads, ) def test_extract_json_object_from_markdown_fence(): raw = """```json {"subtitle": "I'm happy to see you!", "voice_instruction": "Speak warmly."} ```""" parsed = extract_json_object(raw) assert parsed["subtitle"] == "I'm happy to see you!" assert parsed["voice_instruction"] == "Speak warmly." def test_extract_json_object_from_text_wrapper(): raw = 'Result: {"subtitle": "Hello.", "voice_instruction": "Speak clearly."} Done.' parsed = extract_json_object(raw) assert parsed == { "subtitle": "Hello.", "voice_instruction": "Speak clearly.", } def test_normalize_llm_output_blocks_json_subtitle(): normalized = normalize_llm_output( { "subtitle": '{"subtitle": "Bad nested payload"}', "voice_instruction": "Speak warmly.", } ) assert normalized["subtitle"] == "I want to say something." assert normalized["voice_instruction"] == "Speak warmly." def test_safe_json_loads_falls_back_to_raw_text(): parsed = safe_json_loads("not json") assert parsed["raw_input"] == "not json" assert "warning" in parsed def test_deterministic_speech_maps_i_love_you(): result = deterministic_speech_from_intent( { "detected_glosses": ["I", "LOVE", "YOU"], "detected_facial_expression": "neutral", } ) assert result["subtitle"] == "I love you." def test_deterministic_speech_does_not_hallucinate_empty_glosses(): result = deterministic_speech_from_intent({"detected_glosses": []}) assert result["subtitle"] == "No ASL words were detected yet." def test_enforce_intent_consistency_corrects_wrong_love_subtitle(): result = enforce_intent_consistency( {"detected_glosses": ["I", "LOVE", "YOU"]}, {"subtitle": "I am happy to see you.", "voice_instruction": "Speak warmly."}, ) assert result["subtitle"] == "I love you." assert "consistency_warning" in result def test_enforce_intent_consistency_corrects_wrong_where_subtitle(): result = enforce_intent_consistency( {"detected_glosses": ["where"]}, {"subtitle": "I am happy to see you.", "voice_instruction": "Speak warmly."}, ) assert result["subtitle"] == "Where?" assert result["raw_llm_subtitle"] == "I am happy to see you."