STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
test_session_epoch.py
Go to the documentation of this file.
1"""A recreated backend session is recognisable, so the design bar never autosaves its default.
2
32026-10-01: a backend restart left the session holding configs/default.yaml (methalox); the design
4bar's autosave read it as an edit and saved it over an ethalox design. The bar now compares the
5session epoch it loaded the design into with the one the backend reports, and restores the design
6when they differ. This guards the backend half: every session object has its own epoch, and the
7config routes the bar reads report it.
8"""
9import os
10import sys
11
12sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
13
14from fastapi import FastAPI # noqa: E402
15from fastapi.testclient import TestClient # noqa: E402
16
17from backend.session import UserSession, get_session # noqa: E402
18
19
21 a, b = UserSession("epoch-a"), UserSession("epoch-a")
22 assert a.epoch and b.epoch and a.epoch != b.epoch
23
24
26 from backend.routers import config as config_router
27
28 session = UserSession("epoch-b")
29 app = FastAPI()
30 app.include_router(config_router.router)
31 app.dependency_overrides[get_session] = lambda: session
32 client = TestClient(app)
33 got = client.get("/api/config").json()
34 assert got["session_epoch"] == session.epoch
35 loaded = client.post("/api/config/load", json=got["config"]).json()
36 assert loaded["session_epoch"] == session.epoch
37
38
40 """The session is one per user and every tab shares it. Tab B opening design X POSTs X over
41 the design Y tab A holds; A's autosave then read X as an edit and saved it into Y. Every
42 wholesale load now gets a fresh token, and a fresh session has none (only the default is in
43 it), so A can refuse to autosave a config it did not load, and a read-only tab can tell a
44 default it may replace from another tab's design it must not."""
45 from backend.routers import config as config_router
46
47 session = UserSession("epoch-c")
48 app = FastAPI()
49 app.include_router(config_router.router)
50 app.dependency_overrides[get_session] = lambda: session
51 client = TestClient(app)
52 got = client.get("/api/config").json()
53 assert got["load_token"] is None # nobody has loaded anything: the default
54 first = client.post("/api/config/load", json=got["config"]).json()["load_token"]
55 assert first and client.get("/api/config").json()["load_token"] == first
56 second = client.post("/api/config/load", json=got["config"]).json()["load_token"]
57 assert second and second != first
58 # An edit is not a load: the tab that holds the design keeps its token through a PUT.
59 client.put("/api/config", json={})
60 assert client.get("/api/config").json()["load_token"] == second
a class to store JSON values
Definition json.hpp:19401
test_a_wholesale_load_is_stamped_so_another_tab_can_tell_it_is_not_its_own()