168 lines
5.6 KiB
TypeScript
168 lines
5.6 KiB
TypeScript
// tests/api/01-basic.ts
|
|
|
|
import { join } from 'path';
|
|
import { api, log, runTest, saveImage, uploadFile, waitForGeneration, testContext } from './utils';
|
|
import { config, endpoints } from './config';
|
|
|
|
async function main() {
|
|
log.section('BASIC TESTS');
|
|
|
|
// Test 1: Upload image
|
|
await runTest('Upload image', async () => {
|
|
const fixturePath = join(__dirname, config.fixturesDir, 'test-image.png');
|
|
|
|
const response = await uploadFile(fixturePath, {
|
|
alias: '@test-logo',
|
|
description: 'Test logo image',
|
|
});
|
|
|
|
if (!response.image || !response.image.id) {
|
|
throw new Error('No image returned');
|
|
}
|
|
|
|
testContext.uploadedImageId = response.image.id;
|
|
log.detail('Image ID', response.image.id);
|
|
log.detail('Storage Key', response.image.storageKey);
|
|
log.detail('Alias', response.image.alias);
|
|
});
|
|
|
|
// Test 2: List images
|
|
await runTest('List images', async () => {
|
|
const result = await api(endpoints.images);
|
|
|
|
if (!result.data.images || !Array.isArray(result.data.images)) {
|
|
throw new Error('No images array returned');
|
|
}
|
|
|
|
log.detail('Total images', result.data.images.length);
|
|
log.detail('Has uploaded', result.data.images.some((img: any) => img.source === 'uploaded'));
|
|
});
|
|
|
|
// Test 3: Get image by ID
|
|
await runTest('Get image by ID', async () => {
|
|
const result = await api(`${endpoints.images}/${testContext.uploadedImageId}`);
|
|
|
|
if (!result.data.image) {
|
|
throw new Error('Image not found');
|
|
}
|
|
|
|
log.detail('Image ID', result.data.image.id);
|
|
log.detail('Source', result.data.image.source);
|
|
log.detail('File size', `${result.data.image.fileSize} bytes`);
|
|
});
|
|
|
|
// Test 4: Generate image without references
|
|
await runTest('Generate image (simple)', async () => {
|
|
const result = await api(endpoints.generations, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
prompt: 'A beautiful sunset over mountains',
|
|
aspectRatio: '16:9',
|
|
}),
|
|
});
|
|
|
|
if (!result.data.generation) {
|
|
throw new Error('No generation returned');
|
|
}
|
|
|
|
testContext.generationId = result.data.generation.id;
|
|
log.detail('Generation ID', result.data.generation.id);
|
|
log.detail('Status', result.data.generation.status);
|
|
log.detail('Prompt', result.data.generation.originalPrompt);
|
|
|
|
// Wait for completion
|
|
log.info('Waiting for generation to complete...');
|
|
const generation = await waitForGeneration(testContext.generationId);
|
|
|
|
if (generation.status !== 'success') {
|
|
throw new Error(`Generation failed: ${generation.errorMessage}`);
|
|
}
|
|
|
|
log.detail('Processing time', `${generation.processingTimeMs}ms`);
|
|
log.detail('Output image', generation.outputImageId);
|
|
|
|
// Save generated image
|
|
if (generation.outputImageId) {
|
|
const imageResult = await api(`${endpoints.images}/${generation.outputImageId}`);
|
|
|
|
// Download image
|
|
const imageUrl = imageResult.data.image.storageUrl;
|
|
const imageResponse = await fetch(imageUrl);
|
|
const imageBuffer = await imageResponse.arrayBuffer();
|
|
|
|
await saveImage(imageBuffer, 'simple-generation.png');
|
|
testContext.imageId = generation.outputImageId;
|
|
}
|
|
});
|
|
|
|
// Test 5: Generate with uploaded reference
|
|
await runTest('Generate with reference image', async () => {
|
|
const result = await api(endpoints.generations, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
prompt: 'A product photo with the @test-logo in the corner',
|
|
aspectRatio: '1:1',
|
|
referenceImages: ['@test-logo'],
|
|
}),
|
|
});
|
|
|
|
if (!result.data.generation) {
|
|
throw new Error('No generation returned');
|
|
}
|
|
|
|
log.detail('Generation ID', result.data.generation.id);
|
|
log.detail('Referenced images', result.data.generation.referencedImages?.length || 0);
|
|
|
|
// Wait for completion
|
|
log.info('Waiting for generation to complete...');
|
|
const generation = await waitForGeneration(result.data.generation.id);
|
|
|
|
if (generation.status !== 'success') {
|
|
throw new Error(`Generation failed: ${generation.errorMessage}`);
|
|
}
|
|
|
|
// Save generated image
|
|
if (generation.outputImageId) {
|
|
const imageResult = await api(`${endpoints.images}/${generation.outputImageId}`);
|
|
const imageUrl = imageResult.data.image.storageUrl;
|
|
const imageResponse = await fetch(imageUrl);
|
|
const imageBuffer = await imageResponse.arrayBuffer();
|
|
|
|
await saveImage(imageBuffer, 'with-reference.png');
|
|
}
|
|
});
|
|
|
|
// Test 6: List generations
|
|
await runTest('List generations', async () => {
|
|
const result = await api(endpoints.generations);
|
|
|
|
if (!result.data.generations || !Array.isArray(result.data.generations)) {
|
|
throw new Error('No generations array returned');
|
|
}
|
|
|
|
log.detail('Total generations', result.data.generations.length);
|
|
log.detail('Successful', result.data.generations.filter((g: any) => g.status === 'success').length);
|
|
log.detail('Has pagination', !!result.data.pagination);
|
|
});
|
|
|
|
// Test 7: Get generation by ID
|
|
await runTest('Get generation details', async () => {
|
|
const result = await api(`${endpoints.generations}/${testContext.generationId}`);
|
|
|
|
if (!result.data.generation) {
|
|
throw new Error('Generation not found');
|
|
}
|
|
|
|
log.detail('Generation ID', result.data.generation.id);
|
|
log.detail('Status', result.data.generation.status);
|
|
log.detail('Has image', !!result.data.image);
|
|
log.detail('Referenced images', result.data.referencedImages?.length || 0);
|
|
});
|
|
|
|
log.section('BASIC TESTS COMPLETED');
|
|
}
|
|
|
|
main().catch(console.error);
|