Files
FabledGrimoire/tests/test_db_ready.py
T
bvandeusenandClaude Opus 5.5 c5eeec471f
CI & Build / Python lint (push) Successful in 3s
CI & Build / Plugin hooks (push) Successful in 14s
CI & Build / TypeScript typecheck (push) Successful in 53s
CI & Build / integration (push) Successful in 1m2s
CI & Build / Python tests (push) Failing after 1m26s
CI & Build / Build & push image (push) Skipped
rename(547): the plugin is grimoire — manifest, MCP key, hooks, env, skills
Step 3 of the Scribe -> Grimoire rename.

- Plugin `grimoire` in marketplace `grimoire-plugin`, MCP server key
  `grimoire`, so tools reach a session as mcp__plugin_grimoire_grimoire__*.
- hooks/scribe_* -> hooks/grimoire_*, their shell functions and the TMPDIR
  ledgers with them; SCRIBE_URL / SCRIBE_TOKEN / SCRIBE_HOOK_DIR /
  SCRIBE_LEDGER_DIRS -> GRIMOIRE_*; the project marker is `.grimoire`.
- skills/using-scribe -> using-grimoire, /grimoire:sync, stored processes
  arrive as grimoire-proc-<slug>; moment_actions names them the same way.
- Migration 0124 rewrites stored skill names (rule_moments,
  rule_moment_judgments, moment_mappings) from scribe:/scribe-proc- to
  grimoire:/grimoire-proc-, so a rule mounted on a skill keeps firing.
- Settings' install commands and MCP-only defaults, the docs and the
  tests follow.
- scripts/check_plugin.py drops the smoke event for a report-check hook
  that has no file in plugin/hooks.

Kept on purpose: the secret-sealing label, and the names that let backups
written as Scribe restore and be counted.

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

101 lines
3.1 KiB
Python

"""The startup wait for the database: retries what clears up, fails what won't."""
import asyncpg
import pytest
from grimoire.services.db_ready import asyncpg_dsn, wait_for_database
URL = "postgresql+asyncpg://grimoire:p%40ss@db:5432/grimoire"
class _Clock:
"""A fake monotonic clock that only moves when the fake sleep is awaited."""
def __init__(self):
self.now = 0.0
self.sleeps: list[float] = []
def __call__(self) -> float:
return self.now
async def sleep(self, seconds: float) -> None:
self.sleeps.append(seconds)
self.now += seconds
class _Conn:
closed = False
async def close(self):
self.closed = True
def _connect_failing(errors):
"""A connect that raises each error in turn, then succeeds."""
calls = []
conn = _Conn()
async def connect(dsn, timeout):
calls.append(dsn)
if len(calls) <= len(errors):
raise errors[len(calls) - 1]
return conn
return connect, calls, conn
def test_dsn_drops_the_driver_and_keeps_the_escaped_password():
assert asyncpg_dsn(URL) == "postgresql://grimoire:p%40ss@db:5432/grimoire"
async def test_returns_at_once_when_the_database_is_up():
clock = _Clock()
connect, calls, conn = _connect_failing([])
await wait_for_database(URL, connect=connect, sleep=clock.sleep, clock=clock)
assert len(calls) == 1
assert clock.sleeps == []
assert conn.closed
async def test_waits_through_a_database_that_is_still_starting():
clock = _Clock()
connect, calls, conn = _connect_failing([
ConnectionRefusedError("refused"),
OSError("Name or service not known"),
asyncpg.CannotConnectNowError("the database system is starting up"),
])
await wait_for_database(URL, connect=connect, sleep=clock.sleep, clock=clock)
assert len(calls) == 4
assert clock.sleeps == [1.0, 2.0, 4.0]
assert conn.closed
async def test_backoff_is_capped_at_ten_seconds():
clock = _Clock()
connect, _, _ = _connect_failing([ConnectionRefusedError()] * 6)
await wait_for_database(URL, connect=connect, sleep=clock.sleep, clock=clock)
assert clock.sleeps == [1.0, 2.0, 4.0, 8.0, 10.0, 10.0]
async def test_gives_up_with_the_last_error_once_the_deadline_passes():
clock = _Clock()
connect, calls, _ = _connect_failing([ConnectionRefusedError("still down")] * 100)
with pytest.raises(ConnectionRefusedError, match="still down"):
await wait_for_database(
URL, deadline_s=30, connect=connect, sleep=clock.sleep, clock=clock,
)
# 25s waited; another 10s would pass the deadline, so it raises instead.
assert clock.sleeps == [1.0, 2.0, 4.0, 8.0, 10.0]
assert len(calls) == len(clock.sleeps) + 1
async def test_a_wrong_password_fails_at_once():
clock = _Clock()
connect, calls, _ = _connect_failing([
asyncpg.InvalidPasswordError("password authentication failed"),
])
with pytest.raises(asyncpg.InvalidPasswordError):
await wait_for_database(URL, connect=connect, sleep=clock.sleep, clock=clock)
assert len(calls) == 1
assert clock.sleeps == []