test_exploration_marker.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. """Exploration marker recognition under observed map-layout shifts."""
  2. import json
  3. from pathlib import Path
  4. import unittest
  5. from unittest.mock import patch
  6. import numpy as np
  7. from PIL import Image
  8. from module.atom import image as image_module
  9. from module.atom.image import RuleImage
  10. from tasks.GameUi.assets import GameUiAssets
  11. ROOT = Path(__file__).resolve().parents[3]
  12. class ExplorationMarkerTests(unittest.TestCase):
  13. def rule(self):
  14. item = next(item for item in json.loads(
  15. (ROOT / 'tasks/GameUi/page/image_exploration.json').read_text(encoding='utf-8'))
  16. if item['itemName'] == 'check_exploration')
  17. return RuleImage(tuple(map(int, item['roiFront'].split(','))),
  18. tuple(map(int, item['roiBack'].split(','))),
  19. item['method'], item['threshold'],
  20. './tasks/GameUi/page/' + item['imageName'])
  21. def frame(self, rule, x=1152, y=148):
  22. frame = np.full((720, 1280, 3), 100, dtype=np.uint8)
  23. height, width = rule.image.shape[:2]
  24. frame[y:y + height, x:x + width] = rule.image
  25. return frame
  26. def test_detects_both_observed_horizontal_positions(self):
  27. for x in (1152, 1166):
  28. with self.subTest(x=x):
  29. rule = self.rule()
  30. self.assertTrue(rule.match(self.frame(rule, x=x)))
  31. self.assertEqual(rule.roi_front[:2], [x, 148])
  32. def test_old_search_box_reproduces_shifted_map_failure(self):
  33. rule = self.rule()
  34. rule.roi_back = (1150, 146, 25, 26) # Original ROI plus global 2px padding.
  35. self.assertFalse(rule.match(self.frame(rule, x=1166)))
  36. def test_marker_elsewhere_does_not_classify_a_map(self):
  37. rule = self.rule()
  38. self.assertFalse(rule.match(self.frame(rule, x=900)))
  39. def test_blank_frame_does_not_classify_a_map(self):
  40. self.assertFalse(self.rule().match(np.full((720, 1280, 3), 100, dtype=np.uint8)))
  41. def test_chapter_roof_is_not_a_world_map_marker(self):
  42. # Crop at (1128, 124) from the 2026-11-13 09:23:23 timeout screenshot.
  43. with Image.open(Path(__file__).with_name('fixtures') / 'exploration_roof.png') as crop:
  44. frame = np.zeros((720, 1280, 3), dtype=np.uint8)
  45. frame[124:124 + crop.height, 1128:1128 + crop.width] = np.asarray(crop)
  46. for pad in (0, 2):
  47. with self.subTest(pad=pad), patch.object(image_module, 'ROI_MATCH_PAD', pad):
  48. self.assertFalse(self.rule().match(frame))
  49. def test_generated_asset_keeps_the_source_search_box(self):
  50. source = self.rule()
  51. self.assertEqual(GameUiAssets.I_CHECK_EXPLORATION.roi_back, source.roi_back)
  52. self.assertEqual(GameUiAssets.I_CHECK_EXPLORATION.threshold, source.threshold)
  53. if __name__ == '__main__':
  54. unittest.main()