"""Which actions reach which moment — the pure half. Matching and resolution take no database: the defaults are code and an install's mappings arrive as rows, so every case below hands `resolve` the rows it needs. The writes that store those rows are exercised against real Postgres in `test_integration_moment_mappings.py`. """ from types import SimpleNamespace from unittest.mock import AsyncMock, patch import pytest from grimoire.services import moment_actions as ma from grimoire.services import moments def _row(tool, match, moment, effect=ma.ADD): return SimpleNamespace(tool=tool, match=match, moment=moment, effect=effect) def _reached(tool, tool_input=None, mappings=()): return [h["moment"] for h in ma.resolve(tool, tool_input, mappings)] # ── the defaults ───────────────────────────────────────────────────────── def test_every_default_reaches_a_real_moment(): """A default naming a moment that is not in the catalog would never be mounted on — the typo require_moment refuses at the door.""" bad = [a for a in ma.DEFAULT_ACTIONS if not moments.is_moment(a.moment)] assert not bad, bad def test_no_default_is_listed_twice(): keys = [(ma.tool_key(a.tool), a.match, a.moment) for a in ma.DEFAULT_ACTIONS] assert len(keys) == len(set(keys)) def test_every_default_is_a_mapping_the_door_would_accept(): """The defaults pass the same validation an install's mapping does, so a default and an install's copy of it can never disagree about its shape.""" for a in ma.DEFAULT_ACTIONS: assert ma._clean(a.tool, a.match, a.moment) == (a.tool, a.match, a.moment) # ── matching ───────────────────────────────────────────────────────────── @pytest.mark.parametrize("command", [ "git push origin dev", "git push", "cd app && git push", "make lint; git push --tags", "GIT_TRACE=1 git push", " git push ", ]) def test_a_command_prefix_matches_any_segment_of_the_line(command): assert "work.deliver" in _reached("Bash", {"command": command}) @pytest.mark.parametrize("command", ["git pushd", "echo git push", "git status"]) def test_a_command_prefix_needs_a_word_boundary_and_the_head_of_a_segment(command): assert "work.deliver" not in _reached("Bash", {"command": command}) @pytest.mark.parametrize("command", [ # A heredoc's body is data fed to a program, whatever its lines say. "python3 - <<'EOF'\nimport os\n | curl -fsS http://x\nEOF", "cat > hook.sh < x.sh <<'EOF'\necho hi\nEOF\ncurl http://x", "python3 - <