Spaces:
Sleeping
Sleeping
File size: 4,263 Bytes
6123728 | 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 | #!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
اختبار زر التسجيل - محاكاة ملف صوتي
"""
import requests
import io
import wave
import struct
import random
def create_test_audio():
"""إنشاء ملف صوتي تجريبي"""
# إنشاء صوت تجريبي (silence مع بعض الضوضاء)
duration = 2 # ثانيتين
sample_rate = 44100
samples = duration * sample_rate
# إنشاء بيانات صوتية بسيطة
audio_data = []
for i in range(samples):
# ضوضاء بسيطة
value = int(random.uniform(-1000, 1000))
audio_data.append(value)
# إنشاء ملف WAV في الذاكرة
buffer = io.BytesIO()
with wave.open(buffer, 'wb') as wav_file:
wav_file.setnchannels(1) # Mono
wav_file.setsampwidth(2) # 16-bit
wav_file.setframerate(sample_rate)
# كتابة البيانات
for sample in audio_data:
wav_file.writeframes(struct.pack('<h', sample))
buffer.seek(0)
return buffer
def test_recording_endpoint():
"""اختبار endpoint التسجيل"""
print("🎙️ اختبار endpoint التسجيل...")
try:
# إنشاء ملف صوتي تجريبي
audio_buffer = create_test_audio()
# إعداد البيانات
files = {
'audio_data': ('test_audio.wav', audio_buffer, 'audio/wav')
}
data = {
'markers': '[]',
'target_language': 'ar',
'enable_translation': 'true'
}
print("📤 إرسال طلب التسجيل...")
response = requests.post(
'http://localhost:5001/record',
files=files,
data=data,
timeout=30
)
print(f"Status Code: {response.status_code}")
if response.status_code == 200:
result = response.json()
print(f"Success: {result.get('success')}")
if result.get('success'):
print("✅ التسجيل نجح!")
print(f"Original Text: {result.get('original_text', 'غير موجود')}")
print(f"Translation Enabled: {result.get('translation_enabled')}")
print(f"Target Language: {result.get('target_language')}")
return True
else:
print(f"❌ فشل التسجيل: {result.get('error')}")
return False
else:
print(f"❌ خطأ HTTP: {response.status_code}")
print(f"Response: {response.text}")
return False
except Exception as e:
print(f"❌ خطأ في اختبار التسجيل: {e}")
return False
def test_options_request():
"""اختبار طلب OPTIONS للتسجيل"""
print("\n🔧 اختبار طلب OPTIONS للتسجيل...")
try:
response = requests.options('http://localhost:5001/record', timeout=5)
print(f"Status Code: {response.status_code}")
if response.status_code == 204:
print("✅ طلب OPTIONS نجح")
return True
else:
print(f"❌ مشكلة في طلب OPTIONS: {response.status_code}")
return False
except Exception as e:
print(f"❌ خطأ في طلب OPTIONS: {e}")
return False
def main():
"""الدالة الرئيسية لاختبار التسجيل"""
print("🚀 اختبار وظيفة التسجيل")
print("=" * 50)
# اختبار OPTIONS أولاً
options_ok = test_options_request()
if options_ok:
# اختبار التسجيل الفعلي
recording_ok = test_recording_endpoint()
if recording_ok:
print("\n🎉 جميع اختبارات التسجيل نجحت!")
print("✅ زر التسجيل يعمل بشكل مثالي")
else:
print("\n❌ هناك مشكلة في وظيفة التسجيل")
else:
print("\n❌ مشكلة في CORS للتسجيل")
print("=" * 50)
if __name__ == "__main__":
main()
|