import asyncio import os from pathlib import Path import httpx from nicegui import app from nicegui.testing.user_simulation import user_simulation def test_nicegui_pages_and_navigation(monkeypatch,tmp_path): monkeypatch.setenv('NICEGUI_STORAGE_SECRET','test-ui-only') monkeypatch.setenv('COOKIE_SECURE','0') monkeypatch.setenv('UI_HOST','127.0.0.1') now='2026-11-04T10:00:00+00:00' summary={'servers':[{'id':'9d7b98c8-ce9e-4eeb-9a26-599b2f0f7ccd','name':'test-host','segment':'Production','address':'127.0.0.1','status':'Online','last_seen':now,'sample_at':now,'cpu':25,'ram':40,'disk':50,'maintenance':False,'services':{}}],'online':1,'workers':2,'completed':5,'success_rate':100,'chart':[{'bucket':now,'success':5,'failed':0}],'open_alarms':0,'segments':[{'name':'Production','cidr':'10.0.0.0/24'}],'updated_at':now} async def fake_api(method,path,**kwargs): data={'/auth/me/':{'username':'qa','is_staff':True},'/summary/':summary,'/tasks/':{'count':0,'results':[]},'/events/':{'count':0,'results':[]},'/alarms/':{'count':0,'results':[]},'/flower/':[],'/settings/':{'retention_days':90,'heartbeat_timeout':60,'cpu_warning':85,'ram_warning':90,'disk_warning':90}}.get(path,{}) return httpx.Response(200,json=data) async def scenario(): async with user_simulation(main_file=Path('frontend/main.py')) as user: # The route closure retains the actual module namespace loaded by runpy. endpoint=next(r.endpoint for r in app.routes if getattr(r,'path',None)=='/') endpoint.__globals__['api']=fake_api # NiceGUI's page wrapper retains the original page function. for route in app.routes: fn=getattr(route,'endpoint',None) if fn and hasattr(fn,'__wrapped__'):fn.__wrapped__.__globals__['api']=fake_api await user.open('/login') await user.should_see('Anmelden') with user:app.storage.user['token']='fake-token' await user.open('/') await user.should_see('Alles im Blick.') for title,expected in [('Server','Serverübersicht'),('Celery & Tasks','Einträge'),('Flower','Flower-Instanzen'),('Historie','Einträge'),('Alarme','Einträge'),('Einstellungen','Erfassung & Aufbewahrung'),('dilibAI','Infrastruktur analysieren')]: user.find(title).click() await user.should_see(expected) asyncio.run(scenario())