#!/usr/bin/env python3 """ Test script to verify SelectUI works as a library. This doesn't require user interaction - just checks that the API works. """ import os import sys sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', 'src')) from selectui import SelectUI def test_initialization(): """Test that SelectUI can be initialized with items.""" items = [ {"title": "Item 1", "subtitle": "Description 1"}, {"title": "Item 2", "subtitle": "Description 2"}, ] app = SelectUI( items=items, oneshot=True, title_key="title", subtitle_key="subtitle" ) assert app.items == items assert app.oneshot assert app.title_key == "title" assert app.subtitle_key == "subtitle" assert app.selected_item is None print("✓ Initialization test passed") def test_custom_keys(): """Test custom title/subtitle keys.""" items = [ {"name": "Alice", "role": "Engineer"}, {"name": "Bob", "role": "Designer"}, ] app = SelectUI( items=items, title_key="name", subtitle_key="role" ) assert app.title_key == "name" assert app.subtitle_key == "role" print("✓ Custom keys test passed") def test_info_key(): """Test info_key parameter.""" items = [ {"title": "Item 1", "year": "2024"}, ] app = SelectUI( items=items, info_key="year" ) assert app.info_key == "year" print("✓ Info key test passed") def test_events_mode(): """Test events mode initialization.""" app = SelectUI( items=[], events_mode=True ) assert app.events_mode print("✓ Events mode test passed") def test_command_template(): """Test command template initialization.""" app = SelectUI( items=[], command_template="echo {}" ) assert app.command_template == "echo {}" assert app.input_mode print("✓ Command template test passed") def test_single_line_mode(): """Test single-line display (no subtitle).""" items = [ {"filename": "test.py"}, ] app = SelectUI( items=items, title_key="filename", subtitle_key=None ) assert app.subtitle_key is None print("✓ Single-line mode test passed") if __name__ == "__main__": print("Running SelectUI library tests...\n") try: test_initialization() test_custom_keys() test_info_key() test_events_mode() test_command_template() test_single_line_mode() print("\n✅ All tests passed!") print("\nSelectUI is ready to use as a library.") print("See examples/README.md for usage examples.") except AssertionError as e: print(f"\n❌ Test failed: {e}") sys.exit(1) except Exception as e: print(f"\n❌ Error: {e}") import traceback traceback.print_exc() sys.exit(1)