Spaces:
Sleeping
Sleeping
| """Validate incremental /upload-room (one file per request + session_id). | |
| Mimics the mobile native uploader against a RUNNING server. | |
| Run: python scripts/upload_test.py | |
| """ | |
| from __future__ import annotations | |
| import io | |
| import sys | |
| import requests | |
| from PIL import Image | |
| BASE = "http://127.0.0.1:8000" | |
| def jpg(color: tuple[int, int, int]) -> bytes: | |
| b = io.BytesIO() | |
| Image.new("RGB", (640, 480), color).save(b, format="JPEG") | |
| return b.getvalue() | |
| def main() -> int: | |
| # First file, no session -> creates a session with 1 image. | |
| r = requests.post( | |
| f"{BASE}/upload-room", | |
| files={"files": ("a.jpg", jpg((150, 140, 130)), "image/jpeg")}, | |
| timeout=30, | |
| ) | |
| print("call 1:", r.status_code, r.text[:160]) | |
| r.raise_for_status() | |
| d = r.json() | |
| sid = d["session_id"] | |
| assert len(d["images"]) == 1, d | |
| # Second file, with session_id form field -> appends (cumulative 2). | |
| r = requests.post( | |
| f"{BASE}/upload-room", | |
| files={"files": ("b.jpg", jpg((120, 130, 140)), "image/jpeg")}, | |
| data={"session_id": sid}, | |
| timeout=30, | |
| ) | |
| print("call 2:", r.status_code, r.text[:160]) | |
| r.raise_for_status() | |
| d2 = r.json() | |
| assert d2["session_id"] == sid, d2 | |
| assert len(d2["images"]) == 2, d2 | |
| print(f"OK: session {sid[:8]} now has {len(d2['images'])} images") | |
| print("UPLOAD INCREMENTAL TEST PASSED") | |
| return 0 | |
| if __name__ == "__main__": | |
| sys.exit(main()) | |