Type Check Longitudinal Profile Fixtures

This commit is contained in:
firestar5683
2026-11-28 15:54:03 -06:00
parent 59153b404f
commit 81e00cf60d
@@ -26,7 +26,7 @@ from opendbc.car.honda.interface import CarInterface
from opendbc.car.honda.values import CAR
class SelectedProfilesTests(unittest.TestCase):
class ProfileTestCase(unittest.TestCase):
def setUp(self):
self.cp = CarInterface.get_non_essential_params(CAR.HONDA_CIVIC)
self.path = Path(tempfile.mkdtemp(prefix='selected-profiles-'))
@@ -37,6 +37,8 @@ class SelectedProfilesTests(unittest.TestCase):
return profile_document(default_personality_profiles(False), enabled=enabled,
selected_acceleration_profile=acceleration, selected_deceleration_profile=braking)
class SelectedProfilesTests(ProfileTestCase):
def test_fresh_comfort_default_and_existing_choices_are_independent(self):
fresh = profile_document(default_personality_profiles(False), enabled=False)
self.assertEqual(fresh['selectedDecelerationProfile'], 'eco')
@@ -45,6 +47,7 @@ class SelectedProfilesTests(unittest.TestCase):
for personality in range(3):
for traffic in (False, True):
selected = resolve_selected_profiles(fresh, personality, 10.0, self.cp, traffic_mode=traffic)
assert selected is not None
self.assertEqual(selected.cruise_brake_magnitude, 0.6)
for choice in ('dom_default', 'standard', 'eco', 'sport'):
document = self.document(braking=choice)
@@ -60,8 +63,11 @@ class SelectedProfilesTests(unittest.TestCase):
from openpilot.starpilot.feature_runtime import requested
self.assertTrue(requested(self.params, 'profile'))
selected = ProfileHost(self.params).sample_selected(1_000_000_000, 1, V_EGO, self.cp)
assert selected is not None
self.assertEqual(selected.cruise_brake_magnitude, 0.6)
assert selected is not None
self.assertEqual(selected.braking_style, 'eco')
assert selected is not None
self.assertIsNone(selected.acceleration_max)
ordinary, _ = self.run_planner()
comfort, cruise = self.run_planner(selected)
@@ -80,6 +86,7 @@ class SelectedProfilesTests(unittest.TestCase):
path = Path(self.params.get_param_path('LongitudinalPersonalityProfiles'))
path.write_bytes(raw)
selected = ProfileHost(self.params).sample_selected(1_000_000_000, 1, V_EGO, self.cp)
assert selected is not None
self.assertEqual(selected.cruise_brake_magnitude, magnitude)
self.assertEqual(path.read_bytes(), raw)
path.write_bytes(b'not-json')
@@ -97,12 +104,16 @@ class SelectedProfilesTests(unittest.TestCase):
self.assertIsNone(read_settings(self.params))
for personality in (log.LongitudinalPersonality.standard, log.LongitudinalPersonality.relaxed):
selected = resolve_selected_profiles(document, personality, 10.0, self.cp)
assert selected is not None
self.assertEqual(selected.acceleration_max, interpolate_category_curve('acceleration', 10.0,
{'preset': global_choice, 'curve': []}, False))
assert selected is not None
self.assertEqual(selected.cruise_brake_magnitude, 0.6 if global_choice == 'eco' else 1.2)
aggressive = resolve_selected_profiles(document, log.LongitudinalPersonality.aggressive, 10.0, self.cp)
assert aggressive is not None
self.assertEqual(aggressive.acceleration_max, interpolate_category_curve('acceleration', 10.0,
{'preset': 'sport', 'curve': []}, False))
assert aggressive is not None
self.assertEqual(aggressive.cruise_brake_magnitude, 2.0)
def test_v4_migration_preserves_dormant_custom_and_original_activation(self):
@@ -128,12 +139,17 @@ class SelectedProfilesTests(unittest.TestCase):
legacy = resolve(settings, log.LongitudinalPersonality.standard, 10.0, self.cp)
selected = resolve_selected_profiles(migrated, log.LongitudinalPersonality.standard, 10.0, self.cp, legacy=legacy)
if enabled:
assert legacy is not None
self.assertEqual(legacy.cruise_brake_magnitude, 0.8)
assert selected is not None
self.assertIsNone(selected.cruise_brake_magnitude)
assert legacy.acceleration_max is not None
else:
assert legacy is not None
self.assertIsNone(legacy.acceleration_max)
assert selected is not None
self.assertEqual(selected.cruise_brake_magnitude, 2.4)
assert selected is not None
self.assertIsNone(selected.acceleration_max)
updated = update_personality_profile(migrated['profiles'], 'standard', 'acceleration', 'selected_profile', [], False)
assert updated is not None
@@ -168,11 +184,14 @@ class SelectedProfilesTests(unittest.TestCase):
def test_explicit_default_and_traffic_inheritance_are_distinct(self):
document = self.document('sport', 'sport')
inherited = resolve_selected_profiles(document, log.LongitudinalPersonality.standard, 10.0, self.cp, traffic_mode=True)
assert inherited is not None
self.assertEqual(inherited.cruise_brake_magnitude, 2.4)
document['profiles']['traffic']['acceleration'] = {'preset': 'dom_default', 'curve': []}
document['profiles']['traffic']['braking'] = {'preset': 'dom_default', 'curve': []}
explicit = resolve_selected_profiles(document, log.LongitudinalPersonality.standard, 10.0, self.cp, traffic_mode=True)
assert explicit is not None
self.assertIsNone(explicit.acceleration_max)
assert explicit is not None
self.assertIsNone(explicit.cruise_brake_magnitude)
self.assertIsNone(resolve_selected_profiles(document, 1, 10.0, self.cp, traffic_mode=None))
@@ -185,7 +204,9 @@ class SelectedProfilesTests(unittest.TestCase):
document['selectedDecelerationProfile'] = 'sport'
self.params.put('LongitudinalPersonalityProfiles', document, block=True)
self.assertEqual(host.sample_selected(1_010_000_000, 1, 10.0, self.cp), first)
self.assertEqual(host.sample_selected(2_000_000_000, 1, 10.0, self.cp).cruise_brake_magnitude, 2.4)
refreshed = host.sample_selected(2_000_000_000, 1, 10.0, self.cp)
assert refreshed is not None
self.assertEqual(refreshed.cruise_brake_magnitude, 2.4)
invalid = json.dumps({**document, 'selectedAccelerationProfile': []})
saved_path = Path(self.params.get_param_path('LongitudinalPersonalityProfiles'))
saved_path.write_text(invalid)
@@ -271,7 +292,9 @@ class SelectedProfilesTests(unittest.TestCase):
inherited_planner, _ = self.run_planner(inherited, traffic=True)
explicit_planner, _ = self.run_planner(explicit, traffic=True)
self.assertEqual(snapshot(inherited_planner), snapshot(explicit_planner))
assert inherited is not None
self.assertEqual(inherited_planner.mpc.params[0, 1], inherited.acceleration_max)
assert inherited is not None
self.assertEqual(inherited_planner.test_cruise_profile.acceleration_max, inherited.acceleration_max)
self.assertEqual(inherited_planner.last_profile.follow_seconds, ordinary.last_profile.follow_seconds)
self.assertEqual(inherited_planner.last_profile.acceleration_jerk, ordinary.last_profile.acceleration_jerk)
@@ -307,18 +330,20 @@ if __name__ == '__main__':
unittest.main()
class GlobalPowertrainPresetTests(unittest.TestCase):
setUp = SelectedProfilesTests.setUp
document = SelectedProfilesTests.document
class GlobalPowertrainPresetTests(ProfileTestCase):
def test_inherited_ev_override_preserves_personality_custom_braking_and_traffic(self):
from openpilot.starpilot.longitudinal.profile_runtime import GlobalPowertrainPreset
document = self.document('sport', 'sport')
ev = GlobalPowertrainPreset(True, False)
base = resolve_selected_profiles(document, 1, 20., self.cp)
selected = resolve_selected_profiles(document, 1, 20., self.cp, global_powertrain=ev)
assert selected is not None
self.assertEqual(selected.acceleration_max, interpolate_category_curve('acceleration', 20., {'preset':'sport','curve':[]}, True, False))
assert base is not None
assert selected is not None
self.assertNotEqual(selected.acceleration_max, base.acceleration_max)
assert base is not None
assert selected is not None
self.assertEqual(selected.cruise_brake_magnitude, base.cruise_brake_magnitude)
for config in ({'preset':'sport','curve':[]}, {'preset':'custom','curve':[.4]*10}):
document['profiles']['standard']['acceleration'] = config
@@ -328,9 +353,13 @@ class GlobalPowertrainPresetTests(unittest.TestCase):
resolve_selected_profiles(document, 1, 20., self.cp, traffic_mode=True))
default = self.document('dom_default', 'dom_default')
fallback = resolve_selected_profiles(default, 1, 20., self.cp, global_powertrain=ev)
assert fallback is not None
self.assertEqual(fallback.acceleration_max, interpolate_category_curve('acceleration',20.,{'preset':'standard','curve':[]},True,False))
assert fallback is not None
self.assertIsNone(fallback.cruise_brake_magnitude)
self.assertIsNone(resolve_selected_profiles(default,1,20.,self.cp).acceleration_max)
unmodified = resolve_selected_profiles(default, 1, 20., self.cp)
assert unmodified is not None
self.assertIsNone(unmodified.acceleration_max)
def test_actual_saved_ev_choices_truck_priority_and_stock_denial(self):
from opendbc.car.gm.tests.test_ordinary_camera import params
@@ -370,10 +399,14 @@ class GlobalPowertrainPresetTests(unittest.TestCase):
self.params.put_bool('EVTuning', True, block=True)
self.assertEqual(host.sample_selected(1_500_000_000, 1, 20., cp), absent)
ev = host.sample_selected(2_000_000_000, 1, 20., cp)
assert ev is not None
self.assertEqual(ev.acceleration_max, interpolate_category_curve('acceleration',20.,{'preset':'standard','curve':[]},True,False))
assert absent is not None
assert ev is not None
self.assertEqual(ev.cruise_brake_magnitude, absent.cruise_brake_magnitude)
self.params.put_bool('EVTuning', False, block=True)
ice = host.sample_selected(3_000_000_000, 1, 20., cp)
assert ice is not None
self.assertEqual(ice.acceleration_max, interpolate_category_curve('acceleration',20.,{'preset':'standard','curve':[]},False,False))
self.assertEqual(Path(self.params.get_param_path('LongitudinalPersonalityProfiles')).read_bytes(), original)
self.assertIsNone(host.sample_selected(-1, 1, 20., cp))
@@ -392,7 +425,9 @@ class GlobalPowertrainPresetTests(unittest.TestCase):
self.params.put_bool('EVTuning',False,block=True)
self.assertTrue(enabled(self.params,cp,'profile',{}))
selected = ProfileHost(self.params).sample_selected(1_000_000_000,1,20.,cp)
assert selected is not None
self.assertEqual(selected.acceleration_max,interpolate_category_curve('acceleration',20.,{'preset':'standard','curve':[]},False,False))
assert selected is not None
self.assertIsNone(selected.cruise_brake_magnitude)
self.assertFalse(enabled(self.params,stock,'profile',{}))
self.assertFalse(enabled(self.params,self.cp,'profile',{}))
@@ -415,7 +450,9 @@ class GlobalPowertrainPresetTests(unittest.TestCase):
self.assertIsNone(read_global_powertrain_preset(self.params,cp))
self.assertIsNone(self.params.get('EVTuning'))
doc = self.document('sport','eco')
self.assertEqual(resolve_selected_profiles(doc,1,20.,cp).acceleration_max,
selected = resolve_selected_profiles(doc, 1, 20., cp)
assert selected is not None
self.assertEqual(selected.acceleration_max,
interpolate_category_curve('acceleration',20.,{'preset':'sport','curve':[]},True,False))
self.params.put_bool('TruckTuning',False,block=True)
self.assertIsNone(read_global_powertrain_preset(self.params,cp))