-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-image-generation.js
More file actions
159 lines (141 loc) Β· 4.41 KB
/
test-image-generation.js
File metadata and controls
159 lines (141 loc) Β· 4.41 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
// Test script for Image Generation functionality
const testImageRequests = [
{
question: "Generate an image of a cat",
expectedBehavior:
"Should detect image generation request and create a cat image",
},
{
question: "Can you draw a beautiful sunset over mountains?",
expectedBehavior: "Should generate a sunset landscape image",
},
{
question: "Create a cartoon-style image of a robot",
expectedBehavior: "Should generate a cartoon robot image",
},
{
question: "Show me a diagram of the solar system",
expectedBehavior: "Should generate a solar system diagram",
},
{
question: "What is photosynthesis?",
expectedBehavior: "Should provide text explanation (not image generation)",
},
];
async function testImageGeneration() {
console.log("π¨ Testing Image Generation Functionality...\n");
for (const test of testImageRequests) {
console.log(`π Testing: "${test.question}"`);
console.log(`π― Expected: ${test.expectedBehavior}`);
try {
const response = await fetch("http://localhost:3000/api/ai/answer", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
question_text: test.question,
question_type: "text",
language: "en",
response_language: "en",
grade: 8,
}),
});
const result = await response.json();
if (result.success) {
console.log(`β
Answer: ${result.data.answer.substring(0, 100)}...`);
if (result.data.isImageGeneration) {
console.log(`πΌοΈ Image Generation: YES`);
if (result.data.generatedImage) {
console.log(
`πΈ Image Data: ${result.data.generatedImage.data.substring(
0,
50
)}...`
);
console.log(`π¨ MIME Type: ${result.data.generatedImage.mimeType}`);
} else {
console.log(`β No image data returned`);
}
} else {
console.log(`π Text Response: YES`);
}
if (
result.data.followUpQuestions &&
result.data.followUpQuestions.length > 0
) {
console.log(
`π Follow-up questions: ${result.data.followUpQuestions.length}`
);
}
} else {
console.log(`β Error: ${result.error}`);
}
} catch (error) {
console.log(`β Request failed: ${error.message}`);
}
console.log("β".repeat(80));
}
}
async function testDirectImageGeneration() {
console.log("π¨ Testing Direct Image Generation API...\n");
const testPrompts = [
"A cute cat sitting on a windowsill",
"A beautiful sunset over mountains",
"A cartoon robot with big eyes",
"A scientific diagram of the solar system",
];
for (const prompt of testPrompts) {
console.log(`π Testing prompt: "${prompt}"`);
try {
const response = await fetch(
"http://localhost:3000/api/ai/generate-image",
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
prompt: prompt,
style: "realistic",
size: "1024x1024",
}),
}
);
const result = await response.json();
if (result.success) {
console.log(`β
Image generated successfully!`);
console.log(
`πΈ Image Data: ${result.data.image.data.substring(0, 50)}...`
);
console.log(`π¨ MIME Type: ${result.data.image.mimeType}`);
console.log(`β° Timestamp: ${result.data.timestamp}`);
} else {
console.log(`β Error: ${result.error}`);
}
} catch (error) {
console.log(`β Request failed: ${error.message}`);
}
console.log("β".repeat(80));
}
}
// Run tests if this script is executed directly
if (require.main === module) {
console.log("π Starting Image Generation Tests...\n");
// Test through the main AI service
testImageGeneration()
.then(() => {
console.log("\n" + "=".repeat(80));
console.log("π¨ Now testing direct image generation API...\n");
return testDirectImageGeneration();
})
.then(() => {
console.log("\nβ
All tests completed!");
})
.catch(console.error);
}
module.exports = {
testImageGeneration,
testDirectImageGeneration,
testImageRequests,
};