import pytest
import time
from src.api_hook_client import ApiHookClient

def test_gui_context_preset_save_load(live_gui) -> None:
 """Verify that saving and loading context presets works via the GUI app."""
 client = ApiHookClient()
 assert client.wait_for_server(timeout=15)

 preset_name = "test_gui_preset"
 test_files = ["test.py"]
 test_screenshots = ["test.png"]

 client.push_event("custom_callback", {"callback": "simulate_save_preset", "args": [preset_name]})
 time.sleep(1.5)

 project_data = client.get_project()
 project = project_data.get("project", {})
 presets = project.get("context_presets", {})
 
 assert preset_name in presets, f"Preset '{preset_name}' not found in project context_presets"
 
 preset_entry = presets[preset_name]
 preset_files = [f["path"] if isinstance(f, dict) else str(f) for f in preset_entry.get("files", [])]
 assert preset_files == test_files
 assert preset_entry.get("screenshots", []) == test_screenshots

 # Load the preset
 client.push_event("custom_callback", {"callback": "load_context_preset", "args": [preset_name]})
 time.sleep(1.0)

 context = client.get_context_state()
 loaded_files = [f["path"] if isinstance(f, dict) else str(f) for f in context.get("files", [])]
 assert loaded_files == test_files
 assert context.get("screenshots", []) == test_screenshots
