Skip to content

Commit 6ff4df7

Browse files
amirai21asafgardin
authored andcommitted
feat: basic unit tests for rag engine
1 parent a628702 commit 6ff4df7

File tree

2 files changed

+208
-0
lines changed

2 files changed

+208
-0
lines changed

tests/unittests/resources/chat/conversationalRag.test.ts renamed to tests/unittests/resources/chat/conversational-rag.test.ts

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
import * as Models from '../../../../src/types';
22
import {ConversationalRag} from "../../../../src/resources/rag/conversationalRag";
33
import { APIClient } from '../../../../src/APIClient';
4+
import { RAGEngine } from '../../../../src/resources/rag/ragEngine';
45

56

67
class MockAPIClient extends APIClient {
78
public post = jest.fn();
9+
public upload = jest.fn();
10+
public get = jest.fn();
11+
public delete = jest.fn();
12+
public put = jest.fn();
813
}
914

1015

@@ -96,3 +101,99 @@ describe('ConversationalRag', () => {
96101
expect(response).toEqual(expectedResponse);
97102
});
98103
});
104+
105+
describe('RAGEngine', () => {
106+
let ragEngine: RAGEngine;
107+
let mockClient: MockAPIClient;
108+
const dummyAPIKey = "test-api-key";
109+
110+
beforeEach(() => {
111+
mockClient = new MockAPIClient({
112+
baseURL: 'https://api.example.com',
113+
maxRetries: 3,
114+
timeout: 5000,
115+
});
116+
117+
ragEngine = new RAGEngine(mockClient);
118+
});
119+
120+
afterEach(() => {
121+
jest.clearAllMocks();
122+
});
123+
124+
it('should upload a file and return the response', async () => {
125+
const fileInput = 'path/to/file.txt';
126+
const body = { path: 'label' };
127+
const expectedResponse = { fileId: '12345' };
128+
129+
mockClient.upload.mockResolvedValue(expectedResponse);
130+
131+
const response = await ragEngine.create(fileInput, body);
132+
133+
expect(mockClient.upload).toHaveBeenCalledWith(
134+
'/library/files',
135+
fileInput,
136+
{ body, headers: undefined }
137+
);
138+
expect(response).toEqual(expectedResponse);
139+
});
140+
141+
it('should get a file by ID and return the response', async () => {
142+
const fileId = '12345';
143+
const expectedResponse = { id: fileId, name: 'file.txt' };
144+
145+
mockClient.get.mockResolvedValue(expectedResponse);
146+
147+
const response = await ragEngine.get(fileId);
148+
149+
expect(mockClient.get).toHaveBeenCalledWith(
150+
`/library/files/${fileId}`,
151+
undefined
152+
);
153+
expect(response).toEqual(expectedResponse);
154+
});
155+
156+
it('should delete a file by ID', async () => {
157+
const fileId = '12345';
158+
159+
mockClient.delete.mockResolvedValue(null);
160+
161+
const response = await ragEngine.delete(fileId);
162+
163+
expect(mockClient.delete).toHaveBeenCalledWith(
164+
`/library/files/${fileId}`,
165+
undefined
166+
);
167+
expect(response).toBeNull();
168+
});
169+
170+
it('should update a file by ID and return null', async () => {
171+
const fileId = '12345';
172+
const body = { labels: ['test'], publicUrl: 'https://example.com' };
173+
174+
mockClient.put.mockResolvedValue(null);
175+
176+
const response = await ragEngine.update(fileId, body);
177+
178+
expect(mockClient.put).toHaveBeenCalledWith(
179+
`/library/files/${fileId}`,
180+
{ body, headers: undefined }
181+
);
182+
expect(response).toBeNull();
183+
});
184+
185+
it('should list files and return the response', async () => {
186+
const filters = { limit: 4 };
187+
const expectedResponse = [{ id: '12345', name: 'file.txt' }];
188+
189+
mockClient.get.mockResolvedValue(expectedResponse);
190+
191+
const response = await ragEngine.list(filters);
192+
193+
expect(mockClient.get).toHaveBeenCalledWith(
194+
'/library/files',
195+
{ query: filters, headers: undefined }
196+
);
197+
expect(response).toEqual(expectedResponse);
198+
});
199+
});
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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

Comments
 (0)