tt/tests/test_editor_movement.py
2025-01-05 20:21:27 -06:00

113 lines
3.6 KiB
Python

from tt.tui.editor import TableEditor, TableColumnConfig
import pytest
class DummyDataEditor(TableEditor):
TABLE_CONFIG = [
TableColumnConfig("id", "ID"),
TableColumnConfig("name", "Name"),
TableColumnConfig("status", "Status"),
]
def refresh_items(self):
self.table.add_rows(
[
["1", "Item 1", "Active"],
["2", "Item 2", "Pending"],
["3", "Item 3", "Done"],
]
)
@pytest.mark.asyncio
async def test_basic_movement():
app = DummyDataEditor()
async with app.run_test() as pilot:
# Add some test data
# Initial position should be (0,1) - first row, name column
assert app.table.cursor_row == 0
assert app.table.cursor_column == 1
# Test right movement (l key)
await pilot.press("l")
assert app.table.cursor_column == 2, "Cursor should move right"
assert app.table.cursor_type == "cell"
# Test left movement (h key)
await pilot.press("h")
assert app.table.cursor_column == 1, "Cursor should move left"
# Test moving to row select mode
await pilot.press("h")
assert app.table.cursor_column == 0, "Cursor should move to ID column"
assert app.table.cursor_type == "row", "Should be in row select mode"
# Test down movement (j key)
await pilot.press("j")
assert app.table.cursor_row == 1, "Cursor should move down"
assert app.table.cursor_column == 0, "Column should remain the same"
# Test up movement (k key)
await pilot.press("k")
assert app.table.cursor_row == 0, "Cursor should move up"
assert app.table.cursor_column == 0, "Column should remain the same"
@pytest.mark.asyncio
async def test_movement_boundaries():
"""Test movement at table boundaries"""
app = DummyDataEditor()
async with app.run_test() as pilot:
# Test left boundary
await pilot.press("h")
assert app.table.cursor_column == 0, "Should stop at leftmost column"
await pilot.press("h")
assert app.table.cursor_column == 0, "Should not go past left boundary"
# Test right boundary
await pilot.press("l")
await pilot.press("l")
await pilot.press("l")
assert app.table.cursor_column == 2, "Should stop at rightmost column"
await pilot.press("l")
assert app.table.cursor_column == 2, "Should not go past right boundary"
# Test top boundary
await pilot.press("k")
assert app.table.cursor_row == 0, "Should stop at top row"
await pilot.press("k")
assert app.table.cursor_row == 0, "Should not go past top boundary"
# Test bottom boundary
await pilot.press("j")
await pilot.press("j")
await pilot.press("j")
assert app.table.cursor_row == 2, "Should stop at bottom row"
await pilot.press("j")
assert app.table.cursor_row == 2, "Should not go past bottom boundary"
@pytest.mark.asyncio
async def test_save_on_move():
app = DummyDataEditor()
async with app.run_test() as pilot:
update_called = False
def mock_update(**kwargs):
nonlocal update_called
update_called = True
app.update_item_callback = mock_update
# register a save on move
app._register_save_on_move(1, name="Updated Item")
# move cursor and check if save was triggered
await pilot.press("j")
assert update_called, "Save should trigger on movement"
assert app.save_on_move is None, "save_on_move should be cleared after save"