239 lines
7.2 KiB
Python
239 lines
7.2 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Context system test runner script.
|
|
|
|
Features:
|
|
1. Check whether the Redis service is available
|
|
2. Run all Context system tests
|
|
3. Generate a test report
|
|
"""
|
|
|
|
import os
|
|
import sys
|
|
import subprocess
|
|
import time
|
|
import redis
|
|
from typing import Optional, Tuple
|
|
|
|
# Add the project root directory to the Python path.
|
|
project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
sys.path.insert(0, project_root)
|
|
|
|
|
|
def check_redis_connection(host: str = "localhost", port: int = 6379, timeout: int = 5) -> Tuple[bool, str]:
|
|
"""
|
|
Check whether the Redis connection is available.
|
|
|
|
Args:
|
|
host: Redis host address
|
|
port: Redis port
|
|
timeout: Connection timeout in seconds
|
|
|
|
Returns:
|
|
(whether the connection succeeded, error message)
|
|
"""
|
|
try:
|
|
r = redis.Redis(host=host, port=port, socket_connect_timeout=timeout, decode_responses=True)
|
|
r.ping()
|
|
r.close()
|
|
return True, "Redis connection is healthy"
|
|
except redis.ConnectionError as e:
|
|
return False, f"Redis connection failed: {e}"
|
|
except Exception as e:
|
|
return False, f"Redis check exception: {e}"
|
|
|
|
|
|
def start_redis_server() -> bool:
|
|
"""
|
|
Try to start the Redis server.
|
|
|
|
Returns:
|
|
Whether startup succeeded.
|
|
"""
|
|
try:
|
|
# Check whether Redis is already running.
|
|
if check_redis_connection()[0]:
|
|
print("✓ Redis service is already running")
|
|
return True
|
|
|
|
# Try to start Redis.
|
|
print("Trying to start the Redis service...")
|
|
|
|
# Try to start Redis on macOS.
|
|
if sys.platform == "darwin":
|
|
try:
|
|
# Start Redis with brew.
|
|
subprocess.run(["brew", "services", "start", "redis"],
|
|
check=True, capture_output=True, timeout=10)
|
|
time.sleep(2) # Wait for the service to start.
|
|
if check_redis_connection()[0]:
|
|
print("✓ Redis service started successfully")
|
|
return True
|
|
except subprocess.CalledProcessError:
|
|
pass
|
|
|
|
try:
|
|
# Start Redis directly.
|
|
subprocess.run(["redis-server", "--daemonize", "yes"],
|
|
check=True, capture_output=True, timeout=10)
|
|
time.sleep(2)
|
|
if check_redis_connection()[0]:
|
|
print("✓ Redis service started successfully")
|
|
return True
|
|
except subprocess.CalledProcessError:
|
|
pass
|
|
|
|
# Try to start Redis on Linux.
|
|
elif sys.platform.startswith("linux"):
|
|
try:
|
|
subprocess.run(["sudo", "systemctl", "start", "redis"],
|
|
check=True, capture_output=True, timeout=10)
|
|
time.sleep(2)
|
|
if check_redis_connection()[0]:
|
|
print("✓ Redis service started successfully")
|
|
return True
|
|
except subprocess.CalledProcessError:
|
|
pass
|
|
|
|
print("⚠️ Unable to start Redis automatically; please start it manually")
|
|
return False
|
|
|
|
except Exception as e:
|
|
print(f"⚠️ Error while starting Redis service: {e}")
|
|
return False
|
|
|
|
|
|
def install_test_dependencies() -> bool:
|
|
"""
|
|
Install test dependencies.
|
|
|
|
Returns:
|
|
Whether installation succeeded.
|
|
"""
|
|
try:
|
|
print("Checking test dependencies...")
|
|
|
|
# Check pytest.
|
|
try:
|
|
import pytest
|
|
print("✓ pytest is installed")
|
|
except ImportError:
|
|
print("Installing pytest...")
|
|
subprocess.run([sys.executable, "-m", "pip", "install", "pytest", "pytest-asyncio"],
|
|
check=True)
|
|
print("✓ pytest installation complete")
|
|
|
|
# Check redis.
|
|
try:
|
|
import redis
|
|
print("✓ redis is installed")
|
|
except ImportError:
|
|
print("Installing redis...")
|
|
subprocess.run([sys.executable, "-m", "pip", "install", "redis"],
|
|
check=True)
|
|
print("✓ redis installation complete")
|
|
|
|
return True
|
|
|
|
except Exception as e:
|
|
print(f"⚠️ Error while installing test dependencies: {e}")
|
|
return False
|
|
|
|
|
|
def run_tests() -> bool:
|
|
"""
|
|
Run Context system tests.
|
|
|
|
Returns:
|
|
Whether all tests passed.
|
|
"""
|
|
try:
|
|
print("\n" + "="*60)
|
|
print("Starting Context system tests")
|
|
print("="*60)
|
|
|
|
# Get the test file path.
|
|
test_file = os.path.join(project_root, "test", "test_context_system.py")
|
|
|
|
if not os.path.exists(test_file):
|
|
print(f"❌ Test file does not exist: {test_file}")
|
|
return False
|
|
|
|
# Run tests.
|
|
cmd = [
|
|
sys.executable, "-m", "pytest",
|
|
test_file,
|
|
"-v", # Verbose output.
|
|
"-s", # Show print output.
|
|
"--tb=short", # Short traceback.
|
|
"--color=yes" # Colored output.
|
|
]
|
|
|
|
print(f"Executing command: {' '.join(cmd)}")
|
|
print("-" * 60)
|
|
|
|
result = subprocess.run(cmd, cwd=project_root)
|
|
|
|
print("-" * 60)
|
|
if result.returncode == 0:
|
|
app_log("✅ All tests passed!")
|
|
return True
|
|
else:
|
|
print("❌ Some tests failed")
|
|
return False
|
|
|
|
except Exception as e:
|
|
print(f"❌ Error while running tests: {e}")
|
|
return False
|
|
|
|
|
|
def main():
|
|
"""Main function."""
|
|
print("Context System Test Runner")
|
|
print("=" * 40)
|
|
|
|
# 1. Install dependencies.
|
|
if not install_test_dependencies():
|
|
print("❌ Dependency installation failed; exiting")
|
|
return 1
|
|
|
|
# 2. Check Redis connection.
|
|
print("\nChecking Redis service...")
|
|
redis_ok, redis_msg = check_redis_connection()
|
|
|
|
if not redis_ok:
|
|
print(f"⚠️ {redis_msg}")
|
|
print("Trying to start Redis service...")
|
|
|
|
if not start_redis_server():
|
|
print("❌ Unable to start Redis service")
|
|
print("Please start Redis manually and rerun the tests")
|
|
print("Example startup commands:")
|
|
print(" macOS: brew services start redis")
|
|
print(" Linux: sudo systemctl start redis")
|
|
print(" Windows: redis-server")
|
|
return 1
|
|
else:
|
|
print(f"✓ {redis_msg}")
|
|
|
|
# 3. Run tests.
|
|
success = run_tests()
|
|
|
|
if success:
|
|
print("\n🎉 Context system tests complete!")
|
|
print("The test results verify:")
|
|
print(" ✓ Redis storage works correctly")
|
|
print(" ✓ File system persistence works correctly")
|
|
print(" ✓ Message management works correctly")
|
|
print(" ✓ Context manager works correctly")
|
|
print(" ✓ Data serialization works correctly")
|
|
print(" ✓ Concurrent access is safe")
|
|
return 0
|
|
else:
|
|
print("\n❌ Context system tests failed")
|
|
return 1
|
|
|
|
|
|
if __name__ == "__main__":
|
|
exit(main())
|