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"] # Switch to Context Composition tab to ensure it's rendered client.push_event("select_tab", {"tab": "Context Composition"}) time.sleep(1.0) # Inject context state directly client.push_event("custom_callback", {"callback": "set_context_files_for_test", "args": [test_files]}) client.push_event("custom_callback", {"callback": "set_screenshots_for_test", "args": [test_screenshots]}) client.push_event("custom_callback", {"callback": "set_ui_attr", "args": ["ui_new_context_preset_name", preset_name]}) time.sleep(1.0) # Trigger Save (which will trigger validation, and since test.py doesn't exist, it opens a modal) client.push_event("custom_callback", {"callback": "set_ui_attr", "args": ["_pending_save_ctx_click", True]}) time.sleep(1.0) # The "Missing Files Warning" modal should be open. Trigger "Save Anyway". client.push_event("custom_callback", {"callback": "set_ui_attr", "args": ["_pending_save_anyway_click", True]}) 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 # Clear current state client.push_event("custom_callback", {"callback": "set_context_files_for_test", "args": [[]]}) client.push_event("custom_callback", {"callback": "set_screenshots_for_test", "args": [[]]}) time.sleep(1.0) # 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