test(8249): moment and checkpoint hook guards run the hooks
- test_moment_delivery: the route/hook name check runs the hook with the rule, opened and shape ledgers filled and checks every key it sends is read by the route; "leaves Grimoire's own tools alone" runs it with a listed Grimoire tool and sees no ask. - test_gate_stop_hooks: the action hook run twice on one checkpoint rule holds once, and books the rule on the session ledger after the stop. This replaces two line-window checks in test_pre_act_checkpoint. - The write hook never stopping a write: the two string guards (one per test file) are one, which now also forbids the shared deny helper, the call the old permissionDecision check could not see. - route_query_args (tests/helpers.py) reads the query keys a route takes from its AST, shared by both route/hook contract tests. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -14,7 +14,7 @@ from pathlib import Path
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
|
||||
import pytest
|
||||
from tests.helpers import fake_note, http_sink, writepath_cfg
|
||||
from tests.helpers import fake_note, http_sink, route_query_args, writepath_cfg
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
@@ -936,22 +936,6 @@ def test_prior_art_line_takes_a_search_hit_whose_snippet_is_its_excerpt():
|
||||
|
||||
# --- the route between them --------------------------------------------------
|
||||
|
||||
def _route_args(*fns) -> set[str]:
|
||||
"""Every query key the given route functions read, from their AST."""
|
||||
import ast
|
||||
import inspect
|
||||
import textwrap
|
||||
|
||||
keys = set()
|
||||
for fn in fns:
|
||||
for node in ast.walk(ast.parse(textwrap.dedent(inspect.getsource(fn)))):
|
||||
if (isinstance(node, ast.Call) and isinstance(node.func, ast.Attribute)
|
||||
and node.func.attr == "get" and ast.unparse(node.func.value) == "request.args"
|
||||
and node.args and isinstance(node.args[0], ast.Constant)):
|
||||
keys.add(node.args[0].value)
|
||||
return keys
|
||||
|
||||
|
||||
def test_route_reads_every_arg_the_hook_sends(tmp_path):
|
||||
"""The hook's query string and the route's reader are one contract, and
|
||||
the hook is the only caller, so a rename on either side is silent. The
|
||||
@@ -983,7 +967,7 @@ def test_route_reads_every_arg_the_hook_sends(tmp_path):
|
||||
# Every ledger the hook keeps went out, so this run exercised the whole query.
|
||||
assert {"path", "code", "repo", "shapes", "exclude_ids", "exclude_sync_ids", "exclude_rule_ids",
|
||||
"exclude_derive", "held_rule_ids", "seen_ruling_systems"} <= sent, sent
|
||||
read = _route_args(routes.write_path_prior_art, routes._project_scope)
|
||||
read = route_query_args(routes.write_path_prior_art, routes._project_scope)
|
||||
assert sent - {"session_id"} <= read, f"the route ignores {sorted(sent - read)}"
|
||||
# project_id is the other scope key the hook can send (from a project marker).
|
||||
assert "project_id" in read
|
||||
@@ -1018,11 +1002,20 @@ def test_hook_is_registered_for_write_and_edit():
|
||||
def test_hook_never_returns_a_permission_decision():
|
||||
"""The load-bearing property: this hook informs a write, it cannot stop one.
|
||||
A permissionDecision of deny/ask would put a recall aid in the way of the
|
||||
operator's work."""
|
||||
operator's work.
|
||||
|
||||
It is a standing decision: a recall aid may not stand in the way of the
|
||||
work, even where a stop would catch a miss. The command-side checkpoint
|
||||
(grimoire_tool_rules.sh) is the only hook that holds an act, and
|
||||
extending it to writes is the operator's call to make.
|
||||
"""
|
||||
src = HOOK.read_text()
|
||||
assert "additionalContext" in src
|
||||
code_lines = [ln for ln in src.splitlines() if not ln.lstrip().startswith("#")]
|
||||
assert not any("permissionDecision" in ln for ln in code_lines)
|
||||
# The shared helper that emits a deny, too: a call to it carries no
|
||||
# permissionDecision text in this file.
|
||||
assert not any("grimoire_json_deny" in ln for ln in code_lines)
|
||||
|
||||
|
||||
def test_hook_is_executable_and_shell_valid():
|
||||
|
||||
Reference in New Issue
Block a user