feat(gui2): Implement missing GUI hook handlers
This commit is contained in:
@@ -24,54 +24,63 @@ def kill_process_tree(pid):
|
||||
except Exception as e:
|
||||
print(f"[Fixture] Error killing process tree {pid}: {e}")
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def live_gui():
|
||||
@pytest.fixture(scope="session", params=["gui.py", "gui_2.py"])
|
||||
def live_gui(request):
|
||||
"""
|
||||
Session-scoped fixture that starts gui.py with --enable-test-hooks.
|
||||
Ensures the GUI is running before tests start and shuts it down after.
|
||||
Session-scoped fixture that starts a GUI script with --enable-test-hooks.
|
||||
Parameterized to run either gui.py or gui_2.py.
|
||||
"""
|
||||
print("\n[Fixture] Starting gui.py --enable-test-hooks...")
|
||||
gui_script = request.param
|
||||
print(f"\n[Fixture] Starting {gui_script} --enable-test-hooks...")
|
||||
|
||||
# Ensure logs directory exists
|
||||
os.makedirs("logs", exist_ok=True)
|
||||
log_file = open("logs/gui_test.log", "w", encoding="utf-8")
|
||||
log_file = open(f"logs/{gui_script.replace('.', '_')}_test.log", "w", encoding="utf-8")
|
||||
|
||||
# Start gui.py as a subprocess.
|
||||
process = subprocess.Popen(
|
||||
["uv", "run", "python", "gui.py", "--enable-test-hooks"],
|
||||
["uv", "run", "python", gui_script, "--enable-test-hooks"],
|
||||
stdout=log_file,
|
||||
stderr=log_file,
|
||||
text=True,
|
||||
creationflags=subprocess.CREATE_NEW_PROCESS_GROUP if os.name == 'nt' else 0
|
||||
)
|
||||
|
||||
# Wait for the hook server to be ready (Port 8999 per api_hooks.py)
|
||||
max_retries = 5
|
||||
max_retries = 10 # Increased for potentially slower startup of gui_2
|
||||
ready = False
|
||||
print(f"[Fixture] Waiting up to {max_retries}s for Hook Server on port 8999...")
|
||||
|
||||
start_time = time.time()
|
||||
while time.time() - start_time < max_retries:
|
||||
try:
|
||||
# Using /status endpoint defined in HookHandler
|
||||
response = requests.get("http://127.0.0.1:8999/status", timeout=0.5)
|
||||
if response.status_code == 200:
|
||||
ready = True
|
||||
print(f"[Fixture] GUI Hook Server is ready after {round(time.time() - start_time, 2)}s.")
|
||||
print(f"[Fixture] GUI Hook Server for {gui_script} is ready after {round(time.time() - start_time, 2)}s.")
|
||||
break
|
||||
except (requests.exceptions.ConnectionError, requests.exceptions.Timeout):
|
||||
if process.poll() is not None:
|
||||
print("[Fixture] Process died unexpectedly during startup.")
|
||||
print(f"[Fixture] {gui_script} process died unexpectedly during startup.")
|
||||
break
|
||||
time.sleep(0.5)
|
||||
|
||||
if not ready:
|
||||
print("[Fixture] TIMEOUT/FAILURE: Hook server failed to respond on port 8999 within 5s. Cleaning up...")
|
||||
print(f"[Fixture] TIMEOUT/FAILURE: Hook server for {gui_script} failed to respond.")
|
||||
kill_process_tree(process.pid)
|
||||
pytest.fail("Failed to start gui.py with test hooks within 5 seconds.")
|
||||
pytest.fail(f"Failed to start {gui_script} with test hooks.")
|
||||
|
||||
try:
|
||||
yield process
|
||||
yield process, gui_script
|
||||
finally:
|
||||
print("\n[Fixture] Finally block triggered: Shutting down gui.py...")
|
||||
print(f"\n[Fixture] Finally block triggered: Shutting down {gui_script}...")
|
||||
kill_process_tree(process.pid)
|
||||
log_file.close()
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def live_gui_2(live_gui):
|
||||
"""
|
||||
A specific instance of the live_gui fixture that only runs for gui_2.py.
|
||||
This simplifies tests that are specific to gui_2.py.
|
||||
"""
|
||||
process, gui_script = live_gui
|
||||
if gui_script != "gui_2.py":
|
||||
pytest.skip("This test is only for gui_2.py")
|
||||
return process
|
||||
|
||||
Reference in New Issue
Block a user