"""Verify the whole backend imports without errors (no LLM calls).""" import os import sys import tempfile import warnings from pathlib import Path warnings.filterwarnings("ignore", message="The HMAC key is") sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")) tempdir = tempfile.mkdtemp(prefix="st_import_") os.environ["DATA_DIR"] = tempdir os.environ["JWT_SECRET"] = "test-secret-key-0123456789abcdef" from app.factory import create_app # noqa: E402 from app.config import Config # noqa: E402 Config.DATA_DIR = Path(tempdir) # Force no LLM for import/structural test (analyze path tested separately) Config.LLM_API_KEY = "" Config.LLM_BASE_URL = "" def main(): app = create_app() # LLM should be None (no API key in test env) assert app.extensions["llm"] is None, "expect no LLM in test env" client = app.test_client() # login as super-admin r = client.post("/api/auth/login", json={ "username": "admin", "password": "1234"}) assert r.status_code == 200, r.get_json() token = r.get_json()["token"] H = {"Authorization": f"Bearer {token}"} # create a group via JSON form (no files) r = client.post("/api/groups", json={ "product": "Cloud POS system for small restaurants", "segment": "SME restaurants", "description": "Target Bangkok SME restaurants, 1-3 branches", "channel": "line", "language": "th", }, headers=H) assert r.status_code == 201, r.get_json() gid = r.get_json()["group"]["id"] assert r.get_json()["group"]["status"] == "draft" print("[ok] group created (draft)") # Regression: multipart (like the frontend FormData) with product field but NO file # attached must still create the group (was 400 'provide product info' before fix). r = client.post( "/api/groups", data={"product": "Multipart Product"}, content_type="multipart/form-data", headers=H, ) assert r.status_code == 201, f"multipart create failed: {r.get_json()}" assert r.get_json()["group"]["title"] == "Multipart Product" print("[ok] group created via multipart (product only) — regression fixed") # analyze should fail cleanly (LLM None) r = client.post(f"/api/groups/{gid}/analyze", headers=H) assert r.status_code == 500, r.get_json() print("[ok] analyze fails cleanly when LLM unset (500)") # list groups as admin r = client.get("/api/groups", headers=H) assert r.status_code == 200 and len(r.get_json()["groups"]) >= 1 assert all(g["status"] == "draft" for g in r.get_json()["groups"]) print("[ok] admin lists groups") # personas empty until analyze r = client.get(f"/api/groups/{gid}", headers=H) assert r.get_json()["group"]["personas"] == [] print("[ok] group has no personas before analyze") # trainee created; can list groups but only 'ready' ones (this one is 'failed' -> hidden) client.post("/api/admin/users", json={ "name": "Trainee", "username": "trainee1", "password": "pass123", "role": "user"}, headers=H) r = client.post("/api/auth/login", json={"username": "trainee1", "password": "pass123"}) ut = r.get_json()["token"] UH = {"Authorization": f"Bearer {ut}"} r = client.get("/api/groups", headers=UH) assert r.get_json()["groups"] == [], "trainee should not see non-ready groups" print("[ok] trainee cannot see non-ready groups") print("\nALL M1/M2-IMPORT TESTS PASSED") if __name__ == "__main__": main()