mirror of
https://github.com/0xWheatyz/handler.git
synced 2026-08-30 08:26:25 +00:00
6fb26115ce
Phase 2 configures forge for the agents (operator only sets a credential_ref + optional version pin) and lets them drive a junior→senior→deploy workflow: - Credential resolution/injection (control/credentials.py): credential_ref pointers (env:/file:/cmd:) resolved only at spawn, injected as FORGE_TOKEN + host var, with a forge-host-scoped git credential helper reading the token from env (never on disk / in the DB). Resolution is a fail-fast spawn gate. - Role-based forge skills committed into the managed repo (control/skills_gen.py, `handler forge-init`): forge-junior/senior/deploy + a workflow overview. - Hard approval gate (hooks/gate.py, approvals table, migration 0002): merge/deploy — and direct pushes to protected branches — are denied unless a DIFFERENT agent has an `approved` record for the branch, pinned to the reviewed commit (approved_sha). Senior records verdicts via `handler approve`/`reject`. - forge/git seams (control/forge.py, control/gitops.py) matching the Phase 1 seam pattern. - CI status poller (control/poller.py, `handler poll-ci [--watch]`) backfilling ci_status/ci_checked_at via `forge ci list`. - Fix: migrations/env.py commits explicitly after run_migrations — pysqlite on Py 3.12+ was rolling back the final migration's DDL + alembic_version stamp (latent in Phase 1). Reviewed via a separate code-reviewer pass; gate-bypass and credential-scoping findings addressed. 106 tests, ruff clean, verified end-to-end against real git + migrations.
150 lines
4.4 KiB
Python
150 lines
4.4 KiB
Python
"""Shared fixtures. Everything runs on a fresh SQLite file per test, materialized via
|
|
a *real* ``alembic upgrade head`` — so the migration path itself is under test, not
|
|
just ``create_all``. No live claude/tmux/mise is ever touched: the three seams
|
|
(``control.tmux``, ``hooks.verify``, ``control.spawn.resume``) are faked.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
from alembic import command
|
|
from alembic.config import Config
|
|
|
|
REPO_ROOT = Path(__file__).resolve().parents[1]
|
|
|
|
|
|
def _reset_caches() -> None:
|
|
from handler import config
|
|
from handler.db import engine
|
|
|
|
config.get_settings.cache_clear()
|
|
engine.get_engine.cache_clear()
|
|
|
|
|
|
@pytest.fixture
|
|
def env(tmp_path, monkeypatch):
|
|
"""Point every entrypoint at a fresh SQLite db + a known token, migrated."""
|
|
db_path = tmp_path / "handler.db"
|
|
url = f"sqlite:///{db_path}"
|
|
monkeypatch.setenv("DATABASE_URL", url)
|
|
monkeypatch.setenv("AUTH_TOKEN", "test-token")
|
|
monkeypatch.setenv("SHARED_CONTEXT_WRITE_TOKEN", "shared-token")
|
|
monkeypatch.setenv("PROJECTS_ROOT", str(tmp_path / "projects"))
|
|
monkeypatch.delenv("WEBHOOK_URL", raising=False)
|
|
_reset_caches()
|
|
|
|
cfg = Config(str(REPO_ROOT / "alembic.ini"))
|
|
cfg.set_main_option("script_location", str(REPO_ROOT / "src" / "handler" / "migrations"))
|
|
command.upgrade(cfg, "head")
|
|
|
|
yield {"url": url, "token": "test-token", "shared_token": "shared-token", "tmp": tmp_path}
|
|
|
|
_reset_caches()
|
|
|
|
|
|
@pytest.fixture
|
|
def engine(env):
|
|
from handler.db.engine import get_engine
|
|
|
|
return get_engine()
|
|
|
|
|
|
@pytest.fixture
|
|
def conn(engine):
|
|
with engine.begin() as c:
|
|
yield c
|
|
|
|
|
|
@pytest.fixture
|
|
def client(env):
|
|
from fastapi.testclient import TestClient
|
|
|
|
from handler.api.app import create_app
|
|
|
|
return TestClient(create_app())
|
|
|
|
|
|
@pytest.fixture
|
|
def auth(env):
|
|
return {"Authorization": f"Bearer {env['token']}"}
|
|
|
|
|
|
@pytest.fixture
|
|
def fake_tmux(monkeypatch):
|
|
"""Record tmux calls instead of spawning; report sessions as live by default."""
|
|
calls: dict[str, list] = {"new_session": [], "kill_session": [], "send_keys": []}
|
|
live: set[str] = set()
|
|
|
|
from handler.control import tmux
|
|
|
|
def new_session(name, cwd, command, env):
|
|
calls["new_session"].append(
|
|
{"name": name, "cwd": cwd, "command": command, "env": env}
|
|
)
|
|
live.add(name)
|
|
|
|
def has_session(name):
|
|
return name in live
|
|
|
|
def kill_session(name):
|
|
calls["kill_session"].append(name)
|
|
live.discard(name)
|
|
|
|
def send_keys(name, keys):
|
|
calls["send_keys"].append({"name": name, "keys": keys})
|
|
|
|
def list_sessions():
|
|
return list(live)
|
|
|
|
monkeypatch.setattr(tmux, "new_session", new_session)
|
|
monkeypatch.setattr(tmux, "has_session", has_session)
|
|
monkeypatch.setattr(tmux, "kill_session", kill_session)
|
|
monkeypatch.setattr(tmux, "send_keys", send_keys)
|
|
monkeypatch.setattr(tmux, "list_sessions", list_sessions)
|
|
|
|
return {"calls": calls, "live": live}
|
|
|
|
|
|
@pytest.fixture
|
|
def fake_gitops(monkeypatch):
|
|
"""Fake the git seam: record config/add/commit, return a controllable branch/sha."""
|
|
from handler.control import gitops
|
|
|
|
state = {"branch": "feat/x", "sha": "abc123def456", "config": [], "add": [], "commit": []}
|
|
|
|
def config_local(cwd, key, value):
|
|
state["config"].append({"cwd": cwd, "key": key, "value": value})
|
|
return True, ""
|
|
|
|
def add(cwd, paths):
|
|
state["add"].append({"cwd": cwd, "paths": paths})
|
|
return True, ""
|
|
|
|
def commit(cwd, message):
|
|
state["commit"].append({"cwd": cwd, "message": message})
|
|
return True, ""
|
|
|
|
monkeypatch.setattr(gitops, "current_branch", lambda cwd: state["branch"])
|
|
monkeypatch.setattr(gitops, "head_sha", lambda cwd: state["sha"])
|
|
monkeypatch.setattr(gitops, "config_local", config_local)
|
|
monkeypatch.setattr(gitops, "add", add)
|
|
monkeypatch.setattr(gitops, "commit", commit)
|
|
return state
|
|
|
|
|
|
@pytest.fixture
|
|
def fake_forge(monkeypatch):
|
|
"""Fake the forge seam: controllable version check + CI runs."""
|
|
from handler.control import forge
|
|
|
|
state = {"version_ok": True, "version_out": "forge 1.2.3", "ci_ok": True, "runs": []}
|
|
|
|
monkeypatch.setattr(
|
|
forge, "check_version", lambda cwd=".": (state["version_ok"], state["version_out"])
|
|
)
|
|
monkeypatch.setattr(forge, "ci_list", lambda cwd, sha: (state["ci_ok"], state["runs"]))
|
|
monkeypatch.setattr(forge, "ci_log", lambda cwd, run_id: (True, "log"))
|
|
return state
|