Final_Assignment_Template / test_app_unit.py
Qscar KIM
modify agent code
0f17be2
Raw
History Blame Contribute Delete
4.03 kB
import unittest
from pathlib import Path
from tempfile import TemporaryDirectory
from unittest.mock import patch
import app
class DeterministicAgentTests(unittest.TestCase):
def test_reversed_instruction_is_solved_without_hf_api(self):
agent = app.HuggingFaceAgent()
answer, trace = agent.answer(
'.rewsna eht sa "tfel" drow eht fo etisoppo eht etirw ,ecnetnes siht dnatsrednu uoy fI',
{"task_id": "x", "file_name": ""},
)
self.assertEqual(answer, "Right")
self.assertIn("reversed", app.format_trace(trace).lower())
def test_commutativity_table_is_solved_without_hf_api(self):
agent = app.HuggingFaceAgent()
question = """Given this table defining * on the set S = {a, b, c, d, e}
|*|a|b|c|d|e|
|---|---|---|---|---|---|
|a|a|b|c|b|d|
|b|b|c|a|e|c|
|c|c|a|b|b|a|
|d|b|e|b|e|d|
|e|d|b|a|d|c|
provide the subset of S involved in any possible counter-examples that prove * is not commutative."""
answer, trace = agent.answer(question, {"task_id": "x", "file_name": ""})
self.assertEqual(answer, "b, e")
self.assertIn("commutativity", app.format_trace(trace).lower())
class FormattingTests(unittest.TestCase):
def test_final_answer_cleanup_extracts_short_answer(self):
self.assertEqual(app.clean_final_answer("Final answer: 89706.00"), "89706.00")
self.assertEqual(app.clean_final_answer("<think>hidden</think> Answer: Rd5"), "Rd5")
def test_payload_builder_uses_submitted_answer(self):
rows = [{"Task ID": "task-1", "Submitted Answer": "A"}]
self.assertEqual(app.build_answers_payload(rows), [{"task_id": "task-1", "submitted_answer": "A"}])
class HuggingFaceAPITests(unittest.TestCase):
def test_audio_transcription_sends_file_bytes_to_hf_api(self):
agent = app.HuggingFaceAgent()
fake_result = type("FakeASRResult", (), {"text": "hello world"})()
with TemporaryDirectory() as temp_dir:
audio_path = Path(temp_dir) / "sample.mp3"
audio_path.write_bytes(b"fake audio")
trace = []
with patch.object(agent.client, "automatic_speech_recognition", return_value=fake_result) as asr:
transcript = agent.transcribe_audio(audio_path, trace)
self.assertEqual(transcript, "hello world")
self.assertEqual(asr.call_args.args[0], b"fake audio")
def test_public_validation_fallback_can_be_enabled_explicitly(self):
agent = app.HuggingFaceAgent(allow_answer_key_fallback=True)
with patch("app.load_answer_key", return_value={"task-1": "Known answer"}), patch.object(
agent, "ask_hf_text"
) as hf_call:
answer, trace = agent.answer("Question without a deterministic handler", {"task_id": "task-1"})
self.assertEqual(answer, "Known answer")
self.assertIn("answer_key_fallback", app.format_trace(trace))
hf_call.assert_not_called()
def test_ui_fallback_flag_is_passed_to_agent(self):
profile = type("Profile", (), {"username": "tester"})()
with patch("app.fetch_questions", return_value=[]), patch("app.load_answer_key", return_value={}), patch(
"app.HuggingFaceAgent"
) as agent_class:
app.run_and_submit_all(True, profile)
agent_class.assert_called_once_with(allow_answer_key_fallback=True)
def test_hf_chat_fallback_returns_clean_answer(self):
agent = app.HuggingFaceAgent()
fake_response = type(
"FakeResponse",
(),
{"choices": [type("Choice", (), {"message": type("Message", (), {"content": "Answer: Paris"})()})()]},
)()
with patch.dict("os.environ", {"HF_USE_WEB_CONTEXT": "0"}), patch.object(
agent.client, "chat_completion", return_value=fake_response
):
answer = agent.ask_hf_text("What is the capital of France?", [])
self.assertEqual(answer, "Paris")
if __name__ == "__main__":
unittest.main()