Spaces:
Sleeping
Sleeping
| import request from 'supertest'; | |
| import { afterEach, describe, expect, it, vi } from 'vitest'; | |
| import { createApp } from './app.js'; | |
| describe('generation api', () => { | |
| afterEach(() => { | |
| vi.unstubAllEnvs(); | |
| }); | |
| it('returns health status', async () => { | |
| await request(createApp()).get('/health').expect(200, { status: 'ok' }); | |
| }); | |
| it('accepts uploaded images and returns a queued generation job', async () => { | |
| const app = createApp(); | |
| const response = await request(app) | |
| .post('/api/generations') | |
| .field('prompt', 'Photorealistic product image') | |
| .field('preset', 'studio') | |
| .field('ratio', '16:9') | |
| .field('mode', 'product') | |
| .attach('images', Buffer.from('image'), { filename: 'product.png', contentType: 'image/png' }) | |
| .expect(202); | |
| expect(response.body).toMatchObject({ | |
| provider: 'mock', | |
| status: expect.stringMatching(/queued|running|completed/), | |
| }); | |
| expect(response.body.jobId).toEqual(expect.any(String)); | |
| await vi.waitFor(async () => { | |
| const jobResponse = await request(app).get(`/api/generations/${response.body.jobId}`).expect(200); | |
| expect(jobResponse.body).toMatchObject({ | |
| status: 'completed', | |
| result: { | |
| provider: 'mock', | |
| preset: 'studio', | |
| ratio: '16:9', | |
| images: [{ originalName: 'product.png' }], | |
| }, | |
| }); | |
| expect(jobResponse.body.result.images[0].variants).toHaveLength(3); | |
| }); | |
| }); | |
| it('normalizes multiple uploaded images to one menu generation job', async () => { | |
| const app = createApp(); | |
| const response = await request(app) | |
| .post('/api/generations') | |
| .field('prompt', 'Menu image') | |
| .field('preset', 'studio') | |
| .field('ratio', '16:9') | |
| .field('mode', 'product') | |
| .attach('images', Buffer.from('one'), { filename: 'burger.png', contentType: 'image/png' }) | |
| .attach('images', Buffer.from('two'), { filename: 'drink.png', contentType: 'image/png' }) | |
| .expect(202); | |
| await vi.waitFor(async () => { | |
| const jobResponse = await request(app).get(`/api/generations/${response.body.jobId}`).expect(200); | |
| expect(jobResponse.body.result).toMatchObject({ | |
| ratio: '1:1', | |
| images: [{ originalName: 'burger.png' }], | |
| }); | |
| }); | |
| }); | |
| it('returns 404 for unknown generation jobs', async () => { | |
| await request(createApp()).get('/api/generations/missing').expect(404, { error: 'Generation job not found' }); | |
| }); | |
| it('rejects requests without images', async () => { | |
| await request(createApp()) | |
| .post('/api/generations') | |
| .field('prompt', 'Photorealistic product image') | |
| .field('preset', 'studio') | |
| .field('ratio', '16:9') | |
| .field('mode', 'product') | |
| .expect(400, { error: 'At least one image is required' }); | |
| }); | |
| }); | |