from fastapi.testclient import TestClient
import pandas as pd
import io
import pyarrow as pa
from main import app

client = TestClient(app)

def test_health_check():
    response = client.get("/health")
    assert response.status_code == 200
    assert response.json() == {"status": "ok", "service": "backend"}

def test_upload_csv():
    # Create a dummy CSV
    csv_content = "name,age\nAlice,30\nBob,25"
    file = ("test.csv", csv_content, "text/csv")
    
    response = client.post("/api/v1/upload", files={"file": file})
    
    assert response.status_code == 200
    assert response.headers["content-type"] == "application/vnd.apache.arrow.stream"
    assert "X-Column-Metadata" in response.headers
    
    # Verify Arrow data
    buffer = io.BytesIO(response.content)
    with pa.ipc.open_stream(buffer) as reader:
        table = reader.read_all()
        df = table.to_pandas()
        assert df.shape == (2, 2)
        assert list(df.columns) == ["name", "age"]

def test_upload_invalid_format():
    file = ("test.txt", "invalid content", "text/plain")
    response = client.post("/api/v1/upload", files={"file": file})
    assert response.status_code == 400
    assert "Only .xlsx, .xls and .csv files are supported" in response.json()["detail"]
