import { jest } from '@jest/globals' jest.unstable_mockModule('../redis-client.js', () => ({ default: { get: jest.fn(async () => null), setEx: jest.fn(async () => 'OK'), }, })) jest.unstable_mockModule('axios', () => ({ default: { get: jest.fn(async () => ({ data: { products: [] } })), }, })) const { transformOFFProduct, searchBabyProducts, getBabyProductDetails } = await import('../off-service.js') describe('transformOFFProduct', () => { test('transforms a valid OFF product to unified model', () => { const offProduct = { _id: '12345', product_name: 'Baby Rice', brands: 'Nestlé', image_url: 'https://example.com/image.jpg', nutriscore_grade: 'a', ingredients_text: 'Rice, Iron, Vitamins', nova_group: 1, ecoscore_grade: 'b', categories_tags: ['en:baby-foods', 'en:baby-rice'], } const result = transformOFFProduct(offProduct) expect(result).toEqual({ id: '12345', source: 'openfoodfacts', name: 'Baby Rice', brand: 'Nestlé', category: 'baby_food', image_url: 'https://example.com/image.jpg', nutriscore: 'a', ingredients: 'Rice, Iron, Vitamins', nova_group: 1, eco_score: 'b', }) }) test('returns null for null/undefined input', () => { expect(transformOFFProduct(null)).toBeNull() expect(transformOFFProduct(undefined)).toBeNull() }) test('returns null when missing required fields', () => { expect(transformOFFProduct({ _id: '123' })).toBeNull() expect(transformOFFProduct({ product_name: 'Test' })).toBeNull() }) test('sets default brand to empty string when missing', () => { const result = transformOFFProduct({ _id: '999', product_name: 'Plain Product', }) expect(result.brand).toBe('') expect(result.image_url).toBeNull() expect(result.nutriscore).toBeNull() expect(result.ingredients).toBeNull() expect(result.nova_group).toBeNull() expect(result.eco_score).toBeNull() }) test('detects baby_milk category from tags', () => { const result = transformOFFProduct({ _id: '200', product_name: 'Infant Formula', categories_tags: ['en:infant-milk'], }) expect(result.category).toBe('baby_milk') }) test('detects baby_cereal category from tags', () => { const result = transformOFFProduct({ _id: '300', product_name: 'Baby Cereal', categories_tags: ['en:baby-cereals'], }) expect(result.category).toBe('baby_cereal') }) }) describe('searchBabyProducts', () => { test('returns empty array for short query', async () => { const result = await searchBabyProducts('a') expect(result).toEqual([]) }) test('returns empty array for null/empty query', async () => { expect(await searchBabyProducts(null)).toEqual([]) expect(await searchBabyProducts('')).toEqual([]) }) }) describe('getBabyProductDetails', () => { test('returns null for null barcode', async () => { const result = await getBabyProductDetails(null) expect(result).toBeNull() }) })