File size: 1,744 Bytes
38ab39c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import asyncio
import io
import time
import requests
import uvicorn
import threading
from PIL import Image
from cora_engine import CoraEngine
from fastapi.testclient import TestClient
from api import app

def create_dummy_image():
    img = Image.new('RGB', (100, 100), color = 'white')
    return img

def test_engine_logic():
    print("\n--- Testing CoraEngine ---")
    try:
        engine = CoraEngine()
        if not engine.client:
            print("FAIL: Engine initialized/HuggingFace Client missing (Check .env)")
            return False
            
        print("PASS: Engine Initialized")
        
        # Test Resizing
        big_img = Image.new('RGB', (2000, 2000), color='white')
        resized = engine.resize_image(big_img, max_size=1024)
        if resized.size != (1024, 1024):
            print(f"FAIL: Resize logic incorrect. Got {resized.size}")
            return False
        print("PASS: Resize Logic")
        return True
    except Exception as e:
        print(f"FAIL: Engine Error - {e}")
        return False

def test_api_logic():
    print("\n--- Testing API (TestClient) ---")
    client = TestClient(app)
    
    # 1. Health Check
    try:
        response = client.get("/health")
        if response.status_code == 200 and response.json().get("status") == "online":
            print("PASS: /health endpoint")
        else:
            print(f"FAIL: /health endpoint - {response.json()}")
    except Exception as e:
        print(f"FAIL: API Error - {e}")

if __name__ == "__main__":
    img = create_dummy_image()
    
    if test_engine_logic():
        test_api_logic()
    else:
        print("Skipping API test due to Engine failure.")