|
| 1 | +import pytest |
| 2 | +from unittest.mock import AsyncMock, MagicMock |
| 3 | + |
| 4 | +from textual.app import App, ComposeResult |
| 5 | +from textual.widgets import TextArea, Label |
| 6 | + |
| 7 | +from agent_chat_cli.components.tool_permission_prompt import ToolPermissionPrompt |
| 8 | + |
| 9 | + |
| 10 | +class ToolPermissionPromptApp(App): |
| 11 | + def __init__(self): |
| 12 | + super().__init__() |
| 13 | + self.mock_actions = MagicMock() |
| 14 | + self.mock_actions.respond_to_tool_permission = AsyncMock() |
| 15 | + |
| 16 | + def compose(self) -> ComposeResult: |
| 17 | + yield ToolPermissionPrompt(actions=self.mock_actions) |
| 18 | + |
| 19 | + |
| 20 | +class TestToolPermissionPromptVisibility: |
| 21 | + @pytest.fixture |
| 22 | + def app(self): |
| 23 | + return ToolPermissionPromptApp() |
| 24 | + |
| 25 | + async def test_hidden_by_default(self, app): |
| 26 | + async with app.run_test(): |
| 27 | + prompt = app.query_one(ToolPermissionPrompt) |
| 28 | + assert prompt.display is False |
| 29 | + |
| 30 | + async def test_is_visible_true_shows_prompt(self, app): |
| 31 | + async with app.run_test(): |
| 32 | + prompt = app.query_one(ToolPermissionPrompt) |
| 33 | + prompt.is_visible = True |
| 34 | + |
| 35 | + assert prompt.display is True |
| 36 | + |
| 37 | + async def test_is_visible_clears_input_on_show(self, app): |
| 38 | + async with app.run_test(): |
| 39 | + prompt = app.query_one(ToolPermissionPrompt) |
| 40 | + input_widget = prompt.query_one("#permission-input", TextArea) |
| 41 | + input_widget.insert("leftover text") |
| 42 | + |
| 43 | + prompt.is_visible = True |
| 44 | + |
| 45 | + assert input_widget.text == "" |
| 46 | + |
| 47 | + |
| 48 | +class TestToolPermissionPromptToolDisplay: |
| 49 | + @pytest.fixture |
| 50 | + def app(self): |
| 51 | + return ToolPermissionPromptApp() |
| 52 | + |
| 53 | + async def test_displays_mcp_tool_with_server_name(self, app): |
| 54 | + async with app.run_test(): |
| 55 | + prompt = app.query_one(ToolPermissionPrompt) |
| 56 | + prompt.tool_name = "mcp__filesystem__read_file" |
| 57 | + |
| 58 | + label = prompt.query_one("#tool-display", Label) |
| 59 | + rendered = str(label.render()) |
| 60 | + |
| 61 | + assert "filesystem" in rendered |
| 62 | + assert "read_file" in rendered |
| 63 | + |
| 64 | + async def test_displays_non_mcp_tool(self, app): |
| 65 | + async with app.run_test(): |
| 66 | + prompt = app.query_one(ToolPermissionPrompt) |
| 67 | + prompt.tool_name = "bash" |
| 68 | + |
| 69 | + label = prompt.query_one("#tool-display", Label) |
| 70 | + rendered = str(label.render()) |
| 71 | + |
| 72 | + assert "bash" in rendered |
| 73 | + |
| 74 | + |
| 75 | +class TestToolPermissionPromptSubmit: |
| 76 | + @pytest.fixture |
| 77 | + def app(self): |
| 78 | + return ToolPermissionPromptApp() |
| 79 | + |
| 80 | + async def test_empty_submit_defaults_to_yes(self, app): |
| 81 | + async with app.run_test(): |
| 82 | + prompt = app.query_one(ToolPermissionPrompt) |
| 83 | + prompt.is_visible = True |
| 84 | + |
| 85 | + await prompt.action_submit() |
| 86 | + |
| 87 | + app.mock_actions.respond_to_tool_permission.assert_called_with("yes") |
| 88 | + |
| 89 | + async def test_submit_with_text(self, app): |
| 90 | + async with app.run_test(): |
| 91 | + prompt = app.query_one(ToolPermissionPrompt) |
| 92 | + prompt.is_visible = True |
| 93 | + |
| 94 | + input_widget = prompt.query_one("#permission-input", TextArea) |
| 95 | + input_widget.insert("no") |
| 96 | + |
| 97 | + await prompt.action_submit() |
| 98 | + |
| 99 | + app.mock_actions.respond_to_tool_permission.assert_called_with("no") |
| 100 | + |
| 101 | + async def test_escape_submits_no(self, app): |
| 102 | + async with app.run_test() as pilot: |
| 103 | + prompt = app.query_one(ToolPermissionPrompt) |
| 104 | + prompt.is_visible = True |
| 105 | + |
| 106 | + await pilot.press("escape") |
| 107 | + |
| 108 | + app.mock_actions.respond_to_tool_permission.assert_called_with("no") |
0 commit comments