|
| 1 | +"""Tests for libtmux MCP resources.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import json |
| 6 | +import typing as t |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | +from libtmux.mcp.resources.hierarchy import register |
| 11 | + |
| 12 | +if t.TYPE_CHECKING: |
| 13 | + from libtmux.pane import Pane |
| 14 | + from libtmux.server import Server |
| 15 | + from libtmux.session import Session |
| 16 | + from libtmux.window import Window |
| 17 | + |
| 18 | + |
| 19 | +@pytest.fixture |
| 20 | +def resource_functions(mcp_server: Server) -> dict[str, t.Any]: |
| 21 | + """Register resources and return the function references. |
| 22 | +
|
| 23 | + Since resources are registered via decorators, we capture them |
| 24 | + by creating a mock FastMCP and collecting registered functions. |
| 25 | + """ |
| 26 | + functions: dict[str, t.Any] = {} |
| 27 | + |
| 28 | + class MockMCP: |
| 29 | + def resource(self, uri: str) -> t.Any: |
| 30 | + def decorator(fn: t.Any) -> t.Any: |
| 31 | + functions[uri] = fn |
| 32 | + return fn |
| 33 | + |
| 34 | + return decorator |
| 35 | + |
| 36 | + register(MockMCP()) # type: ignore[arg-type] |
| 37 | + return functions |
| 38 | + |
| 39 | + |
| 40 | +def test_sessions_resource( |
| 41 | + resource_functions: dict[str, t.Any], mcp_session: Session |
| 42 | +) -> None: |
| 43 | + """tmux://sessions returns session list.""" |
| 44 | + fn = resource_functions["tmux://sessions"] |
| 45 | + result = fn() |
| 46 | + data = json.loads(result) |
| 47 | + assert isinstance(data, list) |
| 48 | + assert len(data) >= 1 |
| 49 | + |
| 50 | + |
| 51 | +def test_session_detail_resource( |
| 52 | + resource_functions: dict[str, t.Any], mcp_session: Session |
| 53 | +) -> None: |
| 54 | + """tmux://sessions/{name} returns session with windows.""" |
| 55 | + fn = resource_functions["tmux://sessions/{session_name}"] |
| 56 | + result = fn(mcp_session.session_name) |
| 57 | + data = json.loads(result) |
| 58 | + assert "session_id" in data |
| 59 | + assert "windows" in data |
| 60 | + |
| 61 | + |
| 62 | +def test_session_windows_resource( |
| 63 | + resource_functions: dict[str, t.Any], mcp_session: Session |
| 64 | +) -> None: |
| 65 | + """tmux://sessions/{name}/windows returns window list.""" |
| 66 | + fn = resource_functions["tmux://sessions/{session_name}/windows"] |
| 67 | + result = fn(mcp_session.session_name) |
| 68 | + data = json.loads(result) |
| 69 | + assert isinstance(data, list) |
| 70 | + |
| 71 | + |
| 72 | +def test_window_detail_resource( |
| 73 | + resource_functions: dict[str, t.Any], |
| 74 | + mcp_session: Session, |
| 75 | + mcp_window: Window, |
| 76 | +) -> None: |
| 77 | + """tmux://sessions/{name}/windows/{index} returns window with panes.""" |
| 78 | + fn = resource_functions["tmux://sessions/{session_name}/windows/{window_index}"] |
| 79 | + result = fn(mcp_session.session_name, mcp_window.window_index) |
| 80 | + data = json.loads(result) |
| 81 | + assert "window_id" in data |
| 82 | + assert "panes" in data |
| 83 | + |
| 84 | + |
| 85 | +def test_pane_detail_resource( |
| 86 | + resource_functions: dict[str, t.Any], mcp_pane: Pane |
| 87 | +) -> None: |
| 88 | + """tmux://panes/{pane_id} returns pane details.""" |
| 89 | + fn = resource_functions["tmux://panes/{pane_id}"] |
| 90 | + result = fn(mcp_pane.pane_id) |
| 91 | + data = json.loads(result) |
| 92 | + assert data["pane_id"] == mcp_pane.pane_id |
| 93 | + |
| 94 | + |
| 95 | +def test_pane_content_resource( |
| 96 | + resource_functions: dict[str, t.Any], mcp_pane: Pane |
| 97 | +) -> None: |
| 98 | + """tmux://panes/{pane_id}/content returns captured text.""" |
| 99 | + fn = resource_functions["tmux://panes/{pane_id}/content"] |
| 100 | + result = fn(mcp_pane.pane_id) |
| 101 | + assert isinstance(result, str) |
0 commit comments