Files
caveman/tests/test_hooks.py
T
Julius Brussee 8783f63296 Require statusLine and fix PS5.1 parsing
Make the installer require a configured statusLine before treating hooks as already installed and ensure PowerShell 5.1 compatibility. hooks/install.ps1: stop using -AsHashtable, adjust hooks access, track presence of settings.statusLine and only skip installation when SessionStart, UserPromptSubmit and statusLine are present. hooks/install.sh: add HAS_STATUSLINE check and update the Node-based settings validation to require settings.statusLine. tests/test_hooks.py: add test_install_reconfigures_missing_statusline to verify installer adds a missing statusLine. tests/verify_repo.py: add a manifest path to the verification list and assert that -AsHashtable is not used in install.ps1 to preserve compatibility with Windows PowerShell 5.1.
2026-04-11 12:22:07 +02:00

162 lines
5.9 KiB
Python

import json
import os
import subprocess
import tempfile
import unittest
from pathlib import Path
REPO_ROOT = Path(__file__).resolve().parent.parent
class HookScriptTests(unittest.TestCase):
def run_cmd(self, cmd, home):
env = os.environ.copy()
env["HOME"] = str(home)
env["USERPROFILE"] = str(home)
return subprocess.run(
cmd,
cwd=REPO_ROOT,
env=env,
text=True,
capture_output=True,
check=True,
)
def test_install_upgrades_old_two_file_install(self):
with tempfile.TemporaryDirectory(prefix="caveman-hooks-upgrade-") as tmp:
home = Path(tmp)
hooks_dir = home / ".claude" / "hooks"
hooks_dir.mkdir(parents=True)
(home / ".claude" / "settings.json").write_text("{}\n")
(hooks_dir / "caveman-activate.js").write_text("")
(hooks_dir / "caveman-mode-tracker.js").write_text("")
self.run_cmd(["bash", "hooks/install.sh"], home)
statusline = hooks_dir / "caveman-statusline.sh"
self.assertTrue(statusline.exists(), "upgrade should install statusline script")
settings = json.loads((home / ".claude" / "settings.json").read_text())
self.assertIn("statusLine", settings)
self.assertIn(str(statusline), settings["statusLine"]["command"])
def test_install_reconfigures_missing_statusline(self):
with tempfile.TemporaryDirectory(prefix="caveman-hooks-statusline-") as tmp:
home = Path(tmp)
claude_dir = home / ".claude"
hooks_dir = claude_dir / "hooks"
hooks_dir.mkdir(parents=True)
for name in ("caveman-activate.js", "caveman-mode-tracker.js", "caveman-statusline.sh"):
(hooks_dir / name).write_text("")
settings = {
"hooks": {
"SessionStart": [
{
"hooks": [
{
"type": "command",
"command": f'node "{hooks_dir / "caveman-activate.js"}"',
}
]
}
],
"UserPromptSubmit": [
{
"hooks": [
{
"type": "command",
"command": f'node "{hooks_dir / "caveman-mode-tracker.js"}"',
}
]
}
],
}
}
(claude_dir / "settings.json").write_text(json.dumps(settings, indent=2) + "\n")
result = self.run_cmd(["bash", "hooks/install.sh"], home)
self.assertNotIn("Nothing to do", result.stdout)
updated = json.loads((claude_dir / "settings.json").read_text())
self.assertIn("statusLine", updated)
self.assertIn(str(hooks_dir / "caveman-statusline.sh"), updated["statusLine"]["command"])
def test_uninstall_preserves_custom_statusline(self):
with tempfile.TemporaryDirectory(prefix="caveman-hooks-uninstall-") as tmp:
home = Path(tmp)
claude_dir = home / ".claude"
hooks_dir = claude_dir / "hooks"
hooks_dir.mkdir(parents=True)
for name in ("caveman-activate.js", "caveman-mode-tracker.js", "caveman-statusline.sh"):
(hooks_dir / name).write_text("")
settings = {
"statusLine": {
"type": "command",
"command": "bash /tmp/custom-status-with-caveman.sh",
},
"hooks": {
"SessionStart": [
{
"hooks": [
{
"type": "command",
"command": f'node "{hooks_dir / "caveman-activate.js"}"',
}
]
}
],
"UserPromptSubmit": [
{
"hooks": [
{
"type": "command",
"command": f'node "{hooks_dir / "caveman-mode-tracker.js"}"',
}
]
}
],
},
}
(claude_dir / "settings.json").write_text(json.dumps(settings, indent=2) + "\n")
self.run_cmd(["bash", "hooks/uninstall.sh"], home)
updated = json.loads((claude_dir / "settings.json").read_text())
self.assertEqual(
updated["statusLine"]["command"],
"bash /tmp/custom-status-with-caveman.sh",
)
self.assertNotIn("hooks", updated)
def test_activate_does_not_nudge_when_custom_statusline_exists(self):
with tempfile.TemporaryDirectory(prefix="caveman-hooks-activate-") as tmp:
home = Path(tmp)
claude_dir = home / ".claude"
claude_dir.mkdir(parents=True)
(claude_dir / "settings.json").write_text(
json.dumps(
{
"statusLine": {
"type": "command",
"command": "bash /tmp/my-statusline.sh",
}
}
)
+ "\n"
)
result = self.run_cmd(["node", "hooks/caveman-activate.js"], home)
self.assertNotIn("STATUSLINE SETUP NEEDED", result.stdout)
self.assertEqual((claude_dir / ".caveman-active").read_text(), "full")
if __name__ == "__main__":
unittest.main()