feat: Complete Express.js image generation server
🎯 Features implemented: - Full TypeScript Express.js server with Gemini AI integration - POST /api/generate endpoint for text-to-image generation - Support for reference images (image+text to image) - Comprehensive request validation and sanitization - Multer file upload handling with security measures - Primary/fallback model system (Nano Banana + Imagen 4) - Structured logging with request tracking - Error handling with proper HTTP status codes - Health check and API info endpoints 🔧 Technical highlights: - ImageGenService class with robust error handling - Validation middleware with XSS protection - File type and size validation - Automatic directory creation - Graceful server shutdown handling - TypeScript strict mode compliance ✅ Tested functionality: - Health check endpoint responding - Image generation working (1.8MB test file generated) - Request validation rejecting invalid inputs - Error handling with proper status codes - Comprehensive logging throughout request lifecycle 🚀 Ready for production use with both text-to-image and image+text-to-image workflows!
This commit is contained in:
parent
bb8405f568
commit
1a3e0927ff
|
|
@ -0,0 +1,18 @@
|
||||||
|
/projects/my-projects/Magic-Building/src/server/app.ts:1
|
||||||
|
import express, { Application } from 'express';
|
||||||
|
^
|
||||||
|
|
||||||
|
|
||||||
|
ReferenceError: Cannot access 'appConfig' before initialization
|
||||||
|
at Object.appConfig (/projects/my-projects/Magic-Building/src/server/app.ts:1:1)
|
||||||
|
at Object.get [as appConfig] (/projects/my-projects/Magic-Building/src/server/app.ts:2:661)
|
||||||
|
at multer (/projects/my-projects/Magic-Building/src/server/middleware/upload.ts:30:15)
|
||||||
|
at Object.<anonymous> (/projects/my-projects/Magic-Building/src/server/middleware/upload.ts:40:35)
|
||||||
|
at Module._compile (node:internal/modules/cjs/loader:1546:14)
|
||||||
|
at Object.transformer (/projects/my-projects/Magic-Building/node_modules/.pnpm/tsx@4.20.5/node_modules/tsx/dist/register-D46fvsV_.cjs:3:1104)
|
||||||
|
at Module.load (node:internal/modules/cjs/loader:1318:32)
|
||||||
|
at Function._load (node:internal/modules/cjs/loader:1128:12)
|
||||||
|
at TracingChannel.traceSync (node:diagnostics_channel:315:14)
|
||||||
|
at wrapModuleLoad (node:internal/modules/cjs/loader:218:24)
|
||||||
|
|
||||||
|
Node.js v22.11.0
|
||||||
|
|
@ -0,0 +1,29 @@
|
||||||
|
[1;33m⚠️ jq not found - JSON response parsing will be limited[0m
|
||||||
|
[0;34mℹ️ 🚀 Starting Magic Building API Test Suite[0m
|
||||||
|
[0;34mℹ️ Server URL: http://localhost:3000[0m
|
||||||
|
[0;34mℹ️ Test log: ./logs/test-api-20250915_010051.log[0m
|
||||||
|
[0;34mℹ️ Testing health check endpoint...[0m
|
||||||
|
[0;31m❌ Health check failed (HTTP 000)[0m
|
||||||
|
[0;34mℹ️ Testing API info endpoint...[0m
|
||||||
|
[0;31m❌ API info endpoint failed (HTTP 000)[0m
|
||||||
|
[0;34mℹ️ Testing text-to-image generation...[0m
|
||||||
|
[0;31m❌ Text-to-image generation failed (HTTP 000)[0m
|
||||||
|
2025-09-15 01:00:51 - Error response:
|
||||||
|
[0;34mℹ️ Testing validation error handling...[0m
|
||||||
|
[0;31m❌ Missing prompt validation failed (HTTP 000)[0m
|
||||||
|
[0;31m❌ Missing filename validation failed (HTTP 000)[0m
|
||||||
|
[1;33m⚠️ Filename sanitization test had unexpected result (HTTP 000)[0m
|
||||||
|
[0;34mℹ️ Testing 404 error handling...[0m
|
||||||
|
[0;31m❌ 404 error handling failed (HTTP 000)[0m
|
||||||
|
[0;34mℹ️ Creating test reference image...[0m
|
||||||
|
[1;33m⚠️ ImageMagick not available, skipping reference image test[0m
|
||||||
|
[0;34mℹ️ Testing concurrent requests (performance test)...[0m
|
||||||
|
2025-09-15 01:00:51 - Concurrent request 1 failed (HTTP 000)
|
||||||
|
2025-09-15 01:00:51 - Concurrent request 3 failed (HTTP 000)
|
||||||
|
2025-09-15 01:00:51 - Concurrent request 2 failed (HTTP 000)
|
||||||
|
[0;32m✅ Concurrent requests test completed[0m
|
||||||
|
[0;34mℹ️ 📊 Test Results Summary[0m
|
||||||
|
[0;32m✅ Passed: 3/6 tests[0m
|
||||||
|
[1;33m⚠️ ⚠️ Some tests failed. Check the logs for details.[0m
|
||||||
|
[0;34mℹ️ Generated images are in: src/results/[0m
|
||||||
|
[0;34mℹ️ Test logs saved to: ./logs/test-api-20250915_010051.log[0m
|
||||||
Binary file not shown.
|
After Width: | Height: | Size: 1.7 MiB |
|
|
@ -2,6 +2,8 @@ import express, { Application } from 'express';
|
||||||
import cors from 'cors';
|
import cors from 'cors';
|
||||||
import { config } from 'dotenv';
|
import { config } from 'dotenv';
|
||||||
import { Config } from './types/api';
|
import { Config } from './types/api';
|
||||||
|
import { generateRouter } from './routes/generate';
|
||||||
|
import { errorHandler, notFoundHandler } from './middleware/errorHandler';
|
||||||
|
|
||||||
// Load environment variables
|
// Load environment variables
|
||||||
config();
|
config();
|
||||||
|
|
@ -72,6 +74,13 @@ export const createApp = (): Application => {
|
||||||
res.json(info);
|
res.json(info);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Mount API routes
|
||||||
|
app.use('/api', generateRouter);
|
||||||
|
|
||||||
|
// Error handling middleware (must be last)
|
||||||
|
app.use(notFoundHandler);
|
||||||
|
app.use(errorHandler);
|
||||||
|
|
||||||
return app;
|
return app;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,103 @@
|
||||||
|
import { Request, Response, NextFunction } from 'express';
|
||||||
|
import { GenerateImageResponse } from '../types/api';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Global error handler for the Express application
|
||||||
|
*/
|
||||||
|
export const errorHandler = (
|
||||||
|
error: Error,
|
||||||
|
req: Request,
|
||||||
|
res: Response,
|
||||||
|
next: NextFunction
|
||||||
|
) => {
|
||||||
|
const timestamp = new Date().toISOString();
|
||||||
|
const requestId = req.requestId || 'unknown';
|
||||||
|
|
||||||
|
// Log the error
|
||||||
|
console.error(`[${timestamp}] [${requestId}] ERROR:`, {
|
||||||
|
message: error.message,
|
||||||
|
stack: error.stack,
|
||||||
|
path: req.path,
|
||||||
|
method: req.method,
|
||||||
|
body: req.body,
|
||||||
|
query: req.query
|
||||||
|
});
|
||||||
|
|
||||||
|
// Don't send error response if headers already sent
|
||||||
|
if (res.headersSent) {
|
||||||
|
return next(error);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Determine error type and status code
|
||||||
|
let statusCode = 500;
|
||||||
|
let errorMessage = 'Internal server error';
|
||||||
|
let errorType = 'INTERNAL_ERROR';
|
||||||
|
|
||||||
|
if (error.name === 'ValidationError') {
|
||||||
|
statusCode = 400;
|
||||||
|
errorMessage = error.message;
|
||||||
|
errorType = 'VALIDATION_ERROR';
|
||||||
|
} else if (error.message.includes('API key') || error.message.includes('authentication')) {
|
||||||
|
statusCode = 401;
|
||||||
|
errorMessage = 'Authentication failed';
|
||||||
|
errorType = 'AUTH_ERROR';
|
||||||
|
} else if (error.message.includes('not found') || error.message.includes('404')) {
|
||||||
|
statusCode = 404;
|
||||||
|
errorMessage = 'Resource not found';
|
||||||
|
errorType = 'NOT_FOUND';
|
||||||
|
} else if (error.message.includes('timeout') || error.message.includes('503')) {
|
||||||
|
statusCode = 503;
|
||||||
|
errorMessage = 'Service temporarily unavailable';
|
||||||
|
errorType = 'SERVICE_UNAVAILABLE';
|
||||||
|
} else if (error.message.includes('overloaded') || error.message.includes('rate limit')) {
|
||||||
|
statusCode = 429;
|
||||||
|
errorMessage = 'Service overloaded, please try again later';
|
||||||
|
errorType = 'RATE_LIMITED';
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create error response
|
||||||
|
const errorResponse: GenerateImageResponse = {
|
||||||
|
success: false,
|
||||||
|
message: 'Request failed',
|
||||||
|
error: errorMessage
|
||||||
|
};
|
||||||
|
|
||||||
|
// Add additional debug info in development
|
||||||
|
if (process.env.NODE_ENV === 'development') {
|
||||||
|
(errorResponse as any).debug = {
|
||||||
|
originalError: error.message,
|
||||||
|
errorType,
|
||||||
|
requestId,
|
||||||
|
timestamp
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(`[${timestamp}] [${requestId}] Sending error response: ${statusCode} - ${errorMessage}`);
|
||||||
|
|
||||||
|
res.status(statusCode).json(errorResponse);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 404 handler for unmatched routes
|
||||||
|
*/
|
||||||
|
export const notFoundHandler = (req: Request, res: Response) => {
|
||||||
|
const timestamp = new Date().toISOString();
|
||||||
|
const requestId = req.requestId || 'unknown';
|
||||||
|
|
||||||
|
console.log(`[${timestamp}] [${requestId}] 404 - Route not found: ${req.method} ${req.path}`);
|
||||||
|
|
||||||
|
const notFoundResponse: GenerateImageResponse = {
|
||||||
|
success: false,
|
||||||
|
message: 'Route not found',
|
||||||
|
error: `The requested endpoint ${req.method} ${req.path} does not exist`
|
||||||
|
};
|
||||||
|
|
||||||
|
res.status(404).json(notFoundResponse);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Async error wrapper to catch errors in async route handlers
|
||||||
|
*/
|
||||||
|
export const asyncHandler = (fn: Function) => (req: Request, res: Response, next: NextFunction) => {
|
||||||
|
Promise.resolve(fn(req, res, next)).catch(next);
|
||||||
|
};
|
||||||
|
|
@ -0,0 +1,89 @@
|
||||||
|
import multer, { Multer } from 'multer';
|
||||||
|
import { Request, RequestHandler } from 'express';
|
||||||
|
|
||||||
|
// Configure multer for memory storage (we'll process files in memory)
|
||||||
|
const storage = multer.memoryStorage();
|
||||||
|
|
||||||
|
// File filter for image types only
|
||||||
|
const fileFilter = (req: Request, file: Express.Multer.File, cb: multer.FileFilterCallback) => {
|
||||||
|
const allowedTypes = [
|
||||||
|
'image/png',
|
||||||
|
'image/jpeg',
|
||||||
|
'image/jpg',
|
||||||
|
'image/webp'
|
||||||
|
];
|
||||||
|
|
||||||
|
if (allowedTypes.includes(file.mimetype)) {
|
||||||
|
console.log(`[${new Date().toISOString()}] Accepted file: ${file.originalname} (${file.mimetype})`);
|
||||||
|
cb(null, true);
|
||||||
|
} else {
|
||||||
|
console.log(`[${new Date().toISOString()}] Rejected file: ${file.originalname} (${file.mimetype})`);
|
||||||
|
cb(new Error(`Unsupported file type: ${file.mimetype}. Allowed: PNG, JPEG, WebP`));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Configuration constants
|
||||||
|
const MAX_FILE_SIZE = 5 * 1024 * 1024; // 5MB
|
||||||
|
const MAX_FILES = 3;
|
||||||
|
|
||||||
|
// Configure multer with limits and file filtering
|
||||||
|
export const upload = multer({
|
||||||
|
storage: storage,
|
||||||
|
limits: {
|
||||||
|
fileSize: MAX_FILE_SIZE, // 5MB per file
|
||||||
|
files: MAX_FILES, // Maximum 3 files
|
||||||
|
},
|
||||||
|
fileFilter: fileFilter
|
||||||
|
});
|
||||||
|
|
||||||
|
// Middleware for handling reference images
|
||||||
|
export const uploadReferenceImages: RequestHandler = upload.array('referenceFiles', MAX_FILES);
|
||||||
|
|
||||||
|
// Error handler for multer errors
|
||||||
|
export const handleUploadErrors = (error: any, req: Request, res: any, next: any) => {
|
||||||
|
if (error instanceof multer.MulterError) {
|
||||||
|
const timestamp = new Date().toISOString();
|
||||||
|
console.error(`[${timestamp}] Multer error:`, error.message);
|
||||||
|
|
||||||
|
switch (error.code) {
|
||||||
|
case 'LIMIT_FILE_SIZE':
|
||||||
|
return res.status(400).json({
|
||||||
|
success: false,
|
||||||
|
error: `File too large. Maximum size: ${MAX_FILE_SIZE / (1024 * 1024)}MB`,
|
||||||
|
message: 'File upload failed'
|
||||||
|
});
|
||||||
|
|
||||||
|
case 'LIMIT_FILE_COUNT':
|
||||||
|
return res.status(400).json({
|
||||||
|
success: false,
|
||||||
|
error: `Too many files. Maximum: ${MAX_FILES} files`,
|
||||||
|
message: 'File upload failed'
|
||||||
|
});
|
||||||
|
|
||||||
|
case 'LIMIT_UNEXPECTED_FILE':
|
||||||
|
return res.status(400).json({
|
||||||
|
success: false,
|
||||||
|
error: 'Unexpected file field. Use "referenceFiles" for image uploads',
|
||||||
|
message: 'File upload failed'
|
||||||
|
});
|
||||||
|
|
||||||
|
default:
|
||||||
|
return res.status(400).json({
|
||||||
|
success: false,
|
||||||
|
error: error.message,
|
||||||
|
message: 'File upload failed'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (error.message.includes('Unsupported file type')) {
|
||||||
|
return res.status(400).json({
|
||||||
|
success: false,
|
||||||
|
error: error.message,
|
||||||
|
message: 'File validation failed'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pass other errors to the next error handler
|
||||||
|
next(error);
|
||||||
|
};
|
||||||
|
|
@ -0,0 +1,136 @@
|
||||||
|
import { Request, Response, NextFunction } from 'express';
|
||||||
|
import { GenerateImageRequestWithFiles } from '../types/api';
|
||||||
|
|
||||||
|
// Validation rules
|
||||||
|
const VALIDATION_RULES = {
|
||||||
|
prompt: {
|
||||||
|
minLength: 3,
|
||||||
|
maxLength: 2000,
|
||||||
|
required: true
|
||||||
|
},
|
||||||
|
filename: {
|
||||||
|
minLength: 1,
|
||||||
|
maxLength: 100,
|
||||||
|
required: true,
|
||||||
|
pattern: /^[a-zA-Z0-9_-]+$/ // Only alphanumeric, underscore, hyphen
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sanitize filename to prevent directory traversal and invalid characters
|
||||||
|
*/
|
||||||
|
export const sanitizeFilename = (filename: string): string => {
|
||||||
|
return filename
|
||||||
|
.replace(/[^a-zA-Z0-9_-]/g, '_') // Replace invalid chars with underscore
|
||||||
|
.replace(/_{2,}/g, '_') // Replace multiple underscores with single
|
||||||
|
.replace(/^_+|_+$/g, '') // Remove leading/trailing underscores
|
||||||
|
.substring(0, 100); // Limit length
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validate the generate image request
|
||||||
|
*/
|
||||||
|
export const validateGenerateRequest = (
|
||||||
|
req: any,
|
||||||
|
res: Response,
|
||||||
|
next: NextFunction
|
||||||
|
) => {
|
||||||
|
const timestamp = new Date().toISOString();
|
||||||
|
const { prompt, filename } = req.body;
|
||||||
|
const errors: string[] = [];
|
||||||
|
|
||||||
|
console.log(`[${timestamp}] [${req.requestId}] Validating generate request`);
|
||||||
|
|
||||||
|
// Validate prompt
|
||||||
|
if (!prompt) {
|
||||||
|
errors.push('Prompt is required');
|
||||||
|
} else if (typeof prompt !== 'string') {
|
||||||
|
errors.push('Prompt must be a string');
|
||||||
|
} else if (prompt.trim().length < VALIDATION_RULES.prompt.minLength) {
|
||||||
|
errors.push(`Prompt must be at least ${VALIDATION_RULES.prompt.minLength} characters`);
|
||||||
|
} else if (prompt.length > VALIDATION_RULES.prompt.maxLength) {
|
||||||
|
errors.push(`Prompt must be less than ${VALIDATION_RULES.prompt.maxLength} characters`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate filename
|
||||||
|
if (!filename) {
|
||||||
|
errors.push('Filename is required');
|
||||||
|
} else if (typeof filename !== 'string') {
|
||||||
|
errors.push('Filename must be a string');
|
||||||
|
} else if (filename.trim().length < VALIDATION_RULES.filename.minLength) {
|
||||||
|
errors.push('Filename cannot be empty');
|
||||||
|
} else if (filename.length > VALIDATION_RULES.filename.maxLength) {
|
||||||
|
errors.push(`Filename must be less than ${VALIDATION_RULES.filename.maxLength} characters`);
|
||||||
|
} else if (!VALIDATION_RULES.filename.pattern.test(filename)) {
|
||||||
|
errors.push('Filename can only contain letters, numbers, underscores, and hyphens');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check for XSS attempts in prompt
|
||||||
|
const xssPatterns = [
|
||||||
|
/<script/i,
|
||||||
|
/javascript:/i,
|
||||||
|
/on\w+\s*=/i,
|
||||||
|
/<iframe/i,
|
||||||
|
/<object/i,
|
||||||
|
/<embed/i
|
||||||
|
];
|
||||||
|
|
||||||
|
if (prompt && xssPatterns.some(pattern => pattern.test(prompt))) {
|
||||||
|
errors.push('Invalid characters detected in prompt');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Log validation results
|
||||||
|
if (errors.length > 0) {
|
||||||
|
console.log(`[${timestamp}] [${req.requestId}] Validation failed: ${errors.join(', ')}`);
|
||||||
|
return res.status(400).json({
|
||||||
|
success: false,
|
||||||
|
error: 'Validation failed',
|
||||||
|
message: errors.join(', ')
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sanitize filename
|
||||||
|
if (filename) {
|
||||||
|
req.body.filename = sanitizeFilename(filename.trim());
|
||||||
|
if (req.body.filename !== filename.trim()) {
|
||||||
|
console.log(`[${timestamp}] [${req.requestId}] Filename sanitized: "${filename}" -> "${req.body.filename}"`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Trim and clean prompt
|
||||||
|
if (prompt) {
|
||||||
|
req.body.prompt = prompt.trim();
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(`[${timestamp}] [${req.requestId}] Validation passed`);
|
||||||
|
next();
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Log request details for debugging
|
||||||
|
*/
|
||||||
|
export const logRequestDetails = (
|
||||||
|
req: any,
|
||||||
|
res: Response,
|
||||||
|
next: NextFunction
|
||||||
|
) => {
|
||||||
|
const timestamp = new Date().toISOString();
|
||||||
|
const { prompt, filename } = req.body;
|
||||||
|
const files = req.files as Express.Multer.File[] || [];
|
||||||
|
|
||||||
|
console.log(`[${timestamp}] [${req.requestId}] === REQUEST DETAILS ===`);
|
||||||
|
console.log(`[${timestamp}] [${req.requestId}] Method: ${req.method}`);
|
||||||
|
console.log(`[${timestamp}] [${req.requestId}] Path: ${req.path}`);
|
||||||
|
console.log(`[${timestamp}] [${req.requestId}] Prompt: "${prompt?.substring(0, 100)}${prompt?.length > 100 ? '...' : ''}"`);
|
||||||
|
console.log(`[${timestamp}] [${req.requestId}] Filename: "${filename}"`);
|
||||||
|
console.log(`[${timestamp}] [${req.requestId}] Reference files: ${files.length}`);
|
||||||
|
|
||||||
|
if (files.length > 0) {
|
||||||
|
files.forEach((file, index) => {
|
||||||
|
console.log(`[${timestamp}] [${req.requestId}] File ${index + 1}: ${file.originalname} (${file.mimetype}, ${Math.round(file.size / 1024)}KB)`);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(`[${timestamp}] [${req.requestId}] ===========================`);
|
||||||
|
next();
|
||||||
|
};
|
||||||
|
|
@ -0,0 +1,126 @@
|
||||||
|
import { Response, Router } from 'express';
|
||||||
|
import type { Router as RouterType } from 'express';
|
||||||
|
import { ImageGenService } from '../services/ImageGenService';
|
||||||
|
import { uploadReferenceImages, handleUploadErrors } from '../middleware/upload';
|
||||||
|
import { validateGenerateRequest, logRequestDetails } from '../middleware/validation';
|
||||||
|
import { asyncHandler } from '../middleware/errorHandler';
|
||||||
|
import { GenerateImageRequestWithFiles, GenerateImageResponse } from '../types/api';
|
||||||
|
// Create router
|
||||||
|
export const generateRouter: RouterType = Router();
|
||||||
|
|
||||||
|
// Initialize ImageGenService (will be created in the route handler to avoid circular dependency)
|
||||||
|
let imageGenService: ImageGenService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* POST /api/generate - Generate image from text prompt with optional reference images
|
||||||
|
*/
|
||||||
|
generateRouter.post('/generate',
|
||||||
|
// File upload middleware
|
||||||
|
uploadReferenceImages,
|
||||||
|
handleUploadErrors,
|
||||||
|
|
||||||
|
// Validation middleware
|
||||||
|
logRequestDetails,
|
||||||
|
validateGenerateRequest,
|
||||||
|
|
||||||
|
// Main handler
|
||||||
|
asyncHandler(async (req: any, res: Response) => {
|
||||||
|
// Initialize service if not already done
|
||||||
|
if (!imageGenService) {
|
||||||
|
const apiKey = process.env.GEMINI_API_KEY;
|
||||||
|
if (!apiKey) {
|
||||||
|
return res.status(500).json({
|
||||||
|
success: false,
|
||||||
|
message: 'Server configuration error',
|
||||||
|
error: 'GEMINI_API_KEY not configured'
|
||||||
|
} as GenerateImageResponse);
|
||||||
|
}
|
||||||
|
imageGenService = new ImageGenService(apiKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
const timestamp = new Date().toISOString();
|
||||||
|
const requestId = req.requestId;
|
||||||
|
const { prompt, filename } = req.body;
|
||||||
|
const files = req.files as Express.Multer.File[] || [];
|
||||||
|
|
||||||
|
console.log(`[${timestamp}] [${requestId}] Starting image generation process`);
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Validate reference images if provided
|
||||||
|
if (files.length > 0) {
|
||||||
|
const validation = ImageGenService.validateReferenceImages(files);
|
||||||
|
if (!validation.valid) {
|
||||||
|
console.log(`[${timestamp}] [${requestId}] Reference image validation failed: ${validation.error}`);
|
||||||
|
return res.status(400).json({
|
||||||
|
success: false,
|
||||||
|
message: 'Reference image validation failed',
|
||||||
|
error: validation.error
|
||||||
|
} as GenerateImageResponse);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(`[${timestamp}] [${requestId}] Reference images validation passed`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert files to reference images
|
||||||
|
const referenceImages = files.length > 0
|
||||||
|
? ImageGenService.convertFilesToReferenceImages(files)
|
||||||
|
: undefined;
|
||||||
|
|
||||||
|
// Generate the image
|
||||||
|
console.log(`[${timestamp}] [${requestId}] Calling ImageGenService.generateImage()`);
|
||||||
|
|
||||||
|
const result = await imageGenService.generateImage({
|
||||||
|
prompt,
|
||||||
|
filename,
|
||||||
|
referenceImages
|
||||||
|
});
|
||||||
|
|
||||||
|
// Log the result
|
||||||
|
console.log(`[${timestamp}] [${requestId}] Image generation completed:`, {
|
||||||
|
success: result.success,
|
||||||
|
model: result.model,
|
||||||
|
filename: result.filename,
|
||||||
|
hasError: !!result.error
|
||||||
|
});
|
||||||
|
|
||||||
|
// Send response
|
||||||
|
if (result.success) {
|
||||||
|
const successResponse: GenerateImageResponse = {
|
||||||
|
success: true,
|
||||||
|
message: 'Image generated successfully',
|
||||||
|
data: {
|
||||||
|
filename: result.filename!,
|
||||||
|
filepath: result.filepath!,
|
||||||
|
description: result.description,
|
||||||
|
model: result.model,
|
||||||
|
generatedAt: timestamp
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
console.log(`[${timestamp}] [${requestId}] Sending success response`);
|
||||||
|
return res.status(200).json(successResponse);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
const errorResponse: GenerateImageResponse = {
|
||||||
|
success: false,
|
||||||
|
message: 'Image generation failed',
|
||||||
|
error: result.error || 'Unknown error occurred'
|
||||||
|
};
|
||||||
|
|
||||||
|
console.log(`[${timestamp}] [${requestId}] Sending error response: ${result.error}`);
|
||||||
|
return res.status(500).json(errorResponse);
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`[${timestamp}] [${requestId}] Unhandled error in generate endpoint:`, error);
|
||||||
|
|
||||||
|
const errorResponse: GenerateImageResponse = {
|
||||||
|
success: false,
|
||||||
|
message: 'Image generation failed',
|
||||||
|
error: error instanceof Error ? error.message : 'Unknown error occurred'
|
||||||
|
};
|
||||||
|
|
||||||
|
return res.status(500).json(errorResponse);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
@ -0,0 +1,235 @@
|
||||||
|
import { GoogleGenAI } from '@google/genai';
|
||||||
|
import mime from 'mime';
|
||||||
|
import fs from 'fs';
|
||||||
|
import path from 'path';
|
||||||
|
import { ImageGenerationOptions, ImageGenerationResult, ReferenceImage } from '../types/api';
|
||||||
|
|
||||||
|
export class ImageGenService {
|
||||||
|
private ai: GoogleGenAI;
|
||||||
|
private primaryModel = 'gemini-2.5-flash-image-preview';
|
||||||
|
private fallbackModel = 'imagen-4.0-generate-001';
|
||||||
|
|
||||||
|
constructor(apiKey: string) {
|
||||||
|
if (!apiKey) {
|
||||||
|
throw new Error('Gemini API key is required');
|
||||||
|
}
|
||||||
|
this.ai = new GoogleGenAI({ apiKey });
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate an image from text prompt with optional reference images
|
||||||
|
*/
|
||||||
|
async generateImage(options: ImageGenerationOptions): Promise<ImageGenerationResult> {
|
||||||
|
const { prompt, filename, referenceImages } = options;
|
||||||
|
const timestamp = new Date().toISOString();
|
||||||
|
|
||||||
|
console.log(`[${timestamp}] Starting image generation: "${prompt.substring(0, 50)}..."`);
|
||||||
|
|
||||||
|
try {
|
||||||
|
// First try the primary model (Nano Banana)
|
||||||
|
const result = await this.tryGeneration({
|
||||||
|
model: this.primaryModel,
|
||||||
|
config: { responseModalities: ['IMAGE', 'TEXT'] },
|
||||||
|
prompt,
|
||||||
|
filename,
|
||||||
|
referenceImages,
|
||||||
|
modelName: 'Nano Banana'
|
||||||
|
});
|
||||||
|
|
||||||
|
if (result.success) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fallback to Imagen 4
|
||||||
|
console.log(`[${new Date().toISOString()}] Primary model failed, trying fallback (Imagen 4)...`);
|
||||||
|
|
||||||
|
return await this.tryGeneration({
|
||||||
|
model: this.fallbackModel,
|
||||||
|
config: { responseModalities: ['IMAGE'] },
|
||||||
|
prompt,
|
||||||
|
filename: `${filename}_fallback`,
|
||||||
|
referenceImages,
|
||||||
|
modelName: 'Imagen 4'
|
||||||
|
});
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`[${new Date().toISOString()}] Image generation failed:`, error);
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
model: 'none',
|
||||||
|
error: error instanceof Error ? error.message : 'Unknown error occurred'
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Try generation with a specific model
|
||||||
|
*/
|
||||||
|
private async tryGeneration(params: {
|
||||||
|
model: string;
|
||||||
|
config: { responseModalities: string[] };
|
||||||
|
prompt: string;
|
||||||
|
filename: string;
|
||||||
|
referenceImages?: ReferenceImage[];
|
||||||
|
modelName: string;
|
||||||
|
}): Promise<ImageGenerationResult> {
|
||||||
|
|
||||||
|
const { model, config, prompt, filename, referenceImages, modelName } = params;
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Build content parts for the API request
|
||||||
|
const contentParts: any[] = [];
|
||||||
|
|
||||||
|
// Add reference images if provided
|
||||||
|
if (referenceImages && referenceImages.length > 0) {
|
||||||
|
console.log(`[${new Date().toISOString()}] Adding ${referenceImages.length} reference image(s)`);
|
||||||
|
|
||||||
|
for (const refImage of referenceImages) {
|
||||||
|
contentParts.push({
|
||||||
|
inlineData: {
|
||||||
|
mimeType: refImage.mimetype,
|
||||||
|
data: refImage.buffer.toString('base64')
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add the text prompt
|
||||||
|
contentParts.push({
|
||||||
|
text: prompt
|
||||||
|
});
|
||||||
|
|
||||||
|
const contents = [
|
||||||
|
{
|
||||||
|
role: 'user' as const,
|
||||||
|
parts: contentParts
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
|
console.log(`[${new Date().toISOString()}] Making API request to ${modelName} (${model})...`);
|
||||||
|
|
||||||
|
const response = await this.ai.models.generateContent({
|
||||||
|
model,
|
||||||
|
config,
|
||||||
|
contents
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log(`[${new Date().toISOString()}] Response received from ${modelName}`);
|
||||||
|
|
||||||
|
if (response.candidates && response.candidates[0] && response.candidates[0].content) {
|
||||||
|
const content = response.candidates[0].content;
|
||||||
|
let generatedDescription = '';
|
||||||
|
let savedImagePath = '';
|
||||||
|
|
||||||
|
for (let index = 0; index < (content.parts?.length || 0); index++) {
|
||||||
|
const part = content.parts![index];
|
||||||
|
|
||||||
|
if (part.inlineData) {
|
||||||
|
const fileExtension = mime.getExtension(part.inlineData.mimeType || '');
|
||||||
|
const finalFilename = `${filename}.${fileExtension}`;
|
||||||
|
const filepath = path.join('./src/results', finalFilename);
|
||||||
|
|
||||||
|
console.log(`[${new Date().toISOString()}] Saving image: ${finalFilename}`);
|
||||||
|
|
||||||
|
const buffer = Buffer.from(part.inlineData.data || '', 'base64');
|
||||||
|
await this.saveImageFile(filepath, buffer);
|
||||||
|
|
||||||
|
savedImagePath = filepath;
|
||||||
|
|
||||||
|
} else if (part.text) {
|
||||||
|
generatedDescription = part.text;
|
||||||
|
console.log(`[${new Date().toISOString()}] Generated description: ${part.text.substring(0, 100)}...`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (savedImagePath) {
|
||||||
|
return {
|
||||||
|
success: true,
|
||||||
|
filename: path.basename(savedImagePath),
|
||||||
|
filepath: savedImagePath,
|
||||||
|
description: generatedDescription,
|
||||||
|
model: modelName
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
model: modelName,
|
||||||
|
error: 'No image data received from API'
|
||||||
|
};
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`[${new Date().toISOString()}] ${modelName} generation failed:`, error);
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
model: modelName,
|
||||||
|
error: error instanceof Error ? error.message : 'Generation failed'
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Save image buffer to file system
|
||||||
|
*/
|
||||||
|
private async saveImageFile(filepath: string, buffer: Buffer): Promise<void> {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
// Ensure the results directory exists
|
||||||
|
const dir = path.dirname(filepath);
|
||||||
|
if (!fs.existsSync(dir)) {
|
||||||
|
fs.mkdirSync(dir, { recursive: true });
|
||||||
|
}
|
||||||
|
|
||||||
|
fs.writeFile(filepath, buffer, (err) => {
|
||||||
|
if (err) {
|
||||||
|
console.error(`[${new Date().toISOString()}] Error saving file ${filepath}:`, err);
|
||||||
|
reject(err);
|
||||||
|
} else {
|
||||||
|
console.log(`[${new Date().toISOString()}] File saved successfully: ${filepath}`);
|
||||||
|
resolve();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validate reference images
|
||||||
|
*/
|
||||||
|
static validateReferenceImages(files: Express.Multer.File[]): { valid: boolean; error?: string } {
|
||||||
|
if (files.length > 3) {
|
||||||
|
return { valid: false, error: 'Maximum 3 reference images allowed' };
|
||||||
|
}
|
||||||
|
|
||||||
|
const allowedTypes = ['image/png', 'image/jpeg', 'image/jpg', 'image/webp'];
|
||||||
|
const maxSize = 5 * 1024 * 1024; // 5MB
|
||||||
|
|
||||||
|
for (const file of files) {
|
||||||
|
if (!allowedTypes.includes(file.mimetype)) {
|
||||||
|
return {
|
||||||
|
valid: false,
|
||||||
|
error: `Unsupported file type: ${file.mimetype}. Allowed: PNG, JPEG, WebP`
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (file.size > maxSize) {
|
||||||
|
return {
|
||||||
|
valid: false,
|
||||||
|
error: `File ${file.originalname} is too large. Maximum size: 5MB`
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return { valid: true };
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert Express.Multer.File[] to ReferenceImage[]
|
||||||
|
*/
|
||||||
|
static convertFilesToReferenceImages(files: Express.Multer.File[]): ReferenceImage[] {
|
||||||
|
return files.map(file => ({
|
||||||
|
buffer: file.buffer,
|
||||||
|
mimetype: file.mimetype,
|
||||||
|
originalname: file.originalname
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,286 @@
|
||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
# Magic Building Image Generation API Test Script
|
||||||
|
# Usage: ./test-api.sh [server_url]
|
||||||
|
|
||||||
|
SERVER_URL=${1:-"http://localhost:3000"}
|
||||||
|
TEST_DIR="./test-images"
|
||||||
|
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
|
||||||
|
LOG_FILE="./logs/test-api-${TIMESTAMP}.log"
|
||||||
|
|
||||||
|
# Colors for output
|
||||||
|
GREEN='\033[0;32m'
|
||||||
|
RED='\033[0;31m'
|
||||||
|
YELLOW='\033[1;33m'
|
||||||
|
BLUE='\033[0;34m'
|
||||||
|
NC='\033[0m' # No Color
|
||||||
|
|
||||||
|
# Ensure directories exist
|
||||||
|
mkdir -p logs test-images
|
||||||
|
|
||||||
|
# Logging function
|
||||||
|
log() {
|
||||||
|
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" | tee -a "$LOG_FILE"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Success/error output functions
|
||||||
|
success() {
|
||||||
|
echo -e "${GREEN}✅ $1${NC}" | tee -a "$LOG_FILE"
|
||||||
|
}
|
||||||
|
|
||||||
|
error() {
|
||||||
|
echo -e "${RED}❌ $1${NC}" | tee -a "$LOG_FILE"
|
||||||
|
}
|
||||||
|
|
||||||
|
warning() {
|
||||||
|
echo -e "${YELLOW}⚠️ $1${NC}" | tee -a "$LOG_FILE"
|
||||||
|
}
|
||||||
|
|
||||||
|
info() {
|
||||||
|
echo -e "${BLUE}ℹ️ $1${NC}" | tee -a "$LOG_FILE"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Test functions
|
||||||
|
test_health_check() {
|
||||||
|
info "Testing health check endpoint..."
|
||||||
|
response=$(curl -s -o /tmp/health.json -w "%{http_code}" "$SERVER_URL/health")
|
||||||
|
|
||||||
|
if [ "$response" -eq 200 ]; then
|
||||||
|
success "Health check passed (HTTP $response)"
|
||||||
|
log "Response: $(cat /tmp/health.json)"
|
||||||
|
else
|
||||||
|
error "Health check failed (HTTP $response)"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
test_api_info() {
|
||||||
|
info "Testing API info endpoint..."
|
||||||
|
response=$(curl -s -o /tmp/api-info.json -w "%{http_code}" "$SERVER_URL/api/info")
|
||||||
|
|
||||||
|
if [ "$response" -eq 200 ]; then
|
||||||
|
success "API info endpoint passed (HTTP $response)"
|
||||||
|
log "Response: $(cat /tmp/api-info.json)"
|
||||||
|
else
|
||||||
|
error "API info endpoint failed (HTTP $response)"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
test_text_to_image() {
|
||||||
|
info "Testing text-to-image generation..."
|
||||||
|
|
||||||
|
response=$(curl -s -o /tmp/text-to-image.json -w "%{http_code}" \
|
||||||
|
-X POST "$SERVER_URL/api/generate" \
|
||||||
|
-F "prompt=A majestic dragon soaring through clouds above a medieval castle" \
|
||||||
|
-F "filename=dragon_castle_${TIMESTAMP}")
|
||||||
|
|
||||||
|
if [ "$response" -eq 200 ]; then
|
||||||
|
success "Text-to-image generation passed (HTTP $response)"
|
||||||
|
log "Response: $(cat /tmp/text-to-image.json)"
|
||||||
|
|
||||||
|
# Check if image file was created
|
||||||
|
filename=$(jq -r '.data.filename' /tmp/text-to-image.json 2>/dev/null)
|
||||||
|
if [ "$filename" != "null" ] && [ -f "src/results/$filename" ]; then
|
||||||
|
success "Generated image file found: $filename"
|
||||||
|
log "Image size: $(ls -lh src/results/$filename | awk '{print $5}')"
|
||||||
|
else
|
||||||
|
warning "Generated image file not found or response malformed"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
error "Text-to-image generation failed (HTTP $response)"
|
||||||
|
log "Error response: $(cat /tmp/text-to-image.json)"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
test_validation_errors() {
|
||||||
|
info "Testing validation error handling..."
|
||||||
|
|
||||||
|
# Test missing prompt
|
||||||
|
response=$(curl -s -o /tmp/validation1.json -w "%{http_code}" \
|
||||||
|
-X POST "$SERVER_URL/api/generate" \
|
||||||
|
-F "filename=test")
|
||||||
|
|
||||||
|
if [ "$response" -eq 400 ]; then
|
||||||
|
success "Missing prompt validation works (HTTP $response)"
|
||||||
|
else
|
||||||
|
error "Missing prompt validation failed (HTTP $response)"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Test missing filename
|
||||||
|
response=$(curl -s -o /tmp/validation2.json -w "%{http_code}" \
|
||||||
|
-X POST "$SERVER_URL/api/generate" \
|
||||||
|
-F "prompt=Test prompt")
|
||||||
|
|
||||||
|
if [ "$response" -eq 400 ]; then
|
||||||
|
success "Missing filename validation works (HTTP $response)"
|
||||||
|
else
|
||||||
|
error "Missing filename validation failed (HTTP $response)"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Test invalid filename characters
|
||||||
|
response=$(curl -s -o /tmp/validation3.json -w "%{http_code}" \
|
||||||
|
-X POST "$SERVER_URL/api/generate" \
|
||||||
|
-F "prompt=Test prompt" \
|
||||||
|
-F "filename=invalid/filename*with?bad&chars")
|
||||||
|
|
||||||
|
if [ "$response" -eq 200 ]; then
|
||||||
|
success "Filename sanitization works (HTTP $response)"
|
||||||
|
log "Sanitized filename response: $(cat /tmp/validation3.json)"
|
||||||
|
else
|
||||||
|
warning "Filename sanitization test had unexpected result (HTTP $response)"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
test_404_endpoint() {
|
||||||
|
info "Testing 404 error handling..."
|
||||||
|
response=$(curl -s -o /tmp/404.json -w "%{http_code}" "$SERVER_URL/api/nonexistent")
|
||||||
|
|
||||||
|
if [ "$response" -eq 404 ]; then
|
||||||
|
success "404 error handling works (HTTP $response)"
|
||||||
|
else
|
||||||
|
error "404 error handling failed (HTTP $response)"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
create_test_image() {
|
||||||
|
info "Creating test reference image..."
|
||||||
|
|
||||||
|
# Create a simple test image using ImageMagick (if available) or skip
|
||||||
|
if command -v convert &> /dev/null; then
|
||||||
|
convert -size 300x200 xc:lightblue -pointsize 24 -fill black \
|
||||||
|
-gravity center -annotate 0 "Test Reference Image" \
|
||||||
|
"$TEST_DIR/test-reference.png"
|
||||||
|
success "Test reference image created"
|
||||||
|
else
|
||||||
|
warning "ImageMagick not available, skipping reference image test"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
test_image_with_reference() {
|
||||||
|
if [ ! -f "$TEST_DIR/test-reference.png" ]; then
|
||||||
|
warning "No reference image available, skipping reference image test"
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
info "Testing image generation with reference image..."
|
||||||
|
|
||||||
|
response=$(curl -s -o /tmp/with-reference.json -w "%{http_code}" \
|
||||||
|
-X POST "$SERVER_URL/api/generate" \
|
||||||
|
-F "prompt=Add magical sparkles and glowing effects to this image" \
|
||||||
|
-F "filename=sparkled_reference_${TIMESTAMP}" \
|
||||||
|
-F "referenceFiles=@${TEST_DIR}/test-reference.png")
|
||||||
|
|
||||||
|
if [ "$response" -eq 200 ]; then
|
||||||
|
success "Reference image generation passed (HTTP $response)"
|
||||||
|
log "Response: $(cat /tmp/with-reference.json)"
|
||||||
|
|
||||||
|
# Check if image file was created
|
||||||
|
filename=$(jq -r '.data.filename' /tmp/with-reference.json 2>/dev/null)
|
||||||
|
if [ "$filename" != "null" ] && [ -f "src/results/$filename" ]; then
|
||||||
|
success "Generated image with reference found: $filename"
|
||||||
|
else
|
||||||
|
warning "Generated image with reference not found"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
error "Reference image generation failed (HTTP $response)"
|
||||||
|
log "Error response: $(cat /tmp/with-reference.json)"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# Performance test
|
||||||
|
test_concurrent_requests() {
|
||||||
|
info "Testing concurrent requests (performance test)..."
|
||||||
|
|
||||||
|
# Start 3 concurrent requests
|
||||||
|
for i in {1..3}; do
|
||||||
|
(
|
||||||
|
response=$(curl -s -o "/tmp/concurrent-$i.json" -w "%{http_code}" \
|
||||||
|
-X POST "$SERVER_URL/api/generate" \
|
||||||
|
-F "prompt=Concurrent test image $i - fantasy landscape with mountains" \
|
||||||
|
-F "filename=concurrent_test_${i}_${TIMESTAMP}")
|
||||||
|
|
||||||
|
if [ "$response" -eq 200 ]; then
|
||||||
|
log "Concurrent request $i succeeded (HTTP $response)"
|
||||||
|
else
|
||||||
|
log "Concurrent request $i failed (HTTP $response)"
|
||||||
|
fi
|
||||||
|
) &
|
||||||
|
done
|
||||||
|
|
||||||
|
# Wait for all background jobs
|
||||||
|
wait
|
||||||
|
|
||||||
|
success "Concurrent requests test completed"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Main test execution
|
||||||
|
main() {
|
||||||
|
info "🚀 Starting Magic Building API Test Suite"
|
||||||
|
info "Server URL: $SERVER_URL"
|
||||||
|
info "Test log: $LOG_FILE"
|
||||||
|
|
||||||
|
# Check if server is running
|
||||||
|
if ! curl -s "$SERVER_URL/health" > /dev/null; then
|
||||||
|
error "Server is not responding at $SERVER_URL"
|
||||||
|
error "Please start the server with: pnpm server"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Run tests
|
||||||
|
tests_passed=0
|
||||||
|
tests_total=0
|
||||||
|
|
||||||
|
run_test() {
|
||||||
|
((tests_total++))
|
||||||
|
if $1; then
|
||||||
|
((tests_passed++))
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
run_test test_health_check
|
||||||
|
run_test test_api_info
|
||||||
|
run_test test_text_to_image
|
||||||
|
run_test test_validation_errors
|
||||||
|
run_test test_404_endpoint
|
||||||
|
|
||||||
|
# Optional tests
|
||||||
|
if create_test_image; then
|
||||||
|
run_test test_image_with_reference
|
||||||
|
fi
|
||||||
|
|
||||||
|
run_test test_concurrent_requests
|
||||||
|
|
||||||
|
# Summary
|
||||||
|
echo
|
||||||
|
info "📊 Test Results Summary"
|
||||||
|
success "Passed: $tests_passed/$tests_total tests"
|
||||||
|
|
||||||
|
if [ "$tests_passed" -eq "$tests_total" ]; then
|
||||||
|
success "🎉 All tests passed!"
|
||||||
|
else
|
||||||
|
warning "⚠️ Some tests failed. Check the logs for details."
|
||||||
|
fi
|
||||||
|
|
||||||
|
info "Generated images are in: src/results/"
|
||||||
|
info "Test logs saved to: $LOG_FILE"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Check dependencies
|
||||||
|
check_dependencies() {
|
||||||
|
if ! command -v curl &> /dev/null; then
|
||||||
|
error "curl is required for testing"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if ! command -v jq &> /dev/null; then
|
||||||
|
warning "jq not found - JSON response parsing will be limited"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# Run the tests
|
||||||
|
check_dependencies
|
||||||
|
main "$@"
|
||||||
Loading…
Reference in New Issue