-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_fixes.py
More file actions
216 lines (180 loc) · 7.06 KB
/
test_fixes.py
File metadata and controls
216 lines (180 loc) · 7.06 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/usr/bin/env python3
"""
Script para testar as correções do proxy
"""
import requests
import json
import time
import threading
from concurrent.futures import ThreadPoolExecutor
def test_concurrent_requests():
"""Testa requisições concorrentes para verificar se o erro de headers foi corrigido"""
print("🔄 Testando requisições concorrentes...")
def make_request(i):
try:
response = requests.post(
"http://localhost:11434/v1/chat/completions",
json={
"model": "gemini-2.5-pro",
"messages": [{"role": "user", "content": f"Diga apenas: Teste {i}"}],
"stream": False,
"max_tokens": 50
},
timeout=10
)
return f"Request {i}: Status {response.status_code}"
except Exception as e:
return f"Request {i}: Error {e}"
# Fazer 5 requisições simultâneas
with ThreadPoolExecutor(max_workers=5) as executor:
futures = [executor.submit(make_request, i) for i in range(1, 6)]
results = [future.result() for future in futures]
for result in results:
print(f" {result}")
return all("Status 200" in result for result in results)
def test_content_field():
"""Testa se o campo content está presente na resposta não-streaming"""
print("\n📝 Testando campo content na resposta...")
try:
response = requests.post(
"http://localhost:11434/v1/chat/completions",
json={
"model": "gemini-2.5-pro",
"messages": [{"role": "user", "content": "Responda apenas: Olá mundo"}],
"stream": False,
"max_tokens": 50
},
timeout=15
)
if response.status_code == 200:
data = response.json()
print(f" Resposta completa: {json.dumps(data, indent=2)}")
# Verificar se tem content
if 'choices' in data and len(data['choices']) > 0:
message = data['choices'][0].get('message', {})
content = message.get('content')
if content is not None:
print(f" ✅ Campo content presente: '{content}'")
return True
else:
print(f" ❌ Campo content ausente na message: {message}")
return False
else:
print(f" ❌ Estrutura de choices inválida")
return False
else:
print(f" ❌ Status code: {response.status_code}")
print(f" Resposta: {response.text}")
return False
except Exception as e:
print(f" ❌ Exceção: {e}")
return False
def test_streaming_stability():
"""Testa estabilidade do streaming"""
print("\n🌊 Testando estabilidade do streaming...")
try:
response = requests.post(
"http://localhost:11434/v1/chat/completions",
json={
"model": "gemini-2.5-pro",
"messages": [{"role": "user", "content": "Conte uma história muito curta"}],
"stream": True,
"max_tokens": 100
},
stream=True,
timeout=20
)
if response.status_code == 200:
chunks = []
content_parts = []
for line in response.iter_lines():
if line:
line_str = line.decode('utf-8')
chunks.append(line_str)
if line_str.startswith('data: ') and not line_str.endswith('[DONE]'):
data_str = line_str[6:]
try:
chunk_data = json.loads(data_str)
if 'choices' in chunk_data and len(chunk_data['choices']) > 0:
delta = chunk_data['choices'][0].get('delta', {})
content = delta.get('content', '')
if content:
content_parts.append(content)
except:
pass
full_content = ''.join(content_parts)
print(f" Chunks recebidos: {len(chunks)}")
print(f" Conteúdo completo: '{full_content}'")
return len(chunks) > 0 and len(full_content) > 0
else:
print(f" ❌ Status code: {response.status_code}")
return False
except Exception as e:
print(f" ❌ Exceção: {e}")
return False
def test_error_handling():
"""Testa tratamento de erros"""
print("\n🚨 Testando tratamento de erros...")
# Teste com JSON malformado
try:
response = requests.post(
"http://localhost:11434/v1/chat/completions",
headers={"Content-Type": "application/json"},
data="{ invalid json",
timeout=5
)
if response.status_code == 400:
print(" ✅ JSON malformado rejeitado corretamente")
return True
else:
print(f" ❌ Status inesperado para JSON malformado: {response.status_code}")
return False
except Exception as e:
print(f" ❌ Exceção: {e}")
return False
def main():
print("🔧 TESTANDO CORREÇÕES DO PROXY")
print("=" * 50)
# Verificar se servidor está rodando
try:
response = requests.get("http://localhost:11434/v1/models", timeout=5)
if response.status_code != 200:
print("❌ Servidor não está respondendo")
return
except:
print("❌ Servidor não está acessível")
print("💡 Execute: npm start")
return
tests = [
("Requisições Concorrentes", test_concurrent_requests),
("Campo Content", test_content_field),
("Estabilidade Streaming", test_streaming_stability),
("Tratamento de Erros", test_error_handling)
]
results = {}
for test_name, test_func in tests:
print(f"\n{'='*50}")
try:
results[test_name] = test_func()
except Exception as e:
print(f"❌ Teste {test_name} falhou: {e}")
results[test_name] = False
time.sleep(1) # Pausa entre testes
# Resumo
print(f"\n{'='*50}")
print("📊 RESUMO DOS TESTES DE CORREÇÃO")
print("=" * 50)
passed = 0
total = len(results)
for test_name, result in results.items():
status = "✅ PASSOU" if result else "❌ FALHOU"
print(f"{test_name:25} | {status}")
if result:
passed += 1
print(f"\nResultado: {passed}/{total} testes passaram")
if passed == total:
print("🎉 Todas as correções funcionaram!")
else:
print("⚠️ Algumas correções ainda precisam de ajustes")
if __name__ == "__main__":
main()