forked from ChelseaKR/habitable
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_site_positioning.py
More file actions
204 lines (169 loc) · 7.49 KB
/
Copy pathtest_site_positioning.py
File metadata and controls
204 lines (169 loc) · 7.49 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
# SPDX-License-Identifier: AGPL-3.0-or-later
# Copyright 2026 Chelsea Kelly-Reif
"""Positioning, safety-routing, and responsive contracts for the public homepage."""
from __future__ import annotations
import re
from html.parser import HTMLParser
from pathlib import Path
from typing import cast
from urllib.parse import urlparse
import pytest
_SITE_ROOT = Path(__file__).resolve().parent.parent / "site"
_LANDING = _SITE_ROOT / "index.html"
def _normalize(parts: list[str]) -> str:
return re.sub(r"\s+", " ", " ".join(parts)).strip()
def _normalize_inline(parts: list[str]) -> str:
return re.sub(r"\s+", " ", "".join(parts)).strip()
class _PositioningParser(HTMLParser):
def __init__(self) -> None:
super().__init__(convert_charrefs=True)
self.visible_parts: list[str] = []
self.headings: dict[str, list[str]] = {"h1": [], "h2": [], "h3": []}
self.links: list[dict[str, str]] = []
self.form_count = 0
self._in_body = False
self._ignored_depth = 0
self._heading_tag: str | None = None
self._heading_parts: list[str] = []
self._link: dict[str, str] | None = None
self._link_parts: list[str] = []
def handle_starttag(self, tag: str, attrs: list[tuple[str, str | None]]) -> None:
values = {name: value or "" for name, value in attrs}
if tag == "body":
self._in_body = True
elif tag in {"script", "style"}:
self._ignored_depth += 1
elif self._in_body and tag in self.headings:
self._heading_tag = tag
self._heading_parts = []
elif self._in_body and tag == "a":
self._link = values
self._link_parts = []
elif self._in_body and tag == "form":
self.form_count += 1
def handle_endtag(self, tag: str) -> None:
if tag == "body":
self._in_body = False
elif tag in {"script", "style"}:
self._ignored_depth -= 1
elif tag == self._heading_tag:
self.headings[tag].append(_normalize_inline(self._heading_parts))
self._heading_tag = None
self._heading_parts = []
elif tag == "a" and self._link is not None:
self._link["text"] = _normalize_inline(self._link_parts)
self.links.append(self._link)
self._link = None
self._link_parts = []
def handle_data(self, data: str) -> None:
if not self._in_body or self._ignored_depth:
return
if data.strip():
self.visible_parts.append(data.strip())
if self._heading_tag is not None:
self._heading_parts.append(data)
if self._link is not None:
self._link_parts.append(data)
def _landing() -> _PositioningParser:
parser = _PositioningParser()
parser.feed(_LANDING.read_text(encoding="utf-8"))
return parser
def test_opening_uses_the_tenant_copy_thesis_and_honest_alpha_boundary() -> None:
parser = _landing()
assert parser.headings["h1"] == ["Your landlord has the work orders. Keep your own."]
body = _normalize(parser.visible_parts)
assert "Habitable Evidence" in body
assert "not independently audited or proven in court" in body
assert "Do not rely on it for a real legal matter yet" in body
assert "court-organized alpha packet" in body
assert "court-ready" not in body.casefold()
def test_primary_actions_route_to_review_sample_and_evidence_method() -> None:
parser = _landing()
by_id = {link["id"]: link for link in parser.links if link.get("id")}
assert by_id["pilot-cta"]["href"] == "review/"
assert by_id["pilot-cta"]["text"] == "Walk the Unit 4B sample"
assert by_id["sample-cta"]["href"] == "sample-packet/packet.html"
assert by_id["method-cta"]["href"] == "how-it-works/"
assert by_id["method-cta"]["text"] == "Check the evidence method"
body = _normalize(parser.visible_parts)
assert "separates public technical feedback from private organization contact" in body
assert "accepts no evidence uploads" in body
assert "supplied synthetic case" in body
assert parser.form_count == 0, "the static site must not collect review or tenant data"
def test_each_audience_has_an_explicit_route() -> None:
parser = _landing()
headings = parser.headings["h3"]
assert "Tenant unions" in headings
assert "Legal aid, attorneys, and inspectors" in headings
assert "Reviewers and contributors" in headings
link_text = {link["text"] for link in parser.links}
assert link_text >= {
"Plan a bounded union evaluation",
"Follow the legal-aid review guide",
"Follow the inspector review guide",
"Choose one bounded review task",
"Read what reviewers found and what changed",
"Read the open trust gates",
"Browse the source",
}
def test_open_review_and_pilot_gaps_are_visible() -> None:
body = _normalize(_landing().visible_parts)
for gap in (
"An independent security and cryptography audit",
"Housing-law review of the legal framing and packet workflow",
"A real tenant-union or legal-aid pilot with documented outcomes",
"A recorded human NVDA and VoiceOver pass",
"signed native distribution",
):
assert gap in body
assert "cannot prove what a photo depicts" in body
assert "whether a particular court or agency will admit it" in body
def test_help_links_use_authoritative_public_resources() -> None:
parser = _landing()
expected = {
"https://www.usa.gov/tenant-rights",
"https://oag.ca.gov/tenants",
"https://selfhelp.courts.ca.gov/get-free-or-low-cost-legal-help",
}
actual = {link["href"] for link in parser.links if link["href"] in expected}
assert actual == expected
assert {urlparse(url).netloc for url in actual} == {
"www.usa.gov",
"oag.ca.gov",
"selfhelp.courts.ca.gov",
}
body = _normalize(parser.visible_parts)
assert "not legal advice, an emergency service, or a place to post evidence" in body
assert "Do not put tenant names, addresses, photos, or case details in GitHub issues" in body
@pytest.mark.a11y
@pytest.mark.parametrize("width,height", [(320, 800), (1280, 900)])
def test_landing_reflows_and_keeps_primary_actions_tappable(width: int, height: int) -> None:
pytest.importorskip("playwright.sync_api")
from playwright.sync_api import Error as PlaywrightError
from playwright.sync_api import sync_playwright
with sync_playwright() as playwright:
try:
browser = playwright.chromium.launch()
except PlaywrightError as exc:
pytest.skip(f"Chromium not available: {exc}")
try:
page = browser.new_page(viewport={"width": width, "height": height})
page.goto(_LANDING.as_uri(), wait_until="load")
dimensions = cast(
dict[str, int],
page.evaluate(
"""() => ({
viewport: document.documentElement.clientWidth,
content: document.documentElement.scrollWidth
})"""
),
)
assert dimensions["content"] <= dimensions["viewport"]
assert page.locator("h1").is_visible()
for selector in ("#pilot-cta", "#sample-cta"):
box = page.locator(selector).bounding_box()
assert box is not None
assert box["width"] >= 44
assert box["height"] >= 44
finally:
browser.close()