lycheedev 1.0.6 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,132 @@
1
+ """Offline input regression: all window, keyboard and clipboard calls are mocked."""
2
+ import unittest
3
+ from unittest.mock import patch
4
+ from automation import windows
5
+
6
+ class Clipboard:
7
+ def __init__(self): self.text = "previous clipboard"
8
+ def read_text(self): return self.text
9
+ def copy_text(self, value): self.text = value
10
+ def clear(self): self.text = None
11
+
12
+ class InputSafetyTests(unittest.TestCase):
13
+ def test_windows_path_aliases_match(self):
14
+ identity = windows.WindowIdentity(1, 2, r"D:\Game\Wow.exe", "test")
15
+ self.assertTrue(identity.matches(pid=2, exe_path="d:/game/WOW.EXE"))
16
+ self.assertFalse(identity.matches(pid=3))
17
+
18
+ def exercise(self, draft=None, *, paste=True, copy=True, lose_focus=False, mode="foreground", escape=True):
19
+ clipboard = Clipboard()
20
+ state = {"open": draft is not None, "text": draft or "", "selected": False,
21
+ "foreground": True, "public": [], "commands": []}
22
+ def key(vk, ctrl=False):
23
+ if vk == 0x1b:
24
+ if escape: state.update(open=False, text="", selected=False)
25
+ elif vk == 0xBF:
26
+ state["open"] = True
27
+ state["text"] += "/"
28
+ elif vk == windows.VK_RETURN:
29
+ if state["open"]:
30
+ text = state["text"]
31
+ if text: state["commands" if text.startswith("/") else "public"].append(text)
32
+ state.update(open=False, text="", selected=False)
33
+ else: state["open"] = True
34
+ elif ctrl and vk == 0x41: state["selected"] = True
35
+ elif ctrl and vk == windows.VK_V and state["open"]:
36
+ if paste:
37
+ state["text"] = ("" if state["selected"] else state["text"]) + clipboard.text
38
+ state["selected"] = False
39
+ if lose_focus: state["foreground"] = False
40
+ elif ctrl and vk == 0x43 and state["open"] and state["selected"] and copy:
41
+ clipboard.text = state["text"]
42
+ error = None
43
+ with patch.object(windows, "confirm_window", return_value=windows.WindowIdentity(1, 2, "D:/Game/Wow.exe", "test")), \
44
+ patch.object(windows, "focus_window"), \
45
+ patch.object(windows, "is_foreground", side_effect=lambda hwnd: state["foreground"]), \
46
+ patch.object(windows, "_send_key", side_effect=key), patch.object(windows.time, "sleep"):
47
+ try:
48
+ windows.send_command(1, "/reload", clipboard=clipboard, mode=mode)
49
+ except windows.InputError as exc: error = exc
50
+ self.assertEqual(state["public"], [], "automation submitted a public chat draft")
51
+ if paste:
52
+ self.assertEqual(clipboard.text, "previous clipboard")
53
+ return state, error
54
+
55
+ def test_existing_draft_is_cancelled(self):
56
+ state, error = self.exercise("unfinished public chat ")
57
+ self.assertIsNone(error)
58
+ self.assertEqual(state["commands"], ["/reload"])
59
+
60
+ def test_ignored_escape_cannot_submit_draft(self):
61
+ state, error = self.exercise("public draft", escape=False)
62
+ self.assertIsNone(error)
63
+ self.assertEqual(state["commands"], ["/reload"])
64
+
65
+ def test_messages_stays_background_and_cancels_draft(self):
66
+ state = {"open": True, "text": "public draft", "public": [], "commands": []}
67
+ def post(hwnd, message, value, lparam):
68
+ self.assertEqual(hwnd, 1)
69
+ if message == windows.WM_KEYDOWN:
70
+ if value == windows.VK_ESCAPE:
71
+ state.update(open=False, text="")
72
+ elif value == windows.VK_RETURN:
73
+ if state["open"]:
74
+ text = state["text"]
75
+ if text: state["commands" if text.startswith("/") else "public"].append(text)
76
+ state.update(open=False, text="")
77
+ else: state["open"] = True
78
+ elif message == windows.WM_CHAR:
79
+ self.assertEqual(lparam, 1)
80
+ if state["open"]: state["text"] += chr(value)
81
+ return 1
82
+ with patch.object(windows, "confirm_window", return_value=windows.WindowIdentity(1, 2, "D:/Wow.exe", "test")), \
83
+ patch.object(windows.user32, "PostMessageW", side_effect=post), \
84
+ patch.object(windows, "focus_window", side_effect=AssertionError("must stay background")), \
85
+ patch.object(windows, "_send_key", side_effect=AssertionError("must not SendInput")), \
86
+ patch.object(windows, "Clipboard", side_effect=AssertionError("must not use clipboard")), \
87
+ patch.object(windows.time, "sleep"):
88
+ windows.send_command(1, "/reload", mode="messages")
89
+ self.assertEqual(state["public"], [])
90
+ self.assertEqual(state["commands"], ["/reload"])
91
+
92
+ def test_background_rejection_does_not_queue_submit(self):
93
+ posted = []
94
+ def post(hwnd, message, value, lparam):
95
+ posted.append((message, value))
96
+ return message != windows.WM_CHAR
97
+ with patch.object(windows, "confirm_window", return_value=windows.WindowIdentity(1, 2, "D:/Wow.exe", "test")), \
98
+ patch.object(windows.user32, "PostMessageW", side_effect=post), \
99
+ patch.object(windows.time, "sleep"):
100
+ with self.assertRaises(windows.InputError):
101
+ windows.send_command(1, "/reload", mode="messages")
102
+ returns = [entry for entry in posted if entry == (windows.WM_KEYDOWN, windows.VK_RETURN)]
103
+ self.assertEqual(len(returns), 1, "failed typing must not queue final submit")
104
+ self.assertEqual(posted[-1], (windows.WM_KEYUP, windows.VK_ESCAPE))
105
+
106
+ def test_invalid_commands_never_focus_or_type(self):
107
+ with patch.object(windows, "focus_window") as focus, patch.object(windows, "_send_key") as key:
108
+ for command in ("hello", "/reload\n/dev", "/" + "a" * 255):
109
+ with self.assertRaises(windows.InputError):
110
+ windows.send_command(1, command)
111
+ focus.assert_not_called()
112
+ key.assert_not_called()
113
+
114
+ def test_closed_and_empty_chat(self):
115
+ for draft in (None, ""):
116
+ state, error = self.exercise(draft)
117
+ self.assertIsNone(error)
118
+ self.assertEqual(state["commands"], ["/reload"])
119
+
120
+ def test_failed_paste_or_copy_never_submits(self):
121
+ for options in ({"paste": False}, {"copy": False}):
122
+ state, error = self.exercise(**options)
123
+ self.assertIsNotNone(error)
124
+ self.assertEqual(state["commands"], [])
125
+ self.assertFalse(state["open"])
126
+
127
+ def test_focus_loss_never_submits(self):
128
+ state, error = self.exercise(lose_focus=True)
129
+ self.assertIsNotNone(error)
130
+ self.assertEqual(state["commands"], [])
131
+
132
+ if __name__ == "__main__": unittest.main()