import json from store_assistant.tools import TOOL_SCHEMAS, ToolContext, ToolExecutor def run(store, name, args, ctx=None): ctx = ctx or ToolContext(chat_id=42, username="tester") raw = args if isinstance(args, str) else json.dumps(args) return json.loads(ToolExecutor(store).run(name, raw, ctx)) def test_every_schema_has_a_handler(store) -> None: executor = ToolExecutor(store) for schema in TOOL_SCHEMAS: name = schema["function"]["name"] result = json.loads(executor.run(name, "{}", ToolContext(chat_id=1))) assert "unknown tool" not in result.get("error", "") def test_unknown_tool(store) -> None: assert "unknown tool" in run(store, "drop_tables", {})["error"] def test_invalid_json_arguments(store) -> None: assert "invalid arguments" in run(store, "get_delivery_fee", "{not json")["error"] assert "invalid arguments" in run(store, "get_delivery_fee", "[1, 2]")["error"] def test_order_not_found_does_not_leak_data(store) -> None: result = run(store, "get_order_status", {"order_id": "NS-10241", "phone_last4": "1111"}) assert result["found"] is False assert "order" not in result def test_order_found(store) -> None: result = run(store, "get_order_status", {"order_id": "NS-10241", "phone_last4": "4821"}) assert result["found"] is True assert result["order"]["tracking_code"] == "TRK-77310452" def test_delivery_fee_not_covered_lists_wilayas(store) -> None: result = run(store, "get_delivery_fee", {"wilaya": "Illizi"}) assert result["covered"] is False assert "Oran" in result["covered_wilayas"] def test_human_agent_creates_ticket(store) -> None: ctx = ToolContext(chat_id=7, username="karim") result = run(store, "request_human_agent", {"reason": "wants a refund"}, ctx) assert ctx.tickets == [result["ticket_id"]] def test_arabic_text_is_not_escaped(store) -> None: raw = ToolExecutor(store).run("get_delivery_fee", json.dumps({"wilaya": "وهران"}), ToolContext(chat_id=1)) assert "\\u" not in raw