#!/usr/bin/env python
import requests
import os

def test_playground_upload():
    base_url = "http://localhost:8888"
    
    test_file_path = "test_upload.txt"
    test_content = "Este é um arquivo de teste para upload no playground."
    
    try:
        with open(test_file_path, "w", encoding="utf-8") as f:
            f.write(test_content)
        
        print(f"Arquivo de teste criado: {test_file_path}")
        
        session = requests.Session()
        
        login_data = {
            'username': 'teste',
            'password': 'teste123'
        }
        
        login_response = session.post(f"{base_url}/auth/login", data=login_data)
        print(f"Status do login: {login_response.status_code}")
        
        if login_response.status_code != 200:
            print("Erro no login. Verifique suas credenciais.")
            return
        
        with open(test_file_path, "rb") as f:
            files = {'file': (test_file_path, f, 'text/plain')}
            
            upload_response = session.post(
                f"{base_url}/api/playground/upload",
                files=files
            )
        
        print(f"Status do upload: {upload_response.status_code}")
        print(f"Resposta do upload: {upload_response.text}")
        
        if upload_response.status_code == 200:
            print("✅ Upload realizado com sucesso!")
        else:
            print("❌ Erro no upload")
            
    except Exception as e:
        print(f"Erro durante o teste: {str(e)}")
    
    finally:
        if os.path.exists(test_file_path):
            os.remove(test_file_path)
            print(f"Arquivo de teste removido: {test_file_path}")

if __name__ == "__main__":
    test_playground_upload() 