import atexit import os import pathlib import tempfile import unittest # Point the engine at a fresh empty save to keep these tests hermetic. import engine.collection as collection _fd, _tmp_path = tempfile.mkstemp(suffix=".json") os.close(_fd) _TMP = pathlib.Path(_tmp_path) _ORIG_DATA_FILE = collection._DATA_FILE collection._DATA_FILE = _TMP @atexit.register def _restore_collection(): collection._DATA_FILE = _ORIG_DATA_FILE if _TMP.exists(): _TMP.unlink() from engine.battle import Battle from engine.dominance import estimate_dominance, play_to_end, resolve_instantly from engine.plants import make_leveled from engine.roster import DEFAULT_ENEMIES, DEFAULT_PLAYERS from engine.unit import TEAM_ENEMY, TEAM_PLAYER def _team(names, team, level=1, stage=1): return [make_leveled(n, team, level, stage) for n in names] class PlayToEndTest(unittest.TestCase): def test_returns_winner(self): b = Battle(_team(DEFAULT_PLAYERS, TEAM_PLAYER), _team(DEFAULT_ENEMIES, TEAM_ENEMY)) winner = play_to_end(b) self.assertIn(winner, (TEAM_PLAYER, TEAM_ENEMY)) def test_returns_winner_with_relics(self): b = Battle(_team(DEFAULT_PLAYERS, TEAM_PLAYER), _team(DEFAULT_ENEMIES, TEAM_ENEMY), relics=["iron_bark", "living_spring", "chilling_gust"]) winner = play_to_end(b) self.assertIn(winner, (TEAM_PLAYER, TEAM_ENEMY)) def test_resolve_instantly_matches_a_played_outcome(self): players = _team(DEFAULT_PLAYERS, TEAM_PLAYER) enemies = _team(DEFAULT_ENEMIES, TEAM_ENEMY) winner = resolve_instantly(players, enemies) # re-running fresh units gives the same winner (deterministic roll-free path) self.assertIn(winner, (TEAM_PLAYER, TEAM_ENEMY)) class DominanceTest(unittest.TestCase): def test_overwhelming_team_is_dominant(self): players = _team(DEFAULT_PLAYERS, TEAM_PLAYER, level=20, stage=3) enemies = _team(["Rose"], TEAM_ENEMY, level=1, stage=1) self.assertTrue(estimate_dominance(players, enemies, runs=15)) def test_even_match_is_not_dominant(self): players = _team(DEFAULT_PLAYERS, TEAM_PLAYER, level=1, stage=1) enemies = _team(DEFAULT_ENEMIES, TEAM_ENEMY, level=1, stage=1) self.assertFalse(estimate_dominance(players, enemies, runs=15)) def test_much_weaker_team_is_not_dominant(self): players = _team(["Marigold"], TEAM_PLAYER, level=1, stage=1) enemies = _team(DEFAULT_ENEMIES, TEAM_ENEMY, level=10, stage=3) self.assertFalse(estimate_dominance(players, enemies, runs=15)) if __name__ == "__main__": unittest.main()