test(8249): hook contract tests run the hooks instead of searching their source

- test_write_path_trigger:
  - the sync, reuse and rule dedup channels: two string searches become one
    run of the hook twice in a session, checking each class of id comes back
    on its own parameter;
  - "stays a GET": one request to the prior-art route, with no body;
  - the route/hook argument contract: the hook runs with every ledger it
    keeps already holding something, and every key it sends must be read by
    the route (read from the route's AST);
  - the "names the shapes" string search goes: two runtime tests already check
    the Write and the Edit cases.
- test_rule_moment_signal_hooks: the body the route must read is the body the
  hook actually sent.
- test_task_claims: session start is run and must send source and session_id;
  session end is run and releases on logout but not on /clear.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-01 17:24:22 -05:00
co-authored by Claude Opus 5.5
parent 85b29d642f
commit 8fccaa0f3c
3 changed files with 131 additions and 115 deletions
+33 -9
View File
@@ -24,7 +24,7 @@ import pytest_asyncio
from grimoire.services import notes as notes_svc
from grimoire.services import task_claims as tc
from tests.helpers import ensure_user
from tests.helpers import ensure_user, http_sink
ROOT = Path(__file__).resolve().parents[1]
@@ -233,12 +233,33 @@ def test_a_dead_claim_on_finished_work_is_not_news():
assert _render("startup", [done]) == ""
def test_the_session_start_hook_sends_the_source_and_the_session():
def _run_hook(script, event, tmp_path, port):
import os
import shutil
import subprocess
for tool in ("bash", "curl"):
if shutil.which(tool) is None:
pytest.skip(f"hook runtime tool {tool!r} not installed")
env = {"PATH": os.environ["PATH"], "HOME": str(tmp_path), "TMPDIR": str(tmp_path),
"GRIMOIRE_URL": f"http://127.0.0.1:{port}", "GRIMOIRE_TOKEN": "t",
"CLAUDE_PROJECT_DIR": str(tmp_path)}
out = subprocess.run(["bash", str(ROOT / "plugin/hooks" / script)],
input=json.dumps(event), capture_output=True, text=True,
env=env, timeout=30)
assert out.returncode == 0, out.stderr
def test_the_session_start_hook_sends_the_source_and_the_session(tmp_path):
"""The reader branches on what the hook sends; a hook that stopped sending
either would leave every session on the no-claims path, silently."""
text = (ROOT / "plugin/hooks/grimoire_session_context.sh").read_text()
assert re.search(r'q="source=\$\(printf', text)
assert "session_id=$(printf" in text
with http_sink() as (port, seen):
_run_hook("grimoire_session_context.sh",
{"session_id": "s-start", "source": "compact", "cwd": str(tmp_path)},
tmp_path, port)
asked = [q for q in seen if "source" in q]
assert asked and asked[0]["source"] == ["compact"]
assert asked[0]["session_id"] == ["s-start"]
@pytest.mark.integration
@@ -260,15 +281,18 @@ async def test_session_start_after_a_compaction_carries_the_claimed_task(users):
# --- step 4: the hand-off ----------------------------------------------------
def test_session_end_releases_claims_but_not_on_clear():
@pytest.mark.parametrize("reason,releases", [("logout", True), ("clear", False)])
def test_session_end_releases_claims_but_not_on_clear(tmp_path, reason, releases):
"""The mechanical half of the hand-off. A /clear keeps the claim, because
SessionStart(source=clear) pushes the claimed work straight back."""
hooks = json.loads((ROOT / "plugin/hooks/hooks.json").read_text())["hooks"]
commands = [h["command"] for b in hooks.get("SessionEnd", []) for h in b["hooks"]]
assert any("grimoire_session_end.sh" in c for c in commands)
text = (ROOT / "plugin/hooks/grimoire_session_end.sh").read_text()
assert '[ "$reason" = "clear" ] && exit 0' in text
assert "/api/plugin/release-session" in text
with http_sink(by_path={}) as (port, seen):
_run_hook("grimoire_session_end.sh", {"session_id": "s-end", "reason": reason},
tmp_path, port)
released = [q for q in seen if q["_path"] == "/api/plugin/release-session"]
assert ([q["session_id"] for q in released] == [["s-end"]]) is releases
@pytest.mark.integration