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

promptfoo.test.ts 6.0 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
  1. import { fetchWithCache } from '../../src/cache';
  2. import { getEnvString } from '../../src/envars';
  3. import { fetchWithRetries } from '../../src/fetch';
  4. import { getUserEmail } from '../../src/globalConfig/accounts';
  5. import {
  6. PromptfooHarmfulCompletionProvider,
  7. PromptfooChatCompletionProvider,
  8. PromptfooSimulatedUserProvider,
  9. } from '../../src/providers/promptfoo';
  10. jest.mock('../../src/cache');
  11. jest.mock('../../src/envars');
  12. jest.mock('../../src/fetch');
  13. jest.mock('../../src/globalConfig/accounts');
  14. jest.mock('../../src/globalConfig/cloud', () => ({
  15. CloudConfig: class {
  16. isEnabled() {
  17. return false;
  18. }
  19. getApiHost() {
  20. return 'https://api.promptfoo.app';
  21. }
  22. },
  23. }));
  24. describe('PromptfooHarmfulCompletionProvider', () => {
  25. beforeEach(() => {
  26. jest.resetAllMocks();
  27. jest.mocked(getUserEmail).mockReturnValue('test@example.com');
  28. jest.mocked(getEnvString).mockReturnValue('');
  29. });
  30. const options = {
  31. harmCategory: 'test-category',
  32. n: 1,
  33. purpose: 'test-purpose',
  34. };
  35. const provider = new PromptfooHarmfulCompletionProvider(options);
  36. it('should initialize with correct options', () => {
  37. expect(provider.harmCategory).toBe(options.harmCategory);
  38. expect(provider.n).toBe(options.n);
  39. expect(provider.purpose).toBe(options.purpose);
  40. });
  41. it('should return correct id', () => {
  42. expect(provider.id()).toBe('promptfoo:redteam:test-category');
  43. });
  44. it('should return correct string representation', () => {
  45. expect(provider.toString()).toBe(
  46. '[Promptfoo Harmful Completion Provider test-purpose - test-category]',
  47. );
  48. });
  49. it('should handle successful API call', async () => {
  50. const mockResponse = new Response(JSON.stringify({ output: 'test output' }), {
  51. status: 200,
  52. statusText: 'OK',
  53. });
  54. jest.mocked(fetchWithRetries).mockResolvedValue(mockResponse);
  55. const result = await provider.callApi('test prompt');
  56. expect(result).toEqual({ output: ['test output'] });
  57. });
  58. it('should handle API error', async () => {
  59. const mockResponse = new Response('API Error', {
  60. status: 400,
  61. statusText: 'Bad Request',
  62. });
  63. jest.mocked(fetchWithRetries).mockResolvedValue(mockResponse);
  64. const result = await provider.callApi('test prompt');
  65. expect(result.error).toContain('[HarmfulCompletionProvider]');
  66. });
  67. });
  68. describe('PromptfooChatCompletionProvider', () => {
  69. beforeEach(() => {
  70. jest.resetAllMocks();
  71. jest.mocked(getUserEmail).mockReturnValue('test@example.com');
  72. jest.mocked(getEnvString).mockReturnValue('');
  73. });
  74. const options = {
  75. jsonOnly: true,
  76. preferSmallModel: false,
  77. task: 'crescendo' as const,
  78. };
  79. const provider = new PromptfooChatCompletionProvider(options);
  80. it('should return correct id', () => {
  81. expect(provider.id()).toBe('promptfoo:chatcompletion');
  82. });
  83. it('should return correct string representation', () => {
  84. expect(provider.toString()).toBe('[Promptfoo Chat Completion Provider]');
  85. });
  86. it('should handle successful API call', async () => {
  87. const mockResponse = {
  88. data: {
  89. result: 'test result',
  90. tokenUsage: { total: 100 },
  91. },
  92. status: 200,
  93. statusText: 'OK',
  94. cached: false,
  95. };
  96. jest.mocked(fetchWithCache).mockResolvedValue(mockResponse);
  97. const result = await provider.callApi('test prompt');
  98. expect(result).toEqual({
  99. output: 'test result',
  100. tokenUsage: { total: 100 },
  101. });
  102. });
  103. it('should handle missing result', async () => {
  104. const mockResponse = {
  105. data: {
  106. result: null,
  107. },
  108. status: 200,
  109. statusText: 'OK',
  110. cached: false,
  111. };
  112. jest.mocked(fetchWithCache).mockResolvedValue(mockResponse);
  113. const result = await provider.callApi('test prompt');
  114. expect(result.error).toBe('LLM did not return a result, likely refusal');
  115. });
  116. it('should handle API error', async () => {
  117. jest.mocked(fetchWithCache).mockRejectedValue(new Error('API Error'));
  118. const result = await provider.callApi('test prompt');
  119. expect(result.error).toBe('API call error: Error: API Error');
  120. });
  121. });
  122. describe('PromptfooSimulatedUserProvider', () => {
  123. beforeEach(() => {
  124. jest.resetAllMocks();
  125. jest.mocked(getUserEmail).mockReturnValue('test@example.com');
  126. });
  127. const options = {
  128. id: 'test-agent',
  129. instructions: 'test instructions',
  130. };
  131. const provider = new PromptfooSimulatedUserProvider(options);
  132. it('should return correct id', () => {
  133. expect(provider.id()).toBe('test-agent');
  134. });
  135. it('should return default id if not provided', () => {
  136. const defaultProvider = new PromptfooSimulatedUserProvider();
  137. expect(defaultProvider.id()).toBe('promptfoo:agent');
  138. });
  139. it('should return correct string representation', () => {
  140. expect(provider.toString()).toBe('[Promptfoo Agent Provider]');
  141. });
  142. it('should handle successful API call', async () => {
  143. const mockResponse = new Response(
  144. JSON.stringify({
  145. result: 'test result',
  146. tokenUsage: { total: 100 },
  147. }),
  148. {
  149. status: 200,
  150. statusText: 'OK',
  151. },
  152. );
  153. jest.mocked(fetchWithRetries).mockResolvedValue(mockResponse);
  154. const result = await provider.callApi(JSON.stringify([{ role: 'user', content: 'hello' }]));
  155. expect(result).toEqual({
  156. output: 'test result',
  157. tokenUsage: { total: 100 },
  158. });
  159. });
  160. it('should handle API error response', async () => {
  161. const mockResponse = new Response('API Error', {
  162. status: 400,
  163. statusText: 'Bad Request',
  164. });
  165. jest.mocked(fetchWithRetries).mockResolvedValue(mockResponse);
  166. const result = await provider.callApi(JSON.stringify([{ role: 'user', content: 'hello' }]));
  167. expect(result.error).toContain('API call error');
  168. });
  169. it('should handle API call exception', async () => {
  170. jest.mocked(fetchWithRetries).mockRejectedValue(new Error('Network Error'));
  171. const result = await provider.callApi(JSON.stringify([{ role: 'user', content: 'hello' }]));
  172. expect(result.error).toBe('API call error: Error: Network Error');
  173. });
  174. });
Tip!

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

Comments

Loading...