test_priced_buy.py 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. from types import SimpleNamespace
  2. import pytest
  3. from tasks.Component.Buy import buy as buy_module
  4. from tasks.Component.Buy.buy import Buy
  5. from tasks.RichMan.mall.friendship_points import FriendshipPoints
  6. def priced_task(price):
  7. task = object.__new__(Buy)
  8. task.calls = []
  9. task._buy_read_price = lambda: price
  10. task._buy_cancel = lambda check: task.calls.append('cancel')
  11. task.buy_one = lambda *args: task.calls.append('one') or True
  12. task.buy_more = lambda start, number: task.calls.append(('more', number)) or True
  13. task._buy_more_default = lambda start: task.calls.append('default') or True
  14. return task
  15. @pytest.mark.parametrize('price,money,remain,more,expected', [
  16. (None, 500, 5, True, 'cancel'), (90, 500, 5, True, 'cancel'),
  17. (85, 85, 5, True, 'default'), (50, 100, 5, True, 'default'),
  18. (50, 250, 5, True, ('more', None)), (50, 49, 5, True, 'cancel'),
  19. (50, 49, 1, False, 'cancel'), (50, 50, 0, False, 'cancel'),
  20. (50, 50, 1, False, 'one'),
  21. ])
  22. def test_price_and_affordability_guard_both_dialogs(price, money, remain, more, expected):
  23. task = priced_task(price)
  24. assert task._buy_with_price(None, None, more, 85, money, remain) is (expected != 'cancel')
  25. assert task.calls == [expected]
  26. @pytest.mark.parametrize('balance,priced,expected', [(60, True, True), (60, False, False), (0, True, False)])
  27. def test_discounted_ticket_uses_actual_price_not_max_price_as_balance_gate(balance, priced, expected):
  28. task = object.__new__(FriendshipPoints)
  29. task.device = SimpleNamespace(image=None)
  30. task.screenshot = lambda: None
  31. task.appear = lambda asset: True
  32. task._special_check_remain = lambda asset: 5
  33. task.buy_one_or_more = lambda *args, **kwargs: kwargs['money'] == balance
  34. task.buy_one = lambda *args: pytest.fail('Unexpected single purchase')
  35. assert task.buy_mall_one(SimpleNamespace(name='ticket'), None,
  36. SimpleNamespace(ocr=lambda image: balance), 85,
  37. max_price=85 if priced else None) is expected
  38. @pytest.mark.parametrize('current', [0, 2])
  39. def test_unverified_default_quantity_cancels_instead_of_incrementing(monkeypatch, current):
  40. monkeypatch.setattr(buy_module, 'Timer', lambda *args: SimpleNamespace(
  41. start=lambda: SimpleNamespace(reached=lambda: True)))
  42. task = object.__new__(Buy)
  43. task.device = SimpleNamespace(image=None)
  44. task.O_BUY_NUMBER = SimpleNamespace(ocr=lambda image: current)
  45. calls = []
  46. task._buy_cancel = lambda check: calls.append('cancel') or True
  47. task.buy_more = lambda *args: pytest.fail('Increment fallback can exceed budget')
  48. assert task._buy_more_default(None) is False
  49. assert calls == ['cancel']
  50. @pytest.mark.parametrize('price,expected', [(0, None), (9, None), ('bad', None), (50, 50), (85, 85), (90, 90)])
  51. def test_price_ocr_rejects_partial_digits_and_excludes_jade_icon(price, expected):
  52. task = object.__new__(Buy)
  53. task.device = SimpleNamespace(image=None)
  54. task.appear = lambda asset: True
  55. task.I_BUY_JADE = SimpleNamespace(roi_front=(600, 530, 42, 52))
  56. task.O_BUY_PRICE = SimpleNamespace(roi=None, ocr=lambda image: price)
  57. assert task._buy_read_price() == expected
  58. assert task.O_BUY_PRICE.roi == [642, 530, 60, 52]
  59. @pytest.mark.parametrize('multiple', [True, False])
  60. def test_dialog_detection_routes_to_price_check(multiple):
  61. task = object.__new__(Buy)
  62. task.screenshot = lambda: None
  63. check = object()
  64. task.appear = lambda asset: (asset is task.I_BUY_PLUS and multiple) or asset is check
  65. task._buy_with_price = lambda start, image, more, price, money, remain: more == multiple
  66. assert task.buy_one_or_more(None, check, max_price=85, money=100, remain=5)