Spaces:
Running

File size: 12,114 Bytes
b688fe8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# 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())