#!/usr/bin/env python3 """Test CLI functionality.""" import sys from pathlib import Path # Add project root to path sys.path.insert(0, str(Path(__file__).parent)) from cli.config import CLIConfig from cli.session import SessionManager def test_config(): """Test configuration management.""" print("Testing configuration...") # Create test config config = CLIConfig() config.email = "test@example.com" config.api_url = "http://localhost:8080" print(f" API URL: {config.api_url}") print(f" Email: {config.email}") print(f" Config dir: {config.config_dir}") print(f" Config file: {config.config_file}") print(f" Sessions file: {config.sessions_file}") print("✓ Configuration works\n") def test_session_manager(): """Test session management.""" print("Testing session management...") # Create test session manager test_dir = Path.home() / ".lc_test" test_dir.mkdir(exist_ok=True) sessions_file = test_dir / "sessions.json" manager = SessionManager(sessions_file) # Create session session = manager.create_session("test") print(f" Created session: {session.name}") print(f" Session ID: {session.session_id}") print(f" Created at: {session.created_at}") # Get session retrieved = manager.get_session("test") assert retrieved is not None assert retrieved.name == "test" print(f" Retrieved session: {retrieved.name}") # Update session manager.update_last_active("test") print(f" Updated session") # List sessions all_sessions = manager.list_sessions() print(f" Total sessions: {len(all_sessions)}") # Delete session deleted = manager.delete_session("test") assert deleted is True print(f" Deleted session") # Clean up sessions_file.unlink(missing_ok=True) test_dir.rmdir() print("✓ Session management works\n") def test_formatter(): """Test response formatting.""" print("Testing response formatter...") from cli.formatters import ResponseFormatter # Create formatter formatter = ResponseFormatter( show_mood=True, show_relationship=True, show_facts=True, use_rich=False, # Plain text for testing ) # Test message formatting message = formatter.format_message("user", "Hello, world!") assert "You:" in message assert "Hello, world!" in message print(f" User message: {message}") message = formatter.format_message("assistant", "Hi there!") assert "Bartender:" in message assert "Hi there!" in message print(f" Assistant message: {message}") # Test response formatting response = { "response": "That sounds heavy.", "mood": { "label": "calm", "valence": 0.2, "arousal": -0.3, "intensity": 0.4, }, "relationship": { "level": "close_friend", "score": 85, "interactions_count": 42, }, "extracted_facts": ["User mentioned feeling heavy"], } formatted = formatter._format_response_plain(response) assert "Bartender:" in formatted assert "That sounds heavy." in formatted print(f" Formatted response preview: {formatted[:50]}...") print("✓ Response formatter works\n") def test_client(): """Test HTTP client (basic instantiation).""" print("Testing HTTP client...") from cli.client import LoyalCompanionClient # Create client client = LoyalCompanionClient("http://localhost:8080", "test_token") assert client.base_url == "http://localhost:8080" assert client.auth_token == "test_token" print(f" Created client for {client.base_url}") # Test headers headers = client._get_headers() assert "Authorization" in headers assert headers["Authorization"] == "Bearer test_token" print(f" Authorization header: {headers['Authorization']}") client.close() print("✓ HTTP client works\n") def main(): """Run all tests.""" print("=" * 60) print("Loyal Companion CLI - Component Tests") print("=" * 60) print() try: test_config() test_session_manager() test_formatter() test_client() print("=" * 60) print("All tests passed! ✓") print("=" * 60) print() print("CLI components are working correctly.") print("To test end-to-end:") print(" 1. Start the web server: python3 run_web.py") print(" 2. Run the CLI: ./lc talk") print() except Exception as e: print(f"\n✗ Test failed: {e}") import traceback traceback.print_exc() sys.exit(1) if __name__ == "__main__": main()