Files
uconsole_asw_game/tests/test_web_host_browser.py
T
Claude 6a83d5d80c Make the test suite and web host work on Windows
The Windows pytest job found real Windows bugs: the web host crashed on its
first password (no os.fchmod) and refused its own credential file later
(POSIX mode check), the Remote Crew listener let a second program bind its
port (SO_REUSEADDR), and a fresh Windows settings file paired graphics
"full" with the line switch off. The soak tool now runs without the
resource module and prints POSIX paths. Tests run against the uConsole
graphics default on every platform; the uConsole-only hotspot and updater
helpers are skipped off Linux.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-05 15:54:20 +00:00

70 lines
2.8 KiB
Python

"""The web-host login and setup shell executes in a real browser."""
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from importlib import resources
import json
import shutil
import subprocess
import threading
import pytest
from commander_web import WEB_ROUTES
from src.core.i18n import load_catalog
def test_admin_setup_shell_in_chromium():
chromium = shutil.which("chromium") or shutil.which("chromium-browser")
if chromium is None:
pytest.skip("Chromium is not installed")
class Handler(BaseHTTPRequestHandler):
def log_message(self, *args):
pass
def do_GET(self):
files = {"/admin": ("admin.html", "text/html"),
"/admin.js": ("admin.js", "text/javascript"),
"/admin.css": ("admin.css", "text/css")}
if self.path in files:
name, content_type = files[self.path]
payload = resources.files("data.commander").joinpath(name).read_bytes()
elif self.path.startswith(("/css/", "/fonts/")) and self.path in WEB_ROUTES:
content_type, payload = WEB_ROUTES[self.path]
elif self.path in ("/api/v2/ui?lang=en", "/api/v2/ui?lang=de"):
content_type = "application/json"
payload = json.dumps({key: value for key, value in load_catalog(self.path[-2:]).items()
if key.startswith("commander.web.")}).encode()
elif self.path == "/api/v2/web/status":
content_type, payload = "application/json", b'{"configured":false}'
else:
self.send_error(404)
return
self.send_response(200)
self.send_header("Content-Type", content_type)
self.send_header("Content-Length", str(len(payload)))
self.end_headers()
self.wfile.write(payload)
server = ThreadingHTTPServer(("127.0.0.1", 0), Handler)
thread = threading.Thread(target=server.serve_forever, daemon=True)
thread.start()
try:
result = subprocess.run([
chromium, "--headless", "--no-sandbox", "--disable-gpu",
"--disable-dev-shm-usage", "--disable-background-networking",
"--virtual-time-budget=2500", "--dump-dom",
f"http://127.0.0.1:{server.server_port}/admin",
], capture_output=True, text=True, timeout=20)
assert result.returncode == 0, result.stderr[-1000:]
assert "Set up host account" in result.stdout or "Host-Konto einrichten" in result.stdout
assert 'id="setup"' in result.stdout
assert 'id="setup" hidden' not in result.stdout
assert 'id="editor-kind"' not in result.stdout
assert 'id="editor-json"' not in result.stdout
finally:
server.shutdown()
server.server_close()
thread.join(2)