glam/apps/archief-assistent/tests/test_knowledge_graph_cache.py

183 lines
6.8 KiB
Python

#!/usr/bin/env python3
"""
Knowledge Graph Cache Fix - E2E Test Script (v5)
Better console log capture and verification.
"""
import time
from playwright.sync_api import sync_playwright
BASE_URL = 'https://archief.support'
TEST_QUERY = 'Welke archieven zijn er in Den Haag?'
LOGIN_EMAIL = 'scott@bronhouder.nl'
LOGIN_PASSWORD = 'Olivi@1985'
def run_test():
print('🚀 Starting Knowledge Graph Cache Test\n')
with sync_playwright() as p:
browser = p.chromium.launch(headless=False, slow_mo=100)
context = browser.new_context()
page = context.new_page()
all_logs = []
def capture_log(msg):
text = msg.text
all_logs.append(text)
# Print important logs immediately
if any(kw in text for kw in ['ChatPage', 'Cache', 'Restored', 'extractGraph', 'KnowledgeGraph']):
print(f' 🔵 {text[:200]}')
page.on('console', capture_log)
try:
# Navigate and login
print('Step 1: Navigating and logging in...')
page.goto(BASE_URL)
page.wait_for_load_state('networkidle')
if page.locator('button:has-text("Inloggen")').is_visible(timeout=3000):
page.locator('input[type="email"]').fill(LOGIN_EMAIL)
page.locator('input[type="password"]').fill(LOGIN_PASSWORD)
page.locator('button[type="submit"]').click()
page.wait_for_url(lambda url: '/login' not in url, timeout=15000)
page.wait_for_load_state('networkidle')
time.sleep(2)
print(' ✅ Ready\n')
# FIRST QUERY
print('=' * 70)
print('FIRST QUERY - Creating cache entry')
print('=' * 70)
all_logs.clear()
input_field = page.locator('.MuiOutlinedInput-input').first
input_field.fill(TEST_QUERY)
page.locator('button[type="submit"]').first.click()
print('Waiting for response...')
page.wait_for_selector('.MuiPaper-root', timeout=120000)
time.sleep(5)
# Filter for relevant logs
chat_logs_1 = [l for l in all_logs if '[ChatPage]' in l]
print(f'\nFound {len(chat_logs_1)} ChatPage logs:')
for log in chat_logs_1:
print(f' {log[:150]}')
# Open debug panel and check KG
debug_btn = page.locator('button').filter(has_text='Debug')
kg_tab = page.locator('button, [role="tab"]').filter(has_text='Kennisgraaf')
no_data = page.locator('text="Geen graafdata beschikbaar"')
try:
debug_btn.first.click()
time.sleep(0.5)
kg_tab.first.click()
time.sleep(1)
except:
pass
has_kg_1 = not (no_data.is_visible(timeout=2000) if no_data.count() > 0 else False)
print(f'\n📊 First response KG has data: {has_kg_1}')
page.screenshot(path='/tmp/archief-kg-first.png')
# NEW CONVERSATION
print('\n' + '=' * 70)
print('SECOND QUERY - Should hit cache')
print('=' * 70)
# Start new conversation
new_btn = page.locator('button').filter(has_text='Nieuw')
try:
new_btn.first.click()
time.sleep(2)
except:
page.reload()
page.wait_for_load_state('networkidle')
time.sleep(2)
all_logs.clear()
# Submit same query
input_field = page.locator('.MuiOutlinedInput-input').first
input_field.fill(TEST_QUERY)
page.locator('button[type="submit"]').first.click()
print('Waiting for response...')
page.wait_for_selector('.MuiPaper-root', timeout=30000)
time.sleep(3)
# Get logs
chat_logs_2 = [l for l in all_logs if '[ChatPage]' in l]
print(f'\nFound {len(chat_logs_2)} ChatPage logs:')
for log in chat_logs_2:
print(f' {log[:150]}')
# Analyze
cache_hit = any('Cache HIT' in l for l in all_logs)
cache_miss = any('Cache MISS' in l for l in all_logs)
restored = any('Restored' in l for l in all_logs)
print(f'\n📊 Analysis:')
print(f' Cache HIT: {cache_hit}')
print(f' Cache MISS: {cache_miss}')
print(f' Restored: {restored}')
# Check KG on second response
try:
debug_btn.first.click()
time.sleep(0.5)
kg_tab.first.click()
time.sleep(1)
except:
pass
has_kg_2 = not (no_data.is_visible(timeout=2000) if no_data.count() > 0 else False)
print(f' Second KG: {has_kg_2}')
page.screenshot(path='/tmp/archief-kg-second.png')
# RESULTS
print('\n' + '=' * 70)
print('FINAL RESULTS')
print('=' * 70)
if cache_hit and restored:
print('\n✅✅ PERFECT: Cache hit AND results restored!')
print(' The Knowledge Graph cache fix is working correctly.')
elif cache_hit and not restored:
print('\n⚠️ WARNING: Cache hit but NO results restored')
print(' Check if retrievedResults is in the cached data.')
elif has_kg_2:
print('\n✅ PASSED: Knowledge Graph has data on cached response')
print(' (Even if cache hit not detected in logs)')
else:
print('\n❌ FAILED: Knowledge Graph shows no data on second response')
if not chat_logs_2:
print('\n⚠️ NOTE: No console logs captured. This might be because:')
print(' - The app uses a different logging mechanism')
print(' - Console logs are filtered in production build')
# Print all logs for debugging
print('\n📋 All captured logs (last 30):')
for log in all_logs[-30:]:
if len(log) < 300:
print(f' {log}')
print('\n📌 Browser stays open for 60 seconds...')
time.sleep(60)
except Exception as e:
print(f'\n❌ Error: {e}')
import traceback
traceback.print_exc()
page.screenshot(path='/tmp/archief-error.png')
finally:
browser.close()
if __name__ == '__main__':
run_test()