Files
minecraft-bot/tests/test_cache_items.py
T

135 lines
4.3 KiB
Python

import unittest
from munch import Munch
from minecraft.networking.types import BlockFace
from mosfet.info import items
from mosfet.jobs.cache_items import CacheItemsStates
class CacheItemsTests(unittest.TestCase):
def test_successful_labeled_transfer_skips_placing_another_cache_chest(self):
item_id = items.get_id('stone')
stack = Munch(present=True, item_id=item_id, item_count=16)
window = Munch(container_slots=27, contents={
**dict.fromkeys(range(27)), 27: stack,
})
clicks = []
class FakeGame:
@staticmethod
def count_window_slots():
return 0
@staticmethod
def click_window(*args):
clicks.append(args)
class FakeWorld:
@staticmethod
def find_blocks_indexed(*_args):
return []
@staticmethod
def find_labeled_chests(*_args):
return []
g = Munch(
game=FakeGame(), world=FakeWorld(), pos=(0, 64, 0),
inv={}, window=window, item_lock=False)
state = CacheItemsStates(g)
state.is_labeled_container = True
state.container_item_ids = {item_id}
state.move_items()
self.assertEqual(len(clicks), 1)
self.assertFalse(state.transferred_to_labeled_chest)
window.contents[27] = Munch(present=False, item_id=0, item_count=0)
g.item_lock = False
state.pending_transfer['confirm_after'] = 0
state.move_items()
self.assertTrue(state.transferred_to_labeled_chest)
state.labeled_chests = []
state.trapped_chests = []
state.choose_trapped_chest()
self.assertIs(state.state.__func__, state.cleanup.__func__)
def test_mutually_reachable_opening_is_skipped_after_path_failure(self):
start = (0, 64, 0)
first = (4, 64, 0)
nearby = (4, 64, 1)
item_id = items.get_id('stone')
calls = []
class FakeWorld:
@staticmethod
def find_cache_openings(area, _center):
position = first if area == (10, 64, 0) else nearby
return [{'position': position}]
def path_to_place(self, origin, target):
origin, target = tuple(origin), tuple(target)
calls.append((origin, target))
if origin == start and target == first:
return None
if (origin, target) in ((nearby, first), (first, nearby)):
return [origin, target]
return None
state = CacheItemsStates(Munch(
world=FakeWorld(), pos=start,
inv={9: Munch(present=True, item_id=item_id)}))
state.labeled_chests = [
{'position': (10, 64, 0), 'positions': ((10, 64, 0),),
'item_ids': {item_id}, 'labels': {'Stone'}},
{'position': (11, 64, 0), 'positions': ((11, 64, 0),),
'item_ids': {item_id}, 'labels': {'Stone'}},
]
state.trapped_chests = []
state.choose_trapped_chest()
self.assertEqual(calls, [
(start, first), (nearby, first), (first, nearby),
])
self.assertIn(first, state.bad_openings)
self.assertIn(nearby, state.bad_openings)
def test_one_way_reachability_does_not_blacklist_opening(self):
bad = (1, 64, 0)
candidate = (2, 64, 0)
class FakeWorld:
@staticmethod
def path_to_place(start, target):
return [start, target] if tuple(start) == candidate else None
state = CacheItemsStates(Munch())
state.bad_openings = {bad}
self.assertFalse(state._opening_in_bad_region(FakeWorld(), candidate))
self.assertNotIn(candidate, state.bad_openings)
def test_cache_chest_placement_sneaks_to_avoid_opening_support_container(self):
calls = []
class FakeGame:
@staticmethod
def place_block(*args, **kwargs):
calls.append((args, kwargs))
state = CacheItemsStates(Munch(game=FakeGame()))
state.area = (3, 65, 7)
state.place_chest()
self.assertEqual(calls, [(
((3, 64, 7), BlockFace.TOP), {'sneaking': True},
)])
self.assertIs(state.state.__func__, state.open_chest.__func__)
if __name__ == '__main__':
unittest.main()