115 lines
5.2 KiB
Python
115 lines
5.2 KiB
Python
from __future__ import annotations
|
|
|
|
from copy import deepcopy
|
|
|
|
import pytest
|
|
from jsonschema import Draft202012Validator
|
|
|
|
from cognitive_agent.application.assistant_capabilities import AssistantCapabilityCatalog
|
|
from cognitive_agent.application.mode_profile_intent import parse_mode_profile_create
|
|
from cognitive_agent.application.scene_composer import build_scene_output_schema
|
|
from cognitive_agent.application.seat_service import SeatDemoService
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def catalog():
|
|
service = SeatDemoService()
|
|
try:
|
|
yield AssistantCapabilityCatalog(service)
|
|
finally:
|
|
service.close()
|
|
|
|
|
|
@pytest.mark.parametrize("message", (
|
|
"构建一个座椅舒适的功能模式",
|
|
"构建一个让人坐着放松的功能模式",
|
|
"可以帮我创建一个适合座椅放松的功能模式吗?",
|
|
"构建一个主驾按摩的功能模式",
|
|
"创建一个空调舒适的功能模式",
|
|
"构建一个适合看书的功能模式,阅读灯打开",
|
|
))
|
|
def test_mode_creation_is_not_claimed_by_capability_fast_answer(catalog, message):
|
|
assert parse_mode_profile_create(message) is not None
|
|
assert catalog.fast_answer(message) is None
|
|
|
|
|
|
@pytest.mark.parametrize("message", (
|
|
"支持主驾座椅按摩吗?",
|
|
"主驾与副驾座椅有哪些功能?",
|
|
"座椅通风支持哪些档位?",
|
|
"副驾座椅支持按摩吗?",
|
|
))
|
|
def test_genuine_seat_questions_remain_read_only_capability_answers(catalog, message):
|
|
assert parse_mode_profile_create(message) is None
|
|
assert catalog.fast_answer(message) is not None
|
|
|
|
|
|
@pytest.mark.parametrize("message", (
|
|
"不要构建座椅舒适模式",
|
|
"构建一个座椅舒适模式,但不要执行",
|
|
"取消创建坐着放松模式",
|
|
"如何构建一个座椅舒适模式?",
|
|
"解释构建一个座椅舒适的功能模式这句话",
|
|
))
|
|
def test_negations_and_read_only_seat_mode_requests_never_authorize_creation(message):
|
|
assert parse_mode_profile_create(message) is None
|
|
|
|
|
|
def _response(capability, target, parameters):
|
|
return {
|
|
"requests": [{"capability": capability, "target_scope": target, "parameters": parameters}],
|
|
"ambiguities": [],
|
|
"confidence": 0.95,
|
|
}
|
|
|
|
|
|
@pytest.mark.parametrize(("capability", "target", "parameters", "valid"), (
|
|
("seat.set_driver_ventilation_native", "driver", {"state": "on", "level": 2}, True),
|
|
("seat.set_driver_heating_native", "driver", {"state": "on", "level": 1}, True),
|
|
("seat.set_driver_heating_native", "front_left", {"state": "off"}, True),
|
|
("seat.set_driver_massage_native", "driver", {"action": "mode", "mode": "relax"}, True),
|
|
("seat.set_driver_massage_native", "driver", {"action": "strength", "strength": 1}, True),
|
|
("seat.set_driver_massage_native", "driver", {"action": "off"}, True),
|
|
("seat.set_driver_massage_native", "passenger", {"action": "mode", "mode": "relax"}, False),
|
|
("seat.set_driver_massage_native", "driver", {"action": "mode"}, False),
|
|
("seat.set_driver_massage_native", "driver", {"action": "mode", "strength": 1}, False),
|
|
("seat.set_driver_massage_native", "driver", {"action": "off", "mode": "relax"}, False),
|
|
("seat.set_driver_massage_native", "driver", {"action": "mode", "mode": "relax", "strength": 1}, False),
|
|
("seat.set_driver_massage_native", "driver", {"action": "mode", "mode": "unknown"}, False),
|
|
("seat.set_driver_heating_native", "passenger", {"state": "on", "level": 2}, False),
|
|
("seat.set_driver_ventilation_native", "driver", {"state": "on", "level": 3}, False),
|
|
("seat.set_driver_axis_native", "driver", {"axis": "backrest", "direction": "backward", "duration_ms": 500}, False),
|
|
("seat.set_driver_position_native", "driver", {"slot": 1}, False),
|
|
("seat.set_backrest_recline", "driver", {"position_pct": 45}, False),
|
|
))
|
|
def test_scene_schema_respects_real_driver_and_passenger_capabilities(
|
|
catalog, capability, target, parameters, valid,
|
|
):
|
|
context = catalog.scene_composition_context()
|
|
validator = Draft202012Validator(build_scene_output_schema(context))
|
|
assert validator.is_valid(_response(capability, target, parameters)) is valid
|
|
|
|
|
|
def test_scene_context_removes_driver_capabilities_when_runtime_marks_them_unavailable(catalog):
|
|
snapshot = deepcopy(catalog.snapshot())
|
|
for capability in snapshot["capabilities"]:
|
|
if capability["id"].startswith("seat.set_driver_"):
|
|
capability["assistant_callable"] = False
|
|
context = catalog.scene_composition_context(snapshot)
|
|
assert not any(item["id"].startswith("seat.set_driver_") for item in context["capabilities"])
|
|
validator = Draft202012Validator(build_scene_output_schema(context))
|
|
assert not validator.is_valid(_response(
|
|
"seat.set_driver_massage_native", "driver", {"action": "mode", "mode": "relax"},
|
|
))
|
|
|
|
|
|
def test_seat_comfort_detail_answer_uses_seat_specific_natural_language(catalog):
|
|
answer = catalog.fast_answer("座椅舒适模式会控制哪些设备?")
|
|
assert answer is not None
|
|
assert "主驾座椅通风开启低档" in answer.answer
|
|
assert "主驾座椅按摩设为放松模式" in answer.answer
|
|
assert "副驾座椅通风" in answer.answer
|
|
assert "副驾座椅靠背" in answer.answer
|
|
assert "action=" not in answer.answer
|
|
assert "主驾主驾" not in answer.answer
|