Files
handler/tests/test_hook_dispatch.py
Claude eba0e19ec9 feat(mvp): Phase 1 control layer + API vertical slice
Implements the Phase 1 MVP from the README: a stateless control layer + HTTP
API over a centralized database, with hook-enforced test/push gates.

- DB layer: SQLAlchemy Core, one schema rendering both Postgres (BIGSERIAL /
  TIMESTAMPTZ / JSONB) and SQLite (INTEGER PK / TEXT / JSON) via portable types;
  native ON CONFLICT DO UPDATE checkmark upsert on both dialects.
- Alembic dual-dialect migrations (render_as_batch for SQLite); tests run a real
  `alembic upgrade head`.
- FastAPI: projects/agents/checkmark/log/answer/resume + shared log/context
  routes, single global bearer token, higher-trust token gating shared-context
  writes, project isolation on every route.
- Hooks (`python -m handler.hooks <event>`): Stop test gate (block on red),
  PreToolUse AskUserQuestion defer + `git push` gate (tests then throwaway
  build), Notification generic webhook (no-op without WEBHOOK_URL). Identity via
  env injected at spawn; verify is the mock seam.
- Control CLI: spawn/list/attach/kill, hard `.mise.toml [tasks.test]` gate,
  generated per-agent settings.json, identity + DATABASE_URL injected via tmux;
  tmux is the mock seam.
- 45 tests (SQLite), ruff clean. Live claude/tmux/mise spawning deferred behind
  the mocked seams.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01W5ZuS5pV1NS6eKsRZHXonY
2026-07-07 18:16:54 +00:00

42 lines
1.4 KiB
Python

"""The `python -m handler.hooks <event>` dispatch: stdin parsing + identity from env."""
from __future__ import annotations
import io
from handler.db import repository as repo
from handler.db.engine import get_engine
from handler.hooks import __main__ as hook_main
from handler.hooks import verify
def _seed(env):
with get_engine().begin() as conn:
repo.create_project(conn, "p", "/tmp/p")
return repo.create_agent(conn, "p", "a", "/tmp/p/a")
def test_dispatch_stop_via_stdin_and_env(env, monkeypatch, capsys):
agent = _seed(env)
monkeypatch.setenv("HANDLER_AGENT_ID", str(agent["id"]))
monkeypatch.setenv("HANDLER_PROJECT_ID", "p")
monkeypatch.setenv("HANDLER_AGENT_NAME", "a")
monkeypatch.setattr(verify, "run_test", lambda cwd: (True, "ok"))
monkeypatch.setattr("sys.stdin", io.StringIO('{"session_id": "s1"}'))
rc = hook_main.main(["stop"])
assert rc == 0
with get_engine().begin() as conn:
assert repo.get_checkmark(conn, agent["id"])["tests_status"] == "pass"
def test_dispatch_unknown_event_is_usage_error(env):
assert hook_main.main(["frobnicate"]) == 2
def test_dispatch_unresolvable_identity_returns_1(env, monkeypatch):
monkeypatch.delenv("HANDLER_AGENT_ID", raising=False)
monkeypatch.setattr("sys.stdin", io.StringIO('{"cwd": "/nowhere"}'))
assert hook_main.main(["stop"]) == 1