-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_user_input.py
More file actions
133 lines (95 loc) · 4.06 KB
/
test_user_input.py
File metadata and controls
133 lines (95 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import pytest
from unittest.mock import AsyncMock, MagicMock
from textual.app import App, ComposeResult
from textual.widgets import TextArea
from agent_chat_cli.components.slash_command_menu import SlashCommandMenu
from agent_chat_cli.components.user_input import UserInput
class UserInputApp(App):
def __init__(self):
super().__init__()
self.mock_actions = MagicMock()
self.mock_actions.quit = MagicMock()
self.mock_actions.interrupt = AsyncMock()
self.mock_actions.new = AsyncMock()
self.mock_actions.clear = AsyncMock()
self.mock_actions.save = AsyncMock()
self.mock_actions.post_user_message = AsyncMock()
def compose(self) -> ComposeResult:
yield UserInput(actions=self.mock_actions)
class TestUserInputSubmit:
@pytest.fixture
def app(self):
return UserInputApp()
async def test_empty_submit_does_nothing(self, app):
async with app.run_test() as pilot:
await pilot.press("enter")
app.mock_actions.post_user_message.assert_not_called()
async def test_submits_message(self, app):
async with app.run_test() as pilot:
user_input = app.query_one(UserInput)
text_area = user_input.query_one(TextArea)
text_area.insert("Hello agent")
await pilot.press("enter")
app.mock_actions.post_user_message.assert_called_with("Hello agent")
async def test_clears_input_after_submit(self, app):
async with app.run_test() as pilot:
user_input = app.query_one(UserInput)
text_area = user_input.query_one(TextArea)
text_area.insert("Hello agent")
await pilot.press("enter")
assert text_area.text == ""
class TestUserInputNewlines:
@pytest.fixture
def app(self):
return UserInputApp()
async def test_ctrl_j_inserts_newline(self, app):
async with app.run_test() as pilot:
user_input = app.query_one(UserInput)
text_area = user_input.query_one(TextArea)
text_area.insert("line1")
await pilot.press("ctrl+j")
text_area.insert("line2")
assert "line1\nline2" in text_area.text
class TestUserInputSlashMenu:
@pytest.fixture
def app(self):
return UserInputApp()
async def test_slash_shows_menu(self, app):
async with app.run_test() as pilot:
user_input = app.query_one(UserInput)
text_area = user_input.query_one(TextArea)
menu = user_input.query_one(SlashCommandMenu)
await pilot.press("/")
assert menu.is_visible is True
assert text_area.text == ""
async def test_escape_hides_menu(self, app):
async with app.run_test() as pilot:
user_input = app.query_one(UserInput)
menu = user_input.query_one(SlashCommandMenu)
await pilot.press("/")
await pilot.press("escape")
assert menu.is_visible is False
async def test_backspace_hides_menu(self, app):
async with app.run_test() as pilot:
user_input = app.query_one(UserInput)
menu = user_input.query_one(SlashCommandMenu)
await pilot.press("/")
await pilot.press("backspace")
assert menu.is_visible is False
async def test_enter_selects_menu_item(self, app):
async with app.run_test() as pilot:
await pilot.press("/")
await pilot.press("enter")
app.mock_actions.new.assert_called_once()
async def test_clears_input_after_filtering_and_selecting(self, app):
async with app.run_test() as pilot:
user_input = app.query_one(UserInput)
text_area = user_input.query_one(TextArea)
await pilot.press("/")
await pilot.press("s")
await pilot.press("a")
await pilot.press("v")
assert text_area.text == "sav"
await pilot.press("enter")
assert text_area.text == ""
app.mock_actions.save.assert_called_once()