"""Test that parse errors are tracked and logged""" import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).parent / "codenames")) from game.types import Team print("Testing Parse Error Tracking\n") print("="*50) # Create a mock spymaster to test parse methods without API key print("\n๐Ÿ“Œ Testing parse error tracking in parsing methods...") # Import the class from agents.spymaster.mistral_spymaster import MistralSpymaster from agents.operative.mistral_operative import MistralOperative # Create instances without calling __init__ that requires API key class TestSpymaster(MistralSpymaster): def __init__(self): # Skip parent __init__ to avoid API key requirement self.team = Team.RED self.last_parse_error = None class TestOperative(MistralOperative): def __init__(self): # Skip parent __init__ to avoid API key requirement self.team = Team.BLUE self.last_parse_error = None spy = TestSpymaster() op = TestOperative() print("โœ“ Test agents created") # Check initial state print("\n๐Ÿ“‹ Initial parse error state:") print(f" โ€ข Spymaster parse_error: {spy.last_parse_error}") print(f" โ€ข Operative parse_error: {op.last_parse_error}") # Test parse error detection print("\n๐Ÿงช Testing parse error detection...") # Test spymaster parsing with malformed JSON print("\n Testing spymaster with malformed JSON:") test_malformed = "This is not JSON at all" clue_word, number, reasoning = spy._parse_response(test_malformed, 5) print(f" โœ“ Fallback worked: clue='{clue_word}', number={number}") print(f" โœ“ Parse error recorded: {spy.last_parse_error is not None}") if spy.last_parse_error: print(f" Error: {spy.last_parse_error}") # Test with valid JSON print("\n Testing spymaster with valid JSON:") test_valid = '{"reasoning": "Test", "clue": "VALID", "number": 2}' spy.last_parse_error = None # Reset clue_word, number, reasoning = spy._parse_response(test_valid, 5) print(f" โœ“ Parsing worked: clue='{clue_word}', number={number}") print(f" โœ“ No parse error: {spy.last_parse_error is None}") # Test operative parsing with malformed JSON print("\n Testing operative with malformed JSON:") test_malformed_op = "I think the answer is CASTLE" op.last_parse_error = None # Reset guess = op._parse_response(test_malformed_op, ["CASTLE", "TOWER", "KING"]) print(f" โœ“ Fallback worked: guess='{guess}'") print(f" โœ“ Parse error recorded: {op.last_parse_error is not None}") if op.last_parse_error: print(f" Error: {op.last_parse_error}") # Test with valid JSON print("\n Testing operative with valid JSON:") test_valid_op = '{"reasoning": "Test", "guess": "TOWER"}' op.last_parse_error = None # Reset guess = op._parse_response(test_valid_op, ["CASTLE", "TOWER", "KING"]) print(f" โœ“ Parsing worked: guess='{guess}'") print(f" โœ“ No parse error: {op.last_parse_error is None}") print("\n" + "="*50) print("โœ… Parse error tracking working correctly!") print("\nWhen you play a game:") print(" โ€ข JSON parse failures are tracked in agent.last_parse_error") print(" โ€ข The game engine passes this to the logger") print(" โ€ข Game logs include 'parse_error' field when parsing fails") print(" โ€ข You can analyze which responses failed to parse")