test_script_ocr_recovery.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. from pathlib import Path
  2. from types import SimpleNamespace
  3. from unittest.mock import Mock
  4. import pytest
  5. import script as script_module
  6. from module.exception import GameOcrError, TaskEnd
  7. @pytest.mark.parametrize('save_fails', [False, True])
  8. @pytest.mark.parametrize('restart_enabled', [False, True])
  9. def test_ocr_error_runs_restart_even_when_scheduled_restart_is_disabled(
  10. monkeypatch, save_fails, restart_enabled):
  11. script = object.__new__(script_module.Script)
  12. script.instance_guard = None
  13. script._try_acquire_queue_token = Mock(return_value=True)
  14. script.device = SimpleNamespace(screenshot=Mock())
  15. script.config = SimpleNamespace(
  16. model=SimpleNamespace(running_task='RealmRaid'),
  17. restart=SimpleNamespace(scheduler=SimpleNamespace(enable=restart_enabled)),
  18. task_call=Mock(side_effect=AssertionError('Recovery must not depend on scheduling')),
  19. )
  20. script.save_error_log = Mock(side_effect=OSError('Disk unavailable') if save_fails else None)
  21. commands = []
  22. def load_module(name, path):
  23. command = Path(path).parent.name
  24. class Task:
  25. def __init__(self, config, device):
  26. assert config.model.running_task == command
  27. def run(self):
  28. commands.append(command)
  29. if command == 'RealmRaid':
  30. raise GameOcrError('Ticket OCR exhausted')
  31. assert command == 'Restart'
  32. raise TaskEnd
  33. return SimpleNamespace(ScriptTask=Task)
  34. monkeypatch.setattr(script_module, 'load_module', load_module)
  35. assert script.run('RealmRaid') is False
  36. assert commands == ['RealmRaid', 'Restart']
  37. assert script.config.model.running_task == 'RealmRaid'
  38. assert script.config.restart.scheduler.enable is restart_enabled
  39. script.save_error_log.assert_called_once_with()