GET /bootstrap returns the 8 sensors, hourly series for all of them, context, meta, and versions in the CONTRACT.md shape. Snapshot artifacts are projected to contract fields once at boot; incomplete artifacts return 503. Adds tests for shape, sensor count, hourly alignment, and versions. Dockerfile now creates UID/GID 10001, uses COPY --chown=10001:10001, and runs as USER 10001:10001. Adds make check and a minimal Gitea Actions workflow that runs the same local gate: uv sync --frozen, compileall, pytest.
90 lines
2.9 KiB
Python
90 lines
2.9 KiB
Python
from __future__ import annotations
|
|
|
|
from datetime import datetime
|
|
|
|
from pulsecast import config
|
|
|
|
BOOTSTRAP_KEYS = {
|
|
"data_version",
|
|
"model_version",
|
|
"context_version",
|
|
"sensors",
|
|
"series",
|
|
"context",
|
|
"meta",
|
|
}
|
|
|
|
SENSOR_KEYS = {"id", "name", "lat", "lon", "mean", "p95", "battery"}
|
|
|
|
|
|
def _timestamp(value: str) -> datetime:
|
|
assert value.endswith("Z")
|
|
return datetime.fromisoformat(value.replace("Z", "+00:00"))
|
|
|
|
|
|
def test_bootstrap_shape(client):
|
|
response = client.get("/bootstrap")
|
|
assert response.status_code == 200, response.text
|
|
assert response.headers["content-type"].startswith("application/json")
|
|
payload = response.json()
|
|
|
|
assert set(payload) == BOOTSTRAP_KEYS
|
|
_timestamp(payload["data_version"])
|
|
_timestamp(payload["context_version"])
|
|
assert payload["model_version"].startswith("histgb-")
|
|
|
|
sensors = payload["sensors"]
|
|
assert len(sensors) == 8
|
|
assert {sensor["id"] for sensor in sensors} == set(config.USABLE_SENSORS)
|
|
assert [sensor["id"] for sensor in sensors] == sorted(sensor["id"] for sensor in sensors)
|
|
for sensor in sensors:
|
|
assert set(sensor) == SENSOR_KEYS
|
|
assert sensor["p95"] >= sensor["mean"]
|
|
assert sensor["battery"] is None or 0 <= sensor["battery"] <= 100
|
|
|
|
|
|
def test_bootstrap_series_is_hourly_for_all_eight_sensors(client):
|
|
series = client.get("/bootstrap").json()["series"]
|
|
assert set(series) == set(config.USABLE_SENSORS)
|
|
|
|
total_points = 0
|
|
for sensor, points in series.items():
|
|
assert len(points) > 1000, sensor
|
|
total_points += len(points)
|
|
previous = None
|
|
for point in points:
|
|
assert set(point) == {"t", "v"}
|
|
timestamp = _timestamp(point["t"])
|
|
assert timestamp.minute == 0 and timestamp.second == 0
|
|
if previous is not None:
|
|
assert timestamp > previous
|
|
previous = timestamp
|
|
assert point["v"] >= 0
|
|
assert total_points > 20_000
|
|
|
|
|
|
def test_bootstrap_context_and_meta(client):
|
|
payload = client.get("/bootstrap").json()
|
|
|
|
context = payload["context"]
|
|
assert set(context) == {"weather", "holidays", "updated_at"}
|
|
assert set(context["weather"]) == {"now", "forecast"}
|
|
assert len(context["weather"]["forecast"]) == 168
|
|
assert context["holidays"]
|
|
assert {"date", "name"} == set(context["holidays"][0])
|
|
_timestamp(context["updated_at"])
|
|
|
|
meta = payload["meta"]
|
|
assert set(meta) == {"coverage", "metrics", "limits"}
|
|
assert meta["coverage"]["sensors"] == 8
|
|
assert meta["coverage"]["cadence_min"] == 15
|
|
assert meta["metrics"]["mae"] < meta["metrics"]["baseline_mae"]
|
|
assert len(meta["limits"]) >= 4
|
|
|
|
|
|
def test_bootstrap_versions_match_meta(client):
|
|
payload = client.get("/bootstrap").json()
|
|
meta = client.get("/meta").json()
|
|
for key in ("data_version", "model_version", "context_version"):
|
|
assert payload[key] == meta[key]
|