import { highlightMatches } from '@/lib/search' // Mock prisma before importing search module jest.mock('@/lib/prisma', () => ({ prisma: { note: { findMany: jest.fn().mockResolvedValue([]), }, }, })) describe('search.ts', () => { describe('highlightMatches', () => { it('returns first 150 characters when query is empty', () => { const text = 'This is a long text that should be truncated to 150 characters. ' + 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor.' const result = highlightMatches(text, '') expect(result.length).toBeLessThanOrEqual(150) }) it('finds and highlights matching words', () => { const text = 'This is a test document about JavaScript programming.' const result = highlightMatches(text, 'JavaScript') expect(result).toContain('JavaScript') }) it('handles multiple word queries', () => { const text = 'React is a JavaScript library for building user interfaces.' const result = highlightMatches(text, 'JavaScript React') expect(result).toContain('JavaScript') expect(result).toContain('React') }) it('escapes regex special characters in query', () => { const text = 'What is $100 + $200?' const result = highlightMatches(text, '$100') expect(result).toContain('$100') }) it('adds ellipsis when match is not at start', () => { const text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. JavaScript is great.' const result = highlightMatches(text, 'JavaScript') // JavaScript is at position 61, start is max(0, 61-75) = 0, so no ellipsis needed expect(result).toContain('JavaScript') }) it('returns plain text when no match found', () => { const text = 'This is a simple text without the word we are looking for.' const result = highlightMatches(text, 'xyz123') expect(result).not.toContain('') }) it('filters out single character words and returns excerpt', () => { const text = 'A B C D E F G H I J K L M N O P Q R S T U V W X Y Z' const result = highlightMatches(text, 'A B C') // Single char words (A, B, C) are filtered, returns 150 chars expect(result.length).toBeLessThanOrEqual(150) }) }) // Integration tests would go here but require database setup // These would test noteQuery and searchNotes with actual data })