fix(backend): switch OFF API to /cgi/search.pl endpoint and fix test imports
Run Tests on Branches / Detect Changes (push) Successful in 8s
Run Tests on Branches / Frontend Tests (push) Has been cancelled
Run Tests on Branches / Frontend Mobile Tests (push) Has been cancelled
Run Tests on Branches / Backend Tests (push) Has been cancelled

- Changed from /api/v2/search (returning 503) to /cgi/search.pl (working)
- Removed strict categories_tags=baby-food filter that was too restrictive
- Fixed test file to use dynamic imports for ES module compatibility
- Increased timeout to 10s for reliability
This commit is contained in:
Antoni Nuñez Romeu
2026-07-13 16:18:11 +02:00
parent 4df1594b3b
commit 26f309acfb
2 changed files with 58 additions and 43 deletions
+52 -37
View File
@@ -1,4 +1,19 @@
import { transformOFFProduct, searchBabyProducts, getBabyProductDetails } from '../off-service.js'; 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', () => { describe('transformOFFProduct', () => {
test('transforms a valid OFF product to unified model', () => { test('transforms a valid OFF product to unified model', () => {
@@ -12,9 +27,9 @@ describe('transformOFFProduct', () => {
nova_group: 1, nova_group: 1,
ecoscore_grade: 'b', ecoscore_grade: 'b',
categories_tags: ['en:baby-foods', 'en:baby-rice'], categories_tags: ['en:baby-foods', 'en:baby-rice'],
}; }
const result = transformOFFProduct(offProduct); const result = transformOFFProduct(offProduct)
expect(result).toEqual({ expect(result).toEqual({
id: '12345', id: '12345',
@@ -27,66 +42,66 @@ describe('transformOFFProduct', () => {
ingredients: 'Rice, Iron, Vitamins', ingredients: 'Rice, Iron, Vitamins',
nova_group: 1, nova_group: 1,
eco_score: 'b', eco_score: 'b',
}); })
}); })
test('returns null for null/undefined input', () => { test('returns null for null/undefined input', () => {
expect(transformOFFProduct(null)).toBeNull(); expect(transformOFFProduct(null)).toBeNull()
expect(transformOFFProduct(undefined)).toBeNull(); expect(transformOFFProduct(undefined)).toBeNull()
}); })
test('returns null when missing required fields', () => { test('returns null when missing required fields', () => {
expect(transformOFFProduct({ _id: '123' })).toBeNull(); expect(transformOFFProduct({ _id: '123' })).toBeNull()
expect(transformOFFProduct({ product_name: 'Test' })).toBeNull(); expect(transformOFFProduct({ product_name: 'Test' })).toBeNull()
}); })
test('sets default brand to empty string when missing', () => { test('sets default brand to empty string when missing', () => {
const result = transformOFFProduct({ const result = transformOFFProduct({
_id: '999', _id: '999',
product_name: 'Plain Product', product_name: 'Plain Product',
}); })
expect(result.brand).toBe(''); expect(result.brand).toBe('')
expect(result.image_url).toBeNull(); expect(result.image_url).toBeNull()
expect(result.nutriscore).toBeNull(); expect(result.nutriscore).toBeNull()
expect(result.ingredients).toBeNull(); expect(result.ingredients).toBeNull()
expect(result.nova_group).toBeNull(); expect(result.nova_group).toBeNull()
expect(result.eco_score).toBeNull(); expect(result.eco_score).toBeNull()
}); })
test('detects baby_milk category from tags', () => { test('detects baby_milk category from tags', () => {
const result = transformOFFProduct({ const result = transformOFFProduct({
_id: '200', _id: '200',
product_name: 'Infant Formula', product_name: 'Infant Formula',
categories_tags: ['en:infant-milk'], categories_tags: ['en:infant-milk'],
}); })
expect(result.category).toBe('baby_milk'); expect(result.category).toBe('baby_milk')
}); })
test('detects baby_cereal category from tags', () => { test('detects baby_cereal category from tags', () => {
const result = transformOFFProduct({ const result = transformOFFProduct({
_id: '300', _id: '300',
product_name: 'Baby Cereal', product_name: 'Baby Cereal',
categories_tags: ['en:baby-cereals'], categories_tags: ['en:baby-cereals'],
}); })
expect(result.category).toBe('baby_cereal'); expect(result.category).toBe('baby_cereal')
}); })
}); })
describe('searchBabyProducts', () => { describe('searchBabyProducts', () => {
test('returns empty array for short query', async () => { test('returns empty array for short query', async () => {
const result = await searchBabyProducts('a'); const result = await searchBabyProducts('a')
expect(result).toEqual([]); expect(result).toEqual([])
}); })
test('returns empty array for null/empty query', async () => { test('returns empty array for null/empty query', async () => {
expect(await searchBabyProducts(null)).toEqual([]); expect(await searchBabyProducts(null)).toEqual([])
expect(await searchBabyProducts('')).toEqual([]); expect(await searchBabyProducts('')).toEqual([])
}); })
}); })
describe('getBabyProductDetails', () => { describe('getBabyProductDetails', () => {
test('returns null for null barcode', async () => { test('returns null for null barcode', async () => {
const result = await getBabyProductDetails(null); const result = await getBabyProductDetails(null)
expect(result).toBeNull(); expect(result).toBeNull()
}); })
}); })
+6 -6
View File
@@ -1,7 +1,7 @@
import axios from 'axios'; import axios from 'axios';
import redisClient from './redis-client.js'; import redisClient from './redis-client.js';
const OFF_API_BASE = 'https://world.openfoodfacts.org/api/v2'; const OFF_API_BASE = 'https://world.openfoodfacts.org';
const CACHE_TTL = 3600; const CACHE_TTL = 3600;
export function transformOFFProduct(offProduct) { export function transformOFFProduct(offProduct) {
@@ -53,15 +53,15 @@ export async function searchBabyProducts(query) {
} }
console.log(`Fetching from OFF API: ${searchTerm}`); console.log(`Fetching from OFF API: ${searchTerm}`);
const response = await axios.get(`${OFF_API_BASE}/search`, { const response = await axios.get(`${OFF_API_BASE}/cgi/search.pl`, {
params: { params: {
categories_tags: 'baby-food',
search_terms: searchTerm, search_terms: searchTerm,
json: true, json: true,
fields: 'product_name,brands,image_url,nutriscore_grade,ingredients_text,nova_group,ecoscore_grade,categories_tags', fields: 'product_name,brands,image_url,nutriscore_grade,ingredients_text,nova_group,ecoscore_grade,categories_tags',
page_size: 20, page_size: 20,
action: 'process',
}, },
timeout: 5000, timeout: 10000,
}); });
if (response.data && response.data.products) { if (response.data && response.data.products) {
@@ -100,8 +100,8 @@ export async function getBabyProductDetails(barcode) {
} }
console.log(`Fetching OFF product details: ${barcode}`); console.log(`Fetching OFF product details: ${barcode}`);
const response = await axios.get(`${OFF_API_BASE}/product/${barcode}.json`, { const response = await axios.get(`${OFF_API_BASE}/api/v2/product/${barcode}.json`, {
timeout: 5000, timeout: 10000,
}); });
if (response.data && response.data.product) { if (response.data && response.data.product) {