Skip to content

An AI-powered search engine with a generative UI

5.8 KB
Raw Blame
import { beforeEach, describe, expect, it, vi } from 'vitest'

// Mock the modules before any imports
vi.mock('@/lib/db')
vi.mock('@langfuse/client')

// Import after mocking
import { LangfuseClient } from '@langfuse/client'

import { db } from '@/lib/db'

import { getMessageFeedback, updateMessageFeedback } from '../feedback'

describe('Feedback Actions', () => {
  beforeEach(() => {
    vi.clearAllMocks()
  })

  describe('updateMessageFeedback', () => {
    it('should update message feedback successfully', async () => {
      const messageId = 'test-message-id'
      const chatId = 'test-chat-id'
      const score = 1

      // Mock db.select
      const mockLimit = vi.fn().mockResolvedValue([
        {
          metadata: { traceId: 'test-trace-id' },
          chatId
        }
      ])
      const mockWhere = vi.fn().mockReturnValue({ limit: mockLimit })
      const mockFrom = vi.fn().mockReturnValue({ where: mockWhere })
      vi.mocked(db).select = vi.fn().mockReturnValue({ from: mockFrom })

      // Mock db.update
      const mockUpdateWhere = vi.fn().mockResolvedValue(undefined)
      const mockSet = vi.fn().mockReturnValue({ where: mockUpdateWhere })
      vi.mocked(db).update = vi.fn().mockReturnValue({ set: mockSet })

      const result = await updateMessageFeedback(messageId, score)

      expect(result).toEqual({ success: true })
      expect(db.select).toHaveBeenCalled()
      expect(db.update).toHaveBeenCalled()
    })

    it('should return error when message not found', async () => {
      const messageId = 'non-existent-id'
      const score = 1

      // Mock empty database response
      const mockLimit = vi.fn().mockResolvedValue([])
      const mockWhere = vi.fn().mockReturnValue({ limit: mockLimit })
      const mockFrom = vi.fn().mockReturnValue({ where: mockWhere })
      vi.mocked(db).select = vi.fn().mockReturnValue({ from: mockFrom })

      const result = await updateMessageFeedback(messageId, score)

      expect(result).toEqual({
        success: false,
        error: 'Message not found'
      })
    })

    it('should handle errors gracefully', async () => {
      const messageId = 'test-message-id'
      const score = -1

      // Mock database error
      const mockLimit = vi.fn().mockRejectedValue(new Error('Database error'))
      const mockWhere = vi.fn().mockReturnValue({ limit: mockLimit })
      const mockFrom = vi.fn().mockReturnValue({ where: mockWhere })
      vi.mocked(db).select = vi.fn().mockReturnValue({ from: mockFrom })

      const result = await updateMessageFeedback(messageId, score)

      expect(result.success).toBe(false)
      expect(result.error).toBe('Database error')
    })

    it('should not send a Langfuse score (the API route owns score submission)', async () => {
      const messageId = 'test-message-id'
      const chatId = 'test-chat-id'
      const score = 1

      // Mock db.select
      const mockLimit = vi.fn().mockResolvedValue([
        {
          metadata: { traceId: 'test-trace-id' },
          chatId
        }
      ])
      const mockWhere = vi.fn().mockReturnValue({ limit: mockLimit })
      const mockFrom = vi.fn().mockReturnValue({ where: mockWhere })
      vi.mocked(db).select = vi.fn().mockReturnValue({ from: mockFrom })

      // Mock db.update
      const mockUpdateWhere = vi.fn().mockResolvedValue(undefined)
      const mockSet = vi.fn().mockReturnValue({ where: mockUpdateWhere })
      vi.mocked(db).update = vi.fn().mockReturnValue({ set: mockSet })

      const result = await updateMessageFeedback(messageId, score)

      expect(result).toEqual({ success: true })
      expect(LangfuseClient).not.toHaveBeenCalled()
    })
  })

  describe('getMessageFeedback', () => {
    it('should retrieve feedback score successfully', async () => {
      const messageId = 'test-message-id'
      const feedbackScore = 1

      // Mock database response
      const mockLimit = vi.fn().mockResolvedValue([
        {
          metadata: { feedbackScore }
        }
      ])
      const mockWhere = vi.fn().mockReturnValue({ limit: mockLimit })
      const mockFrom = vi.fn().mockReturnValue({ where: mockWhere })
      vi.mocked(db).select = vi.fn().mockReturnValue({ from: mockFrom })

      const result = await getMessageFeedback(messageId)

      expect(result).toBe(feedbackScore)
    })

    it('should return null when message not found', async () => {
      const messageId = 'non-existent-id'

      // Mock empty database response
      const mockLimit = vi.fn().mockResolvedValue([])
      const mockWhere = vi.fn().mockReturnValue({ limit: mockLimit })
      const mockFrom = vi.fn().mockReturnValue({ where: mockWhere })
      vi.mocked(db).select = vi.fn().mockReturnValue({ from: mockFrom })

      const result = await getMessageFeedback(messageId)

      expect(result).toBeNull()
    })

    it('should return null when no feedback score exists', async () => {
      const messageId = 'test-message-id'

      // Mock database response without feedbackScore
      const mockLimit = vi.fn().mockResolvedValue([
        {
          metadata: {}
        }
      ])
      const mockWhere = vi.fn().mockReturnValue({ limit: mockLimit })
      const mockFrom = vi.fn().mockReturnValue({ where: mockWhere })
      vi.mocked(db).select = vi.fn().mockReturnValue({ from: mockFrom })

      const result = await getMessageFeedback(messageId)

      expect(result).toBeNull()
    })

    it('should handle errors and return null', async () => {
      const messageId = 'test-message-id'

      // Mock database error
      const mockLimit = vi.fn().mockRejectedValue(new Error('Database error'))
      const mockWhere = vi.fn().mockReturnValue({ limit: mockLimit })
      const mockFrom = vi.fn().mockReturnValue({ where: mockWhere })
      vi.mocked(db).select = vi.fn().mockReturnValue({ from: mockFrom })

      const result = await getMessageFeedback(messageId)

      expect(result).toBeNull()
    })
  })
})