#!/usr/bin/env python3
"""
Calculator-specific test for the improved launcher logic
Tests that Calculator now shows 'running' status instead of 'terminated'
"""
import logging
import time
import asyncio
import sys
import os
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
class CalculatorTest:
"""Test Calculator launch and status detection"""
def __init__(self):
# Add server path for imports
project_root = os.path.dirname(os.path.dirname(__file__))
server_path = os.path.join(project_root, 'server')
sys.path.insert(0, server_path)
try:
# Import required modules
from config.whitelist import ProcessWhitelist
from process.launcher import ApplicationLauncher
# Initialize components
self.whitelist = ProcessWhitelist()
self.whitelist.load_whitelist(os.path.join(server_path, 'process_whitelist.json'))
self.launcher = ApplicationLauncher(self.whitelist)
logger.info("โ
Calculator test initialized")
except ImportError as e:
logger.error(f"โ Failed to import components: {e}")
raise
async def test_calculator_launch_and_status(self):
"""Test launching Calculator and verifying it shows as running"""
try:
logger.info("๐งฎ Calculator Launch & Status Test")
logger.info("=" * 50)
# Launch Calculator
logger.info("๐ Step 1: Launching Calculator...")
success, message, pid = self.launcher.launch_application('calc.exe')
if success:
logger.info(f"โ
Launch result: {message}")
logger.info(f"๐ Initial PID: {pid}")
# Wait for potential PID updates
logger.info("โณ Waiting for PID resolution...")
await asyncio.sleep(3)
# Check final status
logger.info("๐ Checking launched applications...")
launched_apps = self.launcher.get_launched_applications()
calc_apps = [app for app in launched_apps if 'calc' in app['process_name']]
if calc_apps:
for app in calc_apps:
logger.info(f" โข PID {app['pid']}: {app['process_name']} ({app['status']})")
if app['status'] == 'running':
logger.info("๐ Calculator is correctly showing as RUNNING!")
# Try to terminate it using the current PID
logger.info(f"\\n๐ Step 2: Terminating Calculator PID {app['pid']}...")
term_success, term_message = self.launcher.terminate_application(app['pid'])
if term_success:
logger.info(f"โ
{term_message}")
else:
logger.warning(f"โ ๏ธ {term_message}")
await asyncio.sleep(2)
# Cleanup
removed = self.launcher.cleanup_terminated_applications()
logger.info(f"๐งน Cleaned up {removed} terminated applications")
return True
else:
logger.error(f"โ Calculator shows status: {app['status']} (expected: running)")
return False
else:
logger.error("โ No calculator applications found in launched apps")
return False
else:
logger.error(f"โ Launch failed: {message}")
return False
except Exception as e:
logger.error(f"โ Calculator test failed: {e}")
return False
async def main():
"""Main function"""
try:
logger.info("๐ฌ Calculator Test Starting")
logger.info("=" * 40)
# Initialize test
test = CalculatorTest()
# Run test
success = await test.test_calculator_launch_and_status()
if success:
logger.info("๐ Calculator test completed successfully!")
logger.info("โ
Calculator now properly shows 'running' status!")
else:
logger.error("โ Calculator test failed")
except KeyboardInterrupt:
logger.info("๐ Test interrupted by user")
except Exception as e:
logger.error(f"๐ฅ Unexpected error: {e}")
if __name__ == "__main__":
asyncio.run(main())