chart/tests/test_ws_preferences.py

51 lines
1.8 KiB
Python

from fastapi import FastAPI
from fastapi.testclient import TestClient
from app.analysis.confluence import cluster_levels
from app.analysis.levels import Level, LevelKind, Side
from app.api.ws import router as ws_router
from app.bars.models import Timeframe
from app.config import Settings
from app.runtime import Runtime
def ma(id_: str, period: int, price: float) -> Level:
return Level(
id_, LevelKind.MA, Timeframe.M1, Side.RESISTANCE, 1, 1,
f"SMA{period}", 1000, price, 0, None, 0, 1000, 1000,
False, False, period,
)
def test_websocket_layer_preferences_are_isolated_per_connection(tmp_path):
app = FastAPI()
app.include_router(ws_router)
runtime = Runtime(Settings(manual_lines_path=tmp_path / "manual_lines.json"))
runtime.levels = [ma("ma20", 20, 100.0), ma("ma50", 50, 100.1)]
runtime.price = 99.9
runtime.atr15 = 1
runtime.stream.last_bar_t = 1000
runtime.clusters = cluster_levels(runtime.levels, 1000, runtime.price, runtime.atr15)
app.state.runtime = runtime
client = TestClient(app)
original_levels = list(runtime.levels)
original_clusters = list(runtime.clusters)
with client.websocket_connect("/ws") as filtered, client.websocket_connect("/ws") as default:
assert len(filtered.receive_json()["clusters"]) == 1
assert len(default.receive_json()["clusters"]) == 1
filtered.send_json(
{
"type": "prefs",
"hidden_levels_score": False,
"enabled": {"ma": {"1m": [20]}},
}
)
assert filtered.receive_json()["clusters"] == []
default.send_json({"type": "subscribe", "tf": "1m"})
assert len(default.receive_json()["clusters"]) == 1
assert runtime.levels == original_levels
assert runtime.clusters == original_clusters