|
| 1 | +"""Cross-transport parity: the same Python call must build semantically |
| 2 | +equivalent vehicle instructions over the Fleet REST (cloud) path and the BLE |
| 3 | +signed-command (protobuf) path. |
| 4 | +
|
| 5 | +Response bodies legitimately differ in FORM (cloud returns a REST JSON dict, |
| 6 | +BLE returns decoded protobuf), so these tests assert on the *outbound |
| 7 | +instruction* each transport constructs from identical arguments - that is where |
| 8 | +a parameter-handling divergence between the two code paths would show up. |
| 9 | +
|
| 10 | +The cloud path is exercised through ``VehicleFleet`` with ``_request`` mocked; |
| 11 | +the BLE path through ``VehicleBluetooth`` with ``_send`` mocked (see |
| 12 | +``ble_mocked_transport``). |
| 13 | +""" |
| 14 | + |
| 15 | +from __future__ import annotations |
| 16 | + |
| 17 | +from typing import Any, cast |
| 18 | +from unittest.mock import AsyncMock, MagicMock |
| 19 | + |
| 20 | +from tesla_fleet_api.tesla.vehicle.bluetooth import VehicleBluetooth |
| 21 | +from tesla_fleet_api.tesla.vehicle.fleet import VehicleFleet |
| 22 | +from tesla_fleet_api.tesla.vehicle.proto.car_server_pb2 import Action, VehicleAction |
| 23 | +from tesla_fleet_api.tesla.vehicle.proto.universal_message_pb2 import RoutableMessage |
| 24 | + |
| 25 | +from ble_mocked_transport import ( |
| 26 | + MockedBleTransportTestCase, |
| 27 | + decrypt_sent_command, |
| 28 | + infotainment_action_ok_reply, |
| 29 | +) |
| 30 | + |
| 31 | + |
| 32 | +def _make_fleet_vehicle(vin: str) -> tuple[VehicleFleet[Any], AsyncMock]: |
| 33 | + """A ``VehicleFleet`` whose ``_request`` is mocked to capture the REST call.""" |
| 34 | + parent = MagicMock() |
| 35 | + request = AsyncMock(return_value={"response": {"result": True}}) |
| 36 | + parent._request = request # pyright: ignore[reportAttributeAccessIssue] |
| 37 | + return VehicleFleet(parent, vin), request |
| 38 | + |
| 39 | + |
| 40 | +def _sent_vehicle_action( |
| 41 | + vehicle: VehicleBluetooth[Any], send: AsyncMock |
| 42 | +) -> VehicleAction: |
| 43 | + """Decrypt the signed command the BLE transport was about to send.""" |
| 44 | + assert send.await_args is not None |
| 45 | + sent_msg = cast("RoutableMessage", send.await_args.args[0]) |
| 46 | + plaintext = decrypt_sent_command(vehicle, sent_msg) |
| 47 | + return Action.FromString(plaintext).vehicleAction |
| 48 | + |
| 49 | + |
| 50 | +class TriggerHomelinkParityTests(MockedBleTransportTestCase): |
| 51 | + """``trigger_homelink`` must carry the given coordinates on both transports. |
| 52 | +
|
| 53 | + Regression: the cloud path used a truthy ``if lat and lon`` check that |
| 54 | + silently dropped a valid ``0.0`` coordinate, while BLE (``is not None``) |
| 55 | + kept it - a genuine parameter-handling divergence. |
| 56 | + """ |
| 57 | + |
| 58 | + async def test_zero_coordinates_survive_on_both_transports(self) -> None: |
| 59 | + # Cloud (REST) |
| 60 | + cloud, request = _make_fleet_vehicle(self.VIN) |
| 61 | + await cloud.trigger_homelink(token="tok", lat=0.0, lon=0.0) |
| 62 | + assert request.await_args is not None |
| 63 | + cloud_json = request.await_args.kwargs["json"] |
| 64 | + self.assertEqual(cloud_json["lat"], 0.0) |
| 65 | + self.assertEqual(cloud_json["lon"], 0.0) |
| 66 | + self.assertEqual(cloud_json["token"], "tok") |
| 67 | + |
| 68 | + # BLE (signed protobuf) |
| 69 | + ble, send = self.make_vehicle() |
| 70 | + send.return_value = infotainment_action_ok_reply() |
| 71 | + await ble.trigger_homelink(token="tok", lat=0.0, lon=0.0) |
| 72 | + action = _sent_vehicle_action(ble, send).vehicleControlTriggerHomelinkAction |
| 73 | + self.assertEqual(action.location.latitude, 0.0) |
| 74 | + self.assertEqual(action.location.longitude, 0.0) |
| 75 | + self.assertEqual(action.token, "tok") |
| 76 | + |
| 77 | + async def test_omitted_coordinates_absent_on_both_transports(self) -> None: |
| 78 | + cloud, request = _make_fleet_vehicle(self.VIN) |
| 79 | + await cloud.trigger_homelink(token="tok") |
| 80 | + assert request.await_args is not None |
| 81 | + cloud_json = request.await_args.kwargs["json"] |
| 82 | + self.assertNotIn("lat", cloud_json) |
| 83 | + self.assertNotIn("lon", cloud_json) |
| 84 | + |
| 85 | + ble, send = self.make_vehicle() |
| 86 | + send.return_value = infotainment_action_ok_reply() |
| 87 | + await ble.trigger_homelink(token="tok") |
| 88 | + action = _sent_vehicle_action(ble, send).vehicleControlTriggerHomelinkAction |
| 89 | + # No location submessage set when coordinates are omitted. |
| 90 | + self.assertFalse(action.HasField("location")) |
| 91 | + |
| 92 | + |
| 93 | +class AdjustVolumeParityTests(MockedBleTransportTestCase): |
| 94 | + """``adjust_volume`` must reject the same out-of-range values on both |
| 95 | + transports. |
| 96 | +
|
| 97 | + Regression: the cloud path validated ``0.0 <= volume <= 11.0`` and raised |
| 98 | + ``ValueError``; the BLE path forwarded any float to the car unchecked - a |
| 99 | + parameter-validation divergence. |
| 100 | + """ |
| 101 | + |
| 102 | + async def test_out_of_range_rejected_on_both_transports(self) -> None: |
| 103 | + for bad in (-1.0, 11.5): |
| 104 | + cloud, request = _make_fleet_vehicle(self.VIN) |
| 105 | + with self.assertRaises(ValueError): |
| 106 | + await cloud.adjust_volume(bad) |
| 107 | + request.assert_not_awaited() |
| 108 | + |
| 109 | + ble, send = self.make_vehicle() |
| 110 | + with self.assertRaises(ValueError): |
| 111 | + await ble.adjust_volume(bad) |
| 112 | + send.assert_not_awaited() |
| 113 | + |
| 114 | + async def test_in_range_sends_absolute_volume_on_both_transports(self) -> None: |
| 115 | + cloud, request = _make_fleet_vehicle(self.VIN) |
| 116 | + await cloud.adjust_volume(5.0) |
| 117 | + assert request.await_args is not None |
| 118 | + self.assertEqual(request.await_args.kwargs["json"], {"volume": 5.0}) |
| 119 | + |
| 120 | + ble, send = self.make_vehicle() |
| 121 | + send.return_value = infotainment_action_ok_reply() |
| 122 | + await ble.adjust_volume(5.0) |
| 123 | + action = _sent_vehicle_action(ble, send) |
| 124 | + self.assertAlmostEqual(action.mediaUpdateVolume.volume_absolute_float, 5.0) |
0 commit comments