Skip to content

Commit a05b447

Browse files
committed
Update test
1 parent 0cb99aa commit a05b447

File tree

4 files changed

+121
-113
lines changed

4 files changed

+121
-113
lines changed

tests/playwright/shiny/components/chat/inject/app.py

Lines changed: 0 additions & 84 deletions
This file was deleted.

tests/playwright/shiny/components/chat/inject/test_chat_inject.py

Lines changed: 0 additions & 29 deletions
This file was deleted.
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import asyncio
2+
3+
from shiny import reactive
4+
from shiny.express import input, render, ui
5+
6+
SLEEP_TIME = 0.75
7+
8+
ui.page_opts(title="Hello chat message streams")
9+
10+
with ui.sidebar(style="height:100%"):
11+
ui.input_action_button("basic_stream", "Add message stream")
12+
ui.input_action_button("nested_stream", "Add nested stream")
13+
14+
ui.h6("Message state:", class_="mt-auto mb-0")
15+
16+
@render.code
17+
def message_state():
18+
return str(chat.messages())
19+
20+
21+
chat = ui.Chat(id="chat")
22+
chat.ui()
23+
24+
25+
# TODO: test submitting input after adding stream
26+
@chat.on_user_submit
27+
async def _(user_input: str):
28+
await chat.append_message(f"You said: {user_input}")
29+
30+
31+
@reactive.effect
32+
@reactive.event(input.basic_stream)
33+
async def _():
34+
chunks = [
35+
"Starting stream 1 🔄...\n\n",
36+
"Progress: 0%",
37+
" Progress: 50%",
38+
" Progress: 100%",
39+
]
40+
async with chat.message_stream():
41+
for chunk in chunks:
42+
await chat.append_message_chunk(chunk)
43+
await asyncio.sleep(SLEEP_TIME)
44+
await chat.append_message_chunk("Completed stream 1 ✅", operation="replace")
45+
46+
47+
# TODO: add test here for nested .message_stream()
48+
49+
50+
@reactive.effect
51+
@reactive.event(input.nested_stream)
52+
async def _():
53+
await chat.append_message_stream(mock_stream())
54+
55+
56+
async def mock_stream():
57+
yield "Starting outer stream...\n\n"
58+
await asyncio.sleep(SLEEP_TIME)
59+
await mock_tool()
60+
await asyncio.sleep(SLEEP_TIME)
61+
yield "\n\n...outer stream complete"
62+
63+
64+
async def mock_tool():
65+
chunks = [
66+
"Starting inner stream 🔄...\n\n",
67+
"Progress: 0%",
68+
" Progress: 50%",
69+
" Progress: 100%",
70+
]
71+
for chunk in chunks:
72+
await chat.append_message_chunk(chunk, operation="replace")
73+
74+
75+
# TODO: more tests, like submitting input, etc.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from playwright.sync_api import Page, expect
2+
from utils.deploy_utils import skip_on_webkit
3+
4+
from shiny.playwright import controller
5+
from shiny.run import ShinyAppProc
6+
7+
8+
@skip_on_webkit
9+
def test_validate_chat_inject(page: Page, local_app: ShinyAppProc) -> None:
10+
page.goto(local_app.url)
11+
12+
TIMEOUT = 30 * 1000
13+
14+
chat = controller.Chat(page, "chat")
15+
expect(chat.loc).to_be_visible(timeout=TIMEOUT)
16+
17+
basic_stream = controller.InputActionButton(page, "basic_stream")
18+
expect(basic_stream.loc).to_be_visible(timeout=TIMEOUT)
19+
basic_stream.click()
20+
21+
# TODO: how to test the progress messages?
22+
chat.expect_latest_message(
23+
"Completed stream 1 ✅",
24+
timeout=TIMEOUT,
25+
)
26+
27+
chat.set_user_input("Hello")
28+
chat.send_user_input()
29+
chat.expect_latest_message("You said: Hello", timeout=TIMEOUT)
30+
31+
nested_stream = controller.InputActionButton(page, "nested_stream")
32+
expect(nested_stream.loc).to_be_visible(timeout=TIMEOUT)
33+
nested_stream.click()
34+
35+
# TODO: how to test the progress messages?
36+
chat.expect_latest_message(
37+
"Starting outer stream...\n\nCompleted inner stream ✅\n\n...outer stream complete",
38+
timeout=TIMEOUT,
39+
)
40+
41+
chat.set_user_input("Hello")
42+
chat.send_user_input()
43+
chat.expect_latest_message("You said: Hello", timeout=TIMEOUT)
44+
45+
# TODO: test message state
46+
# message_state = controller.OutputCode(page, "message_state")

0 commit comments

Comments
 (0)