Newer
Older
navi-1 / tests / clients / test_tui_sessions_panel.py
"""Tests for the SessionsPanel widget and session switching."""

from __future__ import annotations

from pathlib import Path

import pytest

from clients.terminal.tui.events import SessionInfo, SessionListUpdated
from clients.terminal.tui.tui_app import NaviCodeTui


@pytest.fixture(autouse=True)
def tmp_state_dir(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path:
    """Override the state dir so tests never touch ~/.navi_code."""
    from clients.terminal import config

    original = config.settings.state_dir
    config.settings.state_dir = tmp_path
    import clients.terminal.tui.settings as settings_module

    settings_module._tui_settings = None
    yield tmp_path
    config.settings.state_dir = original
    settings_module._tui_settings = None


@pytest.mark.anyio
async def test_sessions_panel_mounts() -> None:
    """The TUI layout includes a SessionsPanel on the right."""
    async with NaviCodeTui(new_session=True).run_test() as pilot:
        await pilot.pause()
        assert pilot.app.query_one("SessionsPanel") is not None


@pytest.mark.anyio
async def test_session_list_updated_populates_table() -> None:
    """Posting SessionListUpdated fills the sessions table."""
    async with NaviCodeTui(new_session=True).run_test() as pilot:
        await pilot.pause()
        panel = pilot.app.query_one("SessionsPanel")
        sessions = [
            SessionInfo(id="sess-aaaa-1111", profile_id="navi_code", title="First", created_at=""),
            SessionInfo(id="sess-bbbb-2222", profile_id="dev", title="Second", created_at=""),
        ]
        pilot.app.post_message(SessionListUpdated(sessions, "sess-aaaa-1111"))
        await pilot.pause()
        table = panel._table
        assert table is not None
        assert table.row_count == 2
        row = table.get_row_at(0)
        assert "sess-aaaa" in str(row)
        assert "First" in str(row)


@pytest.mark.anyio
async def test_data_table_row_selects_session() -> None:
    """Selecting a row in the sessions panel switches the active session."""
    async with NaviCodeTui(new_session=True).run_test() as pilot:
        await pilot.pause()
        panel = pilot.app.query_one("SessionsPanel")
        sessions = [
            SessionInfo(id="sess-aaaa-1111", profile_id="navi_code", title="First", created_at=""),
            SessionInfo(id="sess-bbbb-2222", profile_id="dev", title="Second", created_at=""),
        ]
        pilot.app.post_message(SessionListUpdated(sessions, "sess-aaaa-1111"))
        await pilot.pause()

        table = panel._table
        table.action_cursor_down()
        await pilot.pause()
        table.action_select_cursor()
        await pilot.pause()
        assert pilot.app._ctx.session_id == "sess-bbbb-2222"