kuri-tool-guard/tests/test_patch_memory.py
XLion 06a7f93d18
refactor(kuri-tool-guard): modularise monolithic __init__.py and tests
Split the 1341-line __init__.py into 5 focused modules:
  _state.py    - home root helpers, mutable poll state
  _parser.py   - shell tokenizer (independent)
  _predicates.py - terminal predicates + bounded output
  _guards.py   - all 9 guard implementations + AST helpers
  __init__.py  - thin entry point + dispatcher + re-exports

Split the 2226-line test_guard.py into 14 dedicated files:
  13 test_*.py files + tests/_support.py (shared helpers)

Key changes:
- Package-relative imports throughout (Hermes loader compatible)
- No top-level module pollution (hermes_plugins.kuri_tool_guard.* only)
- Typechecked via ty with symlink alias for hyphenated dir name
- All 10 guard policies, messages, and block/allow behaviour preserved
- 338 tests (336 original + 2 new: isolation + loader contract)
- ty, ruff, bandit, vulture all pass in just verify
2026-07-31 02:23:02 +08:00

107 lines
4 KiB
Python

"""Tests for kuri-tool-guard — patch memory guard."""
from __future__ import annotations
import unittest
from tests._support import load_mod
class TestPatchMemoryGuard(unittest.TestCase):
"""Test _guard_patch_memory — blocks patch on USER.md / MEMORY.md."""
def setUp(self):
self.mod = load_mod()
def test_blocks_user_md(self):
result = self.mod._guards._guard_patch_memory({"path": "~/.hermes/memories/USER.md"})
self.assertIsNotNone(result)
self.assertEqual(result["action"], "block")
self.assertIn("memories/USER.md", result["message"])
def test_blocks_memory_md(self):
result = self.mod._guards._guard_patch_memory({"path": "~/.hermes/memories/MEMORY.md"})
self.assertIsNotNone(result)
self.assertEqual(result["action"], "block")
self.assertIn("memory", result["message"])
# ── Bare filename allowed (no memories/ prefix) ──
def test_passes_bare_user_md(self):
"""USER.md alone is NOT a memory path — allow."""
result = self.mod._guards._guard_patch_memory({"path": "USER.md"})
self.assertIsNone(result)
def test_passes_bare_memory_md(self):
result = self.mod._guards._guard_patch_memory({"path": "MEMORY.md"})
self.assertIsNone(result)
def test_passes_docs_memory_md(self):
"""docs/MEMORY.md is not a canonical memory path."""
result = self.mod._guards._guard_patch_memory({"path": "docs/MEMORY.md"})
self.assertIsNone(result)
def test_passes_non_memory_stem(self):
"""memories/NOTES.md — not MEMORY.md or USER.md."""
result = self.mod._guards._guard_patch_memory({"path": "memories/NOTES.md"})
self.assertIsNone(result)
def test_passes_some_memories_md(self):
"""some/memories.md — 'memories' not a directory component."""
result = self.mod._guards._guard_patch_memory({"path": "some/memories.md"})
self.assertIsNone(result)
# ── Canonical memory paths blocked ──
def test_blocks_relative_memories(self):
result = self.mod._guards._guard_patch_memory({"path": "memories/MEMORY.md"})
self.assertIsNotNone(result)
self.assertEqual(result["action"], "block")
def test_blocks_dot_slash_memories(self):
result = self.mod._guards._guard_patch_memory({"path": "./memories/USER.md"})
self.assertIsNotNone(result)
self.assertEqual(result["action"], "block")
def test_blocks_profile_path(self):
result = self.mod._guards._guard_patch_memory(
{"path": "~/.hermes/profiles/test/memories/USER.md"}
)
self.assertIsNotNone(result)
self.assertEqual(result["action"], "block")
def test_blocks_custom_root(self):
result = self.mod._guards._guard_patch_memory({"path": "/custom/root/memories/MEMORY.md"})
self.assertIsNotNone(result)
self.assertEqual(result["action"], "block")
def test_blocks_deep_fixtures(self):
"""tests/fixtures/memories/MEMORY.md — deliberately blocked."""
result = self.mod._guards._guard_patch_memory({"path": "tests/fixtures/memories/MEMORY.md"})
self.assertIsNotNone(result)
self.assertEqual(result["action"], "block")
# ── General allow cases ──
def test_passes_other_file(self):
result = self.mod._guards._guard_patch_memory({"path": "some/other/file.py"})
self.assertIsNone(result)
def test_passes_empty_path(self):
result = self.mod._guards._guard_patch_memory({"path": ""})
self.assertIsNone(result)
def test_blocks_via_dispatcher(self):
result = self.mod._on_pre_tool_call(
tool_name="patch",
args={"path": "~/.hermes/memories/USER.md"},
)
self.assertIsNotNone(result)
self.assertEqual(result["action"], "block")
def test_passes_via_dispatcher(self):
result = self.mod._on_pre_tool_call(
tool_name="patch",
args={"path": "some/other/file.py"},
)
self.assertIsNone(result)