File size: 1,669 Bytes
f02ef17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
330e43a
 
 
 
 
f02ef17
 
 
 
 
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
#!/usr/bin/env python3
"""FastAPI smoke tests for the TTT v1.4 Space app."""
from fastapi.testclient import TestClient
import app as ttt_app
from ttt_codec_v14 import HEADER, MAGIC, NAME_TO_CODEC


def main() -> int:
    client = TestClient(ttt_app.app)
    r = client.get("/health")
    assert r.status_code == 200 and r.json()["packet_format"] == "TTT4"
    print("PASS GET /health")

    original = "TTT v1.4 API — exact bytes 🌏".encode("utf-8")
    encoded = client.post("/encode?mode=auto", content=original)
    assert encoded.status_code == 200, encoded.text
    assert encoded.headers.get("x-ttt-version") == "1.4.0"
    print(f"PASS POST /encode ({len(original)} -> {len(encoded.content)} bytes)")

    decoded = client.post("/decode", content=encoded.content)
    assert decoded.status_code == 200 and decoded.content == original
    print("PASS POST /decode exact byte match")

    bad = client.post("/decode", content=b"not a packet")
    assert bad.status_code == 400
    malformed_gzip = HEADER.pack(MAGIC, NAME_TO_CODEC["gzip"], 12, 0) + b"not gzip"
    bad_payload = client.post("/decode", content=malformed_gzip)
    assert bad_payload.status_code == 400, bad_payload.text
    print("PASS POST /decode rejects invalid/corrupt packets")

    root = client.get("/", follow_redirects=False)
    assert root.status_code in (307, 308) and root.headers.get("location", "").startswith("/ui"), root.status_code
    ui = client.get("/ui/")
    assert ui.status_code == 200, ui.status_code
    print("PASS root redirect and mounted Gradio UI")
    print("ALL TTT v1.4 API TESTS PASSED")
    return 0

if __name__ == "__main__":
    raise SystemExit(main())