"""
Live-GUI smoke test for the tkinter.filedialog AttributeError regression.

On Python installs where the Tcl/Tk runtime is missing, the lazy
`tkinter.filedialog` import raises AttributeError, which previously
crashed the Project Settings window and the Add Project button.
The unit-level test in `test_lazymodule_filedialog_fallback.py`
deterministically exercises the fallback path; this live test verifies
the same fix in the actual running app: opening the Project Settings
window via the Hook API must not produce an AttributeError, and the
app must remain responsive (proving no crash on attribute resolution).
"""
import time
from pathlib import Path
import pytest
from src.api_hook_client import ApiHookClient


def test_live_gui_project_settings_opens_without_filedialog_crash(live_gui) -> None:
 """
 Regression: the Project Settings window's render call chain ends
 in `render_projects_panel` → `filedialog.askopenfilename(...)` on
 the "Add Project" click frame. Before the fix, every frame the
 Project Settings window was open on a broken tkinter install would
 log `AttributeError: module 'tkinter' has no attribute 'filedialog'`.
 The fix in `_LazyModule._resolve()` falls back to a `_FiledialogStub`
 that returns empty strings.

 This test:
 1. Opens the Project Settings window via the Hook API
 2. Waits several render frames
 3. Verifies the window opened (state is reflected back via get_value)
 4. Verifies the app is still responsive (status endpoint returns 200)
 5. Verifies no AttributeError was logged (the bug would print to
    the GUI's stderr, which the live_gui fixture captures to a log)
 """
 handle = live_gui
 process = handle.process
 gui_script = handle.gui_script
 client = ApiHookClient()

 log_path = Path(f"logs/{Path(gui_script).name.replace('.', '_')}_test.log")
 log_offset_before = log_path.stat().st_size if log_path.exists() else 0

 client.set_value('show_windows["Project Settings"]', True)
 time.sleep(2.0)

 opened = client.get_value('show_windows["Project Settings"]')
 assert opened is True, f"Project Settings window did not open: {opened}"

 status = client.get_status()
 assert status is not None, "App status endpoint returned None — app is not responsive"
 assert status.get("status") == "ok", f"App status not ok: {status}"

 time.sleep(1.0)
 if log_path.exists():
  with log_path.open("r", encoding="utf-8", errors="ignore") as f:
   f.seek(log_offset_before)
   new_log = f.read()
  assert "AttributeError: module 'tkinter' has no attribute 'filedialog'" not in new_log, (
   "GUI logged 'AttributeError: module tkinter has no attribute filedialog' "
   "after opening Project Settings. The _LazyModule fallback to _FiledialogStub "
   "is not working in the live app."
  )
  assert "AttributeError: module 'tkinter' has no attribute 'filedialog'" not in new_log
  if "AttributeError" in new_log:
   pytest.fail(f"App logged unexpected AttributeError: {new_log[max(0, new_log.find('AttributeError')-200):new_log.find('AttributeError')+200]}")
