Spaces:
Running
Running
File size: 3,239 Bytes
dce7eca | 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 | import { POST } from '@/app/api/generate/route'
import { exec } from 'child_process'
import fs from 'fs'
// Mock next/server
jest.mock('next/server', () => ({
NextResponse: {
json: (body: any, init?: any) => ({
json: async () => body,
status: init?.status || 200,
}),
},
}))
// Mock child_process
jest.mock('child_process', () => ({
exec: jest.fn(),
}))
// Mock fs and os
jest.mock('fs', () => ({
existsSync: jest.fn().mockReturnValue(true),
mkdirSync: jest.fn(),
writeFileSync: jest.fn(),
unlinkSync: jest.fn(),
rmdirSync: jest.fn(),
}))
jest.mock('os', () => ({
tmpdir: () => '/tmp',
}))
const createMockRequest = (body: any) => ({
formData: async () => {
const data = new Map();
Object.keys(body).forEach(key => {
if (Array.isArray(body[key])) {
data.set(key, body[key]);
} else {
data.set(key, body[key]);
}
});
return {
get: (key: string) => body[key],
getAll: (key: string) => Array.isArray(body[key]) ? body[key] : [body[key]],
};
},
}) as any
describe('Generate API (Hybrid Pipeline)', () => {
beforeEach(() => {
jest.clearAllMocks()
process.env.STABILITY_API_KEY = 'test-key'
jest.spyOn(console, 'error').mockImplementation(() => { })
jest.spyOn(console, 'log').mockImplementation(() => { })
})
it('returns success for valid request via hybrid pipeline', async () => {
const mockImage = new File(['test'], 'test.png', { type: 'image/png' })
const req = createMockRequest({
prompt: 'test prompt',
images: [mockImage]
})
const mockStdout = JSON.stringify({
success: true,
image: 'data:image/png;base64,mockdata'
});
(exec as unknown as jest.Mock).mockImplementation((cmd, options, callback) => {
const cb = typeof options === 'function' ? options : callback;
process.nextTick(() => cb(null, mockStdout, ''));
})
const response = await POST(req)
const json = await response.json()
expect(response.status).toBe(200)
expect(json.success).toBe(true)
// Check if images were "saved" (our mock should have triggered fs.writeFileSync)
expect(fs.writeFileSync).toHaveBeenCalled()
expect(json.method).toBe('cv_ai_hybrid')
})
it('returns 500 on pipeline failure', async () => {
const mockImage = new File(['test'], 'test.png', { type: 'image/png' })
const req = createMockRequest({
prompt: 'fail',
images: [mockImage]
})
const mockError = JSON.stringify({
success: false,
error: 'AI Error'
});
(exec as unknown as jest.Mock).mockImplementation((cmd, options, callback) => {
const cb = typeof options === 'function' ? options : callback;
cb(null, mockError, '')
})
const response = await POST(req)
const json = await response.json()
expect(response.status).toBe(500)
expect(json.message).toBe('AI Error')
})
})
|