// tests/api/01-basic.ts import { join } from 'path'; import { api, log, runTest, saveImage, uploadFile, waitForGeneration, testContext } from './utils'; import { config, endpoints } from './config'; import { fileURLToPath } from 'url'; import { dirname } from 'path'; const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); 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 || !response.id) { throw new Error('No image returned'); } testContext.uploadedImageId = response.id; log.detail('Image ID', response.id); log.detail('Storage Key', response.storageKey); log.detail('Alias', response.alias); }); // Test 2: List images await runTest('List images', async () => { const result = await api(endpoints.images); if (!result.data.data || !Array.isArray(result.data.data)) { throw new Error('No images array returned'); } log.detail('Total images', result.data.data.length); log.detail('Has uploaded', result.data.data.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.data) { throw new Error('Image not found'); } log.detail('Image ID', result.data.data.id); log.detail('Source', result.data.data.source); log.detail('File size', `${result.data.data.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.data) { throw new Error('No generation returned'); } testContext.generationId = result.data.data.id; log.detail('Generation ID', result.data.data.id); log.detail('Status', result.data.data.status); log.detail('Prompt', result.data.data.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.data.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.data) { throw new Error('No generation returned'); } log.detail('Generation ID', result.data.data.id); log.detail('Referenced images', result.data.data.referencedImages?.length || 0); // Wait for completion log.info('Waiting for generation to complete...'); const generation = await waitForGeneration(result.data.data.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.data.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.data || !Array.isArray(result.data.data)) { throw new Error('No generations array returned'); } log.detail('Total generations', result.data.data.length); log.detail('Successful', result.data.data.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.data) { throw new Error('Generation not found'); } log.detail('Generation ID', result.data.data.id); log.detail('Status', result.data.data.status); log.detail('Has output image', !!result.data.data.outputImage); log.detail('Referenced images', result.data.data.referencedImages?.length || 0); }); log.section('BASIC TESTS COMPLETED'); } main().catch(console.error);