@@ -721,73 +721,63 @@ def test_the_tool_arm_is_registered_on_bash():
|
||||
assert any("grimoire_prior_art.sh" in c for c in entries["Write|Edit"])
|
||||
|
||||
|
||||
def test_the_hook_and_the_route_agree_on_every_parameter_name():
|
||||
def test_the_route_reads_every_name_the_hook_sends(tmp_path):
|
||||
"""Two layers that only this test connects agree on their contract.
|
||||
|
||||
The hook is shell and the route is Python; nothing but this test connects
|
||||
them. A renamed query arg fails SILENTLY — the route reads an absent value,
|
||||
the arm quietly searches nothing, and the surface looks like one that never
|
||||
The hook is shell and the route is Python; nothing else connects them. A
|
||||
renamed query key fails SILENTLY — the route reads an absent value, the
|
||||
arm quietly searches nothing, and the surface looks like one that never
|
||||
finds anything rather than one that is broken.
|
||||
|
||||
So the hook is run for real, in a repo with a remote and with every
|
||||
session ledger it keeps holding something, and every key it sends must be
|
||||
one the route reads: in the handler, in the project-scope helper it calls,
|
||||
or in the session binder that runs before every plugin route.
|
||||
"""
|
||||
import re
|
||||
import json
|
||||
import os
|
||||
import subprocess
|
||||
|
||||
hook = Path("plugin/hooks/grimoire_tool_rules.sh").read_text()
|
||||
defs = Path("plugin/hooks/grimoire_defs.sh").read_text()
|
||||
route = Path("src/grimoire/routes/plugin.py").read_text()
|
||||
handler = route.split("async def pre_tool_rules")[1].split("\n@plugin_bp")[0]
|
||||
from grimoire.routes import plugin as routes
|
||||
from tests.helpers import http_sink, need_tools, route_query_args
|
||||
|
||||
sent = set(re.findall(r"[?&]([a-z_]+)=", hook))
|
||||
# `root` and `cwd`: where the command runs, so the paths it
|
||||
# names can be made repo-relative for the rulings arm.
|
||||
# `session_id`: which session the lines go to, for the delivery record.
|
||||
assert sent == {"tool", "command", "exclude_rule_ids", "root", "cwd", "session_id"}, sent
|
||||
need_tools("bash", "curl", "awk", "git")
|
||||
hook = Path(__file__).resolve().parents[1] / "plugin" / "hooks" / "grimoire_tool_rules.sh"
|
||||
env = {"PATH": os.environ["PATH"], "GRIMOIRE_TOKEN": "t",
|
||||
"TMPDIR": str(tmp_path), "HOME": str(tmp_path)}
|
||||
repo = tmp_path / "repo"
|
||||
repo.mkdir()
|
||||
subprocess.run(["git", "init", "-q"], cwd=repo, check=True, env=env)
|
||||
subprocess.run(["git", "remote", "add", "origin", "https://example.com/acme/app.git"],
|
||||
cwd=repo, check=True, env=env)
|
||||
# The opened ledger is written by the hook that watches get_rule, not this one.
|
||||
state = tmp_path / "grimoire-priorart"
|
||||
state.mkdir()
|
||||
(state / "s-args.opened.ids").write_text("7\n")
|
||||
# The first reply fills the named-rule and rulings ledgers, so the second
|
||||
# run sends every key the hook has.
|
||||
reply = b'{"context":"> rule 33","rule_ids":[33],"ruling_system_ids":[4]}'
|
||||
event = {"session_id": "s-args", "cwd": str(repo), "tool_name": "Bash",
|
||||
"tool_input": {"command": "git push origin dev"}}
|
||||
with http_sink(by_path={"/api/plugin/tool-rules": reply}) as (port, seen):
|
||||
env["GRIMOIRE_URL"] = f"http://127.0.0.1:{port}"
|
||||
for _ in range(2):
|
||||
out = subprocess.run(["bash", str(hook)], input=json.dumps(event),
|
||||
capture_output=True, text=True, env=env, timeout=30)
|
||||
assert out.returncode == 0, out.stderr
|
||||
asks = [e for e in seen if e["_path"] == "/api/plugin/tool-rules"]
|
||||
assert len(asks) == 2
|
||||
sent = {k for k in asks[-1] if not k.startswith("_")}
|
||||
assert {"tool", "command", "repo", "root", "cwd", "exclude_rule_ids", "held_rule_ids",
|
||||
"seen_ruling_systems", "session_id"} <= sent, sent
|
||||
|
||||
# The project-scope key is the shared helper's to choose: the hook
|
||||
# splices in `grimoire_scope_query`'s output, which is `repo=` inside a git
|
||||
# repo and `project_id=` where a `.grimoire` marker names the project. Both
|
||||
# halves are one contract with the route, so both are pinned.
|
||||
assert "grimoire_scope_query" in hook, "hook no longer asks for a project scope"
|
||||
# `held_rule_ids` joins them for the same reason: the OPENED ledger
|
||||
# is read by a shared helper so its ageing and format cannot drift from the
|
||||
# naming ledger's, which means the key is spelled in defs rather than here.
|
||||
assert "grimoire_held_query" in hook, "hook no longer sends the opened ledger"
|
||||
assert set(re.findall(r"printf '([a-z_]+)=", defs)) == {"repo", "project_id"}
|
||||
# Spelled with its own leading `&` because it is APPENDED to a query that
|
||||
# already has a scope key, where the two above are alternatives that open
|
||||
# one. Asserted separately for that reason: a scope key and a ledger key
|
||||
# are different contracts that happen to share a file.
|
||||
assert "printf '&held_rule_ids=" in defs, (
|
||||
"the opened ledger's query key is no longer spelled in the helper"
|
||||
)
|
||||
# The rulings ledger, shared with the write-path hook —
|
||||
# spelled once in its helper for the same reason.
|
||||
assert "grimoire_rulings_query" in hook, "hook no longer sends the rulings ledger"
|
||||
assert "printf '&seen_ruling_systems=" in defs, (
|
||||
"the rulings ledger's query key is no longer spelled in the helper"
|
||||
)
|
||||
|
||||
# Both scope keys are read by the shared _project_scope() helper, not inline.
|
||||
assert "_project_scope()" in handler
|
||||
scope = route.split("async def _project_scope")[1].split("\n@plugin_bp")[0]
|
||||
for arg in ("project_id", "repo"):
|
||||
assert f'request.args.get("{arg}"' in scope, (
|
||||
f"the hooks can send {arg!r} and the route never reads it"
|
||||
)
|
||||
# Read by the handler itself, not the scope helper — it is about what the
|
||||
# reader holds, not about which project the work belongs to.
|
||||
assert 'request.args.get("held_rule_ids")' in handler, (
|
||||
"the hook sends held_rule_ids and the route never reads it"
|
||||
)
|
||||
for arg in ("tool", "command", "exclude_rule_ids", "root", "cwd", "seen_ruling_systems"):
|
||||
assert f'request.args.get("{arg}")' in handler, (
|
||||
f"the hook sends {arg!r} and the route never reads it"
|
||||
)
|
||||
# The session is bound for every plugin route at once, before the handler.
|
||||
binder = route.split("async def _bind_delivery_session")[1].split("\n@plugin_bp")[0]
|
||||
assert "@plugin_bp.before_request" in route.split("async def _bind_delivery_session")[0][-80:]
|
||||
assert 'request.args.get("session_id")' in binder, (
|
||||
"the hook sends session_id and no route reads it"
|
||||
)
|
||||
read = route_query_args(routes.pre_tool_rules, routes._project_scope,
|
||||
routes._bind_delivery_session)
|
||||
assert sent <= read, f"the route ignores {sorted(sent - read)}"
|
||||
# The binder reads session_id only because it runs before this route.
|
||||
assert routes._bind_delivery_session in routes.plugin_bp.before_request_funcs[None]
|
||||
# A `.grimoire` marker sends project_id in place of repo, and the route reads that too.
|
||||
assert "project_id" in read
|
||||
|
||||
|
||||
# ── The CALL log is unconditional; the SURFACING log is not ───────────
|
||||
|
||||
Reference in New Issue
Block a user