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

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:
2026-08-21 11:13:17 -04:00
co-authored by Claude Fable 5
parent bbee0d0db1
commit 77bb3729a3
32 changed files with 375 additions and 549 deletions
+7 -59
View File
@@ -3,25 +3,9 @@
Mirrors the pattern in tests/test_events_service.py.
"""
from unittest.mock import AsyncMock, MagicMock, patch
from datetime import datetime, timezone
import pytest
from tests.helpers import make_mock_session
def _fake_rulebook(id=1, owner_user_id=7, title="FabledSword family", description=""):
rb = MagicMock()
rb.id = id
rb.owner_user_id = owner_user_id
rb.title = title
rb.description = description
rb.created_at = datetime.now(timezone.utc)
rb.updated_at = datetime.now(timezone.utc)
rb.to_dict.return_value = {
"id": id, "owner_user_id": owner_user_id,
"title": title, "description": description or "",
}
return rb
from tests.helpers import fake_rule, fake_rulebook, fake_topic, make_mock_session
@pytest.mark.asyncio
@@ -39,7 +23,7 @@ async def test_create_rulebook_stores_to_db():
@pytest.mark.asyncio
async def test_list_rulebooks_returns_owned_only():
rb = _fake_rulebook(id=1)
rb = fake_rulebook(id=1)
mock_session = make_mock_session()
mock_result = MagicMock()
mock_result.scalars.return_value.all.return_value = [rb]
@@ -67,7 +51,7 @@ async def test_get_rulebook_returns_none_when_not_owner():
@pytest.mark.asyncio
async def test_update_rulebook_only_sets_provided_fields():
rb = _fake_rulebook(id=1, title="old")
rb = fake_rulebook(id=1, title="old")
mock_session = make_mock_session()
mock_result = MagicMock()
mock_result.scalar_one_or_none.return_value = rb
@@ -81,7 +65,7 @@ async def test_update_rulebook_only_sets_provided_fields():
@pytest.mark.asyncio
async def test_delete_rulebook_calls_delete():
rb = _fake_rulebook(id=1)
rb = fake_rulebook(id=1)
mock_session = make_mock_session()
mock_result = MagicMock()
mock_result.scalar_one_or_none.return_value = rb
@@ -96,22 +80,6 @@ async def test_delete_rulebook_calls_delete():
# ── Topic CRUD ───────────────────────────────────────────────────────────
def _fake_topic(id=1, rulebook_id=1, title="git-workflow", description="", order_index=0):
t = MagicMock()
t.id = id
t.rulebook_id = rulebook_id
t.title = title
t.description = description
t.order_index = order_index
t.created_at = datetime.now(timezone.utc)
t.updated_at = datetime.now(timezone.utc)
t.to_dict.return_value = {
"id": id, "rulebook_id": rulebook_id, "title": title,
"description": description or "", "order_index": order_index,
}
return t
@pytest.mark.asyncio
async def test_create_topic_requires_owned_rulebook():
"""create_topic raises ValueError if the rulebook isn't owned by user."""
@@ -130,8 +98,8 @@ async def test_create_topic_requires_owned_rulebook():
@pytest.mark.asyncio
async def test_list_topics_returns_topics_for_owned_rulebook():
rb = _fake_rulebook(id=1)
topic = _fake_topic(id=10, rulebook_id=1, title="git-workflow")
rb = fake_rulebook(id=1)
topic = fake_topic(id=10, rulebook_id=1, title="git-workflow")
# Two execute calls: ownership check, then topic select.
mock_session = make_mock_session()
@@ -151,26 +119,6 @@ async def test_list_topics_returns_topics_for_owned_rulebook():
# ── Rule CRUD ───────────────────────────────────────────────────────────
def _fake_rule(id=1, topic_id=10, title="dev is home",
statement="Work directly on dev", why="", how_to_apply=""):
r = MagicMock()
r.id = id
r.topic_id = topic_id
r.title = title
r.statement = statement
r.why = why
r.how_to_apply = how_to_apply
r.order_index = 0
r.created_at = datetime.now(timezone.utc)
r.updated_at = datetime.now(timezone.utc)
r.to_dict.return_value = {
"id": id, "topic_id": topic_id, "title": title,
"statement": statement, "why": why or "",
"how_to_apply": how_to_apply or "", "order_index": 0,
}
return r
@pytest.mark.asyncio
async def test_create_rule_requires_owned_topic():
mock_session = make_mock_session()
@@ -189,7 +137,7 @@ async def test_create_rule_requires_owned_topic():
@pytest.mark.asyncio
async def test_list_rules_filters_by_topic_id():
"""list_rules(topic_id=X) returns rules in that topic, ownership-scoped."""
rule = _fake_rule(id=1, topic_id=10)
rule = fake_rule(id=1, topic_id=10)
mock_session = make_mock_session()
mock_result = MagicMock()
mock_result.scalars.return_value.all.return_value = [rule]