Spaces:
Running
Running
File size: 2,470 Bytes
bb3fbf9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
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()
|