chart/tests/test_user_prefs.py
Chris Amow 6f03560e74 Default confluence zone alerts off; Config can turn them back on.
Armed drawings and DMA bells still fire. The pref lives in user_prefs
so pushes follow it, not a display checkbox.
2026-08-27 17:09:23 -05:00

57 lines
2.2 KiB
Python

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")
def test_confluence_alerts_default_off_and_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/confluence-alerts").json() == {"enabled": False}
assert client.put("/api/prefs/confluence-alerts", json={"enabled": True}).json() == {
"enabled": True,
}
assert client.get("/api/prefs/confluence-alerts").json() == {"enabled": True}
assert client.put("/api/prefs/confluence-alerts", json={"enabled": "yes"}).status_code == 400