feat(api): implement async fetch with callback to enqueue build job

This commit is contained in:
shay
2026-05-04 12:35:41 -04:00
parent 50dd73a0de
commit 4a1d31ffdd
9 changed files with 399 additions and 32 deletions
+51
View File
@@ -10,6 +10,8 @@ def client(monkeypatch):
# No-op the kickoff so tests don't spawn background threads that
# would try to reach the (unmocked) fetcher and race tempdir teardown.
monkeypatch.setattr(apimod, "_kickoff_prefetch_async", lambda *_a, **_kw: None)
# Same reason for the new async-fetch hook used by the OAuth callback.
monkeypatch.setattr(apimod, "_request_fetch_async", lambda *_a, **_kw: None)
with tempfile.TemporaryDirectory() as d:
monkeypatch.setattr(dbmod, "SETTINGS_DB_PATH", os.path.join(d, "s.db"))
monkeypatch.setattr(dbmod, "WIDGETS_DB_PATH", os.path.join(d, "w.db"))
@@ -304,6 +306,55 @@ def test_enroll_endpoint_is_gone(client):
assert r.status_code == 405
# ---- POST /internal/data-ready: callback from fetcher when an async fetch lands ----
def test_data_ready_requires_internal_token(client, monkeypatch):
monkeypatch.setattr(cfg, "FETCHER_INTERNAL_TOKEN", "secret")
r = client.post("/internal/data-ready", json={"username": "alice"})
assert r.status_code == 401
def test_data_ready_enqueues_build_for_enrolled_user(client, monkeypatch):
"""The whole point of the async path: the build job appears in the queue
after the fetcher has stored data, not before. Enrolling a user no
longer adds a job, so /data-ready is the only thing that does."""
monkeypatch.setattr(cfg, "FETCHER_INTERNAL_TOKEN", "secret")
dbmod.enroll("alice", {"theme": "dark"}, enqueue_build=False)
assert dbmod.pending_job_count() == 0
r = client.post("/internal/data-ready",
headers={"X-Internal-Token": "secret"},
json={"username": "alice", "payload_hash": "h1", "ok": True})
assert r.status_code == 200
assert r.get_json()["queued"] is True
assert dbmod.pending_job_count() == 1
def test_data_ready_ignores_unknown_user(client, monkeypatch):
"""If we get a callback for someone who hasn't enrolled here we must not
create a job — the fetcher might be shared or the enrollment might
have been deleted in between."""
monkeypatch.setattr(cfg, "FETCHER_INTERNAL_TOKEN", "secret")
r = client.post("/internal/data-ready",
headers={"X-Internal-Token": "secret"},
json={"username": "stranger", "payload_hash": "h1", "ok": True})
assert r.status_code == 200
assert r.get_json()["ignored"] is True
assert dbmod.pending_job_count() == 0
def test_data_ready_does_not_enqueue_on_failed_fetch(client, monkeypatch):
monkeypatch.setattr(cfg, "FETCHER_INTERNAL_TOKEN", "secret")
dbmod.enroll("alice", {"theme": "dark"}, enqueue_build=False)
r = client.post("/internal/data-ready",
headers={"X-Internal-Token": "secret"},
json={"username": "alice", "payload_hash": "", "ok": False})
assert r.status_code == 200
assert r.get_json()["ignored"] is True
assert dbmod.pending_job_count() == 0
def test_query_override_triggers_adhoc_render_without_persisting(client):
"""GET /api/<u>?theme=... renders on-demand via worker.render_composite_adhoc
and does not mutate the stored settings blob — this is the path visitors
+11
View File
@@ -31,6 +31,17 @@ def test_enroll_twice_noops(tmp_dbs):
assert r1["job_id"] > 0 and r2["job_id"] > 0
def test_enroll_without_build_skips_job(tmp_dbs):
"""OAuth callback uses this so the build job is only added once the
fetcher has actually fetched (via /internal/data-ready). Otherwise the
worker pops a job for a user with no fetcher data yet."""
r = dbmod.enroll("alice", {"theme": "dark"}, enqueue_build=False)
assert r["job_id"] is None
assert dbmod.pending_job_count() == 0
# The user row itself was still created so callbacks can find them.
assert dbmod.get_settings("alice") is not None
def test_enroll_with_github_profile(tmp_dbs):
dbmod.enroll("alice", {"theme": "dark"}, github_id=1, github_avatar_url="https://x/a.png")
with dbmod._settings_conn() as c: