import numpy as np from engine.game.game_state import GameState from engine.models.ability import Ability, Effect, EffectType, TargetType, TriggerType from engine.models.context_indices import ContextIndex from engine.models.opcodes import Opcode def test_ability_compilation(): """Test that Ability.compile() produces expected bytecode.""" # Ability: On Play: Draw 2 ab = Ability( raw_text="Draw 2", trigger=TriggerType.ON_PLAY, effects=[Effect(EffectType.DRAW, value=2, target=TargetType.SELF)], ) bytecode = ab.compile() # Expected: [DRAW, 2, 0, TargetType.SELF] + [RETURN, 0, 0, 0] assert len(bytecode) % 4 == 0 assert bytecode[0] == Opcode.DRAW assert bytecode[1] == 2 assert bytecode[3] == int(TargetType.SELF) assert bytecode[-4] == Opcode.RETURN def test_opcode_resolution_draw(): """Test _resolve_effect_opcode with DRAW opcode and flat context.""" gs = GameState() p = gs.active_player # Setup deck p.main_deck = [1, 2, 3, 4, 5] initial_hand_size = len(p.hand) # Prepare Context ctx = np.zeros(64, dtype=np.int32) ctx[ContextIndex.VALUE] = 2 # Execute Opcode seg = np.array([Opcode.DRAW, 2, 0, 0], dtype=np.int32) gs._resolve_effect_opcode(Opcode.DRAW, seg=seg, context=ctx) # Assert assert len(p.hand) == initial_hand_size + 2 assert len(p.main_deck) == 3 def test_opcode_resolution_draw_legacy_context(): """Test _resolve_effect_opcode with legacy dict context.""" gs = GameState() p = gs.active_player # Setup deck p.main_deck = [1, 2, 3, 4, 5] initial_hand_size = len(p.hand) # Legacy Context ctx = {"value": 1} # Execute Opcode seg = np.array([Opcode.DRAW, 1, 0, 0], dtype=np.int32) gs._resolve_effect_opcode(Opcode.DRAW, seg=seg, context=ctx) # Assert assert len(p.hand) == initial_hand_size + 1 if __name__ == "__main__": try: test_ability_compilation() print("test_ability_compilation PASS") test_opcode_resolution_draw() print("test_opcode_resolution_draw PASS") test_opcode_resolution_draw_legacy_context() print("test_opcode_resolution_draw_legacy_context PASS") print("ALL TESTS PASSED") except Exception as e: print(f"TEST FAILED: {e}") import traceback traceback.print_exc()