STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
test_runs.py
Go to the documentation of this file.
1"""Unit tests for run discovery (no elodin-db / real run needed).
2
3Run: cd webviewer && .venv/bin/python -m pytest backend/test_runs.py -q
4"""
5
6import pytest
7
8from . import config, export_cache, runs
9
10
11@pytest.fixture
12def elodin_dir(tmp_path, monkeypatch):
13 """Point the backend at an empty fake ELODIN_DIR with the cache stubbed out."""
14 monkeypatch.setattr(config, "ELODIN_DIR", tmp_path)
15 monkeypatch.setattr(export_cache, "is_cached", lambda run_id: False)
16 return tmp_path
17
18
20 # The sim prefix comes from session-manager's timestampName(); it must not
21 # make a run invisible (it silently did before the pattern allowed `sim_`).
22 (elodin_dir / "daq_20260811_120000").mkdir()
23 (elodin_dir / "daq_sim_20260827_204900").mkdir()
24
25 found = {r["id"]: r for r in runs.list_runs()}
26 assert set(found) == {"daq_20260811_120000", "daq_sim_20260827_204900"}
27 assert found["daq_20260811_120000"]["simulated"] is False
28 assert found["daq_sim_20260827_204900"]["simulated"] is True
29 # The timestamp is read past the prefix, not off the raw name.
30 assert found["daq_sim_20260827_204900"]["started"] == "2026-08-27T20:49:00"
31
32
34 for name in (
35 "daq_live",
36 "calibration",
37 "daq_sim",
38 "daq_sim_live",
39 "daq_hotfire_test",
40 "daq_2026081_120000", # short date
41 "daq_20260811_12000", # short time
42 "xdaq_20260811_120000",
43 "daq_20260811_120000_old",
44 ):
45 (elodin_dir / name).mkdir()
46 # A file matching the pattern is not a run either.
47 (elodin_dir / "daq_20260811_120000").write_text("not a directory")
48
49 assert runs.list_runs() == []
50
51
53 for name in (
54 "daq_20260811_120000",
55 "daq_sim_20260827_204900",
56 "daq_20260901_000000",
57 ):
58 (elodin_dir / name).mkdir()
59
60 assert [r["id"] for r in runs.list_runs()] == [
61 "daq_20260901_000000",
62 "daq_sim_20260827_204900",
63 "daq_20260811_120000",
64 ]
65
66
68 # RUN_RE also guards the {run_id} path params in main.py.
69 for bad in ("../etc", "daq_20260811_120000/..", "daq_sim_20260827_204900/x", ""):
70 assert config.RUN_RE.match(bad) is None
elodin_dir(tmp_path, monkeypatch)
Definition test_runs.py:12
test_lists_real_and_simulated_runs(elodin_dir)
Definition test_runs.py:19
test_ignores_ad_hoc_and_hand_named_dirs(elodin_dir)
Definition test_runs.py:33
test_run_id_pattern_rejects_path_traversal()
Definition test_runs.py:67
test_sorted_newest_first(elodin_dir)
Definition test_runs.py:52