2026-08-28 21:49:05 -04:00
|
|
|
"""Liveness stays up; readiness/freshness is explicit."""
|
|
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
import asyncio
|
2026-11-14 00:34:39 -05:00
|
|
|
import os
|
|
|
|
|
from datetime import datetime, timedelta, timezone
|
2026-08-28 21:49:05 -04:00
|
|
|
|
2026-11-14 00:34:39 -05:00
|
|
|
import asyncpg
|
2026-08-28 21:49:05 -04:00
|
|
|
import httpx
|
2026-11-14 00:34:39 -05:00
|
|
|
import pytest
|
2026-08-28 21:49:05 -04:00
|
|
|
|
2026-11-14 00:34:39 -05:00
|
|
|
from conftest import requires_db
|
2026-08-28 21:49:05 -04:00
|
|
|
from main import app
|
|
|
|
|
|
|
|
|
|
BASE = "http://test"
|
2026-11-14 00:34:39 -05:00
|
|
|
_FIXTURE_URLS = (
|
|
|
|
|
"https://test.invalid/t_ceb74e64/future",
|
|
|
|
|
"https://test.invalid/t_ceb74e64/fresh",
|
|
|
|
|
)
|
2026-08-28 21:49:05 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
async def _get(path: str) -> httpx.Response:
|
|
|
|
|
transport = httpx.ASGITransport(app=app)
|
|
|
|
|
async with httpx.AsyncClient(transport=transport, base_url=BASE) as client:
|
|
|
|
|
return await client.get(path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_health_includes_checks_even_when_db_ok(monkeypatch):
|
|
|
|
|
"""HUD must be able to show degraded without docker killing the container."""
|
|
|
|
|
body = asyncio.run(_get("/api/health")).json()
|
|
|
|
|
assert "status" in body
|
|
|
|
|
assert "checks" in body
|
|
|
|
|
assert "db" in body["checks"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_ready_endpoint_exists():
|
|
|
|
|
resp = asyncio.run(_get("/api/ready"))
|
|
|
|
|
assert resp.status_code in (200, 503)
|
|
|
|
|
body = resp.json()
|
|
|
|
|
assert "checks" in body
|
|
|
|
|
assert "status" in body
|
2026-11-14 00:34:39 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def _conn_kwargs() -> dict:
|
|
|
|
|
return {
|
|
|
|
|
"host": os.environ["DB_HOST"],
|
|
|
|
|
"port": int(os.environ["DB_PORT"]),
|
|
|
|
|
"user": os.environ["DB_USER"],
|
|
|
|
|
"password": os.environ["DB_PASSWORD"],
|
|
|
|
|
"database": os.environ["DB_NAME"],
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def _prepare_article_age_fixture() -> None:
|
|
|
|
|
"""Insert one row at now+6d and one at now-120s. Deletes only those URLs."""
|
|
|
|
|
conn = await asyncpg.connect(**_conn_kwargs())
|
|
|
|
|
try:
|
|
|
|
|
await conn.execute(
|
|
|
|
|
"DELETE FROM articles WHERE url = ANY($1::text[])",
|
|
|
|
|
list(_FIXTURE_URLS),
|
|
|
|
|
)
|
|
|
|
|
now = datetime.now(timezone.utc)
|
|
|
|
|
await conn.executemany(
|
|
|
|
|
"INSERT INTO articles (title, url, content, domain, timestamp) "
|
|
|
|
|
"VALUES ($1, $2, $3, $4, $5)",
|
|
|
|
|
[
|
|
|
|
|
("future", _FIXTURE_URLS[0], "x", "test.invalid", now + timedelta(days=6)),
|
|
|
|
|
("fresh", _FIXTURE_URLS[1], "x", "test.invalid", now - timedelta(seconds=120)),
|
|
|
|
|
],
|
|
|
|
|
)
|
|
|
|
|
finally:
|
|
|
|
|
await conn.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def _delete_article_age_fixture() -> None:
|
|
|
|
|
conn = await asyncpg.connect(**_conn_kwargs())
|
|
|
|
|
try:
|
|
|
|
|
await conn.execute(
|
|
|
|
|
"DELETE FROM articles WHERE url = ANY($1::text[])",
|
|
|
|
|
list(_FIXTURE_URLS),
|
|
|
|
|
)
|
|
|
|
|
finally:
|
|
|
|
|
await conn.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture()
|
|
|
|
|
def article_age_rows():
|
|
|
|
|
asyncio.run(_prepare_article_age_fixture())
|
|
|
|
|
yield
|
|
|
|
|
asyncio.run(_delete_article_age_fixture())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@requires_db
|
|
|
|
|
def test_health_article_age_ignores_future_rows(article_age_rows):
|
|
|
|
|
"""Future-dated rows must not pin articles.age_s negative.
|
|
|
|
|
|
|
|
|
|
Fixture: one row at now+6d, one at now-120s. age_s is the age of the
|
|
|
|
|
newest timestamp that is not in the future (NULL timestamps do not count).
|
|
|
|
|
"""
|
|
|
|
|
body = asyncio.run(_get("/api/health")).json()
|
|
|
|
|
articles = body["checks"]["articles"]
|
|
|
|
|
age = articles["age_s"]
|
|
|
|
|
assert age is not None
|
|
|
|
|
assert 0 <= age <= 600, articles
|
|
|
|
|
assert articles["ok"] is True
|