Files
StarPilot/starpilot/common/tests/test_starpilot_variables.py
T
firestar5683 9872c5fb4b button
2026-05-10 11:25:58 -05:00

94 lines
2.9 KiB
Python

from types import SimpleNamespace
from openpilot.starpilot.common import starpilot_variables as spv
def test_legacy_volt_stock_acc_models_share_sng_and_auto_hold_scope():
assert spv.LEGACY_VOLT_STOCK_ACC_CARS == {
"CHEVROLET_VOLT",
"CHEVROLET_VOLT_2019",
"CHEVROLET_VOLT_ASCM",
"CHEVROLET_VOLT_CAMERA",
}
def test_get_starpilot_toggles_uses_last_non_empty_broadcast(monkeypatch):
params = SimpleNamespace(get_bool=lambda _key: False)
monkeypatch.setattr(spv.get_starpilot_toggles, "_params", params, raising=False)
monkeypatch.delattr(spv.get_starpilot_toggles, "_last_toggles_text", raising=False)
payload = '{"always_on_lateral": true, "vision_speed_limit_detection": true}'
sm_with_toggles = {"starpilotPlan": SimpleNamespace(starpilotToggles=payload)}
sm_without_toggles = {"starpilotPlan": SimpleNamespace(starpilotToggles="")}
first = spv.get_starpilot_toggles(sm_with_toggles)
second = spv.get_starpilot_toggles(sm_without_toggles)
assert first.always_on_lateral is True
assert second.always_on_lateral is True
assert second.vision_speed_limit_detection is True
class _FakeParams:
def __init__(self, floats=None, ints=None, bools=None):
self.floats = dict(floats or {})
self.ints = dict(ints or {})
self.bools = dict(bools or {})
def get_float(self, key):
return float(self.floats.get(key, 0.0))
def get_int(self, key):
return int(self.ints.get(key, 0))
def get_bool(self, key):
return bool(self.bools.get(key, False))
def put_float(self, key, value):
self.floats[key] = float(value)
def put_int(self, key, value):
self.ints[key] = int(value)
def put_bool(self, key, value):
self.bools[key] = bool(value)
def remove(self, key):
self.floats.pop(key, None)
self.ints.pop(key, None)
self.bools.pop(key, None)
def test_sync_stock_param_does_not_stomp_existing_custom_value_when_stock_missing():
params = _FakeParams({"SteerDelay": 0.35, "SteerDelayStock": 0.0})
variables = object.__new__(spv.StarPilotVariables)
variables.params = params
variables._sync_stock_param("SteerDelay", "SteerDelayStock", 0.10)
assert params.get_float("SteerDelay") == 0.35
assert params.get_float("SteerDelayStock") == 0.10
def test_cancel_button_migration_copies_distance_actions_once():
params = _FakeParams(
ints={
"DistanceButtonControl": 8,
"LongDistanceButtonControl": 4,
"VeryLongDistanceButtonControl": 7,
},
bools={"RemapCancelToDistance": True},
)
assert spv.migrate_cancel_button_controls(params) is True
assert params.get_int("CancelButtonControl") == 8
assert params.get_int("LongCancelButtonControl") == 4
assert params.get_int("VeryLongCancelButtonControl") == 7
assert params.get_bool(spv.CANCEL_BUTTON_MIGRATION_KEY) is True
params.put_int("DistanceButtonControl", 1)
params.put_int("CancelButtonControl", 3)
assert spv.migrate_cancel_button_controls(params) is False
assert params.get_int("CancelButtonControl") == 3