from __future__ import annotations import os from pathlib import Path import numpy as np import pandas as pd import pytest from fastapi.testclient import TestClient CONTROL_TOKEN = "test-token" @pytest.fixture(scope="session") def client(tmp_path_factory): os.environ["CONTROL_TOKEN"] = CONTROL_TOKEN os.environ["PULSECAST_MODELS"] = str(tmp_path_factory.mktemp("models")) os.environ["PULSECAST_UPLOADS"] = str(tmp_path_factory.mktemp("uploads")) from pulsecast.server import app with TestClient(app) as test_client: yield test_client @pytest.fixture(scope="session") def auth_headers() -> dict[str, str]: return {"Authorization": f"Bearer {CONTROL_TOKEN}"} @pytest.fixture(scope="session") def synthetic_csv(tmp_path_factory) -> Path: """Two sensors, 60 days hourly, clear daily/weekly pattern + weather.""" rng = np.random.default_rng(7) hours = pd.date_range("2026-06-01", periods=24 * 60, freq="h", tz="UTC") rows = [] for sensor in ("test-store-a", "test-store-b"): for timestamp in hours: local = timestamp.tz_convert("Europe/Berlin") daily = 35.0 * np.exp(-((local.hour - 19) ** 2) / 18.0) weekend = 12.0 if local.day_of_week == 5 else 0.0 value = max(0.0, 8.0 + daily + weekend + rng.normal(0, 4)) rows.append( { "time": timestamp.strftime("%Y-%m-%dT%H:%M:%SZ"), "sensor": sensor, "value": round(value, 3), "temp_c": round(15.0 + rng.normal(0, 5), 2), "precip_mm": round(max(0.0, rng.normal(0, 1)), 2), } ) path = tmp_path_factory.mktemp("csv") / "upload.csv" pd.DataFrame(rows).to_csv(path, index=False) return path