优化后端和前端

This commit is contained in:
minghanchen
2026-08-24 14:15:53 +08:00
parent 8d05815773
commit 33c92d1352
27 changed files with 2328 additions and 234 deletions
+132 -1
View File
@@ -27,9 +27,13 @@ REPOSITORY_ROOT = Path(__file__).resolve().parents[2]
class _PublishResult:
rc = 0
mid = 17
def is_published(self) -> bool:
return True
def wait_for_publish(self, timeout: float) -> bool:
return timeout > 0
raise AssertionError("control publish must never block for acknowledgement")
class _FakeMqttClient:
@@ -571,11 +575,14 @@ class MqttCanTransportTests(unittest.TestCase):
self.assertEqual(payload["data"], "10 00 00 00 00 00 00 00")
self.assertEqual(payload["repeat"], 1)
self.assertEqual(payload["intervalMs"], 0)
self.assertFalse(payload["expectResponse"])
self.assertEqual(payload["responseTimeoutMs"], 100)
publication = bus.published_messages[0]
self.assertEqual(publication["topic"], topic)
self.assertEqual(publication["qos"], qos)
self.assertEqual(publication["retain"], retain)
self.assertEqual(publication["delivery_status"], "published")
self.assertEqual(publication["message_id"], 17)
self.assertEqual(publication["payload"], payload)
self.assertTrue(
{
@@ -606,6 +613,40 @@ class MqttCanTransportTests(unittest.TestCase):
self.assertEqual(len(self.client.published), 2)
self.assertEqual(len(bus.published_messages), 2)
def test_publish_returns_queued_without_waiting_for_broker_ack(self) -> None:
class QueuedPublishResult:
rc = 0
mid = 29
@staticmethod
def is_published() -> bool:
return False
@staticmethod
def wait_for_publish(timeout: float) -> bool:
raise AssertionError(
f"wait_for_publish({timeout}) must not be called"
)
with patch.object(
self.client,
"publish",
return_value=QueuedPublishResult(),
):
publication = self.transport.publish(
CanFrame(
arbitration_id=0x431,
data=bytes.fromhex("10 00 00 00 00 00 00 00"),
channel=1,
is_fd=True,
bitrate_switch=True,
),
command="shade position 20",
)
self.assertEqual(publication["delivery_status"], "queued")
self.assertEqual(publication["message_id"], 29)
def test_can1_whitelist_publishes_rear_lock_candidate_with_debug_command(self) -> None:
bus = self.transport.bus(channel=1)
bus.set_debug_command("door rear lock")
@@ -717,6 +758,96 @@ class MqttCanTransportTests(unittest.TestCase):
)
)
def test_wireless_fixed_controls_and_shade_preset_never_wait_for_state(
self,
) -> None:
service = SeatDemoService(REPOSITORY_ROOT)
service._vehicle_mqtt_transport = self.transport
service.vehicle_mqtt_settings = SimpleNamespace(feedback_timeout_ms=1_500)
runtime = NativeCanRuntime(REPOSITORY_ROOT)
actions_by_domain = {
"climate": [
{
"domain": "climate",
"capability": "climate.set_temperature",
"target": "driver",
"operation": "set",
"parameters": {"temperature_c": 24},
},
{
"domain": "climate",
"capability": "climate.set_fan_level",
"target": "vehicle",
"operation": "set",
"parameters": {"level": 2},
},
],
"roof": [
{
"domain": "roof",
"capability": "roof.set_shade_motion",
"target": "vehicle",
"operation": "set",
"parameters": {"motion": "open"},
}
],
"ambient_light": [
{
"domain": "ambient_light",
"capability": "ambient_light.set_power",
"target": "vehicle",
"operation": "set",
"parameters": {"state": "on"},
}
],
}
configs = {
f"native_{domain}": runtime.build_function_config(
domain,
actions,
f"req-wireless-no-wait-{domain}",
"local_rules",
)
for domain, actions in actions_by_domain.items()
}
plans = {key: runtime.compile(config) for key, config in configs.items()}
with (
patch.object(
self.transport,
"_receive_after",
side_effect=AssertionError(
"wireless fixed commands must not wait on can/state"
),
) as receive_after,
patch("cognitive_agent.application.native_can_runtime.time.sleep"),
):
reports = service._execute_orchestrated_reports(
configs,
plans,
set(configs),
{},
hardware_request=True,
simulated_hardware_request=False,
simulation_fault="normal",
control_transport="wireless_mqtt",
)
receive_after.assert_not_called()
self.assertEqual(len(self.client.published), 10)
self.assertTrue(
all(
report["spec"]["status"] == "succeeded"
for report in reports.values()
)
)
self.assertTrue(
all(
json.loads(payload)["expectResponse"] is False
for _topic, payload, _qos, _retain in self.client.published
)
)
def test_model_added_nonwhitelisted_action_does_not_block_whitelisted_publish(
self,
) -> None: