1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
"""MCP service create_task_tool unit tests."""
from collections.abc import AsyncGenerator
from datetime import datetime, timedelta
import os
from pathlib import Path
from typing import Any
from mcp import Client as MCPClient
from pygit2 import Oid, Repository, init_repository
from pygittools.tasks import Board, EMPTY_TREE_OID_HEX, get_board
from pygittools.tasks_query import TaskView, board_ref
import pytest
from pygittools.tasks import CreateTaskResult, create_task, Task
from pygittools_mcp.exceptions import BoardNotFoundError, InvalidTaskDueDateError, InvalidTaskPriorityError, InvalidTaskStatusError
from pygittools_mcp.mcp_server import mcp
from pygittools_mcp.models import CreateTaskToolResult
TAGGER: str = "alice <alice@example.com>"
@pytest.fixture
def repo(tmp_path: Path) -> Repository:
"""Create a temp repo with a Tasks board so create_task_tool can find it."""
repo_path = tmp_path / "repo"
repo_path.mkdir()
repo = init_repository(str(repo_path), bare=False)
# Set up initial commit on main
import subprocess
os_repo_path = str(repo_path)
subprocess.run(
["git", "config", "user.email", "test@test.com"],
cwd=os_repo_path,
check=True,
)
subprocess.run(
["git", "config", "user.name", "Test"],
cwd=os_repo_path,
check=True,
)
with open(os.path.join(os_repo_path, "README.md"), "w") as f:
f.write("# initial\n")
subprocess.run(["git", "add", "README.md"], cwd=os_repo_path, check=True)
subprocess.run(
["git", "commit", "-m", "initial"],
cwd=os_repo_path,
check=True,
capture_output=True,
text=True,
)
# Create the Tasks board ref so create_task_tool.get_board() can find it
eoi = Oid(hex=EMPTY_TREE_OID_HEX)
tasks_board = Board(eoi, "refs/tags/boards/Tasks", TAGGER, description="Tasks")
tasks_board.write(repo)
return repo
@pytest.fixture
def test_task(repo: Repository) -> CreateTaskResult:
board = get_board(repo, board_ref("Tasks"))
assert board is not None
return create_task(repo, board, title="Test Task")
@pytest.fixture
async def mcp_client(repo: Repository, monkeypatch: pytest.MonkeyPatch) -> AsyncGenerator[MCPClient, None]:
monkeypatch.setenv("PYGITTOOLS_REPO", repo.path)
async with MCPClient(mcp) as client:
yield client
def assert_default_board(board: dict[str, Any]):
assert board["name"] == "Tasks"
assert board["description"] == "Tasks"
assert board["task_count"] == 0
def assert_is_test_task(view: TaskView, task: CreateTaskResult):
assert view.get("ref") == task["ref"]
assert view.get("title") == task["task"].title
assert view.get("description") == task["task"].description
view_status = view.get("status")
task_status = task["task"].status
assert view_status == task_status.value if task_status else None
view_priority = view.get("priority")
task_priority = task["task"].priority
assert view_priority == task_priority.value if task_priority else None
view_due_date = view.get("due_date")
task_due_date = task["task"].due_date
if view_due_date:
assert view_due_date == task_due_date.isoformat() if task_due_date else None
if (t := view.get("created_at")):
assert t == task["task"].created_at.isoformat()
if (t := view.get("updated_at")):
assert t == task["task"].updated_at.isoformat()
@pytest.mark.anyio
async def test_list_boards_tool(mcp_client: MCPClient) -> None:
result = await mcp_client.call_tool("list_boards_tool", {"project": None})
assert not result.is_error, result.content
content = result.structured_content["result"]
assert isinstance(content, list)
assert len(content) == 1
assert_default_board(content[0])
@pytest.mark.anyio
async def test_list_tasks_tool(mcp_client: MCPClient, test_task: CreateTaskResult) -> None:
result = await mcp_client.call_tool("list_tasks_tool", {})
assert not result.is_error, result.content
content = result.structured_content["result"]
assert isinstance(content, list)
assert len(content) == 1
assert_is_test_task(content[0], test_task)
@pytest.mark.anyio
async def test_get_task_tool(mcp_client: MCPClient, test_task: CreateTaskResult) -> None:
result = await mcp_client.call_tool("get_task_tool", {"task_ref": test_task["ref"]})
assert not result.is_error, result.content
assert_is_test_task(result.structured_content, test_task)
@pytest.mark.anyio
async def test_get_task_tool_not_found(mcp_client: MCPClient) -> None:
result = await mcp_client.call_tool("get_task_tool", {"task_ref": "fake-task-ref"})
assert result.is_error
@pytest.mark.anyio
async def test_create_task_tool_success(mcp_client: MCPClient) -> None:
title = "Test Task"
description = "A test task"
create_result = await mcp_client.call_tool("create_task_tool", {
"title": title,
"description":description
})
assert not create_result.is_error
task: CreateTaskToolResult = create_result.structured_content
task_ref = task["ref"]
get_result = await mcp_client.call_tool("get_task_tool", {"task_ref": task_ref})
assert not get_result.is_error
view: TaskView = get_result.structured_content
assert view.get("ref") == task["ref"]
assert view.get("title") == title
assert view.get("description") == description
@pytest.mark.anyio
async def test_create_task_invalid_board(mcp_client: MCPClient) -> None:
title = "Test Task"
description = "A test task"
board = "Fake-Board"
create_result = await mcp_client.call_tool("create_task_tool", {
"title": title,
"description":description ,
"board": board
})
assert create_result.is_error, create_result.content
assert str(BoardNotFoundError(board)) in create_result.content[0].text
@pytest.mark.anyio
async def test_create_task_invalid_status(mcp_client: MCPClient) -> None:
title = "Test Task"
description = "A test task"
status = "invalid"
create_result = await mcp_client.call_tool("create_task_tool", {
"title": title,
"description": description,
"status": status
})
assert create_result.is_error
assert str(InvalidTaskStatusError(status)) in create_result.content[0].text
@pytest.mark.anyio
async def test_create_task_invalid_priority(mcp_client: MCPClient) -> None:
title = "Test Task"
description = "A test task"
priority = "invalid"
create_result = await mcp_client.call_tool("create_task_tool", {
"title": title,
"description": description,
"priority": priority
})
assert create_result.is_error
assert str(InvalidTaskPriorityError(priority)) in create_result.content[0].text
@pytest.mark.anyio
async def test_create_task_invalid_due_date(mcp_client: MCPClient):
title = "Test Task"
description = "A test task"
due_date = "soon"
create_result = await mcp_client.call_tool("create_task_tool", {
"title": title,
"description": description,
"due_date": due_date
})
assert create_result.is_error
assert str(InvalidTaskDueDateError(due_date)) in create_result.content[0].text
@pytest.mark.anyio
async def test_create_full_task(mcp_client: MCPClient):
call_args = {
"title": "Test Task",
"description": "Test task description",
"board": "Tasks",
"status": Task.Status.IN_PROGRESS.value,
"priority": Task.Priority.HIGH.value,
"assignee": "ahoyt41",
"due_duate": (datetime.now() + timedelta(days=5)).isoformat(),
"tags": '("foo"="bar")',
"points": 3
}
create_result = await mcp_client.call_tool("create_task_tool", call_args)
assert not create_result.is_error, create_result.content
assert create_result.structured_content
task: CreateTaskToolResult = create_result.structured_content
get_result = await mcp_client.call_tool("get_task_tool", {"task_ref": task["ref"]})
assert not get_result.is_error
assert get_result.structured_content
view: TaskView = get_result.structured_content
assert view.get("assignee") == call_args["assignee"]
assert view.get("description") == call_args["description"]
assert view.get("status") == call_args["status"]
assert view.get("priority") == call_args["priority"]
assert view.get("title") == call_args["title"]
assert view.get("ref") == task["ref"]