Spaces:
Sleeping
Sleeping
File size: 6,072 Bytes
79b17a9 0e97c8b 79b17a9 0e97c8b 79b17a9 0e97c8b 79b17a9 0e97c8b 79b17a9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | """Browser test: drives the real Gradio app against a fake OpenRouter.
Run: python tests/test_ui.py (needs playwright + a Chromium)
"""
import os
import subprocess
import sys
import time
import urllib.request
sys.path.insert(0, os.path.dirname(__file__))
from fake_openrouter import Fake
from playwright.sync_api import expect, sync_playwright
ROOT = os.path.join(os.path.dirname(__file__), "..")
PORT = 7861
URL = f"http://127.0.0.1:{PORT}"
CHROMIUM = os.environ.get("CHROMIUM_PATH") or "/opt/pw-browsers/chromium-1194/chrome-linux/chrome"
fake = Fake()
env = dict(os.environ, OPENROUTER_BASE_URL=fake.url, GRADIO_SERVER_PORT=str(PORT), NO_PROXY="127.0.0.1,localhost", no_proxy="127.0.0.1,localhost")
env.pop("SPACE_ID", None)
def start_server():
proc = subprocess.Popen([sys.executable, "app.py"], cwd=ROOT, env=env, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
for _ in range(60):
try:
urllib.request.urlopen(URL, timeout=1)
return proc
except Exception:
time.sleep(0.5)
raise SystemExit("server did not start:\n" + proc.stdout.read().decode())
srv = start_server()
try:
with sync_playwright() as pw:
exe = CHROMIUM if os.path.exists(CHROMIUM) else None
browser = pw.chromium.launch(executable_path=exe)
ctx = browser.new_context()
page = ctx.new_page()
page.goto(URL)
# 1. a formula loads and KaTeX renders it
expect(page.locator("#formula .katex").first).to_be_visible(timeout=20000)
print("ok formula rendered with KaTeX")
# 2. evaluating without a key gives a clear message and no request
page.get_by_label("1. Formal sight-reading").fill("x is a vector in R^n and A is a matrix of size n by n.")
page.get_by_label("2. Concept & mechanics").fill("It measures how much A stretches x along itself.")
page.get_by_label("3. Application & metaphor").fill("PCA variance and vibration frequency of a structure.")
page.get_by_role("button", name="Evaluate").click()
expect(page.get_by_text("Add your OpenRouter API key")).to_be_visible(timeout=10000)
assert fake.requests == []
print("ok no key -> message, no request")
# 3. hints reveal progressively
page.get_by_role("button", name="Hint", exact=True).click()
expect(page.get_by_text("Hint 1 (types)")).to_be_visible()
page.get_by_role("button", name="Hint", exact=True).click()
expect(page.get_by_text("Hint 2 (mechanics)")).to_be_visible()
print("ok hint ladder")
# 4. set key + model, evaluate
page.get_by_role("tab", name="Settings").click()
page.get_by_label("OpenRouter API key").fill("sk-or-UITESTKEY")
page.get_by_role("tab", name="Drill").click()
page.get_by_role("button", name="Evaluate").click()
expect(page.get_by_text("7/10")).to_be_visible(timeout=20000)
expect(page.get_by_text("Competent").first).to_be_visible()
expect(page.get_by_text("This was:")).to_be_visible()
assert fake.requests[-1]["auth"] == "Bearer sk-or-UITESTKEY"
assert len(fake.requests) == 1
print("ok evaluation shown, key sent as bearer")
# hints used (2) are penalised: 7/10, Competent, A -> (0.7+0.5+1)/3=0.733 - 0.2 = 53%
expect(page.get_by_text("53%")).to_be_visible()
print("ok hint penalty applied")
# 5. evaluating again on the same drill is refused (no double count)
page.get_by_role("button", name="Evaluate").click()
expect(page.get_by_text("Press New formula")).to_be_visible(timeout=10000)
assert len(fake.requests) == 1
print("ok no double submission")
# 6. progress tab
page.get_by_role("tab", name="Progress").click()
expect(page.get_by_text("1 drills")).to_be_visible()
print("ok progress updated")
# 7. persistence across reload (BrowserState)
page.reload()
page.get_by_role("tab", name="Progress").click()
expect(page.get_by_text("1 drills")).to_be_visible(timeout=20000)
page.get_by_role("tab", name="Settings").click()
expect(page.get_by_label("OpenRouter API key")).to_have_value("sk-or-UITESTKEY")
print("ok progress and key persist across reload")
# 7b. persistence across a SERVER RESTART (a Space sleeping or rebuilding).
# BrowserState encrypts with a per-process random secret unless one is set,
# which silently drops the saved key and progress after a restart.
srv.terminate()
srv.wait()
srv = start_server()
page.reload()
page.get_by_role("tab", name="Progress").click()
expect(page.get_by_text("1 drills")).to_be_visible(timeout=20000)
page.get_by_role("tab", name="Settings").click()
expect(page.get_by_label("OpenRouter API key")).to_have_value("sk-or-UITESTKEY")
print("ok key and progress survive a server restart")
# 8. model list refresh via the fake server
page.get_by_role("button", name="Load current OpenRouter model list").click()
expect(page.get_by_text("2 models loaded")).to_be_visible(timeout=10000)
print("ok model list refresh")
# 9. judge error is surfaced and nothing is recorded
fake.status = 401
page.get_by_role("tab", name="Drill").click()
page.get_by_role("button", name="New formula").click()
page.get_by_label("1. Formal sight-reading").fill("Typed carefully with enough words to pass the length check.")
page.get_by_role("button", name="Evaluate").click()
expect(page.get_by_text("Judge error")).to_be_visible(timeout=20000)
expect(page.get_by_text("HTTP 401")).to_be_visible()
page.get_by_role("tab", name="Progress").click()
expect(page.get_by_text("1 drills")).to_be_visible()
print("ok judge error surfaced, nothing recorded")
browser.close()
print("ALL UI CHECKS PASSED")
finally:
srv.terminate()
fake.close()
|