"""Tests for the session list sidebar."""

from __future__ import annotations

from typing import TYPE_CHECKING

import pytest
from playwright.sync_api import expect

if TYPE_CHECKING:
    from playwright.sync_api import Page

pytestmark = pytest.mark.e2e


class TestSessionList:
    """Session list sidebar displays session metadata correctly."""

    def test_project_names_visible(self, dashboard: Page) -> None:
        """Project names from fixture data appear in the session list."""
        expect(dashboard.get_by_text("work/myapp")).to_be_visible()
        expect(dashboard.get_by_text("code/webapp")).to_be_visible()

    def test_first_prompt_shown(self, dashboard: Page) -> None:
        """First user prompt is displayed in the session item."""
        expect(dashboard.get_by_text("Help me optimize this function")).to_be_visible()

    def test_session_id_prefix_shown(self, dashboard: Page) -> None:
        """The 8-char session ID prefix is visible."""
        expect(dashboard.get_by_text("sess-aaa")).to_be_visible()
        expect(dashboard.get_by_text("sess-bbb")).to_be_visible()

    def test_message_count_shown(self, dashboard: Page) -> None:
        """Message count badge is visible for sessions with messages."""
        expect(dashboard.get_by_text("4 msgs")).to_be_visible()

    def test_session_list_refreshes_on_poll(self, dashboard: Page) -> None:
        """The HTMX poll fires and the list remains populated."""
        with dashboard.expect_response("**/sessions*"):
            dashboard.wait_for_timeout(5500)
        items = dashboard.locator("#session-list-container > div")
        assert items.count() >= 2
