feat(plan5): openaiClient — génération descriptions GPT-4o (TDD)
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
import { generateDescriptions } from './openaiClient'
|
||||
|
||||
const mockFetch = jest.fn()
|
||||
global.fetch = mockFetch
|
||||
|
||||
const mockResponse = (content: string) => ({
|
||||
ok: true,
|
||||
json: async () => ({ choices: [{ message: { content } }] }),
|
||||
})
|
||||
|
||||
describe('generateDescriptions', () => {
|
||||
beforeEach(() => {
|
||||
process.env.OPENAI_API_KEY = 'sk-test'
|
||||
mockFetch.mockReset()
|
||||
})
|
||||
|
||||
it('retourne shortDesc et longDesc depuis OpenAI', async () => {
|
||||
mockFetch
|
||||
.mockResolvedValueOnce(mockResponse('<p>Description courte</p>'))
|
||||
.mockResolvedValueOnce(mockResponse('<p>Description longue</p>'))
|
||||
const result = await generateDescriptions({
|
||||
title: 'Perceuse', rawDescription: 'Une perceuse', comment: '', vendor: 'BOSCH', famille: 'OUTILLAGE',
|
||||
})
|
||||
expect(result.shortDesc).toBe('<p>Description courte</p>')
|
||||
expect(result.longDesc).toBe('<p>Description longue</p>')
|
||||
expect(mockFetch).toHaveBeenCalledTimes(2)
|
||||
})
|
||||
|
||||
it('lance les deux requêtes en parallèle', async () => {
|
||||
mockFetch.mockResolvedValue(mockResponse('<p>ok</p>'))
|
||||
await generateDescriptions({ title: 'T', rawDescription: '', comment: '', vendor: '', famille: '' })
|
||||
expect(mockFetch).toHaveBeenCalledTimes(2)
|
||||
})
|
||||
|
||||
it('lance une erreur si OPENAI_API_KEY manquant', async () => {
|
||||
delete process.env.OPENAI_API_KEY
|
||||
await expect(generateDescriptions({ title: 'T', rawDescription: '', comment: '', vendor: '', famille: '' }))
|
||||
.rejects.toThrow('OPENAI_API_KEY')
|
||||
})
|
||||
|
||||
it('lance une erreur si la réponse OpenAI est non-ok', async () => {
|
||||
mockFetch.mockResolvedValue({ ok: false, status: 429, text: async () => 'rate limited' })
|
||||
await expect(generateDescriptions({ title: 'T', rawDescription: '', comment: '', vendor: '', famille: '' }))
|
||||
.rejects.toThrow('429')
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user