-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-server.js
More file actions
593 lines (524 loc) · 19.8 KB
/
test-server.js
File metadata and controls
593 lines (524 loc) · 19.8 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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
/**
* Test script for the overlay image server
* Tests the /overlay endpoint with various parameters
*/
import fetch from 'node-fetch';
import dotenv from 'dotenv';
// Load environment variables
dotenv.config();
const BASE_URL = 'http://localhost:8080';
const API_KEY = process.env.OVERLAY_API_KEY || 'default-api-key-change-in-production';
/**
* Test API key validation
*/
async function testApiKeyValidation() {
console.log('🔐 Testing API key validation...\n');
const testCases = [
{
name: 'Valid API key test',
headers: { 'X-API-Key': API_KEY },
shouldSucceed: true
},
{
name: 'Missing API key test',
headers: {},
shouldSucceed: false
},
{
name: 'Invalid API key test',
headers: { 'X-API-Key': 'invalid-key' },
shouldSucceed: false
}
];
for (const testCase of testCases) {
console.log(`📋 ${testCase.name}`);
try {
const url = new URL('/overlay', BASE_URL);
url.searchParams.set('img', 'https://picsum.photos/100/100');
url.searchParams.set('title', 'API Key Test');
console.log(` URL: ${url.toString()}`);
const response = await fetch(url.toString(), {
headers: testCase.headers
});
if (testCase.shouldSucceed && response.ok) {
console.log(` ✅ Success! API key validation passed`);
} else if (!testCase.shouldSucceed && !response.ok) {
const errorText = await response.text();
console.log(` ✅ Expected error: ${response.status} - ${errorText}`);
} else {
console.log(` ❌ Unexpected result: ${response.status}`);
}
} catch (error) {
console.log(` 💥 Exception: ${error.message}`);
}
console.log('');
}
}
/**
* Test the 2slidesReel endpoint (placeholder implementation)
*/
async function test2SlidesReelEndpoint() {
console.log('🎬 Testing 2slidesReel endpoint...\n');
const testCases = [
{
name: 'Valid 2slidesReel request with all parameters',
params: {
slide1: 'https://picsum.photos/1080/1350?random=1',
slide2: 'https://picsum.photos/2160/1080?random=2',
title1: 'First Slide Title this is the first test video. This is a Instagram image.',
title2: 'Second Slide Title this is the first test video. This is a landscape image. And much more text to display on the second slide.',
duration1: 5,
duration2: 7,
transition: 'fade'
},
shouldSucceed: true // Now implemented!
},
{
name: 'Valid 2slidesReel request with minimal parameters',
params: {
slide1: 'https://picsum.photos/1024/1024?random=3',
slide2: 'https://picsum.photos/3072/1024?random=4'
},
shouldSucceed: true // Now implemented!
},
{
name: 'Missing slide1 parameter',
params: {
slide2: 'https://picsum.photos/1080/1920?random=5',
title2: 'Second Slide Only'
},
shouldSucceed: false
},
{
name: 'Missing slide2 parameter',
params: {
slide1: 'https://picsum.photos/1080/1920?random=6',
title1: 'First Slide Only'
},
shouldSucceed: false
},
{
name: 'Invalid duration (too long)',
params: {
slide1: 'https://picsum.photos/1080/1920?random=7',
slide2: 'https://picsum.photos/1080/1920?random=8',
duration1: 60,
duration2: 4
},
shouldSucceed: false
},
{
name: 'Invalid transition type',
params: {
slide1: 'https://picsum.photos/1080/1920?random=9',
slide2: 'https://picsum.photos/1080/1920?random=10',
transition: 'invalid_transition'
},
shouldSucceed: false
}
];
for (const testCase of testCases) {
console.log(`📋 ${testCase.name}`);
try {
const url = new URL('/2slidesReel', BASE_URL);
Object.entries(testCase.params).forEach(([key, value]) => {
url.searchParams.set(key, value);
});
console.log(` URL: ${url.toString()}`);
const response = await fetch(url.toString(), {
headers: { 'X-API-Key': API_KEY }
});
const responseData = await response.json();
if (testCase.shouldSucceed && response.ok) {
console.log(` ✅ Success! Response: ${JSON.stringify(responseData)}`);
} else if (!testCase.shouldSucceed && !response.ok) {
console.log(` ✅ Expected error: ${response.status} - ${responseData.error || responseData.message}`);
} else if (!testCase.shouldSucceed && response.status === 501) {
console.log(` ✅ Expected 501 Not Implemented: ${responseData.message}`);
} else {
console.log(` ❌ Unexpected result: ${response.status} - ${JSON.stringify(responseData)}`);
}
} catch (error) {
console.log(` 💥 Exception: ${error.message}`);
}
console.log('');
}
}
/**
* Test the storage upload endpoint
*/
async function testStorageUploadEndpoint() {
console.log('📤 Testing storage upload endpoint...\n');
const testCases = [
{
name: 'Valid audio file upload (MP3)',
fileType: 'audio',
fileName: 'test-audio.mp3',
mimeType: 'audio/mpeg',
shouldSucceed: true
},
{
name: 'Valid video file upload (MP4)',
fileType: 'video',
fileName: 'test-video.mp4',
mimeType: 'video/mp4',
shouldSucceed: true
},
{
name: 'Valid audio file upload (WAV)',
fileType: 'audio',
fileName: 'test-audio.wav',
mimeType: 'audio/wav',
shouldSucceed: true
},
{
name: 'Invalid file type (text file)',
fileType: 'text',
fileName: 'test.txt',
mimeType: 'text/plain',
shouldSucceed: false
},
{
name: 'Missing file in request',
fileType: 'none',
fileName: null,
mimeType: null,
shouldSucceed: false
}
];
for (const testCase of testCases) {
console.log(`📋 ${testCase.name}`);
try {
const url = new URL('/store/upload', BASE_URL);
console.log(` URL: ${url.toString()}`);
let response;
if (testCase.fileType === 'none') {
// Test without file
response = await fetch(url.toString(), {
method: 'POST',
headers: { 'X-API-Key': API_KEY }
});
} else {
// Create a mock file buffer for testing
const mockFileBuffer = Buffer.from('mock file content for testing');
const formData = new FormData();
const blob = new Blob([mockFileBuffer], { type: testCase.mimeType });
formData.append('file', blob, testCase.fileName);
response = await fetch(url.toString(), {
method: 'POST',
headers: { 'X-API-Key': API_KEY },
body: formData
});
}
const responseData = await response.json();
if (testCase.shouldSucceed && response.ok) {
console.log(` ✅ Success! Response: ${JSON.stringify(responseData)}`);
// Store the file ID for deletion test
if (responseData.id) {
global.testFileId = responseData.id;
}
} else if (!testCase.shouldSucceed && !response.ok) {
console.log(` ✅ Expected error: ${response.status} - ${responseData.error || responseData.message}`);
} else {
console.log(` ❌ Unexpected result: ${response.status} - ${JSON.stringify(responseData)}`);
}
} catch (error) {
console.log(` 💥 Exception: ${error.message}`);
}
console.log('');
}
}
/**
* Test the storage delete endpoint
*/
async function testStorageDeleteEndpoint() {
console.log('🗑️ Testing storage delete endpoint...\n');
const testCases = [
{
name: 'Delete existing file (if available)',
fileId: global.testFileId || 'test-uuid-that-does-not-exist',
shouldSucceed: !!global.testFileId
},
{
name: 'Delete non-existent file',
fileId: '00000000-0000-0000-0000-000000000000',
shouldSucceed: false
},
{
name: 'Invalid file ID format',
fileId: 'invalid-id-format',
shouldSucceed: false
},
{
name: 'Empty file ID',
fileId: '',
shouldSucceed: false
},
{
name: 'File ID with partial match (should not match)',
fileId: '12345678-1234-1234-1234-123456789ab', // Missing last character
shouldSucceed: false
}
];
for (const testCase of testCases) {
console.log(`📋 ${testCase.name}`);
try {
const url = new URL(`/store/${testCase.fileId}`, BASE_URL);
console.log(` URL: ${url.toString()}`);
const response = await fetch(url.toString(), {
method: 'DELETE',
headers: { 'X-API-Key': API_KEY }
});
const responseData = await response.json();
if (testCase.shouldSucceed && response.ok) {
console.log(` ✅ Success! Response: ${JSON.stringify(responseData)}`);
} else if (!testCase.shouldSucceed && !response.ok) {
console.log(` ✅ Expected error: ${response.status} - ${responseData.error || responseData.message}`);
} else {
console.log(` ❌ Unexpected result: ${response.status} - ${JSON.stringify(responseData)}`);
}
} catch (error) {
console.log(` 💥 Exception: ${error.message}`);
}
console.log('');
}
}
/**
* Test the 3slidesReel endpoint
*/
async function test3SlidesReelEndpoint() {
console.log('🎬 Testing 3slidesReel endpoint...\n');
const testCases = [
{
name: 'Valid 3slidesReel request with all parameters',
params: {
slide1: 'https://picsum.photos/1080/1350?random=1',
slide2: 'https://picsum.photos/2160/1080?random=2',
slide3: 'https://picsum.photos/1080/1920?random=3',
title1: 'First Slide Title this is the first test video. This is a Instagram image.',
title2: 'Second Slide Title this is the first test video. This is a landscape image. And much more text to display on the second slide.',
title3: 'Third Slide Title this is the final slide with a portrait image and some additional text content.',
duration1: 4,
duration2: 5,
duration3: 6,
transition: 'fade'
},
shouldSucceed: true
},
{
name: 'Valid 3slidesReel request with minimal parameters',
params: {
slide1: 'https://picsum.photos/1024/1024?random=4',
slide2: 'https://picsum.photos/3072/1024?random=5',
slide3: 'https://picsum.photos/1024/1024?random=6'
},
shouldSucceed: true
},
{
name: 'Missing slide1 parameter',
params: {
slide2: 'https://picsum.photos/1080/1920?random=7',
slide3: 'https://picsum.photos/1080/1920?random=8',
title2: 'Second Slide Only',
title3: 'Third Slide Only'
},
shouldSucceed: false
},
{
name: 'Missing slide2 parameter',
params: {
slide1: 'https://picsum.photos/1080/1920?random=9',
slide3: 'https://picsum.photos/1080/1920?random=10',
title1: 'First Slide Only',
title3: 'Third Slide Only'
},
shouldSucceed: false
},
{
name: 'Missing slide3 parameter',
params: {
slide1: 'https://picsum.photos/1080/1920?random=11',
slide2: 'https://picsum.photos/1080/1920?random=12',
title1: 'First Slide Only',
title2: 'Second Slide Only'
},
shouldSucceed: false
},
{
name: 'Invalid duration (too long)',
params: {
slide1: 'https://picsum.photos/1080/1920?random=13',
slide2: 'https://picsum.photos/1080/1920?random=14',
slide3: 'https://picsum.photos/1080/1920?random=15',
duration1: 60,
duration2: 4,
duration3: 4
},
shouldSucceed: false
},
{
name: 'Invalid transition type',
params: {
slide1: 'https://picsum.photos/1080/1920?random=16',
slide2: 'https://picsum.photos/1080/1920?random=17',
slide3: 'https://picsum.photos/1080/1920?random=18',
transition: 'invalid_transition'
},
shouldSucceed: false
}
];
for (const testCase of testCases) {
console.log(`📋 ${testCase.name}`);
try {
const url = new URL('/3slidesReel', BASE_URL);
Object.entries(testCase.params).forEach(([key, value]) => {
url.searchParams.set(key, value);
});
console.log(` URL: ${url.toString()}`);
const response = await fetch(url.toString(), {
headers: { 'X-API-Key': API_KEY }
});
const responseData = await response.json();
if (testCase.shouldSucceed && response.ok) {
console.log(` ✅ Success! Response: ${JSON.stringify(responseData)}`);
} else if (!testCase.shouldSucceed && !response.ok) {
console.log(` ✅ Expected error: ${response.status} - ${responseData.error || responseData.message}`);
} else {
console.log(` ❌ Unexpected result: ${response.status} - ${JSON.stringify(responseData)}`);
}
} catch (error) {
console.log(` 💥 Exception: ${error.message}`);
}
console.log('');
}
}
/**
* Test the overlay endpoint with sample data
*/
async function testOverlayEndpoint() {
console.log('🧪 Testing overlay endpoint...\n');
const testCases = [
{
name: 'Basic test with random image',
params: {
img: 'https://picsum.photos/1080/1350',
title: 'This is a test title for the overlay',
source: 'Test Source',
w: 1080,
h: 1350
}
},
{
name: 'Long title test (should wrap to multiple lines)',
params: {
img: 'https://picsum.photos/1080/1350?random=1',
title: 'This is a very long title that should definitely wrap to multiple lines and test the text wrapping functionality of our overlay system',
source: 'Long Title Source',
w: 1080,
h: 1350
}
},
{
name: 'Small image test',
params: {
img: 'https://picsum.photos/300/300?random=2',
title: 'Small Image Test',
source: 'Small Test',
w: 300,
h: 300
}
},
{
name: 'Missing parameters test (should return error)',
params: {
title: 'This should fail',
source: 'Error Test'
}
}
];
for (const testCase of testCases) {
console.log(`📋 ${testCase.name}`);
try {
const url = new URL('/overlay', BASE_URL);
Object.entries(testCase.params).forEach(([key, value]) => {
url.searchParams.set(key, value);
});
console.log(` URL: ${url.toString()}`);
const response = await fetch(url.toString(), {
headers: { 'X-API-Key': API_KEY }
});
if (response.ok) {
const contentType = response.headers.get('content-type');
const contentLength = response.headers.get('content-length');
console.log(` ✅ Success! Content-Type: ${contentType}, Size: ${contentLength} bytes`);
// For successful image responses, we could save them for inspection
if (contentType && contentType.startsWith('image/')) {
console.log(` 🖼️ Image generated successfully`);
}
} else {
const errorText = await response.text();
console.log(` ❌ Error ${response.status}: ${errorText}`);
}
} catch (error) {
console.log(` 💥 Exception: ${error.message}`);
}
console.log('');
}
}
/**
* Test server health
*/
async function testServerHealth() {
console.log('🏥 Testing server health...\n');
try {
// Test health check endpoint first (no API key required)
const healthResponse = await fetch(`${BASE_URL}/healthz`);
if (healthResponse.ok) {
const healthData = await healthResponse.json();
console.log('✅ Health check endpoint responding');
console.log(` Version: ${healthData.version}`);
console.log(` Available endpoints: ${healthData.endpoints.join(', ')}`);
} else {
console.log(`❌ Health check failed: ${healthResponse.status}`);
return false;
}
// Test overlay endpoint with API key
const response = await fetch(`${BASE_URL}/overlay?img=https://picsum.photos/100/100&title=health&source=test`, {
headers: { 'X-API-Key': API_KEY }
});
if (response.ok) {
console.log('✅ Server is healthy and responding');
return true;
} else {
console.log(`❌ Server returned error: ${response.status}`);
return false;
}
} catch (error) {
console.log(`💥 Server connection failed: ${error.message}`);
console.log(' Make sure the server is running with: npm run dev');
return false;
}
}
/**
* Main test function
*/
async function runTests() {
console.log('🚀 Starting overlay server tests\n');
console.log('='.repeat(50));
const isHealthy = await testServerHealth();
if (isHealthy) {
await testApiKeyValidation();
await testOverlayEndpoint();
await test2SlidesReelEndpoint();
await test3SlidesReelEndpoint();
await testStorageUploadEndpoint();
await testStorageDeleteEndpoint();
}
console.log('='.repeat(50));
console.log('✨ Tests completed!');
}
// Run tests if this file is executed directly
if (import.meta.url === `file://${process.argv[1]}`) {
runTests().catch(console.error);
}
export { testApiKeyValidation, test2SlidesReelEndpoint, test3SlidesReelEndpoint, testOverlayEndpoint, testStorageUploadEndpoint, testStorageDeleteEndpoint, testServerHealth, runTests };