Files
ai-agent/sidecar/test_turn_hook.py

71 lines
2.5 KiB
Python

"""turn_hook: the no-op Stop hook every account's settings.json gets, so a run
outside the hub writes its turn-end record too."""
import json
import os
import turn_hook as th
def _hooks(path):
return json.loads(path.read_text())["hooks"]
def test_added_once_next_to_the_users_own_hooks(tmp_path):
p = tmp_path / "settings.json"
p.write_text(json.dumps({"model": "fable", "hooks": {
"Stop": [{"hooks": [{"type": "command", "command": "say done"}]}],
"PreToolUse": [{"matcher": "Bash", "hooks": [{"type": "command",
"command": "rtk hook claude"}]}]}}))
os.chmod(p, 0o600)
assert th.ensure(p) is True
hooks = _hooks(p)
assert [h["hooks"][0]["command"] for h in hooks["Stop"]] == ["say done", th.COMMAND]
assert hooks["PreToolUse"][0]["hooks"][0]["command"] == "rtk hook claude"
assert json.loads(p.read_text())["model"] == "fable"
assert os.stat(p).st_mode & 0o777 == 0o600
assert th.ensure(p) is False # found again by its marker
assert len(_hooks(p)["Stop"]) == 2
def test_a_missing_file_is_made(tmp_path):
p = tmp_path / "settings.json"
assert th.ensure(p) is True
assert th.has_hook(json.loads(p.read_text()))
def test_written_through_a_guests_link(tmp_path):
real = tmp_path / "home" / "settings.json"
real.parent.mkdir()
real.write_text("{}")
guest = tmp_path / "guest"
guest.mkdir()
(guest / "settings.json").symlink_to(real)
assert th.ensure(guest / "settings.json") is True
assert (guest / "settings.json").is_symlink()
assert th.has_hook(json.loads(real.read_text()))
def test_a_file_that_isnt_ours_to_fix_is_left_alone(tmp_path):
p = tmp_path / "settings.json"
p.write_text("{not json")
try:
th.ensure(p)
except ValueError:
pass
else:
raise AssertionError("expected ValueError")
assert p.read_text() == "{not json"
def test_config_dirs_dedupe_and_skip_missing(tmp_path, monkeypatch):
home = tmp_path / ".claude"
(home / "projects").mkdir(parents=True)
work = tmp_path / ".claude-work"
work.mkdir()
import config # the dirs are config.claude_config_dirs (callprobe reads them too)
monkeypatch.setattr(config, "CLAUDE_PROJECTS", home / "projects")
monkeypatch.setattr(config, "ACCOUNTS", {"personal": None, "work": work,
"alias": home, "gone": tmp_path / "nope"})
assert th.config_dirs() == [home, work] == config.claude_config_dirs()