|
| 1 | +import * as Models from '../../../src/types'; |
| 2 | +import { RAGEngine } from '../../../src/resources/rag/ragEngine'; |
| 3 | +import { APIClient } from '../../../src/APIClient'; |
| 4 | + |
| 5 | +class MockAPIClient extends APIClient { |
| 6 | + public upload = jest.fn(); |
| 7 | + public get = jest.fn(); |
| 8 | + public delete = jest.fn(); |
| 9 | + public put = jest.fn(); |
| 10 | +} |
| 11 | + |
| 12 | +describe('RAGEngine', () => { |
| 13 | + let ragEngine: RAGEngine; |
| 14 | + let mockClient: MockAPIClient; |
| 15 | + const dummyAPIKey = "test-api-key"; |
| 16 | + const options: Models.RequestOptions = { headers: { 'Authorization': `Bearer ${dummyAPIKey}` } }; |
| 17 | + |
| 18 | + beforeEach(() => { |
| 19 | + mockClient = new MockAPIClient({ |
| 20 | + baseURL: 'https://api.example.com', |
| 21 | + maxRetries: 3, |
| 22 | + timeout: 5000, |
| 23 | + }); |
| 24 | + |
| 25 | + ragEngine = new RAGEngine(mockClient); |
| 26 | + }); |
| 27 | + |
| 28 | + afterEach(() => { |
| 29 | + jest.clearAllMocks(); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should upload a file and return the fileId', async () => { |
| 33 | + const fileInput = 'path/to/file.txt'; |
| 34 | + const body = { path: 'label' }; |
| 35 | + const expectedResponse = { fileId: '12345' }; |
| 36 | + |
| 37 | + mockClient.upload.mockResolvedValue(expectedResponse); |
| 38 | + |
| 39 | + const response = await ragEngine.create(fileInput, body, options); |
| 40 | + |
| 41 | + expect(mockClient.upload).toHaveBeenCalledWith( |
| 42 | + '/library/files', |
| 43 | + fileInput, |
| 44 | + { body, headers: { 'Authorization': `Bearer ${dummyAPIKey}` } } |
| 45 | + ); |
| 46 | + expect(response).toEqual(expectedResponse); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should get a file by ID and return the response', async () => { |
| 50 | + const fileId = '12345'; |
| 51 | + const expectedResponse = { id: fileId, name: 'file.txt' }; |
| 52 | + |
| 53 | + mockClient.get.mockResolvedValue(expectedResponse); |
| 54 | + |
| 55 | + const response = await ragEngine.get(fileId, options); |
| 56 | + |
| 57 | + expect(mockClient.get).toHaveBeenCalledWith( |
| 58 | + `/library/files/${fileId}`, |
| 59 | + { headers: { 'Authorization': `Bearer ${dummyAPIKey}` } } |
| 60 | + ); |
| 61 | + expect(response).toEqual(expectedResponse); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should delete a file by ID', async () => { |
| 65 | + const fileId = '12345'; |
| 66 | + |
| 67 | + mockClient.delete.mockResolvedValue(null); |
| 68 | + |
| 69 | + const response = await ragEngine.delete(fileId, options); |
| 70 | + |
| 71 | + expect(mockClient.delete).toHaveBeenCalledWith( |
| 72 | + `/library/files/${fileId}`, |
| 73 | + { headers: { 'Authorization': `Bearer ${dummyAPIKey}` } } |
| 74 | + ); |
| 75 | + expect(response).toBeNull(); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should update a file by ID and return null', async () => { |
| 79 | + const fileId = '12345'; |
| 80 | + const body = { labels: ['test'], publicUrl: 'https://example.com' }; |
| 81 | + |
| 82 | + mockClient.put.mockResolvedValue(null); |
| 83 | + |
| 84 | + const response = await ragEngine.update(fileId, body, options); |
| 85 | + |
| 86 | + expect(mockClient.put).toHaveBeenCalledWith( |
| 87 | + `/library/files/${fileId}`, |
| 88 | + { body, headers: { 'Authorization': `Bearer ${dummyAPIKey}` } } |
| 89 | + ); |
| 90 | + expect(response).toBeNull(); |
| 91 | + }); |
| 92 | + |
| 93 | + it('should list files and return the response', async () => { |
| 94 | + const filters = { limit: 4 }; |
| 95 | + const expectedResponse = [{ id: '12345', name: 'file.txt' }]; |
| 96 | + |
| 97 | + mockClient.get.mockResolvedValue(expectedResponse); |
| 98 | + |
| 99 | + const response = await ragEngine.list(filters, options); |
| 100 | + |
| 101 | + expect(mockClient.get).toHaveBeenCalledWith( |
| 102 | + '/library/files', |
| 103 | + { query: filters, headers: { 'Authorization': `Bearer ${dummyAPIKey}` } } |
| 104 | + ); |
| 105 | + expect(response).toEqual(expectedResponse); |
| 106 | + }); |
| 107 | +}); |
0 commit comments