from pathlib import Path import sys import unittest from unittest import mock sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'scripts')) import lc_ui # noqa: E402 import levelup # noqa: E402 def shot(w, h, items=()): return {'ok': True, 'screen': {'path': 'x.png', 'w': w, 'h': h}, 'items': [dict(i) for i in items]} class ScaleTest(unittest.TestCase): """脚本坐标按 1600x900 写, 实际客户区可能是 1280x720 或 1920x1080。""" def setUp(self): lc_ui._scale = None def test_720p_items_and_clicks_use_base_coordinates(self): calls = [] def probe(tool, args=None, quiet=True): calls.append((tool, args)) return shot(1280, 720, [{'text': 'Level Up', 'x': 800, 'y': 400, 'w': 80, 'h': 20}]) with mock.patch.object(lc_ui, 'probe', probe): item = lc_ui.items()[0] lc_ui.click(1600, 900) lc_ui.long_press(800, 450) self.assertEqual((item['x'], item['y'], item['w']), (1000, 500, 100)) self.assertEqual(calls[1], ('click', {'x': 1280, 'y': 720, 'times': 1})) self.assertEqual(calls[2], ('drag', {'from_x': 640, 'from_y': 360, 'to_x': 640, 'to_y': 360})) def test_click_before_any_state_reads_client_size_first(self): calls = [] def probe(tool, args=None, quiet=True): calls.append((tool, args)) return shot(1920, 1080) with mock.patch.object(lc_ui, 'probe', probe): lc_ui.click(100, 100) self.assertEqual(calls, [('get_state', None), ('click', {'x': 120, 'y': 120, 'times': 1})]) class Screens: """按顺序返回预设画面的假 state(), 记录点击。""" def __init__(self, *screens): self.screens = list(screens) self.clicks = [] self.keys = [] def state(self): texts = self.screens.pop(0) if len(self.screens) > 1 else self.screens[0] return {'items': [{'text': t, 'x': x, 'y': y} for t, x, y in texts]} def patch(self): return mock.patch.multiple(levelup, state=self.state, time=mock.Mock(), click=lambda *a, **k: self.clicks.append(a), keypress=lambda k: self.keys.append(k)) DETAIL = [('Level Up', 1164, 235), ('Uptie', 1367, 235), ('547/885', 400, 250)] POWERUP = [('Owned:3', 900, 380), ('Owned:0', 900, 485), ('Owned:7', 900, 595), ('Cancel', 603, 670)] class LevelUpVerdictTest(unittest.TestCase): def test_detail_page_without_exp_change_is_not_success(self): # 旧逻辑只要回到详情页(有 Level Up / Uptie)就算成功 screens = Screens(DETAIL, POWERUP, POWERUP, DETAIL, DETAIL) with screens.patch(): ok, desc = levelup.do_level_up(hint=17) self.assertFalse(ok, desc) def test_level_increase_is_success(self): after = [('Level Up', 1164, 235), ('Uptie', 1367, 235), ('100/1261', 400, 250)] screens = Screens(DETAIL, POWERUP, POWERUP, after, after) with screens.patch(): ok, desc = levelup.do_level_up(hint=17) self.assertTrue(ok, desc) self.assertIn('17->19', desc) def test_popup_still_open_after_confirm_is_cancelled(self): screens = Screens(DETAIL, POWERUP, POWERUP, POWERUP) with screens.patch(): ok, desc = levelup.do_level_up(hint=17) self.assertFalse(ok) self.assertEqual(screens.clicks[-1], levelup.POWERUP_CANCEL) UPTIE_POPUP = [('Currency Req.', 700, 300), ('Skills Gained', 700, 400), ('Owned:2', 900, 450), ('Uptie', 1085, 580)] class UptieVerdictTest(unittest.TestCase): def test_popup_still_open_is_failure_even_with_skills_gained(self): screens = Screens(UPTIE_POPUP, UPTIE_POPUP) with screens.patch(): ok, desc = levelup.do_uptie() self.assertFalse(ok, desc) self.assertEqual(screens.keys, []) def test_success_dialog_is_confirmed_without_esc(self): done = [('Uptied!', 700, 300), ('Confirm', 772, 661)] screens = Screens(UPTIE_POPUP, done, done) with screens.patch(): ok, desc = levelup.do_uptie() self.assertTrue(ok, desc) self.assertEqual(screens.keys, []) self.assertEqual(screens.clicks[-1], levelup.UPTIE_DONE_CONFIRM) def test_unknown_screen_presses_esc_once_and_never_clicks_blind(self): story = [("I swear to you! I will return this favor", 500, 700)] screens = Screens(UPTIE_POPUP, story, story, story) with screens.patch(): ok, desc = levelup.do_uptie() self.assertFalse(ok) self.assertEqual(screens.keys, ['esc']) self.assertNotIn(levelup.UPTIE_CANCEL, screens.clicks) def test_cutscene_skip_prompt_counts_as_success(self): story = [("I swear to you!", 500, 700)] skip = [('Skip this story?', 700, 400), ('Confirm', 900, 600)] screens = Screens(UPTIE_POPUP, story, story, skip, skip) with screens.patch(): ok, desc = levelup.do_uptie() self.assertTrue(ok, desc) self.assertEqual(screens.clicks[-1], (900, 600)) if __name__ == '__main__': unittest.main()