Skip to content

An AI-powered search engine with a generative UI

test: add comprehensive tests for feedback functionality

Yoshiki Miura1b0a023efd35
diff --git a/app/api/feedback/__tests__/route.test.ts b/app/api/feedback/__tests__/route.test.ts
new file mode 100644
index 00000000..92a12059
--- /dev/null
+++ b/app/api/feedback/__tests__/route.test.ts
@@ -0,0 +1,270 @@
+import { beforeEach, describe, expect, it, vi } from 'vitest'
+
+// Mock the modules
+vi.mock('@/lib/actions/feedback', () => ({
+ updateMessageFeedback: vi.fn()
+}))
+
+vi.mock('@/lib/utils/telemetry', () => ({
+ isTracingEnabled: vi.fn(() => false)
+}))
+
+vi.mock('langfuse', () => ({
+ Langfuse: vi.fn(() => ({
+ score: vi.fn(),
+ flushAsync: vi.fn(() => Promise.resolve())
+ }))
+}))
+
+// Import after mocking
+import { updateMessageFeedback } from '@/lib/actions/feedback'
+import { isTracingEnabled } from '@/lib/utils/telemetry'
+import { Langfuse } from 'langfuse'
+
+import { POST } from '../route'
+
+describe('Feedback API Route', () => {
+ beforeEach(() => {
+ vi.clearAllMocks()
+ })
+
+ describe('POST /api/feedback', () => {
+ it('should record feedback successfully', async () => {
+ vi.mocked(isTracingEnabled).mockReturnValue(true)
+ vi.mocked(updateMessageFeedback).mockResolvedValue({
+ success: true
+ })
+
+ const mockScore = vi.fn()
+ const mockFlush = vi.fn().mockResolvedValue(undefined)
+ vi.mocked(Langfuse).mockImplementation(
+ () =>
+ ({
+ score: mockScore,
+ flushAsync: mockFlush
+ }) as any
+ )
+
+ const request = new Request('http://localhost:3000/api/feedback', {
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/json'
+ },
+ body: JSON.stringify({
+ traceId: 'test-trace-id',
+ score: 1,
+ comment: 'Great!',
+ messageId: 'test-message-id'
+ })
+ })
+
+ const response = await POST(request)
+ const text = await response.text()
+
+ expect(response.status).toBe(200)
+ expect(text).toBe('Feedback recorded successfully')
+ expect(mockScore).toHaveBeenCalledWith({
+ traceId: 'test-trace-id',
+ name: 'user_feedback',
+ value: 1,
+ comment: 'Great!'
+ })
+ expect(mockFlush).toHaveBeenCalled()
+ expect(updateMessageFeedback).toHaveBeenCalledWith('test-message-id', 1)
+ })
+
+ it('should handle negative feedback', async () => {
+ vi.mocked(isTracingEnabled).mockReturnValue(true)
+ vi.mocked(updateMessageFeedback).mockResolvedValue({
+ success: true
+ })
+
+ const mockScore = vi.fn()
+ const mockFlush = vi.fn().mockResolvedValue(undefined)
+ vi.mocked(Langfuse).mockImplementation(
+ () =>
+ ({
+ score: mockScore,
+ flushAsync: mockFlush
+ }) as any
+ )
+
+ const request = new Request('http://localhost:3000/api/feedback', {
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/json'
+ },
+ body: JSON.stringify({
+ traceId: 'test-trace-id',
+ score: -1,
+ messageId: 'test-message-id'
+ })
+ })
+
+ const response = await POST(request)
+
+ expect(response.status).toBe(200)
+ expect(mockScore).toHaveBeenCalledWith({
+ traceId: 'test-trace-id',
+ name: 'user_feedback',
+ value: -1,
+ comment: undefined
+ })
+ })
+
+ it('should return 400 for missing traceId', async () => {
+ const request = new Request('http://localhost:3000/api/feedback', {
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/json'
+ },
+ body: JSON.stringify({
+ score: 1,
+ messageId: 'test-message-id'
+ })
+ })
+
+ const response = await POST(request)
+ const text = await response.text()
+
+ expect(response.status).toBe(400)
+ expect(text).toBe('traceId is required')
+ })
+
+ it('should return 400 for invalid score', async () => {
+ const request = new Request('http://localhost:3000/api/feedback', {
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/json'
+ },
+ body: JSON.stringify({
+ traceId: 'test-trace-id',
+ score: 0,
+ messageId: 'test-message-id'
+ })
+ })
+
+ const response = await POST(request)
+ const text = await response.text()
+
+ expect(response.status).toBe(400)
+ expect(text).toBe('score must be 1 (good) or -1 (bad)')
+ })
+
+ it('should return 200 when tracing is disabled', async () => {
+ vi.mocked(isTracingEnabled).mockReturnValue(false)
+
+ const request = new Request('http://localhost:3000/api/feedback', {
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/json'
+ },
+ body: JSON.stringify({
+ traceId: 'test-trace-id',
+ score: 1,
+ messageId: 'test-message-id'
+ })
+ })
+
+ const response = await POST(request)
+ const text = await response.text()
+
+ expect(response.status).toBe(200)
+ expect(text).toBe('Feedback tracking is not enabled')
+ expect(updateMessageFeedback).not.toHaveBeenCalled()
+ })
+
+ it('should continue even if database update fails', async () => {
+ vi.mocked(isTracingEnabled).mockReturnValue(true)
+ vi.mocked(updateMessageFeedback).mockResolvedValue({
+ success: false,
+ error: 'Database error'
+ })
+
+ const mockScore = vi.fn()
+ const mockFlush = vi.fn().mockResolvedValue(undefined)
+ vi.mocked(Langfuse).mockImplementation(
+ () =>
+ ({
+ score: mockScore,
+ flushAsync: mockFlush
+ }) as any
+ )
+
+ const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation()
+
+ const request = new Request('http://localhost:3000/api/feedback', {
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/json'
+ },
+ body: JSON.stringify({
+ traceId: 'test-trace-id',
+ score: 1,
+ messageId: 'test-message-id'
+ })
+ })
+
+ const response = await POST(request)
+
+ expect(response.status).toBe(200)
+ expect(consoleErrorSpy).toHaveBeenCalledWith(
+ 'Error updating message feedback:',
+ 'Database error'
+ )
+
+ consoleErrorSpy.mockRestore()
+ })
+
+ it('should work without messageId', async () => {
+ vi.mocked(isTracingEnabled).mockReturnValue(true)
+
+ const mockScore = vi.fn()
+ const mockFlush = vi.fn().mockResolvedValue(undefined)
+ vi.mocked(Langfuse).mockImplementation(
+ () =>
+ ({
+ score: mockScore,
+ flushAsync: mockFlush
+ }) as any
+ )
+
+ const request = new Request('http://localhost:3000/api/feedback', {
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/json'
+ },
+ body: JSON.stringify({
+ traceId: 'test-trace-id',
+ score: 1
+ })
+ })
+
+ const response = await POST(request)
+
+ expect(response.status).toBe(200)
+ expect(updateMessageFeedback).not.toHaveBeenCalled()
+ })
+
+ it('should handle JSON parsing errors', async () => {
+ const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation()
+
+ const request = new Request('http://localhost:3000/api/feedback', {
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/json'
+ },
+ body: 'invalid json'
+ })
+
+ const response = await POST(request)
+ const text = await response.text()
+
+ expect(response.status).toBe(500)
+ expect(text).toBe('Error recording feedback')
+ expect(consoleErrorSpy).toHaveBeenCalled()
+
+ consoleErrorSpy.mockRestore()
+ })
+ })
+})
\ No newline at end of file
diff --git a/lib/actions/__tests__/feedback.test.ts b/lib/actions/__tests__/feedback.test.ts
new file mode 100644
index 00000000..76c0cccd
--- /dev/null
+++ b/lib/actions/__tests__/feedback.test.ts
@@ -0,0 +1,210 @@
+import { beforeEach, describe, expect, it, vi } from 'vitest'
+
+// Mock the modules before any imports
+vi.mock('@/lib/db')
+vi.mock('@/lib/cache/memory-cache')
+vi.mock('langfuse')
+vi.mock('@/lib/utils/telemetry')
+
+// Import after mocking
+import { chatCache } from '@/lib/cache/memory-cache'
+import { db } from '@/lib/db'
+import { isTracingEnabled } from '@/lib/utils/telemetry'
+import { Langfuse } from 'langfuse'
+
+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 })
+
+ // Mock cache
+ vi.mocked(chatCache).deletePattern = vi.fn()
+
+ // Mock tracing disabled
+ vi.mocked(isTracingEnabled).mockReturnValue(false)
+
+ const result = await updateMessageFeedback(messageId, score)
+
+ expect(result).toEqual({ success: true })
+ expect(db.select).toHaveBeenCalled()
+ expect(db.update).toHaveBeenCalled()
+ expect(chatCache.deletePattern).toHaveBeenCalledWith(`${chatId}-`)
+ })
+
+ 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 send feedback to Langfuse when tracing is enabled', async () => {
+ const messageId = 'test-message-id'
+ const chatId = 'test-chat-id'
+ const score = 1
+
+ // Enable tracing
+ vi.mocked(isTracingEnabled).mockReturnValue(true)
+
+ // Mock Langfuse
+ const mockScore = vi.fn()
+ const mockFlush = vi.fn().mockResolvedValue(undefined)
+ vi.mocked(Langfuse).mockImplementation(
+ () =>
+ ({
+ score: mockScore,
+ flushAsync: mockFlush
+ }) as any
+ )
+
+ // 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 })
+
+ // Mock cache
+ vi.mocked(chatCache).deletePattern = vi.fn()
+
+ const result = await updateMessageFeedback(messageId, score)
+
+ expect(result).toEqual({ success: true })
+ expect(Langfuse).toHaveBeenCalled()
+ expect(mockScore).toHaveBeenCalledWith({
+ traceId: 'test-trace-id',
+ name: 'user-feedback',
+ value: score,
+ comment: 'Thumbs up'
+ })
+ expect(mockFlush).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()
+ })
+ })
+})
\ No newline at end of file