-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-api-enhanced.js
More file actions
416 lines (355 loc) · 14.5 KB
/
test-api-enhanced.js
File metadata and controls
416 lines (355 loc) · 14.5 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
// Enhanced API Test Script - No dependencies required
const API_KEY = "ghFUX4ygnpBf7wX2Ua6ZtTnfFWdEg0JWKQ4envGp";
const BASE_URL = "https://api.sportradar.com/tennis/trial/v3/en";
// Tennis Data Validation Functions
function validateMatchStructure(match) {
const errors = [];
const warnings = [];
// Required fields validation
if (!match.id) errors.push("Missing match ID");
if (!match.tournament?.name) errors.push("Missing tournament name");
if (!match.players || match.players.length !== 2) errors.push("Invalid players array");
if (!match.status) errors.push("Missing match status");
// Player validation
match.players?.forEach((player, index) => {
if (!player.name) errors.push(`Player ${index + 1} missing name`);
if (!player.countryCode) warnings.push(`Player ${index + 1} missing country code`);
});
// Score validation for live/completed matches
if (match.status === 'live' || match.status === 'completed') {
if (!match.score) warnings.push("Live/completed match missing score");
else {
if (!match.score.sets || !Array.isArray(match.score.sets)) {
warnings.push("Score missing sets array");
}
// Validate set scores
match.score.sets?.forEach((set, index) => {
if (typeof set.player1 !== 'number' || typeof set.player2 !== 'number') {
errors.push(`Set ${index + 1} has invalid scores`);
}
if (set.player1 < 0 || set.player2 < 0) {
errors.push(`Set ${index + 1} has negative scores`);
}
});
}
}
return {
isValid: errors.length === 0,
errors,
warnings,
dataQuality: errors.length === 0 ? (warnings.length === 0 ? "excellent" : "good") : "poor"
};
}
// Transform SportRadar API data to our format for testing
function transformApiMatchToStandard(summary) {
const tournament = {
id: summary.sport_event?.tournament?.id || '',
name: summary.sport_event?.tournament?.name || 'Unknown Tournament',
category: determineTournamentCategory(summary.sport_event?.tournament?.name || ''),
surface: summary.sport_event?.venue?.surface || 'Hard',
location: `${summary.sport_event?.venue?.city_name || ''}, ${summary.sport_event?.venue?.country_name || ''}`.replace(', ,', '').trim() || 'Unknown'
};
const competitors = summary.sport_event?.competitors || [];
const players = [
{
id: competitors[0]?.id || '',
name: competitors[0]?.name || 'Player 1',
nationality: competitors[0]?.country || 'Unknown',
countryCode: competitors[0]?.country_code || 'XX'
},
{
id: competitors[1]?.id || '',
name: competitors[1]?.name || 'Player 2',
nationality: competitors[1]?.country || 'Unknown',
countryCode: competitors[1]?.country_code || 'XX'
}
];
return {
id: summary.sport_event?.id || '',
tournament,
round: summary.sport_event?.tournament_round?.name || 'Round 1',
status: mapStatus(summary.sport_event_status?.status),
players,
score: transformScore(summary.sport_event_status),
startTime: summary.sport_event?.start_time,
court: summary.sport_event?.venue?.name
};
}
function determineTournamentCategory(tournamentName) {
const name = tournamentName.toLowerCase();
if (name.includes('atp') || name.includes('masters')) return 'ATP';
if (name.includes('wta')) return 'WTA';
return 'Challenger';
}
function mapStatus(status) {
switch (status?.toLowerCase()) {
case 'live':
case 'inprogress':
return 'live';
case 'ended':
case 'closed':
return 'completed';
case 'cancelled':
return 'cancelled';
default:
return 'scheduled';
}
}
function transformScore(eventStatus) {
if (!eventStatus) return undefined;
let sets = [];
if (eventStatus.period_scores && Array.isArray(eventStatus.period_scores)) {
sets = eventStatus.period_scores.map(set => ({
player1: set.home_score || 0,
player2: set.away_score || 0
}));
}
let games = undefined;
if (eventStatus.home_score !== undefined && eventStatus.away_score !== undefined) {
games = {
player1: eventStatus.home_score,
player2: eventStatus.away_score
};
}
if (sets.length === 0 && !games) return undefined;
return { sets, games };
}
// Performance testing utility
async function measureApiPerformance(endpoint, iterations = 3) {
console.log(`\n📊 Testing performance for ${endpoint}...`);
const times = [];
for (let i = 0; i < iterations; i++) {
const start = Date.now();
try {
const response = await fetch(`${BASE_URL}${endpoint}`, {
headers: {
'accept': 'application/json',
'x-api-key': API_KEY
}
});
if (response.ok) {
await response.json();
const duration = Date.now() - start;
times.push(duration);
console.log(` Attempt ${i + 1}: ${duration}ms`);
} else {
console.log(` Attempt ${i + 1}: Failed (${response.status})`);
}
} catch (error) {
console.log(` Attempt ${i + 1}: Error - ${error.message}`);
}
}
if (times.length === 0) return null;
const performance = {
average: Math.round(times.reduce((a, b) => a + b, 0) / times.length),
min: Math.min(...times),
max: Math.max(...times),
attempts: times.length,
successRate: Math.round((times.length / iterations) * 100)
};
console.log(` 📈 Results: Avg ${performance.average}ms, Success ${performance.successRate}%`);
return performance;
}
async function runComprehensiveTests() {
console.log("🎾 Running Enhanced Tennis API Tests...\n");
const testResults = {
endpoints: {},
validation: {},
performance: {},
dataQuality: {}
};
try {
// Test 1: Live Matches Endpoint
console.log("🔴 Testing Live Matches Endpoint...");
try {
const liveResponse = await fetch(`${BASE_URL}/schedules/live/summaries.json`, {
headers: { 'x-api-key': API_KEY, 'accept': 'application/json' }
});
console.log(`Status: ${liveResponse.status} ${liveResponse.statusText}`);
testResults.endpoints.live = { status: liveResponse.status, success: liveResponse.ok };
if (liveResponse.ok) {
const liveData = await liveResponse.json();
console.log(`✅ Found ${liveData.summaries?.length || 0} live matches`);
// Validate data structure
if (liveData.summaries && liveData.summaries.length > 0) {
const firstMatch = transformApiMatchToStandard(liveData.summaries[0]);
const validation = validateMatchStructure(firstMatch);
console.log(`📋 First match validation: ${validation.isValid ? '✅ Valid' : '❌ Invalid'}`);
console.log(`Data quality: ${validation.dataQuality}`);
if (validation.errors.length > 0) {
console.log(`Errors: ${validation.errors.join(', ')}`);
}
if (validation.warnings.length > 0) {
console.log(`Warnings: ${validation.warnings.join(', ')}`);
}
testResults.validation.live = validation;
// Test data consistency across all matches
let validMatches = 0;
let totalWarnings = 0;
liveData.summaries.forEach((summary, index) => {
const match = transformApiMatchToStandard(summary);
const val = validateMatchStructure(match);
if (val.isValid) validMatches++;
totalWarnings += val.warnings.length;
});
console.log(`📊 Match validation: ${validMatches}/${liveData.summaries.length} valid`);
console.log(`⚠️ Total warnings: ${totalWarnings}`);
testResults.dataQuality.live = {
totalMatches: liveData.summaries.length,
validMatches,
totalWarnings,
validityRate: Math.round((validMatches / liveData.summaries.length) * 100)
};
}
}
} catch (error) {
console.log(`❌ Live matches test failed: ${error.message}`);
testResults.endpoints.live = { error: error.message };
}
// Test 2: Today's Matches
console.log("\n📅 Testing Today's Matches Endpoint...");
try {
const today = new Date().toISOString().split('T')[0];
const todayResponse = await fetch(`${BASE_URL}/schedules/${today}/summaries.json`, {
headers: { 'x-api-key': API_KEY, 'accept': 'application/json' }
});
console.log(`Status: ${todayResponse.status} ${todayResponse.statusText}`);
testResults.endpoints.today = { status: todayResponse.status, success: todayResponse.ok };
if (todayResponse.ok) {
const todayData = await todayResponse.json();
console.log(`✅ Found ${todayData.summaries?.length || 0} matches today`);
if (todayData.summaries && todayData.summaries.length > 0) {
// Analyze match statuses
const statusCounts = {};
todayData.summaries.forEach(summary => {
const status = mapStatus(summary.sport_event_status?.status);
statusCounts[status] = (statusCounts[status] || 0) + 1;
});
console.log("📊 Match statuses:", statusCounts);
testResults.dataQuality.today = { statusCounts, totalMatches: todayData.summaries.length };
}
}
} catch (error) {
console.log(`❌ Today's matches test failed: ${error.message}`);
testResults.endpoints.today = { error: error.message };
}
// Test 3: Rankings Endpoint
console.log("\n🏆 Testing Rankings Endpoint...");
try {
const rankingsResponse = await fetch(`${BASE_URL}/rankings.json`, {
headers: { 'x-api-key': API_KEY, 'accept': 'application/json' }
});
console.log(`Status: ${rankingsResponse.status} ${rankingsResponse.statusText}`);
testResults.endpoints.rankings = { status: rankingsResponse.status, success: rankingsResponse.ok };
if (rankingsResponse.ok) {
const rankingsData = await rankingsResponse.json();
console.log(`✅ Found ${rankingsData.rankings?.length || 0} ranking types`);
if (rankingsData.rankings) {
rankingsData.rankings.forEach(ranking => {
console.log(` 📈 ${ranking.name} (${ranking.gender}): ${ranking.competitor_rankings?.length || 0} players`);
});
// Validate player data consistency
const allPlayers = new Map();
rankingsData.rankings.forEach(ranking => {
ranking.competitor_rankings?.forEach(entry => {
if (entry.competitor) {
const playerId = entry.competitor.id;
if (allPlayers.has(playerId)) {
const existing = allPlayers.get(playerId);
if (existing.name !== entry.competitor.name) {
console.log(`⚠️ Name inconsistency for ${playerId}: "${existing.name}" vs "${entry.competitor.name}"`);
}
} else {
allPlayers.set(playerId, {
name: entry.competitor.name,
country: entry.competitor.country,
countryCode: entry.competitor.country_code
});
}
}
});
});
console.log(`📊 Total unique players across rankings: ${allPlayers.size}`);
testResults.dataQuality.rankings = { uniquePlayers: allPlayers.size };
}
}
} catch (error) {
console.log(`❌ Rankings test failed: ${error.message}`);
testResults.endpoints.rankings = { error: error.message };
}
// Test 4: Performance Tests
console.log("\n⚡ Running Performance Tests...");
const performanceResults = await Promise.allSettled([
measureApiPerformance('/schedules/live/summaries.json'),
measureApiPerformance('/rankings.json'),
measureApiPerformance(`/schedules/${new Date().toISOString().split('T')[0]}/summaries.json`)
]);
testResults.performance = {
live: performanceResults[0].value,
rankings: performanceResults[1].value,
today: performanceResults[2].value
};
// Test 5: Error Handling
console.log("\n🚨 Testing Error Handling...");
// Test invalid competitor ID
try {
const invalidResponse = await fetch(`${BASE_URL}/competitors/invalid-id/profile.json`, {
headers: { 'x-api-key': API_KEY, 'accept': 'application/json' }
});
console.log(`Invalid competitor ID test: ${invalidResponse.status} (expected 400/404)`);
testResults.endpoints.errorHandling = { invalidCompetitor: invalidResponse.status };
} catch (error) {
console.log(`Invalid competitor test error: ${error.message}`);
}
// Test missing API key
try {
const noKeyResponse = await fetch(`${BASE_URL}/rankings.json`, {
headers: { 'accept': 'application/json' }
});
console.log(`No API key test: ${noKeyResponse.status} (expected 401/403)`);
testResults.endpoints.errorHandling = {
...testResults.endpoints.errorHandling,
noApiKey: noKeyResponse.status
};
} catch (error) {
console.log(`No API key test error: ${error.message}`);
}
// Summary
console.log("\n📋 TEST SUMMARY");
console.log("================");
const endpointSuccess = Object.values(testResults.endpoints)
.filter(result => result && result.success)
.length;
const totalEndpoints = Object.keys(testResults.endpoints).length;
console.log(`✅ Endpoints working: ${endpointSuccess}/${totalEndpoints}`);
if (testResults.dataQuality.live) {
console.log(`📊 Live match data quality: ${testResults.dataQuality.live.validityRate}%`);
}
if (testResults.performance.live) {
console.log(`⚡ Live matches avg response: ${testResults.performance.live.average}ms`);
}
if (testResults.performance.rankings) {
console.log(`⚡ Rankings avg response: ${testResults.performance.rankings.average}ms`);
}
console.log("\n🎯 RECOMMENDATIONS:");
console.log("1. ✅ API connectivity is working well");
console.log("2. ✅ Data structure validation is comprehensive");
console.log("3. ⚡ Performance is acceptable for real-time usage");
console.log("4. 📊 Consider caching data for better user experience");
console.log("5. 🔄 Implement real-time refresh for live matches");
return testResults;
} catch (error) {
console.error("💥 Test suite failed:", error);
throw error;
}
}
// Run the tests
runComprehensiveTests()
.then(results => {
console.log("\n🏁 All tests completed successfully!");
process.exit(0);
})
.catch(error => {
console.error("\n💥 Test suite failed:", error.message);
process.exit(1);
});