Files
bvandeusenandClaude Opus 5.5 0f5ef0be63
CI & Build / Python lint (push) Successful in 3s
CI & Build / Plugin hooks (push) Successful in 12s
CI & Build / TypeScript typecheck (push) Successful in 56s
CI & Build / integration (push) Successful in 1m4s
CI & Build / Python tests (push) Successful in 1m55s
CI & Build / Build & push image (push) Successful in 44s
rename(547): the server is grimoire — package, image, DB defaults, backups
Step 2 of the Scribe -> Grimoire rename.

- src/scribe -> src/grimoire, and every import, patch target, module path,
  hypercorn entry point and `python -m grimoire.restore` with it; pyproject,
  uv.lock and the frontend package are named grimoire.
- Image git.fabledsword.com/bvandeusen/fabledgrimoire; CI's Postgres service
  runs as grimoire / grimoire_test.
- Compose files default the DB user, database and (prod) volume to grimoire,
  each overridable from .env, so an install created as Scribe keeps its data
  by setting POSTGRES_USER, POSTGRES_DB and PGDATA_VOLUME.
- Backups are written as grimoire-<ts>.tar with format grimoire-backup-tar.
  Archives named scribe-* with the scribe-backup-tar manifest still restore
  and are still counted at the destination (folder and S3), with tests.
- Exports, the MCP auth realm, the ASGI scope key and the persistent logger
  carry the new name.

Kept on purpose: the secret-sealing key label stays "scribe.secrets.v1",
because every stored secret was derived under it.

Plugin identity (plugin name, hooks, env vars, .scribe marker, skills) is
step 3.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 10:50:45 -04:00

289 lines
12 KiB
Python

"""Tests for task lifecycle timestamps and recurrence logic.
The note stand-ins come from `fake_note`, not a bare MagicMock. A fresh
MagicMock is truthy on every attribute, so once `update_note` reads a column
the stand-in did not set, `verify_with` reads as "this record carries a check"
and the guard refuses the write. That is the reason fake_note exists — a
stand-in has to be able to say NO.
"""
from datetime import date, datetime, timezone
from unittest.mock import AsyncMock, MagicMock, patch
from tests.helpers import fake_note, make_mock_session
# ── Timestamp side-effect tests ──────────────────────────────────────────────
async def test_update_note_sets_started_at_on_in_progress():
"""started_at is set when status transitions to in_progress."""
mock_note = fake_note()
mock_note.status = "in_progress"
mock_note.started_at = None
mock_note.recurrence_rule = None
mock_session = make_mock_session()
mock_result = MagicMock()
mock_result.scalars.return_value.first.return_value = mock_note
mock_session.execute = AsyncMock(return_value=mock_result)
mock_session.commit = AsyncMock()
mock_session.refresh = AsyncMock()
with patch("grimoire.services.notes.async_session", return_value=mock_session):
from grimoire.services.notes import update_note
await update_note(1, 1, status="in_progress")
assert mock_note.started_at is not None
async def test_update_note_sets_completed_at_on_done():
"""completed_at is set when status transitions to done."""
mock_note = fake_note()
mock_note.status = "done"
mock_note.started_at = datetime(2026, 3, 1, tzinfo=timezone.utc)
mock_note.completed_at = None
mock_note.recurrence_rule = None
mock_session = make_mock_session()
mock_result = MagicMock()
mock_result.scalars.return_value.first.return_value = mock_note
mock_session.execute = AsyncMock(return_value=mock_result)
mock_session.commit = AsyncMock()
mock_session.refresh = AsyncMock()
with patch("grimoire.services.notes.async_session", return_value=mock_session):
from grimoire.services.notes import update_note
await update_note(1, 1, status="done")
assert mock_note.completed_at is not None
async def test_update_note_clears_timestamps_on_todo():
"""started_at and completed_at are cleared when status resets to todo."""
mock_note = fake_note()
mock_note.status = "todo"
mock_note.started_at = datetime(2026, 3, 1, tzinfo=timezone.utc)
mock_note.completed_at = datetime(2026, 3, 15, tzinfo=timezone.utc)
mock_note.recurrence_rule = None
mock_session = make_mock_session()
mock_result = MagicMock()
mock_result.scalars.return_value.first.return_value = mock_note
mock_session.execute = AsyncMock(return_value=mock_result)
mock_session.commit = AsyncMock()
mock_session.refresh = AsyncMock()
with patch("grimoire.services.notes.async_session", return_value=mock_session):
from grimoire.services.notes import update_note
await update_note(1, 1, status="todo")
assert mock_note.started_at is None
assert mock_note.completed_at is None
async def test_update_note_preserves_started_at_if_already_set():
"""started_at is not overwritten on a second transition to in_progress."""
original_start = datetime(2026, 3, 1, tzinfo=timezone.utc)
mock_note = fake_note()
mock_note.status = "in_progress"
mock_note.started_at = original_start
mock_note.recurrence_rule = None
mock_session = make_mock_session()
mock_result = MagicMock()
mock_result.scalars.return_value.first.return_value = mock_note
mock_session.execute = AsyncMock(return_value=mock_result)
mock_session.commit = AsyncMock()
mock_session.refresh = AsyncMock()
with patch("grimoire.services.notes.async_session", return_value=mock_session):
from grimoire.services.notes import update_note
await update_note(1, 1, status="in_progress")
assert mock_note.started_at == original_start
# ── Recurrence rule validation ────────────────────────────────────────────────
import pytest
def test_validate_interval_rule_valid():
from grimoire.services.recurrence import validate_recurrence_rule
validate_recurrence_rule({"type": "interval", "every": 3, "unit": "month"}) # no error
def test_validate_interval_rule_bad_unit():
from grimoire.services.recurrence import validate_recurrence_rule
with pytest.raises(ValueError, match="unit"):
validate_recurrence_rule({"type": "interval", "every": 3, "unit": "fortnight"})
def test_validate_interval_rule_bad_every():
from grimoire.services.recurrence import validate_recurrence_rule
with pytest.raises(ValueError, match="every"):
validate_recurrence_rule({"type": "interval", "every": 0, "unit": "week"})
def test_validate_calendar_monthly_valid():
from grimoire.services.recurrence import validate_recurrence_rule
validate_recurrence_rule({"type": "calendar", "unit": "month", "day_of_month": 1})
def test_validate_calendar_annual_valid():
from grimoire.services.recurrence import validate_recurrence_rule
validate_recurrence_rule(
{"type": "calendar", "unit": "year", "day_of_month": 15, "month": 6}
)
def test_validate_calendar_bad_day():
from grimoire.services.recurrence import validate_recurrence_rule
with pytest.raises(ValueError, match="day_of_month"):
validate_recurrence_rule({"type": "calendar", "unit": "month", "day_of_month": 32})
def test_validate_calendar_annual_missing_month():
from grimoire.services.recurrence import validate_recurrence_rule
with pytest.raises(ValueError, match="month"):
validate_recurrence_rule(
{"type": "calendar", "unit": "year", "day_of_month": 15}
)
def test_validate_unknown_type():
from grimoire.services.recurrence import validate_recurrence_rule
with pytest.raises(ValueError, match="type"):
validate_recurrence_rule({"type": "cron", "every": 1, "unit": "day"})
# ── calculate_next_due ────────────────────────────────────────────────────────
def test_calculate_next_due_interval_days():
from grimoire.services.recurrence import calculate_next_due
rule = {"type": "interval", "every": 7, "unit": "day"}
assert calculate_next_due(rule, date(2026, 3, 27)) == date(2026, 4, 3)
def test_calculate_next_due_interval_weeks():
from grimoire.services.recurrence import calculate_next_due
rule = {"type": "interval", "every": 2, "unit": "week"}
assert calculate_next_due(rule, date(2026, 3, 27)) == date(2026, 4, 10)
def test_calculate_next_due_interval_months():
from grimoire.services.recurrence import calculate_next_due
rule = {"type": "interval", "every": 3, "unit": "month"}
assert calculate_next_due(rule, date(2026, 3, 1)) == date(2026, 6, 1)
def test_calculate_next_due_interval_months_end_of_month():
"""Month addition clamps to last day when target month is shorter."""
from grimoire.services.recurrence import calculate_next_due
rule = {"type": "interval", "every": 1, "unit": "month"}
assert calculate_next_due(rule, date(2026, 1, 31)) == date(2026, 2, 28)
def test_calculate_next_due_interval_years():
from grimoire.services.recurrence import calculate_next_due
rule = {"type": "interval", "every": 1, "unit": "year"}
assert calculate_next_due(rule, date(2026, 3, 27)) == date(2027, 3, 27)
def test_calculate_next_due_calendar_monthly_future_day():
"""If day_of_month is later this month, return that date."""
from grimoire.services.recurrence import calculate_next_due
rule = {"type": "calendar", "unit": "month", "day_of_month": 28}
assert calculate_next_due(rule, date(2026, 3, 1)) == date(2026, 3, 28)
def test_calculate_next_due_calendar_monthly_same_or_past_day():
"""If day_of_month is today or earlier, advance to next month."""
from grimoire.services.recurrence import calculate_next_due
rule = {"type": "calendar", "unit": "month", "day_of_month": 1}
assert calculate_next_due(rule, date(2026, 3, 1)) == date(2026, 4, 1)
def test_calculate_next_due_calendar_annual_future():
"""Annual date later this year is returned."""
from grimoire.services.recurrence import calculate_next_due
rule = {"type": "calendar", "unit": "year", "day_of_month": 15, "month": 6}
assert calculate_next_due(rule, date(2026, 3, 27)) == date(2026, 6, 15)
def test_calculate_next_due_calendar_annual_past():
"""Annual date already passed this year → next year."""
from grimoire.services.recurrence import calculate_next_due
rule = {"type": "calendar", "unit": "year", "day_of_month": 15, "month": 1}
assert calculate_next_due(rule, date(2026, 3, 27)) == date(2027, 1, 15)
# ── spawn_recurring_tasks ─────────────────────────────────────────────────────
async def test_spawn_recurring_tasks_creates_child():
from unittest.mock import AsyncMock, MagicMock, patch
mock_task = MagicMock()
mock_task.id = 1
mock_task.user_id = 42
mock_task.title = "Change air filter"
mock_task.body = ""
mock_task.priority = "medium"
mock_task.tags = []
mock_task.project_id = None
mock_task.milestone_id = None
mock_task.due_date = date(2026, 3, 1)
mock_task.recurrence_rule = {"type": "interval", "every": 3, "unit": "month"}
mock_session = make_mock_session()
mock_result = MagicMock()
mock_result.scalars.return_value.all.return_value = [mock_task]
mock_session.execute = AsyncMock(return_value=mock_result)
mock_session.get = AsyncMock(return_value=mock_task)
mock_session.commit = AsyncMock()
mock_child = MagicMock()
mock_child.id = 2
mock_child.title = "Change air filter"
mock_child.body = ""
with (
patch("grimoire.services.recurrence.async_session", return_value=mock_session),
patch(
"grimoire.services.notes.create_note",
new_callable=AsyncMock,
return_value=mock_child,
) as mock_create,
):
from grimoire.services.recurrence import spawn_recurring_tasks
count = await spawn_recurring_tasks()
assert count == 1
mock_create.assert_called_once()
kwargs = mock_create.call_args[1]
assert kwargs["title"] == "Change air filter"
assert kwargs["due_date"] == date(2026, 6, 1)
assert kwargs["recurrence_rule"] == {"type": "interval", "every": 3, "unit": "month"}
assert kwargs["status"] == "todo"
# ── Multi-status filtering ────────────────────────────────────────────────────
async def test_list_notes_multi_status_builds_in_clause():
"""list_notes with a list of statuses executes without error."""
from unittest.mock import AsyncMock, MagicMock, patch
mock_session = make_mock_session()
mock_result = MagicMock()
mock_result.scalars.return_value.all.return_value = []
mock_session.execute = AsyncMock(return_value=mock_result)
mock_session.scalar = AsyncMock(return_value=0)
with patch("grimoire.services.notes.async_session", return_value=mock_session):
from grimoire.services.notes import list_notes
notes, total = await list_notes(1, status=["todo", "in_progress"], is_task=True)
assert total == 0
assert notes == []
mock_session.scalar.assert_called_once()
mock_session.execute.assert_called_once()