Spaces:
Sleeping
Sleeping
Download backend/tests/test_safety.py from dauphinking/pump: direct link, hf CLI and curl.
- Browser
- Download file 5 kB
-
https://huggingface.co/spaces/dauphinking/pump/resolve/main/backend/tests/test_safety.py
- Command line
-
hf download hf://spaces/dauphinking/pump/backend/tests/test_safety.py
-
curl -L -o test_safety.py https://huggingface.co/spaces/dauphinking/pump/resolve/main/backend/tests/test_safety.py
5 kB
| import time | |
| from app.plant import Plant | |
| from app.safety import ControlState, SafetyGate | |
| def make_gate(): | |
| return SafetyGate(freq_min=20, freq_max=50, freq_step_per_cycle=5.0, | |
| pressure_max_mpa=0.55, min_pumps=1, watchdog_timeout_s=5.0, | |
| start_freq=20.0, level_ll_stop=3.0, level_hh=6.5, | |
| variable_speed={1: True, 2: True, 3: False, 4: False, 5: True}) | |
| def rt(pumps, pressure=0.30, level=4.75): | |
| return {"pressure": pressure, "level": level, "pumps": pumps} | |
| def test_write_enable_required(): | |
| g = make_gate() | |
| st = ControlState(mode="auto", write_enable=False) | |
| r = g.gate({"active": [1], "freqs": {1: 45}}, | |
| rt([{"id": 1, "freq": 40, "status": 1}]), st) | |
| assert r["approved"] is False | |
| assert any("写使能" in x for x in r["reasons"]) | |
| def test_freq_ramp_rate_limit(): | |
| g = make_gate() | |
| st = ControlState(mode="auto", write_enable=True) | |
| st.beat() | |
| # 目标 50,当前 40,单周期最多 +5 → 输出 45 | |
| r = g.gate({"active": [1], "freqs": {1: 50}}, | |
| rt([{"id": 1, "freq": 40, "status": 1}]), st) | |
| assert r["approved"] is True | |
| assert abs(r["command"]["freqs"][1] - 45.0) < 1e-6 | |
| assert any("限速" in x for x in r["interventions"]) | |
| def test_freq_clip_bounds(): | |
| g = make_gate() | |
| st = ControlState(mode="auto", write_enable=True) | |
| st.beat() | |
| r = g.gate({"active": [1], "freqs": {1: 999}}, | |
| rt([{"id": 1, "freq": 48, "status": 1}]), st) | |
| assert r["command"]["freqs"][1] <= 50.0 | |
| def test_high_pressure_blocks_ramp_up(): | |
| g = make_gate() | |
| st = ControlState(mode="auto", write_enable=True) | |
| st.beat() | |
| # 高压:禁止升频,维持当前 | |
| r = g.gate({"active": [1], "freqs": {1: 50}}, | |
| rt([{"id": 1, "freq": 42, "status": 1}], pressure=0.60), st) | |
| assert r["command"]["freqs"][1] <= 42.0 + 1e-6 | |
| assert any("高压" in x for x in r["interventions"]) | |
| def test_high_pressure_blocks_start(): | |
| g = make_gate() | |
| st = ControlState(mode="auto", write_enable=True) | |
| st.beat() | |
| # 高压下不允许启新泵 → 只保留已运行的 1 号 | |
| r = g.gate({"active": [1, 2], "freqs": {1: 42, 2: 40}}, | |
| rt([{"id": 1, "freq": 42, "status": 1}], pressure=0.60), st) | |
| assert 2 not in r["command"]["freqs"] | |
| def test_watchdog_timeout_reverts(): | |
| g = make_gate() | |
| st = ControlState(mode="auto", write_enable=True) | |
| st.last_heartbeat = time.time() - 10.0 # 心跳过期 | |
| r = g.gate({"active": [1], "freqs": {1: 45}}, | |
| rt([{"id": 1, "freq": 40, "status": 1}]), st) | |
| assert r["approved"] is False | |
| assert r["watchdog_ok"] is False | |
| def test_critical_alarm_skips(): | |
| g = make_gate() | |
| st = ControlState(mode="auto", write_enable=True, critical_alarm=True) | |
| st.beat() | |
| r = g.gate({"active": [1], "freqs": {1: 45}}, | |
| rt([{"id": 1, "freq": 40, "status": 1}]), st) | |
| assert r["approved"] is False | |
| assert any("CRITICAL" in x for x in r["reasons"]) | |
| def test_fixed_speed_pump_locked_50(): | |
| g = make_gate() | |
| st = ControlState(mode="auto", write_enable=True) | |
| st.beat() | |
| # 4 号是工频泵,即便建议给 30Hz 也锁 50(新启从 start_freq? 工频无软启频率→50) | |
| r = g.gate({"active": [4], "freqs": {4: 30}}, | |
| rt([{"id": 4, "freq": 50, "status": 1}]), st) | |
| assert abs(r["command"]["freqs"][4] - 50.0) < 1e-6 | |
| def test_low_level_protection_stops_pumps(): | |
| g = make_gate() | |
| st = ControlState(mode="auto", write_enable=True) | |
| st.beat() | |
| # 液位 2.5 < LL 3.0 → 低水位保护停泵 | |
| r = g.gate({"active": [1, 2], "freqs": {1: 44, 2: 44}}, | |
| rt([{"id": 1, "freq": 44, "status": 1}, {"id": 2, "freq": 44, "status": 1}], level=2.5), st) | |
| assert r["approved"] is False | |
| assert r["command"]["active"] == [] | |
| assert any("低水位" in x for x in r["reasons"]) | |
| def test_high_level_notes_but_allows(): | |
| g = make_gate() | |
| st = ControlState(mode="auto", write_enable=True) | |
| st.beat() | |
| # 液位 6.8 > HH 6.5 → 提示增泵但不阻断 | |
| r = g.gate({"active": [1], "freqs": {1: 44}}, | |
| rt([{"id": 1, "freq": 44, "status": 1}], level=6.8), st) | |
| assert r["approved"] is True | |
| assert any("高水位" in x for x in r["interventions"]) | |
| def test_power_from_current(): | |
| from app.models.curve_fit import power_from_current | |
| # 三相 394V、58A、PF 0.85 → √3·394·58·0.85/1000 ≈ 33.6 kW | |
| p = float(power_from_current(394.0, 58.0, 0.85)) | |
| assert abs(p - 33.65) < 0.5 | |
| def test_config_has_five_pumps_jiaozhou_composition(): | |
| plant = Plant() | |
| assert len(plant.pumps) == 5 | |
| meta = plant.pump_meta | |
| # 胶州二期:变频 1/2/5,工频 3/4 | |
| assert meta[1]["variable_speed"] is True | |
| assert meta[2]["variable_speed"] is True | |
| assert meta[3]["variable_speed"] is False | |
| assert meta[4]["variable_speed"] is False | |
| assert meta[5]["variable_speed"] is True | |