const { buildIdentifyPrompt, normalizeIdentifyResult } = require('../../server/lib/openai'); const { applyCatalogGrounding, isLikelyGermanCommonName } = require('../../server/lib/scanGrounding'); describe('scan language guards', () => { it('keeps the English AI common name when the catalog match is obviously German', () => { const aiResult = { name: 'Poinsettia', botanicalName: 'Euphorbia pulcherrima', confidence: 0.66, description: 'Poinsettia was identified with AI. Care guidance is shown below.', careInfo: { waterIntervalDays: 6, light: 'Bright indirect light', temp: '18-24C', }, }; const catalogEntries = [ { name: 'Weihnachtsstern', botanicalName: 'Euphorbia pulcherrima', description: 'Deutscher Katalogeintrag', careInfo: { waterIntervalDays: 8, light: 'Bright indirect light', temp: '18-24C', }, }, ]; const grounded = applyCatalogGrounding(aiResult, catalogEntries, 'en'); expect(grounded.grounded).toBe(true); expect(grounded.result.name).toBe('Poinsettia'); expect(grounded.result.botanicalName).toBe('Euphorbia pulcherrima'); expect(grounded.result.description).toContain('identified with AI'); expect(grounded.result.careInfo.light).toBe('Bright indirect light'); expect(grounded.result.confidence).toBeGreaterThanOrEqual(0.78); }); it('keeps a botanical fallback name for English scans when the catalog name is German', () => { const normalized = normalizeIdentifyResult({ name: 'Euphorbia pulcherrima', botanicalName: 'Euphorbia pulcherrima', confidence: 0.52, description: 'Euphorbia pulcherrima was identified with AI. Care guidance is shown below.', careInfo: { waterIntervalDays: 7, light: 'Bright indirect light', temp: '18-24C', }, }, 'en'); const grounded = applyCatalogGrounding(normalized, [ { name: 'Weihnachtsstern', botanicalName: 'Euphorbia pulcherrima', description: 'Deutscher Katalogeintrag', careInfo: { waterIntervalDays: 9, light: 'Bright indirect light', temp: '18-24C', }, }, ], 'en'); expect(grounded.result.name).toBe('Euphorbia pulcherrima'); expect(grounded.result.botanicalName).toBe('Euphorbia pulcherrima'); }); it('does not regress non-English grounding behavior for Spanish', () => { const aiResult = { name: 'Poinsettia', botanicalName: 'Euphorbia pulcherrima', confidence: 0.64, description: 'La planta fue identificada con IA.', careInfo: { waterIntervalDays: 6, light: 'Luz indirecta brillante', temp: '18-24C', }, }; const grounded = applyCatalogGrounding(aiResult, [ { name: 'Flor de Pascua', botanicalName: 'Euphorbia pulcherrima', description: 'Entrada de catalogo', careInfo: { waterIntervalDays: 7, light: 'Luz indirecta brillante', temp: '18-24C', }, }, ], 'es'); expect(grounded.result.name).toBe('Flor de Pascua'); expect(grounded.result.description).toBe('La planta fue identificada con IA.'); expect(grounded.result.careInfo.light).toBe('Luz indirecta brillante'); }); it('hardens the English identify prompt against non-English common names', () => { const prompt = buildIdentifyPrompt('en', 'primary'); expect(prompt).toContain('English common name only'); expect(prompt).toContain('Never return a German or other non-English common name'); expect(prompt).toContain('use "botanicalName" as "name" instead'); }); it('detects obviously German common names for override protection', () => { expect(isLikelyGermanCommonName('Weihnachtsstern')).toBe(true); expect(isLikelyGermanCommonName('Weinachtsstern')).toBe(true); expect(isLikelyGermanCommonName('Poinsettia')).toBe(false); }); });