Skip to content

Commit 3f6f65f

Browse files
committed
Tests
1 parent e8a3b12 commit 3f6f65f

File tree

4 files changed

+160
-0
lines changed

4 files changed

+160
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import chatlas
2+
3+
from shiny.express import ui
4+
5+
# Set some Shiny page options
6+
ui.page_opts(
7+
title="Hello Shiny Chat",
8+
fillable=True,
9+
fillable_mobile=True,
10+
)
11+
12+
# Create a chat instance
13+
init_messages = ["""Welcome!"""]
14+
chat = ui.Chat(
15+
id="chat",
16+
messages=init_messages,
17+
)
18+
19+
# Display it
20+
chat.ui()
21+
22+
# Goal: Test that chatlas is serializied and deserialized correctly.
23+
#
24+
# Use ChatOpenAI as it does not need credentials until submission to the server.
25+
# However, if we use `.set_turns()` and `.get_turns()`, a submission is never made to the server... therefore we don't need credentials.
26+
chat_client = chatlas.ChatOpenAI(turns=[]) # pyright: ignore[reportUnknownMemberType]
27+
chat.enable_bookmarking(chat_client, bookmark_store="url")
28+
29+
30+
# Define a callback to run when the user submits a message
31+
@chat.on_user_submit
32+
async def handle_user_input(user_input: str):
33+
mock_msg = f"You said to OpenAI: {user_input}"
34+
chat_client.set_turns( # pyright: ignore[reportUnknownMemberType]
35+
[
36+
*chat_client.get_turns(),
37+
chatlas.Turn(role="user", contents=user_input),
38+
chatlas.Turn(role="assistant", contents=mock_msg),
39+
]
40+
)
41+
42+
# Append a response to the chat
43+
await chat.append_message(mock_msg)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from playwright.sync_api import Page
2+
3+
from shiny.playwright.controller import Chat
4+
from shiny.run import ShinyAppProc
5+
6+
7+
def test_bookmark_chatlas(page: Page, local_app: ShinyAppProc):
8+
9+
page.goto(local_app.url)
10+
11+
assert "?" not in page.url
12+
13+
chat_controller = Chat(page, "chat")
14+
15+
chat_controller.expect_messages("Welcome!")
16+
17+
chat_controller.set_user_input("Testing")
18+
chat_controller.send_user_input()
19+
20+
chat_controller.expect_messages("Welcome!\nTesting\nYou said to OpenAI: Testing")
21+
22+
assert "?" in page.url
23+
24+
page.reload()
25+
26+
chat_controller.expect_messages("Welcome!\nTesting\nYou said to OpenAI: Testing")
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
from typing import cast
2+
3+
from shiny.express import ui
4+
from shiny.types import Jsonifiable, JsonifiableDict
5+
6+
# Set some Shiny page options
7+
ui.page_opts(
8+
title="Hello Shiny Chat",
9+
fillable=True,
10+
fillable_mobile=True,
11+
)
12+
13+
14+
# Create a chat instance
15+
init_messages = ["""Welcome!"""]
16+
chat = ui.Chat(
17+
id="chat",
18+
messages=init_messages,
19+
)
20+
21+
# Display it
22+
chat.ui()
23+
24+
25+
class RepeaterClient:
26+
"""
27+
A simple chat client repeater that echoes back the user's input.
28+
"""
29+
30+
def __init__(self, *, messages: list[str] = []):
31+
self.messages = messages
32+
33+
def append_message(self, message: str) -> str:
34+
msg = f"Repeater: {message}"
35+
self.messages.append(msg)
36+
return msg
37+
38+
async def get_state(self) -> Jsonifiable:
39+
"""
40+
Get the current state of the chat client.
41+
"""
42+
return cast(JsonifiableDict, {"messages": self.messages})
43+
44+
async def set_state(self, state: Jsonifiable) -> None:
45+
""" "
46+
Set the state of the chat client.
47+
"""
48+
assert isinstance(state, dict)
49+
assert "messages" in state
50+
assert isinstance(state["messages"], list)
51+
self.messages = state["messages"]
52+
53+
54+
chat_client = RepeaterClient(messages=init_messages)
55+
56+
chat.enable_bookmarking(chat_client, bookmark_store="url")
57+
58+
59+
# Define a callback to run when the user submits a message
60+
@chat.on_user_submit
61+
async def handle_user_input(user_input: str):
62+
63+
msg = chat_client.append_message(user_input)
64+
# Append a response to the chat
65+
await chat.append_message(msg)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from playwright.sync_api import Page
2+
3+
from shiny.playwright.controller import Chat
4+
from shiny.run import ShinyAppProc
5+
6+
7+
def test_bookmark_chatlas(page: Page, local_app: ShinyAppProc):
8+
9+
page.goto(local_app.url)
10+
11+
assert "?" not in page.url
12+
13+
chat_controller = Chat(page, "chat")
14+
15+
chat_controller.expect_messages("Welcome!")
16+
17+
chat_controller.set_user_input("Testing")
18+
chat_controller.send_user_input()
19+
20+
chat_controller.expect_messages("Welcome!\nTesting\nRepeater: Testing")
21+
22+
assert "?" in page.url
23+
24+
page.reload()
25+
26+
chat_controller.expect_messages("Welcome!\nTesting\nRepeater: Testing")

0 commit comments

Comments
 (0)