"""Unit tests for clarify_schema surface protocol (PR5).""" from __future__ import annotations import json from opensquilla.skills.meta.clarify_schema import ( field_to_protocol, schema_to_protocol, ) from opensquilla.skills.meta.types import ClarifyField, ClarifyStepConfig def test_field_to_protocol_minimal(): f = ClarifyField(name="x", type="string", required=True, prompt="hi") payload = field_to_protocol(f) assert payload == { "name": "x", "type": "string", "required": True, "prompt": "hi", } # No default/min/max/max_chars/choices keys when unset. assert "default" not in payload assert "min" not in payload assert "max" not in payload assert "max_chars" not in payload assert "choices" not in payload def test_field_to_protocol_full(): f = ClarifyField( name="days", type="int", required=True, prompt="days", min=1, max=14, ) payload = field_to_protocol(f) assert payload == { "name": "days", "type": "int", "required": True, "prompt": "days", "min": 1, "max": 14, } def test_field_to_protocol_enum_with_default(): f = ClarifyField( name="budget", type="enum", choices=("budget", "mid", "premium"), default="mid", prompt="budget", ) payload = field_to_protocol(f) assert payload["choices"] == ["budget", "mid", "premium"] assert payload["default"] == "mid" assert payload["required"] is False def test_field_to_protocol_zh_enum_options_keep_values_but_localize_labels(): f = ClarifyField( name="language", type="enum", choices=("en", "zh", "mixed"), default="zh", prompt="输出语言", ) payload = field_to_protocol(f, language="zh") assert payload["choices"] == ["en", "zh", "mixed"] assert payload["options"] == [ {"value": "en", "label": "英文"}, {"value": "zh", "label": "中文"}, {"value": "mixed", "label": "中英混合"}, ] assert payload["default"] == "zh" def test_field_to_protocol_en_enum_options_are_english_labels(): f = ClarifyField( name="time_window", type="enum", choices=("LAST_WEEK", "LAST_MONTH", "LAST_QUARTER"), default="LAST_MONTH", prompt="Time window", ) payload = field_to_protocol(f, language="en") assert payload["choices"] == ["LAST_WEEK", "LAST_MONTH", "LAST_QUARTER"] assert payload["options"] == [ {"value": "LAST_WEEK", "label": "Last week"}, {"value": "LAST_MONTH", "label": "Last month"}, {"value": "LAST_QUARTER", "label": "Last quarter"}, ] def test_field_to_protocol_xml_escapes_prompt(): """Author-supplied prompt strings are XML-escaped so embedding the payload in HTML/XML cannot be hijacked by injecting tags.""" f = ClarifyField( name="x", type="string", required=True, prompt="", ) payload = field_to_protocol(f) assert "