This commit is contained in:
orin
2026-07-30 08:45:38 +08:00
commit 25b24ac4cd
1333 changed files with 170092 additions and 0 deletions
+224
View File
@@ -0,0 +1,224 @@
from __future__ import annotations
import tempfile
import unittest
from pathlib import Path
from unittest.mock import patch
from cognitive_agent.application.domain_runtime import ConfiguredDomainRuntime
from cognitive_agent.application.seat_service import REPOSITORY_ROOT, SeatDemoService
class ConfiguredDomainRuntimeTests(unittest.TestCase):
def test_matrix_backed_domain_frames(self) -> None:
cases = {
"climate": (
[
{
"domain": "climate",
"capability": "climate.set_temperature",
"target": "driver",
"parameters": {"temperature_c": 22},
"operation": "set",
"reason": "test",
},
{
"domain": "climate",
"capability": "climate.set_fan_level",
"target": "vehicle",
"parameters": {"level": 3},
"operation": "set",
"reason": "test",
},
],
["05 16 00 00 00 00 00 00", "04 04 00 00 00 00 00 00"],
),
"roof": (
[
{
"domain": "roof",
"capability": "roof.set_transmittance",
"target": "vehicle",
"parameters": {"transmittance_pct": 30},
"operation": "set",
"reason": "test",
},
{
"domain": "roof",
"capability": "roof.set_shade_position",
"target": "vehicle",
"parameters": {"position_pct": 60},
"operation": "set",
"reason": "test",
},
],
["13 00 1E 00", "04 00 00 00 3C"],
),
"ambient_light": (
[
{
"domain": "ambient_light",
"capability": "ambient_light.set_illumination",
"target": "vehicle",
"parameters": {
"color_hex": "#FF8040",
"brightness_pct": 60,
},
"operation": "set",
"reason": "test",
}
],
["2B FF 80 40 3C"],
),
}
for domain, (actions, expected_frames) in cases.items():
with self.subTest(domain=domain):
runtime = ConfiguredDomainRuntime(REPOSITORY_ROOT, domain)
config = runtime.build_function_config(
actions, "req-test-domain-frame", "test"
)
plan = runtime.compile(config)
report = runtime.execute(plan, config)
frames = [
item["telemetry"]["command_frame"]["data_hex"]
for item in report["spec"]["results"]
]
self.assertEqual(frames, expected_frames)
class MultiDomainServiceTests(unittest.TestCase):
def setUp(self) -> None:
self.service = SeatDemoService(REPOSITORY_ROOT)
self.service.openai.api_key = "test-key"
self.temporary_directory = tempfile.TemporaryDirectory()
self.addCleanup(self.temporary_directory.cleanup)
self.service.artifact_root = Path(self.temporary_directory.name)
def test_four_domain_scene_is_generated_executed_and_persisted(self) -> None:
model_output = {
"detected_domains": ["seat", "climate", "roof", "ambient_light"],
"requests": [
{
"domain": "seat",
"capability": "seat.set_backrest_recline",
"target_scope": "passenger",
"operation": "set",
"parameters": [{"name": "position_pct", "value": 55}],
"reason": "comfortable viewing posture",
},
{
"domain": "climate",
"capability": "climate.set_temperature",
"target_scope": "vehicle",
"operation": "set",
"parameters": [{"name": "temperature_c", "value": 24}],
"reason": "comfortable cabin",
},
{
"domain": "roof",
"capability": "roof.set_transmittance",
"target_scope": "vehicle",
"operation": "set",
"parameters": [{"name": "transmittance_pct", "value": 20}],
"reason": "reduce glare",
},
{
"domain": "ambient_light",
"capability": "ambient_light.set_illumination",
"target_scope": "vehicle",
"operation": "set",
"parameters": [
{"name": "color_hex", "value": "#FF8040"},
{"name": "brightness_pct", "value": 35},
],
"reason": "warm cinema lighting",
},
],
"ambiguities": [],
"decision_summary": ["生成四域观影目标。"],
"confidence": 0.95,
}
with patch.object(self.service.openai, "interpret", return_value=model_output):
result = self.service.process(
"进入观影状态",
"openai",
"conv-four-domain-test",
)
self.assertEqual(result["status"], "succeeded")
self.assertEqual(
set(result["function_configs"]),
{"seat", "climate", "roof", "ambient_light"},
)
self.assertEqual(len(result["scene_config"]["spec"]["function_refs"]), 4)
self.assertEqual(
{item["domain"] for item in result["execution_report"]["spec"]["results"]},
{"seat", "climate", "roof", "ambient_light"},
)
self.assertEqual(result["domain_state"]["roof"]["transmittance_pct"], 20)
self.assertEqual(
result["domain_state"]["ambient_light"]["color_hex"], "#FF8040"
)
run_path = self.service.artifact_root / result["request_id"]
self.assertTrue((run_path / "scene.yaml").is_file())
for domain in ("seat", "climate", "roof", "ambient_light"):
self.assertTrue((run_path / "functions" / f"{domain}.yaml").is_file())
self.assertTrue(
(run_path / "command-plans" / f"{domain}.yaml").is_file()
)
def test_domain_state_is_supplied_to_follow_up_model_turn(self) -> None:
first_plan = {
"detected_domains": ["roof"],
"requests": [
{
"domain": "roof",
"capability": "roof.set_transmittance",
"target_scope": "vehicle",
"operation": "set",
"parameters": [{"name": "transmittance_pct", "value": 30}],
"reason": "initial target",
}
],
"ambiguities": [],
"decision_summary": ["天幕透光率设为30%。"],
"confidence": 0.96,
}
second_plan = {
**first_plan,
"requests": [
{
**first_plan["requests"][0],
"operation": "decrease",
"parameters": [{"name": "transmittance_pct", "value": 20}],
"reason": "resolved relative to current 30 percent",
}
],
"decision_summary": ["基于当前30%继续调暗到20%。"],
}
with patch.object(
self.service.openai,
"interpret",
side_effect=[first_plan, second_plan],
) as interpret:
self.service.process(
"天幕透光率调到30%",
"openai",
"conv-roof-follow-up",
)
result = self.service.process(
"再暗一点",
"openai",
"conv-roof-follow-up",
)
second_context = interpret.call_args_list[1].args[1]
self.assertEqual(
second_context["domain_devices"]["roof"]["transmittance_pct"], 30
)
self.assertEqual(result["domain_state"]["roof"]["transmittance_pct"], 20)
self.assertEqual(result["conversation_turn"], 2)
if __name__ == "__main__":
unittest.main()