Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

utils.test.ts 8.7 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
  1. import * as fs from 'fs';
  2. import * as path from 'path';
  3. import {
  4. coerceString,
  5. getFinalTest,
  6. loadFromJavaScriptFile,
  7. processFileReference,
  8. } from '../../src/assertions/utils';
  9. import cliState from '../../src/cliState';
  10. import { importModule } from '../../src/esm';
  11. import type { ApiProvider, Assertion, TestCase } from '../../src/types';
  12. jest.mock('fs');
  13. jest.mock('path');
  14. jest.mock('../../src/cliState');
  15. jest.mock('../../src/esm', () => ({
  16. importModule: jest.fn(),
  17. }));
  18. describe('processFileReference', () => {
  19. beforeEach(() => {
  20. jest.resetAllMocks();
  21. cliState.basePath = '/base/path';
  22. });
  23. it('should handle undefined basePath', () => {
  24. cliState.basePath = undefined;
  25. const jsonContent = JSON.stringify({ key: 'value' });
  26. jest.mocked(fs.readFileSync).mockReturnValue(jsonContent);
  27. jest.mocked(path.resolve).mockReturnValue('/test.json');
  28. jest.mocked(path.extname).mockReturnValue('.json');
  29. const result = processFileReference('file://test.json');
  30. expect(result).toEqual({ key: 'value' });
  31. expect(fs.readFileSync).toHaveBeenCalledWith('/test.json', 'utf8');
  32. });
  33. it('should process JSON files correctly', () => {
  34. const jsonContent = JSON.stringify({ key: 'value' });
  35. jest.mocked(fs.readFileSync).mockReturnValue(jsonContent);
  36. jest.mocked(path.resolve).mockReturnValue('/base/path/test.json');
  37. jest.mocked(path.extname).mockReturnValue('.json');
  38. const result = processFileReference('file://test.json');
  39. expect(result).toEqual({ key: 'value' });
  40. expect(fs.readFileSync).toHaveBeenCalledWith('/base/path/test.json', 'utf8');
  41. });
  42. it('should process YAML files correctly', () => {
  43. const yamlContent = 'key: value';
  44. jest.mocked(fs.readFileSync).mockReturnValue(yamlContent);
  45. jest.mocked(path.resolve).mockReturnValue('/base/path/test.yaml');
  46. jest.mocked(path.extname).mockReturnValue('.yaml');
  47. const result = processFileReference('file://test.yaml');
  48. expect(result).toEqual({ key: 'value' });
  49. expect(fs.readFileSync).toHaveBeenCalledWith('/base/path/test.yaml', 'utf8');
  50. });
  51. it('should process YML files correctly', () => {
  52. const yamlContent = 'key: value';
  53. jest.mocked(fs.readFileSync).mockReturnValue(yamlContent);
  54. jest.mocked(path.resolve).mockReturnValue('/base/path/test.yml');
  55. jest.mocked(path.extname).mockReturnValue('.yml');
  56. const result = processFileReference('file://test.yml');
  57. expect(result).toEqual({ key: 'value' });
  58. expect(fs.readFileSync).toHaveBeenCalledWith('/base/path/test.yml', 'utf8');
  59. });
  60. it('should process TXT files correctly', () => {
  61. const txtContent = 'plain text content\n';
  62. jest.mocked(fs.readFileSync).mockReturnValue(txtContent);
  63. jest.mocked(path.resolve).mockReturnValue('/base/path/test.txt');
  64. jest.mocked(path.extname).mockReturnValue('.txt');
  65. const result = processFileReference('file://test.txt');
  66. expect(result).toBe('plain text content');
  67. expect(fs.readFileSync).toHaveBeenCalledWith('/base/path/test.txt', 'utf8');
  68. });
  69. it('should throw an error for unsupported file types', () => {
  70. jest.mocked(path.resolve).mockReturnValue('/base/path/test.unsupported');
  71. jest.mocked(path.extname).mockReturnValue('.unsupported');
  72. expect(() => processFileReference('file://test.unsupported')).toThrow('Unsupported file type');
  73. });
  74. });
  75. describe('coerceString', () => {
  76. it('should return string as is when input is a string', () => {
  77. const input = 'hello world';
  78. expect(coerceString(input)).toBe(input);
  79. });
  80. it('should convert object to JSON string', () => {
  81. const input = { key: 'value', nested: { foo: 'bar' } };
  82. expect(coerceString(input)).toBe(JSON.stringify(input));
  83. });
  84. it('should convert array to JSON string', () => {
  85. const input = [1, 2, { key: 'value' }];
  86. expect(coerceString(input)).toBe(JSON.stringify(input));
  87. });
  88. it('should handle empty object', () => {
  89. const input = {};
  90. expect(coerceString(input)).toBe('{}');
  91. });
  92. });
  93. describe('getFinalTest', () => {
  94. beforeEach(() => {
  95. jest.clearAllMocks();
  96. });
  97. const createMockProvider = (id: string): ApiProvider => ({
  98. id: () => id,
  99. callApi: jest.fn(),
  100. });
  101. it('should correctly merge test and assertion data', () => {
  102. const mockApiProvider = createMockProvider('mockProvider');
  103. const testCase: TestCase = {
  104. vars: { var1: 'value1' },
  105. options: {
  106. provider: createMockProvider('testProvider'),
  107. },
  108. };
  109. const assertion: Assertion = {
  110. type: 'equals',
  111. value: 'expected value',
  112. provider: mockApiProvider,
  113. rubricPrompt: 'custom rubric prompt',
  114. };
  115. const result = getFinalTest(testCase, assertion);
  116. expect(Object.isFrozen(result)).toBe(true);
  117. expect(result.options?.provider).toBe(mockApiProvider);
  118. expect(result.options?.rubricPrompt).toBe('custom rubric prompt');
  119. expect(result.vars).toEqual({ var1: 'value1' });
  120. });
  121. it('should use test provider when assertion provider is not provided', () => {
  122. const testProvider = createMockProvider('testProvider');
  123. const testCase: TestCase = {
  124. options: {
  125. provider: testProvider,
  126. },
  127. };
  128. const assertion: Assertion = {
  129. type: 'equals',
  130. value: 'expected value',
  131. };
  132. const result = getFinalTest(testCase, assertion);
  133. expect(result.options?.provider).toBe(testProvider);
  134. });
  135. it('should handle test with direct provider property', () => {
  136. const testProvider = createMockProvider('testProvider');
  137. const assertionProvider = createMockProvider('assertionProvider');
  138. const testCase: TestCase = {
  139. provider: testProvider,
  140. };
  141. const assertion: Assertion = {
  142. type: 'equals',
  143. value: 'expected value',
  144. provider: assertionProvider,
  145. };
  146. const result = getFinalTest(testCase, assertion);
  147. expect(result.provider).toBe(testProvider);
  148. expect(result.options?.provider).toBe(assertionProvider);
  149. });
  150. it('should handle undefined providers correctly', () => {
  151. const testCase: TestCase = {
  152. vars: { test: 'value' },
  153. options: {},
  154. };
  155. const assertion: Assertion = {
  156. type: 'equals',
  157. value: 'expected value',
  158. };
  159. const result = getFinalTest(testCase, assertion);
  160. expect(result.options?.provider).toBeUndefined();
  161. expect(result.provider).toBeUndefined();
  162. });
  163. it('should handle both provider in options and direct provider', () => {
  164. const optionsProvider = createMockProvider('optionsProvider');
  165. const directProvider = createMockProvider('directProvider');
  166. const assertionProvider = createMockProvider('assertionProvider');
  167. const testCase: TestCase = {
  168. provider: directProvider,
  169. options: {
  170. provider: optionsProvider,
  171. },
  172. };
  173. const assertion: Assertion = {
  174. type: 'equals',
  175. value: 'expected value',
  176. provider: assertionProvider,
  177. };
  178. const result = getFinalTest(testCase, assertion);
  179. expect(result.provider).toBe(directProvider);
  180. expect(result.options?.provider).toBe(assertionProvider);
  181. });
  182. });
  183. describe('loadFromJavaScriptFile', () => {
  184. beforeEach(() => {
  185. jest.resetAllMocks();
  186. });
  187. it('should call named function when functionName is provided', async () => {
  188. const mockFn = jest.fn().mockReturnValue('result');
  189. jest.mocked(importModule).mockResolvedValue({ testFn: mockFn });
  190. const result = await loadFromJavaScriptFile('/test.js', 'testFn', ['arg1', 'arg2']);
  191. expect(result).toBe('result');
  192. expect(mockFn).toHaveBeenCalledWith('arg1', 'arg2');
  193. });
  194. it('should call default function when no functionName provided', async () => {
  195. const mockFn = jest.fn().mockReturnValue('result');
  196. jest.mocked(importModule).mockResolvedValue(mockFn);
  197. const result = await loadFromJavaScriptFile('/test.js', undefined, ['arg1']);
  198. expect(result).toBe('result');
  199. expect(mockFn).toHaveBeenCalledWith('arg1');
  200. });
  201. it('should call default export function when available', async () => {
  202. const mockFn = jest.fn().mockReturnValue('result');
  203. jest.mocked(importModule).mockResolvedValue({ default: mockFn });
  204. const result = await loadFromJavaScriptFile('/test.js', undefined, ['arg1']);
  205. expect(result).toBe('result');
  206. expect(mockFn).toHaveBeenCalledWith('arg1');
  207. });
  208. it('should throw error when module does not export a function', async () => {
  209. jest.mocked(importModule).mockResolvedValue({ notAFunction: 'value' });
  210. await expect(loadFromJavaScriptFile('/test.js', undefined, [])).rejects.toThrow(
  211. 'Assertion malformed',
  212. );
  213. });
  214. it('should throw error when named function does not exist', async () => {
  215. jest.mocked(importModule).mockResolvedValue({ otherFn: () => {} });
  216. await expect(loadFromJavaScriptFile('/test.js', 'nonExistentFn', [])).rejects.toThrow(
  217. 'Assertion malformed',
  218. );
  219. });
  220. });
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...