kuri-tool-guard/tests/test_cron_jobs.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

103 lines
3.6 KiB
Python

"""Tests for kuri-tool-guard — cron/jobs.json lockdown guard."""
from __future__ import annotations
import unittest
from tests._support import load_mod
class TestCronJobsJsonGuard(unittest.TestCase):
"""Test _guard_cron_jobs_json — blocks file ops on cron/jobs.json."""
def setUp(self):
self.mod = load_mod()
def test_blocks_patch(self):
result = self.mod._guards._guard_cron_jobs_json(
"patch", {"path": "~/.hermes/cron/jobs.json"}
)
self.assertIsNotNone(result)
self.assertEqual(result["action"], "block")
self.assertIn("cronjob", result["message"])
def test_blocks_write_file(self):
result = self.mod._guards._guard_cron_jobs_json(
"write_file", {"path": "~/.hermes/cron/jobs.json"}
)
self.assertIsNotNone(result)
self.assertEqual(result["action"], "block")
def test_passes_read_file(self):
"""read_file on cron/jobs.json is allowed (read-only, no write risk)."""
result = self.mod._guards._guard_cron_jobs_json(
"read_file", {"path": "~/.hermes/cron/jobs.json"}
)
self.assertIsNone(result)
def test_passes_terminal(self):
"""terminal on cron/jobs.json passes Guard 7 (Guard 2 still catches writes)."""
result = self.mod._guards._guard_cron_jobs_json(
"terminal", {"command": "cat ~/.hermes/cron/jobs.json"}
)
self.assertIsNone(result)
def test_blocks_absolute_path(self):
result = self.mod._guards._guard_cron_jobs_json(
"patch", {"path": "/var/home/alice/.hermes/cron/jobs.json"}
)
self.assertIsNotNone(result)
self.assertEqual(result["action"], "block")
def test_passes_relative_dot_path(self):
"""read_file on relative cron/jobs.json path is allowed."""
result = self.mod._guards._guard_cron_jobs_json(
"read_file", {"path": "./.hermes/cron/jobs.json"}
)
self.assertIsNone(result)
def test_passes_other_file(self):
result = self.mod._guards._guard_cron_jobs_json(
"patch", {"path": "~/.hermes/some/other/file.py"}
)
self.assertIsNone(result)
def test_passes_similar_but_not_cron_jobs(self):
"""Path ending with 'jobs.json' but not 'cron/jobs.json' passes."""
result = self.mod._guards._guard_cron_jobs_json(
"patch", {"path": "~/.hermes/some/jobs.json"}
)
self.assertIsNone(result)
def test_passes_empty_path(self):
result = self.mod._guards._guard_cron_jobs_json("patch", {"path": ""})
self.assertIsNone(result)
def test_passes_untracked_tool(self):
result = self.mod._guards._guard_cron_jobs_json(
"search_files", {"path": "~/.hermes/cron/jobs.json"}
)
self.assertIsNone(result)
def test_blocks_via_dispatcher_patch(self):
result = self.mod._on_pre_tool_call(
tool_name="patch",
args={"path": "~/.hermes/cron/jobs.json"},
)
self.assertIsNotNone(result)
self.assertEqual(result["action"], "block")
def test_passes_via_dispatcher_read(self):
"""read_file on cron/jobs.json passes through dispatcher."""
result = self.mod._on_pre_tool_call(
tool_name="read_file",
args={"path": "~/.hermes/cron/jobs.json"},
)
self.assertIsNone(result)
def test_passes_via_dispatcher_other_file(self):
result = self.mod._on_pre_tool_call(
tool_name="patch",
args={"path": "~/.hermes/some/other/file.py"},
)
self.assertIsNone(result)