import pytest import time import json import os import uuid from pathlib import Path import sys # Ensure project root is in path for imports sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))) from api_hook_client import ApiHookClient # Define a temporary file path for callback testing TEST_CALLBACK_FILE = Path("temp_callback_output.txt") @pytest.fixture(scope="function", autouse=True) def cleanup_callback_file(): """Ensures the test callback file is cleaned up before and after each test.""" if TEST_CALLBACK_FILE.exists(): TEST_CALLBACK_FILE.unlink() yield if TEST_CALLBACK_FILE.exists(): TEST_CALLBACK_FILE.unlink() def test_gui2_set_value_hook_works(live_gui): """ Tests that the 'set_value' GUI hook is correctly implemented. """ client = ApiHookClient() assert client.wait_for_server(timeout=10) test_value = f"New value set by test: {uuid.uuid4()}" gui_data = {'action': 'set_value', 'item': 'ai_input', 'value': test_value} response = client.post_gui(gui_data) assert response == {'status': 'queued'} # Verify the value was actually set using the new get_value hook time.sleep(0.5) current_value = client.get_value('ai_input') assert current_value == test_value def test_gui2_click_hook_works(live_gui): """ Tests that the 'click' GUI hook for the 'Reset' button is implemented. """ client = ApiHookClient() assert client.wait_for_server(timeout=10) # First, set some state that 'Reset' would clear. test_value = "This text should be cleared by the reset button." client.set_value('ai_input', test_value) time.sleep(0.5) assert client.get_value('ai_input') == test_value # Now, trigger the click client.click('btn_reset') time.sleep(0.5) # Verify it was reset assert client.get_value('ai_input') == "" def test_gui2_custom_callback_hook_works(live_gui): """ Tests that the 'custom_callback' GUI hook is correctly implemented. """ client = ApiHookClient() assert client.wait_for_server(timeout=10) test_data = f"Callback executed: {uuid.uuid4()}" gui_data = { 'action': 'custom_callback', 'callback': '_test_callback_func_write_to_file', 'args': [test_data] } response = client.post_gui(gui_data) assert response == {'status': 'queued'} time.sleep(1) # Give gui_2.py time to process its task queue # Assert that the file WAS created and contains the correct data assert TEST_CALLBACK_FILE.exists(), "Custom callback was NOT executed, or file path is wrong!" with open(TEST_CALLBACK_FILE, "r") as f: content = f.read() assert content == test_data, "Callback executed, but file content is incorrect."