#!/usr/bin/env python3
"""
Enhanced Application Launcher Test with Notepad
Tests the improved launcher logic specifically
"""
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 EnhancedLauncherTest:
"""Enhanced test for the improved launcher logic"""
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("โ
Enhanced launcher test initialized")
except ImportError as e:
logger.error(f"โ Failed to import components: {e}")
raise
async def test_notepad_launch_and_track(self):
"""Test launching Notepad and tracking the actual process"""
try:
logger.info("๐ค Enhanced Notepad Launcher Test")
logger.info("=" * 50)
# Launch Notepad
logger.info("๐ Step 1: Launching Notepad...")
success, message, pid = self.launcher.launch_application('notepad.exe')
if success:
logger.info(f"โ
Launch result: {message}")
logger.info(f"๐ Initial PID: {pid}")
# Check initial status
app_info = self.launcher.get_application_by_pid(pid)
if app_info:
logger.info(f"๐ Initial status: {app_info['status']}")
# Wait and check status updates
for i in range(6): # Check every second for 6 seconds
await asyncio.sleep(1)
logger.info(f"โณ Checking status after {i+1} seconds...")
# Force status update
self.launcher._update_application_status()
# Get all launched applications
launched_apps = self.launcher.get_launched_applications()
for app in launched_apps:
if app['process_name'] == 'notepad.exe':
logger.info(f" ๐ PID {app['pid']}: {app['process_name']} ({app['status']})")
# Final status check
logger.info("\\n๐ Final launched applications:")
final_apps = self.launcher.get_launched_applications()
running_notepad = None
for app in final_apps:
if app['process_name'] == 'notepad.exe':
logger.info(f" โข PID {app['pid']}: {app['process_name']} ({app['status']})")
if app['status'] == 'running':
running_notepad = app
# If we have a running notepad, try to terminate it
if running_notepad:
logger.info(f"\\n๐ Step 2: Terminating Notepad PID {running_notepad['pid']}...")
term_success, term_message = self.launcher.terminate_application(running_notepad['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")
else:
logger.warning("โ ๏ธ No running notepad found to terminate")
return True
else:
logger.error(f"โ Launch failed: {message}")
return False
except Exception as e:
logger.error(f"โ Enhanced test failed: {e}")
return False
async def main():
"""Main function"""
try:
logger.info("๐ฌ Enhanced Application Launcher Test Starting")
logger.info("=" * 40)
# Initialize test
test = EnhancedLauncherTest()
# Run enhanced test
success = await test.test_notepad_launch_and_track()
if success:
logger.info("๐ Enhanced test completed successfully!")
else:
logger.error("โ Enhanced 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())