import sys
import os
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent.parent.resolve()))

import asyncio
from simulation.sim_base import run_sim
from simulation.sim_tools import click_tab, click_button_by_label
from src.gui_2 import App
from src.models import FileItem

async def verify_preview(app: App):
    # Let the GUI render once
    await asyncio.sleep(0.1)

    app.context_files = [FileItem(path='src/aggregate.py', auto_aggregate=True)]
    app.controller.context_files = app.context_files
    
    # Try rendering _render_files_and_media
    app.show_windows["Context Preview"] = True
    
    await asyncio.sleep(0.1)
    click_tab(app, "Context Composition")
    await asyncio.sleep(0.1)

    click_button_by_label(app, "Preview##ctx")
    await asyncio.sleep(0.2)

    print("--- PREVIEW TEXT ---")
    print(repr(app.context_preview_text[:200]))
    print("--------------------")

if __name__ == "__main__":
    os.environ['SLOP_TEST_HOOKS'] = '1'
    run_sim(verify_preview)