Skip to content

Commit 35c6323

Browse files
test/add test for Prompt Feature (#161)
* add test * Automatic application of license header --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent a66904d commit 35c6323

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

tests/test_common.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,3 +396,8 @@ async def execute_code(self, code, timeout=60):
396396
structured["result"] = [result_val]
397397

398398
return structured
399+
400+
@requires_session
401+
async def jupyter_cite(self, prompt, cell_indices, notebook_name=""):
402+
prompt = await self._session.get_prompt("jupyter_cite", arguments={"prompt": prompt, "cell_indices": cell_indices, "notebook_name": notebook_name}) # type: ignore
403+
return [message.content.text for message in prompt.messages]

tests/test_prompts.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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

Comments
 (0)