|
| 1 | +/** @jest-environment node */ |
| 2 | + |
| 3 | +import { retrieveContext } from '@datalib/hackbot/getHackbotContext'; |
| 4 | +import { getDatabase } from '@utils/mongodb/mongoClient.mjs'; |
| 5 | +import { embedText } from '@utils/hackbot/embedText'; |
| 6 | +import { retryWithBackoff } from '@utils/hackbot/retryWithBackoff'; |
| 7 | + |
| 8 | +jest.mock('@utils/mongodb/mongoClient.mjs', () => ({ |
| 9 | + getDatabase: jest.fn(), |
| 10 | +})); |
| 11 | + |
| 12 | +jest.mock('@utils/hackbot/embedText', () => ({ |
| 13 | + embedText: jest.fn(), |
| 14 | +})); |
| 15 | + |
| 16 | +jest.mock('@utils/hackbot/retryWithBackoff', () => ({ |
| 17 | + retryWithBackoff: jest.fn(), |
| 18 | +})); |
| 19 | + |
| 20 | +const mockGetDatabase = getDatabase as jest.MockedFunction<typeof getDatabase>; |
| 21 | +const mockEmbedText = embedText as jest.MockedFunction<typeof embedText>; |
| 22 | +const mockRetryWithBackoff = retryWithBackoff as jest.MockedFunction< |
| 23 | + typeof retryWithBackoff |
| 24 | +>; |
| 25 | + |
| 26 | +describe('retrieveContext', () => { |
| 27 | + const aggregateToArray = jest.fn(); |
| 28 | + const aggregate = jest.fn(() => ({ toArray: aggregateToArray })); |
| 29 | + const collection = jest.fn(() => ({ aggregate })); |
| 30 | + |
| 31 | + beforeEach(() => { |
| 32 | + jest.clearAllMocks(); |
| 33 | + |
| 34 | + mockRetryWithBackoff.mockImplementation(async (operation: any) => |
| 35 | + operation() |
| 36 | + ); |
| 37 | + mockEmbedText.mockResolvedValue([0.1, 0.2, 0.3]); |
| 38 | + mockGetDatabase.mockResolvedValue({ collection } as any); |
| 39 | + aggregateToArray.mockResolvedValue([ |
| 40 | + { |
| 41 | + _id: 'doc-1', |
| 42 | + type: 'general', |
| 43 | + title: 'Doc 1', |
| 44 | + text: 'Some useful context', |
| 45 | + url: 'https://example.com', |
| 46 | + }, |
| 47 | + ]); |
| 48 | + }); |
| 49 | + |
| 50 | + it('uses adaptive simple limit for greetings', async () => { |
| 51 | + await retrieveContext('hello'); |
| 52 | + |
| 53 | + const pipeline = aggregate.mock.calls[0][0]; |
| 54 | + expect(pipeline[0].$vectorSearch.limit).toBe(5); |
| 55 | + }); |
| 56 | + |
| 57 | + it('uses adaptive complex limit for schedule/list queries', async () => { |
| 58 | + await retrieveContext('show me all events this weekend'); |
| 59 | + |
| 60 | + const pipeline = aggregate.mock.calls[0][0]; |
| 61 | + expect(pipeline[0].$vectorSearch.limit).toBe(30); |
| 62 | + }); |
| 63 | + |
| 64 | + it('honors explicit limit when provided', async () => { |
| 65 | + await retrieveContext('what is hacking', { limit: 7 }); |
| 66 | + |
| 67 | + const pipeline = aggregate.mock.calls[0][0]; |
| 68 | + expect(pipeline[0].$vectorSearch.limit).toBe(7); |
| 69 | + }); |
| 70 | + |
| 71 | + it('adds preferredTypes filter when provided', async () => { |
| 72 | + await retrieveContext('schedule', { |
| 73 | + preferredTypes: ['schedule', 'general'] as any, |
| 74 | + }); |
| 75 | + |
| 76 | + const pipeline = aggregate.mock.calls[0][0]; |
| 77 | + expect(pipeline[0].$vectorSearch.filter).toEqual({ |
| 78 | + type: { $in: ['schedule', 'general'] }, |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + it('projects only fields needed by downstream code', async () => { |
| 83 | + await retrieveContext('where is check-in?'); |
| 84 | + |
| 85 | + const pipeline = aggregate.mock.calls[0][0]; |
| 86 | + expect(pipeline[1]).toEqual({ |
| 87 | + $project: { |
| 88 | + _id: 1, |
| 89 | + type: 1, |
| 90 | + title: 1, |
| 91 | + text: 1, |
| 92 | + url: 1, |
| 93 | + }, |
| 94 | + }); |
| 95 | + }); |
| 96 | +}); |
0 commit comments