|
| 1 | +# Copyright (c) 2023-2024 Datalayer, Inc. |
| 2 | +# |
| 3 | +# BSD 3-Clause License |
| 4 | + |
| 5 | +""" |
| 6 | +Test for MCP Prompts Feature |
| 7 | +""" |
| 8 | + |
| 9 | +import os |
| 10 | + |
| 11 | +import pytest |
| 12 | + |
| 13 | +from .test_common import MCPClient, timeout_wrapper |
| 14 | + |
| 15 | +# Now, prompt feature is only available in MCP_SERVER mode. |
| 16 | +pytestmark = pytest.mark.skipif( |
| 17 | + not os.environ.get("TEST_MCP_SERVER", "false").lower() == "true", |
| 18 | + reason="Prompt feature is only available in MCP_SERVER mode now." |
| 19 | +) |
| 20 | + |
| 21 | + |
| 22 | +@pytest.mark.asyncio |
| 23 | +@timeout_wrapper(60) |
| 24 | +async def test_jupyter_cite(mcp_client_parametrized: MCPClient): |
| 25 | + """Test jupyter cite prompt feature""" |
| 26 | + async with mcp_client_parametrized: |
| 27 | + await mcp_client_parametrized.use_notebook("new", "new.ipynb") |
| 28 | + await mcp_client_parametrized.use_notebook("notebook", "notebook.ipynb") |
| 29 | + # Test prompt injection |
| 30 | + response = await mcp_client_parametrized.jupyter_cite(prompt="test prompt", cell_indices="0") |
| 31 | + assert "# Matplotlib Examples" in response[0], "Cell 0 should contain Matplotlib Examples" |
| 32 | + assert "test prompt" in response[0], "Prompt should be injected" |
| 33 | + # Test mixed cell_indices |
| 34 | + response = await mcp_client_parametrized.jupyter_cite(prompt="", cell_indices="0-2,4") |
| 35 | + assert "USER Cite cells [0, 1, 2, 4]" in response[0], "Cell indices should be [0, 1, 2, 4]" |
| 36 | + assert "## 1. Import Required Libraries" in response[0], "Cell 1 should contain Import Required Libraries" |
| 37 | + assert "%matplotlib inline" in response[0], "Cell 2 should contain %matplotlib inline" |
| 38 | + assert "## 2. Basic Line Plot" not in response[0], "Cell 3 should not be cited" |
| 39 | + assert "y = np.sin(x)" in response[0], "Cell 4 should contain y = np.sin(x)" |
| 40 | + # Test cite other notebook |
| 41 | + response = await mcp_client_parametrized.jupyter_cite(prompt="", cell_indices="0", notebook_name="new") |
| 42 | + assert "from notebook new" in response[0], "should cite new notebook" |
| 43 | + assert "# A New Notebook" in response[0], "Cell 0 of new notebook should contain A New Notebook" |
0 commit comments