Files
WoW-Hybrid-gateway/tests/owned_chat_input_test.py
2026-11-23 03:28:32 +02:00

98 lines
3.7 KiB
Python

#!/usr/bin/env python3
"""Exercise cancellation and failure boundaries without a client or input."""
import sys
import unittest
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'ops'))
from owned_chat_input import OwnedChatInput
class FakeUI:
def __init__(self, opened=False, fail=None):
self.opened = opened
self.fail = fail
self.calls = []
def ensure_focus(self, *_):
self.calls.append('focus')
def chat_edit_open(self, *_):
self.calls.append('check-open')
return self.opened
def key(self, _, key):
self.calls.append(key)
if key == 'Escape':
self.opened = False
elif key == 'Return':
self.opened = not self.opened
def wait_chat_edit(self, _, __, opened):
self.calls.append(('wait', opened))
if self.fail == ('wait', opened):
raise RuntimeError('unconfirmed edit')
if self.opened != opened:
raise RuntimeError('unexpected edit state')
def clear_edit_selection(self, *_):
self.calls.append('clear')
def type_text(self, _, text):
self.calls.append(('type', text))
if self.fail == 'typing':
raise RuntimeError('input interrupted')
class OwnedChatInputTest(unittest.TestCase):
def test_pending_edit_is_canceled_before_open_and_submit(self):
ui = FakeUI(opened=True)
sender = OwnedChatInput(ui)
sender.send(None, None, 'new command')
self.assertEqual(ui.calls, ['focus', 'check-open', 'Escape', ('wait', False),
'Return', ('wait', True), 'clear', ('type', 'new command'),
'Return', ('wait', False)])
self.assertFalse(sender.partial)
def test_opening_failure_stops_before_typing_or_submission(self):
ui = FakeUI(fail=('wait', True))
sender = OwnedChatInput(ui)
with self.assertRaisesRegex(RuntimeError, 'unconfirmed'):
sender.send(None, None, 'never sent')
self.assertEqual(ui.calls, ['focus', 'check-open', 'Return', ('wait', True)])
self.assertTrue(sender.partial)
def test_interruption_is_canceled_on_explicit_new_send_without_replay(self):
ui = FakeUI(fail='typing')
sender = OwnedChatInput(ui)
with self.assertRaisesRegex(RuntimeError, 'interrupted'):
sender.send(None, None, 'old command')
self.assertTrue(sender.partial)
ui.calls.clear()
ui.fail = None
sender.send(None, None, 'new command')
self.assertEqual(ui.calls, ['focus', 'Escape', ('wait', False), 'Return',
('wait', True), 'clear', ('type', 'new command'),
'Return', ('wait', False)])
self.assertFalse(sender.partial)
def test_paste_uses_one_enter_to_open_and_one_to_submit(self):
ui = FakeUI()
sender = OwnedChatInput(ui)
sender.send(None, None, '/run probe', paste=lambda v:ui.calls.append(('paste', v)))
self.assertEqual(ui.calls, ['focus', 'check-open', 'Return', ('wait', True), 'clear',
('paste', '/run probe'), 'Return', ('wait', False)])
self.assertFalse(sender.partial)
def test_unconfirmed_cancel_stops_before_open_or_typing(self):
ui = FakeUI(opened=True, fail=('wait', False))
sender = OwnedChatInput(ui)
with self.assertRaisesRegex(RuntimeError, 'unconfirmed'):
sender.send(None, None, 'never sent')
self.assertEqual(ui.calls, ['focus', 'check-open', 'Escape', ('wait', False)])
self.assertTrue(sender.partial)
if __name__ == '__main__':
unittest.main()