refactor: finish declutter - test_flow removal, NOT NULL schema, parametrized tests, smoke harness, docs

This commit is contained in:
Jonas Miederer 2026-10-01 00:11:43 +02:00
parent 696db34c90
commit 92bb3bacba
23 changed files with 552 additions and 1261 deletions

View file

@ -11,48 +11,36 @@ from __future__ import annotations
import json
import os
import shutil
import socket
import subprocess
import sys
import tempfile
import time
import urllib.request
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
from tests._harness import (
free_port,
migrate_throwaway,
plant_session_jwt,
spawn_lfx,
wait_healthy,
)
BACKEND = Path(__file__).resolve().parent.parent
# Local venv (uv run) or image venv (/opt/venv in containers): prefer PATH.
VENV_BIN = Path(shutil.which("lfx") or "").parent or (BACKEND / ".venv" / "bin")
FIXTURES = BACKEND / "tests" / "fixtures" / "flows"
def _free_port() -> int:
# Ephemeral ports: the dev API/LFX may already occupy the defaults
# (e.g. when smoke runs inside the api container itself).
with socket.socket() as s:
s.bind(("127.0.0.1", 0))
return s.getsockname()[1]
LFX_PORT = _free_port()
API_PORT = _free_port()
# Ephemeral ports: the dev API/LFX may already occupy the defaults
# (e.g. when smoke runs inside the api container itself).
LFX_PORT = free_port()
API_PORT = free_port()
LFX_KEY = "lfx-759GsfJKjAeOBZ_44-cll9WdHp_yfYn_tIg7eHG38Rs"
# Stateless admin session JWT, minted in main() after planting the user.
_SMOKE_JWT = ""
def wait_healthy(url: str, timeout: int = 180) -> None:
deadline = time.time() + timeout
while time.time() < deadline:
try:
with urllib.request.urlopen(url, timeout=3) as r:
if r.status == 200:
return
except OSError:
time.sleep(1)
raise RuntimeError(f"never healthy: {url}")
def api(method: str, path: str, body: dict | None = None) -> tuple[int, dict | str]:
import httpx
@ -75,35 +63,19 @@ def check(name: str, cond: bool, detail: str = "") -> None:
def main() -> None:
tmp = Path(tempfile.mkdtemp(prefix="kirche-smoke-"))
flow_dir = Path("/tmp/smoke-e2e-flows")
flow_dir.mkdir(exist_ok=True)
flow_dir = Path(tempfile.mkdtemp(prefix="kirche-smoke-flows-"))
(flow_dir / "seed.json").write_text((FIXTURES / "simple_rule.json").read_text())
# Migrate a throwaway SQLite schema to head before starting the API.
from alembic import command as alembic_command
from alembic.config import Config
db_url = f"sqlite:///{tmp / 'smoke.db'}"
cfg = Config()
cfg.set_main_option("script_location", str(BACKEND / "migrations"))
cfg.set_main_option("sqlalchemy.url", db_url)
# migrations/env.py takes the URL from settings (not the ini value):
# point this process at the throwaway DB before migrating.
from app.settings import settings as app_settings
app_settings.database_url = db_url
alembic_command.upgrade(cfg, "head")
migrate_throwaway(BACKEND, db_url)
# Plant an admin user + mint its session JWT: management + runs
# require login, and there is no SMTP in smoke to deliver a magic link.
# Sessions are stateless (see app/auth/service.py) — the cookie value
# below is the JWT sent as `kirche_session`.
global _SMOKE_JWT
import os
from sqlmodel import Session, create_engine
from app.auth import service as auth_service
from app.models import User
from app.settings import settings as app_settings
if not app_settings.jwt_secret:
@ -112,52 +84,34 @@ def main() -> None:
)
engine = create_engine(db_url)
with Session(engine) as s:
user = User(name="smoke", email="smoke@example.com", role="admin")
s.add(user)
s.commit()
s.refresh(user)
_SMOKE_JWT = auth_service.mint_session_token(user)
_, _SMOKE_JWT = plant_session_jwt(s, email="smoke@example.com", name="smoke")
# Smoke predates the shared harness key; keep its long serve key.
print("smoke admin JWT minted")
engine.dispose()
env = {
**os.environ,
# lfx prefers LANGFLOW_API_KEY over LFX_API_KEY; the repo .env may set
# it. Clear it so the serve key below is the one we send.
"LANGFLOW_API_KEY": "",
"LFX_API_KEY": LFX_KEY,
env_extra = {
"DATABASE_URL": db_url,
"FLOWS_WATCH_ENABLED": "false",
"LFX_URL": f"http://127.0.0.1:{LFX_PORT}",
"CALLBACK_BASE_URL": f"http://127.0.0.1:{API_PORT}",
"CALLBACK_TOKEN": "smoke-callback-token",
}
lfx = subprocess.Popen(
[
str(VENV_BIN / "lfx"),
"serve",
"--host",
"127.0.0.1",
"--port",
str(LFX_PORT),
"--flow-dir",
str(flow_dir),
str(flow_dir),
],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
env=env,
)
lfx = spawn_lfx(flow_dir, LFX_PORT, env_extra=env_extra, api_key=LFX_KEY)
uvicorn = subprocess.Popen(
[str(VENV_BIN / "uvicorn"), "app.main:app", "--host", "127.0.0.1", "--port", str(API_PORT)],
cwd=str(BACKEND),
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
env=env,
env={
**os.environ,
"LANGFLOW_API_KEY": "",
"LFX_API_KEY": LFX_KEY,
**env_extra,
},
)
try:
wait_healthy(f"http://127.0.0.1:{LFX_PORT}/health")
wait_healthy(f"http://127.0.0.1:{API_PORT}/health")
wait_healthy(f"http://127.0.0.1:{LFX_PORT}", deadline_s=180)
wait_healthy(f"http://127.0.0.1:{API_PORT}", deadline_s=180)
s, flows = api("GET", "/api/flows")
check("list flows", s == 200 and isinstance(flows, list))