Repository navigation
Expand file tree
/
Copy pathtest_web.py
More file actions
257 lines (200 loc) · 10.7 KB
/
Copy pathtest_web.py
File metadata and controls
257 lines (200 loc) · 10.7 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
"""The web UI: its two themes (head script, toggle, variables, contrast) and the ask endpoint."""
import re
import threading
import urllib.error
import urllib.request
from http.server import ThreadingHTTPServer
from pathlib import Path
import pytest
from revenant.web import app
BUNDLE = Path(__file__).resolve().parent.parent / "deploy" / "bundle"
def _lum(hexcolour: str) -> float:
c = [int(hexcolour[i:i + 2], 16) / 255 for i in (1, 3, 5)]
c = [x / 12.92 if x <= 0.03928 else ((x + 0.055) / 1.055) ** 2.4 for x in c]
return 0.2126 * c[0] + 0.7152 * c[1] + 0.0722 * c[2]
def contrast(a: str, b: str) -> float:
la, lb = sorted((_lum(a), _lum(b)), reverse=True)
return (la + 0.05) / (lb + 0.05)
def test_contrast_helper_matches_wcag():
assert contrast("#000000", "#ffffff") == pytest.approx(21.0)
assert contrast("#777777", "#ffffff") == pytest.approx(4.48, abs=0.01)
@pytest.mark.parametrize("theme", [app.DARK, app.LIGHT], ids=["dark", "light"])
def test_text_colours_meet_wcag_aa(theme):
text = ["ink", "head", "big", "lede", "why", "answer", "dim", "faint", "candle", "alive", "dead"]
surfaces = ["bg", "panel", "panel2", "cert-a", "cert-b", "pre-bg", "input-bg", "code-bg"]
for fg in text:
for bg in surfaces:
assert contrast(theme[fg], theme[bg]) >= 4.5, (fg, bg)
assert contrast(theme["add"], theme["pre-bg"]) >= 4.5
assert contrast(theme["del"], theme["pre-bg"]) >= 4.5
assert contrast(theme["btn-ink"], theme["btn-bg"]) >= 4.5
def test_both_themes_define_every_variable_the_css_uses():
assert app.DARK.keys() == app.LIGHT.keys()
used = set(re.findall(r"var\(--([a-z0-9-]+)\)", app.CSS))
assert used <= set(app.DARK) | {"serif", "sans", "mono"}
assert ":root[data-theme=light]{" in app.CSS
assert "prefers-color-scheme: light" in app.CSS # no-JavaScript fallback
def test_page_sets_theme_in_head_before_styles_and_has_toggle():
html = app.page("Revenant", "<p>hi</p>").decode()
head_script, style, body = html.index(app.THEME_HEAD), html.index("<style>"), html.index("<body>")
assert head_script < style < body # attribute is set before anything paints
assert "localStorage.getItem('revenant-theme')" in app.THEME_HEAD
assert "prefers-color-scheme: light" in app.THEME_HEAD
assert "'dark'" in app.THEME_HEAD # dark stays the default
button = re.search(r"<button class=theme[^>]*>", html).group(0)
assert "aria-label='Switch to light theme'" in button and "type=button" in button
assert "localStorage.setItem('revenant-theme'" in html
assert ">Revenant</a>" in html
def test_page_has_no_external_assets():
html = app.page("Revenant", "").decode()
assert not re.search(r"(src|href)=['\"]?(https?:)?//", html)
assert "@import" not in html and "url(" not in app.CSS
@pytest.mark.skipif(not (BUNDLE / "psf__requests-html").exists(), reason="deploy bundle not present")
def test_served_pages_carry_the_toggle(monkeypatch):
monkeypatch.setenv("REVENANT_HOME", str(BUNDLE))
server = ThreadingHTTPServer(("127.0.0.1", 0), app.Handler)
threading.Thread(target=server.serve_forever, daemon=True).start()
try:
base = f"http://127.0.0.1:{server.server_address[1]}"
for path in ("/", "/p/psf/requests-html", "/p/psf/requests-html/certificate", "/p/psf/requests-html/resurrection"):
body = urllib.request.urlopen(base + path, timeout=10).read().decode()
assert "id=theme-toggle" in body and app.THEME_HEAD in body, path
finally:
server.shutdown()
@pytest.fixture
def live(monkeypatch):
"""The real server on the committed bundle, with the ghost's model replaced by a stub."""
if not (BUNDLE / "psf__requests-html").exists():
pytest.skip("deploy bundle not present")
from revenant.ghost import Ghost
from revenant.memory.retrieve import Retriever
from revenant.memory.store import Memory
from revenant.models import ChatResult
class Stub:
name = "stub"
def chat(self, messages, **opts):
return ChatResult(text="I am silent.", model="stub")
monkeypatch.setenv("REVENANT_HOME", str(BUNDLE))
db = BUNDLE / "psf__requests-html" / "ghost.db"
monkeypatch.setattr(app.Handler, "ghosts", {"psf/requests-html": Ghost("requests-html", Retriever(Memory(db)), Stub())})
server = ThreadingHTTPServer(("127.0.0.1", 0), app.Handler)
threading.Thread(target=server.serve_forever, kwargs={"poll_interval": 0.05}, daemon=True).start()
yield f"http://127.0.0.1:{server.server_address[1]}"
server.shutdown()
def _post(url, body: bytes, ctype: str, headers: dict | None = None):
req = urllib.request.Request(url, data=body, headers={"Content-Type": ctype, **(headers or {})})
try:
with urllib.request.urlopen(req, timeout=10) as r:
return r.status, r.read(), r.headers
except urllib.error.HTTPError as err:
return err.code, err.read(), err.headers
def test_ask_api_answers_json_and_sends_safe_headers(live):
import json
status, body, headers = _post(live + "/p/psf/requests-html/ask", b'{"q": "lxml import error"}', "application/json")
assert status == 200 and json.loads(body)["model"] == "stub"
assert headers["X-Content-Type-Options"] == "nosniff"
def test_head_matches_get_without_a_body(live):
for path, code in (("/healthz", 200), ("/p/psf/requests-html", 200), ("/nope", 404)):
req = urllib.request.Request(live + path, method="HEAD")
try:
with urllib.request.urlopen(req, timeout=10) as r:
status, body, headers = r.status, r.read(), r.headers
except urllib.error.HTTPError as err:
status, body, headers = err.code, err.read(), err.headers
get_len = len(urllib.request.urlopen(live + path, timeout=10).read()) if code == 200 else None
assert status == code and body == b"", path
assert headers["X-Content-Type-Options"] == "nosniff"
if get_len is not None:
assert int(headers["Content-Length"]) == get_len, path
def test_concurrent_first_questions_build_the_ghost_once(live, monkeypatch):
import json
import time
from revenant import cli
ghost = app.Handler.ghosts["psf/requests-html"]
builds = []
def slow_build(p):
builds.append(p.slug)
time.sleep(0.3) # wide window: without the lock every request would build its own
return ghost
monkeypatch.setattr(app.Handler, "ghosts", {})
monkeypatch.setattr(cli, "build_ghost", slow_build)
results = []
def ask():
results.append(_post(live + "/p/psf/requests-html/ask", b'{"q": "lxml"}', "application/json"))
threads = [threading.Thread(target=ask) for _ in range(4)]
for t in threads:
t.start()
for t in threads:
t.join(15)
assert [r[0] for r in results] == [200] * 4
assert all(json.loads(r[1])["model"] == "stub" for r in results)
assert builds == ["psf/requests-html"]
@pytest.mark.parametrize("body,ctype", [
(b"\xff\xfe", "application/x-www-form-urlencoded"), # not UTF-8
(b"[1, 2]", "application/json"), # JSON, but not an object
(b'{"q": 5}', "application/json"), # q is not a string
(b"{nope", "application/json"), # not JSON
(b"q=++", "application/x-www-form-urlencoded"), # empty question
], ids=["bad-utf8", "json-list", "json-number", "bad-json", "empty"])
def test_ask_rejects_malformed_bodies_with_400(live, body, ctype):
status, _, _ = _post(live + "/p/psf/requests-html/ask", body, ctype)
assert status == 400
def test_ask_rejects_oversized_bodies(live):
status, _, _ = _post(live + "/p/psf/requests-html/ask", b"q=" + b"a" * (app.MAX_BODY + 1),
"application/x-www-form-urlencoded")
assert status == 413
def test_unknown_patient_is_404(live):
status, _, _ = _post(live + "/p/nobody/nothing/ask", b'{"q": "hi"}', "application/json")
assert status == 404
with pytest.raises(urllib.error.HTTPError) as err:
urllib.request.urlopen(live + "/p/../../etc/passwd", timeout=10)
assert err.value.code == 404
def test_pages_have_landmarks_labels_and_current_tab():
from revenant.config import Patient
html = app.page("x", app.ask_body(Patient("psf/requests-html")), "psf/requests-html", "ask").decode()
assert "<main>" in html and "</main>" in html
assert re.search(r"<a href='/p/psf/requests-html/ask' class=on aria-current=page>", html)
assert html.count("aria-current") == 1
field = re.search(r"<input name=q[^>]*>", html).group(0)
assert "aria-label=" in field and "required" in field
def test_ask_form_fits_a_phone_and_recovers_from_the_back_button():
# a flex input with no min-width can't shrink below its intrinsic size, which pushed the
# Ask button off a 375px screen
assert re.search(r"form\.ask input\{[^}]*min-width:0", app.CSS)
assert "@media (max-width:640px)" in app.CSS
# a page restored from the back/forward cache must not stay stuck on "listening"
assert "pageshow" in app.JS and "b.disabled=false" in app.JS
def test_overview_asks_in_one_step_and_failures_are_announced(live, monkeypatch):
from revenant.config import Patient
# the overview's box posts the question itself instead of only pre-filling the ask page
assert "<form class=ask method=post action='/p/psf/requests-html/ask'>" in app.overview_body(Patient("psf/requests-html"))
class Broken:
def ask(self, q):
raise ConnectionError("model server down")
monkeypatch.setattr(app.Handler, "ghosts", {"psf/requests-html": Broken()})
status, body, _ = _post(live + "/p/psf/requests-html/ask", b"q=hello", "application/x-www-form-urlencoded")
assert status == 502
assert b"role=alert>The s" in body and b"ConnectionError: model server down" in body
def test_serve_rejects_a_bad_port_with_a_message():
for port in (70000, -5):
with pytest.raises(SystemExit) as e:
app.serve("127.0.0.1", port)
assert "out of range" in str(e.value.code)
def test_serve_reports_a_port_that_is_taken():
import socket
with socket.socket() as busy:
busy.bind(("127.0.0.1", 0))
busy.listen()
with pytest.raises(SystemExit) as e:
app.serve("127.0.0.1", busy.getsockname()[1])
assert "cannot listen on 127.0.0.1" in str(e.value.code)
def test_serve_stops_quietly_on_ctrl_c(monkeypatch, capsys):
def interrupt(self, *a, **kw):
raise KeyboardInterrupt
monkeypatch.setattr(ThreadingHTTPServer, "serve_forever", interrupt)
import socket
with socket.socket() as s:
s.bind(("127.0.0.1", 0))
port = s.getsockname()[1]
app.serve("127.0.0.1", port)
assert "Revenant web stopped" in capsys.readouterr().out