refactor(tests): one definition each for the copied fixtures and fakes (#2825, milestone 296 area 1)
CI & Build / Python lint (push) Successful in 3s
CI & Build / Plugin hooks (push) Successful in 8s
CI & Build / integration (push) Successful in 24s
CI & Build / TypeScript typecheck (push) Successful in 32s
CI & Build / Python tests (push) Successful in 54s
CI & Build / Build & push image (push) Successful in 18s

The shape ledger showed the same test scaffolding defined over and over:
_bind_user x12 (byte-identical), _dispose_engine x10 in three wordings,
_no_supersession x3, _make_mock_session x7 in three subsets, a get-or-create
User helper x2 (+3 inlined), and fifteen hand-rolled MagicMock note factories
each re-explaining the same "an auto-MagicMock attribute is truthy" hazard
(note 2109).

Now: conftest.py carries _bind_user / _dispose_engine / _no_supersession as
opt-in fixtures (pytestmark = usefixtures(...) per module, so unit tests pay
nothing), and tests/helpers.py carries make_mock_session(), ensure_user() and
fake_note(**attrs) — the hazard documented once, real values on every
attribute the product reads. Call sites were rewritten by AST so titles with
dashes and commas survived; the three SimpleNamespace _note stand-ins that
only feed a single function stay local.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
2026-08-21 11:03:48 -04:00
co-authored by Claude Fable 5
parent dad56a51bd
commit bbee0d0db1
38 changed files with 316 additions and 609 deletions
+10 -27
View File
@@ -2,27 +2,10 @@
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from scribe.mcp._context import _user_id_ctx
from tests.helpers import fake_note
@pytest.fixture(autouse=True)
def _bind_user():
token = _user_id_ctx.set(7)
yield
_user_id_ctx.reset(token)
def _fake_note(id=1, title="Drift Audit", note_type="process", user_id=7):
n = MagicMock()
n.id = id
n.title = title
n.note_type = note_type
# Must be a real int, not an auto-attribute: the provenance check compares it
# against the bound caller (7) to decide whether the record is shared.
n.user_id = user_id
n.to_dict.return_value = {"id": id, "title": title, "note_type": note_type}
return n
pytestmark = pytest.mark.usefixtures("_bind_user")
@pytest.mark.asyncio
@@ -36,7 +19,7 @@ async def test_create_process_requires_title_and_body():
@pytest.mark.asyncio
async def test_create_process_sets_note_type():
created = _fake_note()
created = fake_note(title="Drift Audit", note_type="process")
with patch("scribe.mcp.tools.processes.dedup_svc.find_duplicate_note",
AsyncMock(return_value=None)), \
patch("scribe.services.notes.create_note",
@@ -72,7 +55,7 @@ async def test_create_process_blocks_a_near_duplicate():
@pytest.mark.asyncio
async def test_create_process_force_bypasses_the_gate():
created = _fake_note()
created = fake_note(title="Drift Audit", note_type="process")
with patch("scribe.mcp.tools.processes.dedup_svc.find_duplicate_note",
AsyncMock()) as find_mock, \
patch("scribe.services.notes.create_note",
@@ -84,7 +67,7 @@ async def test_create_process_force_bypasses_the_gate():
@pytest.mark.asyncio
async def test_get_process_returns_body_and_candidates():
note = _fake_note(id=7)
note = fake_note(id=7, title="Drift Audit", note_type="process")
with patch("scribe.services.notes.resolve_process",
AsyncMock(return_value=(note, [{"id": 9, "title": "Drift Audit Notes"}]))):
from scribe.mcp.tools.processes import get_process
@@ -101,7 +84,7 @@ async def test_get_process_flags_another_users_process():
"""A shared Process must arrive labelled. get_process's contract is 'follow
the returned body', so an unlabelled one would put someone else's procedure
in charge of the session."""
note = _fake_note(id=7, user_id=9)
note = fake_note(id=7, title="Drift Audit", user_id=9, note_type="process")
with patch("scribe.services.notes.resolve_process",
AsyncMock(return_value=(note, []))), \
patch("scribe.services.access.describe_provenance",
@@ -126,7 +109,7 @@ async def test_get_process_not_found_raises():
async def test_update_process_rejects_non_process_note():
# Resolves share-aware now, so the patch target is get_note_for_user, which
# returns (note, permission).
plain = _fake_note(id=3, note_type="note")
plain = fake_note(id=3, title="Drift Audit", note_type="note")
plain.deleted_at = None
with patch("scribe.services.notes.get_note_for_user",
AsyncMock(return_value=(plain, "owner"))):
@@ -140,7 +123,7 @@ async def test_update_process_refuses_a_read_only_share_with_the_reason():
"""An editor grant lets the holder edit someone else's process; a viewer
grant must be refused, and saying "not found" about a process the caller can
open would just send them looking for a missing id."""
theirs = _fake_note(id=5, user_id=9)
theirs = fake_note(id=5, title="Drift Audit", user_id=9, note_type="process")
theirs.deleted_at = None
with patch("scribe.services.notes.get_note_for_user",
AsyncMock(return_value=(theirs, "viewer"))), \
@@ -154,7 +137,7 @@ async def test_update_process_refuses_a_read_only_share_with_the_reason():
@pytest.mark.asyncio
async def test_delete_process_trashes_it_recoverably():
proc = _fake_note(id=4)
proc = fake_note(id=4, title="Drift Audit", note_type="process")
proc.deleted_at = None
with patch("scribe.services.notes.get_note_for_user",
AsyncMock(return_value=(proc, "owner"))), \
@@ -172,7 +155,7 @@ async def test_delete_process_refuses_a_plain_note():
"""This tool is reached for by name. Letting it trash an ordinary note
because the id happened to resolve would be a destructive action taken on a
mistyped argument."""
plain = _fake_note(id=3, note_type="note")
plain = fake_note(id=3, title="Drift Audit", note_type="note")
plain.deleted_at = None
with patch("scribe.services.notes.get_note_for_user",
AsyncMock(return_value=(plain, "owner"))), \