Fix gift item instance IDs and per-monster cooldown scheduling
Use bag instance IDs required by the client protocol. Apply confirmed feel and giftDate values across stale queries, skip cooling targets, and schedule ready gifts independently of random polling. Replan cultivation locally and retain local strategy errors in diagnostics.
This commit is contained in:
@@ -0,0 +1,158 @@
|
||||
import copy
|
||||
import os
|
||||
import time
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
os.environ.setdefault("QT_QPA_PLATFORM", "offscreen")
|
||||
from PySide6 import QtWidgets as W
|
||||
from PySide6.QtTest import QTest
|
||||
|
||||
from ranch_assistant.engine import Engine, demo_snapshot
|
||||
from ranch_assistant.gifts import GiftPlanner
|
||||
from ranch_assistant.gui import Window
|
||||
from ranch_assistant.model import Catalog, Policy, Snapshot
|
||||
from ranch_assistant.tests.test_core import workspace_temp
|
||||
|
||||
NOW = 1_800_000_000
|
||||
GIFT = "monsterService/monsterSendGift"
|
||||
|
||||
|
||||
def gift_snapshot(catalog):
|
||||
raw = copy.deepcopy(demo_snapshot(catalog).raw)
|
||||
first = copy.deepcopy(next(m for m in raw["monsters"] if m["process"] == 3))
|
||||
first.update(id=1, feel=0, giftTime=0, entangleLevel=0)
|
||||
second = first | {"id": 2}
|
||||
raw.update(monsters=[first, second], stacks=[], teams=[],
|
||||
items=[{"id": 10, "typeId": 30005, "num": 10}])
|
||||
return Snapshot(raw)
|
||||
|
||||
|
||||
def gift_policy():
|
||||
return Policy(enabled=False, gift_enabled=True, gift_liked_only=False,
|
||||
gift_targets={"1": 1, "2": 2}, gift_items={"30005": {"enabled": True, "reserve": 0}})
|
||||
|
||||
|
||||
class GiftClient:
|
||||
def __init__(self, catalog):
|
||||
self.raw = gift_snapshot(catalog).raw
|
||||
self.calls = []
|
||||
self.response = {"feel": 10, "giftDate": NOW * 1000, "addFeel": 10}
|
||||
|
||||
def snapshot(self):
|
||||
# A successful query can briefly lag behind the confirmed gift result.
|
||||
return Snapshot(copy.deepcopy(self.raw))
|
||||
|
||||
def call(self, path, payload):
|
||||
self.calls.append((path, payload))
|
||||
if path != GIFT:
|
||||
raise AssertionError(path)
|
||||
return self.response
|
||||
|
||||
|
||||
class GiftTimingTests(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.catalog = Catalog()
|
||||
|
||||
def test_gift_request_uses_bag_instance_id_instead_of_item_type(self):
|
||||
snapshot = gift_snapshot(self.catalog)
|
||||
action = GiftPlanner(self.catalog).plan(snapshot, gift_policy())[0]
|
||||
self.assertEqual(action.payload["itemId"], 10)
|
||||
self.assertNotEqual(action.payload["itemId"], 30005)
|
||||
|
||||
def test_success_response_updates_each_monster_despite_stale_queries(self):
|
||||
with patch("ranch_assistant.model.time.time", return_value=NOW):
|
||||
client = GiftClient(self.catalog)
|
||||
engine = Engine(self.catalog, client)
|
||||
engine.policy = gift_policy()
|
||||
engine.auto = True
|
||||
first, _, _ = engine.poll()
|
||||
self.assertEqual(first.monsters[1]["feel"], 10)
|
||||
self.assertEqual(first.monsters[1]["giftTime"], NOW)
|
||||
engine.poll()
|
||||
engine.poll()
|
||||
self.assertEqual([payload["monsterId"] for _, payload in client.calls], [1, 2])
|
||||
|
||||
def test_next_due_uses_earliest_eligible_cooldown_and_ignores_full_targets(self):
|
||||
with patch("ranch_assistant.model.time.time", return_value=NOW):
|
||||
snapshot = gift_snapshot(self.catalog)
|
||||
snapshot.monsters[1]["giftTime"] = (NOW - 7200 + 120) * 1000
|
||||
snapshot.monsters[2]["giftTime"] = (NOW - 7200 + 15) * 1000
|
||||
planner = GiftPlanner(self.catalog)
|
||||
self.assertEqual(planner.plan(snapshot, gift_policy()), [])
|
||||
self.assertEqual(planner.next_ready_at, NOW + 15)
|
||||
snapshot.monsters[2]["feel"] = self.catalog.max_feel(snapshot.monsters[2])
|
||||
planner.plan(snapshot, gift_policy())
|
||||
self.assertEqual(planner.next_ready_at, NOW + 120)
|
||||
snapshot.raw["items"][0]["num"] = 0
|
||||
planner.plan(snapshot, gift_policy())
|
||||
self.assertIsNone(planner.next_ready_at)
|
||||
|
||||
def test_unconfirmed_gift_result_stops_before_another_write(self):
|
||||
with patch("ranch_assistant.model.time.time", return_value=NOW):
|
||||
client = GiftClient(self.catalog)
|
||||
client.response = {"feel": 10}
|
||||
engine = Engine(self.catalog, client)
|
||||
engine.policy = gift_policy()
|
||||
engine.auto = True
|
||||
with self.assertRaisesRegex(ValueError, "feel / giftDate"):
|
||||
engine.poll()
|
||||
self.assertFalse(engine.auto)
|
||||
engine.poll()
|
||||
self.assertEqual(len(client.calls), 1)
|
||||
|
||||
|
||||
class GiftTimingGuiTests(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.app = W.QApplication.instance() or W.QApplication([])
|
||||
|
||||
def wait(self, window):
|
||||
for _ in range(600):
|
||||
self.app.processEvents()
|
||||
QTest.qWait(2)
|
||||
if not window.busy:
|
||||
return
|
||||
self.fail("Gift worker did not finish")
|
||||
|
||||
def test_gifts_continue_without_waiting_for_poll_and_stop_at_each_cooldown(self):
|
||||
with workspace_temp() as folder, patch("ranch_assistant.model.time.time", return_value=NOW):
|
||||
window = Window(folder)
|
||||
window.timer.stop()
|
||||
try:
|
||||
self.wait(window)
|
||||
window.engine = Engine(window.catalog)
|
||||
window.engine.snapshot = gift_snapshot(window.catalog)
|
||||
policy = gift_policy()
|
||||
window.ranch_enabled.setChecked(False)
|
||||
window.gift_enabled.setChecked(True)
|
||||
window.gift_liked_only.setChecked(False)
|
||||
window.gift_targets = policy.gift_targets
|
||||
window.gift_items = policy.gift_items
|
||||
window.monitoring = True
|
||||
window.set_strategy_checked(True)
|
||||
with patch.object(window, "poll_delay", return_value=300):
|
||||
window.refresh(automated=True)
|
||||
self.wait(window)
|
||||
self.assertGreater(window.next_poll, time.monotonic() + 250)
|
||||
window.schedule()
|
||||
self.wait(window)
|
||||
self.assertTrue(all(m["giftTime"] > 0 for m in window.engine.snapshot.raw["monsters"]))
|
||||
self.assertGreater(window.next_gift, time.monotonic() + 7100)
|
||||
window.schedule()
|
||||
self.assertFalse(window.busy)
|
||||
due = window.next_gift
|
||||
window.next_poll = float("inf")
|
||||
with patch("ranch_assistant.model.time.time", return_value=NOW + 7201), patch("ranch_assistant.gui.time.monotonic", return_value=due + 1):
|
||||
window.schedule()
|
||||
self.wait(window)
|
||||
self.assertTrue(any(m["giftTime"] > NOW for m in window.engine.snapshot.raw["monsters"]))
|
||||
window.stop_workflow()
|
||||
self.assertEqual(window.next_gift, float("inf"))
|
||||
self.assertTrue(window.monitoring)
|
||||
finally:
|
||||
window.close()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user