contiai / apps /api /scripts /smoke_local.py
aliabozahra315's picture
Claude Opus 5
Take a 100-page book from $7.74 to about a tenth of it
b688fe8
Raw History Blame Contribute Delete
12.1 kB
# ruff: noqa: T201
"""Walk the whole product against the running local stack.
This is the rehearsal for a human doing the same tour: admin signs in, a new
user signs up and waits, gets approved with specific tools, sees only those
tools, an announcement reaches them, suspension locks them out, and one small
run travels queue -> engine -> LLM -> review gate with its cost attributed.
Tokens are minted locally with the legacy HS256 secret, which the verifier
still accepts - so no Supabase round trip is needed to impersonate a user.
Cost: exactly one small model call (the blueprint conversion for a two-unit
pasted outline) plus one 5-token ping. Everything else is free. The run is
cancelled at the gate, never approved.
Usage: .venv/Scripts/python.exe scripts/smoke_local.py
"""
from __future__ import annotations
import sys
import time
import uuid
from datetime import UTC, datetime, timedelta
import httpx
import jwt
sys.path.insert(0, ".")
from app.core.config import settings # noqa: E402
BASE = "http://127.0.0.1:8000"
ADMIN_EMAIL = (settings.bootstrap_admin_emails or ["admin@example.com"])[0]
passed: list[str] = []
failed: list[str] = []
def check(name: str, condition: bool, detail: str = "") -> None:
if condition:
passed.append(name)
print(f" ok {name}")
else:
failed.append(f"{name} {detail}")
print(f" FAIL {name} {detail}")
def token_for(email: str, user_id: uuid.UUID, meta: dict | None = None) -> str:
if not settings.supabase_jwt_secret:
print("SUPABASE_JWT_SECRET is not set; cannot mint local tokens")
sys.exit(2)
now = datetime.now(UTC)
return jwt.encode(
{
"sub": str(user_id),
"email": email,
"aud": "authenticated",
"user_metadata": meta or {},
"iat": now,
"exp": now + timedelta(hours=1),
},
settings.supabase_jwt_secret,
algorithm="HS256",
)
def existing_user_id(email: str) -> uuid.UUID | None:
"""The account this email already owns, if the platform has seen it."""
import sqlalchemy as sa
engine = sa.create_engine(settings.sync_database_url)
try:
with engine.connect() as conn:
row = conn.execute(
sa.text("select id from users where email = :e"), {"e": email.lower()}
).first()
except Exception:
return None
finally:
engine.dispose()
return uuid.UUID(str(row[0])) if row else None
def client(token: str) -> httpx.Client:
return httpx.Client(
base_url=BASE, headers={"Authorization": f"Bearer {token}"}, timeout=30
)
def main() -> int:
print("== health ==")
r = httpx.get(f"{BASE}/health", timeout=10)
check("api is up", r.status_code == 200, str(r.status_code))
# Reuse the row that already exists for this email. A fresh uuid would be
# a second account on a unique column - which is exactly the 409 the
# verifier now returns instead of a 500.
admin_id = existing_user_id(ADMIN_EMAIL) or uuid.uuid4()
admin = client(token_for(ADMIN_EMAIL, admin_id))
print("== bootstrap admin ==")
me = admin.get("/v1/me")
check("bootstrap email signs in", me.status_code == 200, me.text[:120])
if me.status_code == 200:
body = me.json()
check("arrives as admin", body["role"] == "admin", body["role"])
check("arrives active", body["status"] == "active", body["status"])
check("arrives with tools", len(body["allowed_task_types"]) > 0)
admin_id = uuid.UUID(body["id"])
print("== signup -> pending ==")
smoke_email = f"smoke-{uuid.uuid4().hex[:8]}@example.com"
user_id = uuid.uuid4()
user = client(token_for(smoke_email, user_id,
{"full_name": "مستخدم الفحص", "department": "books"}))
me = user.get("/v1/me")
check("new account is created on first call", me.status_code == 200, me.text[:120])
if me.status_code == 200:
body = me.json()
check("lands pending", body["status"] == "pending", body["status"])
check("department recorded", body["department"] == "books", str(body["department"]))
check("name recorded", body["full_name"] == "مستخدم الفحص")
check("pending cannot list tools",
user.get("/v1/task-types").status_code == 403)
check("pending cannot create a project",
user.post("/v1/projects", json={"kind": "book", "title": "x"}).status_code == 403)
print("== approval ==")
queue = admin.get("/v1/admin/users/pending")
check("appears in the queue",
queue.status_code == 200
and any(row["email"] == smoke_email for row in queue.json()),
f"{queue.status_code} {queue.text[:120]}")
approved = admin.post(
f"/v1/admin/users/{user_id}/approve",
json={"allowed_task_types": ["book", "references"], "department": "books"},
)
check("approve succeeds", approved.status_code == 200, approved.text[:120])
grid = user.get("/v1/task-types")
check("grid shows exactly the granted tools",
grid.status_code == 200
and {t["id"] for t in grid.json()["items"]} == {"book", "references"},
grid.text[:120])
check("an ungranted type reads as nonexistent",
user.get("/v1/task-types/level").status_code == 404)
check("an ungranted type cannot be started",
user.post("/v1/projects", json={"kind": "level", "title": "x"}).status_code == 403)
print("== announcements ==")
made = admin.post(
"/v1/admin/announcements",
json={"body": "إعلان فحص تلقائي", "kind": "info", "audience": "all",
"published": True},
)
check("create published", made.status_code == 201, made.text[:120])
ann_id = made.json().get("id") if made.status_code == 201 else None
seen = user.get("/v1/announcements")
check("published announcement reaches the user",
any(a["id"] == ann_id for a in seen.json().get("items", [])), seen.text[:200])
admin.patch(f"/v1/admin/announcements/{ann_id}", json={"published": False})
seen = user.get("/v1/announcements")
check("unpublishing hides it",
not any(a["id"] == ann_id for a in seen.json().get("items", [])))
check("delete announcement",
admin.delete(f"/v1/admin/announcements/{ann_id}").status_code == 204)
print("== one small run through the gate (the only paid part) ==")
project = user.post(
"/v1/projects",
json={"kind": "book", "title": "كتاب الفحص المصغّر"},
)
check("project created", project.status_code == 201, project.text[:120])
project_id = project.json()["id"]
outline = (
"كتاب تجريبي قصير عن آداب المراسلات.\n\n"
"الوحدة الأولى: مبادئ المراسلة\n"
"- التحية الافتتاحية\n- صيغة الخطاب\n\n"
"الوحدة الثانية: الرد والمتابعة\n"
"- مهلة الرد\n- صيغ الاعتذار\n"
)
run = user.post(
f"/v1/projects/{project_id}/runs",
json={"params": {"variant": "from_text",
"brief": {"blueprint_text": outline, "course_name": "كتاب الفحص"}}},
)
check("run accepted", run.status_code == 202, run.text[:200])
run_id = run.json().get("id") if run.status_code == 202 else None
if run_id is None:
print(" (skipping the run leg - the run was not accepted)")
status = ""
for _ in range(60 if run_id else 0):
time.sleep(3)
detail = user.get(f"/v1/runs/{run_id}")
status = detail.json().get("status", "")
if status not in ("queued", "running"):
break
check("run reaches the review gate", status == "awaiting_review", f"status={status}")
if status == "awaiting_review":
review = user.get(f"/v1/runs/{run_id}/review")
check("review returns the blueprint", review.status_code == 200, review.text[:150])
blueprint = review.json().get("blueprint", {})
# The gate for books sits on book.outline, so the blueprint is
# chapters - built from the units the paste was read into.
parts = blueprint.get("chapters") or blueprint.get("units") or []
check("blueprint carries the structure from the paste",
len(parts) >= 2, f"parts={len(parts)} keys={list(blueprint)[:6]}")
titles = " ".join(str(p.get("chapter_title") or p.get("unit_name") or "")
for p in parts)
check("chapter titles came from the pasted text",
"المراسل" in titles or "الرد" in titles, titles[:120])
cancelled = user.post(f"/v1/runs/{run_id}/cancel")
check("rejecting at the gate works", cancelled.status_code == 200,
cancelled.text[:120])
# The meter flushes every ~5s; give it a beat, then ask the dashboard.
time.sleep(8)
spend = admin.get(f"/v1/admin/users/{user_id}/by-task-type")
rows = spend.json() if spend.status_code == 200 else []
book_row = next((r for r in rows if r["task_type"] == "book"), None)
check("the call was metered and attributed to the user and the task type",
book_row is not None and book_row["total_tokens"] > 0,
f"rows={rows}")
print("== suspension ==")
admin.patch(f"/v1/admin/users/{user_id}", json={"status": "suspended"})
check("suspended account is locked out", user.get("/v1/me").status_code == 403)
admin.patch(f"/v1/admin/users/{user_id}", json={"status": "active"})
check("reactivation restores access", user.get("/v1/me").status_code == 200)
print("== qdrant ==")
try:
from qdrant_client import QdrantClient
q = QdrantClient(url=settings.qdrant_url, api_key=settings.qdrant_api_key,
timeout=20)
collections = q.get_collections().collections
check("qdrant reachable", True)
check("collections exist", len(collections) > 0, f"n={len(collections)}")
populated = None
for c in collections[:40]:
if (q.count(c.name).count or 0) > 0:
populated = c.name
break
if populated:
point = q.scroll(populated, limit=1, with_vectors=True)[0][0]
vector = point.vector if isinstance(point.vector, list) else next(
iter(point.vector.values())
)
hits = q.query_points(populated, query=vector, limit=3).points
check("vector search returns results", len(hits) > 0,
f"collection={populated}")
else:
check("vector search returns results", False, "no populated collection")
except Exception as e: # noqa: BLE001
check("qdrant reachable", False, str(e)[:200])
print("== model key ping (5 tokens) ==")
try:
import litellm
out = litellm.completion(
model="openrouter/openai/gpt-4o-mini",
api_key=settings.openrouter_api_key,
api_base="https://openrouter.ai/api/v1",
messages=[{"role": "user", "content": "قل: جاهز"}],
max_tokens=5,
timeout=30,
)
check("openrouter answers", bool(out.choices[0].message.content))
except Exception as e: # noqa: BLE001
check("openrouter answers", False, str(e)[:200])
print("== cleanup ==")
gone = admin.delete(f"/v1/admin/users/{user_id}")
check("smoke user deleted with their work", gone.status_code == 204, gone.text[:120])
audit = admin.get("/v1/admin/audit")
check("deletion leaves an audit entry",
audit.status_code == 200
and any(e["action"] == "user.delete"
for e in audit.json().get("items", [])),
f"{audit.status_code} {audit.text[:120]}")
print()
print(f"passed: {len(passed)} failed: {len(failed)}")
for f in failed:
print(" FAIL:", f)
return 1 if failed else 0
if __name__ == "__main__":
sys.exit(main())