Files
Cognitive_Agent/tests/unit/test_ch1_vehicle_safety.py

97 lines
3.7 KiB
Python

from __future__ import annotations
from contextlib import nullcontext
import os
from types import SimpleNamespace
import unittest
from unittest.mock import Mock, patch
from cognitive_agent.application.seat_service import REPOSITORY_ROOT, SeatDemoService
from cognitive_agent.infrastructure.can.hardware import (
HardwareSafetyError,
VehicleCanSettings,
)
class Ch1VehicleSafetyTests(unittest.TestCase):
def settings(self, channel: int) -> VehicleCanSettings:
environment = {
"COGNITIVE_AGENT_VEHICLE_CAN_ENABLED": "true",
"COGNITIVE_AGENT_VEHICLE_CAN_CHANNEL": str(channel),
"COGNITIVE_AGENT_VEHICLE_CAN_REQUIRED_CHANNEL": "1",
"COGNITIVE_AGENT_HARDWARE_APPROVAL_TOKEN_REQUIRED": "true",
"COGNITIVE_AGENT_HARDWARE_APPROVAL_TOKEN": "approved",
}
with patch.dict(os.environ, environment, clear=False):
return VehicleCanSettings.from_env(REPOSITORY_ROOT)
def test_ch1_is_the_only_allowed_body_can_channel(self) -> None:
settings = self.settings(1)
self.assertTrue(settings.channel_is_allowed)
self.assertEqual(settings.required_channel, 1)
self.assertTrue(settings.configured)
settings.authorize("approved")
info = settings.public_info()
self.assertEqual(info["target_policy"], "ch1_body_can_driver_only")
self.assertTrue(info["channel_is_allowed"])
def test_ch0_is_rejected_before_adapter_open(self) -> None:
settings = self.settings(0)
self.assertFalse(settings.channel_is_allowed)
self.assertFalse(settings.configured)
with self.assertRaisesRegex(HardwareSafetyError, "CH1.*CH0 is forbidden"):
settings.authorize("approved")
def test_policy_cannot_be_redirected_to_ch0(self) -> None:
with patch.dict(
os.environ,
{
"COGNITIVE_AGENT_VEHICLE_CAN_ENABLED": "true",
"COGNITIVE_AGENT_VEHICLE_CAN_CHANNEL": "0",
"COGNITIVE_AGENT_VEHICLE_CAN_REQUIRED_CHANNEL": "0",
"COGNITIVE_AGENT_HARDWARE_APPROVAL_TOKEN_REQUIRED": "false",
},
clear=False,
):
settings = VehicleCanSettings.from_env(REPOSITORY_ROOT)
self.assertFalse(settings.channel_is_allowed)
with self.assertRaises(HardwareSafetyError):
settings.authorize(None)
def test_driver_only_ch1_uses_deployment_authority_without_browser_lease(self) -> None:
service = object.__new__(SeatDemoService)
service.hardware_settings = SimpleNamespace(enabled=False)
service.vehicle_can_settings = SimpleNamespace(
configured=True,
channel_is_allowed=True,
channel=1,
)
service.hardware_execution = Mock()
service.hardware_execution.reserve.return_value = nullcontext()
service.hardware_access = Mock()
service._process_once = Mock(return_value={"status": "succeeded"})
result = service.process(
"ac off",
execution_mode="hardware",
hardware_access_bypass=False,
)
self.assertEqual(result["status"], "succeeded")
self.assertTrue(service.driver_only_ch1_execution_active)
service.hardware_access.ensure_authorized.assert_not_called()
def test_browser_lease_remains_required_if_legacy_seat_path_is_enabled(self) -> None:
service = object.__new__(SeatDemoService)
service.hardware_settings = SimpleNamespace(enabled=True)
service.vehicle_can_settings = SimpleNamespace(
configured=True,
channel_is_allowed=True,
channel=1,
)
self.assertFalse(service.driver_only_ch1_execution_active)
if __name__ == "__main__":
unittest.main()