Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
blakerosenthal committed May 22, 2024
1 parent fc61dbe commit 6dc91eb
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 8 deletions.
62 changes: 54 additions & 8 deletions tests/deploy/ui/test_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,39 +87,85 @@ def base_ui_url(config):


@pytest.fixture
def page(config, api_server, headed_mode):
def context(config, api_server, headed_mode):
server = ui_app(config=config, open_browser=False)

with sync_playwright() as playwright:
server.serve()
browser = playwright.chromium.launch(headless=not headed_mode)
context = browser.new_context()
page = context.new_page()

yield page
yield context

context.close()
browser.close()
pn.state.kill_all_servers()


def test_health(base_ui_url, page) -> None:
def test_health(base_ui_url, context) -> None:
page = context.new_page()
health_url = base_ui_url + "/health"
page.goto(health_url)
expect(page.get_by_role("heading", name="Ok")).to_be_visible()
response = page.goto(health_url)
assert response.ok


def test_index_with_blank_credentials(base_ui_url, page) -> None:
def test_index(base_ui_url, context, config) -> None:
# Index page, no auth
page = context.new_page()
index_url = base_ui_url
page.goto(index_url)
expect(page.get_by_role("button", name="Sign In")).to_be_visible()

# Authorize with no credentials
# page.locator('input[type="text"]').fill("hello")
# page.locator('input[type="password"]').fill("hello")
page.get_by_role("button", name="Sign In").click()

expect(page.get_by_role("button", name=" New Chat")).to_be_visible()

# expect auth token to be set
cookies = context.cookies()
assert len(cookies) == 1
cookie = cookies[0]
assert cookie.get("name") == "auth_token"
auth_token = cookie.get("value")
assert auth_token is not None

# New page button
new_chat_button = page.get_by_role("button", name=" New Chat")
expect(new_chat_button).to_be_visible()
new_chat_button.click()

document_root = config.local_root / "documents"
document_root.mkdir()
document_name = "test.txt"
document_path = document_root / document_name
with open(document_path, "w") as file:
file.write("!\n")

# File upload selector
with page.expect_file_chooser() as fc_info:
page.locator(".fileUpload").click()
file_chooser = fc_info.value
# file_chooser.set_files(document_path)
file_chooser.set_files(
files=[
{"name": "test.txt", "mimeType": "text/plain", "buffer": b"this is a test"}
]
)

# Upload file and expect to see it listed
file_list = page.locator(".fileListContainer")
expect(file_list.first).to_have_text(str(document_name))

start_chat_button = page.get_by_role("button", name="Start Conversation")
expect(start_chat_button).to_be_visible()
start_chat_button.click()

breakpoint()

# chat_box = page.get_by_placeholder("Ask a question about the")
# expect(chat_box).to_be_visible()


@pytest.mark.skip(reason="TODO: figure out best locators")
def test_new_chat(config, page) -> None:
Expand Down
40 changes: 40 additions & 0 deletions tests/deploy/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import os
import time

from fastapi.testclient import TestClient

from ragna.assistants import RagnaDemoAssistant
from ragna.core._utils import default_user


class TestAssistant(RagnaDemoAssistant):
def answer(self, prompt, sources, *, multiple_answer_chunks: bool):
# Simulate a "real" assistant through a small delay. See
# https://github.com/Quansight/ragna/pull/401#issuecomment-2095851440
# for why this is needed.
time.sleep(1e-3)
content = next(super().answer(prompt, sources))

if multiple_answer_chunks:
for chunk in content.split(" "):
yield f"{chunk} "
else:
yield content


def authenticate(client: TestClient) -> None:
username = default_user()
token = (
client.post(
"/token",
data={
"username": username,
"password": os.environ.get(
"RAGNA_DEMO_AUTHENTICATION_PASSWORD", username
),
},
)
.raise_for_status()
.json()
)
client.headers["Authorization"] = f"Bearer {token}"

0 comments on commit 6dc91eb

Please sign in to comment.