|
| 1 | +import { enableFetchMocks } from 'jest-fetch-mock'; |
| 2 | +import { Hyper } from '../index'; |
| 3 | + |
| 4 | +enableFetchMocks(); |
| 5 | + |
| 6 | +const hyper = new Hyper('hyper_1234'); |
| 7 | + |
| 8 | +describe('Hypercode Search API methods', () => { |
| 9 | + afterEach(() => fetchMock.resetMocks()); |
| 10 | + |
| 11 | + describe('execute method', () => { |
| 12 | + it('should return the correct search results for a query with contextId', async () => { |
| 13 | + const expectedResult = [ |
| 14 | + { |
| 15 | + metadata: { |
| 16 | + name: 'Quarterly Sales Report Q1 2023', |
| 17 | + source: 'https://enterprise.gethyper.ai/reports/q1-sales-2023', |
| 18 | + type: 'web', |
| 19 | + }, |
| 20 | + page_content: |
| 21 | + 'Quarterly Sales Report Q1 2023\nExecutive Summary\nThe first quarter saw a 17% increase in sales across all sectors. The enterprise software division exceeded its targets with a significant contribution to the overall growth...', |
| 22 | + type: 'Document', |
| 23 | + }, |
| 24 | + { |
| 25 | + metadata: { |
| 26 | + name: 'Market Analysis: Q1 Consumer Trends', |
| 27 | + source: 'https://enterprise.gethyper.ai/analyses/q1-market-trends', |
| 28 | + type: 'web', |
| 29 | + }, |
| 30 | + page_content: |
| 31 | + 'Market Analysis: Q1 Consumer Trends\nAbstract\nThis paper provides an in-depth analysis of consumer trends in the first quarter of 2023, highlighting the shifts in customer behavior...', |
| 32 | + type: 'Document', |
| 33 | + }, |
| 34 | + ]; |
| 35 | + |
| 36 | + const contextId = '123e4567-e89b-12d3-a456-426614174000'; |
| 37 | + |
| 38 | + fetchMock.mockOnce(JSON.stringify(expectedResult), { |
| 39 | + status: 200, |
| 40 | + }); |
| 41 | + |
| 42 | + const result = await hyper.search.execute('quarterly sales report', { |
| 43 | + contextId, |
| 44 | + }); |
| 45 | + |
| 46 | + expect(result.data).toEqual(expectedResult); |
| 47 | + expect(fetchMock).toHaveBeenLastCalledWith( |
| 48 | + expect.any(String), // endpoint |
| 49 | + // body and headers |
| 50 | + expect.objectContaining({ |
| 51 | + body: expect.stringContaining( |
| 52 | + JSON.stringify({ |
| 53 | + query: 'quarterly sales report', |
| 54 | + context_id: contextId, |
| 55 | + }), |
| 56 | + ), |
| 57 | + }), |
| 58 | + ); |
| 59 | + }); |
| 60 | + }); |
| 61 | +}); |
0 commit comments