Spaces:
Sleeping
Sleeping
| #!/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() | |