import io import time import unittest from contextlib import redirect_stdout from unittest.mock import patch from munch import Munch from mosfet.info import blocks, items from mosfet.jobs.gather_crop import GatherCropStates from mosfet.protocol.types import Slot class CropBatchTests(unittest.TestCase): def test_navigation_looks_at_the_crop_target(self): crop = (5, 64, 8) class FakeWorld: @staticmethod def path_to_place(_start, _target): return [(1, 64, 1), crop] g = Munch(world=FakeWorld(), pos=(1, 64, 1), path=[], look_at=None) state = GatherCropStates(g) state.crop = crop state.nav_to_crop() self.assertEqual(g.look_at, crop) self.assertEqual(g.path[-1], crop) def test_found_crop_log_includes_search_duration(self): crop = (1, 64, 1) mature = blocks.MATURE_WHEAT_ID class FakeWorld: @staticmethod def find_blocks_indexed(_center, _block_ids, _distance, sort=True): return [crop] @staticmethod def filter_work_positions(positions): return list(positions) @staticmethod def block_at(*_position): return mature state = GatherCropStates(Munch( world=FakeWorld(), pos=(0, 64, 0))) output = io.StringIO() clock = [10.0] def monotonic(): clock[0] += 0.1 return clock[0] with patch('mosfet.jobs.gather_crop.time.monotonic', side_effect=monotonic), redirect_stdout(output): state.find_new_crop() self.assertIn('Found crop after 0.90s:', output.getvalue()) self.assertIn('Crop search timings:', output.getvalue()) self.assertIn('ripe-index 0.100s (1/1 allowed)', output.getvalue()) self.assertIn('all-crops+types 0.100s (1/1 allowed)', output.getvalue()) self.assertIn('section-select 0.100s (1 ripe, 1 crops, selected section 1)', output.getvalue()) def test_selection_rechecks_maturity_after_candidate_search(self): position = (1, 64, 1) unripe = blocks.MCD_BLOCKS['wheat']['minStateId'] class FakeWorld: @staticmethod def find_blocks_indexed(_center, block_ids, _distance, sort=True): return [position] @staticmethod def filter_work_positions(positions): return list(positions) @staticmethod def block_at(*_position): return unripe state = GatherCropStates(Munch( world=FakeWorld(), pos=(0, 64, 0))) state.find_new_crop() self.assertIs(state.state.__func__, state.find_new_crop.__func__) self.assertIsNone(state.crop) def test_break_skips_crop_that_became_unripe_on_the_way(self): position = (1, 64, 1) unripe = blocks.MCD_BLOCKS['wheat']['minStateId'] mature = blocks.MATURE_WHEAT_ID class FakeWorld: @staticmethod def block_at(*_position): return unripe class FakeGame: def __init__(self): self.break_calls = [] def break_block(self, target): self.break_calls.append(target) return True game = FakeGame() state = GatherCropStates(Munch(world=FakeWorld(), game=game)) state.crop = position state.type_id = mature state.break_crop() self.assertEqual(game.break_calls, []) self.assertIs(state.state.__func__, state.find_new_crop.__func__) def test_seed_selection_forces_slot_and_checks_inventory_hand(self): position = (1, 64, 1) seed = items.WHEAT_SEEDS_ID class FakeGame: def __init__(self): self.placements = [] self.selections = [] def select_item(self, item_ids, force=False): self.selections.append((set(item_ids), force)) return True def place_block(self, location, face, hand=0): self.placements.append((location, face, hand)) game = FakeGame() state = GatherCropStates(Munch( game=game, holding=seed, holding_hand=1, inv={45: Slot(True, seed, 1)})) state.crop = position state.type_id = blocks.MCD_BLOCKS['wheat']['minStateId'] state.select_seed() self.assertEqual(game.selections, [({seed}, True)]) self.assertIs(state.state.__func__, state.wait_select.__func__) state.wait_select() self.assertIs(state.state.__func__, state.place_crop.__func__) state.place_crop() self.assertEqual(game.placements, [((1, 63, 1), 1, 1)]) def test_wait_select_rejects_stale_local_held_item(self): seed = items.WHEAT_SEEDS_ID other_item = next(iter(items.AXE_IDS)) state = GatherCropStates(Munch( holding=seed, holding_hand=0, holding_slot=0, inv={36: Slot(True, other_item, 1)})) state.target_seed = seed state.seed_deadline = time.monotonic() + 5.0 state.state = state.wait_select state.wait_select() self.assertIs(state.state.__func__, state.wait_select.__func__) def test_seed_selection_uses_crop_species_for_any_age_state(self): unripe_wheat = blocks.MCD_BLOCKS['wheat']['minStateId'] class FakeGame: def __init__(self): self.selected = None def select_item(self, item_ids, force=False): self.selected = set(item_ids) self.force = force return True game = FakeGame() state = GatherCropStates(Munch(game=game)) state.type_id = unripe_wheat state.select_seed() self.assertEqual(state.target_seed, items.WHEAT_SEEDS_ID) self.assertEqual(game.selected, {items.WHEAT_SEEDS_ID}) def test_gather_crop_finishes_after_sixteen_successful_replants(self): unripe_wheat = blocks.MCD_BLOCKS['wheat']['minStateId'] mature_wheat = blocks.MATURE_WHEAT_ID self.assertNotEqual(unripe_wheat, mature_wheat) class FakeWorld: @staticmethod def block_at(*_position): return unripe_wheat state = GatherCropStates(Munch(world=FakeWorld())) state.crop = (1, 64, 1) state.type_id = mature_wheat for completed in range(1, state.CROPS_PER_BATCH): state.wait_place() self.assertEqual(state.completed_crops, completed) self.assertIs(state.state.__func__, state.find_new_crop.__func__) state.state = state.wait_place state.wait_place() self.assertEqual(state.completed_crops, 0) self.assertIs(state.state.__func__, state.cleanup.__func__) if __name__ == '__main__': unittest.main()