143 lines
4.2 KiB
JavaScript
143 lines
4.2 KiB
JavaScript
// Simple test script for the updated useTranslation composable
|
|
// This can be run in the browser console when the chat client is loaded
|
|
|
|
console.log('Testing updated useTranslation composable...');
|
|
|
|
// Test basic translation functionality
|
|
async function testTranslation() {
|
|
try {
|
|
// Test direct API call to /api/translate
|
|
const response = await fetch('/api/translate', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'X-Requested-With': 'XMLHttpRequest'
|
|
},
|
|
credentials: 'same-origin',
|
|
body: JSON.stringify({
|
|
text: 'Hello world',
|
|
target_lang: 'nl',
|
|
source_lang: 'en',
|
|
context: 'test'
|
|
})
|
|
});
|
|
|
|
if (!response.ok) {
|
|
throw new Error(`HTTP error! status: ${response.status}`);
|
|
}
|
|
|
|
const result = await response.json();
|
|
console.log('✅ Direct API test successful:', result);
|
|
|
|
if (result.success) {
|
|
console.log('✅ Translation successful:', result.translated_text);
|
|
} else {
|
|
console.log('❌ Translation failed:', result.error);
|
|
}
|
|
|
|
return result;
|
|
} catch (error) {
|
|
console.error('❌ Direct API test failed:', error);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
// Test empty text handling
|
|
async function testEmptyText() {
|
|
try {
|
|
const response = await fetch('/api/translate', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'X-Requested-With': 'XMLHttpRequest'
|
|
},
|
|
credentials: 'same-origin',
|
|
body: JSON.stringify({
|
|
text: '',
|
|
target_lang: 'nl'
|
|
})
|
|
});
|
|
|
|
const result = await response.json();
|
|
console.log('✅ Empty text test result:', result);
|
|
|
|
if (!result.success && result.error.includes('empty')) {
|
|
console.log('✅ Empty text validation working correctly');
|
|
}
|
|
|
|
return result;
|
|
} catch (error) {
|
|
console.error('❌ Empty text test failed:', error);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
// Test missing parameters
|
|
async function testMissingParams() {
|
|
try {
|
|
const response = await fetch('/api/translate', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'X-Requested-With': 'XMLHttpRequest'
|
|
},
|
|
credentials: 'same-origin',
|
|
body: JSON.stringify({
|
|
text: 'Hello world'
|
|
// Missing target_lang
|
|
})
|
|
});
|
|
|
|
const result = await response.json();
|
|
console.log('✅ Missing params test result:', result);
|
|
|
|
if (!result.success && result.error.includes('missing')) {
|
|
console.log('✅ Parameter validation working correctly');
|
|
}
|
|
|
|
return result;
|
|
} catch (error) {
|
|
console.error('❌ Missing params test failed:', error);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
// Run all tests
|
|
async function runAllTests() {
|
|
console.log('🚀 Starting translation API tests...');
|
|
|
|
console.log('\n1. Testing basic translation...');
|
|
await testTranslation();
|
|
|
|
console.log('\n2. Testing empty text validation...');
|
|
await testEmptyText();
|
|
|
|
console.log('\n3. Testing missing parameters validation...');
|
|
await testMissingParams();
|
|
|
|
console.log('\n✅ All tests completed!');
|
|
}
|
|
|
|
// Export for manual testing
|
|
if (typeof window !== 'undefined') {
|
|
window.testTranslation = testTranslation;
|
|
window.testEmptyText = testEmptyText;
|
|
window.testMissingParams = testMissingParams;
|
|
window.runAllTests = runAllTests;
|
|
|
|
console.log('Test functions available:');
|
|
console.log('- testTranslation()');
|
|
console.log('- testEmptyText()');
|
|
console.log('- testMissingParams()');
|
|
console.log('- runAllTests()');
|
|
}
|
|
|
|
// Auto-run if in Node.js environment (for CI/CD)
|
|
if (typeof module !== 'undefined' && module.exports) {
|
|
module.exports = {
|
|
testTranslation,
|
|
testEmptyText,
|
|
testMissingParams,
|
|
runAllTests
|
|
};
|
|
} |