Files
FabledGrimoire/tests/test_services_dashboard.py
T
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

90 lines
3.3 KiB
Python

"""build_dashboard composition + helpers (services/dashboard.py).
Query semantics (ranking/caps/owner-scope) are exercised by manual smoke —
the repo's unit tests mock the DB, so SQL isn't executed here. These cover the
pure helpers and the section-isolation contract.
"""
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
def test_task_row_maps_fields():
from grimoire.services.dashboard import _task_row
n = MagicMock()
n.id = 5
n.title = "Wire reminders"
n.status = "in_progress"
n.priority = None
# Named, not left to MagicMock: an unset attribute is a truthy Mock, so
# the assertion would pass on a field the row never really carried
# (the reason fake_note exists).
n.task_kind = "spike"
assert _task_row(n) == {
"id": 5, "title": "Wire reminders", "status": "in_progress",
"priority": "none", "task_kind": "spike",
}
def test_task_row_carries_the_kind_so_a_row_can_show_it():
"""The field whose ABSENCE is invisible.
A dashboard row with no task_kind renders no kind badge, which looks
exactly like a list containing no issues and no spikes. The badge is
correct and the payload is short — so the guard belongs on the payload,
where the omission actually was.
"""
from grimoire.services.dashboard import _task_row
n = MagicMock()
n.id = 1
n.title = "t"
n.status = "todo"
n.priority = "none"
n.task_kind = "issue"
assert _task_row(n)["task_kind"] == "issue"
@pytest.mark.asyncio
async def test_safe_returns_value_then_empty_on_error():
from grimoire.services.dashboard import _safe
async def ok():
return [1, 2, 3]
async def boom():
raise RuntimeError("section blew up")
assert await _safe(ok(), []) == [1, 2, 3]
# a failing section returns the supplied empty default, not an exception
assert await _safe(boom(), []) == []
assert await _safe(boom(), {}) == {}
@pytest.mark.asyncio
async def test_build_dashboard_composes_sections():
import grimoire.services.dashboard as dash
with patch.object(dash, "_active_projects", AsyncMock(return_value=["P"])), \
patch.object(dash, "_recently_completed", AsyncMock(return_value=["done"])), \
patch.object(dash, "_open_issues", AsyncMock(return_value=["iss"])), \
patch.object(dash, "_week_stats", AsyncMock(return_value={"open_total": 4})):
out = await dash.build_dashboard(user_id=1)
assert out == {
"active_projects": ["P"],
"recently_completed": ["done"],
"open_issues": ["iss"],
"week_stats": {"open_total": 4},
}
@pytest.mark.asyncio
async def test_build_dashboard_isolates_failing_section():
import grimoire.services.dashboard as dash
with patch.object(dash, "_active_projects", AsyncMock(side_effect=RuntimeError("db down"))), \
patch.object(dash, "_recently_completed", AsyncMock(return_value=["done"])), \
patch.object(dash, "_open_issues", AsyncMock(return_value=[])), \
patch.object(dash, "_week_stats", AsyncMock(return_value={})):
out = await dash.build_dashboard(user_id=1)
# failing section degrades to its empty default; others still populate
assert out["active_projects"] == []
assert out["recently_completed"] == ["done"]