from fastapi import FastAPI from fastapi.testclient import TestClient from app.analysis.user_prefs import SHARED_USER, UserPrefStore from app.api.routes import router from app.config import Settings from app.runtime import Runtime def test_ma_alert_prefs_round_trip(tmp_path): store = UserPrefStore(tmp_path / "user_prefs.json") store.put("ma_alerts", {"1d": [200]}) loaded = UserPrefStore(tmp_path / "user_prefs.json") assert loaded.get("ma_alerts") == {"1d": [200]} assert loaded.get("ma_alerts", user_id="other") is None def test_corrupt_prefs_start_empty(tmp_path): path = tmp_path / "user_prefs.json" path.write_text("{not json", encoding="utf-8") assert UserPrefStore(path).get("ma_alerts", {"1d": []}) == {"1d": []} def test_ma_alert_endpoints_persist(tmp_path): app = FastAPI() app.include_router(router) app.state.runtime = Runtime(Settings( manual_lines_path=tmp_path / "manual_lines.json", alert_state_path=tmp_path / "alert_state.json", user_prefs_path=tmp_path / "user_prefs.json", )) client = TestClient(app) assert client.get("/api/prefs/ma-alerts").json() == {"1d": []} assert client.put("/api/prefs/ma-alerts", json={"1d": [10, 200]}).json() == {"1d": [10, 200]} assert client.get("/api/prefs/ma-alerts").json() == {"1d": [10, 200]} assert client.put("/api/prefs/ma-alerts", json={"1d": [7]}).status_code == 400 assert SHARED_USER in (tmp_path / "user_prefs.json").read_text(encoding="utf-8")