| 1 |
import pytest |
| 2 |
import time |
| 3 |
from IPython.terminal.interactiveshell import PtkHistoryAdapter |
| 4 |
from IPython.terminal.shortcuts.auto_suggest import ( |
| 5 |
accept, |
| 6 |
accept_or_jump_to_end, |
| 7 |
accept_token, |
| 8 |
accept_character, |
| 9 |
accept_word, |
| 10 |
accept_and_keep_cursor, |
| 11 |
discard, |
| 12 |
llm_autosuggestion, |
| 13 |
NavigableAutoSuggestFromHistory, |
| 14 |
swap_autosuggestion_up, |
| 15 |
swap_autosuggestion_down, |
| 16 |
) |
| 17 |
from IPython.terminal.shortcuts.auto_match import skip_over |
| 18 |
from IPython.terminal.shortcuts import create_ipython_shortcuts, reset_search_buffer |
| 19 |
from IPython.testing import decorators as dec |
| 20 |
|
| 21 |
from prompt_toolkit.history import InMemoryHistory |
| 22 |
from prompt_toolkit.buffer import Buffer |
| 23 |
from prompt_toolkit.document import Document |
| 24 |
from prompt_toolkit.auto_suggest import AutoSuggestFromHistory |
| 25 |
from prompt_toolkit.enums import DEFAULT_BUFFER |
| 26 |
|
| 27 |
from unittest.mock import patch, Mock |
| 28 |
|
| 29 |
|
| 30 |
def make_event(text, cursor, suggestion): |
| 31 |
event = Mock() |
| 32 |
event.current_buffer = Mock() |
| 33 |
event.current_buffer.suggestion = Mock() |
| 34 |
event.current_buffer.text = text |
| 35 |
event.current_buffer.cursor_position = cursor |
| 36 |
event.current_buffer.suggestion.text = suggestion |
| 37 |
event.current_buffer.document = Document(text=text, cursor_position=cursor) |
| 38 |
return event |
| 39 |
|
| 40 |
|
| 41 |
try: |
| 42 |
from .fake_llm import FIBONACCI |
| 43 |
except ImportError: |
| 44 |
FIBONACCI = "" |
| 45 |
|
| 46 |
|
| 47 |
@pytest.mark.skip(reason="pydandic messed up") |
| 48 |
@dec.skip_without("jupyter_ai") |
| 49 |
@pytest.mark.asyncio |
| 50 |
async def test_llm_autosuggestion(): |
| 51 |
provider = NavigableAutoSuggestFromHistory() |
| 52 |
ip = get_ipython() |
| 53 |
ip.auto_suggest = provider |
| 54 |
ip.llm_provider_class = "tests.fake_llm.FibonacciCompletionProvider" |
| 55 |
ip.history_manager.get_range = Mock(return_value=[]) |
| 56 |
text = "def fib" |
| 57 |
event = Mock() |
| 58 |
event.current_buffer = Buffer( |
| 59 |
history=PtkHistoryAdapter(ip), |
| 60 |
) |
| 61 |
event.current_buffer.insert_text(text, move_cursor=True) |
| 62 |
await llm_autosuggestion(event) |
| 63 |
assert event.current_buffer.suggestion.text == FIBONACCI[len(text) :] |
| 64 |
|
| 65 |
|
| 66 |
def test_slow_llm_provider_should_not_block_init(): |
| 67 |
ip = get_ipython() |
| 68 |
provider = NavigableAutoSuggestFromHistory() |
| 69 |
ip.auto_suggest = provider |
| 70 |
start = time.perf_counter() |
| 71 |
ip.llm_provider_class = "tests.fake_llm.SlowStartingCompletionProvider" |
| 72 |
end = time.perf_counter() |
| 73 |
elapsed = end - start |
| 74 |
assert elapsed < 0.1 |
| 75 |
|
| 76 |
|
| 77 |
@pytest.mark.parametrize( |
| 78 |
"text, suggestion, expected", |
| 79 |
[ |
| 80 |
("", "def out(tag: str, n=50):", "def out(tag: str, n=50):"), |
| 81 |
("def ", "out(tag: str, n=50):", "out(tag: str, n=50):"), |
| 82 |
], |
| 83 |
) |
| 84 |
def test_accept(text, suggestion, expected): |
| 85 |
event = make_event(text, len(text), suggestion) |
| 86 |
buffer = event.current_buffer |
| 87 |
buffer.insert_text = Mock() |
| 88 |
accept(event) |
| 89 |
assert buffer.insert_text.called |
| 90 |
assert buffer.insert_text.call_args[0] == (expected,) |
| 91 |
|
| 92 |
|
| 93 |
@pytest.mark.parametrize( |
| 94 |
"text, suggestion", |
| 95 |
[ |
| 96 |
("", "def out(tag: str, n=50):"), |
| 97 |
("def ", "out(tag: str, n=50):"), |
| 98 |
], |
| 99 |
) |
| 100 |
def test_discard(text, suggestion): |
| 101 |
event = make_event(text, len(text), suggestion) |
| 102 |
buffer = event.current_buffer |
| 103 |
buffer.insert_text = Mock() |
| 104 |
discard(event) |
| 105 |
assert not buffer.insert_text.called |
| 106 |
assert buffer.suggestion is None |
| 107 |
|
| 108 |
|
| 109 |
@pytest.mark.parametrize( |
| 110 |
"text, cursor, suggestion, called", |
| 111 |
[ |
| 112 |
("123456", 6, "123456789", True), |
| 113 |
("123456", 3, "123456789", False), |
| 114 |
("123456 \n789", 6, "123456789", True), |
| 115 |
], |
| 116 |
) |
| 117 |
def test_autosuggest_at_EOL(text, cursor, suggestion, called): |
| 118 |
""" |
| 119 |
test that autosuggest is only applied at end of line. |
| 120 |
""" |
| 121 |
|
| 122 |
event = make_event(text, cursor, suggestion) |
| 123 |
event.current_buffer.insert_text = Mock() |
| 124 |
accept_or_jump_to_end(event) |
| 125 |
if called: |
| 126 |
event.current_buffer.insert_text.assert_called() |
| 127 |
else: |
| 128 |
event.current_buffer.insert_text.assert_not_called() |
| 129 |
# event.current_buffer.document.get_end_of_line_position.assert_called() |
| 130 |
|
| 131 |
|
| 132 |
@pytest.mark.parametrize( |
| 133 |
"text, suggestion, expected", |
| 134 |
[ |
| 135 |
("", "def out(tag: str, n=50):", "def "), |
| 136 |
("d", "ef out(tag: str, n=50):", "ef "), |
| 137 |
("de ", "f out(tag: str, n=50):", "f "), |
| 138 |
("def", " out(tag: str, n=50):", " "), |
| 139 |
("def ", "out(tag: str, n=50):", "out("), |
| 140 |
("def o", "ut(tag: str, n=50):", "ut("), |
| 141 |
("def ou", "t(tag: str, n=50):", "t("), |
| 142 |
("def out", "(tag: str, n=50):", "("), |
| 143 |
("def out(", "tag: str, n=50):", "tag: "), |
| 144 |
("def out(t", "ag: str, n=50):", "ag: "), |
| 145 |
("def out(ta", "g: str, n=50):", "g: "), |
| 146 |
("def out(tag", ": str, n=50):", ": "), |
| 147 |
("def out(tag:", " str, n=50):", " "), |
| 148 |
("def out(tag: ", "str, n=50):", "str, "), |
| 149 |
("def out(tag: s", "tr, n=50):", "tr, "), |
| 150 |
("def out(tag: st", "r, n=50):", "r, "), |
| 151 |
("def out(tag: str", ", n=50):", ", n"), |
| 152 |
("def out(tag: str,", " n=50):", " n"), |
| 153 |
("def out(tag: str, ", "n=50):", "n="), |
| 154 |
("def out(tag: str, n", "=50):", "="), |
| 155 |
("def out(tag: str, n=", "50):", "50)"), |
| 156 |
("def out(tag: str, n=5", "0):", "0)"), |
| 157 |
("def out(tag: str, n=50", "):", "):"), |
| 158 |
("def out(tag: str, n=50)", ":", ":"), |
| 159 |
], |
| 160 |
) |
| 161 |
def test_autosuggest_token(text, suggestion, expected): |
| 162 |
event = make_event(text, len(text), suggestion) |
| 163 |
event.current_buffer.insert_text = Mock() |
| 164 |
accept_token(event) |
| 165 |
assert event.current_buffer.insert_text.called |
| 166 |
assert event.current_buffer.insert_text.call_args[0] == (expected,) |
| 167 |
|
| 168 |
|
| 169 |
@pytest.mark.parametrize( |
| 170 |
"text, suggestion, expected", |
| 171 |
[ |
| 172 |
("", "def out(tag: str, n=50):", "d"), |
| 173 |
("d", "ef out(tag: str, n=50):", "e"), |
| 174 |
("de ", "f out(tag: str, n=50):", "f"), |
| 175 |
("def", " out(tag: str, n=50):", " "), |
| 176 |
], |
| 177 |
) |
| 178 |
def test_accept_character(text, suggestion, expected): |
| 179 |
event = make_event(text, len(text), suggestion) |
| 180 |
event.current_buffer.insert_text = Mock() |
| 181 |
accept_character(event) |
| 182 |
assert event.current_buffer.insert_text.called |
| 183 |
assert event.current_buffer.insert_text.call_args[0] == (expected,) |
| 184 |
|
| 185 |
|
| 186 |
@pytest.mark.parametrize( |
| 187 |
"text, suggestion, expected", |
| 188 |
[ |
| 189 |
("", "def out(tag: str, n=50):", "def "), |
| 190 |
("d", "ef out(tag: str, n=50):", "ef "), |
| 191 |
("de", "f out(tag: str, n=50):", "f "), |
| 192 |
("def", " out(tag: str, n=50):", " "), |
| 193 |
# (this is why we also have accept_token) |
| 194 |
("def ", "out(tag: str, n=50):", "out(tag: "), |
| 195 |
], |
| 196 |
) |
| 197 |
def test_accept_word(text, suggestion, expected): |
| 198 |
event = make_event(text, len(text), suggestion) |
| 199 |
event.current_buffer.insert_text = Mock() |
| 200 |
accept_word(event) |
| 201 |
assert event.current_buffer.insert_text.called |
| 202 |
assert event.current_buffer.insert_text.call_args[0] == (expected,) |
| 203 |
|
| 204 |
|
| 205 |
@pytest.mark.parametrize( |
| 206 |
"text, suggestion, expected, cursor", |
| 207 |
[ |
| 208 |
("", "def out(tag: str, n=50):", "def out(tag: str, n=50):", 0), |
| 209 |
("def ", "out(tag: str, n=50):", "out(tag: str, n=50):", 4), |
| 210 |
], |
| 211 |
) |
| 212 |
def test_accept_and_keep_cursor(text, suggestion, expected, cursor): |
| 213 |
event = make_event(text, cursor, suggestion) |
| 214 |
buffer = event.current_buffer |
| 215 |
buffer.insert_text = Mock() |
| 216 |
accept_and_keep_cursor(event) |
| 217 |
assert buffer.insert_text.called |
| 218 |
assert buffer.insert_text.call_args[0] == (expected,) |
| 219 |
assert buffer.cursor_position == cursor |
| 220 |
|
| 221 |
|
| 222 |
def test_autosuggest_token_empty(): |
| 223 |
full = "def out(tag: str, n=50):" |
| 224 |
event = make_event(full, len(full), "") |
| 225 |
event.current_buffer.insert_text = Mock() |
| 226 |
|
| 227 |
with patch( |
| 228 |
"prompt_toolkit.key_binding.bindings.named_commands.forward_word" |
| 229 |
) as forward_word: |
| 230 |
accept_token(event) |
| 231 |
assert not event.current_buffer.insert_text.called |
| 232 |
assert forward_word.called |
| 233 |
|
| 234 |
|
| 235 |
def test_reset_search_buffer(): |
| 236 |
event_with_text = Mock() |
| 237 |
event_with_text.current_buffer.document.text = "some text" |
| 238 |
event_with_text.current_buffer.reset = Mock() |
| 239 |
|
| 240 |
event_empty = Mock() |
| 241 |
event_empty.current_buffer.document.text = "" |
| 242 |
event_empty.app.layout.focus = Mock() |
| 243 |
|
| 244 |
reset_search_buffer(event_with_text) |
| 245 |
event_with_text.current_buffer.reset.assert_called_once() |
| 246 |
|
| 247 |
reset_search_buffer(event_empty) |
| 248 |
event_empty.app.layout.focus.assert_called_once_with(DEFAULT_BUFFER) |
| 249 |
|
| 250 |
|
| 251 |
def test_other_providers(): |
| 252 |
"""Ensure that swapping autosuggestions does not break with other providers""" |
| 253 |
provider = AutoSuggestFromHistory() |
| 254 |
ip = get_ipython() |
| 255 |
ip.auto_suggest = provider |
| 256 |
event = Mock() |
| 257 |
event.current_buffer = Buffer() |
| 258 |
assert swap_autosuggestion_up(event) is None |
| 259 |
assert swap_autosuggestion_down(event) is None |
| 260 |
|
| 261 |
|
| 262 |
@pytest.mark.asyncio |
| 263 |
async def test_navigable_provider(): |
| 264 |
provider = NavigableAutoSuggestFromHistory() |
| 265 |
history = InMemoryHistory(history_strings=["very_a", "very", "very_b", "very_c"]) |
| 266 |
buffer = Buffer(history=history) |
| 267 |
ip = get_ipython() |
| 268 |
ip.auto_suggest = provider |
| 269 |
|
| 270 |
async for _ in history.load(): |
| 271 |
pass |
| 272 |
|
| 273 |
buffer.cursor_position = 5 |
| 274 |
buffer.text = "very" |
| 275 |
|
| 276 |
up = swap_autosuggestion_up |
| 277 |
down = swap_autosuggestion_down |
| 278 |
|
| 279 |
event = Mock() |
| 280 |
event.current_buffer = buffer |
| 281 |
|
| 282 |
def get_suggestion(): |
| 283 |
suggestion = provider.get_suggestion(buffer, buffer.document) |
| 284 |
buffer.suggestion = suggestion |
| 285 |
return suggestion |
| 286 |
|
| 287 |
assert get_suggestion().text == "_c" |
| 288 |
|
| 289 |
# should go up |
| 290 |
up(event) |
| 291 |
assert get_suggestion().text == "_b" |
| 292 |
|
| 293 |
# should skip over 'very' which is identical to buffer content |
| 294 |
up(event) |
| 295 |
assert get_suggestion().text == "_a" |
| 296 |
|
| 297 |
# should cycle back to beginning |
| 298 |
up(event) |
| 299 |
assert get_suggestion().text == "_c" |
| 300 |
|
| 301 |
# should cycle back through end boundary |
| 302 |
down(event) |
| 303 |
assert get_suggestion().text == "_a" |
| 304 |
|
| 305 |
down(event) |
| 306 |
assert get_suggestion().text == "_b" |
| 307 |
|
| 308 |
down(event) |
| 309 |
assert get_suggestion().text == "_c" |
| 310 |
|
| 311 |
down(event) |
| 312 |
assert get_suggestion().text == "_a" |
| 313 |
|
| 314 |
|
| 315 |
@pytest.mark.asyncio |
| 316 |
async def test_navigable_provider_multiline_entries(): |
| 317 |
provider = NavigableAutoSuggestFromHistory() |
| 318 |
history = InMemoryHistory(history_strings=["very_a\nvery_b", "very_c"]) |
| 319 |
buffer = Buffer(history=history) |
| 320 |
ip = get_ipython() |
| 321 |
ip.auto_suggest = provider |
| 322 |
|
| 323 |
async for _ in history.load(): |
| 324 |
pass |
| 325 |
|
| 326 |
buffer.cursor_position = 5 |
| 327 |
buffer.text = "very" |
| 328 |
up = swap_autosuggestion_up |
| 329 |
down = swap_autosuggestion_down |
| 330 |
|
| 331 |
event = Mock() |
| 332 |
event.current_buffer = buffer |
| 333 |
|
| 334 |
def get_suggestion(): |
| 335 |
suggestion = provider.get_suggestion(buffer, buffer.document) |
| 336 |
buffer.suggestion = suggestion |
| 337 |
return suggestion |
| 338 |
|
| 339 |
assert get_suggestion().text == "_c" |
| 340 |
|
| 341 |
up(event) |
| 342 |
assert get_suggestion().text == "_b" |
| 343 |
|
| 344 |
up(event) |
| 345 |
assert get_suggestion().text == "_a" |
| 346 |
|
| 347 |
down(event) |
| 348 |
assert get_suggestion().text == "_b" |
| 349 |
|
| 350 |
down(event) |
| 351 |
assert get_suggestion().text == "_c" |
| 352 |
|
| 353 |
|
| 354 |
def create_session_mock(): |
| 355 |
session = Mock() |
| 356 |
session.default_buffer = Buffer() |
| 357 |
return session |
| 358 |
|
| 359 |
|
| 360 |
def test_navigable_provider_connection(): |
| 361 |
provider = NavigableAutoSuggestFromHistory() |
| 362 |
provider.skip_lines = 1 |
| 363 |
|
| 364 |
session_1 = create_session_mock() |
| 365 |
provider.connect(session_1) |
| 366 |
|
| 367 |
assert provider.skip_lines == 1 |
| 368 |
session_1.default_buffer.on_text_insert.fire() |
| 369 |
assert provider.skip_lines == 0 |
| 370 |
|
| 371 |
session_2 = create_session_mock() |
| 372 |
provider.connect(session_2) |
| 373 |
provider.skip_lines = 2 |
| 374 |
|
| 375 |
assert provider.skip_lines == 2 |
| 376 |
session_2.default_buffer.on_text_insert.fire() |
| 377 |
assert provider.skip_lines == 0 |
| 378 |
|
| 379 |
provider.skip_lines = 3 |
| 380 |
provider.disconnect() |
| 381 |
session_1.default_buffer.on_text_insert.fire() |
| 382 |
session_2.default_buffer.on_text_insert.fire() |
| 383 |
assert provider.skip_lines == 3 |
| 384 |
|
| 385 |
|
| 386 |
@pytest.fixture |
| 387 |
def ipython_with_prompt(): |
| 388 |
ip = get_ipython() |
| 389 |
ip.pt_app = Mock() |
| 390 |
ip.pt_app.key_bindings = create_ipython_shortcuts(ip) |
| 391 |
try: |
| 392 |
yield ip |
| 393 |
finally: |
| 394 |
ip.pt_app = None |
| 395 |
|
| 396 |
|
| 397 |
def find_bindings_by_command(command): |
| 398 |
ip = get_ipython() |
| 399 |
return [ |
| 400 |
binding |
| 401 |
for binding in ip.pt_app.key_bindings.bindings |
| 402 |
if binding.handler == command |
| 403 |
] |
| 404 |
|
| 405 |
|
| 406 |
def test_modify_unique_shortcut(ipython_with_prompt): |
| 407 |
original = find_bindings_by_command(accept_token) |
| 408 |
assert len(original) == 1 |
| 409 |
|
| 410 |
ipython_with_prompt.shortcuts = [ |
| 411 |
{"command": "IPython:auto_suggest.accept_token", "new_keys": ["a", "b", "c"]} |
| 412 |
] |
| 413 |
matched = find_bindings_by_command(accept_token) |
| 414 |
assert len(matched) == 1 |
| 415 |
assert list(matched[0].keys) == ["a", "b", "c"] |
| 416 |
assert list(matched[0].keys) != list(original[0].keys) |
| 417 |
assert matched[0].filter == original[0].filter |
| 418 |
|
| 419 |
ipython_with_prompt.shortcuts = [ |
| 420 |
{"command": "IPython:auto_suggest.accept_token", "new_filter": "always"} |
| 421 |
] |
| 422 |
matched = find_bindings_by_command(accept_token) |
| 423 |
assert len(matched) == 1 |
| 424 |
assert list(matched[0].keys) != ["a", "b", "c"] |
| 425 |
assert list(matched[0].keys) == list(original[0].keys) |
| 426 |
assert matched[0].filter != original[0].filter |
| 427 |
|
| 428 |
|
| 429 |
def test_disable_shortcut(ipython_with_prompt): |
| 430 |
matched = find_bindings_by_command(accept_token) |
| 431 |
assert len(matched) == 1 |
| 432 |
|
| 433 |
ipython_with_prompt.shortcuts = [ |
| 434 |
{"command": "IPython:auto_suggest.accept_token", "new_keys": []} |
| 435 |
] |
| 436 |
matched = find_bindings_by_command(accept_token) |
| 437 |
assert len(matched) == 0 |
| 438 |
|
| 439 |
ipython_with_prompt.shortcuts = [] |
| 440 |
matched = find_bindings_by_command(accept_token) |
| 441 |
assert len(matched) == 1 |
| 442 |
|
| 443 |
|
| 444 |
def test_modify_shortcut_with_filters(ipython_with_prompt): |
| 445 |
matched = find_bindings_by_command(skip_over) |
| 446 |
matched_keys = {m.keys[0] for m in matched} |
| 447 |
assert matched_keys == {")", "]", "}", "'", '"'} |
| 448 |
|
| 449 |
with pytest.raises(ValueError, match="Multiple shortcuts matching"): |
| 450 |
ipython_with_prompt.shortcuts = [ |
| 451 |
{"command": "IPython:auto_match.skip_over", "new_keys": ["x"]} |
| 452 |
] |
| 453 |
|
| 454 |
ipython_with_prompt.shortcuts = [ |
| 455 |
{ |
| 456 |
"command": "IPython:auto_match.skip_over", |
| 457 |
"new_keys": ["x"], |
| 458 |
"match_filter": "focused_insert & auto_match & followed_by_single_quote", |
| 459 |
} |
| 460 |
] |
| 461 |
matched = find_bindings_by_command(skip_over) |
| 462 |
matched_keys = {m.keys[0] for m in matched} |
| 463 |
assert matched_keys == {")", "]", "}", "x", '"'} |
| 464 |
|
| 465 |
|
| 466 |
def example_command(): |
| 467 |
pass |
| 468 |
|
| 469 |
|
| 470 |
def test_add_shortcut_for_new_command(ipython_with_prompt): |
| 471 |
matched = find_bindings_by_command(example_command) |
| 472 |
assert len(matched) == 0 |
| 473 |
|
| 474 |
with pytest.raises(ValueError, match="example_command is not a known"): |
| 475 |
ipython_with_prompt.shortcuts = [ |
| 476 |
{"command": "example_command", "new_keys": ["x"]} |
| 477 |
] |
| 478 |
matched = find_bindings_by_command(example_command) |
| 479 |
assert len(matched) == 0 |
| 480 |
|
| 481 |
|
| 482 |
def test_modify_shortcut_failure(ipython_with_prompt): |
| 483 |
with pytest.raises(ValueError, match="No shortcuts matching"): |
| 484 |
ipython_with_prompt.shortcuts = [ |
| 485 |
{ |
| 486 |
"command": "IPython:auto_match.skip_over", |
| 487 |
"match_keys": ["x"], |
| 488 |
"new_keys": ["y"], |
| 489 |
} |
| 490 |
] |
| 491 |
|
| 492 |
|
| 493 |
def test_add_shortcut_for_existing_command(ipython_with_prompt): |
| 494 |
matched = find_bindings_by_command(skip_over) |
| 495 |
assert len(matched) == 5 |
| 496 |
|
| 497 |
with pytest.raises(ValueError, match="Cannot add a shortcut without keys"): |
| 498 |
ipython_with_prompt.shortcuts = [ |
| 499 |
{"command": "IPython:auto_match.skip_over", "new_keys": [], "create": True} |
| 500 |
] |
| 501 |
|
| 502 |
ipython_with_prompt.shortcuts = [ |
| 503 |
{"command": "IPython:auto_match.skip_over", "new_keys": ["x"], "create": True} |
| 504 |
] |
| 505 |
matched = find_bindings_by_command(skip_over) |
| 506 |
assert len(matched) == 6 |
| 507 |
|
| 508 |
ipython_with_prompt.shortcuts = [] |
| 509 |
matched = find_bindings_by_command(skip_over) |
| 510 |
assert len(matched) == 5 |
| 511 |
|
| 512 |
|
| 513 |
def test_setting_shortcuts_before_pt_app_init(): |
| 514 |
ipython = get_ipython() |
| 515 |
assert ipython.pt_app is None |
| 516 |
shortcuts = [ |
| 517 |
{"command": "IPython:auto_match.skip_over", "new_keys": ["x"], "create": True} |
| 518 |
] |
| 519 |
ipython.shortcuts = shortcuts |
| 520 |
assert ipython.shortcuts == shortcuts |