from types import SimpleNamespace import unittest from unittest.mock import Mock from test_render_optimizations import load_definitions class DistrictTeleportTest(unittest.TestCase): def setUp(self): self.cl = SimpleNamespace(district_idx=1, district_all_finished=False, map_x=5, map_y=4, district_square_grid=dict(x1=1, y1=1, x2=8, y2=7), obstacle_instances={(1, 3): object()}, recal_npc_paths=False, get_bomb_instance=lambda x, y: [(x, y)] if (x, y) == (2, 3) else []) self.npc = SimpleNamespace(x=4, y=3, effects_front=[], set_xy=Mock()) self.hero = SimpleNamespace(x=2, y=1, district_locked=True) self.sound = Mock() class Skill: def __init__(self, user, to, skill_instances): self.user, self.to = user, to self.effect_instances = [] self.choose = Mock(side_effect=lambda points: points[-1]) namespace = load_definitions("client_local/skill/parametric2_skill.py", { "Skill": Skill, "level": SimpleNamespace(current_level=self.cl), "random": SimpleNamespace(choice=self.choose), "EffectInstance": Mock(), "sound_player": self.sound, }, classes={"CustomRandomInDistrictTeleportEscape": None, "CustomRandomInDistrictTeleportEscapeClosest": None}) self.teleport = namespace["CustomRandomInDistrictTeleportEscape"] self.teleport_closest = namespace["CustomRandomInDistrictTeleportEscapeClosest"] def cast(self): self.teleport(self.npc, self.hero, {"avoid_range": 1}, []) def test_uses_active_offline_district_and_filters_landing_tiles(self): self.cast() candidates, = self.choose.call_args.args self.assertEqual(set(candidates), {(3, 3), (4, 1), (4, 2)}) self.npc.set_xy.assert_called_once_with(4, 2) self.assertTrue(self.cl.recal_npc_paths) self.sound.play.assert_called_once_with("X08_01") def test_does_not_cast_before_entering_or_after_finishing_or_outside_district(self): self.cl.district_idx = 0 self.cast() self.cl.district_idx, self.hero.district_locked = 1, False self.cast() self.hero.district_locked, self.cl.district_all_finished = True, True self.cast() self.cl.district_all_finished, self.hero.x = False, 0 self.cast() self.choose.assert_not_called() self.npc.set_xy.assert_not_called() def test_closest_randomizes_only_over_equally_near_legal_tiles(self): self.cl.map_x = self.cl.map_y = 7 self.cl.district_square_grid = dict(x1=1, y1=1, x2=6, y2=6) self.cl.obstacle_instances = {(1, 3): object()} self.cl.get_bomb_instance = lambda x, y: [(x, y)] if (x, y) == (3, 5) else [] self.hero.x = self.hero.y = 3 self.npc.x = self.npc.y = 6 self.teleport_closest(self.npc, self.hero, {"avoid_range": 1}, []) candidates, = self.choose.call_args.args self.assertEqual(set(candidates), {(3, 1), (5, 3)}) self.npc.set_xy.assert_called_once_with(5, 3) self.cl.obstacle_instances.update({(3, 1): object(), (5, 3): object()}) self.teleport_closest(self.npc, self.hero, {"avoid_range": 1}, []) candidates, = self.choose.call_args.args self.assertEqual(set(candidates), {(1, 2), (1, 4), (2, 1), (2, 5), (4, 1), (4, 5), (5, 2), (5, 4)})