#!/usr/bin/env python3 """Tests for engine_lib/validation.py.""" import sys import os import json sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) from unittest.mock import patch, MagicMock def test_empty_action(): """Empty action should return (True, '').""" from engine_lib.validation import validate_action valid, reason = validate_action("") assert valid is True assert reason == "" print("✓ empty action returns (True, '')") @patch("engine_lib.validation.state.read_file") @patch("engine_lib.validation.state.truncate_world") @patch("engine_lib.validation.call_llm") def test_valid_action(mock_call_llm, mock_truncate_world, mock_read_file): from engine_lib.validation import validate_action mock_read_file.side_effect = lambda p: "HP: 10\nGold: 5" if "character" in str(p).lower() else "## Location\nTavern" mock_truncate_world.return_value = "## Location\nTavern" mock_call_llm.return_value = json.dumps({"valid": True, "reason": "ok"}) valid, reason = validate_action("I buy a drink", story="At the tavern", log="- Entered the tavern") assert valid is True assert reason == "ok" mock_call_llm.assert_called_once() print("✓ valid action returns (True, reason)") @patch("engine_lib.validation.state.read_file") @patch("engine_lib.validation.state.truncate_world") @patch("engine_lib.validation.call_llm") def test_invalid_action(mock_call_llm, mock_truncate_world, mock_read_file): from engine_lib.validation import validate_action mock_read_file.side_effect = lambda p: "HP: 10\nGold: 0" if "character" in str(p).lower() else "## Location\nTavern" mock_truncate_world.return_value = "## Location\nTavern" mock_call_llm.return_value = json.dumps({"valid": False, "reason": "Not enough gold"}) valid, reason = validate_action("I buy a drink", story="At the tavern", log="- Entered the tavern") assert valid is False assert reason == "Not enough gold" print("✓ invalid action returns (False, reason)") @patch("engine_lib.validation.state.read_file") @patch("engine_lib.validation.state.truncate_world") @patch("engine_lib.validation.call_llm") def test_llm_returns_none(mock_call_llm, mock_truncate_world, mock_read_file): from engine_lib.validation import validate_action mock_read_file.side_effect = lambda p: "HP: 10" if "character" in str(p).lower() else "## Location\nTavern" mock_truncate_world.return_value = "## Location\nTavern" mock_call_llm.return_value = None valid, reason = validate_action("I attack the dragon", story="A dragon appears!", log="- Dragon spotted") assert valid is False assert reason == "Not sure" print("✓ LLM returning None gives (False, 'Not sure')") @patch("engine_lib.validation.state.read_file") @patch("engine_lib.validation.state.truncate_world") @patch("engine_lib.validation.call_llm") def test_llm_returns_bad_json(mock_call_llm, mock_truncate_world, mock_read_file): from engine_lib.validation import validate_action mock_read_file.side_effect = lambda p: "HP: 10" if "character" in str(p).lower() else "## Location\nTavern" mock_truncate_world.return_value = "## Location\nTavern" mock_call_llm.return_value = "not valid json at all" valid, reason = validate_action("I cast a spell", story="In a dungeon", log="- Found a weird altar") assert valid is False assert reason == "Unrecognized" print("✓ bad JSON from LLM gives (False, 'Unrecognized')") @patch("engine_lib.validation.state.read_file") @patch("engine_lib.validation.state.truncate_world") def test_missing_character_sheet(mock_truncate_world, mock_read_file): from engine_lib.validation import validate_action mock_read_file.return_value = "" mock_truncate_world.return_value = "*No world state.*" with patch("engine_lib.validation.call_llm") as mock_call_llm: mock_call_llm.return_value = json.dumps({"valid": True, "reason": "ok"}) valid, reason = validate_action("I look around", story="In a dark room", log="- Entered the room") assert valid is True print("✓ handles missing character sheet gracefully") @patch("engine_lib.validation.state.read_file") @patch("engine_lib.validation.state.truncate_world") @patch("engine_lib.validation.call_llm") def test_on_debug_called(mock_call_llm, mock_truncate_world, mock_read_file): from engine_lib.validation import validate_action mock_read_file.side_effect = lambda p: "HP: 10" if "character" in str(p).lower() else "## Location\nTavern" mock_truncate_world.return_value = "## Location\nTavern" mock_call_llm.return_value = json.dumps({"valid": True, "reason": "ok"}) events = [] def debug_cb(key, data): events.append((key, data)) valid, reason = validate_action("I open the door", story="In a hallway", log="- Heard noises", on_debug=debug_cb) assert valid is True assert len(events) == 1 assert events[0][0] == "action_validation" assert events[0][1]["valid"] is True print("✓ on_debug callback receives action_validation event") if __name__ == "__main__": test_empty_action() test_valid_action() test_invalid_action() test_llm_returns_none() test_llm_returns_bad_json() test_missing_character_sheet() test_on_debug_called() print("\n✓ All validation tests passed")