"""`grimoire_record_read.sh`, run for real through bash with a stand-in `curl`: an opened record and a search the session ran are reported to the server with the session, and the hook is registered on exactly those tools.""" from __future__ import annotations import json import re from tests.test_rule_moment_signal_hooks import HOOKS, _run, sandbox # noqa: F401 - fixture ROOT = HOOKS.parents[1] def _sent(tmp) -> tuple[str, dict | None]: log = (tmp / "curl.log").read_text() if (tmp / "curl.log").exists() else "" body = (tmp / "curl.body").read_text() if (tmp / "curl.body").exists() else "" return log, (json.loads(body) if body else None) def test_an_opened_record_is_reported_with_its_session(sandbox): # noqa: F811 tmp, env = sandbox _run("grimoire_record_read.sh", {"session_id": "s1", "tool_name": "mcp__grimoire__get_task", "tool_input": {"task_id": 42}}, env) log, body = _sent(tmp) assert "/api/plugin/record-opened" in log assert body == {"session_id": "s1", "note_id": 42} def test_a_search_is_reported_with_its_query(sandbox): # noqa: F811 tmp, env = sandbox _run("grimoire_record_read.sh", {"session_id": "s1", "tool_name": "mcp__plugin_grimoire_grimoire__search", "tool_input": {"q": 'why does "the lock" stick', "content_type": "issue"}}, env) log, body = _sent(tmp) assert "/api/plugin/record-searched" in log assert body == {"session_id": "s1", "q": 'why does "the lock" stick'} def test_a_process_opened_by_name_is_not_reported(sandbox): # noqa: F811 tmp, env = sandbox _run("grimoire_record_read.sh", {"session_id": "s1", "tool_name": "mcp__grimoire__get_process", "tool_input": {"name_or_id": "Drift Audit"}}, env) assert _sent(tmp) == ("", None) def test_the_hook_runs_after_every_record_getter_and_search_and_nothing_else(): hooks = json.loads((ROOT / "plugin" / "hooks" / "hooks.json").read_text())["hooks"]["PostToolUse"] matchers = [e["matcher"] for e in hooks if any("grimoire_record_read.sh" in h["command"] for h in e["hooks"])] assert len(matchers) == 1 runs = re.compile(f"^(?:{matchers[0]})$") for tool in ("get_note", "get_task", "get_snippet", "get_lesson", "get_process", "search"): assert runs.match(f"mcp__plugin_grimoire_grimoire__{tool}"), tool for tool in ("get_rule", "search_rules", "update_task", "get_milestone"): assert not runs.match(f"mcp__plugin_grimoire_grimoire__{tool}"), tool