|
| 1 | +"""BattleState 截图回归测试。 |
| 2 | +
|
| 3 | +用实际游戏截图验证 detect_battle_state 在各场景下返回正确的状态。 |
| 4 | +截图放在 tests/replay/cases/battle_state_screenshots/ 下。 |
| 5 | +""" |
| 6 | + |
| 7 | +from __future__ import annotations |
| 8 | + |
| 9 | +from pathlib import Path |
| 10 | + |
| 11 | +import numpy as np |
| 12 | +import pytest |
| 13 | +from PIL import Image |
| 14 | + |
| 15 | +from aao.core.timing.battle_state import BattleState, detect_battle_state |
| 16 | + |
| 17 | +SCREENSHOTS_DIR = Path(__file__).parent / "replay" / "cases" / "battle_state_screenshots" |
| 18 | + |
| 19 | + |
| 20 | +def _load_bgr(name: str) -> np.ndarray: |
| 21 | + """加载 PNG → BGR (H, W, 3) uint8。""" |
| 22 | + rgb = np.asarray(Image.open(SCREENSHOTS_DIR / f"{name}.png").convert("RGB"), dtype=np.uint8) |
| 23 | + return rgb[:, :, ::-1].copy() |
| 24 | + |
| 25 | + |
| 26 | +# 场景 → 期望的 BattleState |
| 27 | +EXPECTED_STATES = { |
| 28 | + "in_battle_running": BattleState.IN_BATTLE, |
| 29 | + "in_battle_paused": BattleState.IN_BATTLE, |
| 30 | + "title_screen": BattleState.BATTLE_BEGIN, |
| 31 | + "settlement": BattleState.NOT_IN_BATTLE, |
| 32 | + "main_menu": BattleState.NOT_IN_BATTLE, |
| 33 | + "deployment": BattleState.NOT_IN_BATTLE, |
| 34 | +} |
| 35 | + |
| 36 | + |
| 37 | +def _make_parametrize(): |
| 38 | + """生成参数化测试列表(无截图时 skip)。""" |
| 39 | + cases = [] |
| 40 | + for name, expected in EXPECTED_STATES.items(): |
| 41 | + png = SCREENSHOTS_DIR / f"{name}.png" |
| 42 | + if png.exists(): |
| 43 | + cases.append((name, expected)) |
| 44 | + return cases |
| 45 | + |
| 46 | + |
| 47 | +_CASES = _make_parametrize() |
| 48 | + |
| 49 | + |
| 50 | +@pytest.mark.parametrize("name,expected", _CASES, ids=[c[0] for c in _CASES]) |
| 51 | +def test_battle_state_screenshot(name: str, expected: BattleState) -> None: |
| 52 | + """截图场景回归:验证 detect_battle_state 返回正确状态。""" |
| 53 | + frame = _load_bgr(name) |
| 54 | + actual = detect_battle_state(frame) |
| 55 | + assert actual is expected, f"{name}: expected {expected.value}, got {actual.value}" |
| 56 | + |
| 57 | + |
| 58 | +def test_has_screenshots_or_skip() -> None: |
| 59 | + """无截图时不 fail,仅提示。""" |
| 60 | + if not _CASES: |
| 61 | + pytest.skip("无截图:请按 battle_state_screenshots/ 添加素材") |
0 commit comments