forked from ChelseaKR/permit-bearings
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_static_demo.py
More file actions
390 lines (347 loc) · 14.5 KB
/
Copy pathtest_static_demo.py
File metadata and controls
390 lines (347 loc) · 14.5 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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
import json
import shutil
import subprocess
from io import BytesIO
import pytest
from demo.app import (
Handler,
MAX_BODY_BYTES,
ROOT,
STRINGS as DEMO_STRINGS,
result_page,
static_path,
)
from scripts.build_demo_bundle import OUTPUT, build_bundle
def test_committed_demo_bundle_matches_canonical_json():
assert OUTPUT.read_text(encoding="utf-8") == build_bundle()
def test_python_trust_rehearsal_uses_human_readable_source_label():
html = (ROOT / "demo" / "app.py").read_text(encoding="utf-8")
assert '"Gov. Code § 66321"' in html
assert "Gov. Code § {html.escape(changed[0])}" not in html
def test_index_loads_offline_bundle_before_application_code():
html = (ROOT / "index.html").read_text(encoding="utf-8")
bundle_tag = '<script src="data/demo-data.js"></script>'
application_start = "<script>\nconst STRINGS"
assert bundle_tag in html
assert application_start in html
assert html.index(bundle_tag) < html.index(application_start)
assert "globalThis.PERMIT_PATHWAYS_DEMO_DATA" in html
def test_public_brand_name_and_tagline_are_consistent():
public_files = {
ROOT / "index.html",
ROOT / "demo" / "app.py",
ROOT / "README.md",
ROOT / "docs" / "PRODUCT-CONTEXT.md",
ROOT / "AGENTS.md",
ROOT / "LICENSE",
ROOT / "THIRD_PARTY_NOTICES.md",
ROOT / "src" / "permit_pathways" / "__init__.py",
}
legacy_human_name = "Permit " + "Pathways"
for path in public_files:
assert legacy_human_name not in path.read_text(encoding="utf-8")
html = (ROOT / "index.html").read_text(encoding="utf-8")
tagline = (
"Find a candidate route. See the sources behind it. "
"Take open questions to staff."
)
assert (
"<title>Permit Bearings — cited housing-permit guidance for "
"California jurisdictions</title>"
) in html
assert '<meta property="og:title" content="Permit Bearings">' in html
assert '<h1 id="t-title">Permit Bearings</h1>' in html
assert f'<p class="tag" id="t-tagline">{tagline}</p>' in html
assert "Prototype source-grounded ADU, JADU, and SB 9" in html
assert DEMO_STRINGS["en"]["title"] == "Permit Bearings — demo"
assert DEMO_STRINGS["en"]["tagline"] == tagline
assert DEMO_STRINGS["es"]["title"] == "Permit Bearings — demostración"
assert DEMO_STRINGS["es"]["tagline"] == (
"Encuentre una posible ruta. Vea las fuentes que la respaldan. "
"Consulte las preguntas pendientes con el personal de la agencia."
)
rendered_page = result_page(
{
"jurisdiction": "Davis",
"project_type": "adu",
"primary_dwelling_status": "existing_single_family",
"adu_project_form": "new_detached",
},
"en",
)
assert '<a href="/?lang=en">Permit Bearings</a>' in rendered_page
pyproject = (ROOT / "pyproject.toml").read_text(encoding="utf-8")
assert 'name = "permit-pathways"' in pyproject
assert "globalThis.PERMIT_PATHWAYS_DEMO_DATA" in html
assert (ROOT / "src" / "permit_pathways").is_dir()
def test_static_result_cards_keep_explanations_separate_from_matching():
html = (ROOT / "index.html").read_text(encoding="utf-8")
assert "function screen(intake)" in html
assert "function renderResultCard(rule, explanation" in html
assert "async function normalizeExplanations(payload, rules)" in html
assert "Array.isArray(payload.entries)" in html
assert "async function citationFingerprint(rule)" in html
assert "async function ruleFingerprint(rule)" in html
assert "async function localizedContentFingerprint" in html
assert "source_dependencies: rule.source_dependencies" in html
assert "function validHighlights(value)" in html
assert "record.citation_fingerprint !== expectedFingerprint" in html
assert "record.rule_fingerprint !== expectedRuleFingerprint" in html
assert "if (!globalThis.crypto || !globalThis.crypto.subtle) return new Map()" in html
assert "EXPLANATIONS.get(rule.rule_id)" in html
assert "EXPLANATIONS = await normalizeExplanations" in html
assert "data-rule-id=" in html
assert "<details>" in html
assert 'source: "Source"' in html
assert "Draft explanation · made with AI · not reviewed by a person" in html
assert "no revisado para comprobar su exactitud" in html
assert "We are not showing next steps" in html
assert "Check candidate pathways" in html
assert "limited set of rules in this prototype" in html
assert 'id="resultStatus"' in html
assert '"primary_dwelling_status"' in html
assert '"adu_project_form"' in html
assert '["yes","Yes"],["no","No"],["unknown","I\'m not sure"]' in html
assert 'id="loadSample"' in html and "<button" in html
assert 'id="resultsHeading" tabindex="-1"' in html
assert 'aria-invalid' in html
assert 'name="has_primary_dwelling"' not in html
assert '"jadu")\n return ["primary_dwelling_status", "unpermitted_existing"]' in html
assert "two_unit_contributing_historic_location" in html
assert "lot_split_alters_historic_district_resource" in html
assert "Supporting local information is shown below" in html
assert 'fieldset data-question="${esc(name)}"${describedBy}' in html
assert "ADU review-clock illustration" in html
@pytest.mark.skipif(shutil.which("node") is None, reason="Node.js unavailable")
def test_static_explanation_normalizer_accepts_canonical_data_and_fails_closed():
html = (ROOT / "index.html").read_text(encoding="utf-8")
validation_source = html[
html.index("function isJsonNumber"):
html.index("function renderForm")
]
bundle = json.loads(
(ROOT / "data" / "explanations" / "plain-language.json").read_text(
encoding="utf-8"
)
)
rules = []
for path in sorted((ROOT / "data" / "rules").glob("*.json")):
if path.name == "index.json":
continue
rules.extend(json.loads(path.read_text(encoding="utf-8")))
script = f"""
import {{webcrypto}} from "node:crypto";
Object.defineProperty(globalThis, "crypto",
{{value: webcrypto, configurable: true}});
{validation_source}
const payload = {json.dumps(bundle)};
const rules = {json.dumps(rules)};
const canonical = await normalizeExplanations(payload, rules);
if (canonical.size !== rules.length) throw new Error(`canonical:${{canonical.size}}`);
if (normalizeRules(rules).length !== rules.length)
throw new Error("canonical rules rejected");
function expectRuleRejection(mutator, label) {{
const invalid = structuredClone(rules);
mutator(invalid[0]);
try {{
normalizeRules(invalid);
throw new Error(`${{label}} accepted`);
}} catch (error) {{
if (String(error.message) === `${{label}} accepted`) throw error;
}}
}}
expectRuleRejection(rule => {{ rule.unknown_field = true; }}, "unknown field");
expectRuleRejection(
rule => {{ rule.citation.verified_on = "2099-01-01"; }},
"future citation date"
);
expectRuleRejection(
rule => {{ delete rule.required_documents; }},
"missing required_documents"
);
expectRuleRejection(
rule => {{ rule.criteria[0].value = Number.MAX_SAFE_INTEGER + 1; }},
"unsafe numeric criterion"
);
const duplicate = structuredClone(payload);
duplicate.entries.push(structuredClone(duplicate.entries[0]));
const duplicateResult = await normalizeExplanations(duplicate, rules);
if (duplicateResult.has(duplicate.entries[0].source_rule_id))
throw new Error("duplicate accepted");
const duplicateRules = [...rules, structuredClone(rules[0])];
const duplicateRuleResult = await normalizeExplanations(payload, duplicateRules);
if (duplicateRuleResult.size !== 0)
throw new Error("duplicate canonical rule ID did not fail closed");
const changed = structuredClone(rules);
changed[0].notes += " changed";
const drifted = await normalizeExplanations(payload, changed);
if (drifted.has(changed[0].rule_id)) throw new Error("rule drift accepted");
const realCrypto = globalThis.crypto;
Object.defineProperty(globalThis, "crypto",
{{value: undefined, configurable: true}});
const noCrypto = await normalizeExplanations(payload, rules);
if (noCrypto.size !== 0) throw new Error("no-WebCrypto did not fail closed");
Object.defineProperty(globalThis, "crypto", {{
value: {{subtle: {{digest: async () => {{throw new Error("digest failed")}}}}}},
configurable: true
}});
const rejectedDigest = await normalizeExplanations(payload, rules);
if (rejectedDigest.size !== 0)
throw new Error("digest rejection did not fail closed");
Object.defineProperty(globalThis, "crypto",
{{value: realCrypto, configurable: true}});
"""
subprocess.run(
["node", "--input-type=module", "-e", script],
check=True,
capture_output=True,
text=True,
)
@pytest.mark.skipif(shutil.which("node") is None, reason="Node.js unavailable")
def test_static_matcher_is_type_strict_and_source_changes_use_exact_ids():
html = (ROOT / "index.html").read_text(encoding="utf-8")
matching_source = html[
html.index("function isJsonNumber"):
html.index("function esc")
]
script = f"""
function nonBlank(value) {{
return typeof value === "string" && value.trim().length > 0;
}}
function validIsoDate(value) {{
if (!/^\\d{{4}}-\\d{{2}}-\\d{{2}}$/.test(value || "")) return false;
const parsed = new Date(`${{value}}T00:00:00Z`);
return !Number.isNaN(parsed.getTime())
&& parsed.toISOString().slice(0, 10) === value;
}}
{matching_source}
const boolRule = {{
criteria: [{{field: "confirmed", op: "eq", value: true}}]
}};
if (!matches(boolRule, {{confirmed: true}})) throw new Error("true rejected");
if (matches(boolRule, {{confirmed: 1}})) throw new Error("boolean coerced");
if (matches(boolRule, {{confirmed: "unknown"}})) throw new Error("unknown matched");
if (matches(boolRule, {{}})) throw new Error("missing matched");
const today = new Date().toISOString().slice(0, 10);
const sourceRule = {{
citation: {{verified_on: today}},
source_dependencies: ["ca-gov-66321"]
}};
if (ruleStatus(sourceRule, ["66321"]) !== "verified")
throw new Error("substring source ID matched");
if (ruleStatus(sourceRule, ["ca-gov-66321"]) !== "stale")
throw new Error("exact source ID did not match");
const futureRule = {{
citation: {{verified_on: "2099-01-01"}},
source_dependencies: []
}};
if (ruleStatus(futureRule, []) !== "stale")
throw new Error("future source date accepted");
"""
subprocess.run(
["node", "--input-type=module", "-e", script],
check=True,
capture_output=True,
text=True,
)
def test_demo_server_exposes_only_intended_static_files():
assert static_path("/index.html") == ROOT / "index.html"
assert static_path("/showcase") == ROOT / "index.html"
assert static_path("/data/demo-data.js") == OUTPUT
assert static_path("/data/explanations/plain-language.json") == (
ROOT / "data" / "explanations" / "plain-language.json"
)
assert static_path("/README.md") is None
assert static_path("/data/missing.json") is None
assert static_path("/data/../README.md") is None
assert static_path("/data/%2e%2e/README.md") is None
def _capturing_handler(path="/", *, body=b"", headers=None):
handler = Handler.__new__(Handler)
handler.path = path
handler.headers = headers or {}
handler.rfile = BytesIO(body)
handler.wfile = BytesIO()
handler.close_connection = False
handler.status = None
handler.response_headers = {}
handler.send_response = lambda status: setattr(handler, "status", status)
handler.send_header = (
lambda name, value: handler.response_headers.__setitem__(name, value)
)
handler.end_headers = lambda: None
return handler
def test_demo_server_sets_security_headers_and_limits_post_routes():
handler = _capturing_handler()
handler._send("<h1>ok</h1>")
assert handler.status == 200
headers = handler.response_headers
assert "frame-ancestors 'none'" in headers["Content-Security-Policy"]
assert headers["Referrer-Policy"] == "no-referrer"
assert headers["X-Content-Type-Options"] == "nosniff"
assert headers["X-Frame-Options"] == "DENY"
wrong_route = _capturing_handler(
"/trust",
headers={
"Content-Type": "application/x-www-form-urlencoded",
"Content-Length": "0",
},
)
wrong_route.do_POST()
assert wrong_route.status == 405
assert wrong_route.response_headers["Allow"] == "GET"
oversized = _capturing_handler(
"/screen",
headers={
"Content-Type": "application/x-www-form-urlencoded",
"Content-Length": str(MAX_BODY_BYTES + 1),
},
)
oversized.do_POST()
assert oversized.status == 413
assert b"Request is too large" in oversized.wfile.getvalue()
def test_demo_server_unknown_material_fact_routes_to_staff():
body = result_page(
{
"project_type": ["adu"],
"jurisdiction": ["davis"],
"primary_dwelling_status": ["existing_single_family"],
"adu_project_form": ["new_detached"],
"unpermitted_existing": ["unknown"],
},
"en",
)
assert "Staff review is needed" in body
assert "data-rule-id=" not in body
def test_python_demo_exposes_jadu_legalization_and_route_specific_sb9_fields():
source = (ROOT / "demo" / "app.py").read_text(encoding="utf-8")
assert '"jadu": ("primary_dwelling_status", "unpermitted_existing")' in source
assert '"two_unit_contributing_historic_location"' in source
assert '"lot_split_alters_historic_district_resource"' in source
assert 's["unpermitted_questions"]["adu"]' in source
assert 's["unpermitted_questions"]["jadu"]' in source
assert 'projects="jadu"' in source
assert 'f"{described_by}>"' in source
result = result_page(
{
"project_type": ["jadu"],
"jurisdiction": ["davis"],
"primary_dwelling_status": ["existing_single_family"],
"unpermitted_existing": ["yes"],
},
"en",
)
assert 'data-rule-id="jadu-unpermitted-legalization"' in result
def test_python_demo_warns_when_only_local_process_information_matches():
result = result_page(
{
"project_type": ["adu"],
"jurisdiction": ["woodland"],
"primary_dwelling_status": ["none"],
"adu_project_form": ["new_detached"],
"unpermitted_existing": ["no"],
},
"en",
)
assert "do not identify a possible path" in result
assert "Supporting local information is shown below" in result
assert 'data-rule-id="woodland-adu-ordinance-2026"' in result