#!/usr/bin/env python3 """End-to-end test for the web-agent app. Boots the real server on a scratch port, then verifies over HTTP: 1. /api/evaluation matches the system's recorded run files exactly (results/run_on.json / results/run_off.json, read independently here from the system directory). 2. /api/tasks lists all 36 tasks, including adversarial_rename_under_overlay. 3. A live SSE run of adversarial_rename_under_overlay (recovery on) passes with exactly 2 recoveries and 5 attempts — the recorded behaviour — with streamed screenshots and step events. 4. A live SSE run of the control task `login` passes with 0 recoveries. 5. The UI and a fixture page are served. 6. Public-page rails over HTTP: private-IP / localhost / non-http(s) URLs are refused before any browsing. 7. The three stored traces play back with the results-panel figures of their recorded runs, verified over SSE and again in a headless browser (panel totals + trace strip). 8. The public-page loop end-to-end, in-process, against local test pages (guard resolution layer stubbed in the test only): type and click intents pass for real; a password field is refused; a cross-domain navigation is a reported hard stop; a field hidden behind a same-purpose toggle (Wikipedia's header search structure) is revealed and typed into; the grounding order (label > aria-label > placeholder > name) picks the right field; a toggle-hidden password field is refused before anything is clicked. Run with the web-agent system's venv Python (provides Playwright): ~/workspace/browser-agent-lab/.venv/bin/python tests/test_app.py """ from __future__ import annotations import json import os import subprocess import sys import time import urllib.error import urllib.parse import urllib.request from pathlib import Path APP_DIR = Path(__file__).resolve().parent.parent LAB_DIR = APP_DIR.parent / "browser-agent-lab" PORT = int(os.environ.get("TEST_PORT", "8931")) BASE = f"http://127.0.0.1:{PORT}" def get_json(path): with urllib.request.urlopen(BASE + path, timeout=30) as r: return json.loads(r.read()) def get_text(path): with urllib.request.urlopen(BASE + path, timeout=30) as r: return r.status, r.read().decode("utf-8", "replace") def sse_run(task, recovery="on"): """Read the SSE stream line by line; stop at the terminal event.""" return sse_get(f"/api/run?task={task}&recovery={recovery}") def sse_get(path): events = [] with urllib.request.urlopen(BASE + path, timeout=240) as r: for raw in r: line = raw.decode("utf-8", "replace").strip() if line.startswith("data: "): ev = json.loads(line[6:]) events.append(ev) if ev["type"] in ("verdict", "error"): break return events def get_status(path): try: with urllib.request.urlopen(BASE + path, timeout=30) as r: return r.status except urllib.error.HTTPError as e: return e.code def check(label, cond, detail=""): print(("PASS " if cond else "FAIL ") + label + (f" — {detail}" if detail else "")) if not cond: raise SystemExit(f"test failed: {label}") PUBLIC_PAGE = """

Public test page

Leave site
""" # Wikipedia's header structure, reduced to the load-bearing parts: # the search input has NO