forked from ChelseaKR/sprout
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_freshness.py
More file actions
233 lines (189 loc) · 7.58 KB
/
Copy pathtest_freshness.py
File metadata and controls
233 lines (189 loc) · 7.58 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
"""Citation freshness / link-liveness tests (research item E7).
``today`` is fixed throughout for determinism, mirroring ``tests/conftest.py``'s
hermetic, no-disk fixture style.
"""
from __future__ import annotations
from datetime import date
from typing import Any
import pytest
from pydantic import ValidationError
from sprout.freshness import (
FreshnessFinding,
check_freshness,
check_liveness,
summarize,
)
from sprout.ingest import ManifestEntry
_TODAY = date(2026, 7, 3)
def _entry(
file: str = "monstera.md",
*,
fetch_date: str = "2026-05-01",
topic: str = "care",
title: str = "Monstera care",
url: str = "https://example.invalid/monstera",
) -> ManifestEntry:
return ManifestEntry(
file=file,
title=title,
source_name="Synthetic Plant-Care Notes",
url=url,
license="CC0-1.0",
fetch_date=fetch_date,
topic=topic,
)
def test_fresh_manifest_yields_no_findings() -> None:
manifest = {"monstera.md": _entry(fetch_date="2026-05-01")}
findings = check_freshness(manifest, today=_TODAY, max_age_days=365)
assert findings == []
def test_old_fetch_date_flags_stale_warning() -> None:
old = "2024-01-01" # well over 365d before _TODAY
manifest = {"monstera.md": _entry(fetch_date=old)}
findings = check_freshness(manifest, today=_TODAY, max_age_days=365)
assert len(findings) == 1
finding = findings[0]
assert finding.severity == "warning"
assert finding.file == "monstera.md"
assert finding.age_days is not None and finding.age_days > 365
assert "citation is" in finding.reason
def test_toxicity_topic_uses_stricter_threshold() -> None:
# 200 days old: within the general 365d window but past the toxicity 180d window.
fetch_date = date.fromisoformat("2026-07-03")
stale_date = fetch_date.toordinal() - 200
fetch_date_str = date.fromordinal(stale_date).isoformat()
manifest = {
"care.md": _entry(file="care.md", fetch_date=fetch_date_str, topic="care"),
"tox.md": _entry(
file="tox.md",
fetch_date=fetch_date_str,
topic="toxicity",
title="Pothos toxicity",
),
}
findings = check_freshness(manifest, today=_TODAY, max_age_days=365, toxicity_max_age_days=180)
by_file = {f.file: f for f in findings}
assert "care.md" not in by_file # 200d < 365d general window: not stale
assert by_file["tox.md"].severity == "high"
assert "toxicity" in by_file["tox.md"].reason
def test_toxicity_inferred_from_title_when_topic_is_generic() -> None:
# The bundled corpus has no dedicated "toxicity" topic yet (everything is "care"),
# so a toxicity-titled entry must still get the stricter threshold via the title
# fallback.
stale_date = date.fromordinal(_TODAY.toordinal() - 200).isoformat()
manifest = {
"snake-plant.md": _entry(
file="snake-plant.md",
fetch_date=stale_date,
topic="care",
title="Snake plant care and toxicity",
)
}
findings = check_freshness(manifest, today=_TODAY, max_age_days=365, toxicity_max_age_days=180)
assert len(findings) == 1
assert findings[0].severity == "high"
def test_unparseable_date_flags_error() -> None:
manifest = {"monstera.md": _entry(fetch_date="not-a-date")}
findings = check_freshness(manifest, today=_TODAY, max_age_days=365)
assert len(findings) == 1
assert findings[0].severity == "high"
assert "unparseable" in findings[0].reason
def test_future_date_flags_error() -> None:
manifest = {"monstera.md": _entry(fetch_date="2099-01-01")}
findings = check_freshness(manifest, today=_TODAY, max_age_days=365)
assert len(findings) == 1
assert findings[0].severity == "high"
assert "future" in findings[0].reason
def test_summarize_counts_by_severity() -> None:
findings = [
FreshnessFinding(
file="a.md",
url="https://example.invalid/a",
topic="care",
fetch_date="2024-01-01",
age_days=900,
severity="warning",
reason="x",
),
FreshnessFinding(
file="b.md",
url="https://example.invalid/b",
topic="toxicity",
fetch_date="bad",
age_days=None,
severity="high",
reason="y",
),
]
counts = summarize(findings)
assert counts == {"warning": 1, "high": 1}
assert summarize([]) == {"warning": 0, "high": 0}
class _FakeResponse:
def __init__(self, status_code: int) -> None:
self.status_code = status_code
class _FakeClient:
"""Records every call so tests can assert example.invalid is never touched."""
def __init__(self, status_codes: dict[str, int] | None = None) -> None:
self.status_codes = status_codes or {}
self.calls: list[str] = []
def head(self, url: str) -> _FakeResponse:
self.calls.append(url)
return _FakeResponse(self.status_codes.get(url, 200))
def get(self, url: str) -> _FakeResponse:
self.calls.append(url)
return _FakeResponse(self.status_codes.get(url, 200))
def close(self) -> None:
pass
def test_example_invalid_urls_excluded_from_liveness() -> None:
manifest = {
"monstera.md": _entry(url="https://example.invalid/monstera"),
"pothos.md": _entry(file="pothos.md", url="https://example.invalid/pothos"),
}
client = _FakeClient()
findings = check_liveness(manifest, client=client)
assert findings == []
assert client.calls == [] # never touched — synthetic corpus stays fully offline
def test_liveness_flags_dead_link_for_non_excluded_host() -> None:
manifest = {
"real.md": _entry(file="real.md", url="https://real.example.com/care"),
}
client = _FakeClient(status_codes={"https://real.example.com/care": 404})
findings = check_liveness(manifest, client=client)
assert len(findings) == 1
assert findings[0].severity == "high"
assert "404" in findings[0].reason
def test_liveness_ok_link_yields_no_finding() -> None:
manifest = {
"real.md": _entry(file="real.md", url="https://real.example.com/care"),
}
client = _FakeClient(status_codes={"https://real.example.com/care": 200})
findings = check_liveness(manifest, client=client)
assert findings == []
def test_liveness_transport_error_flags_unreachable() -> None:
class _RaisingClient:
def head(self, url: str) -> Any:
raise ConnectionError("boom")
def close(self) -> None:
pass
manifest = {"real.md": _entry(file="real.md", url="https://real.example.com/care")}
findings = check_liveness(manifest, client=_RaisingClient())
assert len(findings) == 1
assert findings[0].severity == "high"
assert "unreachable" in findings[0].reason
def test_check_liveness_owns_and_closes_default_client_when_all_urls_excluded() -> None:
# No client injected, but every URL is example.invalid, so this must not touch the
# network and must still succeed (constructs and closes a real httpx.Client).
manifest = {"monstera.md": _entry(url="https://example.invalid/monstera")}
findings = check_liveness(manifest)
assert findings == []
def test_freshness_finding_rejects_unknown_fields() -> None:
with pytest.raises(ValidationError):
FreshnessFinding(
file="a.md",
url="https://example.invalid/a",
topic="care",
fetch_date="2026-05-01",
age_days=0,
severity="warning",
reason="x",
bogus="nope", # type: ignore[call-arg]
)