-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-image-generation.js
More file actions
147 lines (127 loc) · 4.53 KB
/
test-image-generation.js
File metadata and controls
147 lines (127 loc) · 4.53 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
// Test script for image generation
// Run: node test-image-generation.js
const API_KEY = process.env.OPENROUTER_API_KEY || 'your-api-key-here';
const BACKEND_URL = 'http://localhost:5000';
async function testImageGeneration() {
console.log('🧪 Testing Image Generation with OpenRouter GPT-5 Image Mini\n');
// Test 1: Health check
console.log('Test 1: Health Check');
try {
const healthResponse = await fetch(`${BACKEND_URL}/api/health`);
const healthData = await healthResponse.json();
console.log('✅ Health check:', healthData);
} catch (error) {
console.error('❌ Health check failed:', error.message);
return;
}
// Test 2: Image generation with valid API key
console.log('\nTest 2: Image Generation (with API key)');
try {
const response = await fetch(`${BACKEND_URL}/api/generate-image`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-OpenRouter-API-Key': API_KEY
},
body: JSON.stringify({
prompt: 'A beautiful sunset over mountains',
aspectRatio: '1:1'
})
});
const data = await response.json();
if (!response.ok) {
console.error('❌ Image generation failed:', JSON.stringify(data, null, 2));
return;
}
if (data.generatedImages && data.generatedImages.length > 0) {
const image = data.generatedImages[0];
if (image.image?.imageBytes) {
console.log('✅ Image generated successfully!');
console.log(' Image size (base64):', image.image.imageBytes.length, 'characters');
} else if (image.imageUrl) {
console.log('✅ Image URL received:', image.imageUrl);
} else {
console.error('❌ Unexpected response format:', JSON.stringify(data, null, 2));
}
} else {
console.error('❌ No images in response:', JSON.stringify(data, null, 2));
}
} catch (error) {
console.error('❌ Image generation error:', error.message);
}
// Test 3: Different aspect ratios
console.log('\nTest 3: Different Aspect Ratios');
const aspectRatios = ['1:1', '16:9', '9:16'];
for (const aspectRatio of aspectRatios) {
console.log(`\nTesting aspect ratio: ${aspectRatio}`);
try {
const response = await fetch(`${BACKEND_URL}/api/generate-image`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-OpenRouter-API-Key': API_KEY
},
body: JSON.stringify({
prompt: `Test image for ${aspectRatio} aspect ratio`,
aspectRatio: aspectRatio
})
});
const data = await response.json();
if (response.ok && data.generatedImages && data.generatedImages.length > 0) {
console.log(`✅ ${aspectRatio} - Success`);
} else {
console.error(`❌ ${aspectRatio} - Failed:`, data.error?.message || 'Unknown error');
}
} catch (error) {
console.error(`❌ ${aspectRatio} - Error:`, error.message);
}
}
// Test 4: Error handling (no API key)
console.log('\nTest 4: Error Handling (no API key)');
try {
const response = await fetch(`${BACKEND_URL}/api/generate-image`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
prompt: 'Test without API key',
aspectRatio: '1:1'
})
});
const data = await response.json();
if (!response.ok && data.error) {
console.log('✅ Error handling works:', data.error.message);
} else {
console.error('❌ Expected error but got:', JSON.stringify(data, null, 2));
}
} catch (error) {
console.error('❌ Error handling test failed:', error.message);
}
// Test 5: Empty prompt
console.log('\nTest 5: Empty Prompt Validation');
try {
const response = await fetch(`${BACKEND_URL}/api/generate-image`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-OpenRouter-API-Key': API_KEY
},
body: JSON.stringify({
prompt: '',
aspectRatio: '1:1'
})
});
const data = await response.json();
if (!response.ok && response.status === 400) {
console.log('✅ Empty prompt validation works:', data.error?.message);
} else {
console.error('❌ Expected validation error but got:', JSON.stringify(data, null, 2));
}
} catch (error) {
console.error('❌ Validation test failed:', error.message);
}
console.log('\n✅ All tests completed!');
}
// Run tests
testImageGeneration().catch(console.error);