|
| 1 | +# pyrefly: ignore-errors |
| 2 | +# Pydantic dynamic-attribute noise: validate_props("QuickReplies", ...) returns |
| 3 | +# _CatalogBase per its signature, so concrete attrs (.items) read as |
| 4 | +# missing-attribute; and the optional ``action`` field is Optional[ActionUnion], |
| 5 | +# so pyrefly can't narrow `.type` / `.target` after a runtime assert. The |
| 6 | +# intentional invalid-literal case (target="popup") also trips bad-argument-type. |
| 7 | +# All resolve fine at runtime; same suppression as test_tile_validation.py. |
| 8 | +"""Tests for redirect (``open_url``) support on quick-reply pills. |
| 9 | +
|
| 10 | +Quick replies historically only sent ``value`` back to the agent. A pill |
| 11 | +can now optionally carry an ``action`` — an ``open_url`` action makes it |
| 12 | +redirect instead of messaging. These tests pin both the new behavior and |
| 13 | +backward compatibility (no ``action`` => unchanged send-to-agent path). |
| 14 | +""" |
| 15 | + |
| 16 | +from __future__ import annotations |
| 17 | + |
| 18 | +from types import SimpleNamespace |
| 19 | + |
| 20 | +import pytest |
| 21 | +from pydantic import ValidationError |
| 22 | + |
| 23 | +from app.ai.voice.agents.breeze_buddy.template.types import QuickReplyOption |
| 24 | + |
| 25 | +# Load the template package (ui_catalog) before any chat/* module — same |
| 26 | +# circular-import precaution as the sibling ui_prompt / ui_stream tests. |
| 27 | +from app.ai.voice.agents.breeze_buddy.template.ui_catalog import ( |
| 28 | + OpenUrlAction, |
| 29 | + QuickReplyItem, |
| 30 | + validate_props, |
| 31 | +) |
| 32 | +from app.api.routers.breeze_buddy.widget.handlers import _extract_widget_config |
| 33 | +from app.schemas.breeze_buddy.chat import QuickReplyWire |
| 34 | + |
| 35 | +# --------------------------------------------------------------------------- |
| 36 | +# OpenUrlAction.target |
| 37 | +# --------------------------------------------------------------------------- |
| 38 | + |
| 39 | + |
| 40 | +def test_open_url_defaults_to_new_tab(): |
| 41 | + """Existing open_url actions (no target) keep opening in a new tab.""" |
| 42 | + action = OpenUrlAction(url="https://shop.example/orders/1") |
| 43 | + assert action.target == "new_tab" |
| 44 | + |
| 45 | + |
| 46 | +def test_open_url_accepts_same_tab(): |
| 47 | + action = OpenUrlAction(url="https://shop.example/checkout", target="same_tab") |
| 48 | + assert action.target == "same_tab" |
| 49 | + |
| 50 | + |
| 51 | +def test_open_url_rejects_unknown_target(): |
| 52 | + with pytest.raises(ValidationError): |
| 53 | + OpenUrlAction(url="https://shop.example/x", target="popup") |
| 54 | + |
| 55 | + |
| 56 | +# --------------------------------------------------------------------------- |
| 57 | +# Dynamic QuickReplies primitive |
| 58 | +# --------------------------------------------------------------------------- |
| 59 | + |
| 60 | + |
| 61 | +def test_quick_replies_with_redirect_action_validates(): |
| 62 | + qr = validate_props( |
| 63 | + "QuickReplies", |
| 64 | + { |
| 65 | + "items": [ |
| 66 | + {"label": "Yes"}, |
| 67 | + { |
| 68 | + "label": "View order", |
| 69 | + "action": { |
| 70 | + "type": "open_url", |
| 71 | + "url": "https://shop.example/orders/1", |
| 72 | + "target": "same_tab", |
| 73 | + }, |
| 74 | + }, |
| 75 | + ] |
| 76 | + }, |
| 77 | + ) |
| 78 | + assert qr.items[0].action is None # backward-compat: message pill |
| 79 | + assert qr.items[1].action.type == "open_url" |
| 80 | + assert qr.items[1].action.target == "same_tab" |
| 81 | + |
| 82 | + |
| 83 | +def test_quick_reply_item_without_action_is_backward_compatible(): |
| 84 | + item = QuickReplyItem(label="Track", value="Track my order") |
| 85 | + assert item.action is None |
| 86 | + assert item.value == "Track my order" |
| 87 | + |
| 88 | + |
| 89 | +def test_quick_reply_item_accepts_to_assistant_action(): |
| 90 | + item = QuickReplyItem(label="x", action={"type": "to_assistant", "msg": "hi"}) |
| 91 | + assert item.action.type == "to_assistant" |
| 92 | + |
| 93 | + |
| 94 | +# --------------------------------------------------------------------------- |
| 95 | +# Static config model (QuickReplyOption) + wire (QuickReplyWire) |
| 96 | +# --------------------------------------------------------------------------- |
| 97 | + |
| 98 | + |
| 99 | +def test_static_option_redirect_needs_no_value(): |
| 100 | + opt = QuickReplyOption( |
| 101 | + label="Checkout", |
| 102 | + action={"type": "open_url", "url": "https://shop.example/checkout"}, |
| 103 | + ) |
| 104 | + assert opt.value is None |
| 105 | + assert opt.action.type == "open_url" |
| 106 | + |
| 107 | + |
| 108 | +def test_static_option_without_action_is_backward_compatible(): |
| 109 | + opt = QuickReplyOption(label="Track", value="Track my order") |
| 110 | + assert opt.action is None |
| 111 | + |
| 112 | + |
| 113 | +def test_wire_carries_action_with_null_value(): |
| 114 | + wire = QuickReplyWire( |
| 115 | + label="Checkout", |
| 116 | + value=None, |
| 117 | + action={"type": "open_url", "url": "https://shop.example/checkout"}, |
| 118 | + ) |
| 119 | + assert wire.value is None |
| 120 | + assert wire.action.target == "new_tab" |
| 121 | + |
| 122 | + |
| 123 | +# --------------------------------------------------------------------------- |
| 124 | +# Widget handler — value fallback only for message pills |
| 125 | +# --------------------------------------------------------------------------- |
| 126 | + |
| 127 | + |
| 128 | +def _template_with_quick_replies(options): |
| 129 | + configurations = SimpleNamespace(quick_replies=options, enable_text_input=True) |
| 130 | + return SimpleNamespace(configurations=configurations) |
| 131 | + |
| 132 | + |
| 133 | +def test_handler_message_pill_falls_back_to_label(): |
| 134 | + template = _template_with_quick_replies([QuickReplyOption(label="Track my order")]) |
| 135 | + replies, enable_text_input = _extract_widget_config(template) |
| 136 | + assert enable_text_input is True |
| 137 | + assert replies[0].value == "Track my order" # label fallback |
| 138 | + assert replies[0].action is None |
| 139 | + |
| 140 | + |
| 141 | +def test_handler_redirect_pill_has_null_value_and_passes_action(): |
| 142 | + template = _template_with_quick_replies( |
| 143 | + [ |
| 144 | + QuickReplyOption( |
| 145 | + label="View your order", |
| 146 | + action={"type": "open_url", "url": "https://shop.example/orders/1"}, |
| 147 | + ) |
| 148 | + ] |
| 149 | + ) |
| 150 | + replies, _ = _extract_widget_config(template) |
| 151 | + # Redirect pills don't message the agent — no label fallback. |
| 152 | + assert replies[0].value is None |
| 153 | + assert replies[0].action.type == "open_url" |
| 154 | + |
| 155 | + |
| 156 | +def test_handler_no_configurations_returns_defaults(): |
| 157 | + assert _extract_widget_config(SimpleNamespace(configurations=None)) == ([], True) |
0 commit comments