|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Smoke test do frontend (Playwright Python — evita conflito npm/lightningcss).""" |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import json |
| 6 | +import re |
| 7 | +import sys |
| 8 | +import urllib.request |
| 9 | + |
| 10 | +from playwright.sync_api import sync_playwright |
| 11 | + |
| 12 | +BASE = __import__("os").environ.get("SMOKE_BASE_URL", "http://127.0.0.1:8001") |
| 13 | + |
| 14 | +SKIP_INTRO_JS = """() => { |
| 15 | + try { sessionStorage.setItem('kernel_intro_seen', '1'); } catch (_) {} |
| 16 | + document.querySelectorAll('.entrance-init-hidden').forEach((el) => { |
| 17 | + el.classList.remove('entrance-init-hidden'); |
| 18 | + }); |
| 19 | +}""" |
| 20 | + |
| 21 | + |
| 22 | +def skip_entrance_animation(page) -> None: |
| 23 | + page.evaluate(SKIP_INTRO_JS) |
| 24 | + |
| 25 | + |
| 26 | +def wait_for_chat_input(page, timeout: float = 15000) -> None: |
| 27 | + page.locator("#message-input").wait_for(state="visible", timeout=timeout) |
| 28 | + |
| 29 | + |
| 30 | +def check_public_config() -> tuple[str, bool]: |
| 31 | + try: |
| 32 | + with urllib.request.urlopen(f"{BASE}/api/public-config", timeout=10) as resp: |
| 33 | + data = json.loads(resp.read().decode()) |
| 34 | + ok = bool(data.get("iss_lesson_base", "").startswith("http")) |
| 35 | + return "public-config ISS base", ok |
| 36 | + except Exception as exc: |
| 37 | + return "public-config ISS base", False if not str(exc) else False |
| 38 | + |
| 39 | + |
| 40 | +def main() -> int: |
| 41 | + results: list[dict[str, object]] = [] |
| 42 | + |
| 43 | + label, ok = check_public_config() |
| 44 | + results.append({"check": label, "ok": ok}) |
| 45 | + |
| 46 | + with sync_playwright() as p: |
| 47 | + browser = p.chromium.launch(headless=True) |
| 48 | + page = browser.new_page() |
| 49 | + try: |
| 50 | + page.goto(BASE, wait_until="networkidle", timeout=30000) |
| 51 | + |
| 52 | + page.wait_for_function( |
| 53 | + """() => /disciplinas/i.test( |
| 54 | + document.querySelector('.entrance-title-text')?.textContent || '' |
| 55 | + )""", |
| 56 | + timeout=15000, |
| 57 | + ) |
| 58 | + |
| 59 | + hero = page.locator(".entrance-title-text").text_content(timeout=15000) or "" |
| 60 | + results.append({"check": "T5 hero", "ok": bool(re.search(r"disciplinas", hero, re.I))}) |
| 61 | + |
| 62 | + pill_count = page.locator(".entrance-discipline-pills .cmd-pill").count() |
| 63 | + results.append({"check": "T4 pills", "ok": pill_count >= 5}) |
| 64 | + |
| 65 | + results.append({ |
| 66 | + "check": "T1 notice hidden empty", |
| 67 | + "ok": page.locator("#context-window-notice").is_hidden(), |
| 68 | + }) |
| 69 | + |
| 70 | + page.evaluate( |
| 71 | + """() => { |
| 72 | + try { sessionStorage.setItem('kernel_intro_seen', '1'); } catch (_) {} |
| 73 | + const turns = []; |
| 74 | + for (let i = 0; i < 13; i++) { |
| 75 | + turns.push({ role: "user", text: `q${i}`, ts: Date.now() }); |
| 76 | + turns.push({ role: "bot", text: `a${i}`, ts: Date.now() }); |
| 77 | + } |
| 78 | + localStorage.setItem( |
| 79 | + "kernel_conversations_v2", |
| 80 | + JSON.stringify({ |
| 81 | + activeId: "test", |
| 82 | + conversations: [{ |
| 83 | + id: "test", title: "Test", createdAt: 0, updatedAt: 0, |
| 84 | + session_id: "x", turns |
| 85 | + }], |
| 86 | + }), |
| 87 | + ); |
| 88 | + }""" |
| 89 | + ) |
| 90 | + page.reload(wait_until="networkidle") |
| 91 | + page.locator(".message-row").first.wait_for(state="attached", timeout=15000) |
| 92 | + page.wait_for_function( |
| 93 | + "() => document.body.classList.contains('chat-active')", |
| 94 | + timeout=15000, |
| 95 | + ) |
| 96 | + skip_entrance_animation(page) |
| 97 | + results.append({ |
| 98 | + "check": "T1 notice with 26 turns", |
| 99 | + "ok": page.locator("#context-window-notice").is_visible(), |
| 100 | + }) |
| 101 | + |
| 102 | + wait_for_chat_input(page) |
| 103 | + # T3 slash menu — com histórico carregado o input está visível |
| 104 | + page.locator("#message-input").fill("/py") |
| 105 | + page.wait_for_timeout(300) |
| 106 | + results.append({ |
| 107 | + "check": "T3 slash menu", |
| 108 | + "ok": page.locator("#slash-command-menu:not([hidden])").is_visible(), |
| 109 | + }) |
| 110 | + |
| 111 | + page.goto(f"{BASE}/?d=python", wait_until="networkidle") |
| 112 | + skip_entrance_animation(page) |
| 113 | + wait_for_chat_input(page) |
| 114 | + input_val = page.locator("#message-input").input_value() |
| 115 | + results.append({"check": "T11 ?d=python", "ok": input_val.startswith("/python")}) |
| 116 | + |
| 117 | + page.goto(BASE, wait_until="networkidle") |
| 118 | + results.append({ |
| 119 | + "check": "T10 sidebar", |
| 120 | + "ok": page.locator("#conversation-sidebar").count() == 1, |
| 121 | + }) |
| 122 | + |
| 123 | + # UI/UX roadmap: public-config carregado no boot |
| 124 | + iss_base = page.evaluate( |
| 125 | + """async () => { |
| 126 | + const r = await fetch('/api/public-config'); |
| 127 | + if (!r.ok) return ''; |
| 128 | + const j = await r.json(); |
| 129 | + return j.iss_lesson_base || ''; |
| 130 | + }""" |
| 131 | + ) |
| 132 | + results.append({ |
| 133 | + "check": "ISS config fetch in page", |
| 134 | + "ok": str(iss_base).startswith("http"), |
| 135 | + }) |
| 136 | + finally: |
| 137 | + browser.close() |
| 138 | + |
| 139 | + print(json.dumps(results, indent=2, ensure_ascii=False)) |
| 140 | + failed = [r for r in results if not r["ok"]] |
| 141 | + return 1 if failed else 0 |
| 142 | + |
| 143 | + |
| 144 | +if __name__ == "__main__": |
| 145 | + sys.exit(main()) |
0 commit comments