import { filterConversationResponse, filterFeedbackByPageResponse, filterFeedbackResponse, filterSearchResponse, } from '../../src/analytics/filters.js'; import type { ConversationResponse, FeedbackByPageResponse, FeedbackResponse, SearchResponse, } from '../../src/analytics/types.js'; describe('analytics response filters', () => { it('returns a search response whose JSON rows match query and page filters', () => { const data: SearchResponse = { searches: [ { searchQuery: 'Deploy a site', hits: 5, ctr: 40, topClickedPage: '/deploy', lastSearchedAt: '2024-01-01T00:00:00Z', }, { searchQuery: 'Deploy locally', hits: 3, ctr: 20, topClickedPage: '/development', lastSearchedAt: '2024-01-02T00:00:00Z', }, { searchQuery: 'Authentication', hits: 2, ctr: 10, topClickedPage: '/auth', lastSearchedAt: '2024-01-03T00:00:00Z', }, ], totalSearches: 10, nextCursor: null, }; expect(filterSearchResponse(data, { query: 'deploy', page: '/deploy' }).searches).toEqual([ data.searches[0], ]); }); it('filters grouped feedback in the returned response', () => { const data: FeedbackByPageResponse = { feedback: [ { path: '/guide', thumbsUp: 1, thumbsDown: 0, code: 0, total: 1 }, { path: '/api', thumbsUp: 0, thumbsDown: 1, code: 0, total: 1 }, ], hasMore: false, }; expect(filterFeedbackByPageResponse(data, '/guide').feedback).toEqual([data.feedback[0]]); }); it('filters individual feedback in the returned response', () => { const data: FeedbackResponse = { feedback: [ { id: '1', path: '/guide', comment: null, createdAt: null, source: 'contextual', status: 'pending', }, { id: '2', path: '/api', comment: null, createdAt: null, source: 'contextual', status: 'pending', }, ], nextCursor: null, hasMore: false, }; expect(filterFeedbackResponse(data, '/guide').feedback).toEqual([data.feedback[0]]); }); it('filters conversations in the returned response', () => { const data: ConversationResponse = { conversations: [ { id: '1', timestamp: '2024-01-01T00:00:00Z', query: 'Guide question', response: 'Answer', sources: [{ title: 'Guide', url: 'https://docs.example.com/guide' }], queryCategory: null, }, { id: '2', timestamp: '2024-01-02T00:00:00Z', query: 'API question', response: 'Answer', sources: [{ title: 'API', url: 'https://docs.example.com/api' }], queryCategory: null, }, ], nextCursor: null, hasMore: false, }; expect(filterConversationResponse(data, '/guide').conversations).toEqual([ data.conversations[0], ]); }); });