Add hatch retention, automatic breakthroughs and exploration
This commit is contained in:
@@ -0,0 +1,157 @@
|
||||
"""Repeated native Qt rendering, manual drafts and account isolation."""
|
||||
import os
|
||||
os.environ.setdefault("QT_QPA_PLATFORM", "offscreen")
|
||||
|
||||
from dataclasses import replace
|
||||
import copy
|
||||
import time
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
from PySide6 import QtCore as C, QtWidgets as W
|
||||
from PySide6.QtTest import QTest
|
||||
|
||||
from ranch_assistant import gui
|
||||
from ranch_assistant.exploration import ExplorePlanner
|
||||
from ranch_assistant.tests.test_core import workspace_temp
|
||||
from ranch_assistant.tests.test_exploration import AREAS, make_snapshot, policy
|
||||
|
||||
|
||||
class ExplorationGuiTests(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.app = W.QApplication.instance() or W.QApplication([])
|
||||
|
||||
def wait(self, window, predicate=lambda: True, timeout=8):
|
||||
deadline = time.monotonic() + timeout
|
||||
while time.monotonic() < deadline:
|
||||
self.app.processEvents()
|
||||
QTest.qWait(5)
|
||||
if (not window.busy and not window.exploration_table.row_batch.running
|
||||
and not window._exploration_member_busy and predicate()):
|
||||
return
|
||||
self.fail("Offline exploration Qt job did not finish")
|
||||
|
||||
def setUp(self):
|
||||
self.folder = self.enterContext(workspace_temp())
|
||||
self.qt_messages = []
|
||||
self.qt_handler = C.qInstallMessageHandler(lambda kind, context, message: self.qt_messages.append(message))
|
||||
self.window = gui.Window(self.folder)
|
||||
self.window.timer.stop()
|
||||
self.window.auto_save_timer.stop()
|
||||
self.clock, self.snapshot = make_snapshot()
|
||||
self.clock.start()
|
||||
self.addCleanup(self.clock.stop)
|
||||
self.window.engine.snapshot = self.snapshot
|
||||
self.window.engine.explore_planner = ExplorePlanner(self.window.catalog, areas=AREAS)
|
||||
self.window.engine.explore_policy = policy()
|
||||
self.window.apply_explore_policy(policy())
|
||||
self.window.presentation_active = True
|
||||
self.window.pending_exploration_view = False
|
||||
self.window.tabs.setCurrentWidget(self.window.exploration_page)
|
||||
|
||||
def tearDown(self):
|
||||
self.window.pool.clear()
|
||||
self.window.pool.waitForDone(3000)
|
||||
self.window.close()
|
||||
self.app.processEvents()
|
||||
C.qInstallMessageHandler(self.qt_handler)
|
||||
self.assertFalse([message for message in self.qt_messages if "Traceback" in message or "QTableWidgetItem" in message], self.qt_messages)
|
||||
|
||||
def controls(self):
|
||||
return self.window.exploration_table._controls["1"]
|
||||
|
||||
def test_real_window_three_selections_and_busy_policy_queue(self):
|
||||
window = self.window
|
||||
window.refresh_exploration()
|
||||
self.wait(window, lambda: bool(window._exploration_member_options))
|
||||
control = self.controls()
|
||||
window.busy = True
|
||||
before = window.engine.explore_policy.to_dict()
|
||||
saved = []
|
||||
with patch.object(window, "settings_changed", side_effect=lambda: saved.append(window.read_explore_policy().to_dict())):
|
||||
control["members"][0].setCurrentIndex(control["members"][0].findData(1))
|
||||
self.assertEqual(window.read_explore_policy().teams["1"].monsters, [])
|
||||
self.assertEqual(window.engine.explore_policy.to_dict(), before)
|
||||
for _ in range(5):
|
||||
self.app.processEvents()
|
||||
QTest.qWait(2)
|
||||
self.assertEqual(window._exploration_drafts["1"], [1, None, None])
|
||||
control["members"][1].setCurrentIndex(control["members"][1].findData(2))
|
||||
self.assertEqual(window._exploration_drafts["1"][:2], [1, 2])
|
||||
self.assertEqual(window.read_explore_policy().teams["1"].monsters, [])
|
||||
control["members"][2].setCurrentIndex(control["members"][2].findData(3))
|
||||
self.assertEqual(window.read_explore_policy().teams["1"].monsters, [1, 2, 3])
|
||||
self.assertNotIn("1", window._exploration_drafts)
|
||||
# The module signals only; host update_policy owns engine writes.
|
||||
self.assertEqual(window.engine.explore_policy.to_dict(), before)
|
||||
self.assertEqual(saved[-1]["teams"]["1"]["monsters"], [1, 2, 3])
|
||||
control["auto"].click()
|
||||
self.assertEqual(window.read_explore_policy().teams["1"].monsters, [])
|
||||
window.busy = False
|
||||
|
||||
def test_real_window_update_policy_waits_for_job(self):
|
||||
window = self.window
|
||||
window.refresh_exploration()
|
||||
self.wait(window, lambda: bool(window._exploration_member_options))
|
||||
configured = policy(monsters=[1, 2, 3])
|
||||
window.apply_explore_policy(configured)
|
||||
window.busy = True
|
||||
window.pending_policy = None
|
||||
self.assertFalse(window.update_policy())
|
||||
self.assertEqual(window.pending_policy["explore_policy"].teams["1"].monsters, [1, 2, 3])
|
||||
self.assertEqual(window.engine.explore_policy.to_dict(), policy().to_dict())
|
||||
window.busy = False
|
||||
|
||||
def test_repeated_render_keeps_owned_items_and_drafts_and_captures_no_qt_warning(self):
|
||||
window = self.window
|
||||
window.refresh_exploration()
|
||||
self.wait(window, lambda: bool(window._exploration_member_options))
|
||||
rows = copy.deepcopy(window._exploration_rows)
|
||||
identities = [window.exploration_table.item(0, column) for column in range(7)]
|
||||
control = self.controls()
|
||||
control["members"][0].setCurrentIndex(control["members"][0].findData(1))
|
||||
warnings = []
|
||||
handler = C.qInstallMessageHandler(lambda kind, context, message: warnings.append(message))
|
||||
try:
|
||||
with patch.object(gui, "prepare_exploration_rows", wraps=gui.prepare_exploration_rows) as worker:
|
||||
for _ in range(12):
|
||||
# Prevent member refetch jobs so this test repeatedly renders
|
||||
# a stable already-prepared small DTO in the GUI thread.
|
||||
window._exploration_member_options["1"] = [{"id": 1, "level": 20, "label": "one", "automatic": True, "reason": ""}]
|
||||
window.render_exploration(copy.deepcopy(rows))
|
||||
while window.exploration_table.row_batch.running:
|
||||
self.app.processEvents()
|
||||
QTest.qWait(2)
|
||||
worker.assert_not_called()
|
||||
finally:
|
||||
C.qInstallMessageHandler(handler)
|
||||
self.assertEqual([window.exploration_table.item(0, column) for column in range(7)], identities)
|
||||
self.assertEqual(window._exploration_drafts["1"], [1, None, None])
|
||||
self.assertEqual(window.read_explore_policy().teams["1"].monsters, [])
|
||||
self.assertFalse(warnings)
|
||||
|
||||
def test_account_switch_drops_drafts_and_cached_candidates(self):
|
||||
window = self.window
|
||||
window.refresh_exploration()
|
||||
self.wait(window, lambda: bool(window._exploration_member_options))
|
||||
control = self.controls()
|
||||
control["members"][0].setCurrentIndex(control["members"][0].findData(1))
|
||||
other_clock, other = make_snapshot(master={"id": 12, "ti": 100})
|
||||
with other_clock:
|
||||
window.engine.snapshot = other
|
||||
window.render_exploration([])
|
||||
self.assertFalse(window._exploration_drafts)
|
||||
self.assertFalse(window._exploration_member_options)
|
||||
self.assertEqual(window.exploration_table.rowCount(), 0)
|
||||
|
||||
def test_hidden_team_rules_survive_render_and_worker_returns_plain_rows(self):
|
||||
configured = policy(monsters=[1, 2, 3])
|
||||
configured.teams["99"] = replace(configured.teams["1"], area="2", monsters=[4, 5, 6])
|
||||
self.window.apply_explore_policy(configured)
|
||||
self.window.render_exploration([])
|
||||
self.assertEqual(self.window.read_explore_policy(), configured)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user