Files
minecraft-bot/tests/test_gather_wart.py
T
2026-11-07 17:44:18 +00:00

184 lines
6.0 KiB
Python

import unittest
from munch import Munch
from mosfet.commands import Commands
from mosfet.info import blocks, items
from mosfet.job import JobStates
from mosfet.jobs.gather_wart import GatherWartStates
class GatherWartTests(unittest.TestCase):
def test_nether_wart_states_are_indexed_for_patch_search(self):
self.assertTrue(blocks.NETHERWART_IDS.issubset(blocks.INDEXED_IDS))
def test_farm_wart_command_is_enabled_and_keeps_job_sequence(self):
old_wanted = items.WANTED_ITEMS
job = JobStates(Munch())
class FakeChat:
def __init__(self):
self.sent = []
def set_handler(self, _handler):
pass
def send(self, message):
self.sent.append(message)
chat = FakeChat()
g = Munch(name='mosfet1', chat=chat, job=job,
inv={0: Munch(item_id=next(iter(items.BED_IDS)))})
try:
Commands(g).handle_chat(('CHAT', 'tester',
'tester: 1farm wart'))
self.assertEqual(chat.sent, ['ok'])
self.assertIs(job.state.__func__, job.farm_wart.__func__)
self.assertIs(job.farm_wart()[0], job.gather_wart_states)
self.assertIn(items.NETHERWART_ID, items.WANTED_ITEMS)
finally:
items.WANTED_ITEMS = old_wanted
def test_finds_only_indexed_mature_wart_allowed_by_work_signs(self):
position = (5, 64, 8)
mature = max(blocks.NETHERWART_IDS)
class FakeWorld:
def __init__(self):
self.search = None
self.filter_input = None
def find_blocks_indexed(self, center, block_ids, distance):
self.search = (center, block_ids, distance)
return [position]
def filter_work_positions(self, positions):
self.filter_input = list(positions)
return list(positions)
world = FakeWorld()
state = GatherWartStates(Munch(world=world, pos=(1.5, 64, 1.5)))
state.find_new_wart()
self.assertEqual(world.search, ((1, 64, 1), {mature}, 50))
self.assertEqual(world.filter_input, [position])
self.assertEqual(state.wart, position)
self.assertIs(state.state.__func__, state.nav_to_wart.__func__)
def test_harvests_confirms_replant_on_soul_sand_below_wart(self):
position = (5, 64, 8)
mature = max(blocks.NETHERWART_IDS)
unripe = min(blocks.NETHERWART_IDS)
class FakeWorld:
def __init__(self):
self.state_id = mature
def block_at(self, *_position):
return self.state_id
class FakeGame:
def __init__(self, global_state):
self.g = global_state
self.break_calls = []
self.place_calls = []
def break_block(self, target):
self.break_calls.append(target)
return True
def select_item(self, item_ids):
self.g.holding = items.NETHERWART_ID
return items.NETHERWART_ID in item_ids
def place_block(self, target, face):
self.place_calls.append((target, face))
world = FakeWorld()
g = Munch(world=world, pos=position, holding=0, look_at=None)
game = FakeGame(g)
g.game = game
state = GatherWartStates(g)
state.wart = position
state.type_id = mature
state.break_wart()
self.assertIs(state.state.__func__, state.wait_break.__func__)
self.assertEqual(game.break_calls, [position])
world.state_id = blocks.AIR
state.wait_break()
self.assertIs(state.state.__func__, state.select_wart.__func__)
state.select_wart()
state.wait_select()
self.assertIs(state.state.__func__, state.place_wart.__func__)
state.place_wart()
support = (position[0], position[1] - 1, position[2])
self.assertEqual(game.place_calls, [(support, 1)])
world.state_id = unripe
state.wait_place()
self.assertEqual(state.completed_warts, 1)
self.assertIs(state.state.__func__, state.find_new_wart.__func__)
def test_navigation_looks_at_the_wart_target(self):
position = (5, 64, 8)
class FakeWorld:
@staticmethod
def path_to_place(_start, _target):
return [(1, 64, 1), position]
g = Munch(world=FakeWorld(), pos=(1, 64, 1), path=[], look_at=None)
state = GatherWartStates(g)
state.wart = position
state.nav_to_wart()
self.assertEqual(g.look_at, position)
self.assertEqual(g.path[-1], position)
def test_gather_wart_finishes_after_sixteen_successful_replants(self):
mature = max(blocks.NETHERWART_IDS)
unripe = min(blocks.NETHERWART_IDS)
class FakeWorld:
@staticmethod
def block_at(*_position):
return unripe
state = GatherWartStates(Munch(world=FakeWorld()))
state.wart = (5, 64, 8)
state.type_id = mature
for completed in range(1, state.WARTS_PER_BATCH):
state.wait_place()
self.assertEqual(state.completed_warts, completed)
self.assertIs(state.state.__func__, state.find_new_wart.__func__)
state.state = state.wait_place
state.wait_place()
self.assertEqual(state.completed_warts, 0)
self.assertIs(state.state.__func__, state.cleanup.__func__)
def test_unreachable_wart_is_blacklisted_without_undefined_variable(self):
position = (5, 64, 8)
class FakeWorld:
@staticmethod
def path_to_place(_start, _target):
return None
state = GatherWartStates(Munch(
world=FakeWorld(), pos=(1, 64, 1), path=[]))
state.wart = position
state.nav_to_wart()
self.assertIn(position, state.bad_warts)
self.assertIs(state.state.__func__, state.find_new_wart.__func__)
if __name__ == '__main__':
unittest.main()