forked from ChelseaKR/permit-bearings
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_source_review_window.py
More file actions
172 lines (129 loc) · 5.97 KB
/
Copy pathtest_source_review_window.py
File metadata and controls
172 lines (129 loc) · 5.97 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
"""One review window, three runtimes.
The 180-day source review window used to be written out separately in the
harness, the readiness validator, the deployed browser bundle, and the
reference server, with nothing tying them together. These tests fail if any
of them drifts, and if the reference server's staleness precedence stops
matching the harness the browser copies.
"""
from __future__ import annotations
import importlib.util
import json
import re
import sys
from datetime import date, timedelta
from pathlib import Path
from types import ModuleType
import pytest
from permit_pathways.dates import SOURCE_REVIEW_WINDOW_DAYS
from permit_pathways.harness.runner import DEFAULT_MAX_AGE_DAYS
from permit_pathways.readiness import SOURCE_MAX_AGE_DAYS
from permit_pathways.screening import Rule, load_rules
REPOSITORY_ROOT = Path(__file__).resolve().parents[1]
DEMO_APP_PATH = REPOSITORY_ROOT / "demo" / "app.py"
DEMO_JS_PATH = REPOSITORY_ROOT / "assets" / "demo.js"
RULES_PATH = REPOSITORY_ROOT / "data" / "rules"
def _load_demo_app() -> ModuleType:
"""Import the reference server without starting it."""
sys.path.insert(0, str(REPOSITORY_ROOT / "src"))
try:
spec = importlib.util.spec_from_file_location("_demo_app", DEMO_APP_PATH)
assert spec is not None and spec.loader is not None
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
finally:
sys.path.remove(str(REPOSITORY_ROOT / "src"))
return module
@pytest.fixture(scope="module")
def demo_app() -> ModuleType:
return _load_demo_app()
def test_python_constants_share_one_definition() -> None:
assert DEFAULT_MAX_AGE_DAYS is SOURCE_REVIEW_WINDOW_DAYS
assert SOURCE_MAX_AGE_DAYS is SOURCE_REVIEW_WINDOW_DAYS
def test_browser_bundle_mirrors_the_python_window() -> None:
"""``assets/demo.js`` ships to users; it cannot import Python."""
source = DEMO_JS_PATH.read_text(encoding="utf-8")
matches = re.findall(r"^const MAX_AGE_DAYS = (\d+);$", source, re.MULTILINE)
assert matches == [str(SOURCE_REVIEW_WINDOW_DAYS)], (
"assets/demo.js MAX_AGE_DAYS must equal "
f"permit_pathways.dates.SOURCE_REVIEW_WINDOW_DAYS ({SOURCE_REVIEW_WINDOW_DAYS})"
)
def test_reference_server_holds_no_second_window_literal() -> None:
"""The server renders the window; it must not restate the number."""
source = DEMO_APP_PATH.read_text(encoding="utf-8")
assert str(SOURCE_REVIEW_WINDOW_DAYS) not in source, (
"demo/app.py must take the review window from "
"permit_pathways.dates.SOURCE_REVIEW_WINDOW_DAYS, not restate it"
)
def _first_rule_with_dependencies() -> Rule:
rules = load_rules(RULES_PATH)
for rule in rules:
if rule.source_dependencies and rule.citation.is_verified:
return rule
raise AssertionError("no verified rule with source dependencies in data/rules")
def _result_for(rule: Rule):
class _Result:
def __init__(self, matched: Rule) -> None:
self.rule = matched
self.verified = matched.citation.is_verified
return _Result(rule)
def test_changed_source_beats_a_fresh_citation(demo_app: ModuleType) -> None:
"""The bug this file was written for.
A rule whose citation was verified yesterday is still stale once one of
its sources changes. ``harness.runner`` and ``assets/demo.js`` both said
so; ``demo/app.py`` used to answer ``verified``.
"""
rule = _first_rule_with_dependencies()
result = _result_for(rule)
strings = demo_app.STRINGS["en"]
today = date.fromisoformat(rule.citation.verified_on) + timedelta(days=1)
unchanged, _ = demo_app._result_badge(result, strings, today=today)
assert unchanged == "verified"
changed, _ = demo_app._result_badge(
result,
strings,
today=today,
changed_source_ids=[rule.source_dependencies[0]],
)
assert changed == "stale"
def test_reference_server_matches_the_harness_for_every_rule(
demo_app: ModuleType,
) -> None:
"""Walk every rule under both precedences and require agreement."""
from permit_pathways.harness.runner import verify_rules
rules = load_rules(RULES_PATH)
golden_path = REPOSITORY_ROOT / "data" / "golden" / "example.json"
strings = demo_app.STRINGS["en"]
today = date(2026, 8, 15)
for changed in ([], [rule.source_dependencies[0] for rule in rules[:1]]):
report = verify_rules(
RULES_PATH, golden_path, today=today, changed_source_ids=changed
)
expected = {rule_id: "stale" for rule_id in report.stale}
expected.update({rule_id: "unverified" for rule_id in report.unverified})
expected.update({rule_id: "verified" for rule_id in report.verified})
for rule in rules:
status, _ = demo_app._result_badge(
_result_for(rule), strings, today=today, changed_source_ids=changed
)
assert status == expected[rule.rule_id], (
f"{rule.rule_id}: demo/app.py said {status}, "
f"harness said {expected[rule.rule_id]} (changed={changed})"
)
def test_committed_changed_sources_reach_the_screen_path(
demo_app: ModuleType,
) -> None:
"""The server must read the same record the browser bundle ships."""
assert demo_app.SOURCE_STATE_PATH == (
REPOSITORY_ROOT / "data" / "source-status" / "current.json"
)
committed = demo_app.committed_changed_source_ids()
assert isinstance(committed, tuple)
assert all(isinstance(value, str) for value in committed)
record = json.loads(demo_app.SOURCE_STATE_PATH.read_text(encoding="utf-8"))
assert committed == tuple(record["changed_source_ids"])
def test_missing_source_state_does_not_break_the_server(
demo_app: ModuleType, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""An unreadable record must not crash a screening result."""
monkeypatch.setattr(demo_app, "SOURCE_STATE_PATH", tmp_path / "absent.json")
assert demo_app.committed_changed_source_ids() == ()