refactor(tests): per-model fakes, FakeMCP and session mocks come from tests/helpers (#2825, milestone 296 area 1, batch 2)
CI & Build / Python lint (push) Successful in 4s
CI & Build / Plugin hooks (push) Successful in 8s
CI & Build / integration (push) Successful in 24s
CI & Build / TypeScript typecheck (push) Successful in 33s
CI & Build / Python tests (push) Failing after 36s
CI & Build / Build & push image (push) Skipped
CI & Build / Python lint (push) Successful in 4s
CI & Build / Plugin hooks (push) Successful in 8s
CI & Build / integration (push) Successful in 24s
CI & Build / TypeScript typecheck (push) Successful in 33s
CI & Build / Python tests (push) Failing after 36s
CI & Build / Build & push image (push) Skipped
Second pass over the tests/ ledger after bbee0d0. fake_record(**attrs) is the
one MagicMock-with-real-attributes builder (to_dict mirrors them; the
note-2109 hazard documented once); fake_note/fake_task/fake_snippet/
fake_project/fake_milestone/fake_system/fake_rulebook/fake_topic/fake_rule
carry each model's ordinary defaults on top of it, replacing 14 per-file
factories (two rulebook trios in tool-vs-service wordings, _fake_task, _fake_ms,
_fake_project, _plan_note, _fake_snippet, two _snippet adapters now one-liners
over fake_snippet). FakeMCP replaces the five closure-over-a-list registrar
fakes (+ _Recorder); loc() and design_token_stub() replace the paired _loc /
_token / _T stand-ins; every hand-built async_session mock (9 helper defs and
14 inline copies) now starts from make_mock_session(). Call sites rewritten by
AST with each file's former defaults made explicit, so behaviour is unchanged.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -8,35 +8,15 @@ from scribe.mcp.tools.tasks import (
|
||||
list_tasks, get_task, create_task,
|
||||
update_task, add_task_log,
|
||||
)
|
||||
from tests.helpers import fake_task
|
||||
|
||||
|
||||
pytestmark = pytest.mark.usefixtures("_bind_user")
|
||||
|
||||
|
||||
def _fake_task(*, parent_id: int | None = None, user_id: int = 7,
|
||||
**overrides) -> MagicMock:
|
||||
n = MagicMock()
|
||||
n.parent_id = parent_id
|
||||
base = {
|
||||
"id": 1, "title": "t", "body": "", "status": "todo",
|
||||
"priority": "none", "tags": [], "parent_id": parent_id,
|
||||
"is_task": True,
|
||||
}
|
||||
base.update(overrides)
|
||||
n.to_dict.return_value = base
|
||||
n.title = base["title"]
|
||||
n.id = base["id"]
|
||||
# Real values, not auto-attributes: get_task reads deleted_at and compares
|
||||
# user_id against the bound caller for the shared/owner marker — a MagicMock
|
||||
# is truthy on both (note 2109).
|
||||
n.user_id = user_id
|
||||
n.deleted_at = None
|
||||
return n
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_list_tasks_passes_is_task_true_and_repackages():
|
||||
rows = [_fake_task(id=1), _fake_task(id=2)]
|
||||
rows = [fake_task(id=1), fake_task(id=2)]
|
||||
mock = AsyncMock(return_value=(rows, 2))
|
||||
with patch("scribe.mcp.tools.tasks.notes_svc.list_notes", mock):
|
||||
out = await list_tasks()
|
||||
@@ -63,7 +43,7 @@ async def test_list_tasks_empty_status_means_no_filter():
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_task_with_no_parent_returns_null_parent_title():
|
||||
fake = _fake_task(id=5, title="solo", parent_id=None)
|
||||
fake = fake_task(id=5, title="solo", parent_id=None)
|
||||
with patch(
|
||||
"scribe.mcp.tools.tasks.notes_svc.get_note_for_user",
|
||||
AsyncMock(return_value=(fake, "owner")),
|
||||
@@ -77,8 +57,8 @@ async def test_get_task_with_no_parent_returns_null_parent_title():
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_task_enriches_with_parent_title():
|
||||
"""When parent_id is set, get_task fetches the parent and adds parent_title."""
|
||||
child = _fake_task(id=10, title="child", parent_id=5)
|
||||
parent = _fake_task(id=5, title="parent of 10", parent_id=None)
|
||||
child = fake_task(id=10, title="child", parent_id=5)
|
||||
parent = fake_task(id=5, title="parent of 10", parent_id=None)
|
||||
# fetched twice: once for the child, once for the parent
|
||||
mock_get = AsyncMock(side_effect=[(child, "owner"), (parent, "owner")])
|
||||
with patch("scribe.mcp.tools.tasks.notes_svc.get_note_for_user", mock_get):
|
||||
@@ -90,7 +70,7 @@ async def test_get_task_enriches_with_parent_title():
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_task_parent_missing_returns_null():
|
||||
"""If parent_id is set but the parent is gone (orphaned), parent_title is None."""
|
||||
child = _fake_task(id=10, parent_id=5)
|
||||
child = fake_task(id=10, parent_id=5)
|
||||
mock_get = AsyncMock(side_effect=[(child, "owner"), None])
|
||||
with patch("scribe.mcp.tools.tasks.notes_svc.get_note_for_user", mock_get):
|
||||
out = await get_task(task_id=10)
|
||||
@@ -101,7 +81,7 @@ async def test_get_task_parent_missing_returns_null():
|
||||
async def test_get_task_opens_a_shared_task_and_says_whose_it_is():
|
||||
"""A task in a shared project opens in the web UI, so the agent path must not
|
||||
answer "not found" for the same id — and must say it isn't the caller's."""
|
||||
theirs = _fake_task(id=5, title="Their task", user_id=9)
|
||||
theirs = fake_task(id=5, title="Their task", user_id=9)
|
||||
with patch(
|
||||
"scribe.mcp.tools.tasks.notes_svc.get_note_for_user",
|
||||
AsyncMock(return_value=(theirs, "viewer")),
|
||||
@@ -127,7 +107,7 @@ async def test_get_task_raises_when_not_found():
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_create_task_passes_status():
|
||||
fake = _fake_task()
|
||||
fake = fake_task()
|
||||
mock = AsyncMock(return_value=fake)
|
||||
with patch("scribe.mcp.tools.tasks.notes_svc.create_note", mock):
|
||||
await create_task(title="do x", status="todo")
|
||||
@@ -155,7 +135,7 @@ async def test_create_task_force_bypasses_duplicate_gate():
|
||||
find_mock = AsyncMock()
|
||||
with patch("scribe.mcp.tools.tasks.dedup_svc.find_duplicate_note", find_mock), \
|
||||
patch("scribe.mcp.tools.tasks.notes_svc.create_note",
|
||||
AsyncMock(return_value=_fake_task(id=9))):
|
||||
AsyncMock(return_value=fake_task(id=9))):
|
||||
out = await create_task(title="dup", force=True)
|
||||
assert out["id"] == 9
|
||||
find_mock.assert_not_called() # gate not even consulted
|
||||
@@ -173,7 +153,7 @@ async def test_create_task_rejects_retired_plan_kind():
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_create_task_priority_empty_becomes_none():
|
||||
fake = _fake_task()
|
||||
fake = fake_task()
|
||||
mock = AsyncMock(return_value=fake)
|
||||
with patch("scribe.mcp.tools.tasks.notes_svc.create_note", mock):
|
||||
await create_task(title="x", priority="")
|
||||
@@ -182,7 +162,7 @@ async def test_create_task_priority_empty_becomes_none():
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_create_task_zero_id_sentinels_become_none():
|
||||
fake = _fake_task()
|
||||
fake = fake_task()
|
||||
mock = AsyncMock(return_value=fake)
|
||||
with patch("scribe.mcp.tools.tasks.notes_svc.create_note", mock):
|
||||
await create_task(title="x", project_id=0, milestone_id=0, parent_id=0)
|
||||
@@ -193,7 +173,7 @@ async def test_create_task_zero_id_sentinels_become_none():
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_update_task_only_sends_non_default_fields():
|
||||
fake = _fake_task()
|
||||
fake = fake_task()
|
||||
mock = AsyncMock(return_value=fake)
|
||||
with patch("scribe.mcp.tools.tasks.notes_svc.update_note", mock):
|
||||
await update_task(task_id=1, status="done")
|
||||
@@ -205,7 +185,7 @@ async def test_update_task_only_sends_non_default_fields():
|
||||
@pytest.mark.asyncio
|
||||
async def test_update_task_empty_priority_is_omitted():
|
||||
"""Priority="" is "leave unchanged" — must not reach service as empty string."""
|
||||
fake = _fake_task()
|
||||
fake = fake_task()
|
||||
mock = AsyncMock(return_value=fake)
|
||||
with patch("scribe.mcp.tools.tasks.notes_svc.update_note", mock):
|
||||
await update_task(task_id=1, status="done", priority="")
|
||||
@@ -225,7 +205,7 @@ async def test_update_task_raises_when_not_found():
|
||||
@pytest.mark.asyncio
|
||||
async def test_update_task_milestone_zero_is_omitted():
|
||||
"""milestone_id=0 is 'leave unchanged' — must not reach the service."""
|
||||
fake = _fake_task()
|
||||
fake = fake_task()
|
||||
mock = AsyncMock(return_value=fake)
|
||||
with patch("scribe.mcp.tools.tasks.notes_svc.update_note", mock):
|
||||
await update_task(task_id=1, milestone_id=0)
|
||||
@@ -234,7 +214,7 @@ async def test_update_task_milestone_zero_is_omitted():
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_update_task_milestone_positive_is_set():
|
||||
fake = _fake_task()
|
||||
fake = fake_task()
|
||||
mock = AsyncMock(return_value=fake)
|
||||
with patch("scribe.mcp.tools.tasks.notes_svc.update_note", mock):
|
||||
await update_task(task_id=1, milestone_id=42)
|
||||
@@ -244,7 +224,7 @@ async def test_update_task_milestone_positive_is_set():
|
||||
@pytest.mark.asyncio
|
||||
async def test_update_task_milestone_negative_one_clears():
|
||||
"""milestone_id=-1 clears the milestone (sets the column NULL)."""
|
||||
fake = _fake_task()
|
||||
fake = fake_task()
|
||||
mock = AsyncMock(return_value=fake)
|
||||
with patch("scribe.mcp.tools.tasks.notes_svc.update_note", mock):
|
||||
await update_task(task_id=1, milestone_id=-1)
|
||||
@@ -254,7 +234,7 @@ async def test_update_task_milestone_negative_one_clears():
|
||||
@pytest.mark.asyncio
|
||||
async def test_update_task_clearing_project_also_clears_milestone():
|
||||
"""project_id=-1 clears the project and, with it, the milestone."""
|
||||
fake = _fake_task()
|
||||
fake = fake_task()
|
||||
mock = AsyncMock(return_value=fake)
|
||||
with patch("scribe.mcp.tools.tasks.notes_svc.update_note", mock):
|
||||
await update_task(task_id=1, project_id=-1)
|
||||
|
||||
Reference in New Issue
Block a user