Repository navigation
Expand file tree
/
Copy pathtest_cli_integration.py
More file actions
311 lines (257 loc) · 10.2 KB
/
Copy pathtest_cli_integration.py
File metadata and controls
311 lines (257 loc) · 10.2 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import asyncio
import os
from unittest.mock import AsyncMock, patch
import pytest
from app.cli.app import NL2SQLApp
def get_richlog_text(widget) -> str:
"""Extract plain text from all rendered lines in a RichLog."""
parts = []
for strip in widget.lines:
parts.append(strip.text)
return "\n".join(parts)
async def focus_input(app):
"""Focus the input widget and wait."""
input_w = app.query_one("#user-input")
input_w.focus()
from textual import get_app
# Small pause after focus
app = get_app()
await asyncio.sleep(0.05)
async def type_and_submit(app, pilot, text):
"""Type text into the focused input and submit."""
input_w = app.query_one("#user-input")
input_w.focus()
await pilot.pause()
for ch in text:
await pilot.press(ch)
await pilot.pause()
await pilot.press("enter")
await pilot.pause()
@pytest.mark.asyncio
async def test_app_starts():
"""Test that the app starts without errors."""
app = NL2SQLApp()
async with app.run_test(size=(120, 40)) as pilot:
assert app.query_one("#message-log") is not None, "message-log not found"
assert app.query_one("#history-log") is not None, "history-log not found"
assert app.query_one("#user-input") is not None, "user-input not found"
assert app.query_one("#sidebar") is not None, "sidebar not found"
assert app.query_one("#command-palette") is not None, "command-palette not found"
print("PASS: App starts and all widgets render")
return True
@pytest.mark.asyncio
async def test_help_command():
"""Test /help command."""
app = NL2SQLApp()
async with app.run_test(size=(120, 40)) as pilot:
await type_and_submit(app, pilot, "/help")
log = app.query_one("#message-log")
text = get_richlog_text(log)
assert "Commands" in text or "help" in text.lower(), f"/help output missing. Got: {text[:300]}"
print("PASS: /help command works")
return True
@pytest.mark.asyncio
async def test_clear_command():
"""Test /clear command."""
app = NL2SQLApp()
async with app.run_test(size=(120, 40)) as pilot:
await type_and_submit(app, pilot, "/help")
await asyncio.sleep(0.1)
await type_and_submit(app, pilot, "/clear")
await asyncio.sleep(0.1)
await type_and_submit(app, pilot, "y")
await asyncio.sleep(0.1)
log = app.query_one("#message-log")
text = get_richlog_text(log)
assert "cleared" in text.lower() or "消息已清空" in text or "Messages cleared" in text, f"/clear output missing. Got: {text[:300]}"
print("PASS: /clear command works")
return True
@pytest.mark.asyncio
async def test_export_command():
"""Test /export command creates the file."""
app = NL2SQLApp()
export_file = "nl2sql_history.txt"
if os.path.exists(export_file):
os.remove(export_file)
async with app.run_test(size=(120, 40)) as pilot:
await type_and_submit(app, pilot, "/export")
await asyncio.sleep(0.2)
log = app.query_one("#message-log")
text = get_richlog_text(log)
assert "exported" in text.lower() or "nl2sql_history.txt" in text, f"/export output missing. Got: {text[:300]}"
assert os.path.exists(export_file), "nl2sql_history.txt was not created"
content = open(export_file, "r", encoding="utf-8").read()
print(f"PASS: /export command works. File content length: {len(content)}")
if os.path.exists(export_file):
os.remove(export_file)
return True
@pytest.mark.asyncio
async def test_config_command():
"""Test /config command."""
app = NL2SQLApp()
async with app.run_test(size=(120, 40)) as pilot:
await type_and_submit(app, pilot, "/config")
log = app.query_one("#message-log")
text = get_richlog_text(log)
assert "Config" in text or "Database" in text or "LLM" in text, f"/config output missing. Got: {text[:300]}"
print("PASS: /config command works")
return True
@pytest.mark.asyncio
async def test_unknown_command():
"""Test unknown command handling."""
app = NL2SQLApp()
async with app.run_test(size=(120, 40)) as pilot:
await type_and_submit(app, pilot, "/foo")
log = app.query_one("#message-log")
text = get_richlog_text(log)
assert "Unknown" in text or "unknown" in text or "未知命令" in text, f"Unknown command output missing. Got: {text[:300]}"
print("PASS: Unknown command handling works")
return True
@pytest.mark.asyncio
async def test_empty_input():
"""Test that empty input is handled gracefully."""
app = NL2SQLApp()
async with app.run_test(size=(120, 40)) as pilot:
input_w = app.query_one("#user-input")
input_w.focus()
await pilot.pause()
await pilot.press("enter")
await pilot.pause()
print("PASS: Empty input handled gracefully")
return True
@pytest.mark.asyncio
async def test_question_input_mocked():
"""Test question handling with mocked backend."""
app = NL2SQLApp()
mock_result = {
"question": "show all users",
"sql": "SELECT * FROM users",
"columns": ["id", "name"],
"rows": [[1, "Alice"]],
"error": None,
}
async with app.run_test(size=(120, 40)) as pilot:
with patch("app.cli.app.ask", new_callable=AsyncMock, return_value=mock_result):
await type_and_submit(app, pilot, "show all users")
await asyncio.sleep(0.5)
log = app.query_one("#message-log")
text = get_richlog_text(log)
assert "SQL" in text or "SELECT" in text, f"SQL result not shown. Got: {text[:500]}"
print("PASS: Question handling with mocked backend works")
return True
@pytest.mark.asyncio
async def test_question_error_mocked():
"""Test question handling when backend returns error."""
app = NL2SQLApp()
mock_result = {
"question": "test question",
"sql": "SELECT 1",
"columns": [],
"rows": [],
"error": "Connection refused",
}
async with app.run_test(size=(120, 40)) as pilot:
with patch("app.cli.app.ask", new_callable=AsyncMock, return_value=mock_result):
await type_and_submit(app, pilot, "test question")
await asyncio.sleep(1.0)
log = app.query_one("#message-log")
text = get_richlog_text(log)
assert "Error" in text or "error" in text or "错误" in text, f"Error message not shown. Got: {text[:500]}"
print("PASS: Error handling works")
return True
@pytest.mark.asyncio
async def test_sidebar_toggle():
"""Test sidebar toggle with Ctrl+H."""
app = NL2SQLApp()
async with app.run_test(size=(120, 40)) as pilot:
sidebar = app.query_one("#sidebar")
initial = sidebar.display
await pilot.press("ctrl+h")
await pilot.pause()
assert sidebar.display != initial, "Sidebar did not toggle"
await pilot.press("ctrl+h")
await pilot.pause()
assert sidebar.display == initial, "Sidebar did not toggle back"
print("PASS: Sidebar toggle works")
return True
@pytest.mark.asyncio
async def test_ctrl_l_clear():
"""Test Ctrl+L shortcut to clear messages."""
app = NL2SQLApp()
async with app.run_test(size=(120, 40)) as pilot:
await type_and_submit(app, pilot, "/help")
await asyncio.sleep(0.1)
log = app.query_one("#message-log")
text_before = get_richlog_text(log)
assert len(text_before.strip()) > 0, "No content before clear"
await pilot.press("ctrl+l")
await pilot.pause()
text_after = get_richlog_text(log)
assert len(text_after.strip()) == 0, f"Content not cleared. Got: {text_after[:200]}"
print("PASS: Ctrl+L shortcut works")
return True
@pytest.mark.asyncio
async def test_export_includes_history():
"""Test that export includes previously typed questions."""
app = NL2SQLApp()
mock_result = {
"question": "count users",
"sql": "SELECT COUNT(*) FROM users",
"columns": ["count"],
"rows": [[42]],
"error": None,
}
export_file = "nl2sql_history.txt"
if os.path.exists(export_file):
os.remove(export_file)
async with app.run_test(size=(120, 40)) as pilot:
with patch("app.cli.app.ask", new_callable=AsyncMock, return_value=mock_result):
await type_and_submit(app, pilot, "count users")
await asyncio.sleep(1.0)
await type_and_submit(app, pilot, "/export")
await asyncio.sleep(0.5)
assert os.path.exists(export_file), "Export file not created"
content = open(export_file, "r", encoding="utf-8").read()
assert "count users" in content, f"Export does not contain question. Content: {content[:200]}"
print(f"PASS: Export includes history (content length: {len(content)})")
if os.path.exists(export_file):
os.remove(export_file)
return True
async def main():
tests = [
("App starts", test_app_starts),
("/help command", test_help_command),
("/clear command", test_clear_command),
("/export command", test_export_command),
("/config command", test_config_command),
("Unknown command", test_unknown_command),
("Empty input", test_empty_input),
("Question (mocked)", test_question_input_mocked),
("Error handling (mocked)", test_question_error_mocked),
("Sidebar toggle", test_sidebar_toggle),
("Ctrl+L clear", test_ctrl_l_clear),
("Export includes history", test_export_includes_history),
]
passed = 0
failed = 0
errors = []
for name, test_fn in tests:
try:
result = await test_fn()
if result:
passed += 1
except Exception as e:
failed += 1
errors.append((name, str(e)))
print(f"FAIL: {name}: {e}")
print(f"\n{'='*50}")
print(f"Results: {passed} passed, {failed} failed")
if errors:
print("Failures:")
for name, err in errors:
print(f" - {name}: {err}")
print(f"{'='*50}")
return failed == 0
if __name__ == "__main__":
success = asyncio.run(main())
exit(0 if success else 1)