feat(versions): prune_auto_pins FIFO-trims auto-pinned bucket

Auto-pinned versions live in their own bucket with MAX_AUTO_PINS=25 cap.
The scan job calls this after each note's promotions complete; the
oldest auto-pinned rows are dropped past the cap. Manual pins and
rolling rows are untouched.
This commit is contained in:
2026-05-13 13:57:09 -04:00
parent 9c0308dfee
commit bb6249e00e
2 changed files with 67 additions and 0 deletions
+38
View File
@@ -43,3 +43,41 @@ async def test_create_version_prune_sql_filters_to_unpinned():
assert any("pin_kind IS NULL" in s for s in captured_sql), (
f"Expected prune SQL to filter pin_kind IS NULL; got: {captured_sql!r}"
)
async def test_prune_auto_pins_filters_to_auto_kind():
"""prune_auto_pins must filter to pin_kind='auto' so manual pins and
rolling rows aren't touched, and must bind MAX_AUTO_PINS as the OFFSET."""
mock_session = AsyncMock()
mock_session.commit = AsyncMock()
mock_session.__aenter__ = AsyncMock(return_value=mock_session)
mock_session.__aexit__ = AsyncMock(return_value=False)
captured_sql: list[str] = []
captured_params: list[dict] = []
async def execute_capture(stmt, *args, **kwargs):
captured_sql.append(str(stmt))
try:
captured_params.append(dict(stmt.compile().params))
except Exception:
captured_params.append({})
return MagicMock()
mock_session.execute = AsyncMock(side_effect=execute_capture)
with patch(
"fabledassistant.services.version_pinning.async_session",
return_value=mock_session,
):
from fabledassistant.services.version_pinning import (
prune_auto_pins, MAX_AUTO_PINS,
)
await prune_auto_pins(user_id=1, note_id=42)
assert any("pin_kind = 'auto'" in s for s in captured_sql), (
f"expected auto-bucket filter in SQL; got: {captured_sql!r}"
)
assert any(
p.get("max_auto_pins") == MAX_AUTO_PINS for p in captured_params
), f"bound params: {captured_params!r}"