forked from ChelseaKR/queer-the-stacks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_server_lifecycle.py
More file actions
411 lines (312 loc) · 13.9 KB
/
Copy pathtest_server_lifecycle.py
File metadata and controls
411 lines (312 loc) · 13.9 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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
"""FIX-14 — serving-path lifecycle: startup checks, view cache, 503-until-refreshed.
Covers what `app/server.py` is actually responsible for now that first-run
ingest no longer runs inside a request:
* data routes work end-to-end against a populated store (behind auth),
* data routes fail closed with 503 against a never-refreshed store,
* the built `DashboardView` is cached and only rebuilt when the store's
`refreshed_at()` stamp (or the view-relevant config) actually changes,
* the FastAPI lifespan fails closed on bad config / an unreachable store.
"""
from __future__ import annotations
import time
from pathlib import Path
import pytest
AUTH = {"Authorization": "Bearer demo-token"}
def _demo_env(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:
monkeypatch.setenv("STACKS_DEMO", "1")
monkeypatch.setenv("STACKS_DATA_DIR", str(tmp_path))
# --- (a) full route coverage against a populated store -----------------------
def test_all_data_routes_serve_against_a_populated_store(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
pytest.importorskip("fastapi")
from app import server
from fastapi.testclient import TestClient
from tests.conftest import seed_store_from_env
_demo_env(monkeypatch, tmp_path)
seed_store_from_env()
with TestClient(server.create_app()) as client:
dashboard = client.get("/", headers=AUTH)
assert dashboard.status_code == 200
assert "Queer the Stacks" in dashboard.text
browse = client.get("/browse", headers=AUTH, params={"q": "a"})
assert browse.status_code == 200
share = client.get("/share", headers=AUTH)
assert share.status_code == 200
assert "Share cards" in share.text
svg = client.get("/share/card.svg", headers=AUTH)
assert svg.status_code == 200
assert svg.headers["content-type"].startswith("image/svg+xml")
# --- (b) 503 fail-closed on an unpopulated store ------------------------------
def test_data_routes_503_until_the_store_is_refreshed(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
pytest.importorskip("fastapi")
from app import server
from fastapi.testclient import TestClient
_demo_env(monkeypatch, tmp_path)
# Deliberately do NOT seed the store — no `stacks refresh` has ever run.
with TestClient(server.create_app()) as client:
for path in ("/", "/browse", "/share", "/share/card.svg"):
resp = client.get(path, headers=AUTH)
assert resp.status_code == 503, path
def test_healthz_and_livez_never_503_when_unpopulated(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
pytest.importorskip("fastapi")
from app import server
from fastapi.testclient import TestClient
_demo_env(monkeypatch, tmp_path)
with TestClient(server.create_app()) as client:
assert client.get("/healthz").status_code == 200
assert client.get("/livez").status_code == 200
# --- (c) the view cache is stamp+config keyed ---------------------------------
def test_view_cache_reuses_the_built_view_until_the_store_is_refreshed_again(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
pytest.importorskip("fastapi")
from app import server
from fastapi.testclient import TestClient
from ingest.config import load_config
from ingest.refresh import refresh
from ingest.store import Store
from tests.conftest import seed_store_from_env
_demo_env(monkeypatch, tmp_path)
seed_store_from_env()
calls = {"n": 0}
real_view_from_store = server.view_from_store
def counting_view_from_store(*args: object, **kwargs: object) -> object:
calls["n"] += 1
return real_view_from_store(*args, **kwargs) # type: ignore[arg-type]
monkeypatch.setattr(server, "view_from_store", counting_view_from_store)
app = server.create_app()
with TestClient(app) as client:
first = client.get("/", headers=AUTH)
second = client.get("/", headers=AUTH)
assert first.status_code == second.status_code == 200
assert calls["n"] == 1, "second request should hit the cache, not rebuild"
# /browse only replaces .library on the cached base view — still no rebuild.
browse = client.get("/browse", headers=AUTH)
assert browse.status_code == 200
assert calls["n"] == 1
# Bump refreshed_at (as `stacks refresh` would) -> the next request rebuilds.
config = load_config()
store = Store(config.store_path)
try:
refresh(config, store, now=int(time.time()) + 3600, force=True)
finally:
store.close()
third = client.get("/", headers=AUTH)
assert third.status_code == 200
assert calls["n"] == 2, "a new refreshed_at stamp must invalidate the cache"
def test_view_cache_invalidates_when_catalog_changes_without_new_refresh_stamp(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Catalog persistence advances a revision even within the same second."""
pytest.importorskip("fastapi")
from app import server
from fastapi.testclient import TestClient
from ingest.config import load_config
from ingest.models import Book
from ingest.store import CatalogSourceUpdate, Store
from tests.conftest import seed_store_from_env
_demo_env(monkeypatch, tmp_path)
seed_store_from_env()
calls = {"n": 0}
real_view_from_store = server.view_from_store
def counting_view_from_store(*args: object, **kwargs: object) -> object:
calls["n"] += 1
return real_view_from_store(*args, **kwargs) # type: ignore[arg-type]
monkeypatch.setattr(server, "view_from_store", counting_view_from_store)
with TestClient(server.create_app()) as client:
first = client.get("/", headers=AUTH)
config = load_config()
with Store(config.store_path) as store:
stamp = store.refreshed_at()
store.save_catalog_refresh(
(
CatalogSourceUpdate(
source_id="demo:changed",
books=(Book(book_id="new", title="Changed Pool"),),
),
),
active_source_ids={"demo:changed"},
attempted_at=stamp or 0,
outbound_mode="off",
)
assert store.refreshed_at() == stamp
changed = client.get("/", headers=AUTH)
assert first.status_code == changed.status_code == 200
assert calls["n"] == 2
assert "demo:changed" in changed.text
def test_view_cache_key_changes_with_view_relevant_config(tmp_path: Path) -> None:
import dataclasses
from app import server
from ingest.config import Config
base = Config(
calibre_db=None,
koreader_db=None,
data_dir=tmp_path,
kosync_host=None,
kosync_user=None,
kosync_key=None,
demo=True,
)
varied = dataclasses.replace(base, goal_books=42)
assert server._cache_key(base, 100) != server._cache_key(varied, 100)
assert server._cache_key(base, 100) == server._cache_key(base, 100)
assert server._cache_key(base, 100) != server._cache_key(base, 200)
assert server._cache_key(base, 100) != server._cache_key(base, 100, hide_sensitive=True)
def test_view_cache_keeps_alternating_privacy_requests_separate(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""A redacted request must never receive a previously cached full view."""
pytest.importorskip("fastapi")
from app import server
from fastapi.testclient import TestClient
from tests.conftest import seed_store_from_env
_demo_env(monkeypatch, tmp_path)
seed_store_from_env()
calls = {"n": 0}
real_view_from_store = server.view_from_store
def counting_view_from_store(*args: object, **kwargs: object) -> object:
calls["n"] += 1
return real_view_from_store(*args, **kwargs) # type: ignore[arg-type]
monkeypatch.setattr(server, "view_from_store", counting_view_from_store)
with TestClient(server.create_app()) as client:
full = client.get("/", headers=AUTH)
redacted = client.get("/", headers=AUTH, params={"hide_sensitive": "true"})
full_again = client.get("/", headers=AUTH)
assert full.status_code == redacted.status_code == full_again.status_code == 200
assert "(hidden for privacy)" not in full.text
assert "(hidden for privacy)" in redacted.text
assert "(hidden for privacy)" not in full_again.text
assert calls["n"] == 3
def test_browse_preserves_structured_filters_in_search_form(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
pytest.importorskip("fastapi")
from app import server
from fastapi.testclient import TestClient
from tests.conftest import seed_store_from_env
_demo_env(monkeypatch, tmp_path)
seed_store_from_env()
with TestClient(server.create_app()) as client:
response = client.get(
"/browse",
headers=AUTH,
params={
"q": "stone",
"theme": 'trans "history"',
"author": "Leslie",
"series": "Series A",
"status": "unread",
},
)
assert response.status_code == 200
assert 'value="stone"' in response.text
assert 'name="theme" value="trans "history""' in response.text
assert 'name="author" value="Leslie"' in response.text
assert 'name="series" value="Series A"' in response.text
assert 'name="status" value="unread"' in response.text
def test_view_cache_invalidates_when_lens_file_content_changes(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
pytest.importorskip("fastapi")
from app import server
from fastapi.testclient import TestClient
from tests.conftest import seed_store_from_env
lens_path = tmp_path / "lenses.toml"
lens_path.write_text(
'[[lenses]]\nname = "First lens"\nlabels = ["queer"]\n',
encoding="utf-8",
)
_demo_env(monkeypatch, tmp_path)
monkeypatch.setenv("STACKS_LENS_CONFIG", str(lens_path))
seed_store_from_env()
calls = {"n": 0}
real_view_from_store = server.view_from_store
def counting_view_from_store(*args: object, **kwargs: object) -> object:
calls["n"] += 1
return real_view_from_store(*args, **kwargs) # type: ignore[arg-type]
monkeypatch.setattr(server, "view_from_store", counting_view_from_store)
with TestClient(server.create_app()) as client:
assert client.get("/", headers=AUTH).status_code == 200
lens_path.write_text(
'[[lenses]]\nname = "Other lens"\nlabels = ["fantasy"]\n',
encoding="utf-8",
)
changed = client.get("/", headers=AUTH)
assert changed.status_code == 200
assert calls["n"] == 2
assert "Other lens" in changed.text
def test_view_cache_invalidates_when_authored_lists_change(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
pytest.importorskip("fastapi")
from app import server
from fastapi.testclient import TestClient
from recommender.lists import CuratedList
from recommender.lists_store import save_lists
from tests.conftest import seed_store_from_env
_demo_env(monkeypatch, tmp_path)
seed_store_from_env()
calls = {"n": 0}
real_view_from_store = server.view_from_store
def counting_view_from_store(*args: object, **kwargs: object) -> object:
calls["n"] += 1
return real_view_from_store(*args, **kwargs) # type: ignore[arg-type]
monkeypatch.setattr(server, "view_from_store", counting_view_from_store)
with TestClient(server.create_app()) as client:
first = client.get("/", headers=AUTH)
save_lists(
tmp_path / "lists.json",
(
CuratedList(
name="My local list",
citation="local-list:mine",
book_ids=("calibre:1",),
retrieved_at="2026-08-15",
),
),
)
changed = client.get("/", headers=AUTH)
assert first.status_code == changed.status_code == 200
assert calls["n"] == 2
assert "My local list" in changed.text
# --- (d) lifespan validation fails closed -------------------------------------
def test_lifespan_fails_closed_on_invalid_config(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
pytest.importorskip("fastapi")
from app import server
from fastapi.testclient import TestClient
bad_config = tmp_path / "stacks.toml"
bad_config.write_text("this is not valid TOML [[[", encoding="utf-8")
monkeypatch.setenv("STACKS_CONFIG", str(bad_config))
with pytest.raises(server.ConfigInvalid), TestClient(server.create_app()):
pass # pragma: no cover - startup must raise before we get here
def test_lifespan_fails_closed_on_unreachable_store(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
pytest.importorskip("fastapi")
from app import server
from fastapi.testclient import TestClient
# A regular file where the store's data dir should be: Store's mkdir(parents=True)
# cannot create a directory there, so opening the store raises at startup.
blocker = tmp_path / "not-a-directory"
blocker.write_text("x", encoding="utf-8")
monkeypatch.setenv("STACKS_DEMO", "1")
monkeypatch.setenv("STACKS_DATA_DIR", str(blocker / "data"))
with pytest.raises(server.StoreUnavailable), TestClient(server.create_app()):
pass # pragma: no cover - startup must raise before we get here
def test_lifespan_does_not_block_startup_when_store_is_unpopulated(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""An empty store is a warning, not a startup failure — routes 503 instead."""
pytest.importorskip("fastapi")
from app import server
from fastapi.testclient import TestClient
_demo_env(monkeypatch, tmp_path)
with TestClient(server.create_app()) as client:
assert client.get("/healthz").status_code == 200