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 -15
View File
@@ -12,7 +12,7 @@ satisfied by dropping a record instead checks that it is still present.
"""
from __future__ import annotations
from unittest.mock import AsyncMock, MagicMock, patch
from unittest.mock import AsyncMock, patch
import pytest
@@ -20,12 +20,7 @@ from scribe.services.embeddings import (
_SUPERSESSION_PENALTY,
_apply_supersession_penalty,
)
def _note(note_id: int):
n = MagicMock()
n.id = note_id
return n
from tests.helpers import fake_note
def _stale(*ids):
@@ -37,7 +32,7 @@ def _stale(*ids):
@pytest.mark.asyncio
async def test_a_superseded_record_falls_behind_an_equal_live_one():
scored = [(0.70, _note(1)), (0.69, _note(2))] # 1 leads on raw score
scored = [(0.70, fake_note(id=1)), (0.69, fake_note(id=2))] # 1 leads on raw score
with _stale(1):
out = await _apply_supersession_penalty(scored, limit=5)
assert [int(n.id) for _s, n in out] == [2, 1]
@@ -51,7 +46,7 @@ async def test_a_strong_superseded_record_still_beats_a_weak_live_one():
answers a question nothing else answers should still surface, just behind
anything comparable that is current.
"""
scored = [(0.90, _note(1)), (0.50, _note(2))]
scored = [(0.90, fake_note(id=1)), (0.50, fake_note(id=2))]
with _stale(1):
out = await _apply_supersession_penalty(scored, limit=5)
assert [int(n.id) for _s, n in out] == [1, 2]
@@ -62,7 +57,7 @@ async def test_a_strong_superseded_record_still_beats_a_weak_live_one():
async def test_the_superseded_record_is_still_returned():
"""The whole point. A test that only checked ordering would pass just as
happily against an implementation that dropped it."""
scored = [(0.70, _note(1))]
scored = [(0.70, fake_note(id=1))]
with _stale(1):
out = await _apply_supersession_penalty(scored, limit=5)
assert [int(n.id) for _s, n in out] == [1]
@@ -73,7 +68,7 @@ async def test_the_returned_score_is_the_adjusted_one():
"""Downstream gates must see the adjusted value — the auto-inject margin
band in particular, which exists to stop near-ties dragging in neighbours
and would otherwise re-tie exactly what this just separated."""
scored = [(0.70, _note(1)), (0.68, _note(2))]
scored = [(0.70, fake_note(id=1)), (0.68, fake_note(id=2))]
with _stale(1):
out = await _apply_supersession_penalty(scored, limit=5)
by_id = {int(n.id): s for s, n in out}
@@ -83,7 +78,7 @@ async def test_the_returned_score_is_the_adjusted_one():
@pytest.mark.asyncio
async def test_nothing_superseded_leaves_the_order_untouched():
scored = [(0.70, _note(1)), (0.69, _note(2)), (0.60, _note(3))]
scored = [(0.70, fake_note(id=1)), (0.69, fake_note(id=2)), (0.60, fake_note(id=3))]
with _stale():
out = await _apply_supersession_penalty(scored, limit=5)
assert [int(n.id) for _s, n in out] == [1, 2, 3]
@@ -94,7 +89,7 @@ async def test_ties_keep_their_database_order():
"""Stable sort. Equal scores must not reshuffle per call — a menu that
reorders between identical queries reads as nondeterminism and sends
someone hunting for a bug that isn't there."""
scored = [(0.70, _note(1)), (0.70, _note(2)), (0.70, _note(3))]
scored = [(0.70, fake_note(id=1)), (0.70, fake_note(id=2)), (0.70, fake_note(id=3))]
with _stale():
out = await _apply_supersession_penalty(scored, limit=5)
assert [int(n.id) for _s, n in out] == [1, 2, 3]
@@ -104,7 +99,7 @@ async def test_ties_keep_their_database_order():
async def test_the_limit_is_applied_after_reordering():
"""Over-fetching is pointless if the cut happens first. Three candidates,
limit 2, and the demoted leader must be the one that falls out."""
scored = [(0.70, _note(1)), (0.69, _note(2)), (0.68, _note(3))]
scored = [(0.70, fake_note(id=1)), (0.69, fake_note(id=2)), (0.68, fake_note(id=3))]
with _stale(1):
out = await _apply_supersession_penalty(scored, limit=2)
assert [int(n.id) for _s, n in out] == [2, 3]
@@ -115,7 +110,7 @@ async def test_a_failed_lookup_returns_unpenalised_results_not_none():
"""Fail OPEN, and the direction matters. Ranking without the penalty is the
behaviour that shipped for months; returning nothing would turn a
supersession hiccup into a broken search."""
scored = [(0.70, _note(1)), (0.69, _note(2))]
scored = [(0.70, fake_note(id=1)), (0.69, fake_note(id=2))]
with patch("scribe.services.supersession.superseded_ids",
AsyncMock(side_effect=RuntimeError("db gone"))):
out = await _apply_supersession_penalty(scored, limit=5)