|
| 1 | +""" |
| 2 | +Playwright tests for inline visualization and fullscreen behavior. |
| 3 | +
|
| 4 | +These tests verify that: |
| 5 | +1. The visualize_query tool renders Altair charts inline in tool result cards |
| 6 | +2. The fullscreen toggle button appears on visualization tool results |
| 7 | +3. Fullscreen mode works (expand and collapse via button and Escape key) |
| 8 | +""" |
| 9 | + |
| 10 | +from __future__ import annotations |
| 11 | + |
| 12 | +from typing import TYPE_CHECKING |
| 13 | + |
| 14 | +import pytest |
| 15 | +from playwright.sync_api import expect |
| 16 | + |
| 17 | +if TYPE_CHECKING: |
| 18 | + from playwright.sync_api import Page |
| 19 | + from shinychat.playwright import ChatController |
| 20 | + |
| 21 | + |
| 22 | +class TestInlineVisualization: |
| 23 | + """Tests for inline chart rendering in tool result cards.""" |
| 24 | + |
| 25 | + @pytest.fixture(autouse=True) |
| 26 | + def setup( |
| 27 | + self, page: Page, app_10_viz: str, chat_10_viz: ChatController |
| 28 | + ) -> None: |
| 29 | + """Navigate to the viz app before each test.""" |
| 30 | + page.goto(app_10_viz) |
| 31 | + page.wait_for_selector("table", timeout=30000) |
| 32 | + self.page = page |
| 33 | + self.chat = chat_10_viz |
| 34 | + |
| 35 | + def test_app_loads_with_query_plot_tab(self) -> None: |
| 36 | + """VIZ-INIT: App with visualize_query has a Query Plot tab.""" |
| 37 | + expect(self.page.get_by_role("tab", name="Query Plot")).to_be_visible() |
| 38 | + |
| 39 | + def test_viz_tool_renders_inline_chart(self) -> None: |
| 40 | + """VIZ-INLINE: Visualization tool result contains an inline chart widget.""" |
| 41 | + self.chat.set_user_input( |
| 42 | + "Create a scatter plot of age vs fare for the titanic passengers" |
| 43 | + ) |
| 44 | + self.chat.send_user_input(method="click") |
| 45 | + |
| 46 | + # Wait for a tool result card with full-screen attribute (viz results have it) |
| 47 | + tool_card = self.page.locator("shiny-tool-result[full-screen]") |
| 48 | + expect(tool_card).to_be_visible(timeout=90000) |
| 49 | + |
| 50 | + # The card should contain a widget output (Altair chart) |
| 51 | + widget_output = tool_card.locator(".jupyter-widgets") |
| 52 | + expect(widget_output).to_be_visible(timeout=10000) |
| 53 | + |
| 54 | + def test_fullscreen_button_visible_on_viz_card(self) -> None: |
| 55 | + """VIZ-FS-BTN: Fullscreen toggle button appears on visualization cards.""" |
| 56 | + self.chat.set_user_input( |
| 57 | + "Make a bar chart showing count of passengers by class" |
| 58 | + ) |
| 59 | + self.chat.send_user_input(method="click") |
| 60 | + |
| 61 | + # Wait for viz tool result |
| 62 | + tool_card = self.page.locator("shiny-tool-result[full-screen]") |
| 63 | + expect(tool_card).to_be_visible(timeout=90000) |
| 64 | + |
| 65 | + # Fullscreen toggle should be visible |
| 66 | + fs_button = tool_card.locator(".tool-fullscreen-toggle") |
| 67 | + expect(fs_button).to_be_visible() |
| 68 | + |
| 69 | + def test_fullscreen_toggle_expands_card(self) -> None: |
| 70 | + """VIZ-FS-EXPAND: Clicking fullscreen button expands the card.""" |
| 71 | + self.chat.set_user_input( |
| 72 | + "Plot a histogram of passenger ages from the titanic data" |
| 73 | + ) |
| 74 | + self.chat.send_user_input(method="click") |
| 75 | + |
| 76 | + # Wait for viz tool result |
| 77 | + tool_result = self.page.locator("shiny-tool-result[full-screen]") |
| 78 | + expect(tool_result).to_be_visible(timeout=90000) |
| 79 | + |
| 80 | + # Click fullscreen toggle |
| 81 | + fs_button = tool_result.locator(".tool-fullscreen-toggle") |
| 82 | + fs_button.click() |
| 83 | + |
| 84 | + # The .shiny-tool-card inside should now have fullscreen attribute |
| 85 | + card = tool_result.locator(".shiny-tool-card[fullscreen]") |
| 86 | + expect(card).to_be_visible() |
| 87 | + |
| 88 | + def test_escape_closes_fullscreen(self) -> None: |
| 89 | + """VIZ-FS-ESC: Pressing Escape closes fullscreen mode.""" |
| 90 | + self.chat.set_user_input( |
| 91 | + "Create a visualization of survival rate by passenger class" |
| 92 | + ) |
| 93 | + self.chat.send_user_input(method="click") |
| 94 | + |
| 95 | + # Wait for viz tool result |
| 96 | + tool_result = self.page.locator("shiny-tool-result[full-screen]") |
| 97 | + expect(tool_result).to_be_visible(timeout=90000) |
| 98 | + |
| 99 | + # Enter fullscreen |
| 100 | + fs_button = tool_result.locator(".tool-fullscreen-toggle") |
| 101 | + fs_button.click() |
| 102 | + |
| 103 | + card = tool_result.locator(".shiny-tool-card[fullscreen]") |
| 104 | + expect(card).to_be_visible() |
| 105 | + |
| 106 | + # Press Escape |
| 107 | + self.page.keyboard.press("Escape") |
| 108 | + |
| 109 | + # Fullscreen should be removed |
| 110 | + expect(card).not_to_be_visible() |
| 111 | + |
| 112 | + def test_non_viz_tool_results_have_no_fullscreen(self) -> None: |
| 113 | + """VIZ-NO-FS: Non-visualization tool results don't have fullscreen.""" |
| 114 | + self.chat.set_user_input("Show me passengers who survived") |
| 115 | + self.chat.send_user_input(method="click") |
| 116 | + |
| 117 | + # Wait for a tool result (any) |
| 118 | + tool_result = self.page.locator("shiny-tool-result").first |
| 119 | + expect(tool_result).to_be_visible(timeout=90000) |
| 120 | + |
| 121 | + # Non-viz tool results should NOT have full-screen attribute |
| 122 | + fs_results = self.page.locator("shiny-tool-result[full-screen]") |
| 123 | + expect(fs_results).to_have_count(0) |
0 commit comments