-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-examples.js
More file actions
198 lines (177 loc) · 5.73 KB
/
test-examples.js
File metadata and controls
198 lines (177 loc) · 5.73 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
/**
* Test script for all Rahyana API examples
*
* This script tests all the JavaScript examples to ensure they work correctly
* when the server is running properly.
*/
const API_KEY = 'YOUR_API_KEY_HERE'; // کلید API خود را اینجا قرار دهید
const BASE_URL = 'https://rahyana.ir/api/v1';
async function testServerHealth() {
try {
const response = await fetch('http://localhost:3000/api/health');
if (response.ok) {
const data = await response.json();
console.log('✅ Server is healthy');
console.log('Status:', data.status);
return true;
} else {
console.log('❌ Server health check failed:', response.status);
return false;
}
} catch (error) {
console.log('❌ Server is not running or not accessible');
console.log('Error:', error.message);
return false;
}
}
async function testBasicChat() {
try {
console.log('\n🧪 Testing basic chat...');
const response = await fetch(`${BASE_URL}/chat/completions`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${API_KEY}`,
'Content-Type': 'application/json',
'HTTP-Referer': 'https://test-app.com',
'X-Title': 'Test Example'
},
body: JSON.stringify({
model: 'openai/gpt-4o', // مدل GPT-4o - مدلهای محبوب: openai/gpt-4o, openai/gpt-4o-mini, openai/gpt-5
messages: [
{
role: 'user',
content: 'Hello, test message'
}
],
stream: false,
max_tokens: 10
})
});
if (response.ok) {
const data = await response.json();
console.log('✅ Basic chat test passed');
return true;
} else {
console.log('❌ Basic chat test failed:', response.status);
const error = await response.text();
console.log('Error:', error);
return false;
}
} catch (error) {
console.log('❌ Basic chat test error:', error.message);
return false;
}
}
async function testModels() {
try {
console.log('\n🧪 Testing models endpoint...');
const response = await fetch(`${BASE_URL}/models`, {
method: 'GET',
headers: {
'Authorization': `Bearer ${API_KEY}`,
'Content-Type': 'application/json'
}
});
if (response.ok) {
const data = await response.json();
console.log('✅ Models test passed');
console.log('Found', data.data ? data.data.length : 0, 'models');
return true;
} else {
console.log('❌ Models test failed:', response.status);
const error = await response.text();
console.log('Error:', error);
return false;
}
} catch (error) {
console.log('❌ Models test error:', error.message);
return false;
}
}
async function testKeyInfo() {
try {
console.log('\n🧪 Testing key info endpoint...');
const response = await fetch(`${BASE_URL}/key`, {
method: 'GET',
headers: {
'Authorization': `Bearer ${API_KEY}`,
'Content-Type': 'application/json'
}
});
if (response.ok) {
const data = await response.json();
console.log('✅ Key info test passed');
return true;
} else {
console.log('❌ Key info test failed:', response.status);
const error = await response.text();
console.log('Error:', error);
return false;
}
} catch (error) {
console.log('❌ Key info test error:', error.message);
return false;
}
}
async function testCompletions() {
try {
console.log('\n🧪 Testing completions endpoint...');
const response = await fetch(`${BASE_URL}/completions`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${API_KEY}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
model: 'openai/gpt-4o', // مدل GPT-4o - مدلهای محبوب: openai/gpt-4o, openai/gpt-4o-mini, openai/gpt-5
prompt: 'Test prompt:',
max_tokens: 10
})
});
if (response.ok) {
const data = await response.json();
console.log('✅ Completions test passed');
return true;
} else {
console.log('❌ Completions test failed:', response.status);
const error = await response.text();
console.log('Error:', error);
return false;
}
} catch (error) {
console.log('❌ Completions test error:', error.message);
return false;
}
}
async function runAllTests() {
console.log('🚀 Starting Rahyana API Examples Test Suite');
console.log('==========================================');
const serverHealthy = await testServerHealth();
if (!serverHealthy) {
console.log('\n❌ Server is not healthy. Please start the server and try again.');
console.log('Run: npm run dev or your server start command');
return;
}
const results = {
basicChat: await testBasicChat(),
models: await testModels(),
keyInfo: await testKeyInfo(),
completions: await testCompletions()
};
console.log('\n📊 Test Results Summary');
console.log('=======================');
console.log('Basic Chat:', results.basicChat ? '✅ PASS' : '❌ FAIL');
console.log('Models:', results.models ? '✅ PASS' : '❌ FAIL');
console.log('Key Info:', results.keyInfo ? '✅ PASS' : '❌ FAIL');
console.log('Completions:', results.completions ? '✅ PASS' : '❌ FAIL');
const passedTests = Object.values(results).filter(Boolean).length;
const totalTests = Object.keys(results).length;
console.log(`\n🎯 Overall: ${passedTests}/${totalTests} tests passed`);
if (passedTests === totalTests) {
console.log('🎉 All tests passed! Examples are ready to use.');
} else {
console.log('⚠️ Some tests failed. Check server logs for details.');
}
}
// Run the tests
runAllTests().catch(console.error);