1+ import asyncio
2+
13from io import BytesIO
24
35import pytest
68from panel .chat .interface import ChatInterface
79from panel .layout import Row , Tabs
810from panel .pane import Image
9- from panel .tests .util import wait_until
11+ from panel .tests .util import async_wait_until , wait_until
1012from panel .widgets .button import Button
1113from panel .widgets .input import FileInput , TextAreaInput , TextInput
1214
15+ ChatInterface .callback_exception = "raise"
16+
1317
1418class TestChatInterface :
1519 @pytest .fixture
@@ -88,12 +92,10 @@ def test_click_send(self, chat_interface: ChatInterface):
8892 def test_show_stop_disabled (self , chat_interface : ChatInterface ):
8993 async def callback (msg , user , instance ):
9094 yield "A"
91- send_button = chat_interface ._input_layout [1 ]
92- stop_button = chat_interface ._input_layout [2 ]
93- assert send_button .name == "Send"
94- assert stop_button .name == "Stop"
95- assert send_button .visible
96- assert not send_button .disabled
95+ send_button = instance ._buttons ["send" ]
96+ stop_button = instance ._buttons ["stop" ]
97+ wait_until (lambda : send_button .visible )
98+ wait_until (lambda : send_button .disabled ) # should be disabled while callback is running
9799 assert not stop_button .visible
98100 yield "B" # should not stream this
99101
@@ -110,12 +112,10 @@ async def callback(msg, user, instance):
110112
111113 def test_show_stop_for_async (self , chat_interface : ChatInterface ):
112114 async def callback (msg , user , instance ):
113- send_button = instance ._input_layout [1 ]
114- stop_button = instance ._input_layout [2 ]
115- assert send_button .name == "Send"
116- assert stop_button .name == "Stop"
117- assert not send_button .visible
118- assert stop_button .visible
115+ send_button = instance ._buttons ["send" ]
116+ stop_button = instance ._buttons ["stop" ]
117+ await async_wait_until (lambda : stop_button .visible )
118+ await async_wait_until (lambda : not send_button .visible )
119119
120120 chat_interface .callback = callback
121121 chat_interface .send ("Message" , respond = True )
@@ -124,12 +124,10 @@ async def callback(msg, user, instance):
124124
125125 def test_show_stop_for_sync (self , chat_interface : ChatInterface ):
126126 def callback (msg , user , instance ):
127- send_button = instance ._input_layout [1 ]
128- stop_button = instance ._input_layout [2 ]
129- assert send_button .name == "Send"
130- assert stop_button .name == "Stop"
131- assert not send_button .visible
132- assert stop_button .visible
127+ send_button = instance ._buttons ["send" ]
128+ stop_button = instance ._buttons ["stop" ]
129+ wait_until (lambda : stop_button .visible )
130+ wait_until (lambda : not send_button .visible )
133131
134132 chat_interface .callback = callback
135133 chat_interface .send ("Message" , respond = True )
@@ -138,25 +136,21 @@ def callback(msg, user, instance):
138136
139137 def test_click_stop (self , chat_interface : ChatInterface ):
140138 async def callback (msg , user , instance ):
141- send_button = instance ._input_layout [1 ]
142- stop_button = instance ._input_layout [2 ]
143- assert send_button .name == "Send"
144- assert stop_button .name == "Stop"
145- assert not send_button .visible
146- assert stop_button .visible
147- wait_until (lambda : len (instance .objects ) == 2 )
148- assert instance ._placeholder in instance .objects
139+ send_button = instance ._buttons ["send" ]
140+ stop_button = instance ._buttons ["stop" ]
141+ await async_wait_until (lambda : stop_button .visible )
142+ await async_wait_until (lambda : not send_button .visible )
149143 instance ._click_stop (None )
150- assert send_button .visible
151- assert not send_button .disabled
152- assert not stop_button .visible
153- assert instance ._placeholder not in instance .objects
154144
155145 chat_interface .callback = callback
156146 chat_interface .placeholder_threshold = 0.001
157- chat_interface .send ("Message" , respond = True )
158- send_button = chat_interface ._input_layout [1 ]
159- assert not send_button .disabled
147+ try :
148+ chat_interface .send ("Message" , respond = True )
149+ except asyncio .exceptions .CancelledError :
150+ pass
151+ wait_until (lambda : not chat_interface ._buttons ["send" ].disabled )
152+ wait_until (lambda : chat_interface ._buttons ["send" ].visible )
153+ wait_until (lambda : not chat_interface ._buttons ["stop" ].visible )
160154
161155 @pytest .mark .parametrize ("widget" , [TextInput (), TextAreaInput ()])
162156 def test_auto_send_types (self , chat_interface : ChatInterface , widget ):
0 commit comments