agnes-the-ai-analyst/tests/test_setup_hooks_template.py
ZdenekSrotyr d0b7e122d6 feat(cli): smart local sync — Claude Code SessionStart/SessionEnd hooks + da sync --quiet
The analyst flow becomes a closed loop with the server-curated table catalog:

  - `da analyst setup` writes `<workspace>/.claude/settings.json` with two hooks:
      SessionStart → `da sync --quiet || true`        — pulls fresh RBAC-filtered parquets at session start
      SessionEnd   → `da sync --upload-only --quiet || true` — uploads session jsonl + CLAUDE.local.md
  - `|| true` keeps Claude Code unblocked when the server is down.
  - Workspace-level (not user-home) so the hooks fire only when Claude Code opens this analyst workspace.
  - `da sync --quiet` rewrites the CLI output for hook consumption — 0 stdout on success, single-line error on failure.
  - Existing settings.json is patched (deep-merged), not overwritten; malformed JSON is reported, not silently overwritten.

Tests cover: workspace bootstrap, hook insertion, malformed-json safety, quiet-mode output shape.
2026-05-01 20:25:27 +02:00

33 lines
1.3 KiB
Python

"""The shipped Claude settings template must point hooks at `da sync`, not the deleted server/scripts."""
import json
from pathlib import Path
REPO_ROOT = Path(__file__).resolve().parents[1]
TEMPLATE = REPO_ROOT / "docs" / "setup" / "claude_settings.json"
def test_template_has_session_start_da_sync():
cfg = json.loads(TEMPLATE.read_text())
starts = cfg.get("hooks", {}).get("SessionStart", [])
assert starts, "SessionStart hook missing"
cmds = [h["command"] for entry in starts for h in entry.get("hooks", [])]
assert any("da sync" in c and "--upload-only" not in c for c in cmds), (
f"Expected `da sync` in SessionStart, got {cmds}"
)
def test_template_has_session_end_upload():
cfg = json.loads(TEMPLATE.read_text())
ends = cfg.get("hooks", {}).get("SessionEnd", [])
cmds = [h["command"] for entry in ends for h in entry.get("hooks", [])]
assert any("da sync --upload-only" in c for c in cmds), (
f"Expected `da sync --upload-only` in SessionEnd, got {cmds}"
)
def test_template_drops_dead_server_scripts_reference():
raw = TEMPLATE.read_text()
assert "server/scripts/collect_session.py" not in raw, (
"Template still references the deleted server/scripts/collect_session.py — "
"the SessionEnd hook would silently fail."
)