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

@ -1,7 +1,7 @@
"""Shared e2e harness: free ports, health waits, lfx spawn, migrate, sessions.
Single place for logic duplicated between tests/conftest.py and
scripts/smoke_e2e.py (~60 lines). Full real-LFX runs are kept.
Single place for logic shared between tests/conftest.py and
scripts/smoke_e2e.py. Full real-LFX runs are kept.
"""
from __future__ import annotations
@ -15,6 +15,10 @@ import time
import urllib.request
from pathlib import Path
# Single constant: lfx prefers LANGFLOW_API_KEY over LFX_API_KEY, so the
# repo .env value must be cleared wherever a test key is used.
TEST_LFX_KEY = "test-lfx-key"
def free_port() -> int:
sock = socket.socket()
@ -36,10 +40,14 @@ def wait_healthy(url: str, deadline_s: float = 240.0) -> None:
raise RuntimeError(f"server did not become healthy: {url}")
def spawn_lfx(flow_dir: Path, port: int) -> subprocess.Popen:
def spawn_lfx(
flow_dir: Path, port: int, env_extra: dict | None = None, api_key: str = TEST_LFX_KEY
) -> subprocess.Popen:
bindir = Path(sys.executable).parent
lfx_bin = shutil.which("lfx", path=str(bindir)) or "lfx"
env = {**os.environ, "LANGFLOW_API_KEY": "", "LFX_API_KEY": "test-lfx-key"}
env = {**os.environ, "LANGFLOW_API_KEY": "", "LFX_API_KEY": api_key}
if env_extra:
env.update(env_extra)
return subprocess.Popen(
[
lfx_bin,
@ -59,20 +67,21 @@ def spawn_lfx(flow_dir: Path, port: int) -> subprocess.Popen:
)
def migrate_throwaway(backend_dir: Path) -> None:
def migrate_throwaway(backend_dir: Path, db_url: str | None = None) -> None:
from alembic import command as alembic_command
from alembic.config import Config
cfg = Config()
cfg.set_main_option("script_location", str(backend_dir / "migrations"))
if db_url is not None:
cfg.set_main_option("sqlalchemy.url", db_url)
# migrations/env.py takes the URL from settings (not the ini value).
from app.settings import settings as app_settings
app_settings.database_url = db_url
alembic_command.upgrade(cfg, "head")
# Single constant: lfx prefers LANGFLOW_API_KEY over LFX_API_KEY, so the
# repo .env value must be cleared wherever a test key is used.
TEST_LFX_KEY = "test-lfx-key"
def spawn_uvicorn(app, host: str = "127.0.0.1", port: int = 0): # type: ignore[no-untyped-def]
"""Real uvicorn server in a daemon thread. Returns (server, thread)."""
import threading
@ -91,7 +100,7 @@ def spawn_uvicorn(app, host: str = "127.0.0.1", port: int = 0): # type: ignore[
return server, thread
def plant_session_jwt(session, email: str, role: str = "admin", name: str = "Test") -> object:
def plant_session_jwt(session, email: str, role: str = "admin", name: str = "Test") -> tuple:
"""Idempotent test user + session JWT (shared with smoke pattern)."""
from sqlmodel import select